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