]> git.ipfire.org Git - people/ms/network.git/blob - functions.cli
Reduce startup time by letting systemd create our files in /run.
[people/ms/network.git] / functions.cli
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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
22 function cli_config() {
23 if cli_help_requested $@; then
24 cli_usage root-config
25 exit ${EXIT_OK}
26 fi
27
28 if [ -n "${1}" ]; then
29 network_config_set $@
30 else
31 network_config_print
32 fi
33 }
34
35 function cli_device() {
36 local device=${1}
37 local action=${2}
38 shift 2
39
40 assert device_exists ${device}
41
42 if zone_exists ${device} || port_exists ${device}; then
43 error "The device '${device}' has already been configured."
44 error "You cannot do a device action."
45 return ${EXIT_ERROR}
46 fi
47
48 case "${action}" in
49 discover)
50 echo "# XXX need to implement --raw here"
51 cli_device_discover ${device} $@
52 ;;
53
54 show|"")
55 # XXX device_show needs to be implemented
56 device_show ${device}
57 ;;
58 *)
59 cli_usage device
60 ;;
61 esac
62 }
63
64 function cli_device_discover() {
65 local device=${1}
66 shift
67
68 local device_type=$(device_get_type ${device})
69 if [ "${device_type}" != "real" ]; then
70 return ${EXIT_OK}
71 fi
72
73 local raw
74
75 while [ $# -gt 0 ]; do
76 case "${1}" in
77 --raw)
78 raw=1
79 ;;
80 esac
81 shift
82 done
83
84 local up
85 device_is_up ${device} && up=1
86 device_set_up ${device}
87
88 enabled raw || echo "${device}"
89
90 local hook
91 local out
92 local ret
93 for hook in $(hook_zone_get_all); do
94 out=$(hook_zone_exec ${hook} discover ${device})
95 ret=$?
96
97 [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
98
99 if enabled raw; then
100 case "${ret}" in
101 ${DISCOVER_OK})
102 echo "${hook}: OK"
103 local line
104 while read line; do
105 echo "${hook}: ${line}"
106 done <<<"${out}"
107 ;;
108
109 ${DISCOVER_ERROR})
110 echo "${hook}: FAILED"
111 ;;
112 esac
113 else
114 case "${ret}" in
115 ${DISCOVER_OK})
116 echo " ${hook} was successful."
117 local line
118 while read line; do
119 echo " ${line}"
120 done <<<"${out}"
121 ;;
122
123 ${DISCOVER_ERROR})
124 echo " ${hook} failed."
125 ;;
126 esac
127 fi
128 done
129
130 echo # New line
131
132 [ "${up}" = "1" ] || device_set_down ${device}
133 }
134
135 function cli_hostname() {
136 if cli_help_requested $@; then
137 cli_usage hostname
138 exit ${EXIT_OK}
139 fi
140
141 local hostname=${1}
142
143 if [ -n "${hostname}" ]; then
144 config_hostname ${hostname}
145 log INFO "Hostname was set to '${hostname}'."
146 log INFO "Changes do only take affect after reboot."
147 exit ${EXIT_OK}
148 fi
149
150 echo "$(config_hostname)"
151 exit ${EXIT_OK}
152 }
153
154 function cli_hotplug() {
155 if cli_help_requested $@; then
156 cli_usage root-hotplug
157 exit ${EXIT_OK}
158 fi
159
160 local command=${1}
161 shift
162
163 case "${command}" in
164 device)
165 device_hotplug $@
166 exit $?
167 ;;
168 *)
169 cli_usage root-hotplug
170 exit ${EXIT_OK}
171 ;;
172 esac
173 }
174
175 function cli_port() {
176 if cli_help_requested $@; then
177 cli_usage root-port
178 exit ${EXIT_OK}
179 fi
180
181 local action
182 local port
183
184 if port_exists ${1}; then
185 port=${1}
186 action=${2}
187 shift 2
188
189 # Action aliases
190 case "${action}" in
191 start)
192 action="up"
193 ;;
194 stop)
195 action="down"
196 ;;
197 show)
198 action="status"
199 ;;
200 esac
201
202 case "${action}" in
203 edit|up|down|status)
204 port_${action} ${port} $@
205 ;;
206 *)
207 error "Unrecognized argument: ${action}"
208 exit ${EXIT_ERROR}
209 ;;
210 esac
211 else
212 action=${1}
213 shift
214
215 case "${action}" in
216 create|destroy)
217 port_${action} $@
218 ;;
219 *)
220 error "Unrecognized argument: ${action}"
221 exit ${EXIT_ERROR}
222 ;;
223 esac
224 fi
225 }
226
227 function cli_zone() {
228 if cli_help_requested $@; then
229 cli_usage root-zone
230 exit ${EXIT_OK}
231 fi
232
233 local action
234 local zone
235
236 if zone_name_is_valid ${1}; then
237 zone=${1}
238 action=${2}
239 shift 2
240
241 # Action aliases
242 case "${action}" in
243 start)
244 action="up"
245 ;;
246 stop)
247 action="down"
248 ;;
249 show)
250 action="status"
251 ;;
252 esac
253
254 case "${action}" in
255 config|down|edit|port|status|up)
256 zone_${action} ${zone} $@
257 ;;
258 *)
259 error "Unrecognized argument: ${action}"
260 cli_usage root-zone-subcommands
261 exit ${EXIT_ERROR}
262 ;;
263 esac
264 else
265 action=${1}
266 shift
267
268 case "${action}" in
269 create|remove)
270 zone_${action} $@
271 ;;
272 ""|*)
273 if [ -n "${action}" ]; then
274 error "Unrecognized argument: '${action}'"
275 echo
276 fi
277
278 cli_usage root-zone
279 exit ${EXIT_ERROR}
280 ;;
281 esac
282 fi
283 }
284
285 function cli_start() {
286 if cli_help_requested $@; then
287 cli_usage root-start
288 exit ${EXIT_OK}
289 fi
290
291 local zones=$(zones_get $@)
292
293 local zone
294 for zone in ${zones}; do
295 zone_start ${zone} &
296 done
297
298 wait # until everything is settled
299 }
300
301 function cli_stop() {
302 if cli_help_requested $@; then
303 cli_usage root-stop
304 exit ${EXIT_OK}
305 fi
306
307 local zones=$(zones_get $@)
308
309 local zone
310 for zone in ${zones}; do
311 zone_stop ${zone} &
312 done
313
314 wait # until everything is settled
315 }
316
317 function cli_restart() {
318 if cli_help_requested $@; then
319 cli_usage root-restart
320 exit ${EXIT_OK}
321 fi
322
323 cli_stop $@
324
325 # Give the system some time to calm down
326 sleep ${TIMEOUT_RESTART}
327
328 cli_start $@
329 }
330
331 function cli_status() {
332 if cli_help_requested $@; then
333 cli_usage root-status
334 exit ${EXIT_OK}
335 fi
336
337 # When dumping status information, the debug
338 # mode clutters the console which is not what we want.
339 # Logging on the console is disabled for a short time.
340 local log_disable_stdout=${LOG_DISABLE_STDOUT}
341 LOG_DISABLE_STDOUT="true"
342
343 local zones=$(zones_get $@)
344
345 local zone
346 for zone in ${zones}; do
347 zone_status ${zone}
348 done
349
350 # Reset logging.
351 LOG_DISABLE_STDOUT=${log_disable_stdout}
352 }
353
354 function cli_reset() {
355 if cli_help_requested $@; then
356 cli_usage root-reset
357 exit ${EXIT_OK}
358 fi
359
360 warning_log "Will reset the whole network configuration!!!"
361
362 # Force mode is disabled by default
363 local force=0
364
365 while [ $# -gt 0 ]; do
366 case "${1}" in
367 --force|-f)
368 force=1
369 ;;
370 esac
371 shift
372 done
373
374 # If we are not running in force mode, we ask the user if he does know
375 # what he is doing.
376 if ! enabled force; then
377 if ! cli_yesno "Do you really want to reset the whole network configuration?"; then
378 exit ${EXIT_ERROR}
379 fi
380 fi
381
382 local zone
383 for zone in $(zones_get --all); do
384 zone_remove ${zone}
385 done
386
387 local port
388 for port in $(ports_get --all); do
389 port_remove ${port}
390 done
391
392 # Re-run the initialization functions
393 init_run
394
395 exit ${EXIT_OK}
396 }
397
398 function cli_help_requested() {
399 local argument="${1}"
400
401 if [ -n "${argument}" ]; then
402 if listmatch ${argument} help -h --help; then
403 return ${EXIT_OK}
404 fi
405 fi
406
407 return ${EXIT_ERROR}
408 }
409
410 function cli_usage() {
411 local what=${1}
412
413 case "${what}" in
414 root)
415 echo "${0}: [command] <options ...>"
416 echo
417 echo " start - ..."
418 echo " stop - ..."
419 echo " restart - ..."
420 echo " status - ..."
421 echo
422 echo " config - ..."
423 echo
424 echo " device - ..."
425 echo " zone - ..."
426 echo
427 ;;
428 root-config)
429 echo "${0}: ${what#root-} [KEY=VAL, ...]"
430 echo
431 echo " This command allows setting of global configuration parameters."
432 echo
433 echo " If no additional arguments are passed it will list the current configuration."
434 echo
435 echo " You can overwrite the settings like the following:"
436 echo
437 echo " ${0} ${what#root-} DEBUG=1 ..."
438 echo
439 ;;
440 root-reset)
441 echo "${0}: ${what#root-} [--force | -f]"
442 echo
443 echo " This command resets the network configuration."
444 echo
445 echo " Will delete all zones and ports."
446 echo
447 echo -e " ${COLOUR_RED}USE WITH CAUTION!${COLOUR_NORMAL}"
448 echo
449 ;;
450 root-start|root-stop|root-restart)
451 echo "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]"
452 echo
453 echo " This commands ${what#root-}s all zones by default."
454 echo " One can pass several parameters to only process a subset of all"
455 echo " available zones:"
456 echo
457 echo -e " ${COLOUR_BOLD}--local-only${COLOUR_NORMAL}"
458 echo " Process all local zones which includes every zone without red."
459 echo
460 echo -e " ${COLOUR_BOLD}--remote-only${COLOUR_NORMAL}"
461 echo " Process all remote zones which means only the red ones."
462 echo
463 echo -e " ${COLOUR_BOLD}--all${COLOUR_NORMAL}"
464 echo " Process all zones. This is the default parameter."
465 echo
466 echo " Additionally, you can pass one or more zone names which will"
467 echo " be processed."
468 echo
469 ;;
470 root-status)
471 echo "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]"
472 echo
473 echo " This commands shows status information of all zones by default."
474 echo " One can pass several parameters to only process a subset of all"
475 echo " available zones:"
476 echo
477 echo -e " ${COLOUR_BOLD}--local-only${COLOUR_NORMAL}"
478 echo " Process all local zones which includes every zone without red."
479 echo
480 echo -e " ${COLOUR_BOLD}--remote-only${COLOUR_NORMAL}"
481 echo " Process all remote zones which means only the red ones."
482 echo
483 echo -e " ${COLOUR_BOLD}--all${COLOUR_NORMAL}"
484 echo " Process all zones. This is the default parameter."
485 echo
486 echo " Additionally, you can pass one or more zone names which will"
487 echo " be processed."
488 echo
489 ;;
490 root-zone)
491 echo "${0}: ${what#root-} <create|remove> <zone> [<type> <options...>]"
492 echo
493 echo " Create or remove a zone."
494 echo
495 echo -e " ${COLOUR_BOLD}create <zone> <type> <options>${COLOUR_NORMAL}"
496 echo " Create a new zone of type <type> where <zone> is an allowed"
497 echo " zone name."
498 echo
499 echo -e " ${COLOUR_BOLD}remove <zone>${COLOUR_NORMAL}"
500 echo " Remove the zone <zone>."
501 echo
502 echo " You may also edit the configuration of the zones."
503 echo
504 echo -e " ${COLOUR_BOLD}<zone> ...${COLOUR_NORMAL}"
505 echo " Edit the zone <zone>."
506 echo
507 ;;
508 usage)
509 echo
510 echo " Run '${0} help' to get information how to use this tool."
511 echo
512 ;;
513 *)
514 error "No help available for this command '${what}'."
515 echo
516 ;;
517 esac
518
519 echo "Network configuration tool. Report all bugs to <http://bugs.ipfire.org>."
520 }
521
522 function cli_status_headline() {
523 local zone=${1}
524
525 local state="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}"
526 zone_is_up ${zone} && state="${COLOUR_UP}UP${COLOUR_NORMAL}"
527
528 echo -e "${zone} - ${state} - $(zone_get_hook ${zone})"
529 }
530
531 function cli_headline() {
532 echo
533 echo -e "${COLOUR_BOLD}$@${COLOUR_NORMAL}"
534 }
535
536 function cli_yesno() {
537 local message="$@ [y/N] "
538 local yesno
539
540 echo
541 echo -ne "${message}"
542 read yesno
543
544 if listmatch ${yesno} y Y j J yes YES Yes; then
545 return ${EXIT_OK}
546 fi
547
548 return ${EXIT_ERROR}
549 }
550
551 function cli_get_key() {
552 local key="${1%%=*}"
553 echo "${key/--/}"
554 }
555
556 function cli_get_val() {
557 echo "${@##*=}"
558 }