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 # Speedup function to avoid a call of the basename binary
99 printf -v "${key}" "${format}" "$@
"
109 for unit in ${units}; do
113 if [ ${m} -gt 0 ]; then
114 ret="${m}${unit} ${ret}"
139 if ! isinteger arg; then
143 # Convert hours and minutes into seconds
146 arg=$(( ${arg} * 3600 ))
149 arg=$(( ${arg} * 60 ))
154 ret=$(( ${ret} + ${arg} ))
165 format "${key}" "%s
" "$@
"
172 [ -r "${file}" ] || return ${EXIT_ERROR}
182 if [ ! -w "${file}" ]; then
183 log ERROR "${file}: No such
file"
187 print "%s
" "$@
" >> ${file} 2>/dev/null
193 local dirname="$
(dirname "${path}")"
194 mkdir -p "${dirname}"
200 list_match "${!param}" yes on true 1
204 local b="$
(random
12)"
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 ))
212 printf -v output "%02x
" "${first_byte}"
214 output="${output}:${b:2:2}:${b:4:2}:${b:6:2}:${b:8:2}:${b:10:2}"
216 # Check if output is valid
217 assert mac_is_valid "${output}"
226 # Remove all colons and make the rest lowercase.
231 if [ "${#mac}" = "12" ]; then
232 # Add colons (:) to mac address
235 for i in 2 4 6 8 10; do
236 output="${output}:${mac:${i}:2}"
242 assert mac_is_valid ${output}
250 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
254 echo $(</proc/sys/kernel/random/uuid)
260 if [ ${val} -lt 0 ]; then
273 local length="${1:-8}"
276 while [ ${#random} -lt ${length} ]; do
277 random="${random}$
(rand
)"
280 echo "${random:0:${length}}"
293 list_match "${var}" "$@
"
299 isoneof ${var} 0 1 no yes on off true false
305 [[ ${var} =~ ^[0-9]+$ ]]
326 [ ${mtu} -ge 576 ] && [ ${mtu} -le 9000 ]
329 [ ${mtu} -ge 1280 ] && [ ${mtu} -le 9000 ]
332 error "${proto} is not a valid proto
"
342 error_log "Backtrace
(most recent call
in first line
):"
345 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
346 [ -z "${FUNCNAME[${i}]}" ] && continue
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}"
356 source=${BASH_SOURCE[$(( ${i} + 1 ))]}
357 error_log " $
(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
364 if ! ${assertion}; then
365 error_log
"Assertion '${assertion}' failed."
367 exit ${EXIT_ERROR_ASSERT}
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.
375 assert_check_retval
() {
378 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
379 exit ${EXIT_ERROR_ASSERT}
385 # This function executes the given command and inverses the return code
389 ${command} && return ${EXIT_FALSE} || return ${EXIT_TRUE}
395 log DEBUG
"Running command: ${cmd}"
398 LOG_DISABLE_STDOUT
="${LOG_DISABLE_STDOUT}" \
399 LOG_FACILITY
="${LOG_FACILITY}" \
403 #log DEBUG "Returned with code '${ret}'"
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}
416 log DEBUG
"Running command: ${cmd}"
421 log DEBUG
"Returned with code '${ret}'"
433 log DEBUG
"Exec'ing command: ${cmd}"
437 log ERROR
"Could not exec-ute: ${cmd}"
441 cmd_not_implemented
() {
442 assert false
"not implemented"
445 # Runs a command in a clean environment so that no confidential information
446 # is leaked to any untrusted commands.
447 cmd_clean_environment
() {
450 log DEBUG
"Running command in a clean environment: ${cmd}"
454 log DEBUG
"Returned with code '${ret}'"
458 # Executes the given command in background
463 # Prints the PID of the process that was started last
464 cmd_background_get_pid
() {
468 cmd_background_result
() {
474 # Increase security of the read command
480 if [ $# -eq 2 ]; then
481 eval echo {${1}..
${2}}
482 elif [ $# -eq 3 ]; then
483 eval echo {${1}..${3}..${2}}
488 eval echo {0..$
(( ${1} - 1 ))}
505 # Prints the number of seconds since epoch.
515 for unit
in s m h d w
; do
528 [ ${value} -lt ${limit} ] && break
530 value
=$
(( ${value} / ${limit} ))
533 echo "${value}${unit}"
541 for unit
in B k M G T
; do
542 [ ${value} -lt ${limit} ] && break
543 value
=$
(( ${value} / ${limit} ))
546 echo "${value}${unit}"
552 if ! grep -q "^${module}" /proc
/modules
; then
553 log DEBUG
"Loading module '${module}'."
561 if [ -n "$(type -p ${binary})" ]; then
569 local function="${1}"
571 if [ "$(type -t "${function}")" = "function" ]; then
581 if ! isinteger process
; then
582 process
=$
(pidof
${process})
587 for pid
in ${process}; do
589 [ -d "/proc/${pid}" ] ||
break
600 if [ "${hex:0:2}" != "0x" ]; then
604 printf "%d\n" "${hex}"
610 [ ${char} -lt 256 ] ||
return ${EXIT_ERROR}
612 printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n"
616 LC_CTYPE
="C" printf "%d\n" "'${1}"
623 network_is_running
() {
624 # Check, if the network service is running.
625 service_is_active network
632 local var2
=${var// /}
634 if [ ${#var} -ne ${#var2} ]; then
645 while [ ${pos} -lt ${#string} ]; do
646 print
"${string:${pos}:1}"
647 pos
=$
(( ${pos} + 1 ))
658 while [ ${pos} -lt ${#string} ]; do
659 output
="${string:${pos}:1}${output}"
660 pos
=$
(( ${pos} + 1 ))
673 for i
in 7 6 5 4 3 2 1; do
676 if [ $
(( ${number} / ${div} )) -eq 1 ]; then
681 number
="$(( ${number} % ${div} ))"
684 if [ $
(( ${number} % 2 )) -eq 1 ]; then
698 while [ ${pos} -lt ${#string} ]; do
699 char
="${string:${pos}:1}"
700 pos
=$
(( ${pos} + 1 ))
702 number
=$
(( ${number} << 1 ))
707 number
=$
(( ${number} + 1 ))
710 assert false
"Invalid character: ${char}"
720 local dec
="$(ord "${1}")"
726 local dec
="$(bin2dec "$@
")"
732 local dec
="$(bin2dec "$@
")"
738 local dec
="$(hex2dec "$@
")"
746 # Prepend 0x if necessary.
747 [ "${hex:0:2}" = "0x" ] || hex
="0x${hex}"
749 printf "%d\n" "${hex}"
753 printf "%02x\n" "${1}"
756 # This function just copy config files
763 # Check if ${dst} is a directory
764 if [ -d "${dst}" ]; then
765 log ERROR
"${dst} is a directory"
769 if ! fread
"${src}" > "${dst}"; then
770 log ERROR
"Could not copy data from ${src} to ${dst}"