2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
22 # A simple print statement
26 printf -- "${fmt}\n" "$@"
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
33 echo "$@" |
xargs printf "%s\n"
39 if [ "${var:0:1}" = "\"" ]; then
43 local last
=$
(( ${#var} - 1 ))
44 if [ ${last} -ge 0 ] && [ "${var:${last}:1}" = "\"" ]; then
58 # remove leading whitespace characters
59 value="${value#"${value%%[![:space:]]*}"}"
61 # remove trailing whitespace characters
62 value="${value%"${value##*[![:space:]]}"}"
67 # Print a pretty error message
69 echo -e " ${CLR_RED_B}ERROR
${CLR_RESET} : $@
" >&2
76 # Print a pretty warn message
78 echo -e " ${CLR_YELLOW_B}WARNING
${CLR_RESET}: $@
" >&2
85 # The next three functions are kept for backwards
86 # compatibility. The need to be dropped at some time.
99 # Speedup function to avoid a call of the basename binary
113 printf -v "${key}" "${format}" "$@
"
123 for unit in ${units}; do
127 if [ ${m} -gt 0 ]; then
128 ret="${m}${unit} ${ret}"
153 if ! isinteger arg; then
157 # Convert hours and minutes into seconds
160 arg=$(( ${arg} * 3600 ))
163 arg=$(( ${arg} * 60 ))
168 ret=$(( ${ret} + ${arg} ))
179 format "${key}" "%s
" "$@
"
186 [ -r "${file}" ] || return ${EXIT_ERROR}
196 if [ ! -w "${file}" ]; then
197 log ERROR "${file}: No such
file"
201 print "%s
" "$@
" >> ${file} 2>/dev/null
207 local dirname="$
(dirname "${path}")"
208 mkdir -p "${dirname}"
214 list_match "${!param}" yes on true 1
218 local b="$
(random
12)"
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 ))
226 printf -v output "%02x
" "${first_byte}"
228 output="${output}:${b:2:2}:${b:4:2}:${b:6:2}:${b:8:2}:${b:10:2}"
230 # Check if output is valid
231 assert mac_is_valid "${output}"
240 # Remove all colons and make the rest lowercase.
245 if [ "${#mac}" = "12" ]; then
246 # Add colons (:) to mac address
249 for i in 2 4 6 8 10; do
250 output="${output}:${mac:${i}:2}"
256 assert mac_is_valid ${output}
264 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
268 echo $(</proc/sys/kernel/random/uuid)
277 local length="${1:-8}"
280 while [ ${#random} -lt ${length} ]; do
281 random="${random}$
(rand
)"
284 echo "${random:0:${length}}"
297 list_match "${var}" "$@
"
303 isoneof ${var} 0 1 no yes on off
309 [[ ${var} =~ ^[0-9]+$ ]]
328 error_log "Backtrace
(most recent call
in first line
):"
331 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
332 [ -z "${FUNCNAME[${i}]}" ] && continue
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}"
342 source=${BASH_SOURCE[$(( ${i} + 1 ))]}
343 error_log " $
(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
350 if ! ${assertion}; then
351 error_log
"Assertion '${assertion}' failed."
353 exit ${EXIT_ERROR_ASSERT}
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.
361 assert_check_retval
() {
364 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
365 exit ${EXIT_ERROR_ASSERT}
371 # This function executes the given command and inverses the return code
375 ${command} && return ${EXIT_FALSE} || return ${EXIT_TRUE}
381 log DEBUG
"Running command: ${cmd}"
384 LOG_DISABLE_STDOUT
="${LOG_DISABLE_STDOUT}" \
385 LOG_FACILITY
="${LOG_FACILITY}" \
389 #log DEBUG "Returned with code '${ret}'"
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}
402 log DEBUG
"Running command: ${cmd}"
407 log DEBUG
"Returned with code '${ret}'"
419 log DEBUG
"Exec'ing command: ${cmd}"
423 log ERROR
"Could not exec-ute: ${cmd}"
427 cmd_not_implemented
() {
428 assert false
"not implemented"
431 # Runs a command in a clean environment so that no confidential information
432 # is leaked to any untrusted commands.
433 cmd_clean_environment
() {
436 log DEBUG
"Running command in a clean environment: ${cmd}"
440 log DEBUG
"Returned with code '${ret}'"
444 # Executes the given command in background
449 # Prints the PID of the process that was started last
450 cmd_background_get_pid
() {
454 cmd_background_result
() {
460 # Increase security of the read command
466 if [ $# -eq 2 ]; then
467 eval echo {${1}..
${2}}
468 elif [ $# -eq 3 ]; then
469 eval echo {${1}..${3}..${2}}
474 eval echo {0..$
(( ${1} - 1 ))}
491 # Prints the number of seconds since epoch.
501 for unit
in s m h d w
; do
514 [ ${value} -lt ${limit} ] && break
516 value
=$
(( ${value} / ${limit} ))
519 echo "${value}${unit}"
527 for unit
in B k M G T
; do
528 [ ${value} -lt ${limit} ] && break
529 value
=$
(( ${value} / ${limit} ))
532 echo "${value}${unit}"
538 if ! grep -q "^${module}" /proc
/modules
; then
539 log DEBUG
"Loading module '${module}'."
547 if [ -n "$(type -p ${binary})" ]; then
555 local function="${1}"
557 if [ "$(type -t "${function}")" = "function" ]; then
567 if ! isinteger process
; then
568 process
=$
(pidof
${process})
573 for pid
in ${process}; do
575 [ -d "/proc/${pid}" ] ||
break
586 if [ "${hex:0:2}" != "0x" ]; then
590 printf "%d\n" "${hex}"
596 [ ${char} -lt 256 ] ||
return ${EXIT_ERROR}
598 printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n"
602 LC_CTYPE
="C" printf "%d\n" "'${1}"
609 network_is_running
() {
610 # Check, if the network service is running.
611 service_is_active network
618 local var2
=${var// /}
620 if [ ${#var} -ne ${#var2} ]; then
631 while [ ${pos} -lt ${#string} ]; do
632 print
"${string:${pos}:1}"
633 pos
=$
(( ${pos} + 1 ))
644 while [ ${pos} -lt ${#string} ]; do
645 output
="${string:${pos}:1}${output}"
646 pos
=$
(( ${pos} + 1 ))
659 for i
in 7 6 5 4 3 2 1; do
662 if [ $
(( ${number} / ${div} )) -eq 1 ]; then
667 number
="$(( ${number} % ${div} ))"
670 if [ $
(( ${number} % 2 )) -eq 1 ]; then
684 while [ ${pos} -lt ${#string} ]; do
685 char
="${string:${pos}:1}"
686 pos
=$
(( ${pos} + 1 ))
688 number
=$
(( ${number} << 1 ))
693 number
=$
(( ${number} + 1 ))
696 assert false
"Invalid character: ${char}"
706 local dec
="$(ord "${1}")"
712 local dec
="$(bin2dec "$@
")"
718 local dec
="$(bin2dec "$@
")"
724 local dec
="$(hex2dec "$@
")"
732 # Prepend 0x if necessary.
733 [ "${hex:0:2}" = "0x" ] || hex
="0x${hex}"
735 printf "%d\n" "${hex}"
739 printf "%02x\n" "${1}"