]> git.ipfire.org Git - people/ms/network.git/blame - functions.cli
network: pppoe: Kill pppd daemon in a better way.
[people/ms/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
MT
23 if cli_help_requested $@; then
24 cli_usage root-config
25 exit ${EXIT_OK}
26 fi
27
1848564d
MT
28 if [ -n "${1}" ]; then
29 network_config_set $@
30 else
31 network_config_print
32 fi
33}
34
35function 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
65function 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
79function 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
150function cli_zone() {
8db9698f
MT
151 if cli_help_requested $@; then
152 cli_usage root-zone
153 exit ${EXIT_OK}
154 fi
155
1848564d
MT
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 ;;
8db9698f
MT
168 *)
169 error "Unrecognized argument: ${action}"
170 cli_usage root-zone-subcommands
171 exit ${EXIT_ERROR}
172 ;;
1848564d
MT
173 esac
174 else
175 action=${1}
176 shift
177
178 case "${action}" in
179 create|remove)
180 zone_${action} $@
181 ;;
8db9698f
MT
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}
1848564d
MT
190 ;;
191 esac
192 fi
193}
194
195function cli_start() {
1d7bc4f3
MT
196 if cli_help_requested $@; then
197 cli_usage root-start
198 exit ${EXIT_OK}
199 fi
200
1848564d
MT
201 local zones=$(zones_get $@)
202
203 local zone
204 for zone in ${zones}; do
205 zone_up ${zone}
206 done
207}
208
209function cli_stop() {
1d7bc4f3
MT
210 if cli_help_requested $@; then
211 cli_usage root-stop
212 exit ${EXIT_OK}
213 fi
214
1848564d
MT
215 local zones=$(zones_get $@)
216
217 local zone
218 for zone in ${zones}; do
219 zone_down ${zone}
220 done
221}
222
bcef495d
MT
223function 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
2ae4f579 232 sleep ${TIMEOUT_RESTART}
bcef495d
MT
233
234 cli_start $@
235}
236
1d7bc4f3
MT
237function cli_help_requested() {
238 local argument
239 for argument in $@; do
240 if [ "${argument}" = "help" -o "${argument}" = "-h" -o "${argument}" = "--help" ]; then
241 return ${EXIT_OK}
242 fi
243 done
244
245 return ${EXIT_ERROR}
246}
247
1848564d
MT
248function cli_usage() {
249 local what=${1}
250
251 case "${what}" in
252 root)
253 echo "${0}: [command] <options ...>"
254 echo
255 echo " start - ..."
256 echo " stop - ..."
bcef495d 257 echo " restart - ..."
1848564d
MT
258 echo
259 echo " config - ..."
260 echo
261 echo " device - ..."
262 echo " show - ???"
263 echo " zone - ..."
264 echo
265 ;;
fe688aa4
MT
266 root-config)
267 echo "${0}: ${what#root-} [KEY=VAL, ...]"
268 echo
269 echo " This command allows setting of global configuration parameters."
270 echo
271 echo " If no additional arguments are passed it will list the current configuration."
272 echo
273 echo " You can overwrite the settings like the following:"
274 echo
275 echo " ${0} ${what#root-} DEBUG=1 ..."
276 echo
277 ;;
bcef495d 278 root-start|root-stop|root-restart)
1d7bc4f3
MT
279 echo "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]"
280 echo
281 echo " This commands ${what#root-}s all zones by default."
282 echo " One can pass several parameters to only process a subset of all"
283 echo " available zones:"
284 echo
285 echo -e " ${BOLD}--local-only${NORMAL}"
286 echo " Process all local zones which includes every zone without red."
287 echo
288 echo -e " ${BOLD}--remote-only${NORMAL}"
289 echo " Process all remote zones which means only the red ones."
290 echo
291 echo -e " ${BOLD}--all${NORMAL}"
292 echo " Process all zones. This is the default parameter."
293 echo
294 echo " Additionally, you can pass one or more zone names which will"
295 echo " be processed."
296 echo
297 ;;
8db9698f
MT
298 root-zone)
299 echo "${0}: ${what#root-} <create|remove> <zone> [<type> <options...>]"
300 echo
301 echo " Create or remove a zone."
302 echo
303 echo -e " ${BOLD}create <zone> <type> <options>${NORMAL}"
304 echo " Create a new zone of type <type> where <zone> is an allowed"
305 echo " zone name."
306 echo
307 echo -e " ${BOLD}remove <zone>${NORMAL}"
308 echo " Remove the zone <zone>."
309 echo
310 echo " You may also edit the configuration of the zones."
311 echo
312 echo -e " ${BOLD}<zone> ...${NORMAL}"
313 echo " Edit the zone <zone>."
314 echo
315 ;;
1848564d
MT
316 usage)
317 echo
318 echo " Run '${0} help' to get information how to use this tool."
319 echo
320 ;;
321 *)
322 error "No help available for this command '${what}'."
1d7bc4f3 323 echo
1848564d
MT
324 ;;
325 esac
1d7bc4f3
MT
326
327 echo "Network configuration tool. Report all bugs to <http://bugs.ipfire.org>."
1848564d 328}