]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.util
Remove the function keyword which is a bashism
[people/stevee/network.git] / src / functions / functions.util
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
97cb552e 22# A simple print statement
1c6a4e30 23print() {
acc9efd5
MT
24 local fmt=${1}; shift
25
40e3553f 26 printf -- "${fmt}\n" "$@"
97cb552e
MT
27}
28
cb965348
MT
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
1c6a4e30 32args() {
cb965348
MT
33 echo "$@" | xargs printf "%s\n"
34}
35
1c6a4e30 36unquote() {
04854c77
MT
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
1c6a4e30 51quote() {
04854c77
MT
52 print "\"%s\"" "$@"
53}
54
1c6a4e30 55strip() {
fe52c5e0
MT
56 local value="$@"
57
58 # remove leading whitespace characters
59 value="${value#"${value%%[![:space:]]*}"}"
60
61 # remove trailing whitespace characters
62 value="${value%"${value##*[![:space:]]}"}"
63
64 print "${value}"
65}
66
1848564d 67# Print a pretty error message
1c6a4e30 68error() {
fcbf6823 69 echo -e " ${CLR_RED_B}ERROR${CLR_RESET} : $@" >&2
1848564d
MT
70}
71
1c6a4e30 72error_log() {
1b7a1578
MT
73 log ERROR "$@"
74}
75
1848564d 76# Print a pretty warn message
1c6a4e30 77warning() {
fcbf6823 78 echo -e " ${CLR_YELLOW_B}WARNING${CLR_RESET}: $@" >&2
1848564d
MT
79}
80
1c6a4e30 81warning_log() {
1b7a1578
MT
82 log WARNING "$@"
83}
84
e726ef8d
MT
85# The next three functions are kept for backwards
86# compatibility. The need to be dropped at some time.
1c6a4e30 87listsort() {
e726ef8d 88 list_sort $@
1848564d
MT
89}
90
1c6a4e30 91listmatch() {
e726ef8d 92 list_match $@
711ffac1
MT
93}
94
1c6a4e30 95listlength() {
e726ef8d 96 list_length $@
711ffac1
MT
97}
98
1848564d 99# Speedup function to avoid a call of the basename binary
1c6a4e30 100basename() {
1848564d
MT
101 echo "${1##*/}"
102}
103
1c6a4e30 104format() {
e5651e17
MT
105 local key=${1}
106 assert isset key
107
108 local format=${2}
109 assert isset format
110
111 shift 2
112
113 printf -v "${key}" "${format}" "$@"
114}
115
1c6a4e30 116assign() {
b79ad79b
MT
117 local key=${1}
118 assert isset key
119 shift
120
e5651e17 121 format "${key}" "%s" "$@"
b79ad79b
MT
122}
123
1c6a4e30 124fread() {
b79ad79b
MT
125 local file=${1}
126 assert isset file
127
128 [ -r "${file}" ] || return ${EXIT_ERROR}
129
130 print "$(<${file})"
131}
132
1c6a4e30 133fwrite() {
b79ad79b
MT
134 local file=${1}
135 assert isset file
136 shift
137
8d4e0d52
MT
138 if [ ! -w "${file}" ]; then
139 log ERROR "${file}: No such file"
140 return ${EXIT_ERROR}
141 fi
142
143 print "%s" "$@" >> ${file} 2>/dev/null
b79ad79b
MT
144}
145
1c6a4e30 146enabled() {
1848564d
MT
147 local param=${1}
148
e726ef8d 149 list_match "${!param}" yes on true 1
1848564d
MT
150}
151
1c6a4e30 152mac_generate() {
790b7ec9
MT
153 # Get a bunch of random hex digits
154 # and remove all dashes from the input.
155 local random=$(</proc/sys/kernel/random/uuid)
156 random=${random//-/}
157 assert isset random
1848564d
MT
158
159 local output
790b7ec9
MT
160
161 local i o
162 for i in $(seq 0 5); do
163 o="0x${random:0:2}"
164 random="${random:2:${#random}}"
165
166 case "${i}" in
167 0)
168 # Remove multicast bit
169 # and set address is software assigned
170 o=$(( ${o} & 0xfe ))
171 o=$(( ${o} | 0x02 ))
172
173 printf -v output "%02x" "${o}"
174 ;;
175 *)
176 printf -v output "%s:%02x" "${output}" "${o}"
177 ;;
178 esac
1848564d
MT
179 done
180
181 # Check if output is valid
182 assert mac_is_valid ${output}
183
790b7ec9 184 echo "${output}"
1848564d
MT
185}
186
1c6a4e30 187mac_format() {
18b43372 188 local mac=${1}
48bc31eb 189 assert isset mac
18b43372 190
48bc31eb
MT
191 # Remove all colons and make the rest lowercase.
192 mac=${mac//:/}
193 mac=${mac,,}
18b43372 194
48bc31eb 195 local output
18b43372
MT
196 if [ "${#mac}" = "12" ]; then
197 # Add colons (:) to mac address
198 output=${mac:0:2}
199 local i
200 for i in 2 4 6 8 10; do
201 output="${output}:${mac:${i}:2}"
202 done
48bc31eb
MT
203 else
204 output=${mac}
18b43372
MT
205 fi
206
207 assert mac_is_valid ${output}
208
48bc31eb 209 print "${output}"
18b43372
MT
210}
211
1c6a4e30 212mac_is_valid() {
1848564d
MT
213 local mac=${1}
214
215 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
216}
217
1c6a4e30 218uuid() {
de543653 219 echo $(</proc/sys/kernel/random/uuid)
1848564d
MT
220}
221
1c6a4e30 222isset() {
1848564d
MT
223 local var=${1}
224
225 [ -n "${!var}" ]
226}
227
1c6a4e30 228isoneof() {
1848564d
MT
229 local var=${!1}
230 shift
231
e726ef8d 232 list_match "${var}" "$@"
1848564d
MT
233}
234
1c6a4e30 235isbool() {
1848564d
MT
236 local var=${1}
237
238 isoneof ${var} 0 1 no yes on off
239}
240
1c6a4e30 241isinteger() {
1848564d
MT
242 local var=${!1}
243
244 [[ ${var} =~ ^[0-9]+$ ]]
245}
246
1c6a4e30 247ismac() {
1848564d
MT
248 local mac=${!1}
249
250 mac_is_valid ${mac}
251}
252
1c6a4e30 253isipaddress() {
fef4edaf
MT
254 local addr=${!1}
255
256 ip_is_valid ${addr}
257}
258
1c6a4e30 259backtrace() {
711ffac1
MT
260 local start=1
261
262 echo # Empty line
263 error_log "Backtrace (most recent call in first line):"
264
04608623 265 local i source
711ffac1
MT
266 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
267 [ -z "${FUNCNAME[${i}]}" ] && continue
6396ccab
MT
268
269 # Print called binary with arguments.
270 if [ "${FUNCNAME[${i}]}" == "main" ]; then
271 local args="$(list_reverse ${BASH_ARGV[*]})"
272 printf -v source "%20s" "$0"
273 error_log " ${source} ${args}"
274 continue
275 fi
711ffac1 276
04608623
MT
277 source=${BASH_SOURCE[$(( ${i} + 1 ))]}
278 error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
711ffac1
MT
279 done
280}
281
1c6a4e30 282assert() {
1848564d
MT
283 local assertion="$@"
284
285 if ! ${assertion}; then
4c670d7c 286 error_log "Assertion '${assertion}' failed."
711ffac1 287 backtrace
cfbe0802 288 exit ${EXIT_ERROR_ASSERT}
1848564d
MT
289 fi
290
291 return ${EXIT_OK}
292}
cad8bd85 293
b0b2f995
MT
294# This function checks, if the given argument is an assert error
295# exit code. If this is the case, the script will halt immediately.
1c6a4e30 296assert_check_retval() {
b0b2f995
MT
297 local ret=${1}
298
299 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
300 exit ${EXIT_ERROR_ASSERT}
301 fi
302
303 return ${ret}
304}
305
1c6a4e30 306exec_cmd() {
711ffac1
MT
307 local cmd=$@
308
309 log DEBUG "Running command: ${cmd}"
310
b816e04b 311 DEBUG=${DEBUG} \
8c63fa13
MT
312 LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \
313 LOG_FACILITY="${LOG_FACILITY}" \
b816e04b 314 ${SHELL} ${cmd}
711ffac1
MT
315 local ret=$?
316
317 #log DEBUG "Returned with code '${ret}'"
318
319 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
320 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
321 exit ${EXIT_ERROR_ASSERT}
322 fi
323
324 return ${ret}
325}
326
1c6a4e30 327cmd() {
b816e04b
MT
328 local cmd=$@
329
330 log DEBUG "Running command: ${cmd}"
331
332 ${cmd}
333 local ret=$?
334
335 log DEBUG "Returned with code '${ret}'"
336
337 return ${ret}
338}
339
1c6a4e30 340cmd_quiet() {
98146c00 341 cmd $@ &>/dev/null
3efecbb3
MT
342}
343
1c6a4e30 344cmd_exec() {
f80ce052
MT
345 local cmd=$@
346
347 log DEBUG "Exec'ing command: ${cmd}"
348
349 exec ${cmd}
350
351 log ERROR "Could not exec-ute: ${cmd}"
352 exit ${EXIT_ERROR}
353}
354
1c6a4e30 355cmd_not_implemented() {
2181765d
MT
356 assert false "not implemented"
357}
358
b8026986 359# Increase security of the read command
1c6a4e30 360read() {
b8026986
MT
361 builtin read -r $@
362}
363
1c6a4e30 364seq() {
3efecbb3
MT
365 if [ $# -eq 2 ]; then
366 eval echo {${1}..${2}}
367 elif [ $# -eq 3 ]; then
368 eval echo {${1}..${3}..${2}}
369 fi
370}
371
1c6a4e30 372which() {
76e6cd51
MT
373 type -P $@
374}
375
fe52c5e0 376# Prints the number of seconds since epoch.
1c6a4e30 377timestamp() {
fe52c5e0
MT
378 date -u "+%s"
379}
380
1c6a4e30 381beautify_time() {
d82cf370
MT
382 local value=${1}
383
384 local unit
385 local limit
386 for unit in s m h d w; do
387 case "${unit}" in
388 s|m|h)
389 limit=60
390 ;;
391 d)
392 limit=24
393 ;;
394 w)
395 limit=7
396 ;;
397 esac
398
399 [ ${value} -lt ${limit} ] && break
400
401 value=$(( ${value} / ${limit} ))
402 done
403
404 echo "${value}${unit}"
405}
711ffac1 406
1c6a4e30 407beautify_bytes() {
711ffac1
MT
408 local value=${1}
409
410 local unit
411 local limit=1024
412 for unit in B k M G T; do
413 [ ${value} -lt ${limit} ] && break
414 value=$(( ${value} / ${limit} ))
415 done
416
417 echo "${value}${unit}"
418}
943e3f7e 419
1c6a4e30 420module_load() {
943e3f7e
MT
421 local module=${1}
422
423 if ! grep -q "^${module}" /proc/modules; then
424 log DEBUG "Loading module '${module}'."
425 modprobe ${module}
426 fi
427}
6b3f9c85 428
1c6a4e30 429binary_exists() {
6b3f9c85
MT
430 local binary=${1}
431
432 if [ -n "$(type -p ${binary})" ]; then
433 return ${EXIT_OK}
434 fi
435
436 return ${EXIT_ERROR}
437}
d76f5107 438
1c6a4e30 439function_exists() {
1e6f187e
MT
440 local function="${1}"
441
442 if [ "$(type -t "${function}")" = "function" ]; then
443 return ${EXIT_TRUE}
444 fi
445
446 return ${EXIT_FALSE}
447}
448
1c6a4e30 449process_kill() {
d76f5107
MT
450 local process=${1}
451
452 if ! isinteger process; then
453 process=$(pidof ${process})
454 fi
455
456 local pid
457 local sig
458 for pid in ${process}; do
459 for sig in 15 9; do
460 [ -d "/proc/${pid}" ] || break
461
462 kill -${sig} ${pid}
463 sleep 1
464 done
465 done
466}
feb76eaf 467
1c6a4e30 468dec() {
feb76eaf
MT
469 local hex=${1}
470
471 if [ "${hex:0:2}" != "0x" ]; then
472 hex="0x${hex}"
473 fi
474
475 printf "%d\n" "${hex}"
476}
3a7fef62 477
1c6a4e30 478chr() {
5cf0edf9
MT
479 local char="${1}"
480
481 [ ${char} -lt 256 ] || return ${EXIT_ERROR}
482
483 printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n"
484}
485
1c6a4e30 486ord() {
5cf0edf9
MT
487 LC_CTYPE="C" printf "%d\n" "'${1}"
488}
489
1c6a4e30 490hex() {
5cf0edf9
MT
491 printf "%X\n" "${1}"
492}
493
1c6a4e30 494network_is_running() {
3a7fef62
MT
495 # Check, if the network service is running.
496 service_is_active network
497}
f80ce052 498
1c6a4e30 499contains_spaces() {
f80ce052
MT
500 local var="$@"
501
502 # Eliminate spaces.
503 local var2=${var// /}
504
505 if [ ${#var} -ne ${#var2} ]; then
506 return ${EXIT_TRUE}
507 fi
508
509 return ${EXIT_FALSE}
510}
5cf0edf9 511
1c6a4e30 512string_split() {
5cf0edf9
MT
513 local string="$@"
514
515 local pos=0
516 while [ ${pos} -lt ${#string} ]; do
517 print "${string:${pos}:1}"
518 pos=$(( ${pos} + 1 ))
519 done
520
521 return ${EXIT_OK}
522}
523
1c6a4e30 524string_reverse() {
5cf0edf9
MT
525 local string="$@"
526
527 local output
528 local pos=0
529 while [ ${pos} -lt ${#string} ]; do
530 output="${string:${pos}:1}${output}"
531 pos=$(( ${pos} + 1 ))
532 done
533
534 print "${output}"
535 return ${EXIT_OK}
536}
537
1c6a4e30 538dec2bin() {
5cf0edf9
MT
539 local number="${1}"
540
541 local output
542
543 local i div
544 for i in 7 6 5 4 3 2 1; do
545 div=$(( 2 ** ${i} ))
546
547 if [ $(( ${number} / ${div} )) -eq 1 ]; then
548 output="${output}1"
549 else
550 output="${output}0"
551 fi
552 number="$(( ${number} % ${div} ))"
553 done
554
555 if [ $(( ${number} % 2 )) -eq 1 ]; then
556 output="${output}1"
557 else
558 output="${output}0"
559 fi
560
561 print "${output}"
562}
563
1c6a4e30 564bin2dec() {
5cf0edf9
MT
565 local string="${1}"
566 local number=0
567
568 local pos=0 char
569 while [ ${pos} -lt ${#string} ]; do
570 char="${string:${pos}:1}"
571 pos=$(( ${pos} + 1 ))
572
573 number=$(( ${number} << 1 ))
574
575 case "${char}" in
576 0) ;;
577 1)
578 number=$(( ${number} + 1 ))
579 ;;
580 *)
581 assert false "Invalid character: ${char}"
582 ;;
583 esac
584 done
585
586 print "${number}"
587 return ${EXIT_OK}
588}
589
1c6a4e30 590char2bin() {
5cf0edf9
MT
591 local dec="$(ord "${1}")"
592
593 dec2bin "${dec}"
594}
595
1c6a4e30 596bin2char() {
5cf0edf9
MT
597 local dec="$(bin2dec "$@")"
598
599 chr "${dec}"
600}
601
1c6a4e30 602bin2hex() {
5cf0edf9
MT
603 local dec="$(bin2dec "$@")"
604
605 dec2hex "${dec}"
606}
607
1c6a4e30 608hex2bin() {
5cf0edf9
MT
609 local dec="$(hex2dec "$@")"
610
611 dec2bin "${dec}"
612}
613
1c6a4e30 614hex2dec() {
5cf0edf9
MT
615 local hex="${1}"
616
617 # Prepend 0x if necessary.
618 [ "${hex:0:2}" = "0x" ] || hex="0x${hex}"
619
620 printf "%d\n" "${hex}"
621}
622
1c6a4e30 623dec2hex() {
5cf0edf9
MT
624 printf "%02x\n" "${1}"
625}