]> git.ipfire.org Git - people/stevee/network.git/blob - functions.cli
Add documentation about the config options.
[people/stevee/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_show_man network-config
25 exit ${EXIT_OK}
26 fi
27
28 if [ -n "${1}" ]; then
29 config_set $@
30 network_config_write
31 else
32 network_config_print
33 fi
34 }
35
36 function cli_device() {
37 local device=${1}
38 local action=${2}
39 shift 2
40
41 assert device_exists ${device}
42
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
48
49 case "${action}" in
50 discover)
51 echo "# XXX need to implement --raw here"
52 cli_device_discover ${device} $@
53 ;;
54
55 show|"")
56 # XXX device_show needs to be implemented
57 device_show ${device}
58 ;;
59 *)
60 cli_show_man network-device
61 ;;
62 esac
63 }
64
65 function 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
94 for hook in $(hook_zone_get_all); do
95 out=$(hook_zone_exec ${hook} discover ${device})
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
136 function cli_hostname() {
137 if cli_help_requested $@; then
138 cli_show_man network
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
155 function cli_port() {
156 if cli_help_requested $@; then
157 cli_show_man network-port
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
207 function cli_zone() {
208 if cli_help_requested $@; then
209 cli_show_man network-zone
210 exit ${EXIT_OK}
211 fi
212
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
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
234 case "${action}" in
235 config|down|edit|port|status|up)
236 zone_${action} ${zone} $@
237 ;;
238 *)
239 error "Unrecognized argument: ${action}"
240 cli_show_man network-zone
241 exit ${EXIT_ERROR}
242 ;;
243 esac
244 else
245 action=${1}
246 shift
247
248 case "${action}" in
249 create)
250 zone_${action} $@
251 ;;
252 remove)
253 cli_zone_remove $@
254 ;;
255 ""|*)
256 if [ -n "${action}" ]; then
257 error "Unrecognized argument: '${action}'"
258 echo
259 fi
260
261 cli_show_man network-zone
262 exit ${EXIT_ERROR}
263 ;;
264 esac
265 fi
266 }
267
268 # Removes a zone either immediately, if it is currently down,
269 # or adds a tag that the removal will be done when the zone
270 # is brought down the next time.
271 function cli_zone_remove() {
272 if cli_help_requested $@; then
273 cli_show_man network-zone
274 exit ${EXIT_OK}
275 fi
276
277 local zone=${1}
278 assert zone_exists ${zone}
279
280 if zone_is_up ${zone}; then
281 echo "Zone '${zone}' is up and will be removed when it goes down the next time."
282 zone_remove ${zone}
283 else
284 echo "Removing zone '${zone}' now..."
285 zone_remove_now ${zone}
286 fi
287
288 exit ${EXIT_OK}
289 }
290
291 function cli_start() {
292 if cli_help_requested $@; then
293 cli_show_man network
294 exit ${EXIT_OK}
295 fi
296
297 local zones=$(zones_get $@)
298
299 local zone
300 for zone in ${zones}; do
301 zone_start ${zone} &
302 done
303
304 wait # until everything is settled
305 }
306
307 function cli_stop() {
308 if cli_help_requested $@; then
309 cli_show_man network
310 exit ${EXIT_OK}
311 fi
312
313 local zones=$(zones_get $@)
314
315 local zone
316 for zone in ${zones}; do
317 zone_stop ${zone} &
318 done
319
320 wait # until everything is settled
321 }
322
323 function cli_restart() {
324 if cli_help_requested $@; then
325 cli_show_man network
326 exit ${EXIT_OK}
327 fi
328
329 cli_stop $@
330
331 # Give the system some time to calm down
332 sleep ${TIMEOUT_RESTART}
333
334 cli_start $@
335 }
336
337 function cli_status() {
338 if cli_help_requested $@; then
339 cli_show_man network
340 exit ${EXIT_OK}
341 fi
342
343 # When dumping status information, the debug
344 # mode clutters the console which is not what we want.
345 # Logging on the console is disabled for a short time.
346 local log_disable_stdout=${LOG_DISABLE_STDOUT}
347 LOG_DISABLE_STDOUT="true"
348
349 local zones=$(zones_get $@)
350
351 local zone
352 for zone in ${zones}; do
353 zone_status ${zone}
354 done
355
356 # Reset logging.
357 LOG_DISABLE_STDOUT=${log_disable_stdout}
358 }
359
360 function cli_reset() {
361 if cli_help_requested $@; then
362 cli_show_man network
363 exit ${EXIT_OK}
364 fi
365
366 warning_log "Will reset the whole network configuration!!!"
367
368 # Force mode is disabled by default
369 local force=0
370
371 while [ $# -gt 0 ]; do
372 case "${1}" in
373 --force|-f)
374 force=1
375 ;;
376 esac
377 shift
378 done
379
380 # If we are not running in force mode, we ask the user if he does know
381 # what he is doing.
382 if ! enabled force; then
383 if ! cli_yesno "Do you really want to reset the whole network configuration?"; then
384 exit ${EXIT_ERROR}
385 fi
386 fi
387
388 local zone
389 for zone in $(zones_get --all); do
390 zone_remove ${zone}
391 done
392
393 local port
394 for port in $(ports_get --all); do
395 port_remove ${port}
396 done
397
398 # Re-run the initialization functions
399 init_run
400
401 exit ${EXIT_OK}
402 }
403
404 function cli_help_requested() {
405 local argument="${1}"
406
407 if [ -n "${argument}" ]; then
408 if listmatch ${argument} help -h --help; then
409 return ${EXIT_OK}
410 fi
411 fi
412
413 return ${EXIT_ERROR}
414 }
415
416 function cli_status_headline() {
417 local zone=${1}
418
419 local state="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}"
420 zone_is_up ${zone} && state="${COLOUR_UP}UP${COLOUR_NORMAL}"
421
422 echo -e "${zone} - ${state} - $(zone_get_hook ${zone})"
423 }
424
425 function cli_headline() {
426 echo
427 echo -e "${COLOUR_BOLD}$@${COLOUR_NORMAL}"
428 }
429
430 function cli_yesno() {
431 local message="$@ [y/N] "
432 local yesno
433
434 echo
435 echo -ne "${message}"
436 read yesno
437
438 if listmatch ${yesno} y Y j J yes YES Yes; then
439 return ${EXIT_OK}
440 fi
441
442 return ${EXIT_ERROR}
443 }
444
445 function cli_get_key() {
446 local key="${1%%=*}"
447 echo "${key/--/}"
448 }
449
450 function cli_get_val() {
451 echo "${@##*=}"
452 }
453
454 function cli_usage() {
455 local command="$@"
456 local basename="$(basename ${0})"
457
458 if ! isset command; then
459 command="${basename} help"
460 fi
461
462 echo "The given command was not understood by ${basename}." >&2
463 echo "Please run '${command}' for detailed help." >&2
464 }
465
466 function cli_show_man() {
467 local manpage=${1}
468 assert isset manpage
469
470 if ! binary_exists man; then
471 error "The man package is not installed on this system."
472 error "Please install 'man' in order to view the help."
473 exit ${EXIT_ERROR}
474 fi
475
476 man ${manpage}
477 }