]> git.ipfire.org Git - people/ms/network.git/blob - functions.util
Introduce concept of firewall zones.
[people/ms/network.git] / functions.util
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 # A simple print statement
23 function print() {
24 local fmt=${1}; shift
25
26 printf -- "${fmt}\n" "$@"
27 }
28
29 # Print a pretty error message
30 function error() {
31 echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL} : $@" >&2
32 }
33
34 function error_log() {
35 log ERROR "$@"
36 }
37
38 # Print a pretty warn message
39 function warning() {
40 echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2
41 }
42
43 function warning_log() {
44 log WARNING "$@"
45 }
46
47 function listsort() {
48 local i
49 for i in $@; do
50 echo "${i}"
51 done | sort | tr '\n' ' '
52 echo
53 }
54
55 function listmatch() {
56 local match=${1}
57 shift
58
59 local i
60 for i in $@; do
61 [ "${match}" = "${i}" ] && return ${EXIT_OK}
62 done
63
64 return ${EXIT_ERROR}
65 }
66
67 function listlength() {
68 local length=0
69
70 local i
71 for i in $@; do
72 length=$(( ${length} + 1 ))
73 done
74
75 echo "${length}"
76 }
77
78 # Speedup function to avoid a call of the basename binary
79 function basename() {
80 echo "${1##*/}"
81 }
82
83 function enabled() {
84 local param=${1}
85
86 listmatch "${!param}" yes on true 1
87 }
88
89 function mac_generate() {
90 # Get a bunch of random hex digits
91 # and remove all dashes from the input.
92 local random=$(</proc/sys/kernel/random/uuid)
93 random=${random//-/}
94 assert isset random
95
96 local output
97
98 local i o
99 for i in $(seq 0 5); do
100 o="0x${random:0:2}"
101 random="${random:2:${#random}}"
102
103 case "${i}" in
104 0)
105 # Remove multicast bit
106 # and set address is software assigned
107 o=$(( ${o} & 0xfe ))
108 o=$(( ${o} | 0x02 ))
109
110 printf -v output "%02x" "${o}"
111 ;;
112 *)
113 printf -v output "%s:%02x" "${output}" "${o}"
114 ;;
115 esac
116 done
117
118 # Check if output is valid
119 assert mac_is_valid ${output}
120
121 echo "${output}"
122 }
123
124 function mac_format() {
125 local mac=${1}
126
127 local output
128
129 if [ "${#mac}" = "12" ]; then
130 # Add colons (:) to mac address
131 output=${mac:0:2}
132 local i
133 for i in 2 4 6 8 10; do
134 output="${output}:${mac:${i}:2}"
135 done
136 fi
137
138 assert mac_is_valid ${output}
139
140 echo "${output}"
141 }
142
143 function mac_is_valid() {
144 local mac=${1}
145
146 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
147 }
148
149 function uuid() {
150 echo $(</proc/sys/kernel/random/uuid)
151 }
152
153 function isset() {
154 local var=${1}
155
156 [ -n "${!var}" ]
157 }
158
159 function isoneof() {
160 local var=${!1}
161 shift
162
163 listmatch "${var}" "$@"
164 }
165
166 function isbool() {
167 local var=${1}
168
169 isoneof ${var} 0 1 no yes on off
170 }
171
172 function isinteger() {
173 local var=${!1}
174
175 [[ ${var} =~ ^[0-9]+$ ]]
176 }
177
178 function ismac() {
179 local mac=${!1}
180
181 mac_is_valid ${mac}
182 }
183
184 function backtrace() {
185 local start=1
186
187 echo # Empty line
188 error_log "Backtrace (most recent call in first line):"
189
190 local i source
191 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
192 [ -z "${FUNCNAME[${i}]}" ] && continue
193 [ "${FUNCNAME[${i}]}" == "main" ] && continue
194
195 source=${BASH_SOURCE[$(( ${i} + 1 ))]}
196 error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
197 done
198 }
199
200 function assert() {
201 local assertion="$@"
202
203 if ! ${assertion}; then
204 error_log "Assertion '${assertion}' failed."
205 backtrace
206 exit ${EXIT_ERROR_ASSERT}
207 fi
208
209 return ${EXIT_OK}
210 }
211
212 # This function checks, if the given argument is an assert error
213 # exit code. If this is the case, the script will halt immediately.
214 function assert_check_retval() {
215 local ret=${1}
216
217 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
218 exit ${EXIT_ERROR_ASSERT}
219 fi
220
221 return ${ret}
222 }
223
224 function exec_cmd() {
225 local cmd=$@
226
227 log DEBUG "Running command: ${cmd}"
228
229 DEBUG=${DEBUG} \
230 LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \
231 LOG_FACILITY="${LOG_FACILITY}" \
232 ${SHELL} ${cmd}
233 local ret=$?
234
235 #log DEBUG "Returned with code '${ret}'"
236
237 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
238 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
239 exit ${EXIT_ERROR_ASSERT}
240 fi
241
242 return ${ret}
243 }
244
245 function cmd() {
246 local cmd=$@
247
248 log DEBUG "Running command: ${cmd}"
249
250 ${cmd}
251 local ret=$?
252
253 log DEBUG "Returned with code '${ret}'"
254
255 return ${ret}
256 }
257
258 function cmd_quiet() {
259 cmd $@ &>/dev/null
260 }
261
262 function seq() {
263 if [ $# -eq 2 ]; then
264 eval echo {${1}..${2}}
265 elif [ $# -eq 3 ]; then
266 eval echo {${1}..${3}..${2}}
267 fi
268 }
269
270 function which() {
271 type -P $@
272 }
273
274 function beautify_time() {
275 local value=${1}
276
277 local unit
278 local limit
279 for unit in s m h d w; do
280 case "${unit}" in
281 s|m|h)
282 limit=60
283 ;;
284 d)
285 limit=24
286 ;;
287 w)
288 limit=7
289 ;;
290 esac
291
292 [ ${value} -lt ${limit} ] && break
293
294 value=$(( ${value} / ${limit} ))
295 done
296
297 echo "${value}${unit}"
298 }
299
300 function beautify_bytes() {
301 local value=${1}
302
303 local unit
304 local limit=1024
305 for unit in B k M G T; do
306 [ ${value} -lt ${limit} ] && break
307 value=$(( ${value} / ${limit} ))
308 done
309
310 echo "${value}${unit}"
311 }
312
313 function module_load() {
314 local module=${1}
315
316 if ! grep -q "^${module}" /proc/modules; then
317 log DEBUG "Loading module '${module}'."
318 modprobe ${module}
319 fi
320 }
321
322 function binary_exists() {
323 local binary=${1}
324
325 if [ -n "$(type -p ${binary})" ]; then
326 return ${EXIT_OK}
327 fi
328
329 return ${EXIT_ERROR}
330 }
331
332 function process_kill() {
333 local process=${1}
334
335 if ! isinteger process; then
336 process=$(pidof ${process})
337 fi
338
339 local pid
340 local sig
341 for pid in ${process}; do
342 for sig in 15 9; do
343 [ -d "/proc/${pid}" ] || break
344
345 kill -${sig} ${pid}
346 sleep 1
347 done
348 done
349 }
350
351 function dec() {
352 local hex=${1}
353
354 if [ "${hex:0:2}" != "0x" ]; then
355 hex="0x${hex}"
356 fi
357
358 printf "%d\n" "${hex}"
359 }
360
361 function network_is_running() {
362 # Check, if the network service is running.
363 service_is_active network
364 }