]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.util
DHCP: Cleanup some code
[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() {
fb1416c6
MT
160 local b="$(random 12)"
161
162 # Remove multicast bit
163 # and set address is software assigned
164 local first_byte=$(( 0x${b:0:2} & 0xfe ))
165 first_byte=$(( ${first_byte} | 0x02 ))
1848564d
MT
166
167 local output
fb1416c6 168 printf -v output "%02x" "${first_byte}"
790b7ec9 169
fb1416c6 170 output="${output}:${b:2:2}:${b:4:2}:${b:6:2}:${b:8:2}:${b:10:2}"
1848564d
MT
171
172 # Check if output is valid
fb1416c6 173 assert mac_is_valid "${output}"
1848564d 174
790b7ec9 175 echo "${output}"
1848564d
MT
176}
177
1c6a4e30 178mac_format() {
18b43372 179 local mac=${1}
48bc31eb 180 assert isset mac
18b43372 181
48bc31eb
MT
182 # Remove all colons and make the rest lowercase.
183 mac=${mac//:/}
184 mac=${mac,,}
18b43372 185
48bc31eb 186 local output
18b43372
MT
187 if [ "${#mac}" = "12" ]; then
188 # Add colons (:) to mac address
189 output=${mac:0:2}
190 local i
191 for i in 2 4 6 8 10; do
192 output="${output}:${mac:${i}:2}"
193 done
48bc31eb
MT
194 else
195 output=${mac}
18b43372
MT
196 fi
197
198 assert mac_is_valid ${output}
199
48bc31eb 200 print "${output}"
18b43372
MT
201}
202
1c6a4e30 203mac_is_valid() {
1848564d
MT
204 local mac=${1}
205
206 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
207}
208
1c6a4e30 209uuid() {
de543653 210 echo $(</proc/sys/kernel/random/uuid)
1848564d
MT
211}
212
fb1416c6
MT
213rand() {
214 local uuid="$(uuid)"
215 echo "${uuid//-/}"
216}
217
218random() {
219 local length="${1:-8}"
220
221 local random
222 while [ ${#random} -lt ${length} ]; do
223 random="${random}$(rand)"
224 done
225
226 echo "${random:0:${length}}"
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
2bb20bbd
SS
313# This function executes the given command and inverses the return code
314not() {
315 local command="$@"
316
317 ${command} && return ${EXIT_FALSE} || return ${EXIT_TRUE}
318}
319
1c6a4e30 320exec_cmd() {
711ffac1
MT
321 local cmd=$@
322
323 log DEBUG "Running command: ${cmd}"
324
b816e04b 325 DEBUG=${DEBUG} \
8c63fa13
MT
326 LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \
327 LOG_FACILITY="${LOG_FACILITY}" \
b816e04b 328 ${SHELL} ${cmd}
711ffac1
MT
329 local ret=$?
330
331 #log DEBUG "Returned with code '${ret}'"
332
333 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
334 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
335 exit ${EXIT_ERROR_ASSERT}
336 fi
337
338 return ${ret}
339}
340
1c6a4e30 341cmd() {
b816e04b
MT
342 local cmd=$@
343
344 log DEBUG "Running command: ${cmd}"
345
346 ${cmd}
347 local ret=$?
348
349 log DEBUG "Returned with code '${ret}'"
350
351 return ${ret}
352}
353
1c6a4e30 354cmd_quiet() {
98146c00 355 cmd $@ &>/dev/null
3efecbb3
MT
356}
357
1c6a4e30 358cmd_exec() {
f80ce052
MT
359 local cmd=$@
360
361 log DEBUG "Exec'ing command: ${cmd}"
362
363 exec ${cmd}
364
365 log ERROR "Could not exec-ute: ${cmd}"
366 exit ${EXIT_ERROR}
367}
368
1c6a4e30 369cmd_not_implemented() {
2181765d
MT
370 assert false "not implemented"
371}
372
de3cecef
MT
373# Runs a command in a clean environment so that no confidential information
374# is leaked to any untrusted commands.
375cmd_clean_environment() {
376 local cmd=$@
377
378 log DEBUG "Running command in a clean environment: ${cmd}"
379 env -i -- ${cmd}
380 local ret=${?}
381
382 log DEBUG "Returned with code '${ret}'"
383 return ${ret}
384}
385
f5ee091e
MT
386# Executes the given command in background
387cmd_background() {
388 cmd_quiet $@ &
389}
390
391# Prints the PID of the process that was started last
392cmd_background_get_pid() {
393 print "${!}"
394}
395
396cmd_background_result() {
397 local pids=$@
398
399 wait ${pids}
400}
401
b8026986 402# Increase security of the read command
1c6a4e30 403read() {
b8026986
MT
404 builtin read -r $@
405}
406
1c6a4e30 407seq() {
3efecbb3
MT
408 if [ $# -eq 2 ]; then
409 eval echo {${1}..${2}}
410 elif [ $# -eq 3 ]; then
411 eval echo {${1}..${3}..${2}}
412 fi
413}
414
1c6a4e30 415which() {
76e6cd51
MT
416 type -P $@
417}
418
fe52c5e0 419# Prints the number of seconds since epoch.
1c6a4e30 420timestamp() {
fe52c5e0
MT
421 date -u "+%s"
422}
423
1c6a4e30 424beautify_time() {
d82cf370
MT
425 local value=${1}
426
427 local unit
428 local limit
429 for unit in s m h d w; do
430 case "${unit}" in
431 s|m|h)
432 limit=60
433 ;;
434 d)
435 limit=24
436 ;;
437 w)
438 limit=7
439 ;;
440 esac
441
442 [ ${value} -lt ${limit} ] && break
443
444 value=$(( ${value} / ${limit} ))
445 done
446
447 echo "${value}${unit}"
448}
711ffac1 449
1c6a4e30 450beautify_bytes() {
711ffac1
MT
451 local value=${1}
452
453 local unit
454 local limit=1024
455 for unit in B k M G T; do
456 [ ${value} -lt ${limit} ] && break
457 value=$(( ${value} / ${limit} ))
458 done
459
460 echo "${value}${unit}"
461}
943e3f7e 462
1c6a4e30 463module_load() {
943e3f7e
MT
464 local module=${1}
465
466 if ! grep -q "^${module}" /proc/modules; then
467 log DEBUG "Loading module '${module}'."
468 modprobe ${module}
469 fi
470}
6b3f9c85 471
1c6a4e30 472binary_exists() {
6b3f9c85
MT
473 local binary=${1}
474
475 if [ -n "$(type -p ${binary})" ]; then
476 return ${EXIT_OK}
477 fi
478
479 return ${EXIT_ERROR}
480}
d76f5107 481
1c6a4e30 482function_exists() {
1e6f187e
MT
483 local function="${1}"
484
485 if [ "$(type -t "${function}")" = "function" ]; then
486 return ${EXIT_TRUE}
487 fi
488
489 return ${EXIT_FALSE}
490}
491
1c6a4e30 492process_kill() {
d76f5107
MT
493 local process=${1}
494
495 if ! isinteger process; then
496 process=$(pidof ${process})
497 fi
498
499 local pid
500 local sig
501 for pid in ${process}; do
502 for sig in 15 9; do
503 [ -d "/proc/${pid}" ] || break
504
505 kill -${sig} ${pid}
506 sleep 1
507 done
508 done
509}
feb76eaf 510
1c6a4e30 511dec() {
feb76eaf
MT
512 local hex=${1}
513
514 if [ "${hex:0:2}" != "0x" ]; then
515 hex="0x${hex}"
516 fi
517
518 printf "%d\n" "${hex}"
519}
3a7fef62 520
1c6a4e30 521chr() {
5cf0edf9
MT
522 local char="${1}"
523
524 [ ${char} -lt 256 ] || return ${EXIT_ERROR}
525
526 printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n"
527}
528
1c6a4e30 529ord() {
5cf0edf9
MT
530 LC_CTYPE="C" printf "%d\n" "'${1}"
531}
532
1c6a4e30 533hex() {
5cf0edf9
MT
534 printf "%X\n" "${1}"
535}
536
1c6a4e30 537network_is_running() {
3a7fef62
MT
538 # Check, if the network service is running.
539 service_is_active network
540}
f80ce052 541
1c6a4e30 542contains_spaces() {
f80ce052
MT
543 local var="$@"
544
545 # Eliminate spaces.
546 local var2=${var// /}
547
548 if [ ${#var} -ne ${#var2} ]; then
549 return ${EXIT_TRUE}
550 fi
551
552 return ${EXIT_FALSE}
553}
5cf0edf9 554
1c6a4e30 555string_split() {
5cf0edf9
MT
556 local string="$@"
557
558 local pos=0
559 while [ ${pos} -lt ${#string} ]; do
560 print "${string:${pos}:1}"
561 pos=$(( ${pos} + 1 ))
562 done
563
564 return ${EXIT_OK}
565}
566
1c6a4e30 567string_reverse() {
5cf0edf9
MT
568 local string="$@"
569
570 local output
571 local pos=0
572 while [ ${pos} -lt ${#string} ]; do
573 output="${string:${pos}:1}${output}"
574 pos=$(( ${pos} + 1 ))
575 done
576
577 print "${output}"
578 return ${EXIT_OK}
579}
580
1c6a4e30 581dec2bin() {
5cf0edf9
MT
582 local number="${1}"
583
584 local output
585
586 local i div
587 for i in 7 6 5 4 3 2 1; do
588 div=$(( 2 ** ${i} ))
589
590 if [ $(( ${number} / ${div} )) -eq 1 ]; then
591 output="${output}1"
592 else
593 output="${output}0"
594 fi
595 number="$(( ${number} % ${div} ))"
596 done
597
598 if [ $(( ${number} % 2 )) -eq 1 ]; then
599 output="${output}1"
600 else
601 output="${output}0"
602 fi
603
604 print "${output}"
605}
606
1c6a4e30 607bin2dec() {
5cf0edf9
MT
608 local string="${1}"
609 local number=0
610
611 local pos=0 char
612 while [ ${pos} -lt ${#string} ]; do
613 char="${string:${pos}:1}"
614 pos=$(( ${pos} + 1 ))
615
616 number=$(( ${number} << 1 ))
617
618 case "${char}" in
619 0) ;;
620 1)
621 number=$(( ${number} + 1 ))
622 ;;
623 *)
624 assert false "Invalid character: ${char}"
625 ;;
626 esac
627 done
628
629 print "${number}"
630 return ${EXIT_OK}
631}
632
1c6a4e30 633char2bin() {
5cf0edf9
MT
634 local dec="$(ord "${1}")"
635
636 dec2bin "${dec}"
637}
638
1c6a4e30 639bin2char() {
5cf0edf9
MT
640 local dec="$(bin2dec "$@")"
641
642 chr "${dec}"
643}
644
1c6a4e30 645bin2hex() {
5cf0edf9
MT
646 local dec="$(bin2dec "$@")"
647
648 dec2hex "${dec}"
649}
650
1c6a4e30 651hex2bin() {
5cf0edf9
MT
652 local dec="$(hex2dec "$@")"
653
654 dec2bin "${dec}"
655}
656
1c6a4e30 657hex2dec() {
5cf0edf9
MT
658 local hex="${1}"
659
660 # Prepend 0x if necessary.
661 [ "${hex:0:2}" = "0x" ] || hex="0x${hex}"
662
663 printf "%d\n" "${hex}"
664}
665
1c6a4e30 666dec2hex() {
5cf0edf9
MT
667 printf "%02x\n" "${1}"
668}