]> git.ipfire.org Git - people/arne_f/network.git/blob - functions.cli
Fix weird device CLI command.
[people/arne_f/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_port() {
136 if cli_help_requested $@; then
137 cli_usage root-port
138 exit ${EXIT_OK}
139 fi
140
141 local action
142 local port
143
144 if port_exists ${1}; then
145 port=${1}
146 action=${2}
147 shift 2
148
149 # Action aliases
150 case "${action}" in
151 start)
152 action="up"
153 ;;
154 stop)
155 action="down"
156 ;;
157 show)
158 action="status"
159 ;;
160 esac
161
162 case "${action}" in
163 edit|up|down|status)
164 port_${action} ${port} $@
165 ;;
166 *)
167 error "Unrecognized argument: ${action}"
168 exit ${EXIT_ERROR}
169 ;;
170 esac
171 else
172 action=${1}
173 shift
174
175 case "${action}" in
176 create|destroy)
177 port_${action} $@
178 ;;
179 *)
180 error "Unrecognized argument: ${action}"
181 exit ${EXIT_ERROR}
182 ;;
183 esac
184 fi
185 }
186
187 function cli_zone() {
188 if cli_help_requested $@; then
189 cli_usage root-zone
190 exit ${EXIT_OK}
191 fi
192
193 local action
194 local zone
195
196 if zone_name_is_valid ${1}; then
197 zone=${1}
198 action=${2}
199 shift 2
200
201 # Action aliases
202 case "${action}" in
203 start)
204 action="up"
205 ;;
206 stop)
207 action="down"
208 ;;
209 show)
210 action="status"
211 ;;
212 esac
213
214 case "${action}" in
215 config|down|edit|port|status|up)
216 zone_${action} ${zone} $@
217 ;;
218 *)
219 error "Unrecognized argument: ${action}"
220 cli_usage root-zone-subcommands
221 exit ${EXIT_ERROR}
222 ;;
223 esac
224 else
225 action=${1}
226 shift
227
228 case "${action}" in
229 create|remove)
230 zone_${action} $@
231 ;;
232 ""|*)
233 if [ -n "${action}" ]; then
234 error "Unrecognized argument: '${action}'"
235 echo
236 fi
237
238 cli_usage root-zone
239 exit ${EXIT_ERROR}
240 ;;
241 esac
242 fi
243 }
244
245 function cli_start() {
246 if cli_help_requested $@; then
247 cli_usage root-start
248 exit ${EXIT_OK}
249 fi
250
251 local zones=$(zones_get $@)
252
253 local zone
254 for zone in ${zones}; do
255 zone_up ${zone}
256 done
257 }
258
259 function cli_stop() {
260 if cli_help_requested $@; then
261 cli_usage root-stop
262 exit ${EXIT_OK}
263 fi
264
265 local zones=$(zones_get $@)
266
267 local zone
268 for zone in ${zones}; do
269 zone_down ${zone}
270 done
271 }
272
273 function cli_restart() {
274 if cli_help_requested $@; then
275 cli_usage root-restart
276 exit ${EXIT_OK}
277 fi
278
279 cli_stop $@
280
281 # Give the system some time to calm down
282 sleep ${TIMEOUT_RESTART}
283
284 cli_start $@
285 }
286
287 function cli_status() {
288 if cli_help_requested $@; then
289 cli_usage root-status
290 exit ${EXIT_OK}
291 fi
292
293 local zones=$(zones_get $@)
294
295 local zone
296 for zone in ${zones}; do
297 zone_status ${zone}
298 done
299 }
300
301 function cli_reset() {
302 if cli_help_requested $@; then
303 cli_usage root-reset
304 exit ${EXIT_OK}
305 fi
306
307 warning_log "Will reset the whole network configuration!!!"
308
309 # Force mode is disabled by default
310 local force=0
311
312 while [ $# -gt 0 ]; do
313 case "${1}" in
314 --force|-f)
315 force=1
316 ;;
317 esac
318 shift
319 done
320
321 # If we are not running in force mode, we ask the user if he does know
322 # what he is doing.
323 if ! enabled force; then
324 if ! cli_yesno "Do you really want to reset the whole network configuration?"; then
325 exit ${EXIT_ERROR}
326 fi
327 fi
328
329 local zone
330 for zone in $(zones_get --all); do
331 zone_remove ${zone}
332 done
333
334 local port
335 for port in $(ports_get --all); do
336 port_remove ${port}
337 done
338
339 # Re-run the initialization functions
340 init_run
341
342 exit ${EXIT_OK}
343 }
344
345 function cli_help_requested() {
346 local argument="${1}"
347
348 if [ -n "${argument}" ]; then
349 if listmatch ${argument} help -h --help; then
350 return ${EXIT_OK}
351 fi
352 fi
353
354 return ${EXIT_ERROR}
355 }
356
357 function cli_usage() {
358 local what=${1}
359
360 case "${what}" in
361 root)
362 echo "${0}: [command] <options ...>"
363 echo
364 echo " start - ..."
365 echo " stop - ..."
366 echo " restart - ..."
367 echo " status - ..."
368 echo
369 echo " config - ..."
370 echo
371 echo " device - ..."
372 echo " zone - ..."
373 echo
374 ;;
375 root-config)
376 echo "${0}: ${what#root-} [KEY=VAL, ...]"
377 echo
378 echo " This command allows setting of global configuration parameters."
379 echo
380 echo " If no additional arguments are passed it will list the current configuration."
381 echo
382 echo " You can overwrite the settings like the following:"
383 echo
384 echo " ${0} ${what#root-} DEBUG=1 ..."
385 echo
386 ;;
387 root-reset)
388 echo "${0}: ${what#root-} [--force | -f]"
389 echo
390 echo " This command resets the network configuration."
391 echo
392 echo " Will delete all zones and ports."
393 echo
394 echo -e " ${COLOUR_RED}USE WITH CAUTION!${COLOUR_NORMAL}"
395 echo
396 ;;
397 root-start|root-stop|root-restart)
398 echo "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]"
399 echo
400 echo " This commands ${what#root-}s all zones by default."
401 echo " One can pass several parameters to only process a subset of all"
402 echo " available zones:"
403 echo
404 echo -e " ${COLOUR_BOLD}--local-only${COLOUR_NORMAL}"
405 echo " Process all local zones which includes every zone without red."
406 echo
407 echo -e " ${COLOUR_BOLD}--remote-only${COLOUR_NORMAL}"
408 echo " Process all remote zones which means only the red ones."
409 echo
410 echo -e " ${COLOUR_BOLD}--all${COLOUR_NORMAL}"
411 echo " Process all zones. This is the default parameter."
412 echo
413 echo " Additionally, you can pass one or more zone names which will"
414 echo " be processed."
415 echo
416 ;;
417 root-status)
418 echo "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]"
419 echo
420 echo " This commands shows status information of all zones by default."
421 echo " One can pass several parameters to only process a subset of all"
422 echo " available zones:"
423 echo
424 echo -e " ${COLOUR_BOLD}--local-only${COLOUR_NORMAL}"
425 echo " Process all local zones which includes every zone without red."
426 echo
427 echo -e " ${COLOUR_BOLD}--remote-only${COLOUR_NORMAL}"
428 echo " Process all remote zones which means only the red ones."
429 echo
430 echo -e " ${COLOUR_BOLD}--all${COLOUR_NORMAL}"
431 echo " Process all zones. This is the default parameter."
432 echo
433 echo " Additionally, you can pass one or more zone names which will"
434 echo " be processed."
435 echo
436 ;;
437 root-zone)
438 echo "${0}: ${what#root-} <create|remove> <zone> [<type> <options...>]"
439 echo
440 echo " Create or remove a zone."
441 echo
442 echo -e " ${COLOUR_BOLD}create <zone> <type> <options>${COLOUR_NORMAL}"
443 echo " Create a new zone of type <type> where <zone> is an allowed"
444 echo " zone name."
445 echo
446 echo -e " ${COLOUR_BOLD}remove <zone>${COLOUR_NORMAL}"
447 echo " Remove the zone <zone>."
448 echo
449 echo " You may also edit the configuration of the zones."
450 echo
451 echo -e " ${COLOUR_BOLD}<zone> ...${COLOUR_NORMAL}"
452 echo " Edit the zone <zone>."
453 echo
454 ;;
455 usage)
456 echo
457 echo " Run '${0} help' to get information how to use this tool."
458 echo
459 ;;
460 *)
461 error "No help available for this command '${what}'."
462 echo
463 ;;
464 esac
465
466 echo "Network configuration tool. Report all bugs to <http://bugs.ipfire.org>."
467 }
468
469 function cli_status_headline() {
470 local zone=${1}
471
472 local state="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}"
473 zone_is_up ${zone} && state="${COLOUR_UP}UP${COLOUR_NORMAL}"
474
475 echo -e "${zone} - ${state} - $(zone_get_hook ${zone})"
476 }
477
478 function cli_headline() {
479 echo
480 echo -e "${COLOUR_BOLD}$@${COLOUR_NORMAL}"
481 }
482
483 function cli_yesno() {
484 local message="$@ [y/N] "
485 local yesno
486
487 echo
488 echo -ne "${message}"
489 read yesno
490
491 if listmatch ${yesno} y Y j J yes YES Yes; then
492 return ${EXIT_OK}
493 fi
494
495 return ${EXIT_ERROR}
496 }
497
498 function cli_get_key() {
499 local key="${1%%=*}"
500 echo "${key/--/}"
501 }
502
503 function cli_get_val() {
504 echo "${1##*=}"
505 }