]> git.ipfire.org Git - people/stevee/network.git/blob - functions.util
Merge remote-tracking branch 'kbarthel/dhcp_dns'
[people/stevee/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 # The args() function takes a number of arguments like
30 # var1="abc d" var2="abc" var3="abcd e"
31 # and splits them into several arguments, devided by newline
32 function args() {
33 echo "$@" | xargs printf "%s\n"
34 }
35
36 function unquote() {
37 local var="$@"
38
39 if [ "${var:0:1}" = "\"" ]; then
40 var=${var:1}
41 fi
42
43 local last=$(( ${#var} - 1 ))
44 if [ ${last} -ge 0 ] && [ "${var:${last}:1}" = "\"" ]; then
45 var=${var:0:${last}}
46 fi
47
48 print "${var}"
49 }
50
51 function quote() {
52 print "\"%s\"" "$@"
53 }
54
55 # Print a pretty error message
56 function error() {
57 echo -e " ${CLR_RED_B}ERROR${CLR_RESET} : $@" >&2
58 }
59
60 function error_log() {
61 log ERROR "$@"
62 }
63
64 # Print a pretty warn message
65 function warning() {
66 echo -e " ${CLR_YELLOW_B}WARNING${CLR_RESET}: $@" >&2
67 }
68
69 function warning_log() {
70 log WARNING "$@"
71 }
72
73 # The next three functions are kept for backwards
74 # compatibility. The need to be dropped at some time.
75 function listsort() {
76 list_sort $@
77 }
78
79 function listmatch() {
80 list_match $@
81 }
82
83 function listlength() {
84 list_length $@
85 }
86
87 # Speedup function to avoid a call of the basename binary
88 function basename() {
89 echo "${1##*/}"
90 }
91
92 function assign() {
93 local key=${1}
94 assert isset key
95 shift
96
97 printf -v "${key}" "%s" "$@"
98 }
99
100 function fread() {
101 local file=${1}
102 assert isset file
103
104 [ -r "${file}" ] || return ${EXIT_ERROR}
105
106 print "$(<${file})"
107 }
108
109 function fwrite() {
110 local file=${1}
111 assert isset file
112 shift
113
114 print "%s" "$@" >> ${file}
115 }
116
117 function enabled() {
118 local param=${1}
119
120 list_match "${!param}" yes on true 1
121 }
122
123 function mac_generate() {
124 # Get a bunch of random hex digits
125 # and remove all dashes from the input.
126 local random=$(</proc/sys/kernel/random/uuid)
127 random=${random//-/}
128 assert isset random
129
130 local output
131
132 local i o
133 for i in $(seq 0 5); do
134 o="0x${random:0:2}"
135 random="${random:2:${#random}}"
136
137 case "${i}" in
138 0)
139 # Remove multicast bit
140 # and set address is software assigned
141 o=$(( ${o} & 0xfe ))
142 o=$(( ${o} | 0x02 ))
143
144 printf -v output "%02x" "${o}"
145 ;;
146 *)
147 printf -v output "%s:%02x" "${output}" "${o}"
148 ;;
149 esac
150 done
151
152 # Check if output is valid
153 assert mac_is_valid ${output}
154
155 echo "${output}"
156 }
157
158 function mac_format() {
159 local mac=${1}
160 assert isset mac
161
162 # Remove all colons and make the rest lowercase.
163 mac=${mac//:/}
164 mac=${mac,,}
165
166 local output
167 if [ "${#mac}" = "12" ]; then
168 # Add colons (:) to mac address
169 output=${mac:0:2}
170 local i
171 for i in 2 4 6 8 10; do
172 output="${output}:${mac:${i}:2}"
173 done
174 else
175 output=${mac}
176 fi
177
178 assert mac_is_valid ${output}
179
180 print "${output}"
181 }
182
183 function mac_is_valid() {
184 local mac=${1}
185
186 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
187 }
188
189 function uuid() {
190 echo $(</proc/sys/kernel/random/uuid)
191 }
192
193 function isset() {
194 local var=${1}
195
196 [ -n "${!var}" ]
197 }
198
199 function isoneof() {
200 local var=${!1}
201 shift
202
203 list_match "${var}" "$@"
204 }
205
206 function isbool() {
207 local var=${1}
208
209 isoneof ${var} 0 1 no yes on off
210 }
211
212 function isinteger() {
213 local var=${!1}
214
215 [[ ${var} =~ ^[0-9]+$ ]]
216 }
217
218 function ismac() {
219 local mac=${!1}
220
221 mac_is_valid ${mac}
222 }
223
224 function isipaddress() {
225 local addr=${!1}
226
227 ip_is_valid ${addr}
228 }
229
230 function backtrace() {
231 local start=1
232
233 echo # Empty line
234 error_log "Backtrace (most recent call in first line):"
235
236 local i source
237 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
238 [ -z "${FUNCNAME[${i}]}" ] && continue
239 [ "${FUNCNAME[${i}]}" == "main" ] && continue
240
241 source=${BASH_SOURCE[$(( ${i} + 1 ))]}
242 error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
243 done
244 }
245
246 function assert() {
247 local assertion="$@"
248
249 if ! ${assertion}; then
250 error_log "Assertion '${assertion}' failed."
251 backtrace
252 exit ${EXIT_ERROR_ASSERT}
253 fi
254
255 return ${EXIT_OK}
256 }
257
258 # This function checks, if the given argument is an assert error
259 # exit code. If this is the case, the script will halt immediately.
260 function assert_check_retval() {
261 local ret=${1}
262
263 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
264 exit ${EXIT_ERROR_ASSERT}
265 fi
266
267 return ${ret}
268 }
269
270 function exec_cmd() {
271 local cmd=$@
272
273 log DEBUG "Running command: ${cmd}"
274
275 DEBUG=${DEBUG} \
276 LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \
277 LOG_FACILITY="${LOG_FACILITY}" \
278 ${SHELL} ${cmd}
279 local ret=$?
280
281 #log DEBUG "Returned with code '${ret}'"
282
283 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
284 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
285 exit ${EXIT_ERROR_ASSERT}
286 fi
287
288 return ${ret}
289 }
290
291 function cmd() {
292 local cmd=$@
293
294 log DEBUG "Running command: ${cmd}"
295
296 ${cmd}
297 local ret=$?
298
299 log DEBUG "Returned with code '${ret}'"
300
301 return ${ret}
302 }
303
304 function cmd_quiet() {
305 cmd $@ &>/dev/null
306 }
307
308 function cmd_exec() {
309 local cmd=$@
310
311 log DEBUG "Exec'ing command: ${cmd}"
312
313 exec ${cmd}
314
315 log ERROR "Could not exec-ute: ${cmd}"
316 exit ${EXIT_ERROR}
317 }
318
319 function seq() {
320 if [ $# -eq 2 ]; then
321 eval echo {${1}..${2}}
322 elif [ $# -eq 3 ]; then
323 eval echo {${1}..${3}..${2}}
324 fi
325 }
326
327 function which() {
328 type -P $@
329 }
330
331 function beautify_time() {
332 local value=${1}
333
334 local unit
335 local limit
336 for unit in s m h d w; do
337 case "${unit}" in
338 s|m|h)
339 limit=60
340 ;;
341 d)
342 limit=24
343 ;;
344 w)
345 limit=7
346 ;;
347 esac
348
349 [ ${value} -lt ${limit} ] && break
350
351 value=$(( ${value} / ${limit} ))
352 done
353
354 echo "${value}${unit}"
355 }
356
357 function beautify_bytes() {
358 local value=${1}
359
360 local unit
361 local limit=1024
362 for unit in B k M G T; do
363 [ ${value} -lt ${limit} ] && break
364 value=$(( ${value} / ${limit} ))
365 done
366
367 echo "${value}${unit}"
368 }
369
370 function module_load() {
371 local module=${1}
372
373 if ! grep -q "^${module}" /proc/modules; then
374 log DEBUG "Loading module '${module}'."
375 modprobe ${module}
376 fi
377 }
378
379 function binary_exists() {
380 local binary=${1}
381
382 if [ -n "$(type -p ${binary})" ]; then
383 return ${EXIT_OK}
384 fi
385
386 return ${EXIT_ERROR}
387 }
388
389 function process_kill() {
390 local process=${1}
391
392 if ! isinteger process; then
393 process=$(pidof ${process})
394 fi
395
396 local pid
397 local sig
398 for pid in ${process}; do
399 for sig in 15 9; do
400 [ -d "/proc/${pid}" ] || break
401
402 kill -${sig} ${pid}
403 sleep 1
404 done
405 done
406 }
407
408 function dec() {
409 local hex=${1}
410
411 if [ "${hex:0:2}" != "0x" ]; then
412 hex="0x${hex}"
413 fi
414
415 printf "%d\n" "${hex}"
416 }
417
418 function network_is_running() {
419 # Check, if the network service is running.
420 service_is_active network
421 }
422
423 function contains_spaces() {
424 local var="$@"
425
426 # Eliminate spaces.
427 local var2=${var// /}
428
429 if [ ${#var} -ne ${#var2} ]; then
430 return ${EXIT_TRUE}
431 fi
432
433 return ${EXIT_FALSE}
434 }