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 # Print a pretty error message
24 echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL} : $@" >&2
27 function error_log
() {
32 # Print a pretty warn message
34 echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2
37 function warning_log
() {
42 # This function does not exist because we cannot use /usr/bin/sort.
43 # It implements some kind of bubble sort which is generally very slow
44 # but we only have to sort very small data.
52 while [ "${list[*]}" != "${list_prev}" ]; do
53 list_prev
="${list[*]}"
54 for j
in $
(seq 1 ${#list[*]}); do
55 [ ${j} -ge ${#list[*]} ] && continue
57 if [[ "${list[${j}]}" < "${list[${i}]}" ]]; then
59 list
[${i}]="${list[${j}]}"
68 function listmatch() {
76 [ "${match}" = "${i}" ] && return ${EXIT_OK}
82 function listlength
() {
87 length
=$
(( ${length} + 1 ))
93 function config_read
() {
94 local config_file
=${1}
96 if [ -e "${config_file}" ]; then
102 function config_write
() {
103 local config_file
=${1}
106 # Check if all values to be written are sane
109 log DEBUG
"Writing configuration file ${config_file}."
114 for param
in $
(listsort $@
); do
115 echo "${param}=\"${!param}\"" >> ${config_file}
119 function config_print() {
122 for param in $(listsort $@); do
123 printf "%-16s = %s
\n" "${param}" "${!param}"
127 function config_check() {
128 # If there is a function defined that is called __check
129 # we call that function
130 [ -n "$
(type -t _check
)" ] && _check
133 function network_config_set() {
134 while [ $# -gt 0 ]; do
137 log INFO "Setting configuration option
'${1}'".
141 warning "Invalid parameter given
: ${1}"
147 # Write configuration to disk
151 function network_config_read() {
152 config_read ${CONFIG_FILE}
155 function network_config_write() {
156 config_write ${CONFIG_FILE} ${CONFIG_FILE_PARAMS}
159 function network_config_print() {
160 config_print ${CONFIG_FILE_PARAMS}
163 # Speedup function to avoid a call of the basename binary
164 function basename() {
171 [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ]
174 function mac_generate() {
176 for i in $(seq 0 5); do
178 mac[i]="0x
${mac[i]:0:2}"
181 # Remove multicast bit
182 # and set address is software assigned
183 # XXX must doublecheck if this works
184 mac[0]=$((mac[0] & 0xfe))
185 mac[0]=$((mac[0] | 0x02))
188 for i in ${mac[*]}; do
189 if [ -n "${output}" ]; then
193 output="${output}$
(printf "%02x" ${i})"
196 # Check if output is valid
197 assert mac_is_valid ${output}
202 function mac_format() {
207 if [ "${#mac}" = "12" ]; then
208 # Add colons (:) to mac address
211 for i in 2 4 6 8 10; do
212 output="${output}:${mac:${i}:2}"
216 assert mac_is_valid ${output}
221 function mac_is_valid() {
224 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
228 echo $(</proc/sys/kernel/random/uuid)
237 # XXX Nearly same as listmatch
243 [ "${var}" = "${i}" ] && return ${EXIT_OK}
252 isoneof
${var} 0 1 no
yes on off
255 function isinteger
() {
258 [[ ${var} =~ ^
[0-9]+$
]]
267 function backtrace
() {
271 error_log
"Backtrace (most recent call in first line):"
274 for i
in $
(seq ${start} ${#BASH_SOURCE[*]}); do
275 [ -z "${FUNCNAME[${i}]}" ] && continue
276 [ "${FUNCNAME[${i}]}" == "main" ] && continue
278 error_log
" $(printf "%20s
" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
285 if ! ${assertion}; then
286 error_log "Assertion '${assertion}' failed."
294 function exec_cmd() {
297 log DEBUG "Running command: ${cmd}"
302 #log DEBUG "Returned with code '${ret}'"
304 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
305 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
306 exit ${EXIT_ERROR_ASSERT}
312 function uppercase() {
318 function lowercase() {
325 if [ $# -eq 2 ]; then
326 eval echo {${1}..${2}}
327 elif [ $# -eq 3 ]; then
328 eval echo {${1}..${3}..${2}}
332 function beautify_time() {
337 for unit in s m h d w; do
350 [ ${value} -lt ${limit} ] && break
352 value=$(( ${value} / ${limit} ))
355 echo "${value}${unit}"
358 function beautify_bytes() {
363 for unit in B k M G T; do
364 [ ${value} -lt ${limit} ] && break
365 value=$(( ${value} / ${limit} ))
368 echo "${value}${unit}"
371 function module_load() {
374 if ! grep -q "^${module}" /proc/modules; then
375 log DEBUG "Loading module '${module}'."