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" "$@"
40 # The args() function takes a number of arguments like
41 # var1="abc d" var2="abc" var3="abcd e"
42 # and splits them into several arguments, devided by newline
44 echo "$@" |
xargs printf "%s\n"
50 if [ "${var:0:1}" = "\"" ]; then
54 local last
=$
(( ${#var} - 1 ))
55 if [ ${last} -ge 0 ] && [ "${var:${last}:1}" = "\"" ]; then
69 # remove leading whitespace characters
70 value="${value#"${value%%[![:space:]]*}"}"
72 # remove trailing whitespace characters
73 value="${value%"${value##*[![:space:]]}"}"
78 # Print a pretty error message
80 echo -e " ${CLR_RED_B}ERROR
${CLR_RESET} : $@
" >&2
87 # Print a pretty warn message
89 echo -e " ${CLR_YELLOW_B}WARNING
${CLR_RESET}: $@
" >&2
96 # Speedup function to avoid a call of the basename binary
110 printf -v "${key}" "${format}" "$@
"
120 for unit in ${units}; do
124 if [ ${m} -gt 0 ]; then
125 ret="${m}${unit} ${ret}"
150 if ! isinteger arg; then
154 # Convert hours and minutes into seconds
157 arg=$(( ${arg} * 3600 ))
160 arg=$(( ${arg} * 60 ))
165 ret=$(( ${ret} + ${arg} ))
176 format "${key}" "%s
" "$@
"
183 [ -r "${file}" ] || return ${EXIT_ERROR}
193 if [ ! -w "${file}" ]; then
194 log ERROR "${file}: No such
file"
198 print "%s
" "$@
" >> ${file} 2>/dev/null
204 local dirname="$
(dirname "${path}")"
205 mkdir -p "${dirname}"
211 list_match "${!param}" yes on true 1
215 local b="$
(random
12)"
217 # Remove multicast bit
218 # and set address is software assigned
219 local first_byte=$(( 0x${b:0:2} & 0xfe ))
220 first_byte=$(( ${first_byte} | 0x02 ))
223 printf -v output "%02x
" "${first_byte}"
225 output="${output}:${b:2:2}:${b:4:2}:${b:6:2}:${b:8:2}:${b:10:2}"
227 # Check if output is valid
228 assert mac_is_valid "${output}"
237 # Remove all colons and make the rest lowercase.
242 if [ "${#mac}" = "12" ]; then
243 # Add colons (:) to mac address
246 for i in 2 4 6 8 10; do
247 output="${output}:${mac:${i}:2}"
253 assert mac_is_valid ${output}
261 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
265 echo $(</proc/sys/kernel/random/uuid)
271 if [ ${val} -lt 0 ]; then
284 local length="${1:-8}"
287 while [ ${#random} -lt ${length} ]; do
288 random="${random}$
(rand
)"
291 echo "${random:0:${length}}"
304 list_match "${var}" "$@
"
310 isoneof ${var} 0 1 no yes on off true false
316 [[ ${var} =~ ^[0-9]+$ ]]
337 [ ${mtu} -ge 576 ] && [ ${mtu} -le 9000 ]
340 [ ${mtu} -ge 1280 ] && [ ${mtu} -le 9000 ]
343 error "${proto} is not a valid proto
"
353 error_log "Backtrace
(most recent call
in first line
):"
356 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
357 [ -z "${FUNCNAME[${i}]}" ] && continue
359 # Print called binary with arguments.
360 if [ "${FUNCNAME[${i}]}" == "main
" ]; then
361 local args="$
(list_reverse
${BASH_ARGV[*]})"
362 printf -v source "%20s
" "$0"
363 error_log " ${source} ${args}"
367 source=${BASH_SOURCE[$(( ${i} + 1 ))]}
368 error_log " $
(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
375 if ! ${assertion}; then
376 error_log
"Assertion '${assertion}' failed."
378 exit ${EXIT_ERROR_ASSERT}
384 # This function checks, if the given argument is an assert error
385 # exit code. If this is the case, the script will halt immediately.
386 assert_check_retval
() {
389 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
390 exit ${EXIT_ERROR_ASSERT}
396 # This function executes the given command and inverses the return code
400 ${command} && return ${EXIT_FALSE} || return ${EXIT_TRUE}
406 log DEBUG
"Running command: ${cmd}"
409 LOG_DISABLE_STDOUT
="${LOG_DISABLE_STDOUT}" \
410 LOG_FACILITY
="${LOG_FACILITY}" \
414 #log DEBUG "Returned with code '${ret}'"
416 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
417 error_log
"Stopping parent process due to assertion error in child process: ${cmd}"
418 exit ${EXIT_ERROR_ASSERT}
427 log DEBUG
"Running command: ${cmd}"
432 log DEBUG
"Returned with code '${ret}'"
446 log DEBUG
"Exec'ing command: ${cmd}"
450 log ERROR
"Could not exec-ute: ${cmd}"
454 cmd_not_implemented
() {
455 assert false
"not implemented"
458 # Runs a command in a clean environment so that no confidential information
459 # is leaked to any untrusted commands.
460 cmd_clean_environment
() {
463 log DEBUG
"Running command in a clean environment: ${cmd}"
467 log DEBUG
"Returned with code '${ret}'"
471 # Executes the given command in background
476 # Prints the PID of the process that was started last
477 cmd_background_get_pid
() {
481 cmd_background_result
() {
487 # Increase security of the read command
493 if [ $# -eq 2 ]; then
494 eval echo {${1}..
${2}}
495 elif [ $# -eq 3 ]; then
496 eval echo {${1}..${3}..${2}}
501 eval echo {0..$
(( ${1} - 1 ))}
518 # Prints the number of seconds since epoch.
528 for unit
in s m h d w
; do
541 [ ${value} -lt ${limit} ] && break
543 value
=$
(( ${value} / ${limit} ))
546 echo "${value}${unit}"
554 for unit
in B k M G T
; do
555 [ ${value} -lt ${limit} ] && break
556 value
=$
(( ${value} / ${limit} ))
559 echo "${value}${unit}"
565 if ! grep -q "^${module}" /proc
/modules
; then
566 log DEBUG
"Loading module '${module}'."
574 if [ -n "$(type -p ${binary})" ]; then
582 local function="${1}"
584 if [ "$(type -t "${function}")" = "function" ]; then
594 if ! isinteger process
; then
595 process
=$
(pidof
${process})
600 for pid
in ${process}; do
602 [ -d "/proc/${pid}" ] ||
break
613 if [ "${hex:0:2}" != "0x" ]; then
617 printf "%d\n" "${hex}"
623 [ ${char} -lt 256 ] ||
return ${EXIT_ERROR}
625 printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n"
629 LC_CTYPE
="C" printf "%d\n" "'${1}"
636 network_is_running
() {
637 # Check, if the network service is running.
638 service_is_active network
645 local var2
=${var// /}
647 if [ ${#var} -ne ${#var2} ]; then
658 [[ ${string} =~ ${match} ]] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
665 while [ ${pos} -lt ${#string} ]; do
666 print
"${string:${pos}:1}"
667 pos
=$
(( ${pos} + 1 ))
678 while [ ${pos} -lt ${#string} ]; do
679 output
="${string:${pos}:1}${output}"
680 pos
=$
(( ${pos} + 1 ))
693 for i
in 7 6 5 4 3 2 1; do
696 if [ $
(( ${number} / ${div} )) -eq 1 ]; then
701 number
="$(( ${number} % ${div} ))"
704 if [ $
(( ${number} % 2 )) -eq 1 ]; then
718 while [ ${pos} -lt ${#string} ]; do
719 char
="${string:${pos}:1}"
720 pos
=$
(( ${pos} + 1 ))
722 number
=$
(( ${number} << 1 ))
727 number
=$
(( ${number} + 1 ))
730 assert false
"Invalid character: ${char}"
740 local dec
="$(ord "${1}")"
746 local dec
="$(bin2dec "$@
")"
752 local dec
="$(bin2dec "$@
")"
758 local dec
="$(hex2dec "$@
")"
766 # Prepend 0x if necessary.
767 [ "${hex:0:2}" = "0x" ] || hex
="0x${hex}"
769 printf "%d\n" "${hex}"
773 printf "%02x\n" "${1}"
776 # This function just copy config files
783 # Check if we can read from the source
784 if [ ! -r "${src}" ]; then
785 log ERROR
"Cannot read ${src}"
789 # Check if ${dst} is a directory
790 if [ -d "${dst}" ]; then
791 log ERROR
"${dst} is a directory"
795 if ! fread
"${src}" > "${dst}"; then
796 log ERROR
"Could not copy data from ${src} to ${dst}"