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
90 # Speedup function to avoid a call of the basename binary
104 printf -v "${key}" "${format}" "$@
"
114 for unit in ${units}; do
118 if [ ${m} -gt 0 ]; then
119 ret="${m}${unit} ${ret}"
144 if ! isinteger arg; then
148 # Convert hours and minutes into seconds
151 arg=$(( ${arg} * 3600 ))
154 arg=$(( ${arg} * 60 ))
159 ret=$(( ${ret} + ${arg} ))
170 format "${key}" "%s
" "$@
"
177 [ -r "${file}" ] || return ${EXIT_ERROR}
187 if [ ! -w "${file}" ]; then
188 log ERROR "${file}: No such
file"
192 print "%s
" "$@
" >> ${file} 2>/dev/null
198 local dirname="$
(dirname "${path}")"
199 mkdir -p "${dirname}"
205 list_match "${!param}" yes on true 1
209 local b="$
(random
12)"
211 # Remove multicast bit
212 # and set address is software assigned
213 local first_byte=$(( 0x${b:0:2} & 0xfe ))
214 first_byte=$(( ${first_byte} | 0x02 ))
217 printf -v output "%02x
" "${first_byte}"
219 output="${output}:${b:2:2}:${b:4:2}:${b:6:2}:${b:8:2}:${b:10:2}"
221 # Check if output is valid
222 assert mac_is_valid "${output}"
231 # Remove all colons and make the rest lowercase.
236 if [ "${#mac}" = "12" ]; then
237 # Add colons (:) to mac address
240 for i in 2 4 6 8 10; do
241 output="${output}:${mac:${i}:2}"
247 assert mac_is_valid ${output}
255 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
259 echo $(</proc/sys/kernel/random/uuid)
265 if [ ${val} -lt 0 ]; then
278 local length="${1:-8}"
281 while [ ${#random} -lt ${length} ]; do
282 random="${random}$
(rand
)"
285 echo "${random:0:${length}}"
298 list_match "${var}" "$@
"
304 isoneof ${var} 0 1 no yes on off true false
310 [[ ${var} =~ ^[0-9]+$ ]]
331 [ ${mtu} -ge 576 ] && [ ${mtu} -le 9000 ]
334 [ ${mtu} -ge 1280 ] && [ ${mtu} -le 9000 ]
337 error "${proto} is not a valid proto
"
347 error_log "Backtrace
(most recent call
in first line
):"
350 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
351 [ -z "${FUNCNAME[${i}]}" ] && continue
353 # Print called binary with arguments.
354 if [ "${FUNCNAME[${i}]}" == "main
" ]; then
355 local args="$
(list_reverse
${BASH_ARGV[*]})"
356 printf -v source "%20s
" "$0"
357 error_log " ${source} ${args}"
361 source=${BASH_SOURCE[$(( ${i} + 1 ))]}
362 error_log " $
(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
369 if ! ${assertion}; then
370 error_log
"Assertion '${assertion}' failed."
372 exit ${EXIT_ERROR_ASSERT}
378 # This function checks, if the given argument is an assert error
379 # exit code. If this is the case, the script will halt immediately.
380 assert_check_retval
() {
383 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
384 exit ${EXIT_ERROR_ASSERT}
390 # This function executes the given command and inverses the return code
394 ${command} && return ${EXIT_FALSE} || return ${EXIT_TRUE}
400 log DEBUG
"Running command: ${cmd}"
403 LOG_DISABLE_STDOUT
="${LOG_DISABLE_STDOUT}" \
404 LOG_FACILITY
="${LOG_FACILITY}" \
408 #log DEBUG "Returned with code '${ret}'"
410 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
411 error_log
"Stopping parent process due to assertion error in child process: ${cmd}"
412 exit ${EXIT_ERROR_ASSERT}
421 log DEBUG
"Running command: ${cmd}"
426 log DEBUG
"Returned with code '${ret}'"
438 log DEBUG
"Exec'ing command: ${cmd}"
442 log ERROR
"Could not exec-ute: ${cmd}"
446 cmd_not_implemented
() {
447 assert false
"not implemented"
450 # Runs a command in a clean environment so that no confidential information
451 # is leaked to any untrusted commands.
452 cmd_clean_environment
() {
455 log DEBUG
"Running command in a clean environment: ${cmd}"
459 log DEBUG
"Returned with code '${ret}'"
463 # Executes the given command in background
468 # Prints the PID of the process that was started last
469 cmd_background_get_pid
() {
473 cmd_background_result
() {
479 # Increase security of the read command
485 if [ $# -eq 2 ]; then
486 eval echo {${1}..
${2}}
487 elif [ $# -eq 3 ]; then
488 eval echo {${1}..${3}..${2}}
493 eval echo {0..$
(( ${1} - 1 ))}
510 # Prints the number of seconds since epoch.
520 for unit
in s m h d w
; do
533 [ ${value} -lt ${limit} ] && break
535 value
=$
(( ${value} / ${limit} ))
538 echo "${value}${unit}"
546 for unit
in B k M G T
; do
547 [ ${value} -lt ${limit} ] && break
548 value
=$
(( ${value} / ${limit} ))
551 echo "${value}${unit}"
557 if ! grep -q "^${module}" /proc
/modules
; then
558 log DEBUG
"Loading module '${module}'."
566 if [ -n "$(type -p ${binary})" ]; then
574 local function="${1}"
576 if [ "$(type -t "${function}")" = "function" ]; then
586 if ! isinteger process
; then
587 process
=$
(pidof
${process})
592 for pid
in ${process}; do
594 [ -d "/proc/${pid}" ] ||
break
605 if [ "${hex:0:2}" != "0x" ]; then
609 printf "%d\n" "${hex}"
615 [ ${char} -lt 256 ] ||
return ${EXIT_ERROR}
617 printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n"
621 LC_CTYPE
="C" printf "%d\n" "'${1}"
628 network_is_running
() {
629 # Check, if the network service is running.
630 service_is_active network
637 local var2
=${var// /}
639 if [ ${#var} -ne ${#var2} ]; then
650 while [ ${pos} -lt ${#string} ]; do
651 print
"${string:${pos}:1}"
652 pos
=$
(( ${pos} + 1 ))
663 while [ ${pos} -lt ${#string} ]; do
664 output
="${string:${pos}:1}${output}"
665 pos
=$
(( ${pos} + 1 ))
678 for i
in 7 6 5 4 3 2 1; do
681 if [ $
(( ${number} / ${div} )) -eq 1 ]; then
686 number
="$(( ${number} % ${div} ))"
689 if [ $
(( ${number} % 2 )) -eq 1 ]; then
703 while [ ${pos} -lt ${#string} ]; do
704 char
="${string:${pos}:1}"
705 pos
=$
(( ${pos} + 1 ))
707 number
=$
(( ${number} << 1 ))
712 number
=$
(( ${number} + 1 ))
715 assert false
"Invalid character: ${char}"
725 local dec
="$(ord "${1}")"
731 local dec
="$(bin2dec "$@
")"
737 local dec
="$(bin2dec "$@
")"
743 local dec
="$(hex2dec "$@
")"
751 # Prepend 0x if necessary.
752 [ "${hex:0:2}" = "0x" ] || hex
="0x${hex}"
754 printf "%d\n" "${hex}"
758 printf "%02x\n" "${1}"