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
72 function error_log() {
76 # Print a pretty warn message
78 echo -e " ${CLR_YELLOW_B}WARNING
${CLR_RESET}: $@
" >&2
81 function warning_log() {
85 # The next three functions are kept for backwards
86 # compatibility. The need to be dropped at some time.
91 function listmatch() {
95 function listlength() {
99 # Speedup function to avoid a call of the basename binary
100 function basename() {
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 list_match "${!param}" yes on true 1
152 function mac_generate() {
153 # Get a bunch of random hex digits
154 # and remove all dashes from the input.
155 local random=$(</proc/sys/kernel/random/uuid)
162 for i in $(seq 0 5); do
164 random="${random:2:${#random}}"
168 # Remove multicast bit
169 # and set address is software assigned
173 printf -v output "%02x
" "${o}"
176 printf -v output "%s
:%02x
" "${output}" "${o}"
181 # Check if output is valid
182 assert mac_is_valid ${output}
187 function mac_format() {
191 # Remove all colons and make the rest lowercase.
196 if [ "${#mac}" = "12" ]; then
197 # Add colons (:) to mac address
200 for i in 2 4 6 8 10; do
201 output="${output}:${mac:${i}:2}"
207 assert mac_is_valid ${output}
212 function mac_is_valid() {
215 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
219 echo $(</proc/sys/kernel/random/uuid)
232 list_match "${var}" "$@
"
238 isoneof ${var} 0 1 no yes on off
241 function isinteger() {
244 [[ ${var} =~ ^[0-9]+$ ]]
253 function isipaddress() {
259 function backtrace() {
263 error_log "Backtrace
(most recent call
in first line
):"
266 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
267 [ -z "${FUNCNAME[${i}]}" ] && continue
269 # Print called binary with arguments.
270 if [ "${FUNCNAME[${i}]}" == "main
" ]; then
271 local args="$
(list_reverse
${BASH_ARGV[*]})"
272 printf -v source "%20s
" "$0"
273 error_log " ${source} ${args}"
277 source=${BASH_SOURCE[$(( ${i} + 1 ))]}
278 error_log " $
(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
285 if ! ${assertion}; then
286 error_log
"Assertion '${assertion}' failed."
288 exit ${EXIT_ERROR_ASSERT}
294 # This function checks, if the given argument is an assert error
295 # exit code. If this is the case, the script will halt immediately.
296 function assert_check_retval
() {
299 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
300 exit ${EXIT_ERROR_ASSERT}
306 function exec_cmd
() {
309 log DEBUG
"Running command: ${cmd}"
312 LOG_DISABLE_STDOUT
="${LOG_DISABLE_STDOUT}" \
313 LOG_FACILITY
="${LOG_FACILITY}" \
317 #log DEBUG "Returned with code '${ret}'"
319 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
320 error_log
"Stopping parent process due to assertion error in child process: ${cmd}"
321 exit ${EXIT_ERROR_ASSERT}
330 log DEBUG
"Running command: ${cmd}"
335 log DEBUG
"Returned with code '${ret}'"
340 function cmd_quiet
() {
344 function cmd_exec
() {
347 log DEBUG
"Exec'ing command: ${cmd}"
351 log ERROR
"Could not exec-ute: ${cmd}"
355 function cmd_not_implemented
() {
356 assert false
"not implemented"
359 # Increase security of the read command
365 if [ $# -eq 2 ]; then
366 eval echo {${1}..
${2}}
367 elif [ $# -eq 3 ]; then
368 eval echo {${1}..${3}..${2}}
376 # Prints the number of seconds since epoch.
377 function timestamp
() {
381 function beautify_time
() {
386 for unit
in s m h d w
; do
399 [ ${value} -lt ${limit} ] && break
401 value
=$
(( ${value} / ${limit} ))
404 echo "${value}${unit}"
407 function beautify_bytes
() {
412 for unit
in B k M G T
; do
413 [ ${value} -lt ${limit} ] && break
414 value
=$
(( ${value} / ${limit} ))
417 echo "${value}${unit}"
420 function module_load
() {
423 if ! grep -q "^${module}" /proc
/modules
; then
424 log DEBUG
"Loading module '${module}'."
429 function binary_exists
() {
432 if [ -n "$(type -p ${binary})" ]; then
439 function function_exists
() {
440 local function="${1}"
442 if [ "$(type -t "${function}")" = "function" ]; then
449 function process_kill
() {
452 if ! isinteger process
; then
453 process
=$
(pidof
${process})
458 for pid
in ${process}; do
460 [ -d "/proc/${pid}" ] ||
break
471 if [ "${hex:0:2}" != "0x" ]; then
475 printf "%d\n" "${hex}"
481 [ ${char} -lt 256 ] ||
return ${EXIT_ERROR}
483 printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n"
487 LC_CTYPE
="C" printf "%d\n" "'${1}"
494 function network_is_running
() {
495 # Check, if the network service is running.
496 service_is_active network
499 function contains_spaces
() {
503 local var2
=${var// /}
505 if [ ${#var} -ne ${#var2} ]; then
512 function string_split
() {
516 while [ ${pos} -lt ${#string} ]; do
517 print
"${string:${pos}:1}"
518 pos
=$
(( ${pos} + 1 ))
524 function string_reverse
() {
529 while [ ${pos} -lt ${#string} ]; do
530 output
="${string:${pos}:1}${output}"
531 pos
=$
(( ${pos} + 1 ))
544 for i
in 7 6 5 4 3 2 1; do
547 if [ $
(( ${number} / ${div} )) -eq 1 ]; then
552 number
="$(( ${number} % ${div} ))"
555 if [ $
(( ${number} % 2 )) -eq 1 ]; then
569 while [ ${pos} -lt ${#string} ]; do
570 char
="${string:${pos}:1}"
571 pos
=$
(( ${pos} + 1 ))
573 number
=$
(( ${number} << 1 ))
578 number
=$
(( ${number} + 1 ))
581 assert false
"Invalid character: ${char}"
590 function char2bin
() {
591 local dec
="$(ord "${1}")"
596 function bin2char
() {
597 local dec
="$(bin2dec "$@
")"
603 local dec
="$(bin2dec "$@
")"
609 local dec
="$(hex2dec "$@
")"
617 # Prepend 0x if necessary.
618 [ "${hex:0:2}" = "0x" ] || hex
="0x${hex}"
620 printf "%d\n" "${hex}"
624 printf "%02x\n" "${1}"