]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.util
hotplug: Don't handle any events for loopback
[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
d13929d4
MT
116format_time() {
117 local s=${1}
118 local ret m
119
120 local units="s m h"
121
122 local unit
123 for unit in ${units}; do
124 m=$(( ${s} % 60 ))
125 s=$(( ${s} / 60 ))
126
127 if [ ${m} -gt 0 ]; then
128 ret="${m}${unit} ${ret}"
129 fi
130 done
131
132 # Remove whitespace
133 echo ${ret}
134}
135
b383499d
MT
136parse_time() {
137 local ret=0
138
139 local arg
140 for arg in $@; do
141 local unit
142
143 case "${arg}" in
144 *h|*m|*s)
145 # Store unit
146 unit="${arg: -1}"
147
148 # Remove unit
149 arg="${arg:0:-1}"
150 ;;
151 esac
152
153 if ! isinteger arg; then
154 return ${EXIT_ERROR}
155 fi
156
157 # Convert hours and minutes into seconds
158 case "${unit}" in
159 h)
160 arg=$(( ${arg} * 3600 ))
161 ;;
162 m)
163 arg=$(( ${arg} * 60 ))
164 ;;
165 esac
166
167 # Add up everything
168 ret=$(( ${ret} + ${arg} ))
169 done
170
171 print "${ret}"
172}
173
1c6a4e30 174assign() {
b79ad79b
MT
175 local key=${1}
176 assert isset key
177 shift
178
e5651e17 179 format "${key}" "%s" "$@"
b79ad79b
MT
180}
181
1c6a4e30 182fread() {
b79ad79b
MT
183 local file=${1}
184 assert isset file
185
186 [ -r "${file}" ] || return ${EXIT_ERROR}
187
188 print "$(<${file})"
189}
190
1c6a4e30 191fwrite() {
b79ad79b
MT
192 local file=${1}
193 assert isset file
194 shift
195
8d4e0d52
MT
196 if [ ! -w "${file}" ]; then
197 log ERROR "${file}: No such file"
198 return ${EXIT_ERROR}
199 fi
200
201 print "%s" "$@" >> ${file} 2>/dev/null
b79ad79b
MT
202}
203
c041b631
MT
204make_parent_dir() {
205 local path="${1}"
206
207 local dirname="$(dirname "${path}")"
208 mkdir -p "${dirname}"
209}
210
1c6a4e30 211enabled() {
1848564d
MT
212 local param=${1}
213
e726ef8d 214 list_match "${!param}" yes on true 1
1848564d
MT
215}
216
1c6a4e30 217mac_generate() {
fb1416c6
MT
218 local b="$(random 12)"
219
220 # Remove multicast bit
221 # and set address is software assigned
222 local first_byte=$(( 0x${b:0:2} & 0xfe ))
223 first_byte=$(( ${first_byte} | 0x02 ))
1848564d
MT
224
225 local output
fb1416c6 226 printf -v output "%02x" "${first_byte}"
790b7ec9 227
fb1416c6 228 output="${output}:${b:2:2}:${b:4:2}:${b:6:2}:${b:8:2}:${b:10:2}"
1848564d
MT
229
230 # Check if output is valid
fb1416c6 231 assert mac_is_valid "${output}"
1848564d 232
790b7ec9 233 echo "${output}"
1848564d
MT
234}
235
1c6a4e30 236mac_format() {
18b43372 237 local mac=${1}
48bc31eb 238 assert isset mac
18b43372 239
48bc31eb
MT
240 # Remove all colons and make the rest lowercase.
241 mac=${mac//:/}
242 mac=${mac,,}
18b43372 243
48bc31eb 244 local output
18b43372
MT
245 if [ "${#mac}" = "12" ]; then
246 # Add colons (:) to mac address
247 output=${mac:0:2}
248 local i
249 for i in 2 4 6 8 10; do
250 output="${output}:${mac:${i}:2}"
251 done
48bc31eb
MT
252 else
253 output=${mac}
18b43372
MT
254 fi
255
256 assert mac_is_valid ${output}
257
48bc31eb 258 print "${output}"
18b43372
MT
259}
260
1c6a4e30 261mac_is_valid() {
1848564d
MT
262 local mac=${1}
263
264 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
265}
266
1c6a4e30 267uuid() {
de543653 268 echo $(</proc/sys/kernel/random/uuid)
1848564d
MT
269}
270
fb1416c6
MT
271rand() {
272 local uuid="$(uuid)"
273 echo "${uuid//-/}"
274}
275
276random() {
277 local length="${1:-8}"
278
279 local random
280 while [ ${#random} -lt ${length} ]; do
281 random="${random}$(rand)"
282 done
283
284 echo "${random:0:${length}}"
285}
286
1c6a4e30 287isset() {
1848564d
MT
288 local var=${1}
289
290 [ -n "${!var}" ]
291}
292
1c6a4e30 293isoneof() {
1848564d
MT
294 local var=${!1}
295 shift
296
e726ef8d 297 list_match "${var}" "$@"
1848564d
MT
298}
299
1c6a4e30 300isbool() {
1848564d
MT
301 local var=${1}
302
303 isoneof ${var} 0 1 no yes on off
304}
305
1c6a4e30 306isinteger() {
1848564d
MT
307 local var=${!1}
308
309 [[ ${var} =~ ^[0-9]+$ ]]
310}
311
1c6a4e30 312ismac() {
1848564d
MT
313 local mac=${!1}
314
315 mac_is_valid ${mac}
316}
317
1c6a4e30 318isipaddress() {
fef4edaf
MT
319 local addr=${!1}
320
321 ip_is_valid ${addr}
322}
323
1c6a4e30 324backtrace() {
711ffac1
MT
325 local start=1
326
327 echo # Empty line
328 error_log "Backtrace (most recent call in first line):"
329
04608623 330 local i source
711ffac1
MT
331 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
332 [ -z "${FUNCNAME[${i}]}" ] && continue
6396ccab
MT
333
334 # Print called binary with arguments.
335 if [ "${FUNCNAME[${i}]}" == "main" ]; then
336 local args="$(list_reverse ${BASH_ARGV[*]})"
337 printf -v source "%20s" "$0"
338 error_log " ${source} ${args}"
339 continue
340 fi
711ffac1 341
04608623
MT
342 source=${BASH_SOURCE[$(( ${i} + 1 ))]}
343 error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
711ffac1
MT
344 done
345}
346
1c6a4e30 347assert() {
1848564d
MT
348 local assertion="$@"
349
350 if ! ${assertion}; then
4c670d7c 351 error_log "Assertion '${assertion}' failed."
711ffac1 352 backtrace
cfbe0802 353 exit ${EXIT_ERROR_ASSERT}
1848564d
MT
354 fi
355
356 return ${EXIT_OK}
357}
cad8bd85 358
b0b2f995
MT
359# This function checks, if the given argument is an assert error
360# exit code. If this is the case, the script will halt immediately.
1c6a4e30 361assert_check_retval() {
b0b2f995
MT
362 local ret=${1}
363
364 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
365 exit ${EXIT_ERROR_ASSERT}
366 fi
367
368 return ${ret}
369}
370
2bb20bbd
SS
371# This function executes the given command and inverses the return code
372not() {
373 local command="$@"
374
375 ${command} && return ${EXIT_FALSE} || return ${EXIT_TRUE}
376}
377
1c6a4e30 378exec_cmd() {
711ffac1
MT
379 local cmd=$@
380
381 log DEBUG "Running command: ${cmd}"
382
b816e04b 383 DEBUG=${DEBUG} \
8c63fa13
MT
384 LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \
385 LOG_FACILITY="${LOG_FACILITY}" \
b816e04b 386 ${SHELL} ${cmd}
711ffac1
MT
387 local ret=$?
388
389 #log DEBUG "Returned with code '${ret}'"
390
391 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
392 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
393 exit ${EXIT_ERROR_ASSERT}
394 fi
395
396 return ${ret}
397}
398
1c6a4e30 399cmd() {
b816e04b
MT
400 local cmd=$@
401
402 log DEBUG "Running command: ${cmd}"
403
404 ${cmd}
405 local ret=$?
406
407 log DEBUG "Returned with code '${ret}'"
408
409 return ${ret}
410}
411
1c6a4e30 412cmd_quiet() {
98146c00 413 cmd $@ &>/dev/null
3efecbb3
MT
414}
415
1c6a4e30 416cmd_exec() {
f80ce052
MT
417 local cmd=$@
418
419 log DEBUG "Exec'ing command: ${cmd}"
420
421 exec ${cmd}
422
423 log ERROR "Could not exec-ute: ${cmd}"
424 exit ${EXIT_ERROR}
425}
426
1c6a4e30 427cmd_not_implemented() {
2181765d
MT
428 assert false "not implemented"
429}
430
de3cecef
MT
431# Runs a command in a clean environment so that no confidential information
432# is leaked to any untrusted commands.
433cmd_clean_environment() {
434 local cmd=$@
435
436 log DEBUG "Running command in a clean environment: ${cmd}"
437 env -i -- ${cmd}
438 local ret=${?}
439
440 log DEBUG "Returned with code '${ret}'"
441 return ${ret}
442}
443
f5ee091e
MT
444# Executes the given command in background
445cmd_background() {
446 cmd_quiet $@ &
447}
448
449# Prints the PID of the process that was started last
450cmd_background_get_pid() {
451 print "${!}"
452}
453
454cmd_background_result() {
455 local pids=$@
456
457 wait ${pids}
458}
459
b8026986 460# Increase security of the read command
1c6a4e30 461read() {
b8026986
MT
462 builtin read -r $@
463}
464
1c6a4e30 465seq() {
3efecbb3
MT
466 if [ $# -eq 2 ]; then
467 eval echo {${1}..${2}}
468 elif [ $# -eq 3 ]; then
469 eval echo {${1}..${3}..${2}}
470 fi
471}
472
de72bd91
MT
473range() {
474 eval echo {0..$(( ${1} - 1 ))}
475}
476
477count() {
478 local i=0
479
480 while read; do
481 ((i++))
482 done
483
484 echo ${i}
485}
486
1c6a4e30 487which() {
76e6cd51
MT
488 type -P $@
489}
490
fe52c5e0 491# Prints the number of seconds since epoch.
1c6a4e30 492timestamp() {
fe52c5e0
MT
493 date -u "+%s"
494}
495
1c6a4e30 496beautify_time() {
d82cf370
MT
497 local value=${1}
498
499 local unit
500 local limit
501 for unit in s m h d w; do
502 case "${unit}" in
503 s|m|h)
504 limit=60
505 ;;
506 d)
507 limit=24
508 ;;
509 w)
510 limit=7
511 ;;
512 esac
513
514 [ ${value} -lt ${limit} ] && break
515
516 value=$(( ${value} / ${limit} ))
517 done
518
519 echo "${value}${unit}"
520}
711ffac1 521
1c6a4e30 522beautify_bytes() {
711ffac1
MT
523 local value=${1}
524
525 local unit
526 local limit=1024
527 for unit in B k M G T; do
528 [ ${value} -lt ${limit} ] && break
529 value=$(( ${value} / ${limit} ))
530 done
531
532 echo "${value}${unit}"
533}
943e3f7e 534
1c6a4e30 535module_load() {
943e3f7e
MT
536 local module=${1}
537
538 if ! grep -q "^${module}" /proc/modules; then
539 log DEBUG "Loading module '${module}'."
540 modprobe ${module}
541 fi
542}
6b3f9c85 543
1c6a4e30 544binary_exists() {
6b3f9c85
MT
545 local binary=${1}
546
547 if [ -n "$(type -p ${binary})" ]; then
548 return ${EXIT_OK}
549 fi
550
551 return ${EXIT_ERROR}
552}
d76f5107 553
1c6a4e30 554function_exists() {
1e6f187e
MT
555 local function="${1}"
556
557 if [ "$(type -t "${function}")" = "function" ]; then
558 return ${EXIT_TRUE}
559 fi
560
561 return ${EXIT_FALSE}
562}
563
1c6a4e30 564process_kill() {
d76f5107
MT
565 local process=${1}
566
567 if ! isinteger process; then
568 process=$(pidof ${process})
569 fi
570
571 local pid
572 local sig
573 for pid in ${process}; do
574 for sig in 15 9; do
575 [ -d "/proc/${pid}" ] || break
576
577 kill -${sig} ${pid}
578 sleep 1
579 done
580 done
581}
feb76eaf 582
1c6a4e30 583dec() {
feb76eaf
MT
584 local hex=${1}
585
586 if [ "${hex:0:2}" != "0x" ]; then
587 hex="0x${hex}"
588 fi
589
590 printf "%d\n" "${hex}"
591}
3a7fef62 592
1c6a4e30 593chr() {
5cf0edf9
MT
594 local char="${1}"
595
596 [ ${char} -lt 256 ] || return ${EXIT_ERROR}
597
598 printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n"
599}
600
1c6a4e30 601ord() {
5cf0edf9
MT
602 LC_CTYPE="C" printf "%d\n" "'${1}"
603}
604
1c6a4e30 605hex() {
5cf0edf9
MT
606 printf "%X\n" "${1}"
607}
608
1c6a4e30 609network_is_running() {
3a7fef62
MT
610 # Check, if the network service is running.
611 service_is_active network
612}
f80ce052 613
1c6a4e30 614contains_spaces() {
f80ce052
MT
615 local var="$@"
616
617 # Eliminate spaces.
618 local var2=${var// /}
619
620 if [ ${#var} -ne ${#var2} ]; then
621 return ${EXIT_TRUE}
622 fi
623
624 return ${EXIT_FALSE}
625}
5cf0edf9 626
1c6a4e30 627string_split() {
5cf0edf9
MT
628 local string="$@"
629
630 local pos=0
631 while [ ${pos} -lt ${#string} ]; do
632 print "${string:${pos}:1}"
633 pos=$(( ${pos} + 1 ))
634 done
635
636 return ${EXIT_OK}
637}
638
1c6a4e30 639string_reverse() {
5cf0edf9
MT
640 local string="$@"
641
642 local output
643 local pos=0
644 while [ ${pos} -lt ${#string} ]; do
645 output="${string:${pos}:1}${output}"
646 pos=$(( ${pos} + 1 ))
647 done
648
649 print "${output}"
650 return ${EXIT_OK}
651}
652
1c6a4e30 653dec2bin() {
5cf0edf9
MT
654 local number="${1}"
655
656 local output
657
658 local i div
659 for i in 7 6 5 4 3 2 1; do
660 div=$(( 2 ** ${i} ))
661
662 if [ $(( ${number} / ${div} )) -eq 1 ]; then
663 output="${output}1"
664 else
665 output="${output}0"
666 fi
667 number="$(( ${number} % ${div} ))"
668 done
669
670 if [ $(( ${number} % 2 )) -eq 1 ]; then
671 output="${output}1"
672 else
673 output="${output}0"
674 fi
675
676 print "${output}"
677}
678
1c6a4e30 679bin2dec() {
5cf0edf9
MT
680 local string="${1}"
681 local number=0
682
683 local pos=0 char
684 while [ ${pos} -lt ${#string} ]; do
685 char="${string:${pos}:1}"
686 pos=$(( ${pos} + 1 ))
687
688 number=$(( ${number} << 1 ))
689
690 case "${char}" in
691 0) ;;
692 1)
693 number=$(( ${number} + 1 ))
694 ;;
695 *)
696 assert false "Invalid character: ${char}"
697 ;;
698 esac
699 done
700
701 print "${number}"
702 return ${EXIT_OK}
703}
704
1c6a4e30 705char2bin() {
5cf0edf9
MT
706 local dec="$(ord "${1}")"
707
708 dec2bin "${dec}"
709}
710
1c6a4e30 711bin2char() {
5cf0edf9
MT
712 local dec="$(bin2dec "$@")"
713
714 chr "${dec}"
715}
716
1c6a4e30 717bin2hex() {
5cf0edf9
MT
718 local dec="$(bin2dec "$@")"
719
720 dec2hex "${dec}"
721}
722
1c6a4e30 723hex2bin() {
5cf0edf9
MT
724 local dec="$(hex2dec "$@")"
725
726 dec2bin "${dec}"
727}
728
1c6a4e30 729hex2dec() {
5cf0edf9
MT
730 local hex="${1}"
731
732 # Prepend 0x if necessary.
733 [ "${hex:0:2}" = "0x" ] || hex="0x${hex}"
734
735 printf "%d\n" "${hex}"
736}
737
1c6a4e30 738dec2hex() {
5cf0edf9
MT
739 printf "%02x\n" "${1}"
740}