]> git.ipfire.org Git - people/stevee/network.git/blob - src/network
Merge branch 'master' of git://git.ipfire.org/network
[people/stevee/network.git] / src / network
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 # Parse the command line
23 while [ $# -gt 0 ]; do
24 case "${1}" in
25 -d|--debug)
26 DEBUG=1
27 ;;
28 *)
29 action=${1}
30 ;;
31 esac
32 shift
33 [ -n "${action}" ] && break
34 done
35
36 . /usr/lib/network/functions
37
38 # Read network settings
39 network_settings_read
40
41 cli_settings() {
42 if cli_help_requested $@; then
43 cli_show_man network-settings
44 exit ${EXIT_OK}
45 fi
46
47 if [ -n "${1}" ]; then
48 settings_set $@
49 network_settings_write
50 else
51 network_settings_print
52 fi
53 }
54
55 cli_device() {
56 if cli_help_requested $@; then
57 cli_show_man network-device
58 exit ${EXIT_OK}
59 fi
60
61 local action="${1}"
62 shift
63
64 case "${action}" in
65 list)
66 cli_device_list $@
67 ;;
68 *)
69 local device="${action}"
70 action="${1}"
71 shift
72
73 if ! isset device; then
74 cli_show_man network-device
75 return ${EXIT_ERROR}
76 fi
77
78 assert device_exists ${device}
79
80 case "${action}" in
81 discover)
82 cli_device_discover ${device} $@
83 ;;
84 monitor)
85 cli_device_monitor "${device}" $@
86 ;;
87 status)
88 cli_device_status ${device}
89 ;;
90 unlock)
91 cli_device_serial_unlock ${device} $@
92 ;;
93 ussd)
94 cli_device_send_ussd_command "${device}" $@
95 ;;
96 *)
97 cli_show_man network-device
98 ;;
99 esac
100 ;;
101 esac
102
103 return ${EXIT_OK}
104 }
105
106 cli_device_status() {
107 local device="${1}"
108 assert isset device
109
110 # Disable debugging output here.
111 local log_disable_stdout=${LOG_DISABLE_STDOUT}
112 LOG_DISABLE_STDOUT="true"
113
114 # Save the type of the device for later.
115 local type=$(device_get_type ${device})
116
117 cli_headline 1 "Device status: ${device}"
118 cli_print_fmt1 1 "Name" "${device}"
119
120 # Handle special devices
121 case "${type}" in
122 phy)
123 cli_device_status_phy "${device}"
124 return $?
125 ;;
126 serial)
127 cli_device_status_serial "${device}"
128 return $?
129 ;;
130 esac
131
132 # Print the device status.
133 device_is_up ${device} &>/dev/null
134 local status=$?
135
136 case "${status}" in
137 ${EXIT_TRUE})
138 status="${CLR_GREEN_B}UP${CLR_RESET}"
139 ;;
140 ${EXIT_FALSE})
141 status="${CLR_RED_B}DOWN${CLR_RESET}"
142 ;;
143 esac
144
145 cli_print_fmt1 1 "Status" "${status}"
146 cli_print_fmt1 1 "Type" "${type}"
147
148 # Ethernet-compatible?
149 device_is_ethernet_compatible "${device}" &>/dev/null
150 cli_print_fmt1 1 "Ethernet-compatible" "$(cli_print_bool $?)"
151
152 cli_print_fmt1 1 "Address" "$(device_get_address ${device})"
153 cli_space
154
155 # Print the link speed for ethernet devices.
156 if device_is_up ${device} &>/dev/null; then
157 local link="$(device_get_link_string "${device}")"
158 if isset link; then
159 cli_print_fmt1 1 "Link" "${link}"
160 fi
161 fi
162
163 cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})"
164 cli_space
165
166 # Print device statistics.
167 cli_device_stats 2 ${device}
168
169 # Print some more information.
170 device_has_carrier ${device} &>/dev/null
171 cli_print_fmt1 1 "Has carrier?" "$(cli_print_bool $?)"
172
173 device_is_promisc ${device} &>/dev/null
174 cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)"
175 cli_space
176
177 # Print all vlan devices.
178 local vlans=$(device_get_vlans ${device})
179 if [ -n "${vlans}" ]; then
180 cli_headline 2 "VLAN devices"
181
182 local vlan
183 for vlan in ${vlans}; do
184 cli_print 2 "* %-6s - %s" "${vlan}" "$(device_get_address ${vlan})"
185 done
186 cli_space
187 fi
188
189 case "${type}" in
190 wireless*)
191 local phy="$(device_get_phy "${device}")"
192 if isset phy; then
193 cli_headline 2 "PHY"
194 cli_print_fmt1 2 "Name" "${phy}"
195 cli_print_fmt1 2 "Address" "$(phy_get_address "${phy}")"
196 cli_space
197 fi
198 ;;
199 esac
200
201 # Reset the logging level.
202 LOG_DISABLE_STDOUT=${log_disable_stdout}
203 }
204
205 cli_device_status_serial() {
206 local device=${1}
207 assert device_is_serial ${device}
208
209 serial_is_locked ${device} &>/dev/null
210 local locked=$?
211
212 cli_print_fmt1 1 "Locked" "$(cli_print_bool ${locked})"
213 cli_space
214
215 # Cannot go on when the device is locked.
216 [ ${locked} -eq ${EXIT_TRUE} ] && return ${EXIT_OK}
217
218 cli_print_fmt1 1 "Manufacturer" \
219 "$(modem_get_manufacturer ${device})"
220 cli_print_fmt1 1 "Model" \
221 "$(modem_get_model ${device})"
222 cli_print_fmt1 1 "Software version" \
223 "$(modem_get_software_version ${device})"
224
225 if modem_is_mobile ${device}; then
226 cli_print_fmt1 1 "IMEI" \
227 "$(modem_get_device_imei ${device})"
228 cli_space
229
230 cli_headline 2 "Network status"
231 modem_sim_status ${device} &>/dev/null
232 local sim_status_code=$?
233
234 local sim_status="unknown"
235 case "${sim_status_code}" in
236 ${EXIT_SIM_READY})
237 sim_status="SIM ready"
238 ;;
239 ${EXIT_SIM_PIN})
240 sim_status="PIN locked"
241 ;;
242 ${EXIT_SIM_PUK})
243 sim_status="PUK locked"
244 ;;
245 esac
246 cli_print_fmt1 2 "SIM status" "${sim_status}"
247
248 if [ ${sim_status_code} -eq ${EXIT_SIM_READY} ]; then
249 cli_print_fmt1 2 "IMSI" \
250 "$(modem_get_sim_imsi ${device})"
251 cli_print_fmt1 2 "Operator" \
252 "$(modem_get_network_operator ${device})"
253 cli_print_fmt1 2 "Mode" \
254 "$(modem_get_network_mode ${device})"
255 cli_print_fmt1 2 "Signal quality" \
256 "$(modem_get_signal_quality ${device}) dBm"
257
258 local ber=$(modem_get_bit_error_rate ${device})
259 isset ber || ber="unknown"
260 cli_print_fmt1 2 "Bit Error Rate" "${ber}"
261 fi
262 fi
263 cli_space
264 }
265
266 cli_device_status_phy() {
267 local phy="${1}"
268 assert phy_exists "${phy}"
269
270 local address="$(phy_get_address "${phy}")"
271 cli_print_fmt1 1 "Address" "${address}"
272 cli_space
273
274 local devices="$(phy_get_devices "${phy}")"
275 if isset devices; then
276 cli_headline 2 "Soft interfaces"
277
278 local device
279 for device in ${devices}; do
280 cli_print 2 "* %s" "${device}"
281 done
282 cli_space
283 fi
284
285 return ${EXIT_OK}
286 }
287
288 cli_device_discover() {
289 local device=${1}
290 shift
291
292 # This can only be executed for ethernet (or compatible) devices
293 if ! device_is_ethernet_compatible "${device}"; then
294 return ${EXIT_OK}
295 fi
296
297 local raw
298
299 while [ $# -gt 0 ]; do
300 case "${1}" in
301 --raw)
302 raw=1
303 ;;
304 esac
305 shift
306 done
307
308 local up
309 device_is_up ${device} && up=1
310 device_set_up ${device}
311
312 enabled raw || echo "${device}"
313
314 local hook
315 local out
316 local ret
317 for hook in $(hook_zone_get_all); do
318 out=$(hook_zone_exec ${hook} discover ${device})
319 ret=$?
320
321 [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
322
323 if enabled raw; then
324 case "${ret}" in
325 ${DISCOVER_OK})
326 echo "${hook}: OK"
327 local line
328 while read line; do
329 echo "${hook}: ${line}"
330 done <<<"${out}"
331 ;;
332
333 ${DISCOVER_ERROR})
334 echo "${hook}: FAILED"
335 ;;
336 esac
337 else
338 case "${ret}" in
339 ${DISCOVER_OK})
340 echo " ${hook} was successful."
341 local line
342 while read line; do
343 echo " ${line}"
344 done <<<"${out}"
345 ;;
346
347 ${DISCOVER_ERROR})
348 echo " ${hook} failed."
349 ;;
350 esac
351 fi
352 done
353
354 echo # New line
355
356 [ "${up}" = "1" ] || device_set_down ${device}
357 }
358
359 cli_device_serial_unlock() {
360 if cli_help_requested $@; then
361 cli_show_man network-device
362 exit ${EXIT_OK}
363 fi
364
365 local device=${1}
366 assert isset device
367
368 if ! device_is_serial ${device}; then
369 error "${device} is not a serial device."
370 error "Unlocking is only supported for serial devices."
371 exit ${EXIT_ERROR}
372 fi
373
374 # Read the current state of the SIM card.
375 modem_sim_status ${device} &>/dev/null
376 local sim_status_code=$?
377
378 # If the SIM card is already unlocked, we don't need to do anything.
379 if [ ${sim_status_code} -eq ${EXIT_SIM_READY} ]; then
380 print "The SIM card is already unlocked."
381 exit ${EXIT_OK}
382
383 # If the SIM card is in an unknown state, we cannot do anything.
384 elif [ ${sim_status_code} -eq ${EXIT_SIM_UNKNOWN} ]; then
385 error "The SIM card is in an unknown state."
386 exit ${EXIT_ERROR}
387 fi
388
389 # Ask for the code.
390 local code=${2}
391 local require_new_pin="false"
392 local new_pin
393
394 while ! isinteger code; do
395 local message
396 case "${sim_status_code}" in
397 ${EXIT_SIM_PIN})
398 message="Please enter PIN:"
399 ;;
400 ${EXIT_SIM_PUK})
401 message="Please enter PUK:"
402 require_new_pin="true"
403 ;;
404 esac
405 assert isset message
406
407 echo -n "${message} "
408 read -s code
409 echo # Print newline.
410
411 if enabled require_new_pin; then
412 local i new_pin2
413 for i in 0 1; do
414 case "${i}" in
415 0)
416 message="Please enter a new PIN code:"
417 ;;
418 1)
419 message="Please confirm the new PIN code:"
420 ;;
421 esac
422
423 echo -n "${message} "
424 read -s new_pin2
425 echo # Print newline.
426
427 if [ -n "${new_pin}" ]; then
428 if [ "${new_pin}" != "${new_pin2}" ]; then
429 error "The entered PIN codes did not match."
430 exit ${EXIT_ERROR}
431 fi
432 else
433 new_pin=${new_pin2}
434 fi
435 done
436 fi
437 done
438
439 # Trying to unlock the SIM card.
440 modem_sim_unlock ${device} ${code} ${new_pin}
441
442 exit $?
443 }
444
445 cli_device_send_ussd_command() {
446 local device="${1}"
447 assert isset device
448 shift
449
450 local command
451 local timeout
452
453 while [ $# -gt 0 ]; do
454 case "${1}" in
455 --timeout=*)
456 timeout="$(cli_get_val "${1}")"
457 ;;
458 *)
459 if isset command; then
460 warning "Unrecognized argument: ${1}"
461 else
462 command="${1}"
463 fi
464 ;;
465 esac
466 shift
467 done
468
469 assert device_is_serial "${device}"
470
471 local args
472 if isset timeout; then
473 args="${args} --timeout=${timeout}"
474 fi
475
476 modem_ussd_send_command "${device}" "${command}" ${args}
477 exit $?
478 }
479
480 cli_device_monitor() {
481 local device="${1}"
482 assert isset device
483
484 if ! device_is_wireless "${device}"; then
485 error "This action only works with wireless devices. Exiting."
486 exit ${EXIT_ERROR}
487 fi
488
489 wireless_monitor "${device}"
490 exit $?
491 }
492
493 cli_device_list() {
494 local device
495 for device in $(device_list); do
496 cli_device_status "${device}"
497 done
498
499 exit ${EXIT_OK}
500 }
501
502 cli_hostname() {
503 if cli_help_requested $@; then
504 cli_show_man network
505 exit ${EXIT_OK}
506 fi
507
508 local hostname=${1}
509
510 if [ -n "${hostname}" ]; then
511 config_hostname ${hostname}
512 log INFO "Hostname was set to '${hostname}'."
513 log INFO "Changes do only take affect after reboot."
514 exit ${EXIT_OK}
515 fi
516
517 echo "$(config_hostname)"
518 exit ${EXIT_OK}
519 }
520
521 cli_port() {
522 if cli_help_requested $@; then
523 cli_show_man network-port
524 exit ${EXIT_OK}
525 fi
526
527 local action
528 local port
529
530 if port_exists ${1}; then
531 port=${1}
532 action=${2}
533 shift 2
534
535 case "${action}" in
536 edit|create|remove|up|down|status)
537 port_${action} "${port}" $@
538 ;;
539 *)
540 error "Unrecognized argument: ${action}"
541 exit ${EXIT_ERROR}
542 ;;
543 esac
544 else
545 action=${1}
546 shift
547
548 case "${action}" in
549 new|destroy)
550 port_${action} $@
551 ;;
552 *)
553 error "Unrecognized argument: ${action}"
554 exit ${EXIT_ERROR}
555 ;;
556 esac
557 fi
558 }
559
560 cli_zone() {
561 if cli_help_requested $@; then
562 cli_show_man network-zone
563 exit ${EXIT_OK}
564 fi
565
566 local action
567 local zone
568
569 if zone_name_is_valid ${1}; then
570 zone=${1}
571 action=${2}
572 shift 2
573
574 # Action aliases
575 case "${action}" in
576 start|reload)
577 action="up"
578 ;;
579 stop)
580 action="down"
581 ;;
582 show)
583 action="status"
584 ;;
585 esac
586
587 case "${action}" in
588 port)
589 cli_zone_port "${zone}" $@
590 ;;
591 config|disable|down|edit|enable|status|up)
592 zone_${action} ${zone} $@
593 ;;
594 *)
595 error "Unrecognized argument: ${action}"
596 cli_show_man network-zone
597 exit ${EXIT_ERROR}
598 ;;
599 esac
600 else
601 action=${1}
602 shift
603
604 case "${action}" in
605 new)
606 cli_zone_new $@
607 ;;
608 destroy)
609 cli_zone_destroy $@
610 ;;
611 list-hooks)
612 cli_list_hooks zone $@
613 ;;
614 ""|*)
615 if [ -n "${action}" ]; then
616 error "Unrecognized argument: '${action}'"
617 echo
618 fi
619
620 cli_show_man network-zone
621 exit ${EXIT_ERROR}
622 ;;
623 esac
624 fi
625 }
626
627 cli_zone_new() {
628 if cli_help_requested $@ || [ $# -lt 2 ]; then
629 cli_show_man network-zone-new
630 exit ${EXIT_OK}
631 fi
632
633 zone_new $@
634 }
635
636 # Removes a zone either immediately, if it is currently down,
637 # or adds a tag that the removal will be done when the zone
638 # is brought down the next time.
639 cli_zone_destroy() {
640 if cli_help_requested $@; then
641 cli_show_man network-zone
642 exit ${EXIT_OK}
643 fi
644
645 local zone="${1}"
646 assert zone_exists "${zone}"
647
648 if zone_is_up ${zone}; then
649 echo "Zone '${zone}' is up and will be removed when it goes down the next time."
650 zone_destroy "${zone}"
651 else
652 echo "Removing zone '${zone}' now..."
653 zone_destroy_now "${zone}"
654 fi
655
656 exit ${EXIT_OK}
657 }
658
659 cli_zone_port() {
660 if cli_help_requested $@; then
661 cli_show_man network-zone-port
662 exit ${EXIT_OK}
663 fi
664
665 local zone="${1}"
666 assert zone_exists "${zone}"
667
668 if port_exists "${2}"; then
669 local port="${2}"
670 local action="${3}"
671 shift 3
672
673 case "${action}" in
674 edit)
675 zone_port_edit "${zone}" "${port}" $@
676 ;;
677 *)
678 error "Unrecognised argument: ${action}"
679 exit ${EXIT_ERROR}
680 ;;
681 esac
682 else
683 local action="${2}"
684 shift 2
685
686 case "${action}" in
687 attach)
688 zone_port_attach "${zone}" $@
689 ;;
690 detach)
691 zone_port_detach "${zone}" $@
692 ;;
693 *)
694 error "Unrecognised argument: ${action}"
695 exit ${EXIT_ERROR}
696 ;;
697 esac
698 fi
699
700 exit ${EXIT_OK}
701 }
702
703 cli_list_hooks() {
704 local type=${1}
705 shift
706
707 if cli_help_requested $@; then
708 cli_show_man network-zone
709 exit ${EXIT_OK}
710 fi
711
712 local hook_dir=$(hook_dir ${type})
713 local hook
714
715 for hook in ${hook_dir}/*; do
716 hook=$(basename ${hook})
717 if hook_exists ${type} ${hook}; then
718 echo "${hook}"
719 fi
720 done | sort -u
721 }
722
723 cli_route() {
724 if cli_help_requested $@; then
725 cli_show_man network-route
726 exit ${EXIT_OK}
727 fi
728
729 local action=${1}
730 shift
731
732 case "${action}" in
733 # Add a new route.
734 add)
735 route_add $@
736 ;;
737 # Remove an existing route.
738 remove)
739 route_remove $@
740 ;;
741 # List all routes.
742 list)
743 route_list $@
744 return ${EXIT_OK}
745 ;;
746 *)
747 error "Unrecognized action: ${action}"
748 cli_run_help network route
749
750 exit ${EXIT_ERROR}
751 ;;
752 esac
753
754 # Applying all routes.
755 route_apply
756
757 exit ${EXIT_OK}
758 }
759
760 cli_dhcpd() {
761 local proto=${1}
762 shift
763
764 if cli_help_requested $@; then
765 cli_show_man network-dhcp
766 exit ${EXIT_OK}
767 fi
768
769 local action=${1}
770 shift
771
772 case "${action}" in
773 edit)
774 dhcpd_edit ${proto} $@
775 ;;
776 start)
777 dhcpd_start ${proto}
778 ;;
779 stop)
780 dhcpd_stop ${proto}
781 ;;
782 restart|reload)
783 dhcpd_reload ${proto}
784 ;;
785 subnet)
786 cli_dhcpd_subnet ${proto} $@
787 ;;
788 show|"")
789 cli_dhcpd_show ${proto} $@
790 ;;
791 *)
792 error "Unrecognized action: ${action}"
793 cli_run_help network dhcpvN
794
795 exit ${EXIT_ERROR}
796 ;;
797 esac
798
799 exit ${EXIT_OK}
800 }
801
802 cli_dhcpd_show() {
803 local proto=${1}
804 assert isset proto
805
806 local settings=$(dhcpd_settings ${proto})
807 assert isset settings
808
809 local ${settings}
810 dhcpd_global_settings_read ${proto}
811
812 cli_headline 1 "Dynamic Host Configuration Protocol Daemon for ${proto/ip/IP}"
813
814 case "${proto}" in
815 ipv6)
816 cli_headline 2 "Lease times"
817 if isinteger VALID_LIFETIME; then
818 cli_print_fmt1 2 "Valid lifetime" "${VALID_LIFETIME}s"
819 fi
820
821 if isinteger PREFERRED_LIFETIME; then
822 cli_print_fmt1 2 "Preferred lifetime" "${PREFERRED_LIFETIME}s"
823 fi
824
825 cli_space
826 ;;
827 ipv4)
828 cli_print_fmt1 1 "Authoritative" $(cli_print_enabled AUTHORITATIVE)
829 cli_space
830
831 cli_headline 2 "Lease times"
832 cli_print_fmt1 2 "Default lease time" "${DEFAULT_LEASE_TIME}s"
833 cli_print_fmt1 2 "Max. lease time" "${MAX_LEASE_TIME}s"
834
835 if isset MIN_LEASE_TIME; then
836 cli_print_fmt1 2 "Min. lease time" "${MIN_LEASE_TIME}s"
837 fi
838
839 cli_space
840 ;;
841 esac
842
843 # Read the options.
844 local -A options
845 dhcpd_global_options_read ${proto} ${subnet_id}
846
847 # Print the options if any.
848 if [ ${#options[*]} -gt 0 ]; then
849 cli_headline 2 "Options"
850
851 local option
852 for option in $(dhcpd_options ${proto}); do
853 [ -n "${options[${option}]}" ] || continue
854
855 cli_print_fmt1 2 \
856 "${option}" "${options[${option}]}"
857 done
858 cli_space
859 fi
860
861 # Subnets.
862 local subnets=$(dhcpd_subnet_list ${proto})
863 if [ -n "${subnets}" ]; then
864 cli_headline 2 "Subnets"
865 local subnet_id
866 for subnet_id in ${subnets}; do
867 cli_dhcpd_subnet_show ${proto} ${subnet_id} 2
868 done
869 fi
870 }
871
872 cli_dhcpd_subnet() {
873 local proto=${1}
874 shift
875
876 if cli_help_requested $@; then
877 cli_show_man network-dhcp-subnet
878 exit ${EXIT_OK}
879 fi
880
881 local action=${1}
882 shift
883
884 case "${action}" in
885 new)
886 dhcpd_subnet_new ${proto} $@
887 ;;
888 remove)
889 dhcpd_subnet_remove ${proto} $@
890 ;;
891 [0-9]*)
892 local subnet_id=${action}
893
894 if ! dhcpd_subnet_exists ${proto} ${subnet_id}; then
895 error "The given subnet with ID ${subnet_id} does not exist."
896 return ${EXIT_ERROR}
897 fi
898
899 # Update the action.
900 action=${1}
901 shift
902
903 case "${action}" in
904 edit)
905 dhcpd_subnet_edit ${proto} ${subnet_id} $@
906 local ret=$?
907
908 if [ ${ret} -eq ${EXIT_OK} ]; then
909 dhcpd_reload ${proto}
910 fi
911 exit ${ret}
912 ;;
913 range)
914 cli_dhcpd_subnet_range ${proto} ${subnet_id} $@
915 exit $?
916 ;;
917 show)
918 cli_dhcpd_subnet_show ${proto} ${subnet_id} $@
919 exit $?
920 ;;
921 options)
922 cli_dhcpd_subnet_options ${proto} ${subnet_id} $@
923 exit $?
924 ;;
925 *)
926 error "Unrecognized action: ${action}"
927 cli_run_help network dhcpvN subnet
928 exit ${EXIT_ERROR}
929 ;;
930 esac
931 ;;
932 show)
933 local subnet_id
934 for subnet_id in $(dhcpd_subnet_list ${proto}); do
935 cli_dhcpd_subnet_show ${proto} ${subnet_id}
936 done
937 ;;
938 *)
939 error "Unrecognized action: ${action}"
940 cli_run_help network dhcpvN subnet
941
942 exit ${EXIT_ERROR}
943 ;;
944 esac
945
946 exit ${EXIT_OK}
947 }
948
949 cli_dhcpd_subnet_range() {
950 local proto=${1}
951 assert isset proto
952 shift
953
954 local subnet_id=${1}
955 assert isset subnet_id
956 shift
957
958 local action=${1}
959 shift
960
961 case "${action}" in
962 new)
963 dhcpd_subnet_range_new ${proto} ${subnet_id} $@
964 exit $?
965 ;;
966 remove)
967 dhcpd_subnet_range_remove ${proto} ${subnet_id} $@
968 exit $?
969 ;;
970 *)
971 error "Unrecognized action: ${action}"
972 cli_run_help network dhcpvN subnet range
973 exit ${EXIT_ERROR}
974 ;;
975 esac
976 }
977
978 cli_dhcpd_subnet_show() {
979 local proto=${1}
980 assert isset proto
981
982 local subnet_id=${2}
983 assert isset subnet_id
984
985 local level=${3}
986 isset level || level=0
987
988 local $(dhcpd_subnet_settings ${proto})
989
990 # Read in configuration settings.
991 dhcpd_subnet_read ${proto} ${subnet_id}
992
993 cli_headline $(( ${level} + 1 )) \
994 "DHCP${proto/ip/} subnet declaration #${subnet_id}"
995 cli_print_fmt1 $(( ${level} + 1 )) \
996 "Subnet" "${ADDRESS}/${PREFIX}"
997 cli_space
998
999 # Read the options.
1000 local -A options
1001 dhcpd_subnet_options_read "${proto}" "${subnet_id}"
1002
1003 # Print the options if any.
1004 if [ ${#options[*]} -gt 0 ]; then
1005 cli_headline $(( ${level} + 2 )) "Options"
1006
1007 local option
1008 for option in $(dhcpd_subnet_options_list ${proto}); do
1009 [ -n "${options[${option}]}" ] || continue
1010
1011 cli_print_fmt1 $(( ${level} + 2 )) \
1012 "${option}" "${options[${option}]}"
1013 done
1014 cli_space
1015 fi
1016
1017 # Ranges.
1018 cli_headline $(( ${level} + 2 )) "Ranges"
1019
1020 local ranges=$(dhcpd_subnet_range_list ${proto} ${subnet_id})
1021 if isset ranges; then
1022 local range_id $(dhcpd_subnet_range_settings ${proto})
1023 for range_id in ${ranges}; do
1024 dhcpd_subnet_range_read ${proto} ${subnet_id} ${range_id}
1025
1026 cli_print $(( ${level} + 2 )) \
1027 "#%d: %s - %s" ${range_id} ${START} ${END}
1028 done
1029 else
1030 cli_print $(( ${level} + 2 )) "No ranges have been defined."
1031 fi
1032
1033 cli_space
1034 }
1035
1036 cli_dhcpd_subnet_options() {
1037 local proto=${1}
1038 assert isset proto
1039 shift
1040
1041 local subnet_id=${1}
1042 assert isset subnet_id
1043 shift
1044
1045 local key val
1046 while [ $# -gt 0 ]; do
1047 case "${1}" in
1048 *=*)
1049 key=$(cli_get_key ${1})
1050 val=$(cli_get_val ${1})
1051
1052 dhcpd_subnet_option_set ${proto} ${subnet_id} ${key} ${val}
1053 esac
1054 done
1055 }
1056
1057 cli_start() {
1058 if cli_help_requested $@; then
1059 cli_show_man network
1060 exit ${EXIT_OK}
1061 fi
1062
1063 local zones=$(zones_get $@)
1064
1065 local zone
1066 for zone in ${zones}; do
1067 zone_start ${zone} &
1068 done
1069
1070 wait # until everything is settled
1071 }
1072
1073 cli_stop() {
1074 if cli_help_requested $@; then
1075 cli_show_man network
1076 exit ${EXIT_OK}
1077 fi
1078
1079 local zones=$(zones_get $@)
1080
1081 local zone
1082 for zone in ${zones}; do
1083 zone_stop ${zone} &
1084 done
1085
1086 wait # until everything is settled
1087 }
1088
1089 cli_restart() {
1090 if cli_help_requested $@; then
1091 cli_show_man network
1092 exit ${EXIT_OK}
1093 fi
1094
1095 cli_stop $@
1096
1097 # Give the system some time to calm down
1098 sleep ${TIMEOUT_RESTART}
1099
1100 cli_start $@
1101 }
1102
1103 cli_status() {
1104 if cli_help_requested $@; then
1105 cli_show_man network
1106 exit ${EXIT_OK}
1107 fi
1108
1109 # When dumping status information, the debug
1110 # mode clutters the console which is not what we want.
1111 # Logging on the console is disabled for a short time.
1112 local log_disable_stdout=${LOG_DISABLE_STDOUT}
1113 LOG_DISABLE_STDOUT="true"
1114
1115 local zones=$(zones_get $@)
1116
1117 local zone
1118 for zone in ${zones}; do
1119 zone_status ${zone}
1120 done
1121
1122 # Reset logging.
1123 LOG_DISABLE_STDOUT=${log_disable_stdout}
1124 }
1125
1126 cli_reset() {
1127 if cli_help_requested $@; then
1128 cli_show_man network
1129 exit ${EXIT_OK}
1130 fi
1131
1132 warning_log "Will reset the whole network configuration!!!"
1133
1134 # Force mode is disabled by default
1135 local force=0
1136
1137 while [ $# -gt 0 ]; do
1138 case "${1}" in
1139 --force|-f)
1140 force=1
1141 ;;
1142 esac
1143 shift
1144 done
1145
1146 # If we are not running in force mode, we ask the user if he does know
1147 # what he is doing.
1148 if ! enabled force; then
1149 if ! cli_yesno "Do you really want to reset the whole network configuration?"; then
1150 exit ${EXIT_ERROR}
1151 fi
1152 fi
1153
1154 local zone
1155 for zone in $(zones_get --all); do
1156 zone_destroy_now "${zone}"
1157 done
1158
1159 local port
1160 for port in $(ports_get --all); do
1161 port_destroy "${port}"
1162 done
1163
1164 # Flush all DNS servers.
1165 dns_server_flush
1166
1167 # Re-run the initialization functions
1168 init_run
1169
1170 exit ${EXIT_OK}
1171 }
1172
1173 # Help function: will show the default man page to the user.
1174 # Optionally, there are two arguments taken, the type of hook
1175 # and which hook should be shown.
1176 cli_help() {
1177 local type=${1}
1178 local what=${2}
1179
1180 # Remove unknown types.
1181 if ! listmatch ${type} zone port config; then
1182 type=""
1183 fi
1184
1185 # If no arguments were given, we will show the default page.
1186 if [ -z "${type}" ]; then
1187 cli_show_man network
1188 return ${EXIT_OK}
1189 fi
1190
1191 if ! hook_exists ${type} ${what}; then
1192 error "Hook of type '${type}' and name '${what}' could not be found."
1193 exit "${EXIT_ERROR}"
1194 fi
1195
1196 hook_exec ${type} ${what} help
1197 }
1198
1199 cli_dns_server() {
1200 if cli_help_requested $@; then
1201 cli_show_man network-dns-server
1202 exit ${EXIT_OK}
1203 fi
1204
1205 # Get the command.
1206 local cmd=${1}; shift
1207 if [ -z "${cmd}" ]; then
1208 cli_show_man network-dns-server
1209 exit ${EXIT_ERROR}
1210 fi
1211
1212 # Get the new server to process (if any).
1213 local server=${1}
1214 local priority=${2}
1215
1216 case "${cmd}" in
1217 list)
1218 dns_server_show
1219 exit ${EXIT_OK}
1220 ;;
1221 add)
1222 if dns_server_exists ${server}; then
1223 error "DNS server '${server}' already exists!"
1224 exit ${EXIT_ERROR}
1225 fi
1226
1227 log INFO "Adding new DNS server: ${server}"
1228 dns_server_add ${server} ${priority}
1229 ;;
1230 remove)
1231 if ! dns_server_exists ${server}; then
1232 error "DNS server '${server}' does not exist!"
1233 exit ${EXIT_ERROR}
1234 fi
1235
1236 log INFO "Removing DNS server: ${server}"
1237 dns_server_remove ${server} ${priority}
1238 ;;
1239 update)
1240 # Just run the update afterwards.
1241 ;;
1242 *)
1243 error "No such command: ${cmd}"
1244 exit ${EXIT_ERROR}
1245 esac
1246
1247 # Update the local DNS configuration after changes have been made.
1248 dns_server_update
1249
1250 exit ${EXIT_OK}
1251 }
1252
1253 cli_raw() {
1254 local cmd="${1}"
1255 assert isset cmd
1256 shift
1257
1258 case "${cmd}" in
1259 db-dump)
1260 db_dump
1261 ;;
1262 list-devices)
1263 device_list
1264 ;;
1265 list-dhcpd-ranges-of-subnet)
1266 dhcpd_subnet_range_list $@
1267 ;;
1268 list-dhcpd-settings)
1269 dhcpd_global_settings_list $@
1270 ;;
1271 list-dhcpd-subnets)
1272 dhcpd_subnet_list $@
1273 ;;
1274 list-dhcpd-subnet-options)
1275 dhcpd_subnet_options_list $@
1276 ;;
1277 list-dns-servers)
1278 dns_server_list
1279 ;;
1280 list-free-ports)
1281 port_list_free
1282 ;;
1283 list-hooks)
1284 hook_list $@
1285 ;;
1286 list-ports)
1287 port_list
1288 ;;
1289 list-ports-of-zone)
1290 zone_get_ports $@
1291 ;;
1292 list-settings)
1293 network_settings_list
1294 ;;
1295 list-zones)
1296 zones_get_all
1297 ;;
1298 *)
1299 error "No such command: ${cmd}"
1300 exit ${EXIT_ERROR}
1301 ;;
1302 esac
1303
1304 exit ${EXIT_OK}
1305 }
1306
1307 # Process the given action
1308 case "${action}" in
1309 init)
1310 init_run
1311 ;;
1312
1313 settings|hostname|port|device|zone|start|stop|restart|status|reset|route)
1314 cli_${action} $@
1315 ;;
1316
1317 # DHCP server configuration (automatically detects which protocol to use).
1318 dhcpv6|dhcpv4)
1319 cli_dhcpd ${action/dhcp/ip} $@
1320 ;;
1321
1322 # DNS server configuration.
1323 dns-server)
1324 cli_dns_server $@
1325 ;;
1326
1327 ""|help|--help|-h)
1328 cli_help $@
1329 ;;
1330
1331 raw)
1332 cli_raw $@
1333 ;;
1334
1335 *)
1336 error "Invalid command given: ${action}"
1337 cli_usage "network help"
1338 exit ${EXIT_CONF_ERROR}
1339 ;;
1340 esac
1341
1342 exit ${EXIT_OK}