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