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
() {
31 # Print a pretty warn message
33 echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2
36 function warning_log
() {
40 # This function does not exist because we cannot use /usr/bin/sort.
41 # It implements some kind of bubble sort which is generally very slow
42 # but we only have to sort very small data.
50 while [ "${list[*]}" != "${list_prev}" ]; do
51 list_prev
="${list[*]}"
52 for j
in $
(seq 1 ${#list[*]}); do
53 [ ${j} -ge ${#list[*]} ] && continue
55 if [[ "${list[${j}]}" < "${list[${i}]}" ]]; then
57 list
[${i}]="${list[${j}]}"
66 function listmatch() {
74 [ "${match}" = "${i}" ] && return ${EXIT_OK}
80 function listlength
() {
85 length
=$
(( ${length} + 1 ))
91 function config_read
() {
92 local config_file
=${1}
94 log DEBUG
"Reading configuration: ${config_file}"
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 config_hostname() {
136 if [ -n "${hostname}" ]; then
137 echo "${hostname}" > ${CONFIG_HOSTNAME}
139 echo "$
(<${CONFIG_HOSTNAME})"
143 function network_config_set() {
144 while [ $# -gt 0 ]; do
147 log INFO "Setting configuration option
'${1}'".
151 warning "Invalid parameter given
: ${1}"
157 # Write configuration to disk
161 function network_config_read() {
162 # Save state of DEBUG and restore it later.
165 config_read ${CONFIG_FILE}
167 if [ -n "${debug}" ]; then
172 function network_config_write() {
173 config_write ${CONFIG_FILE} ${CONFIG_FILE_PARAMS}
176 function network_config_print() {
177 config_print ${CONFIG_FILE_PARAMS}
180 # Speedup function to avoid a call of the basename binary
181 function basename() {
188 [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ]
191 function mac_generate() {
193 for i in $(seq 0 5); do
195 mac[i]="0x
${mac[i]:0:2}"
198 # Remove multicast bit
199 # and set address is software assigned
200 # XXX must doublecheck if this works
201 mac[0]=$((mac[0] & 0xfe))
202 mac[0]=$((mac[0] | 0x02))
205 for i in ${mac[*]}; do
206 if [ -n "${output}" ]; then
210 output="${output}$
(printf "%02x" ${i})"
213 # Check if output is valid
214 assert mac_is_valid ${output}
219 function mac_format() {
224 if [ "${#mac}" = "12" ]; then
225 # Add colons (:) to mac address
228 for i in 2 4 6 8 10; do
229 output="${output}:${mac:${i}:2}"
233 assert mac_is_valid ${output}
238 function mac_is_valid() {
241 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
245 echo $(</proc/sys/kernel/random/uuid)
254 # XXX Nearly same as listmatch
260 [ "${var}" = "${i}" ] && return ${EXIT_OK}
269 isoneof
${var} 0 1 no
yes on off
272 function isinteger
() {
275 [[ ${var} =~ ^
[0-9]+$
]]
284 function backtrace
() {
288 error_log
"Backtrace (most recent call in first line):"
291 for i
in $
(seq ${start} ${#BASH_SOURCE[*]}); do
292 [ -z "${FUNCNAME[${i}]}" ] && continue
293 [ "${FUNCNAME[${i}]}" == "main" ] && continue
295 error_log
" $(printf "%20s
" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
302 if ! ${assertion}; then
303 error_log "Assertion '${assertion}' failed."
311 function exec_cmd() {
314 log DEBUG "Running command: ${cmd}"
320 #log DEBUG "Returned with code '${ret}'"
322 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
323 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
324 exit ${EXIT_ERROR_ASSERT}
333 log DEBUG "Running command: ${cmd}"
338 log DEBUG "Returned with code '${ret}'"
343 function uppercase() {
349 function lowercase() {
356 if [ $# -eq 2 ]; then
357 eval echo {${1}..${2}}
358 elif [ $# -eq 3 ]; then
359 eval echo {${1}..${3}..${2}}
363 function beautify_time() {
368 for unit in s m h d w; do
381 [ ${value} -lt ${limit} ] && break
383 value=$(( ${value} / ${limit} ))
386 echo "${value}${unit}"
389 function beautify_bytes() {
394 for unit in B k M G T; do
395 [ ${value} -lt ${limit} ] && break
396 value=$(( ${value} / ${limit} ))
399 echo "${value}${unit}"
402 function module_load() {
405 if ! grep -q "^${module}" /proc/modules; then
406 log DEBUG "Loading module '${module}'."
411 function binary_exists() {
414 if [ -n "$(type -p ${binary})" ]; then
421 function process_kill() {
424 if ! isinteger process; then
425 process=$(pidof ${process})
430 for pid in ${process}; do
432 [ -d "/proc/${pid}" ] || break
443 if [ "${hex:0:2}" != "0x" ]; then
447 printf "%d\n" "${hex}"
450 function network_is_running() {
451 # Check, if the network service is running.
452 service_is_active network