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