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