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
() {
44 done |
sort |
tr '\n' ' '
48 function listmatch
() {
56 [ "${match}" = "${i}" ] && return ${EXIT_OK}
62 function listlength() {
67 length=$(( ${length} + 1 ))
73 function config_read() {
74 local config_file=${1}
76 log DEBUG "Reading configuration
: ${config_file}"
78 if [ -e "${config_file}" ]; then
84 function config_write() {
85 local config_file=${1}
88 # Check if all values to be written are sane
91 log DEBUG "Writing configuration
file ${config_file}.
"
96 for param in $(listsort $@); do
97 echo "${param}=\"${!param}\"" >> ${config_file}
101 function config_print
() {
104 for param
in $
(listsort $@
); do
105 printf "%-16s = %s\n" "${param}" "${!param}"
109 function config_check
() {
110 # If there is a function defined that is called __check
111 # we call that function
112 [ -n "$(type -t _check)" ] && _check
115 function config_hostname
() {
118 if [ -n "${hostname}" ]; then
119 echo "${hostname}" > ${CONFIG_HOSTNAME}
121 echo "$(<${CONFIG_HOSTNAME})"
125 function network_config_set
() {
126 while [ $# -gt 0 ]; do
129 log INFO
"Setting configuration option '${1}'".
133 warning
"Invalid parameter given: ${1}"
139 # Write configuration to disk
143 function network_config_read
() {
144 # Save state of DEBUG and restore it later.
147 config_read
${CONFIG_FILE}
149 if [ -n "${debug}" ]; then
154 function network_config_write
() {
155 config_write
${CONFIG_FILE} ${CONFIG_FILE_PARAMS}
158 function network_config_print
() {
159 config_print
${CONFIG_FILE_PARAMS}
162 # Speedup function to avoid a call of the basename binary
163 function basename() {
170 [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ]
173 function mac_generate
() {
175 for i
in $
(seq 0 5); do
177 mac
[i
]="0x${mac[i]:0:2}"
180 # Remove multicast bit
181 # and set address is software assigned
182 # XXX must doublecheck if this works
183 mac
[0]=$
((mac
[0] & 0xfe))
184 mac
[0]=$
((mac
[0] |
0x02))
187 for i
in ${mac[*]}; do
188 if [ -n "${output}" ]; then
192 output
="${output}$(printf "%02x
" ${i})"
195 # Check if output is valid
196 assert mac_is_valid
${output}
201 function mac_format
() {
206 if [ "${#mac}" = "12" ]; then
207 # Add colons (:) to mac address
210 for i
in 2 4 6 8 10; do
211 output
="${output}:${mac:${i}:2}"
215 assert mac_is_valid
${output}
220 function mac_is_valid
() {
223 [[ ${mac} =~ ^
([0-9a-f]{2}\
:){5}[0-9a-f]{2}$
]]
227 echo $
(</proc
/sys
/kernel
/random
/uuid
)
236 # XXX Nearly same as listmatch
242 [ "${var}" = "${i}" ] && return ${EXIT_OK}
251 isoneof ${var} 0 1 no yes on off
254 function isinteger() {
257 [[ ${var} =~ ^[0-9]+$ ]]
266 function backtrace() {
270 error_log "Backtrace
(most recent call
in first line
):"
273 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
274 [ -z "${FUNCNAME[${i}]}" ] && continue
275 [ "${FUNCNAME[${i}]}" == "main
" ] && continue
277 error_log " $
(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
284 if ! ${assertion}; then
285 error_log
"Assertion '${assertion}' failed."
293 function exec_cmd
() {
296 log DEBUG
"Running command: ${cmd}"
299 LOG_DISABLE_STDOUT
="${LOG_DISABLE_STDOUT}" \
300 LOG_FACILITY
="${LOG_FACILITY}" \
304 #log DEBUG "Returned with code '${ret}'"
306 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
307 error_log
"Stopping parent process due to assertion error in child process: ${cmd}"
308 exit ${EXIT_ERROR_ASSERT}
317 log DEBUG
"Running command: ${cmd}"
322 log DEBUG
"Returned with code '${ret}'"
327 function uppercase
() {
333 function lowercase
() {
340 if [ $# -eq 2 ]; then
341 eval echo {${1}..
${2}}
342 elif [ $# -eq 3 ]; then
343 eval echo {${1}..${3}..${2}}
347 function beautify_time
() {
352 for unit
in s m h d w
; do
365 [ ${value} -lt ${limit} ] && break
367 value
=$
(( ${value} / ${limit} ))
370 echo "${value}${unit}"
373 function beautify_bytes
() {
378 for unit
in B k M G T
; do
379 [ ${value} -lt ${limit} ] && break
380 value
=$
(( ${value} / ${limit} ))
383 echo "${value}${unit}"
386 function module_load
() {
389 if ! grep -q "^${module}" /proc
/modules
; then
390 log DEBUG
"Loading module '${module}'."
395 function binary_exists
() {
398 if [ -n "$(type -p ${binary})" ]; then
405 function process_kill
() {
408 if ! isinteger process
; then
409 process
=$
(pidof
${process})
414 for pid
in ${process}; do
416 [ -d "/proc/${pid}" ] ||
break
427 if [ "${hex:0:2}" != "0x" ]; then
431 printf "%d\n" "${hex}"
434 function network_is_running
() {
435 # Check, if the network service is running.
436 service_is_active network