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