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