]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.util
Add vpn security policies to cli
[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
418 ${cmd}
419 local ret=$?
420
421 log DEBUG "Returned with code '${ret}'"
422
423 return ${ret}
424}
425
1c6a4e30 426cmd_quiet() {
98146c00 427 cmd $@ &>/dev/null
3efecbb3
MT
428}
429
1c6a4e30 430cmd_exec() {
f80ce052
MT
431 local cmd=$@
432
433 log DEBUG "Exec'ing command: ${cmd}"
434
435 exec ${cmd}
436
437 log ERROR "Could not exec-ute: ${cmd}"
438 exit ${EXIT_ERROR}
439}
440
1c6a4e30 441cmd_not_implemented() {
2181765d
MT
442 assert false "not implemented"
443}
444
de3cecef
MT
445# Runs a command in a clean environment so that no confidential information
446# is leaked to any untrusted commands.
447cmd_clean_environment() {
448 local cmd=$@
449
450 log DEBUG "Running command in a clean environment: ${cmd}"
451 env -i -- ${cmd}
452 local ret=${?}
453
454 log DEBUG "Returned with code '${ret}'"
455 return ${ret}
456}
457
f5ee091e
MT
458# Executes the given command in background
459cmd_background() {
460 cmd_quiet $@ &
461}
462
463# Prints the PID of the process that was started last
464cmd_background_get_pid() {
465 print "${!}"
466}
467
468cmd_background_result() {
469 local pids=$@
470
471 wait ${pids}
472}
473
b8026986 474# Increase security of the read command
1c6a4e30 475read() {
b8026986
MT
476 builtin read -r $@
477}
478
1c6a4e30 479seq() {
3efecbb3
MT
480 if [ $# -eq 2 ]; then
481 eval echo {${1}..${2}}
482 elif [ $# -eq 3 ]; then
483 eval echo {${1}..${3}..${2}}
484 fi
485}
486
de72bd91
MT
487range() {
488 eval echo {0..$(( ${1} - 1 ))}
489}
490
491count() {
492 local i=0
493
494 while read; do
495 ((i++))
496 done
497
498 echo ${i}
499}
500
1c6a4e30 501which() {
76e6cd51
MT
502 type -P $@
503}
504
fe52c5e0 505# Prints the number of seconds since epoch.
1c6a4e30 506timestamp() {
fe52c5e0
MT
507 date -u "+%s"
508}
509
1c6a4e30 510beautify_time() {
d82cf370
MT
511 local value=${1}
512
513 local unit
514 local limit
515 for unit in s m h d w; do
516 case "${unit}" in
517 s|m|h)
518 limit=60
519 ;;
520 d)
521 limit=24
522 ;;
523 w)
524 limit=7
525 ;;
526 esac
527
528 [ ${value} -lt ${limit} ] && break
529
530 value=$(( ${value} / ${limit} ))
531 done
532
533 echo "${value}${unit}"
534}
711ffac1 535
1c6a4e30 536beautify_bytes() {
711ffac1
MT
537 local value=${1}
538
539 local unit
540 local limit=1024
541 for unit in B k M G T; do
542 [ ${value} -lt ${limit} ] && break
543 value=$(( ${value} / ${limit} ))
544 done
545
546 echo "${value}${unit}"
547}
943e3f7e 548
1c6a4e30 549module_load() {
943e3f7e
MT
550 local module=${1}
551
552 if ! grep -q "^${module}" /proc/modules; then
553 log DEBUG "Loading module '${module}'."
554 modprobe ${module}
555 fi
556}
6b3f9c85 557
1c6a4e30 558binary_exists() {
6b3f9c85
MT
559 local binary=${1}
560
561 if [ -n "$(type -p ${binary})" ]; then
562 return ${EXIT_OK}
563 fi
564
565 return ${EXIT_ERROR}
566}
d76f5107 567
1c6a4e30 568function_exists() {
1e6f187e
MT
569 local function="${1}"
570
571 if [ "$(type -t "${function}")" = "function" ]; then
572 return ${EXIT_TRUE}
573 fi
574
575 return ${EXIT_FALSE}
576}
577
1c6a4e30 578process_kill() {
d76f5107
MT
579 local process=${1}
580
581 if ! isinteger process; then
582 process=$(pidof ${process})
583 fi
584
585 local pid
586 local sig
587 for pid in ${process}; do
588 for sig in 15 9; do
589 [ -d "/proc/${pid}" ] || break
590
591 kill -${sig} ${pid}
592 sleep 1
593 done
594 done
595}
feb76eaf 596
1c6a4e30 597dec() {
feb76eaf
MT
598 local hex=${1}
599
600 if [ "${hex:0:2}" != "0x" ]; then
601 hex="0x${hex}"
602 fi
603
604 printf "%d\n" "${hex}"
605}
3a7fef62 606
1c6a4e30 607chr() {
5cf0edf9
MT
608 local char="${1}"
609
610 [ ${char} -lt 256 ] || return ${EXIT_ERROR}
611
612 printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n"
613}
614
1c6a4e30 615ord() {
5cf0edf9
MT
616 LC_CTYPE="C" printf "%d\n" "'${1}"
617}
618
1c6a4e30 619hex() {
5cf0edf9
MT
620 printf "%X\n" "${1}"
621}
622
1c6a4e30 623network_is_running() {
3a7fef62
MT
624 # Check, if the network service is running.
625 service_is_active network
626}
f80ce052 627
1c6a4e30 628contains_spaces() {
f80ce052
MT
629 local var="$@"
630
631 # Eliminate spaces.
632 local var2=${var// /}
633
634 if [ ${#var} -ne ${#var2} ]; then
635 return ${EXIT_TRUE}
636 fi
637
638 return ${EXIT_FALSE}
639}
5cf0edf9 640
1c6a4e30 641string_split() {
5cf0edf9
MT
642 local string="$@"
643
644 local pos=0
645 while [ ${pos} -lt ${#string} ]; do
646 print "${string:${pos}:1}"
647 pos=$(( ${pos} + 1 ))
648 done
649
650 return ${EXIT_OK}
651}
652
1c6a4e30 653string_reverse() {
5cf0edf9
MT
654 local string="$@"
655
656 local output
657 local pos=0
658 while [ ${pos} -lt ${#string} ]; do
659 output="${string:${pos}:1}${output}"
660 pos=$(( ${pos} + 1 ))
661 done
662
663 print "${output}"
664 return ${EXIT_OK}
665}
666
1c6a4e30 667dec2bin() {
5cf0edf9
MT
668 local number="${1}"
669
670 local output
671
672 local i div
673 for i in 7 6 5 4 3 2 1; do
674 div=$(( 2 ** ${i} ))
675
676 if [ $(( ${number} / ${div} )) -eq 1 ]; then
677 output="${output}1"
678 else
679 output="${output}0"
680 fi
681 number="$(( ${number} % ${div} ))"
682 done
683
684 if [ $(( ${number} % 2 )) -eq 1 ]; then
685 output="${output}1"
686 else
687 output="${output}0"
688 fi
689
690 print "${output}"
691}
692
1c6a4e30 693bin2dec() {
5cf0edf9
MT
694 local string="${1}"
695 local number=0
696
697 local pos=0 char
698 while [ ${pos} -lt ${#string} ]; do
699 char="${string:${pos}:1}"
700 pos=$(( ${pos} + 1 ))
701
702 number=$(( ${number} << 1 ))
703
704 case "${char}" in
705 0) ;;
706 1)
707 number=$(( ${number} + 1 ))
708 ;;
709 *)
710 assert false "Invalid character: ${char}"
711 ;;
712 esac
713 done
714
715 print "${number}"
716 return ${EXIT_OK}
717}
718
1c6a4e30 719char2bin() {
5cf0edf9
MT
720 local dec="$(ord "${1}")"
721
722 dec2bin "${dec}"
723}
724
1c6a4e30 725bin2char() {
5cf0edf9
MT
726 local dec="$(bin2dec "$@")"
727
728 chr "${dec}"
729}
730
1c6a4e30 731bin2hex() {
5cf0edf9
MT
732 local dec="$(bin2dec "$@")"
733
734 dec2hex "${dec}"
735}
736
1c6a4e30 737hex2bin() {
5cf0edf9
MT
738 local dec="$(hex2dec "$@")"
739
740 dec2bin "${dec}"
741}
742
1c6a4e30 743hex2dec() {
5cf0edf9
MT
744 local hex="${1}"
745
746 # Prepend 0x if necessary.
747 [ "${hex:0:2}" = "0x" ] || hex="0x${hex}"
748
749 printf "%d\n" "${hex}"
750}
751
1c6a4e30 752dec2hex() {
5cf0edf9
MT
753 printf "%02x\n" "${1}"
754}
a95d16fc
JS
755
756copy() {
757 # This function just copy config files
758 assert [ $# -eq 2 ]
759
760 local src=${1}
761 local dst=${2}
762
763 # Check if ${dst} is a directory
764 if [ -d ${dst} ]; then
765 log ERROR "${dst} is a directory."
766 return ${EXIT_ERROR}
767 fi
768
769 if ! fread "${src}" > "${dst}"; then
770 log ERROR "Could not copy data from ${src} to ${dst}"
771 return ${EXIT_ERROR}
772 fi
773}