]> git.ipfire.org Git - people/arne_f/network.git/blob - functions.cli
network: Magnificent changes on code.
[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 if device_config_exists ${1}; then
37 local device=${1}
38 local action=${2}
39 shift 2
40
41 case "${action}" in
42 down|up)
43 device_${action} ${device} $@
44 ;;
45 esac
46 else
47 local action=${1}
48 shift
49
50 case "${action}" in
51 create)
52 device_${action} $@
53 ;;
54
55 discover)
56 echo "# XXX need to implement --raw here"
57 local device
58 for device in ${devices}; do
59 cli_device_discover ${device} $@
60 done
61 ;;
62
63 show|"")
64 local device
65 for device in $(device_get $@); do
66 device_print ${device}
67 done
68 ;;
69 *)
70 cli_usage device
71 ;;
72 esac
73 fi
74 }
75
76 function cli_device_discover() {
77 local device=${1}
78 shift
79
80 local device_type=$(device_get_type ${device})
81 if [ "${device_type}" != "real" ]; then
82 return ${EXIT_OK}
83 fi
84
85 local raw
86
87 while [ $# -gt 0 ]; do
88 case "${1}" in
89 --raw)
90 raw=1
91 ;;
92 esac
93 shift
94 done
95
96 local up
97 device_is_up ${device} && up=1
98 device_set_up ${device}
99
100 enabled raw || echo "${device}"
101
102 local hook
103 local out
104 local ret
105 for hook in $(hook_zone_get_all); do
106 out=$(hook_zone_exec ${hook} discover ${device})
107 ret=$?
108
109 [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
110
111 if enabled raw; then
112 case "${ret}" in
113 ${DISCOVER_OK})
114 echo "${hook}: OK"
115 local line
116 while read line; do
117 echo "${hook}: ${line}"
118 done <<<"${out}"
119 ;;
120
121 ${DISCOVER_ERROR})
122 echo "${hook}: FAILED"
123 ;;
124 esac
125 else
126 case "${ret}" in
127 ${DISCOVER_OK})
128 echo " ${hook} was successful."
129 local line
130 while read line; do
131 echo " ${line}"
132 done <<<"${out}"
133 ;;
134
135 ${DISCOVER_ERROR})
136 echo " ${hook} failed."
137 ;;
138 esac
139 fi
140 done
141
142 echo # New line
143
144 [ "${up}" = "1" ] || device_set_down ${device}
145 }
146
147 function cli_port() {
148 if cli_help_requested $@; then
149 cli_usage root-port
150 exit ${EXIT_OK}
151 fi
152
153 local action
154 local port
155
156 if port_exists ${1}; then
157 port=${1}
158 action=${2}
159 shift 2
160
161 # Action aliases
162 case "${action}" in
163 start)
164 action="up"
165 ;;
166 stop)
167 action="down"
168 ;;
169 show)
170 action="status"
171 ;;
172 esac
173
174 case "${action}" in
175 edit|up|down|status)
176 port_${action} ${port} $@
177 ;;
178 *)
179 error "Unrecognized argument: ${action}"
180 exit ${EXIT_ERROR}
181 ;;
182 esac
183 else
184 action=${1}
185 shift
186
187 case "${action}" in
188 create|destroy)
189 port_${action} $@
190 ;;
191 *)
192 error "Unrecognized argument: ${action}"
193 exit ${EXIT_ERROR}
194 ;;
195 esac
196 fi
197 }
198
199 function cli_zone() {
200 if cli_help_requested $@; then
201 cli_usage root-zone
202 exit ${EXIT_OK}
203 fi
204
205 local action
206 local zone
207
208 if zone_name_is_valid ${1}; then
209 zone=${1}
210 action=${2}
211 shift 2
212
213 # Action aliases
214 case "${action}" in
215 start)
216 action="up"
217 ;;
218 stop)
219 action="down"
220 ;;
221 show)
222 action="status"
223 ;;
224 esac
225
226 case "${action}" in
227 config|down|edit|port|status|up)
228 zone_${action} ${zone} $@
229 ;;
230 *)
231 error "Unrecognized argument: ${action}"
232 cli_usage root-zone-subcommands
233 exit ${EXIT_ERROR}
234 ;;
235 esac
236 else
237 action=${1}
238 shift
239
240 case "${action}" in
241 create|remove)
242 zone_${action} $@
243 ;;
244 ""|*)
245 if [ -n "${action}" ]; then
246 error "Unrecognized argument: '${action}'"
247 echo
248 fi
249
250 cli_usage root-zone
251 exit ${EXIT_ERROR}
252 ;;
253 esac
254 fi
255 }
256
257 function cli_start() {
258 if cli_help_requested $@; then
259 cli_usage root-start
260 exit ${EXIT_OK}
261 fi
262
263 local zones=$(zones_get $@)
264
265 local zone
266 for zone in ${zones}; do
267 zone_up ${zone}
268 done
269 }
270
271 function cli_stop() {
272 if cli_help_requested $@; then
273 cli_usage root-stop
274 exit ${EXIT_OK}
275 fi
276
277 local zones=$(zones_get $@)
278
279 local zone
280 for zone in ${zones}; do
281 zone_down ${zone}
282 done
283 }
284
285 function cli_restart() {
286 if cli_help_requested $@; then
287 cli_usage root-restart
288 exit ${EXIT_OK}
289 fi
290
291 cli_stop $@
292
293 # Give the system some time to calm down
294 sleep ${TIMEOUT_RESTART}
295
296 cli_start $@
297 }
298
299 function cli_status() {
300 if cli_help_requested $@; then
301 cli_usage root-status
302 exit ${EXIT_OK}
303 fi
304
305 local zones=$(zones_get $@)
306
307 local zone
308 for zone in ${zones}; do
309 zone_status ${zone}
310 done
311 }
312
313 function cli_help_requested() {
314 local argument
315 for argument in ${1}; do
316 if [ "${argument}" = "help" -o "${argument}" = "-h" -o "${argument}" = "--help" ]; then
317 return ${EXIT_OK}
318 fi
319 done
320
321 return ${EXIT_ERROR}
322 }
323
324 function cli_usage() {
325 local what=${1}
326
327 case "${what}" in
328 root)
329 echo "${0}: [command] <options ...>"
330 echo
331 echo " start - ..."
332 echo " stop - ..."
333 echo " restart - ..."
334 echo " status - ..."
335 echo
336 echo " config - ..."
337 echo
338 echo " device - ..."
339 echo " zone - ..."
340 echo
341 ;;
342 root-config)
343 echo "${0}: ${what#root-} [KEY=VAL, ...]"
344 echo
345 echo " This command allows setting of global configuration parameters."
346 echo
347 echo " If no additional arguments are passed it will list the current configuration."
348 echo
349 echo " You can overwrite the settings like the following:"
350 echo
351 echo " ${0} ${what#root-} DEBUG=1 ..."
352 echo
353 ;;
354 root-start|root-stop|root-restart)
355 echo "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]"
356 echo
357 echo " This commands ${what#root-}s all zones by default."
358 echo " One can pass several parameters to only process a subset of all"
359 echo " available zones:"
360 echo
361 echo -e " ${COLOUR_BOLD}--local-only${COLOUR_NORMAL}"
362 echo " Process all local zones which includes every zone without red."
363 echo
364 echo -e " ${COLOUR_BOLD}--remote-only${COLOUR_NORMAL}"
365 echo " Process all remote zones which means only the red ones."
366 echo
367 echo -e " ${COLOUR_BOLD}--all${COLOUR_NORMAL}"
368 echo " Process all zones. This is the default parameter."
369 echo
370 echo " Additionally, you can pass one or more zone names which will"
371 echo " be processed."
372 echo
373 ;;
374 root-status)
375 echo "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]"
376 echo
377 echo " This commands shows status information of all zones by default."
378 echo " One can pass several parameters to only process a subset of all"
379 echo " available zones:"
380 echo
381 echo -e " ${COLOUR_BOLD}--local-only${COLOUR_NORMAL}"
382 echo " Process all local zones which includes every zone without red."
383 echo
384 echo -e " ${COLOUR_BOLD}--remote-only${COLOUR_NORMAL}"
385 echo " Process all remote zones which means only the red ones."
386 echo
387 echo -e " ${COLOUR_BOLD}--all${COLOUR_NORMAL}"
388 echo " Process all zones. This is the default parameter."
389 echo
390 echo " Additionally, you can pass one or more zone names which will"
391 echo " be processed."
392 echo
393 ;;
394 root-zone)
395 echo "${0}: ${what#root-} <create|remove> <zone> [<type> <options...>]"
396 echo
397 echo " Create or remove a zone."
398 echo
399 echo -e " ${COLOUR_BOLD}create <zone> <type> <options>${COLOUR_NORMAL}"
400 echo " Create a new zone of type <type> where <zone> is an allowed"
401 echo " zone name."
402 echo
403 echo -e " ${COLOUR_BOLD}remove <zone>${COLOUR_NORMAL}"
404 echo " Remove the zone <zone>."
405 echo
406 echo " You may also edit the configuration of the zones."
407 echo
408 echo -e " ${COLOUR_BOLD}<zone> ...${COLOUR_NORMAL}"
409 echo " Edit the zone <zone>."
410 echo
411 ;;
412 usage)
413 echo
414 echo " Run '${0} help' to get information how to use this tool."
415 echo
416 ;;
417 *)
418 error "No help available for this command '${what}'."
419 echo
420 ;;
421 esac
422
423 echo "Network configuration tool. Report all bugs to <http://bugs.ipfire.org>."
424 }
425
426 function cli_status_headline() {
427 local zone=${1}
428
429 local state="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}"
430 zone_is_up ${zone} && state="${COLOUR_UP}UP${COLOUR_NORMAL}"
431
432 echo -e "${zone} - ${state} - $(zone_get_hook ${zone})"
433 }
434
435 function cli_headline() {
436 echo
437 echo -e "${COLOUR_BOLD}$@${COLOUR_NORMAL}"
438 }