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