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 # Print a pretty error message
31 echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL} : $@" >&2
34 function error_log
() {
38 # Print a pretty warn message
40 echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2
43 function warning_log
() {
51 done |
sort |
tr '\n' ' '
55 function listmatch
() {
61 [ "${match}" = "${i}" ] && return ${EXIT_OK}
67 function listlength() {
72 length=$(( ${length} + 1 ))
78 # Speedup function to avoid a call of the basename binary
86 listmatch "${!param}" yes on true 1
89 function mac_generate() {
90 # Get a bunch of random hex digits
91 # and remove all dashes from the input.
92 local random=$(</proc/sys/kernel/random/uuid)
99 for i in $(seq 0 5); do
101 random="${random:2:${#random}}"
105 # Remove multicast bit
106 # and set address is software assigned
110 printf -v output "%02x
" "${o}"
113 printf -v output "%s
:%02x
" "${output}" "${o}"
118 # Check if output is valid
119 assert mac_is_valid ${output}
124 function mac_format() {
129 if [ "${#mac}" = "12" ]; then
130 # Add colons (:) to mac address
133 for i in 2 4 6 8 10; do
134 output="${output}:${mac:${i}:2}"
138 assert mac_is_valid ${output}
143 function mac_is_valid() {
146 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
150 echo $(</proc/sys/kernel/random/uuid)
163 listmatch "${var}" "$@
"
169 isoneof ${var} 0 1 no yes on off
172 function isinteger() {
175 [[ ${var} =~ ^[0-9]+$ ]]
184 function backtrace() {
188 error_log "Backtrace
(most recent call
in first line
):"
191 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
192 [ -z "${FUNCNAME[${i}]}" ] && continue
193 [ "${FUNCNAME[${i}]}" == "main
" ] && continue
195 error_log " $
(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
202 if ! ${assertion}; then
203 error_log
"Assertion '${assertion}' failed."
211 function exec_cmd
() {
214 log DEBUG
"Running command: ${cmd}"
217 LOG_DISABLE_STDOUT
="${LOG_DISABLE_STDOUT}" \
218 LOG_FACILITY
="${LOG_FACILITY}" \
222 #log DEBUG "Returned with code '${ret}'"
224 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
225 error_log
"Stopping parent process due to assertion error in child process: ${cmd}"
226 exit ${EXIT_ERROR_ASSERT}
235 log DEBUG
"Running command: ${cmd}"
240 log DEBUG
"Returned with code '${ret}'"
245 function cmd_quiet
() {
250 if [ $# -eq 2 ]; then
251 eval echo {${1}..
${2}}
252 elif [ $# -eq 3 ]; then
253 eval echo {${1}..${3}..${2}}
257 function beautify_time
() {
262 for unit
in s m h d w
; do
275 [ ${value} -lt ${limit} ] && break
277 value
=$
(( ${value} / ${limit} ))
280 echo "${value}${unit}"
283 function beautify_bytes
() {
288 for unit
in B k M G T
; do
289 [ ${value} -lt ${limit} ] && break
290 value
=$
(( ${value} / ${limit} ))
293 echo "${value}${unit}"
296 function module_load
() {
299 if ! grep -q "^${module}" /proc
/modules
; then
300 log DEBUG
"Loading module '${module}'."
305 function binary_exists
() {
308 if [ -n "$(type -p ${binary})" ]; then
315 function process_kill
() {
318 if ! isinteger process
; then
319 process
=$
(pidof
${process})
324 for pid
in ${process}; do
326 [ -d "/proc/${pid}" ] ||
break
337 if [ "${hex:0:2}" != "0x" ]; then
341 printf "%d\n" "${hex}"
344 function network_is_running
() {
345 # Check, if the network service is running.
346 service_is_active network