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' ' '
47 function listmatch
() {
55 [ "${match}" = "${i}" ] && return ${EXIT_OK}
61 function listlength() {
66 length=$(( ${length} + 1 ))
72 function config_read() {
73 local config_file=${1}
75 log DEBUG "Reading configuration
: ${config_file}"
77 if [ -e "${config_file}" ]; then
83 function config_write() {
84 local config_file=${1}
87 # Check if all values to be written are sane
90 log DEBUG "Writing configuration
file ${config_file}.
"
95 for param in $(listsort $@); do
96 echo "${param}=\"${!param}\"" >> ${config_file}
100 function config_print
() {
103 for param
in $
(listsort $@
); do
104 printf "%-16s = %s\n" "${param}" "${!param}"
108 function config_check
() {
109 # If there is a function defined that is called __check
110 # we call that function
111 [ -n "$(type -t _check)" ] && _check
114 function config_hostname
() {
117 if [ -n "${hostname}" ]; then
118 echo "${hostname}" > ${CONFIG_HOSTNAME}
120 echo "$(<${CONFIG_HOSTNAME})"
124 function network_config_set
() {
125 while [ $# -gt 0 ]; do
128 log INFO
"Setting configuration option '${1}'".
132 warning
"Invalid parameter given: ${1}"
138 # Write configuration to disk
142 function network_config_read
() {
143 # Save state of DEBUG and restore it later.
146 config_read
${CONFIG_FILE}
148 if [ -n "${debug}" ]; then
153 function network_config_write
() {
154 config_write
${CONFIG_FILE} ${CONFIG_FILE_PARAMS}
157 function network_config_print
() {
158 config_print
${CONFIG_FILE_PARAMS}
161 # Speedup function to avoid a call of the basename binary
162 function basename() {
169 [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ]
172 function mac_generate
() {
174 for i
in $
(seq 0 5); do
176 mac
[i
]="0x${mac[i]:0:2}"
179 # Remove multicast bit
180 # and set address is software assigned
181 # XXX must doublecheck if this works
182 mac
[0]=$
((mac
[0] & 0xfe))
183 mac
[0]=$
((mac
[0] |
0x02))
186 for i
in ${mac[*]}; do
187 if [ -n "${output}" ]; then
191 output
="${output}$(printf "%02x
" ${i})"
194 # Check if output is valid
195 assert mac_is_valid
${output}
200 function mac_format
() {
205 if [ "${#mac}" = "12" ]; then
206 # Add colons (:) to mac address
209 for i
in 2 4 6 8 10; do
210 output
="${output}:${mac:${i}:2}"
214 assert mac_is_valid
${output}
219 function mac_is_valid
() {
222 [[ ${mac} =~ ^
([0-9a-f]{2}\
:){5}[0-9a-f]{2}$
]]
226 echo $
(</proc
/sys
/kernel
/random
/uuid
)
235 # XXX Nearly same as listmatch
241 [ "${var}" = "${i}" ] && return ${EXIT_OK}
250 isoneof ${var} 0 1 no yes on off
253 function isinteger() {
256 [[ ${var} =~ ^[0-9]+$ ]]
265 function backtrace() {
269 error_log "Backtrace
(most recent call
in first line
):"
272 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
273 [ -z "${FUNCNAME[${i}]}" ] && continue
274 [ "${FUNCNAME[${i}]}" == "main
" ] && continue
276 error_log " $
(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
283 if ! ${assertion}; then
284 error_log
"Assertion '${assertion}' failed."
292 function exec_cmd
() {
295 log DEBUG
"Running command: ${cmd}"
298 LOG_DISABLE_STDOUT
="${LOG_DISABLE_STDOUT}" \
299 LOG_FACILITY
="${LOG_FACILITY}" \
303 #log DEBUG "Returned with code '${ret}'"
305 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
306 error_log
"Stopping parent process due to assertion error in child process: ${cmd}"
307 exit ${EXIT_ERROR_ASSERT}
316 log DEBUG
"Running command: ${cmd}"
321 log DEBUG
"Returned with code '${ret}'"
326 function uppercase
() {
332 function lowercase
() {
339 if [ $# -eq 2 ]; then
340 eval echo {${1}..
${2}}
341 elif [ $# -eq 3 ]; then
342 eval echo {${1}..${3}..${2}}
346 function beautify_time
() {
351 for unit
in s m h d w
; do
364 [ ${value} -lt ${limit} ] && break
366 value
=$
(( ${value} / ${limit} ))
369 echo "${value}${unit}"
372 function beautify_bytes
() {
377 for unit
in B k M G T
; do
378 [ ${value} -lt ${limit} ] && break
379 value
=$
(( ${value} / ${limit} ))
382 echo "${value}${unit}"
385 function module_load
() {
388 if ! grep -q "^${module}" /proc
/modules
; then
389 log DEBUG
"Loading module '${module}'."
394 function binary_exists
() {
397 if [ -n "$(type -p ${binary})" ]; then
404 function process_kill
() {
407 if ! isinteger process
; then
408 process
=$
(pidof
${process})
413 for pid
in ${process}; do
415 [ -d "/proc/${pid}" ] ||
break
426 if [ "${hex:0:2}" != "0x" ]; then
430 printf "%d\n" "${hex}"
433 function network_is_running
() {
434 # Check, if the network service is running.
435 service_is_active network