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