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
55 # Print a pretty error message
57 echo -e " ${CLR_RED_B}ERROR
${CLR_RESET} : $@
" >&2
60 function error_log() {
64 # Print a pretty warn message
66 echo -e " ${CLR_YELLOW_B}WARNING
${CLR_RESET}: $@
" >&2
69 function warning_log() {
73 # The next three functions are kept for backwards
74 # compatibility. The need to be dropped at some time.
79 function listmatch() {
83 function listlength() {
87 # Speedup function to avoid a call of the basename binary
97 printf -v "${key}" "%s
" "$@
"
104 [ -r "${file}" ] || return ${EXIT_ERROR}
114 print "%s
" "$@
" >> ${file}
120 list_match "${!param}" yes on true 1
123 function mac_generate() {
124 # Get a bunch of random hex digits
125 # and remove all dashes from the input.
126 local random=$(</proc/sys/kernel/random/uuid)
133 for i in $(seq 0 5); do
135 random="${random:2:${#random}}"
139 # Remove multicast bit
140 # and set address is software assigned
144 printf -v output "%02x
" "${o}"
147 printf -v output "%s
:%02x
" "${output}" "${o}"
152 # Check if output is valid
153 assert mac_is_valid ${output}
158 function mac_format() {
162 # Remove all colons and make the rest lowercase.
167 if [ "${#mac}" = "12" ]; then
168 # Add colons (:) to mac address
171 for i in 2 4 6 8 10; do
172 output="${output}:${mac:${i}:2}"
178 assert mac_is_valid ${output}
183 function mac_is_valid() {
186 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
190 echo $(</proc/sys/kernel/random/uuid)
203 list_match "${var}" "$@
"
209 isoneof ${var} 0 1 no yes on off
212 function isinteger() {
215 [[ ${var} =~ ^[0-9]+$ ]]
224 function isipaddress() {
230 function backtrace() {
234 error_log "Backtrace
(most recent call
in first line
):"
237 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
238 [ -z "${FUNCNAME[${i}]}" ] && continue
239 [ "${FUNCNAME[${i}]}" == "main
" ] && continue
241 source=${BASH_SOURCE[$(( ${i} + 1 ))]}
242 error_log " $
(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
249 if ! ${assertion}; then
250 error_log
"Assertion '${assertion}' failed."
252 exit ${EXIT_ERROR_ASSERT}
258 # This function checks, if the given argument is an assert error
259 # exit code. If this is the case, the script will halt immediately.
260 function assert_check_retval
() {
263 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
264 exit ${EXIT_ERROR_ASSERT}
270 function exec_cmd
() {
273 log DEBUG
"Running command: ${cmd}"
276 LOG_DISABLE_STDOUT
="${LOG_DISABLE_STDOUT}" \
277 LOG_FACILITY
="${LOG_FACILITY}" \
281 #log DEBUG "Returned with code '${ret}'"
283 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
284 error_log
"Stopping parent process due to assertion error in child process: ${cmd}"
285 exit ${EXIT_ERROR_ASSERT}
294 log DEBUG
"Running command: ${cmd}"
299 log DEBUG
"Returned with code '${ret}'"
304 function cmd_quiet
() {
308 function cmd_exec
() {
311 log DEBUG
"Exec'ing command: ${cmd}"
315 log ERROR
"Could not exec-ute: ${cmd}"
320 if [ $# -eq 2 ]; then
321 eval echo {${1}..
${2}}
322 elif [ $# -eq 3 ]; then
323 eval echo {${1}..${3}..${2}}
331 function beautify_time
() {
336 for unit
in s m h d w
; do
349 [ ${value} -lt ${limit} ] && break
351 value
=$
(( ${value} / ${limit} ))
354 echo "${value}${unit}"
357 function beautify_bytes
() {
362 for unit
in B k M G T
; do
363 [ ${value} -lt ${limit} ] && break
364 value
=$
(( ${value} / ${limit} ))
367 echo "${value}${unit}"
370 function module_load
() {
373 if ! grep -q "^${module}" /proc
/modules
; then
374 log DEBUG
"Loading module '${module}'."
379 function binary_exists
() {
382 if [ -n "$(type -p ${binary})" ]; then
389 function process_kill
() {
392 if ! isinteger process
; then
393 process
=$
(pidof
${process})
398 for pid
in ${process}; do
400 [ -d "/proc/${pid}" ] ||
break
411 if [ "${hex:0:2}" != "0x" ]; then
415 printf "%d\n" "${hex}"
418 function network_is_running
() {
419 # Check, if the network service is running.
420 service_is_active network
423 function contains_spaces
() {
427 local var2
=${var// /}
429 if [ ${#var} -ne ${#var2} ]; then