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