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
() {
46 done |
sort |
tr "\n" " "
49 function config_read
() {
50 local config_file
=${1}
52 if [ -e "${config_file}" ]; then
58 function config_write
() {
59 local config_file
=${1}
62 # Check if all values to be written are sane
65 log DEBUG
"Writing configuration file ${config_file}."
70 for param
in $
(listsort $@
); do
71 echo "${param}=\"${!param}\"" >> ${config_file}
75 function config_print() {
78 for param in $(listsort $@); do
79 printf "%-16s = %s
\n" "${param}" "${!param}"
83 function config_check() {
84 # If there is a function defined that is called __check
85 # we call that function
86 [ -n "$
(type -t _check
)" ] && _check
89 function network_config_set() {
90 while [ $# -gt 0 ]; do
93 log INFO "Setting configuration option
'${1}'".
97 warning "Invalid parameter given
: ${1}"
103 # Write configuration to disk
107 function network_config_read() {
108 config_read ${CONFIG_FILE}
111 function network_config_write() {
112 config_write ${CONFIG_FILE} ${CONFIG_FILE_PARAMS}
115 function network_config_print() {
116 config_print ${CONFIG_FILE_PARAMS}
119 # Speedup function to avoid a call of the basename binary
120 function basename() {
127 [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ]
130 function mac_generate() {
132 for i in $(seq 0 5); do
134 mac[i]="0x
${mac[i]:0:2}"
137 # Remove multicast bit
138 # and set address is software assigned
139 # XXX must doublecheck if this works
140 mac[0]=$((mac[0] & 0xfe))
141 mac[0]=$((mac[0] | 0x02))
144 for i in ${mac[*]}; do
145 if [ -n "${output}" ]; then
149 output="${output}$
(printf "%02x" ${i})"
152 # Check if output is valid
153 assert mac_is_valid ${output}
158 function mac_format() {
163 if [ "${#mac}" = "12" ]; then
164 # Add colons (:) to mac address
167 for i in 2 4 6 8 10; do
168 output="${output}:${mac:${i}:2}"
172 assert mac_is_valid ${output}
177 function mac_is_valid() {
180 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
184 echo $(</proc/sys/kernel/random/uuid)
198 [ "${var}" = "${i}" ] && return ${EXIT_OK}
207 isoneof
${var} 0 1 no
yes on off
210 function isinteger
() {
213 [[ ${var} =~ ^
[0-9]+$
]]
225 if ! ${assertion}; then
226 error_log
"Assertion '${assertion}' failed."
233 function uppercase
() {
239 function beautify_time
() {
244 for unit
in s m h d w
; do
257 [ ${value} -lt ${limit} ] && break
259 value
=$
(( ${value} / ${limit} ))
262 echo "${value}${unit}"