]> git.ipfire.org Git - people/stevee/network.git/blame - functions.cli
Add "network zone list-hooks" command.
[people/stevee/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 23 if cli_help_requested $@; then
d2a21d01 24 cli_show_man network-config
fe688aa4
MT
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 *)
de28a630 60 cli_show_man network-device
8a4ccede
MT
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
de28a630 138 cli_show_man network
31e59f2b
MT
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
711ffac1
MT
155function cli_port() {
156 if cli_help_requested $@; then
de28a630 157 cli_show_man network-port
711ffac1
MT
158 exit ${EXIT_OK}
159 fi
160
161 local action
162 local port
163
164 if port_exists ${1}; then
165 port=${1}
166 action=${2}
167 shift 2
168
169 # Action aliases
170 case "${action}" in
171 start)
172 action="up"
173 ;;
174 stop)
175 action="down"
176 ;;
177 show)
178 action="status"
179 ;;
180 esac
181
182 case "${action}" in
183 edit|up|down|status)
184 port_${action} ${port} $@
185 ;;
186 *)
187 error "Unrecognized argument: ${action}"
188 exit ${EXIT_ERROR}
189 ;;
190 esac
191 else
192 action=${1}
193 shift
194
195 case "${action}" in
196 create|destroy)
197 port_${action} $@
198 ;;
199 *)
200 error "Unrecognized argument: ${action}"
201 exit ${EXIT_ERROR}
202 ;;
203 esac
204 fi
205}
206
1848564d 207function cli_zone() {
8db9698f 208 if cli_help_requested $@; then
de28a630 209 cli_show_man network-zone
8db9698f
MT
210 exit ${EXIT_OK}
211 fi
212
1848564d
MT
213 local action
214 local zone
215
216 if zone_name_is_valid ${1}; then
217 zone=${1}
218 action=${2}
219 shift 2
220
711ffac1
MT
221 # Action aliases
222 case "${action}" in
223 start)
224 action="up"
225 ;;
226 stop)
227 action="down"
228 ;;
229 show)
230 action="status"
231 ;;
232 esac
233
1848564d 234 case "${action}" in
711ffac1 235 config|down|edit|port|status|up)
1848564d
MT
236 zone_${action} ${zone} $@
237 ;;
8db9698f
MT
238 *)
239 error "Unrecognized argument: ${action}"
de28a630 240 cli_show_man network-zone
8db9698f
MT
241 exit ${EXIT_ERROR}
242 ;;
1848564d
MT
243 esac
244 else
245 action=${1}
246 shift
247
248 case "${action}" in
69ace22b 249 create)
1848564d
MT
250 zone_${action} $@
251 ;;
69ace22b
MT
252 remove)
253 cli_zone_remove $@
254 ;;
3871df73
MT
255 list-hooks)
256 cli_list_hooks zone $@
257 ;;
8db9698f
MT
258 ""|*)
259 if [ -n "${action}" ]; then
260 error "Unrecognized argument: '${action}'"
261 echo
262 fi
263
de28a630 264 cli_show_man network-zone
8db9698f 265 exit ${EXIT_ERROR}
1848564d
MT
266 ;;
267 esac
268 fi
269}
270
3871df73
MT
271function cli_list_hooks() {
272 local type=${1}
273 shift
274
275 if cli_help_requested $@; then
276 cli_show_man network-zone
277 exit ${EXIT_OK}
278 fi
279
280 local hook_dir=$(hook_dir ${type})
281 local hook
282
283 for hook in ${hook_dir}/*; do
284 hook=$(basename ${hook})
285 if hook_exists ${type} ${hook}; then
286 echo "${hook}"
287 fi
288 done | sort -u
289}
290
69ace22b
MT
291# Removes a zone either immediately, if it is currently down,
292# or adds a tag that the removal will be done when the zone
293# is brought down the next time.
294function cli_zone_remove() {
295 if cli_help_requested $@; then
296 cli_show_man network-zone
297 exit ${EXIT_OK}
298 fi
299
300 local zone=${1}
301 assert zone_exists ${zone}
302
303 if zone_is_up ${zone}; then
304 echo "Zone '${zone}' is up and will be removed when it goes down the next time."
305 zone_remove ${zone}
306 else
307 echo "Removing zone '${zone}' now..."
308 zone_remove_now ${zone}
309 fi
310
311 exit ${EXIT_OK}
312}
313
1848564d 314function cli_start() {
1d7bc4f3 315 if cli_help_requested $@; then
de28a630 316 cli_show_man network
1d7bc4f3
MT
317 exit ${EXIT_OK}
318 fi
319
1848564d
MT
320 local zones=$(zones_get $@)
321
322 local zone
323 for zone in ${zones}; do
5bb2429a 324 zone_start ${zone} &
1848564d 325 done
5bb2429a
MT
326
327 wait # until everything is settled
1848564d
MT
328}
329
330function cli_stop() {
1d7bc4f3 331 if cli_help_requested $@; then
de28a630 332 cli_show_man network
1d7bc4f3
MT
333 exit ${EXIT_OK}
334 fi
335
1848564d
MT
336 local zones=$(zones_get $@)
337
338 local zone
339 for zone in ${zones}; do
5bb2429a 340 zone_stop ${zone} &
1848564d 341 done
5bb2429a
MT
342
343 wait # until everything is settled
1848564d
MT
344}
345
bcef495d
MT
346function cli_restart() {
347 if cli_help_requested $@; then
de28a630 348 cli_show_man network
bcef495d
MT
349 exit ${EXIT_OK}
350 fi
351
352 cli_stop $@
353
354 # Give the system some time to calm down
2ae4f579 355 sleep ${TIMEOUT_RESTART}
bcef495d
MT
356
357 cli_start $@
358}
359
4c5857b2
MT
360function cli_status() {
361 if cli_help_requested $@; then
de28a630 362 cli_show_man network
4c5857b2
MT
363 exit ${EXIT_OK}
364 fi
365
67d6dbb7
MT
366 # When dumping status information, the debug
367 # mode clutters the console which is not what we want.
368 # Logging on the console is disabled for a short time.
369 local log_disable_stdout=${LOG_DISABLE_STDOUT}
370 LOG_DISABLE_STDOUT="true"
371
4c5857b2
MT
372 local zones=$(zones_get $@)
373
374 local zone
375 for zone in ${zones}; do
376 zone_status ${zone}
377 done
67d6dbb7
MT
378
379 # Reset logging.
380 LOG_DISABLE_STDOUT=${log_disable_stdout}
4c5857b2
MT
381}
382
f90e550b
MT
383function cli_reset() {
384 if cli_help_requested $@; then
de28a630 385 cli_show_man network
f90e550b
MT
386 exit ${EXIT_OK}
387 fi
388
389 warning_log "Will reset the whole network configuration!!!"
390
391 # Force mode is disabled by default
392 local force=0
393
394 while [ $# -gt 0 ]; do
395 case "${1}" in
396 --force|-f)
397 force=1
398 ;;
399 esac
400 shift
401 done
402
403 # If we are not running in force mode, we ask the user if he does know
404 # what he is doing.
405 if ! enabled force; then
406 if ! cli_yesno "Do you really want to reset the whole network configuration?"; then
407 exit ${EXIT_ERROR}
408 fi
409 fi
410
411 local zone
412 for zone in $(zones_get --all); do
413 zone_remove ${zone}
414 done
415
416 local port
417 for port in $(ports_get --all); do
418 port_remove ${port}
419 done
420
2ae0fb8d
MT
421 # Re-run the initialization functions
422 init_run
f90e550b
MT
423
424 exit ${EXIT_OK}
425}
426
1d7bc4f3 427function cli_help_requested() {
866de228
MT
428 local argument="${1}"
429
430 if [ -n "${argument}" ]; then
431 if listmatch ${argument} help -h --help; then
1d7bc4f3
MT
432 return ${EXIT_OK}
433 fi
866de228 434 fi
1d7bc4f3
MT
435
436 return ${EXIT_ERROR}
437}
438
9178284d
MT
439function cli_status_headline() {
440 local zone=${1}
441
442 local state="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}"
443 zone_is_up ${zone} && state="${COLOUR_UP}UP${COLOUR_NORMAL}"
444
445 echo -e "${zone} - ${state} - $(zone_get_hook ${zone})"
446}
447
448function cli_headline() {
449 echo
450 echo -e "${COLOUR_BOLD}$@${COLOUR_NORMAL}"
451}
f90e550b
MT
452
453function cli_yesno() {
454 local message="$@ [y/N] "
455 local yesno
456
457 echo
458 echo -ne "${message}"
459 read yesno
460
461 if listmatch ${yesno} y Y j J yes YES Yes; then
462 return ${EXIT_OK}
463 fi
464
465 return ${EXIT_ERROR}
466}
d76f5107
MT
467
468function cli_get_key() {
469 local key="${1%%=*}"
470 echo "${key/--/}"
471}
472
473function cli_get_val() {
e9ea243e 474 echo "${@##*=}"
d76f5107 475}
de28a630
MT
476
477function cli_usage() {
478 local command="$@"
479 local basename="$(basename ${0})"
480
481 if ! isset command; then
482 command="${basename} help"
483 fi
484
485 echo "The given command was not understood by ${basename}." >&2
486 echo "Please run '${command}' for detailed help." >&2
487}
488
489function cli_show_man() {
490 local manpage=${1}
491 assert isset manpage
492
493 if ! binary_exists man; then
494 error "The man package is not installed on this system."
495 error "Please install 'man' in order to view the help."
496 exit ${EXIT_ERROR}
497 fi
498
499 man ${manpage}
500}