]>
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 | ||
9111eb72 MT |
38 | function cli_config() { |
39 | if cli_help_requested $@; then | |
40 | cli_show_man network-config | |
41 | exit ${EXIT_OK} | |
42 | fi | |
43 | ||
44 | if [ -n "${1}" ]; then | |
45 | config_set $@ | |
46 | network_config_write | |
47 | else | |
48 | network_config_print | |
49 | fi | |
50 | } | |
51 | ||
52 | function cli_device() { | |
6c74a64c MT |
53 | if cli_help_requested $@; then |
54 | cli_show_man network-device | |
55 | exit ${EXIT_OK} | |
56 | fi | |
57 | ||
9111eb72 MT |
58 | local device=${1} |
59 | local action=${2} | |
60 | shift 2 | |
61 | ||
ec63256a MT |
62 | if ! isset device; then |
63 | cli_show_man network-device | |
9111eb72 MT |
64 | return ${EXIT_ERROR} |
65 | fi | |
66 | ||
ec63256a MT |
67 | assert device_exists ${device} |
68 | ||
9111eb72 MT |
69 | case "${action}" in |
70 | discover) | |
9111eb72 MT |
71 | cli_device_discover ${device} $@ |
72 | ;; | |
ec63256a MT |
73 | status) |
74 | cli_device_status ${device} | |
9111eb72 | 75 | ;; |
6c74a64c MT |
76 | unlock) |
77 | cli_device_serial_unlock ${device} $@ | |
78 | ;; | |
9111eb72 MT |
79 | *) |
80 | cli_show_man network-device | |
81 | ;; | |
82 | esac | |
ec63256a MT |
83 | |
84 | return ${EXIT_OK} | |
85 | } | |
86 | ||
87 | function cli_device_status() { | |
88 | local device=${1} | |
89 | assert device_exists ${device} | |
90 | ||
6c74a64c MT |
91 | # Disable debugging output here. |
92 | local log_disable_stdout=${LOG_DISABLE_STDOUT} | |
93 | LOG_DISABLE_STDOUT="true" | |
94 | ||
ec63256a MT |
95 | # Save the type of the device for later. |
96 | local type=$(device_get_type ${device}) | |
97 | ||
98 | cli_headline 1 "Device status: ${device}" | |
99 | cli_print_fmt1 1 "Name" "${device}" | |
100 | ||
6c74a64c MT |
101 | # Handle serial devices. |
102 | if [ "${type}" = "serial" ]; then | |
103 | cli_device_status_serial ${device} | |
104 | return $? | |
105 | fi | |
106 | ||
ec63256a MT |
107 | # Print the device status. |
108 | device_is_up ${device} &>/dev/null | |
109 | local status=$? | |
110 | ||
111 | case "${status}" in | |
112 | ${EXIT_TRUE}) | |
fcbf6823 | 113 | status="${CLR_GREEN_B}UP${CLR_RESET}" |
ec63256a MT |
114 | ;; |
115 | ${EXIT_FALSE}) | |
fcbf6823 | 116 | status="${CLR_RED_B}DOWN${CLR_RESET}" |
ec63256a MT |
117 | ;; |
118 | esac | |
119 | ||
120 | cli_print_fmt1 1 "Status" "${status}" | |
121 | cli_print_fmt1 1 "Type" "${type}" | |
122 | cli_print_fmt1 1 "Address" "$(device_get_address ${device})" | |
123 | cli_space | |
124 | ||
125 | # Print the link speed for ethernet devices. | |
245dffc9 MT |
126 | if device_is_up ${device} &>/dev/null; then |
127 | case "${type}" in | |
128 | ethernet) | |
129 | cli_print_fmt1 1 "Link" \ | |
130 | "$(device_get_speed ${device}) MBit/s $(device_get_duplex ${device}) duplex" | |
131 | ;; | |
132 | esac | |
133 | fi | |
ec63256a MT |
134 | |
135 | cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})" | |
136 | cli_space | |
137 | ||
3cb2fc42 MT |
138 | # Print device statistics. |
139 | cli_device_stats 2 ${device} | |
ec63256a MT |
140 | |
141 | # Print some more information. | |
142 | device_has_carrier ${device} &>/dev/null | |
143 | cli_print_fmt1 1 "Has carrier?" "$(cli_print_bool $?)" | |
144 | ||
145 | device_is_promisc ${device} &>/dev/null | |
146 | cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)" | |
147 | cli_space | |
148 | ||
7951525a MT |
149 | # Print all vlan devices. |
150 | local vlans=$(device_get_vlans ${device}) | |
151 | if [ -n "${vlans}" ]; then | |
152 | cli_headline 2 "VLAN devices" | |
153 | ||
154 | local vlan | |
155 | for vlan in ${vlans}; do | |
156 | cli_print 2 "* %-6s - %s" "${vlan}" "$(device_get_address ${vlan})" | |
ec63256a MT |
157 | done |
158 | cli_space | |
159 | fi | |
160 | ||
6c74a64c MT |
161 | # Reset the logging level. |
162 | LOG_DISABLE_STDOUT=${log_disable_stdout} | |
163 | } | |
164 | ||
165 | function cli_device_status_serial() { | |
166 | local device=${1} | |
167 | assert device_is_serial ${device} | |
168 | ||
169 | serial_is_locked ${device} &>/dev/null | |
170 | local locked=$? | |
171 | ||
172 | cli_print_fmt1 1 "Locked" "$(cli_print_bool ${locked})" | |
173 | cli_space | |
174 | ||
175 | # Cannot go on when the device is locked. | |
176 | [ ${locked} -eq ${EXIT_TRUE} ] && return ${EXIT_OK} | |
177 | ||
178 | cli_print_fmt1 1 "Manufacturer" \ | |
179 | "$(modem_get_manufacturer ${device})" | |
180 | cli_print_fmt1 1 "Model" \ | |
181 | "$(modem_get_model ${device})" | |
182 | cli_print_fmt1 1 "Software version" \ | |
183 | "$(modem_get_software_version ${device})" | |
184 | ||
185 | if modem_is_mobile ${device}; then | |
186 | cli_print_fmt1 1 "IMEI" \ | |
187 | "$(modem_get_device_imei ${device})" | |
188 | cli_space | |
189 | ||
190 | cli_headline 2 "Network status" | |
191 | modem_sim_status ${device} &>/dev/null | |
192 | local sim_status_code=$? | |
193 | ||
194 | local sim_status="unknown" | |
195 | case "${sim_status_code}" in | |
196 | ${EXIT_SIM_READY}) | |
197 | sim_status="SIM ready" | |
198 | ;; | |
199 | ${EXIT_SIM_PIN}) | |
200 | sim_status="PIN locked" | |
201 | ;; | |
202 | ${EXIT_SIM_PUK}) | |
203 | sim_status="PUK locked" | |
204 | ;; | |
205 | esac | |
206 | cli_print_fmt1 2 "SIM status" "${sim_status}" | |
207 | ||
208 | if [ ${sim_status_code} -eq ${EXIT_SIM_READY} ]; then | |
209 | cli_print_fmt1 2 "IMSI" \ | |
210 | "$(modem_get_sim_imsi ${device})" | |
211 | cli_print_fmt1 2 "Operator" \ | |
212 | "$(modem_get_network_operator ${device})" | |
213 | cli_print_fmt1 2 "Mode" \ | |
214 | "$(modem_get_network_mode ${device})" | |
215 | cli_print_fmt1 2 "Signal quality" \ | |
216 | "$(modem_get_signal_quality ${device}) dBm" | |
217 | ||
218 | local ber=$(modem_get_bit_error_rate ${device}) | |
219 | isset ber || ber="unknown" | |
220 | cli_print_fmt1 2 "Bit Error Rate" "${ber}" | |
221 | fi | |
222 | fi | |
223 | cli_space | |
9111eb72 MT |
224 | } |
225 | ||
226 | function cli_device_discover() { | |
227 | local device=${1} | |
228 | shift | |
229 | ||
230 | local device_type=$(device_get_type ${device}) | |
231 | if [ "${device_type}" != "real" ]; then | |
232 | return ${EXIT_OK} | |
233 | fi | |
234 | ||
235 | local raw | |
236 | ||
237 | while [ $# -gt 0 ]; do | |
238 | case "${1}" in | |
239 | --raw) | |
240 | raw=1 | |
241 | ;; | |
242 | esac | |
243 | shift | |
244 | done | |
245 | ||
246 | local up | |
247 | device_is_up ${device} && up=1 | |
248 | device_set_up ${device} | |
249 | ||
250 | enabled raw || echo "${device}" | |
251 | ||
252 | local hook | |
253 | local out | |
254 | local ret | |
255 | for hook in $(hook_zone_get_all); do | |
256 | out=$(hook_zone_exec ${hook} discover ${device}) | |
257 | ret=$? | |
258 | ||
259 | [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue | |
260 | ||
261 | if enabled raw; then | |
262 | case "${ret}" in | |
263 | ${DISCOVER_OK}) | |
264 | echo "${hook}: OK" | |
265 | local line | |
266 | while read line; do | |
267 | echo "${hook}: ${line}" | |
268 | done <<<"${out}" | |
269 | ;; | |
270 | ||
271 | ${DISCOVER_ERROR}) | |
272 | echo "${hook}: FAILED" | |
273 | ;; | |
274 | esac | |
275 | else | |
276 | case "${ret}" in | |
277 | ${DISCOVER_OK}) | |
278 | echo " ${hook} was successful." | |
279 | local line | |
280 | while read line; do | |
281 | echo " ${line}" | |
282 | done <<<"${out}" | |
283 | ;; | |
284 | ||
285 | ${DISCOVER_ERROR}) | |
286 | echo " ${hook} failed." | |
287 | ;; | |
288 | esac | |
289 | fi | |
290 | done | |
291 | ||
292 | echo # New line | |
293 | ||
294 | [ "${up}" = "1" ] || device_set_down ${device} | |
295 | } | |
296 | ||
6c74a64c MT |
297 | function cli_device_serial_unlock() { |
298 | if cli_help_requested $@; then | |
299 | cli_show_man network-device | |
300 | exit ${EXIT_OK} | |
301 | fi | |
302 | ||
303 | local device=${1} | |
304 | assert isset device | |
305 | ||
306 | if ! device_is_serial ${device}; then | |
307 | error "${device} is not a serial device." | |
308 | error "Unlocking is only supported for serial devices." | |
309 | exit ${EXIT_ERROR} | |
310 | fi | |
311 | ||
312 | # Read the current state of the SIM card. | |
313 | modem_sim_status ${device} &>/dev/null | |
314 | local sim_status_code=$? | |
315 | ||
316 | # If the SIM card is already unlocked, we don't need to do anything. | |
317 | if [ ${sim_status_code} -eq ${EXIT_SIM_READY} ]; then | |
318 | print "The SIM card is already unlocked." | |
319 | exit ${EXIT_OK} | |
320 | ||
321 | # If the SIM card is in an unknown state, we cannot do anything. | |
322 | elif [ ${sim_status_code} -eq ${EXIT_SIM_UNKNOWN} ]; then | |
323 | error "The SIM card is in an unknown state." | |
324 | exit ${EXIT_ERROR} | |
325 | fi | |
326 | ||
327 | # Ask for the code. | |
328 | local code=${2} | |
329 | local require_new_pin="false" | |
330 | local new_pin | |
331 | ||
332 | while ! isinteger code; do | |
333 | local message | |
334 | case "${sim_status_code}" in | |
335 | ${EXIT_SIM_PIN}) | |
336 | message="Please enter PIN:" | |
337 | ;; | |
338 | ${EXIT_SIM_PUK}) | |
339 | message="Please enter PUK:" | |
340 | require_new_pin="true" | |
341 | ;; | |
342 | esac | |
343 | assert isset message | |
344 | ||
345 | echo -n "${message} " | |
346 | read -s code | |
347 | echo # Print newline. | |
348 | ||
349 | if enabled require_new_pin; then | |
350 | local i new_pin2 | |
351 | for i in 0 1; do | |
352 | case "${i}" in | |
353 | 0) | |
354 | message="Please enter a new PIN code:" | |
355 | ;; | |
356 | 1) | |
357 | message="Please confirm the new PIN code:" | |
358 | ;; | |
359 | esac | |
360 | ||
361 | echo -n "${message} " | |
362 | read -s new_pin2 | |
363 | echo # Print newline. | |
364 | ||
365 | if [ -n "${new_pin}" ]; then | |
366 | if [ "${new_pin}" != "${new_pin2}" ]; then | |
367 | error "The entered PIN codes did not match." | |
368 | exit ${EXIT_ERROR} | |
369 | fi | |
370 | else | |
371 | new_pin=${new_pin2} | |
372 | fi | |
373 | done | |
374 | fi | |
375 | done | |
376 | ||
377 | # Trying to unlock the SIM card. | |
378 | modem_sim_unlock ${device} ${code} ${new_pin} | |
379 | ||
380 | exit $? | |
381 | } | |
382 | ||
9111eb72 MT |
383 | function cli_hostname() { |
384 | if cli_help_requested $@; then | |
385 | cli_show_man network | |
386 | exit ${EXIT_OK} | |
387 | fi | |
388 | ||
389 | local hostname=${1} | |
390 | ||
391 | if [ -n "${hostname}" ]; then | |
392 | config_hostname ${hostname} | |
393 | log INFO "Hostname was set to '${hostname}'." | |
394 | log INFO "Changes do only take affect after reboot." | |
395 | exit ${EXIT_OK} | |
396 | fi | |
397 | ||
398 | echo "$(config_hostname)" | |
399 | exit ${EXIT_OK} | |
400 | } | |
401 | ||
402 | function cli_port() { | |
403 | if cli_help_requested $@; then | |
404 | cli_show_man network-port | |
405 | exit ${EXIT_OK} | |
406 | fi | |
407 | ||
408 | local action | |
409 | local port | |
410 | ||
411 | if port_exists ${1}; then | |
412 | port=${1} | |
413 | action=${2} | |
414 | shift 2 | |
415 | ||
416 | # Action aliases | |
417 | case "${action}" in | |
418 | start) | |
419 | action="up" | |
420 | ;; | |
421 | stop) | |
422 | action="down" | |
423 | ;; | |
424 | show) | |
425 | action="status" | |
426 | ;; | |
427 | esac | |
428 | ||
429 | case "${action}" in | |
430 | edit|up|down|status) | |
431 | port_${action} ${port} $@ | |
432 | ;; | |
433 | *) | |
434 | error "Unrecognized argument: ${action}" | |
435 | exit ${EXIT_ERROR} | |
436 | ;; | |
437 | esac | |
438 | else | |
439 | action=${1} | |
440 | shift | |
441 | ||
442 | case "${action}" in | |
443 | create|destroy) | |
444 | port_${action} $@ | |
445 | ;; | |
446 | *) | |
447 | error "Unrecognized argument: ${action}" | |
448 | exit ${EXIT_ERROR} | |
449 | ;; | |
450 | esac | |
451 | fi | |
452 | } | |
453 | ||
454 | function cli_zone() { | |
455 | if cli_help_requested $@; then | |
456 | cli_show_man network-zone | |
457 | exit ${EXIT_OK} | |
458 | fi | |
459 | ||
460 | local action | |
461 | local zone | |
462 | ||
463 | if zone_name_is_valid ${1}; then | |
464 | zone=${1} | |
465 | action=${2} | |
466 | shift 2 | |
467 | ||
468 | # Action aliases | |
469 | case "${action}" in | |
470 | start) | |
471 | action="up" | |
472 | ;; | |
473 | stop) | |
474 | action="down" | |
475 | ;; | |
476 | show) | |
477 | action="status" | |
478 | ;; | |
479 | esac | |
480 | ||
481 | case "${action}" in | |
482 | config|down|edit|port|status|up) | |
483 | zone_${action} ${zone} $@ | |
484 | ;; | |
485 | *) | |
486 | error "Unrecognized argument: ${action}" | |
487 | cli_show_man network-zone | |
488 | exit ${EXIT_ERROR} | |
489 | ;; | |
490 | esac | |
491 | else | |
492 | action=${1} | |
493 | shift | |
494 | ||
495 | case "${action}" in | |
496 | create) | |
497 | zone_${action} $@ | |
498 | ;; | |
499 | remove) | |
500 | cli_zone_remove $@ | |
501 | ;; | |
502 | list-hooks) | |
503 | cli_list_hooks zone $@ | |
504 | ;; | |
505 | ""|*) | |
506 | if [ -n "${action}" ]; then | |
507 | error "Unrecognized argument: '${action}'" | |
508 | echo | |
509 | fi | |
510 | ||
511 | cli_show_man network-zone | |
512 | exit ${EXIT_ERROR} | |
513 | ;; | |
514 | esac | |
515 | fi | |
516 | } | |
517 | ||
518 | # Removes a zone either immediately, if it is currently down, | |
519 | # or adds a tag that the removal will be done when the zone | |
520 | # is brought down the next time. | |
521 | function cli_zone_remove() { | |
522 | if cli_help_requested $@; then | |
523 | cli_show_man network-zone | |
524 | exit ${EXIT_OK} | |
525 | fi | |
526 | ||
527 | local zone=${1} | |
528 | assert zone_exists ${zone} | |
529 | ||
530 | if zone_is_up ${zone}; then | |
531 | echo "Zone '${zone}' is up and will be removed when it goes down the next time." | |
532 | zone_remove ${zone} | |
533 | else | |
534 | echo "Removing zone '${zone}' now..." | |
535 | zone_remove_now ${zone} | |
536 | fi | |
537 | ||
538 | exit ${EXIT_OK} | |
539 | } | |
540 | ||
541 | function cli_list_hooks() { | |
542 | local type=${1} | |
543 | shift | |
544 | ||
545 | if cli_help_requested $@; then | |
546 | cli_show_man network-zone | |
547 | exit ${EXIT_OK} | |
548 | fi | |
549 | ||
550 | local hook_dir=$(hook_dir ${type}) | |
551 | local hook | |
552 | ||
553 | for hook in ${hook_dir}/*; do | |
554 | hook=$(basename ${hook}) | |
555 | if hook_exists ${type} ${hook}; then | |
556 | echo "${hook}" | |
557 | fi | |
558 | done | sort -u | |
559 | } | |
560 | ||
cb965348 MT |
561 | function cli_route() { |
562 | if cli_help_requested $@; then | |
563 | cli_show_man network-route | |
564 | exit ${EXIT_OK} | |
565 | fi | |
566 | ||
567 | local action=${1} | |
568 | shift | |
569 | ||
570 | case "${action}" in | |
571 | # Add a new route. | |
572 | add) | |
573 | route_add $@ | |
574 | ;; | |
575 | # Remove an existing route. | |
576 | remove) | |
577 | route_remove $@ | |
578 | ;; | |
579 | # List all routes. | |
580 | list) | |
581 | route_list $@ | |
d2021e87 | 582 | return ${EXIT_OK} |
cb965348 MT |
583 | ;; |
584 | *) | |
585 | error "Unrecognized action: ${action}" | |
586 | cli_run_help network route | |
587 | ||
588 | exit ${EXIT_ERROR} | |
589 | ;; | |
590 | esac | |
591 | ||
d2021e87 MT |
592 | # Applying all routes. |
593 | route_apply | |
594 | ||
cb965348 MT |
595 | exit ${EXIT_OK} |
596 | } | |
597 | ||
6c07160e MT |
598 | function cli_dhcpd() { |
599 | local proto=${1} | |
600 | shift | |
601 | ||
602 | if cli_help_requested $@; then | |
603 | cli_show_man network-dhcp | |
604 | exit ${EXIT_OK} | |
605 | fi | |
606 | ||
607 | local action=${1} | |
608 | shift | |
609 | ||
610 | case "${action}" in | |
611 | edit) | |
612 | dhcpd_edit ${proto} $@ | |
613 | ;; | |
614 | start) | |
615 | dhcpd_start ${proto} | |
616 | ;; | |
617 | stop) | |
618 | dhcpd_stop ${proto} | |
619 | ;; | |
620 | restart|reload) | |
621 | dhcpd_reload ${proto} | |
622 | ;; | |
623 | subnet) | |
624 | cli_dhcpd_subnet ${proto} $@ | |
625 | ;; | |
626 | show|"") | |
627 | cli_dhcpd_show ${proto} $@ | |
628 | ;; | |
629 | *) | |
630 | error "Unrecognized action: ${action}" | |
631 | cli_run_help network dhcpvN | |
632 | ||
633 | exit ${EXIT_ERROR} | |
634 | ;; | |
635 | esac | |
636 | ||
637 | exit ${EXIT_OK} | |
638 | } | |
639 | ||
640 | function cli_dhcpd_show() { | |
641 | local proto=${1} | |
642 | assert isset proto | |
643 | ||
644 | local settings=$(dhcpd_settings ${proto}) | |
645 | assert isset settings | |
646 | ||
647 | local ${settings} | |
648 | dhcpd_global_settings_read ${proto} | |
649 | ||
650 | cli_headline 1 "Dynamic Host Configuration Protocol Daemon for ${proto/ip/IP}" | |
651 | ||
652 | case "${proto}" in | |
653 | ipv6) | |
654 | cli_headline 2 "Lease times" | |
655 | if isinteger VALID_LIFETIME; then | |
656 | cli_print_fmt1 2 "Valid lifetime" "${VALID_LIFETIME}s" | |
657 | fi | |
658 | ||
659 | if isinteger PREFERRED_LIFETIME; then | |
660 | cli_print_fmt1 2 "Preferred lifetime" "${PREFERRED_LIFETIME}s" | |
661 | fi | |
662 | ||
663 | cli_space | |
664 | ;; | |
665 | ipv4) | |
666 | cli_print_fmt1 1 "Authoritative" $(cli_print_enabled AUTHORITATIVE) | |
667 | cli_space | |
668 | ||
669 | cli_headline 2 "Lease times" | |
670 | cli_print_fmt1 2 "Default lease time" "${DEFAULT_LEASE_TIME}s" | |
671 | cli_print_fmt1 2 "Max. lease time" "${MAX_LEASE_TIME}s" | |
672 | ||
673 | if isset MIN_LEASE_TIME; then | |
674 | cli_print_fmt1 2 "Min. lease time" "${MIN_LEASE_TIME}s" | |
675 | fi | |
676 | ||
677 | cli_space | |
678 | ;; | |
679 | esac | |
680 | ||
681 | # Read the options. | |
682 | local -A options | |
683 | dhcpd_global_options_read ${proto} ${subnet_id} | |
684 | ||
685 | # Print the options if any. | |
686 | if [ ${#options[*]} -gt 0 ]; then | |
687 | cli_headline 2 "Options" | |
688 | ||
689 | local option | |
690 | for option in $(dhcpd_options ${proto}); do | |
691 | [ -n "${options[${option}]}" ] || continue | |
692 | ||
693 | cli_print_fmt1 2 \ | |
694 | "${option}" "${options[${option}]}" | |
695 | done | |
696 | cli_space | |
697 | fi | |
698 | ||
699 | # Subnets. | |
700 | local subnets=$(dhcpd_subnet_list ${proto}) | |
701 | if [ -n "${subnets}" ]; then | |
702 | cli_headline 2 "Subnets" | |
703 | local subnet_id | |
704 | for subnet_id in ${subnets}; do | |
705 | cli_dhcpd_subnet_show ${proto} ${subnet_id} 2 | |
706 | done | |
707 | fi | |
708 | } | |
709 | ||
710 | function cli_dhcpd_subnet() { | |
711 | local proto=${1} | |
712 | shift | |
713 | ||
714 | if cli_help_requested $@; then | |
715 | cli_show_man network-dhcp-subnet | |
716 | exit ${EXIT_OK} | |
717 | fi | |
718 | ||
719 | local action=${1} | |
720 | shift | |
721 | ||
722 | case "${action}" in | |
723 | new) | |
724 | dhcpd_subnet_new ${proto} $@ | |
725 | ;; | |
726 | remove) | |
727 | dhcpd_subnet_remove ${proto} $@ | |
728 | ;; | |
729 | [0-9]*) | |
730 | local subnet_id=${action} | |
731 | ||
732 | if ! dhcpd_subnet_exists ${proto} ${subnet_id}; then | |
733 | error "The given subnet with ID ${subnet_id} does not exist." | |
734 | return ${EXIT_ERROR} | |
735 | fi | |
736 | ||
737 | # Update the action. | |
738 | action=${1} | |
739 | shift | |
740 | ||
741 | case "${action}" in | |
742 | edit) | |
743 | dhcpd_subnet_edit ${proto} ${subnet_id} $@ | |
744 | local ret=$? | |
745 | ||
746 | if [ ${ret} -eq ${EXIT_OK} ]; then | |
747 | dhcpd_reload ${proto} | |
748 | fi | |
749 | exit ${ret} | |
750 | ;; | |
751 | range) | |
752 | cli_dhcpd_subnet_range ${proto} ${subnet_id} $@ | |
753 | exit $? | |
754 | ;; | |
755 | show) | |
756 | cli_dhcpd_subnet_show ${proto} ${subnet_id} $@ | |
757 | exit $? | |
758 | ;; | |
759 | options) | |
760 | cli_dhcpd_subnet_options ${proto} ${subnet_id} $@ | |
761 | exit $? | |
762 | ;; | |
763 | *) | |
764 | error "Unrecognized action: ${action}" | |
765 | cli_run_help network dhcpvN subnet | |
766 | exit ${EXIT_ERROR} | |
767 | ;; | |
768 | esac | |
769 | ;; | |
770 | show) | |
771 | local subnet_id | |
772 | for subnet_id in $(dhcpd_subnet_list ${proto}); do | |
773 | cli_dhcpd_subnet_show ${proto} ${subnet_id} | |
774 | done | |
775 | ;; | |
776 | *) | |
777 | error "Unrecognized action: ${action}" | |
778 | cli_run_help network dhcpvN subnet | |
779 | ||
780 | exit ${EXIT_ERROR} | |
781 | ;; | |
782 | esac | |
783 | ||
784 | exit ${EXIT_OK} | |
785 | } | |
786 | ||
787 | function cli_dhcpd_subnet_range() { | |
788 | local proto=${1} | |
789 | assert isset proto | |
790 | shift | |
791 | ||
792 | local subnet_id=${1} | |
793 | assert isset subnet_id | |
794 | shift | |
795 | ||
796 | local action=${1} | |
797 | shift | |
798 | ||
799 | case "${action}" in | |
800 | new) | |
801 | dhcpd_subnet_range_new ${proto} ${subnet_id} $@ | |
802 | exit $? | |
803 | ;; | |
804 | remove) | |
805 | dhcpd_subnet_range_remove ${proto} ${subnet_id} $@ | |
806 | exit $? | |
807 | ;; | |
808 | *) | |
809 | error "Unrecognized action: ${action}" | |
810 | cli_run_help network dhcpvN subnet range | |
811 | exit ${EXIT_ERROR} | |
812 | ;; | |
813 | esac | |
814 | } | |
815 | ||
816 | function cli_dhcpd_subnet_show() { | |
817 | local proto=${1} | |
818 | assert isset proto | |
819 | ||
820 | local subnet_id=${2} | |
821 | assert isset subnet_id | |
822 | ||
823 | local level=${3} | |
824 | isset level || level=0 | |
825 | ||
826 | local $(dhcpd_subnet_settings ${proto}) | |
827 | ||
828 | # Read in configuration settings. | |
829 | dhcpd_subnet_read ${proto} ${subnet_id} | |
830 | ||
831 | cli_headline $(( ${level} + 1 )) \ | |
832 | "DHCP${proto/ip/} subnet declaration #${subnet_id}" | |
833 | cli_print_fmt1 $(( ${level} + 1 )) \ | |
834 | "Subnet" "${ADDRESS}/${PREFIX}" | |
835 | cli_space | |
836 | ||
837 | # Read the options. | |
838 | local -A options | |
839 | dhcpd_subnet_options_read ${proto} ${subnet_id} | |
840 | ||
841 | # Print the options if any. | |
842 | if [ ${#options[*]} -gt 0 ]; then | |
843 | cli_headline $(( ${level} + 2 )) "Options" | |
844 | ||
845 | local option | |
846 | for option in $(dhcpd_subnet_options ${proto}); do | |
847 | [ -n "${options[${option}]}" ] || continue | |
848 | ||
849 | cli_print_fmt1 $(( ${level} + 2 )) \ | |
850 | "${option}" "${options[${option}]}" | |
851 | done | |
852 | cli_space | |
853 | fi | |
854 | ||
855 | # Ranges. | |
856 | cli_headline $(( ${level} + 2 )) "Ranges" | |
857 | ||
858 | local ranges=$(dhcpd_subnet_range_list ${proto} ${subnet_id}) | |
859 | if isset ranges; then | |
860 | local range_id $(dhcpd_subnet_range_settings ${proto}) | |
861 | for range_id in ${ranges}; do | |
862 | dhcpd_subnet_range_read ${proto} ${subnet_id} ${range_id} | |
863 | ||
864 | cli_print $(( ${level} + 2 )) \ | |
865 | "#%d: %s - %s" ${range_id} ${START} ${END} | |
866 | done | |
867 | else | |
868 | cli_print $(( ${level} + 2 )) "No ranges have been defined." | |
869 | fi | |
870 | ||
871 | cli_space | |
872 | } | |
873 | ||
874 | function cli_dhcpd_options() { | |
875 | local proto=${1} | |
876 | assert isset proto | |
877 | shift | |
878 | ||
879 | local subnet_id=${1} | |
880 | assert isset subnet_id | |
881 | shift | |
882 | ||
883 | local valid_options=$(dhcpd_subnet_options ${proto}) | |
884 | ||
885 | local key val | |
886 | while [ $# -gt 0 ]; do | |
887 | case "${1}" in | |
888 | *=*) | |
889 | key=$(cli_get_key ${1}) | |
890 | val=$(cli_get_val ${1}) | |
891 | ||
892 | dhcpd_subnet_option_set ${proto} ${subnet_id} ${key} ${val} | |
893 | esac | |
894 | done | |
895 | } | |
896 | ||
9111eb72 MT |
897 | function cli_start() { |
898 | if cli_help_requested $@; then | |
899 | cli_show_man network | |
900 | exit ${EXIT_OK} | |
901 | fi | |
902 | ||
903 | local zones=$(zones_get $@) | |
904 | ||
905 | local zone | |
906 | for zone in ${zones}; do | |
907 | zone_start ${zone} & | |
908 | done | |
909 | ||
910 | wait # until everything is settled | |
911 | } | |
912 | ||
913 | function cli_stop() { | |
914 | if cli_help_requested $@; then | |
915 | cli_show_man network | |
916 | exit ${EXIT_OK} | |
917 | fi | |
918 | ||
919 | local zones=$(zones_get $@) | |
920 | ||
921 | local zone | |
922 | for zone in ${zones}; do | |
923 | zone_stop ${zone} & | |
924 | done | |
925 | ||
926 | wait # until everything is settled | |
927 | } | |
928 | ||
929 | function cli_restart() { | |
930 | if cli_help_requested $@; then | |
931 | cli_show_man network | |
932 | exit ${EXIT_OK} | |
933 | fi | |
934 | ||
935 | cli_stop $@ | |
936 | ||
937 | # Give the system some time to calm down | |
938 | sleep ${TIMEOUT_RESTART} | |
939 | ||
940 | cli_start $@ | |
941 | } | |
942 | ||
943 | function cli_status() { | |
944 | if cli_help_requested $@; then | |
945 | cli_show_man network | |
946 | exit ${EXIT_OK} | |
947 | fi | |
948 | ||
949 | # When dumping status information, the debug | |
950 | # mode clutters the console which is not what we want. | |
951 | # Logging on the console is disabled for a short time. | |
952 | local log_disable_stdout=${LOG_DISABLE_STDOUT} | |
953 | LOG_DISABLE_STDOUT="true" | |
954 | ||
955 | local zones=$(zones_get $@) | |
956 | ||
957 | local zone | |
958 | for zone in ${zones}; do | |
959 | zone_status ${zone} | |
960 | done | |
961 | ||
962 | # Reset logging. | |
963 | LOG_DISABLE_STDOUT=${log_disable_stdout} | |
964 | } | |
965 | ||
966 | function cli_reset() { | |
967 | if cli_help_requested $@; then | |
968 | cli_show_man network | |
969 | exit ${EXIT_OK} | |
970 | fi | |
971 | ||
972 | warning_log "Will reset the whole network configuration!!!" | |
973 | ||
974 | # Force mode is disabled by default | |
975 | local force=0 | |
976 | ||
977 | while [ $# -gt 0 ]; do | |
978 | case "${1}" in | |
979 | --force|-f) | |
980 | force=1 | |
981 | ;; | |
982 | esac | |
983 | shift | |
984 | done | |
985 | ||
986 | # If we are not running in force mode, we ask the user if he does know | |
987 | # what he is doing. | |
988 | if ! enabled force; then | |
989 | if ! cli_yesno "Do you really want to reset the whole network configuration?"; then | |
990 | exit ${EXIT_ERROR} | |
991 | fi | |
992 | fi | |
993 | ||
994 | local zone | |
995 | for zone in $(zones_get --all); do | |
996 | zone_remove ${zone} | |
997 | done | |
998 | ||
999 | local port | |
1000 | for port in $(ports_get --all); do | |
1001 | port_remove ${port} | |
1002 | done | |
1003 | ||
acc9efd5 MT |
1004 | # Flush all DNS servers. |
1005 | dns_server_flush | |
1006 | ||
9111eb72 MT |
1007 | # Re-run the initialization functions |
1008 | init_run | |
1009 | ||
1010 | exit ${EXIT_OK} | |
1011 | } | |
1012 | ||
85afd775 MT |
1013 | # Help function: will show the default man page to the user. |
1014 | # Optionally, there are two arguments taken, the type of hook | |
1015 | # and which hook should be shown. | |
1016 | function cli_help() { | |
1017 | local type=${1} | |
1018 | local what=${2} | |
1019 | ||
1020 | # Remove unknown types. | |
1021 | if ! listmatch ${type} zone port config; then | |
1022 | type="" | |
1023 | fi | |
1024 | ||
1025 | # If no arguments were given, we will show the default page. | |
1026 | if [ -z "${type}" ]; then | |
1027 | cli_show_man network | |
1028 | return ${EXIT_OK} | |
1029 | fi | |
1030 | ||
1031 | if ! hook_exists ${type} ${what}; then | |
1032 | error "Hook of type '${type}' and name '${what}' could not be found." | |
1033 | exit "${EXIT_ERROR}" | |
1034 | fi | |
1035 | ||
1036 | hook_exec ${type} ${what} help | |
1037 | } | |
1038 | ||
6b34112f | 1039 | function cli_dns_server() { |
acc9efd5 | 1040 | if cli_help_requested $@; then |
6b34112f | 1041 | cli_show_man network-dns-server |
acc9efd5 MT |
1042 | exit ${EXIT_OK} |
1043 | fi | |
1044 | ||
1045 | # Get the command. | |
1046 | local cmd=${1}; shift | |
1047 | if [ -z "${cmd}" ]; then | |
6b34112f | 1048 | cli_show_man network-dns-server |
acc9efd5 MT |
1049 | exit ${EXIT_ERROR} |
1050 | fi | |
1051 | ||
6f923dac MT |
1052 | # Get the new server to process (if any). |
1053 | local server=${1} | |
1054 | local priority=${2} | |
1055 | ||
acc9efd5 MT |
1056 | case "${cmd}" in |
1057 | list) | |
acc9efd5 | 1058 | dns_server_list |
e5efaa6b | 1059 | exit ${EXIT_OK} |
acc9efd5 MT |
1060 | ;; |
1061 | add) | |
e5651e17 MT |
1062 | if dns_server_exists ${server}; then |
1063 | error "DNS server '${server}' already exists!" | |
1064 | exit ${EXIT_ERROR} | |
1065 | fi | |
1066 | ||
6f923dac MT |
1067 | log INFO "Adding new DNS server: ${server}" |
1068 | dns_server_add ${server} ${priority} | |
acc9efd5 MT |
1069 | ;; |
1070 | remove) | |
e5651e17 MT |
1071 | if ! dns_server_exists ${server}; then |
1072 | error "DNS server '${server}' does not exist!" | |
1073 | exit ${EXIT_ERROR} | |
1074 | fi | |
1075 | ||
6f923dac MT |
1076 | log INFO "Removing DNS server: ${server}" |
1077 | dns_server_remove ${server} ${priority} | |
acc9efd5 MT |
1078 | ;; |
1079 | update) | |
1080 | # Just run the update afterwards. | |
1081 | ;; | |
1082 | *) | |
1083 | error "No such command: ${cmd}" | |
1084 | exit ${EXIT_ERROR} | |
1085 | esac | |
1086 | ||
1087 | # Update the local DNS configuration after changes have been made. | |
1088 | dns_generate_resolvconf | |
6f923dac | 1089 | radvd_update |
acc9efd5 MT |
1090 | |
1091 | exit ${EXIT_OK} | |
1092 | } | |
1093 | ||
1848564d MT |
1094 | # Process the given action |
1095 | case "${action}" in | |
b8357295 MT |
1096 | init) |
1097 | init_run | |
1098 | ;; | |
1099 | ||
6b34112f | 1100 | config|hostname|port|device|zone|start|stop|restart|status|reset|route) |
0a79ea02 | 1101 | cli_${action} $@ |
1848564d MT |
1102 | ;; |
1103 | ||
6c07160e MT |
1104 | # DHCP server configuration (automatically detects which protocol to use). |
1105 | dhcpv6|dhcpv4) | |
1106 | cli_dhcpd ${action/dhcp/ip} $@ | |
1107 | ;; | |
1108 | ||
6b34112f MT |
1109 | # DNS server configuration. |
1110 | dns-server) | |
1111 | cli_dns_server $@ | |
1112 | ;; | |
1113 | ||
fe4555b5 | 1114 | ""|help|--help|-h) |
85afd775 | 1115 | cli_help $@ |
1848564d | 1116 | ;; |
fe4555b5 | 1117 | |
1848564d | 1118 | *) |
fe4555b5 | 1119 | error "Invalid command given: ${action}" |
de28a630 | 1120 | cli_usage "network help" |
1848564d | 1121 | exit ${EXIT_CONF_ERROR} |
fe4555b5 | 1122 | ;; |
1848564d | 1123 | esac |
85afd775 MT |
1124 | |
1125 | exit ${EXIT_OK} |