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