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