]> git.ipfire.org Git - people/ms/network.git/blame - functions.cli
network: Update codebase.
[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() {
23 if [ -n "${1}" ]; then
24 network_config_set $@
25 else
26 network_config_print
27 fi
28}
29
30function cli_device() {
31 local action=${1}
32 shift
33
34 local device
35 local devices=$@
36
37 if [ -z "${devices}" ]; then
38 devices=$(devices_get_all)
39 fi
40
41 case "${action}" in
42 discover)
43 echo "# XXX need to implement --raw here"
44 for device in ${devices}; do
45 cli_device_discover ${device} $@
46 done
47 ;;
48
49 show|"")
50 for device in ${devices}; do
51 cli_device_print ${device}
52 done
53 ;;
54 *)
55 cli_usage device
56 ;;
57 esac
58}
59
60function cli_device_print() {
61 local device=${1}
62
63 if ! device_exists ${device}; then
64 error "Device '${device}' does not exist."
65 return ${EXIT_ERROR}
66 fi
67
68 echo "${device}"
69 echo " Type: $(device_get_type ${device})"
70 echo " Addr: $(device_get_address ${device})"
71 echo
72}
73
74function cli_device_discover() {
75 local device=${1}
76 shift
77
78 local device_type=$(device_get_type ${device})
79 if [ "${device_type}" != "real" ]; then
80 return ${EXIT_OK}
81 fi
82
83 local raw
84
85 while [ $# -gt 0 ]; do
86 case "${1}" in
87 --raw)
88 raw=1
89 ;;
90 esac
91 shift
92 done
93
94 local up
95 device_is_up ${device} && up=1
96 device_set_up ${device}
97
98 enabled raw || echo "${device}"
99
100 local hook
101 local out
102 local ret
103 for hook in $(hooks_get_all); do
104 out=$(hook_exec ${hook} discover ${device})
105 ret=$?
106
107 [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
108
109 if enabled raw; then
110 case "${ret}" in
111 ${DISCOVER_OK})
112 echo "${hook}: OK"
113 local line
114 while read line; do
115 echo "${hook}: ${line}"
116 done <<<"${out}"
117 ;;
118
119 ${DISCOVER_ERROR})
120 echo "${hook}: FAILED"
121 ;;
122 esac
123 else
124 case "${ret}" in
125 ${DISCOVER_OK})
126 echo " ${hook} was successful."
127 local line
128 while read line; do
129 echo " ${line}"
130 done <<<"${out}"
131 ;;
132
133 ${DISCOVER_ERROR})
134 echo " ${hook} failed."
135 ;;
136 esac
137 fi
138 done
139
140 echo # New line
141
142 [ "${up}" = "1" ] || device_set_down ${device}
143}
144
145function cli_zone() {
146 local action
147 local zone
148
149 if zone_name_is_valid ${1}; then
150 zone=${1}
151 action=${2}
152 shift 2
153
154 case "${action}" in
155 config|down|edit|port|show|status|up)
156 zone_${action} ${zone} $@
157 ;;
158 esac
159 else
160 action=${1}
161 shift
162
163 case "${action}" in
164 create|remove)
165 zone_${action} $@
166 ;;
167 *)
168 error "Unrecognized argument: '${action}'"
169 ;;
170 esac
171 fi
172}
173
174function cli_start() {
175 local zones=$(zones_get $@)
176
177 local zone
178 for zone in ${zones}; do
179 zone_up ${zone}
180 done
181}
182
183function cli_stop() {
184 local zones=$(zones_get $@)
185
186 local zone
187 for zone in ${zones}; do
188 zone_down ${zone}
189 done
190}
191
192function cli_usage() {
193 local what=${1}
194
195 case "${what}" in
196 root)
197 echo "${0}: [command] <options ...>"
198 echo
199 echo " start - ..."
200 echo " stop - ..."
201 echo
202 echo " config - ..."
203 echo
204 echo " device - ..."
205 echo " show - ???"
206 echo " zone - ..."
207 echo
208 ;;
209 usage)
210 echo
211 echo " Run '${0} help' to get information how to use this tool."
212 echo
213 ;;
214 *)
215 error "No help available for this command '${what}'."
216 ;;
217 esac
218}