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}" "$@
"
121 format "${key}" "%s
" "$@
"
128 [ -r "${file}" ] || return ${EXIT_ERROR}
138 if [ ! -w "${file}" ]; then
139 log ERROR "${file}: No such
file"
143 print "%s
" "$@
" >> ${file} 2>/dev/null
149 local dirname="$
(dirname "${path}")"
150 mkdir -p "${dirname}"
156 list_match "${!param}" yes on true 1
160 local b="$
(random
12)"
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 ))
168 printf -v output "%02x
" "${first_byte}"
170 output="${output}:${b:2:2}:${b:4:2}:${b:6:2}:${b:8:2}:${b:10:2}"
172 # Check if output is valid
173 assert mac_is_valid "${output}"
182 # Remove all colons and make the rest lowercase.
187 if [ "${#mac}" = "12" ]; then
188 # Add colons (:) to mac address
191 for i in 2 4 6 8 10; do
192 output="${output}:${mac:${i}:2}"
198 assert mac_is_valid ${output}
206 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
210 echo $(</proc/sys/kernel/random/uuid)
219 local length="${1:-8}"
222 while [ ${#random} -lt ${length} ]; do
223 random="${random}$
(rand
)"
226 echo "${random:0:${length}}"
239 list_match "${var}" "$@
"
245 isoneof ${var} 0 1 no yes on off
251 [[ ${var} =~ ^[0-9]+$ ]]
270 error_log "Backtrace
(most recent call
in first line
):"
273 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
274 [ -z "${FUNCNAME[${i}]}" ] && continue
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}"
284 source=${BASH_SOURCE[$(( ${i} + 1 ))]}
285 error_log " $
(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
292 if ! ${assertion}; then
293 error_log
"Assertion '${assertion}' failed."
295 exit ${EXIT_ERROR_ASSERT}
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.
303 assert_check_retval
() {
306 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
307 exit ${EXIT_ERROR_ASSERT}
316 log DEBUG
"Running command: ${cmd}"
319 LOG_DISABLE_STDOUT
="${LOG_DISABLE_STDOUT}" \
320 LOG_FACILITY
="${LOG_FACILITY}" \
324 #log DEBUG "Returned with code '${ret}'"
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}
337 log DEBUG
"Running command: ${cmd}"
342 log DEBUG
"Returned with code '${ret}'"
354 log DEBUG
"Exec'ing command: ${cmd}"
358 log ERROR
"Could not exec-ute: ${cmd}"
362 cmd_not_implemented
() {
363 assert false
"not implemented"
366 # Runs a command in a clean environment so that no confidential information
367 # is leaked to any untrusted commands.
368 cmd_clean_environment
() {
371 log DEBUG
"Running command in a clean environment: ${cmd}"
375 log DEBUG
"Returned with code '${ret}'"
379 # Increase security of the read command
385 if [ $# -eq 2 ]; then
386 eval echo {${1}..
${2}}
387 elif [ $# -eq 3 ]; then
388 eval echo {${1}..${3}..${2}}
396 # Prints the number of seconds since epoch.
406 for unit
in s m h d w
; do
419 [ ${value} -lt ${limit} ] && break
421 value
=$
(( ${value} / ${limit} ))
424 echo "${value}${unit}"
432 for unit
in B k M G T
; do
433 [ ${value} -lt ${limit} ] && break
434 value
=$
(( ${value} / ${limit} ))
437 echo "${value}${unit}"
443 if ! grep -q "^${module}" /proc
/modules
; then
444 log DEBUG
"Loading module '${module}'."
452 if [ -n "$(type -p ${binary})" ]; then
460 local function="${1}"
462 if [ "$(type -t "${function}")" = "function" ]; then
472 if ! isinteger process
; then
473 process
=$
(pidof
${process})
478 for pid
in ${process}; do
480 [ -d "/proc/${pid}" ] ||
break
491 if [ "${hex:0:2}" != "0x" ]; then
495 printf "%d\n" "${hex}"
501 [ ${char} -lt 256 ] ||
return ${EXIT_ERROR}
503 printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n"
507 LC_CTYPE
="C" printf "%d\n" "'${1}"
514 network_is_running
() {
515 # Check, if the network service is running.
516 service_is_active network
523 local var2
=${var// /}
525 if [ ${#var} -ne ${#var2} ]; then
536 while [ ${pos} -lt ${#string} ]; do
537 print
"${string:${pos}:1}"
538 pos
=$
(( ${pos} + 1 ))
549 while [ ${pos} -lt ${#string} ]; do
550 output
="${string:${pos}:1}${output}"
551 pos
=$
(( ${pos} + 1 ))
564 for i
in 7 6 5 4 3 2 1; do
567 if [ $
(( ${number} / ${div} )) -eq 1 ]; then
572 number
="$(( ${number} % ${div} ))"
575 if [ $
(( ${number} % 2 )) -eq 1 ]; then
589 while [ ${pos} -lt ${#string} ]; do
590 char
="${string:${pos}:1}"
591 pos
=$
(( ${pos} + 1 ))
593 number
=$
(( ${number} << 1 ))
598 number
=$
(( ${number} + 1 ))
601 assert false
"Invalid character: ${char}"
611 local dec
="$(ord "${1}")"
617 local dec
="$(bin2dec "$@
")"
623 local dec
="$(bin2dec "$@
")"
629 local dec
="$(hex2dec "$@
")"
637 # Prepend 0x if necessary.
638 [ "${hex:0:2}" = "0x" ] || hex
="0x${hex}"
640 printf "%d\n" "${hex}"
644 printf "%02x\n" "${1}"