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