]> git.ipfire.org Git - people/stevee/network.git/blame - src/network
Show help for zone creation
[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
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
e9df08ad
MT
48 settings_set $@
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 ;;
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
1c6a4e30 106cli_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
1c6a4e30 205cli_device_status_serial() {
6c74a64c
MT
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
1c6a4e30 266cli_device_status_phy() {
f90dd58c
MT
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
1c6a4e30 288cli_device_discover() {
9111eb72
MT
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
1c6a4e30 359cli_device_serial_unlock() {
6c74a64c
MT
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
1c6a4e30 445cli_device_send_ussd_command() {
5cf0edf9
MT
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
1c6a4e30 480cli_device_monitor() {
5a38ea84
MT
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
1c6a4e30 493cli_device_list() {
f90dd58c
MT
494 local device
495 for device in $(device_list); do
496 cli_device_status "${device}"
497 done
498
499 exit ${EXIT_OK}
500}
501
1c6a4e30 502cli_hostname() {
9111eb72
MT
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
1c6a4e30 521cli_port() {
9111eb72
MT
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
1c6a4e30 560cli_zone() {
9111eb72
MT
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 605 new)
2c37b88b 606 cli_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
2c37b88b
MT
627cli_zone_new() {
628 if cli_help_requested $@ || [ $# -lt 2 ]; then
629 cli_show_man network-zone-new
630 exit ${EXIT_OK}
631 fi
632
633 zone_new $@
634}
635
9111eb72
MT
636# Removes a zone either immediately, if it is currently down,
637# or adds a tag that the removal will be done when the zone
638# is brought down the next time.
1c6a4e30 639cli_zone_destroy() {
9111eb72
MT
640 if cli_help_requested $@; then
641 cli_show_man network-zone
642 exit ${EXIT_OK}
643 fi
644
cf0fc8ab
MT
645 local zone="${1}"
646 assert zone_exists "${zone}"
9111eb72
MT
647
648 if zone_is_up ${zone}; then
649 echo "Zone '${zone}' is up and will be removed when it goes down the next time."
cf0fc8ab 650 zone_destroy "${zone}"
9111eb72
MT
651 else
652 echo "Removing zone '${zone}' now..."
cf0fc8ab 653 zone_destroy_now "${zone}"
9111eb72
MT
654 fi
655
656 exit ${EXIT_OK}
657}
658
1c6a4e30 659cli_zone_port() {
ac694a6a
MT
660 if cli_help_requested $@; then
661 cli_show_man network-zone-port
662 exit ${EXIT_OK}
663 fi
664
665 local zone="${1}"
666 assert zone_exists "${zone}"
667
668 if port_exists "${2}"; then
669 local port="${2}"
670 local action="${3}"
671 shift 3
672
673 case "${action}" in
674 edit)
675 zone_port_edit "${zone}" "${port}" $@
676 ;;
677 *)
678 error "Unrecognised argument: ${action}"
679 exit ${EXIT_ERROR}
680 ;;
681 esac
682 else
683 local action="${2}"
684 shift 2
685
686 case "${action}" in
687 attach)
688 zone_port_attach "${zone}" $@
689 ;;
690 detach)
691 zone_port_detach "${zone}" $@
692 ;;
693 *)
694 error "Unrecognised argument: ${action}"
695 exit ${EXIT_ERROR}
696 ;;
697 esac
698 fi
699
700 exit ${EXIT_OK}
701}
702
1c6a4e30 703cli_list_hooks() {
9111eb72
MT
704 local type=${1}
705 shift
706
707 if cli_help_requested $@; then
708 cli_show_man network-zone
709 exit ${EXIT_OK}
710 fi
711
712 local hook_dir=$(hook_dir ${type})
713 local hook
714
715 for hook in ${hook_dir}/*; do
716 hook=$(basename ${hook})
717 if hook_exists ${type} ${hook}; then
718 echo "${hook}"
719 fi
720 done | sort -u
721}
722
1c6a4e30 723cli_route() {
cb965348
MT
724 if cli_help_requested $@; then
725 cli_show_man network-route
726 exit ${EXIT_OK}
727 fi
728
729 local action=${1}
730 shift
731
732 case "${action}" in
733 # Add a new route.
734 add)
735 route_add $@
736 ;;
737 # Remove an existing route.
738 remove)
739 route_remove $@
740 ;;
741 # List all routes.
742 list)
743 route_list $@
d2021e87 744 return ${EXIT_OK}
cb965348
MT
745 ;;
746 *)
747 error "Unrecognized action: ${action}"
748 cli_run_help network route
749
750 exit ${EXIT_ERROR}
751 ;;
752 esac
753
d2021e87
MT
754 # Applying all routes.
755 route_apply
756
cb965348
MT
757 exit ${EXIT_OK}
758}
759
1c6a4e30 760cli_dhcpd() {
6c07160e
MT
761 local proto=${1}
762 shift
763
764 if cli_help_requested $@; then
765 cli_show_man network-dhcp
766 exit ${EXIT_OK}
767 fi
768
769 local action=${1}
770 shift
771
772 case "${action}" in
773 edit)
774 dhcpd_edit ${proto} $@
775 ;;
776 start)
777 dhcpd_start ${proto}
778 ;;
779 stop)
780 dhcpd_stop ${proto}
781 ;;
782 restart|reload)
783 dhcpd_reload ${proto}
784 ;;
785 subnet)
786 cli_dhcpd_subnet ${proto} $@
787 ;;
788 show|"")
789 cli_dhcpd_show ${proto} $@
790 ;;
791 *)
792 error "Unrecognized action: ${action}"
793 cli_run_help network dhcpvN
794
795 exit ${EXIT_ERROR}
796 ;;
797 esac
798
799 exit ${EXIT_OK}
800}
801
1c6a4e30 802cli_dhcpd_show() {
6c07160e
MT
803 local proto=${1}
804 assert isset proto
805
806 local settings=$(dhcpd_settings ${proto})
807 assert isset settings
808
809 local ${settings}
810 dhcpd_global_settings_read ${proto}
811
812 cli_headline 1 "Dynamic Host Configuration Protocol Daemon for ${proto/ip/IP}"
813
814 case "${proto}" in
815 ipv6)
816 cli_headline 2 "Lease times"
817 if isinteger VALID_LIFETIME; then
818 cli_print_fmt1 2 "Valid lifetime" "${VALID_LIFETIME}s"
819 fi
820
821 if isinteger PREFERRED_LIFETIME; then
822 cli_print_fmt1 2 "Preferred lifetime" "${PREFERRED_LIFETIME}s"
823 fi
824
825 cli_space
826 ;;
827 ipv4)
828 cli_print_fmt1 1 "Authoritative" $(cli_print_enabled AUTHORITATIVE)
829 cli_space
830
831 cli_headline 2 "Lease times"
832 cli_print_fmt1 2 "Default lease time" "${DEFAULT_LEASE_TIME}s"
833 cli_print_fmt1 2 "Max. lease time" "${MAX_LEASE_TIME}s"
834
835 if isset MIN_LEASE_TIME; then
836 cli_print_fmt1 2 "Min. lease time" "${MIN_LEASE_TIME}s"
837 fi
838
839 cli_space
840 ;;
841 esac
842
843 # Read the options.
844 local -A options
845 dhcpd_global_options_read ${proto} ${subnet_id}
846
847 # Print the options if any.
848 if [ ${#options[*]} -gt 0 ]; then
849 cli_headline 2 "Options"
850
851 local option
852 for option in $(dhcpd_options ${proto}); do
853 [ -n "${options[${option}]}" ] || continue
854
855 cli_print_fmt1 2 \
856 "${option}" "${options[${option}]}"
857 done
858 cli_space
859 fi
860
861 # Subnets.
862 local subnets=$(dhcpd_subnet_list ${proto})
863 if [ -n "${subnets}" ]; then
864 cli_headline 2 "Subnets"
865 local subnet_id
866 for subnet_id in ${subnets}; do
867 cli_dhcpd_subnet_show ${proto} ${subnet_id} 2
868 done
869 fi
870}
871
1c6a4e30 872cli_dhcpd_subnet() {
6c07160e
MT
873 local proto=${1}
874 shift
875
876 if cli_help_requested $@; then
877 cli_show_man network-dhcp-subnet
878 exit ${EXIT_OK}
879 fi
880
881 local action=${1}
882 shift
883
884 case "${action}" in
885 new)
886 dhcpd_subnet_new ${proto} $@
887 ;;
888 remove)
889 dhcpd_subnet_remove ${proto} $@
890 ;;
891 [0-9]*)
892 local subnet_id=${action}
893
894 if ! dhcpd_subnet_exists ${proto} ${subnet_id}; then
895 error "The given subnet with ID ${subnet_id} does not exist."
896 return ${EXIT_ERROR}
897 fi
898
899 # Update the action.
900 action=${1}
901 shift
902
903 case "${action}" in
904 edit)
905 dhcpd_subnet_edit ${proto} ${subnet_id} $@
906 local ret=$?
907
908 if [ ${ret} -eq ${EXIT_OK} ]; then
909 dhcpd_reload ${proto}
910 fi
911 exit ${ret}
912 ;;
913 range)
914 cli_dhcpd_subnet_range ${proto} ${subnet_id} $@
915 exit $?
916 ;;
917 show)
918 cli_dhcpd_subnet_show ${proto} ${subnet_id} $@
919 exit $?
920 ;;
921 options)
922 cli_dhcpd_subnet_options ${proto} ${subnet_id} $@
923 exit $?
924 ;;
925 *)
926 error "Unrecognized action: ${action}"
927 cli_run_help network dhcpvN subnet
928 exit ${EXIT_ERROR}
929 ;;
930 esac
931 ;;
932 show)
933 local subnet_id
934 for subnet_id in $(dhcpd_subnet_list ${proto}); do
935 cli_dhcpd_subnet_show ${proto} ${subnet_id}
936 done
937 ;;
938 *)
939 error "Unrecognized action: ${action}"
940 cli_run_help network dhcpvN subnet
941
942 exit ${EXIT_ERROR}
943 ;;
944 esac
945
946 exit ${EXIT_OK}
947}
948
1c6a4e30 949cli_dhcpd_subnet_range() {
6c07160e
MT
950 local proto=${1}
951 assert isset proto
952 shift
953
954 local subnet_id=${1}
955 assert isset subnet_id
956 shift
957
958 local action=${1}
959 shift
960
961 case "${action}" in
962 new)
963 dhcpd_subnet_range_new ${proto} ${subnet_id} $@
964 exit $?
965 ;;
966 remove)
967 dhcpd_subnet_range_remove ${proto} ${subnet_id} $@
968 exit $?
969 ;;
970 *)
971 error "Unrecognized action: ${action}"
972 cli_run_help network dhcpvN subnet range
973 exit ${EXIT_ERROR}
974 ;;
975 esac
976}
977
1c6a4e30 978cli_dhcpd_subnet_show() {
6c07160e
MT
979 local proto=${1}
980 assert isset proto
981
982 local subnet_id=${2}
983 assert isset subnet_id
984
985 local level=${3}
986 isset level || level=0
987
988 local $(dhcpd_subnet_settings ${proto})
989
990 # Read in configuration settings.
991 dhcpd_subnet_read ${proto} ${subnet_id}
992
993 cli_headline $(( ${level} + 1 )) \
994 "DHCP${proto/ip/} subnet declaration #${subnet_id}"
995 cli_print_fmt1 $(( ${level} + 1 )) \
996 "Subnet" "${ADDRESS}/${PREFIX}"
997 cli_space
998
999 # Read the options.
1000 local -A options
ea878018 1001 dhcpd_subnet_options_read "${proto}" "${subnet_id}"
6c07160e
MT
1002
1003 # Print the options if any.
1004 if [ ${#options[*]} -gt 0 ]; then
1005 cli_headline $(( ${level} + 2 )) "Options"
1006
1007 local option
cc02f6be 1008 for option in $(dhcpd_subnet_options_list ${proto}); do
6c07160e
MT
1009 [ -n "${options[${option}]}" ] || continue
1010
1011 cli_print_fmt1 $(( ${level} + 2 )) \
1012 "${option}" "${options[${option}]}"
1013 done
1014 cli_space
1015 fi
1016
1017 # Ranges.
1018 cli_headline $(( ${level} + 2 )) "Ranges"
1019
1020 local ranges=$(dhcpd_subnet_range_list ${proto} ${subnet_id})
1021 if isset ranges; then
1022 local range_id $(dhcpd_subnet_range_settings ${proto})
1023 for range_id in ${ranges}; do
1024 dhcpd_subnet_range_read ${proto} ${subnet_id} ${range_id}
1025
1026 cli_print $(( ${level} + 2 )) \
1027 "#%d: %s - %s" ${range_id} ${START} ${END}
1028 done
1029 else
1030 cli_print $(( ${level} + 2 )) "No ranges have been defined."
1031 fi
1032
1033 cli_space
1034}
1035
1c6a4e30 1036cli_dhcpd_subnet_options() {
6c07160e
MT
1037 local proto=${1}
1038 assert isset proto
1039 shift
1040
1041 local subnet_id=${1}
1042 assert isset subnet_id
1043 shift
1044
6c07160e
MT
1045 local key val
1046 while [ $# -gt 0 ]; do
1047 case "${1}" in
1048 *=*)
1049 key=$(cli_get_key ${1})
1050 val=$(cli_get_val ${1})
1051
1052 dhcpd_subnet_option_set ${proto} ${subnet_id} ${key} ${val}
1053 esac
1054 done
1055}
1056
1c6a4e30 1057cli_start() {
9111eb72
MT
1058 if cli_help_requested $@; then
1059 cli_show_man network
1060 exit ${EXIT_OK}
1061 fi
1062
1063 local zones=$(zones_get $@)
1064
1065 local zone
1066 for zone in ${zones}; do
1067 zone_start ${zone} &
1068 done
1069
1070 wait # until everything is settled
1071}
1072
1c6a4e30 1073cli_stop() {
9111eb72
MT
1074 if cli_help_requested $@; then
1075 cli_show_man network
1076 exit ${EXIT_OK}
1077 fi
1078
1079 local zones=$(zones_get $@)
1080
1081 local zone
1082 for zone in ${zones}; do
1083 zone_stop ${zone} &
1084 done
1085
1086 wait # until everything is settled
1087}
1088
1c6a4e30 1089cli_restart() {
9111eb72
MT
1090 if cli_help_requested $@; then
1091 cli_show_man network
1092 exit ${EXIT_OK}
1093 fi
1094
1095 cli_stop $@
1096
1097 # Give the system some time to calm down
1098 sleep ${TIMEOUT_RESTART}
1099
1100 cli_start $@
1101}
1102
1c6a4e30 1103cli_status() {
9111eb72
MT
1104 if cli_help_requested $@; then
1105 cli_show_man network
1106 exit ${EXIT_OK}
1107 fi
1108
1109 # When dumping status information, the debug
1110 # mode clutters the console which is not what we want.
1111 # Logging on the console is disabled for a short time.
1112 local log_disable_stdout=${LOG_DISABLE_STDOUT}
1113 LOG_DISABLE_STDOUT="true"
1114
1115 local zones=$(zones_get $@)
1116
1117 local zone
1118 for zone in ${zones}; do
1119 zone_status ${zone}
1120 done
1121
1122 # Reset logging.
1123 LOG_DISABLE_STDOUT=${log_disable_stdout}
1124}
1125
1c6a4e30 1126cli_reset() {
9111eb72
MT
1127 if cli_help_requested $@; then
1128 cli_show_man network
1129 exit ${EXIT_OK}
1130 fi
1131
1132 warning_log "Will reset the whole network configuration!!!"
1133
1134 # Force mode is disabled by default
1135 local force=0
1136
1137 while [ $# -gt 0 ]; do
1138 case "${1}" in
1139 --force|-f)
1140 force=1
1141 ;;
1142 esac
1143 shift
1144 done
1145
1146 # If we are not running in force mode, we ask the user if he does know
1147 # what he is doing.
1148 if ! enabled force; then
1149 if ! cli_yesno "Do you really want to reset the whole network configuration?"; then
1150 exit ${EXIT_ERROR}
1151 fi
1152 fi
1153
1154 local zone
1155 for zone in $(zones_get --all); do
cf0fc8ab 1156 zone_destroy_now "${zone}"
9111eb72
MT
1157 done
1158
1159 local port
1160 for port in $(ports_get --all); do
1ba6a2bb 1161 port_destroy "${port}"
9111eb72
MT
1162 done
1163
acc9efd5
MT
1164 # Flush all DNS servers.
1165 dns_server_flush
1166
9111eb72
MT
1167 # Re-run the initialization functions
1168 init_run
1169
1170 exit ${EXIT_OK}
1171}
1172
85afd775
MT
1173# Help function: will show the default man page to the user.
1174# Optionally, there are two arguments taken, the type of hook
1175# and which hook should be shown.
1c6a4e30 1176cli_help() {
85afd775
MT
1177 local type=${1}
1178 local what=${2}
1179
1180 # Remove unknown types.
1181 if ! listmatch ${type} zone port config; then
1182 type=""
1183 fi
1184
1185 # If no arguments were given, we will show the default page.
1186 if [ -z "${type}" ]; then
1187 cli_show_man network
1188 return ${EXIT_OK}
1189 fi
1190
1191 if ! hook_exists ${type} ${what}; then
1192 error "Hook of type '${type}' and name '${what}' could not be found."
1193 exit "${EXIT_ERROR}"
1194 fi
1195
1196 hook_exec ${type} ${what} help
1197}
1198
1c6a4e30 1199cli_dns_server() {
acc9efd5 1200 if cli_help_requested $@; then
6b34112f 1201 cli_show_man network-dns-server
acc9efd5
MT
1202 exit ${EXIT_OK}
1203 fi
1204
1205 # Get the command.
1206 local cmd=${1}; shift
1207 if [ -z "${cmd}" ]; then
6b34112f 1208 cli_show_man network-dns-server
acc9efd5
MT
1209 exit ${EXIT_ERROR}
1210 fi
1211
6f923dac
MT
1212 # Get the new server to process (if any).
1213 local server=${1}
1214 local priority=${2}
1215
acc9efd5
MT
1216 case "${cmd}" in
1217 list)
8d1d2745 1218 dns_server_show
e5efaa6b 1219 exit ${EXIT_OK}
acc9efd5
MT
1220 ;;
1221 add)
e5651e17
MT
1222 if dns_server_exists ${server}; then
1223 error "DNS server '${server}' already exists!"
1224 exit ${EXIT_ERROR}
1225 fi
1226
6f923dac
MT
1227 log INFO "Adding new DNS server: ${server}"
1228 dns_server_add ${server} ${priority}
acc9efd5
MT
1229 ;;
1230 remove)
e5651e17
MT
1231 if ! dns_server_exists ${server}; then
1232 error "DNS server '${server}' does not exist!"
1233 exit ${EXIT_ERROR}
1234 fi
1235
6f923dac
MT
1236 log INFO "Removing DNS server: ${server}"
1237 dns_server_remove ${server} ${priority}
acc9efd5
MT
1238 ;;
1239 update)
1240 # Just run the update afterwards.
1241 ;;
1242 *)
1243 error "No such command: ${cmd}"
1244 exit ${EXIT_ERROR}
1245 esac
1246
1247 # Update the local DNS configuration after changes have been made.
1248 dns_generate_resolvconf
6f923dac 1249 radvd_update
acc9efd5
MT
1250
1251 exit ${EXIT_OK}
1252}
1253
1c6a4e30 1254cli_raw() {
bae37360
MT
1255 local cmd="${1}"
1256 assert isset cmd
1257 shift
1258
1259 case "${cmd}" in
c041b631
MT
1260 db-dump)
1261 db_dump
1262 ;;
bae37360
MT
1263 list-devices)
1264 device_list
1265 ;;
cc02f6be
MT
1266 list-dhcpd-ranges-of-subnet)
1267 dhcpd_subnet_range_list $@
1268 ;;
1269 list-dhcpd-settings)
1270 dhcpd_global_settings_list $@
1271 ;;
1272 list-dhcpd-subnets)
1273 dhcpd_subnet_list $@
1274 ;;
1275 list-dhcpd-subnet-options)
1276 dhcpd_subnet_options_list $@
1277 ;;
8d1d2745
MT
1278 list-dns-servers)
1279 dns_server_list
1280 ;;
bae37360
MT
1281 list-free-ports)
1282 port_list_free
1283 ;;
1284 list-hooks)
1285 hook_list $@
1286 ;;
1287 list-ports)
1288 port_list
1289 ;;
1290 list-ports-of-zone)
1291 zone_get_ports $@
1292 ;;
1293 list-settings)
1294 network_settings_list
1295 ;;
1296 list-zones)
1297 zones_get_all
1298 ;;
1299 *)
1300 error "No such command: ${cmd}"
1301 exit ${EXIT_ERROR}
1302 ;;
1303 esac
1304
1305 exit ${EXIT_OK}
1306}
1307
1848564d
MT
1308# Process the given action
1309case "${action}" in
b8357295
MT
1310 init)
1311 init_run
1312 ;;
1313
e9df08ad 1314 settings|hostname|port|device|zone|start|stop|restart|status|reset|route)
0a79ea02 1315 cli_${action} $@
1848564d
MT
1316 ;;
1317
6c07160e
MT
1318 # DHCP server configuration (automatically detects which protocol to use).
1319 dhcpv6|dhcpv4)
1320 cli_dhcpd ${action/dhcp/ip} $@
1321 ;;
1322
6b34112f
MT
1323 # DNS server configuration.
1324 dns-server)
1325 cli_dns_server $@
1326 ;;
1327
fe4555b5 1328 ""|help|--help|-h)
85afd775 1329 cli_help $@
1848564d 1330 ;;
fe4555b5 1331
bae37360
MT
1332 raw)
1333 cli_raw $@
1334 ;;
1335
1848564d 1336 *)
fe4555b5 1337 error "Invalid command given: ${action}"
de28a630 1338 cli_usage "network help"
1848564d 1339 exit ${EXIT_CONF_ERROR}
fe4555b5 1340 ;;
1848564d 1341esac
85afd775
MT
1342
1343exit ${EXIT_OK}