]> git.ipfire.org Git - people/arne_f/network.git/blob - functions.util
network: Experimental support for wireless access points.
[people/arne_f/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 # Print a pretty error message
23 function error() {
24 echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL} : $@" >&2
25 }
26
27 function error_log() {
28 error "$@"
29 log ERROR "$@"
30 }
31
32 # Print a pretty warn message
33 function warning() {
34 echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2
35 }
36
37 function warning_log() {
38 warning "$@"
39 log WARNING "$@"
40 }
41
42 # This function does not exist because we cannot use /usr/bin/sort.
43 # It implements some kind of bubble sort which is generally very slow
44 # but we only have to sort very small data.
45 function listsort() {
46 local list=($@)
47 local list_prev
48
49 local i
50 local j
51 local var
52 while [ "${list[*]}" != "${list_prev}" ]; do
53 list_prev="${list[*]}"
54 for j in $(seq 1 ${#list[*]}); do
55 [ ${j} -ge ${#list[*]} ] && continue
56 i=$(( ${j} - 1 ))
57 if [[ "${list[${j}]}" < "${list[${i}]}" ]]; then
58 var="${list[${i}]}"
59 list[${i}]="${list[${j}]}"
60 list[${j}]="${var}"
61 fi
62 done
63 done
64
65 echo "${list[*]}"
66 }
67
68 function listmatch() {
69 local match=${1}
70 shift
71
72 assert isset match
73
74 local i
75 for i in $@; do
76 [ "${match}" = "${i}" ] && return ${EXIT_OK}
77 done
78
79 return ${EXIT_ERROR}
80 }
81
82 function listlength() {
83 local length=0
84
85 local i
86 for i in $@; do
87 length=$(( ${length} + 1 ))
88 done
89
90 echo "${length}"
91 }
92
93 function config_read() {
94 local config_file=${1}
95
96 if [ -e "${config_file}" ]; then
97 . ${config_file}
98 config_check
99 fi
100 }
101
102 function config_write() {
103 local config_file=${1}
104 shift
105
106 # Check if all values to be written are sane
107 config_check
108
109 log DEBUG "Writing configuration file ${config_file}."
110
111 > ${config_file}
112
113 local param
114 for param in $(listsort $@); do
115 echo "${param}=\"${!param}\"" >> ${config_file}
116 done
117 }
118
119 function config_print() {
120 local param
121
122 for param in $(listsort $@); do
123 printf "%-16s = %s\n" "${param}" "${!param}"
124 done
125 }
126
127 function config_check() {
128 # If there is a function defined that is called __check
129 # we call that function
130 [ -n "$(type -t _check)" ] && _check
131 }
132
133 function network_config_set() {
134 while [ $# -gt 0 ]; do
135 case "${1}" in
136 *=*)
137 log INFO "Setting configuration option '${1}'".
138 eval ${1}
139 ;;
140 *)
141 warning "Invalid parameter given: ${1}"
142 ;;
143 esac
144 shift
145 done
146
147 # Write configuration to disk
148 network_config_write
149 }
150
151 function network_config_read() {
152 config_read ${CONFIG_FILE}
153 }
154
155 function network_config_write() {
156 config_write ${CONFIG_FILE} ${CONFIG_FILE_PARAMS}
157 }
158
159 function network_config_print() {
160 config_print ${CONFIG_FILE_PARAMS}
161 }
162
163 # Speedup function to avoid a call of the basename binary
164 function basename() {
165 echo "${1##*/}"
166 }
167
168 function enabled() {
169 local param=${1}
170
171 [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ]
172 }
173
174 function mac_generate() {
175 local mac=()
176 for i in $(seq 0 5); do
177 mac[i]="$(uuid)"
178 mac[i]="0x${mac[i]:0:2}"
179 done
180
181 # Remove multicast bit
182 # and set address is software assigned
183 # XXX must doublecheck if this works
184 mac[0]=$((mac[0] & 0xfe))
185 mac[0]=$((mac[0] | 0x02))
186
187 local output
188 for i in ${mac[*]}; do
189 if [ -n "${output}" ]; then
190 output="${output}:"
191 fi
192
193 output="${output}$(printf "%02x" ${i})"
194 done
195
196 # Check if output is valid
197 assert mac_is_valid ${output}
198
199 echo ${output}
200 }
201
202 function mac_format() {
203 local mac=${1}
204
205 local output
206
207 if [ "${#mac}" = "12" ]; then
208 # Add colons (:) to mac address
209 output=${mac:0:2}
210 local i
211 for i in 2 4 6 8 10; do
212 output="${output}:${mac:${i}:2}"
213 done
214 fi
215
216 assert mac_is_valid ${output}
217
218 echo "${output}"
219 }
220
221 function mac_is_valid() {
222 local mac=${1}
223
224 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
225 }
226
227 function uuid() {
228 echo $(</proc/sys/kernel/random/uuid)
229 }
230
231 function isset() {
232 local var=${1}
233
234 [ -n "${!var}" ]
235 }
236
237 # XXX Nearly same as listmatch
238 function isoneof() {
239 local var=${!1}
240 shift
241
242 for i in $@; do
243 [ "${var}" = "${i}" ] && return ${EXIT_OK}
244 done
245
246 return ${EXIT_ERROR}
247 }
248
249 function isbool() {
250 local var=${1}
251
252 isoneof ${var} 0 1 no yes on off
253 }
254
255 function isinteger() {
256 local var=${!1}
257
258 [[ ${var} =~ ^[0-9]+$ ]]
259 }
260
261 function ismac() {
262 local mac=${!1}
263
264 mac_is_valid ${mac}
265 }
266
267 function backtrace() {
268 local start=1
269
270 echo # Empty line
271 error_log "Backtrace (most recent call in first line):"
272
273 local i
274 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
275 [ -z "${FUNCNAME[${i}]}" ] && continue
276 [ "${FUNCNAME[${i}]}" == "main" ] && continue
277
278 error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
279 done
280 }
281
282 function assert() {
283 local assertion="$@"
284
285 if ! ${assertion}; then
286 error_log "Assertion '${assertion}' failed."
287 backtrace
288 exit ${EXIT_ERROR}
289 fi
290
291 return ${EXIT_OK}
292 }
293
294 function exec_cmd() {
295 local cmd=$@
296
297 log DEBUG "Running command: ${cmd}"
298
299 ${SHELL} ${cmd}
300 local ret=$?
301
302 #log DEBUG "Returned with code '${ret}'"
303
304 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
305 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
306 exit ${EXIT_ERROR_ASSERT}
307 fi
308
309 return ${ret}
310 }
311
312 function uppercase() {
313 local input
314 read input
315 echo "${input^^}"
316 }
317
318 function lowercase() {
319 local input
320 read input
321 echo "${input,,}"
322 }
323
324 function seq() {
325 if [ $# -eq 2 ]; then
326 eval echo {${1}..${2}}
327 elif [ $# -eq 3 ]; then
328 eval echo {${1}..${3}..${2}}
329 fi
330 }
331
332 function beautify_time() {
333 local value=${1}
334
335 local unit
336 local limit
337 for unit in s m h d w; do
338 case "${unit}" in
339 s|m|h)
340 limit=60
341 ;;
342 d)
343 limit=24
344 ;;
345 w)
346 limit=7
347 ;;
348 esac
349
350 [ ${value} -lt ${limit} ] && break
351
352 value=$(( ${value} / ${limit} ))
353 done
354
355 echo "${value}${unit}"
356 }
357
358 function beautify_bytes() {
359 local value=${1}
360
361 local unit
362 local limit=1024
363 for unit in B k M G T; do
364 [ ${value} -lt ${limit} ] && break
365 value=$(( ${value} / ${limit} ))
366 done
367
368 echo "${value}${unit}"
369 }
370
371 function module_load() {
372 local module=${1}
373
374 if ! grep -q "^${module}" /proc/modules; then
375 log DEBUG "Loading module '${module}'."
376 modprobe ${module}
377 fi
378 }
379
380 function binary_exists() {
381 local binary=${1}
382
383 if [ -n "$(type -p ${binary})" ]; then
384 return ${EXIT_OK}
385 fi
386
387 return ${EXIT_ERROR}
388 }
389
390 function process_kill() {
391 local process=${1}
392
393 if ! isinteger process; then
394 process=$(pidof ${process})
395 fi
396
397 local pid
398 local sig
399 for pid in ${process}; do
400 for sig in 15 9; do
401 [ -d "/proc/${pid}" ] || break
402
403 kill -${sig} ${pid}
404 sleep 1
405 done
406 done
407 }