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