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