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