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