]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.util
bridge: Remove unnecessary call of mac_generate
[people/ms/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
de3cecef
MT
366# Runs a command in a clean environment so that no confidential information
367# is leaked to any untrusted commands.
368cmd_clean_environment() {
369 local cmd=$@
370
371 log DEBUG "Running command in a clean environment: ${cmd}"
372 env -i -- ${cmd}
373 local ret=${?}
374
375 log DEBUG "Returned with code '${ret}'"
376 return ${ret}
377}
378
b8026986 379# Increase security of the read command
1c6a4e30 380read() {
b8026986
MT
381 builtin read -r $@
382}
383
1c6a4e30 384seq() {
3efecbb3
MT
385 if [ $# -eq 2 ]; then
386 eval echo {${1}..${2}}
387 elif [ $# -eq 3 ]; then
388 eval echo {${1}..${3}..${2}}
389 fi
390}
391
1c6a4e30 392which() {
76e6cd51
MT
393 type -P $@
394}
395
fe52c5e0 396# Prints the number of seconds since epoch.
1c6a4e30 397timestamp() {
fe52c5e0
MT
398 date -u "+%s"
399}
400
1c6a4e30 401beautify_time() {
d82cf370
MT
402 local value=${1}
403
404 local unit
405 local limit
406 for unit in s m h d w; do
407 case "${unit}" in
408 s|m|h)
409 limit=60
410 ;;
411 d)
412 limit=24
413 ;;
414 w)
415 limit=7
416 ;;
417 esac
418
419 [ ${value} -lt ${limit} ] && break
420
421 value=$(( ${value} / ${limit} ))
422 done
423
424 echo "${value}${unit}"
425}
711ffac1 426
1c6a4e30 427beautify_bytes() {
711ffac1
MT
428 local value=${1}
429
430 local unit
431 local limit=1024
432 for unit in B k M G T; do
433 [ ${value} -lt ${limit} ] && break
434 value=$(( ${value} / ${limit} ))
435 done
436
437 echo "${value}${unit}"
438}
943e3f7e 439
1c6a4e30 440module_load() {
943e3f7e
MT
441 local module=${1}
442
443 if ! grep -q "^${module}" /proc/modules; then
444 log DEBUG "Loading module '${module}'."
445 modprobe ${module}
446 fi
447}
6b3f9c85 448
1c6a4e30 449binary_exists() {
6b3f9c85
MT
450 local binary=${1}
451
452 if [ -n "$(type -p ${binary})" ]; then
453 return ${EXIT_OK}
454 fi
455
456 return ${EXIT_ERROR}
457}
d76f5107 458
1c6a4e30 459function_exists() {
1e6f187e
MT
460 local function="${1}"
461
462 if [ "$(type -t "${function}")" = "function" ]; then
463 return ${EXIT_TRUE}
464 fi
465
466 return ${EXIT_FALSE}
467}
468
1c6a4e30 469process_kill() {
d76f5107
MT
470 local process=${1}
471
472 if ! isinteger process; then
473 process=$(pidof ${process})
474 fi
475
476 local pid
477 local sig
478 for pid in ${process}; do
479 for sig in 15 9; do
480 [ -d "/proc/${pid}" ] || break
481
482 kill -${sig} ${pid}
483 sleep 1
484 done
485 done
486}
feb76eaf 487
1c6a4e30 488dec() {
feb76eaf
MT
489 local hex=${1}
490
491 if [ "${hex:0:2}" != "0x" ]; then
492 hex="0x${hex}"
493 fi
494
495 printf "%d\n" "${hex}"
496}
3a7fef62 497
1c6a4e30 498chr() {
5cf0edf9
MT
499 local char="${1}"
500
501 [ ${char} -lt 256 ] || return ${EXIT_ERROR}
502
503 printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n"
504}
505
1c6a4e30 506ord() {
5cf0edf9
MT
507 LC_CTYPE="C" printf "%d\n" "'${1}"
508}
509
1c6a4e30 510hex() {
5cf0edf9
MT
511 printf "%X\n" "${1}"
512}
513
1c6a4e30 514network_is_running() {
3a7fef62
MT
515 # Check, if the network service is running.
516 service_is_active network
517}
f80ce052 518
1c6a4e30 519contains_spaces() {
f80ce052
MT
520 local var="$@"
521
522 # Eliminate spaces.
523 local var2=${var// /}
524
525 if [ ${#var} -ne ${#var2} ]; then
526 return ${EXIT_TRUE}
527 fi
528
529 return ${EXIT_FALSE}
530}
5cf0edf9 531
1c6a4e30 532string_split() {
5cf0edf9
MT
533 local string="$@"
534
535 local pos=0
536 while [ ${pos} -lt ${#string} ]; do
537 print "${string:${pos}:1}"
538 pos=$(( ${pos} + 1 ))
539 done
540
541 return ${EXIT_OK}
542}
543
1c6a4e30 544string_reverse() {
5cf0edf9
MT
545 local string="$@"
546
547 local output
548 local pos=0
549 while [ ${pos} -lt ${#string} ]; do
550 output="${string:${pos}:1}${output}"
551 pos=$(( ${pos} + 1 ))
552 done
553
554 print "${output}"
555 return ${EXIT_OK}
556}
557
1c6a4e30 558dec2bin() {
5cf0edf9
MT
559 local number="${1}"
560
561 local output
562
563 local i div
564 for i in 7 6 5 4 3 2 1; do
565 div=$(( 2 ** ${i} ))
566
567 if [ $(( ${number} / ${div} )) -eq 1 ]; then
568 output="${output}1"
569 else
570 output="${output}0"
571 fi
572 number="$(( ${number} % ${div} ))"
573 done
574
575 if [ $(( ${number} % 2 )) -eq 1 ]; then
576 output="${output}1"
577 else
578 output="${output}0"
579 fi
580
581 print "${output}"
582}
583
1c6a4e30 584bin2dec() {
5cf0edf9
MT
585 local string="${1}"
586 local number=0
587
588 local pos=0 char
589 while [ ${pos} -lt ${#string} ]; do
590 char="${string:${pos}:1}"
591 pos=$(( ${pos} + 1 ))
592
593 number=$(( ${number} << 1 ))
594
595 case "${char}" in
596 0) ;;
597 1)
598 number=$(( ${number} + 1 ))
599 ;;
600 *)
601 assert false "Invalid character: ${char}"
602 ;;
603 esac
604 done
605
606 print "${number}"
607 return ${EXIT_OK}
608}
609
1c6a4e30 610char2bin() {
5cf0edf9
MT
611 local dec="$(ord "${1}")"
612
613 dec2bin "${dec}"
614}
615
1c6a4e30 616bin2char() {
5cf0edf9
MT
617 local dec="$(bin2dec "$@")"
618
619 chr "${dec}"
620}
621
1c6a4e30 622bin2hex() {
5cf0edf9
MT
623 local dec="$(bin2dec "$@")"
624
625 dec2hex "${dec}"
626}
627
1c6a4e30 628hex2bin() {
5cf0edf9
MT
629 local dec="$(hex2dec "$@")"
630
631 dec2bin "${dec}"
632}
633
1c6a4e30 634hex2dec() {
5cf0edf9
MT
635 local hex="${1}"
636
637 # Prepend 0x if necessary.
638 [ "${hex:0:2}" = "0x" ] || hex="0x${hex}"
639
640 printf "%d\n" "${hex}"
641}
642
1c6a4e30 643dec2hex() {
5cf0edf9
MT
644 printf "%02x\n" "${1}"
645}