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 log DEBUG
"Reading configuration: ${config_file}"
98 if [ -e "${config_file}" ]; then
104 function config_write
() {
105 local config_file
=${1}
108 # Check if all values to be written are sane
111 log DEBUG
"Writing configuration file ${config_file}."
116 for param
in $
(listsort $@
); do
117 echo "${param}=\"${!param}\"" >> ${config_file}
121 function config_print() {
124 for param in $(listsort $@); do
125 printf "%-16s = %s
\n" "${param}" "${!param}"
129 function config_check() {
130 # If there is a function defined that is called __check
131 # we call that function
132 [ -n "$
(type -t _check
)" ] && _check
135 function config_hostname() {
138 if [ -n "${hostname}" ]; then
139 echo "${hostname}" > ${CONFIG_HOSTNAME}
141 echo "$
(<${CONFIG_HOSTNAME})"
145 function network_config_set() {
146 while [ $# -gt 0 ]; do
149 log INFO "Setting configuration option
'${1}'".
153 warning "Invalid parameter given
: ${1}"
159 # Write configuration to disk
163 function network_config_read() {
164 config_read ${CONFIG_FILE}
167 function network_config_write() {
168 config_write ${CONFIG_FILE} ${CONFIG_FILE_PARAMS}
171 function network_config_print() {
172 config_print ${CONFIG_FILE_PARAMS}
175 # Speedup function to avoid a call of the basename binary
176 function basename() {
183 [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ]
186 function mac_generate() {
188 for i in $(seq 0 5); do
190 mac[i]="0x
${mac[i]:0:2}"
193 # Remove multicast bit
194 # and set address is software assigned
195 # XXX must doublecheck if this works
196 mac[0]=$((mac[0] & 0xfe))
197 mac[0]=$((mac[0] | 0x02))
200 for i in ${mac[*]}; do
201 if [ -n "${output}" ]; then
205 output="${output}$
(printf "%02x" ${i})"
208 # Check if output is valid
209 assert mac_is_valid ${output}
214 function mac_format() {
219 if [ "${#mac}" = "12" ]; then
220 # Add colons (:) to mac address
223 for i in 2 4 6 8 10; do
224 output="${output}:${mac:${i}:2}"
228 assert mac_is_valid ${output}
233 function mac_is_valid() {
236 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
240 echo $(</proc/sys/kernel/random/uuid)
249 # XXX Nearly same as listmatch
255 [ "${var}" = "${i}" ] && return ${EXIT_OK}
264 isoneof
${var} 0 1 no
yes on off
267 function isinteger
() {
270 [[ ${var} =~ ^
[0-9]+$
]]
279 function backtrace
() {
283 error_log
"Backtrace (most recent call in first line):"
286 for i
in $
(seq ${start} ${#BASH_SOURCE[*]}); do
287 [ -z "${FUNCNAME[${i}]}" ] && continue
288 [ "${FUNCNAME[${i}]}" == "main" ] && continue
290 error_log
" $(printf "%20s
" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
297 if ! ${assertion}; then
298 error_log "Assertion '${assertion}' failed."
306 function exec_cmd() {
309 log DEBUG "Running command: ${cmd}"
314 #log DEBUG "Returned with code '${ret}'"
316 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
317 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
318 exit ${EXIT_ERROR_ASSERT}
324 function uppercase() {
330 function lowercase() {
337 if [ $# -eq 2 ]; then
338 eval echo {${1}..${2}}
339 elif [ $# -eq 3 ]; then
340 eval echo {${1}..${3}..${2}}
344 function beautify_time() {
349 for unit in s m h d w; do
362 [ ${value} -lt ${limit} ] && break
364 value=$(( ${value} / ${limit} ))
367 echo "${value}${unit}"
370 function beautify_bytes() {
375 for unit in B k M G T; do
376 [ ${value} -lt ${limit} ] && break
377 value=$(( ${value} / ${limit} ))
380 echo "${value}${unit}"
383 function module_load() {
386 if ! grep -q "^${module}" /proc/modules; then
387 log DEBUG "Loading module '${module}'."
392 function binary_exists() {
395 if [ -n "$(type -p ${binary})" ]; then
402 function process_kill() {
405 if ! isinteger process; then
406 process=$(pidof ${process})
411 for pid in ${process}; do
413 [ -d "/proc/${pid}" ] || break
424 if [ "${hex:0:2}" != "0x" ]; then
428 printf "%d\n" "${hex}"