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