]>
Commit | Line | Data |
---|---|---|
1848564d MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2010 Michael Tremer & Christian Schmidt # | |
6 | # # | |
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. # | |
11 | # # | |
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. # | |
16 | # # | |
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/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
22 | # Print a pretty error message | |
23 | function error() { | |
2ab7f50f | 24 | echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL} : $@" >&2 |
1848564d MT |
25 | } |
26 | ||
1b7a1578 MT |
27 | function error_log() { |
28 | error "$@" | |
29 | log ERROR "$@" | |
30 | } | |
31 | ||
1848564d MT |
32 | # Print a pretty warn message |
33 | function warning() { | |
2ab7f50f | 34 | echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2 |
1848564d MT |
35 | } |
36 | ||
1b7a1578 MT |
37 | function warning_log() { |
38 | warning "$@" | |
39 | log WARNING "$@" | |
40 | } | |
41 | ||
711ffac1 | 42 | # XXX uses tr |
1848564d MT |
43 | function listsort() { |
44 | local i | |
45 | for i in $@; do | |
46 | echo "${i}" | |
47 | done | sort | tr "\n" " " | |
48 | } | |
49 | ||
711ffac1 MT |
50 | function listmatch() { |
51 | local match=${1} | |
52 | shift | |
53 | ||
54 | assert isset match | |
55 | ||
56 | local i | |
57 | for i in $@; do | |
58 | [ "${match}" = "${i}" ] && return ${EXIT_OK} | |
59 | done | |
60 | ||
61 | return ${EXIT_ERROR} | |
62 | } | |
63 | ||
64 | function listlength() { | |
65 | local length=0 | |
66 | ||
67 | local i | |
68 | for i in $@; do | |
69 | length=$(( ${length} + 1 )) | |
70 | done | |
71 | ||
72 | echo "${length}" | |
73 | } | |
74 | ||
1848564d MT |
75 | function config_read() { |
76 | local config_file=${1} | |
77 | ||
78 | if [ -e "${config_file}" ]; then | |
79 | . ${config_file} | |
80 | config_check | |
81 | fi | |
82 | } | |
83 | ||
84 | function config_write() { | |
85 | local config_file=${1} | |
86 | shift | |
87 | ||
88 | # Check if all values to be written are sane | |
89 | config_check | |
90 | ||
1b7a1578 MT |
91 | log DEBUG "Writing configuration file ${config_file}." |
92 | ||
1848564d MT |
93 | > ${config_file} |
94 | ||
95 | local param | |
96 | for param in $(listsort $@); do | |
97 | echo "${param}=\"${!param}\"" >> ${config_file} | |
98 | done | |
99 | } | |
100 | ||
101 | function config_print() { | |
102 | local param | |
103 | ||
104 | for param in $(listsort $@); do | |
105 | printf "%-16s = %s\n" "${param}" "${!param}" | |
106 | done | |
107 | } | |
108 | ||
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 | |
113 | } | |
114 | ||
115 | function network_config_set() { | |
116 | while [ $# -gt 0 ]; do | |
117 | case "${1}" in | |
118 | *=*) | |
1b7a1578 MT |
119 | log INFO "Setting configuration option '${1}'". |
120 | eval ${1} | |
1848564d MT |
121 | ;; |
122 | *) | |
123 | warning "Invalid parameter given: ${1}" | |
124 | ;; | |
125 | esac | |
126 | shift | |
127 | done | |
128 | ||
129 | # Write configuration to disk | |
130 | network_config_write | |
131 | } | |
132 | ||
133 | function network_config_read() { | |
134 | config_read ${CONFIG_FILE} | |
135 | } | |
136 | ||
137 | function network_config_write() { | |
138 | config_write ${CONFIG_FILE} ${CONFIG_FILE_PARAMS} | |
139 | } | |
140 | ||
141 | function network_config_print() { | |
142 | config_print ${CONFIG_FILE_PARAMS} | |
143 | } | |
144 | ||
145 | # Speedup function to avoid a call of the basename binary | |
146 | function basename() { | |
147 | echo "${1##*/}" | |
148 | } | |
149 | ||
150 | function enabled() { | |
151 | local param=${1} | |
152 | ||
153 | [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ] | |
154 | } | |
155 | ||
156 | function mac_generate() { | |
157 | local mac=() | |
158 | for i in $(seq 0 5); do | |
21dbdbb9 MT |
159 | mac[i]="$(uuid)" |
160 | mac[i]="0x${mac[i]:0:2}" | |
1848564d MT |
161 | done |
162 | ||
163 | # Remove multicast bit | |
164 | # and set address is software assigned | |
165 | # XXX must doublecheck if this works | |
166 | mac[0]=$((mac[0] & 0xfe)) | |
167 | mac[0]=$((mac[0] | 0x02)) | |
168 | ||
169 | local output | |
170 | for i in ${mac[*]}; do | |
21dbdbb9 MT |
171 | if [ -n "${output}" ]; then |
172 | output="${output}:" | |
173 | fi | |
1848564d | 174 | |
21dbdbb9 | 175 | output="${output}$(printf "%02x" ${i})" |
1848564d MT |
176 | done |
177 | ||
178 | # Check if output is valid | |
179 | assert mac_is_valid ${output} | |
180 | ||
181 | echo ${output} | |
182 | } | |
183 | ||
18b43372 MT |
184 | function mac_format() { |
185 | local mac=${1} | |
186 | ||
187 | local output | |
188 | ||
189 | if [ "${#mac}" = "12" ]; then | |
190 | # Add colons (:) to mac address | |
191 | output=${mac:0:2} | |
192 | local i | |
193 | for i in 2 4 6 8 10; do | |
194 | output="${output}:${mac:${i}:2}" | |
195 | done | |
196 | fi | |
197 | ||
198 | assert mac_is_valid ${output} | |
199 | ||
200 | echo "${output}" | |
201 | } | |
202 | ||
1848564d MT |
203 | function mac_is_valid() { |
204 | local mac=${1} | |
205 | ||
206 | [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]] | |
207 | } | |
208 | ||
209 | function uuid() { | |
de543653 | 210 | echo $(</proc/sys/kernel/random/uuid) |
1848564d MT |
211 | } |
212 | ||
213 | function isset() { | |
214 | local var=${1} | |
215 | ||
216 | [ -n "${!var}" ] | |
217 | } | |
218 | ||
943e3f7e | 219 | # XXX Nearly same as listmatch |
1848564d MT |
220 | function isoneof() { |
221 | local var=${!1} | |
222 | shift | |
223 | ||
224 | for i in $@; do | |
225 | [ "${var}" = "${i}" ] && return ${EXIT_OK} | |
226 | done | |
227 | ||
228 | return ${EXIT_ERROR} | |
229 | } | |
230 | ||
231 | function isbool() { | |
232 | local var=${1} | |
233 | ||
234 | isoneof ${var} 0 1 no yes on off | |
235 | } | |
236 | ||
237 | function isinteger() { | |
238 | local var=${!1} | |
239 | ||
240 | [[ ${var} =~ ^[0-9]+$ ]] | |
241 | } | |
242 | ||
243 | function ismac() { | |
244 | local mac=${!1} | |
245 | ||
246 | mac_is_valid ${mac} | |
247 | } | |
248 | ||
711ffac1 MT |
249 | function backtrace() { |
250 | local start=1 | |
251 | ||
252 | echo # Empty line | |
253 | error_log "Backtrace (most recent call in first line):" | |
254 | ||
255 | local i | |
256 | for i in $(seq ${start} ${#BASH_SOURCE[*]}); do | |
257 | [ -z "${FUNCNAME[${i}]}" ] && continue | |
258 | [ "${FUNCNAME[${i}]}" == "main" ] && continue | |
259 | ||
260 | error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}" | |
261 | done | |
262 | } | |
263 | ||
1848564d MT |
264 | function assert() { |
265 | local assertion="$@" | |
266 | ||
267 | if ! ${assertion}; then | |
4c670d7c | 268 | error_log "Assertion '${assertion}' failed." |
711ffac1 | 269 | backtrace |
1848564d MT |
270 | exit ${EXIT_ERROR} |
271 | fi | |
272 | ||
273 | return ${EXIT_OK} | |
274 | } | |
cad8bd85 | 275 | |
711ffac1 MT |
276 | function exec_cmd() { |
277 | local cmd=$@ | |
278 | ||
279 | log DEBUG "Running command: ${cmd}" | |
280 | ||
281 | ${SHELL} ${cmd} | |
282 | local ret=$? | |
283 | ||
284 | #log DEBUG "Returned with code '${ret}'" | |
285 | ||
286 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
287 | error_log "Stopping parent process due to assertion error in child process: ${cmd}" | |
288 | exit ${EXIT_ERROR_ASSERT} | |
289 | fi | |
290 | ||
291 | return ${ret} | |
292 | } | |
293 | ||
cad8bd85 MT |
294 | function uppercase() { |
295 | local input | |
296 | read input | |
297 | echo "${input^^}" | |
298 | } | |
d82cf370 | 299 | |
3efecbb3 MT |
300 | function lowercase() { |
301 | local input | |
302 | read input | |
303 | echo "${input,,}" | |
304 | } | |
305 | ||
306 | function seq() { | |
307 | if [ $# -eq 2 ]; then | |
308 | eval echo {${1}..${2}} | |
309 | elif [ $# -eq 3 ]; then | |
310 | eval echo {${1}..${3}..${2}} | |
311 | fi | |
312 | } | |
313 | ||
d82cf370 MT |
314 | function beautify_time() { |
315 | local value=${1} | |
316 | ||
317 | local unit | |
318 | local limit | |
319 | for unit in s m h d w; do | |
320 | case "${unit}" in | |
321 | s|m|h) | |
322 | limit=60 | |
323 | ;; | |
324 | d) | |
325 | limit=24 | |
326 | ;; | |
327 | w) | |
328 | limit=7 | |
329 | ;; | |
330 | esac | |
331 | ||
332 | [ ${value} -lt ${limit} ] && break | |
333 | ||
334 | value=$(( ${value} / ${limit} )) | |
335 | done | |
336 | ||
337 | echo "${value}${unit}" | |
338 | } | |
711ffac1 MT |
339 | |
340 | function beautify_bytes() { | |
341 | local value=${1} | |
342 | ||
343 | local unit | |
344 | local limit=1024 | |
345 | for unit in B k M G T; do | |
346 | [ ${value} -lt ${limit} ] && break | |
347 | value=$(( ${value} / ${limit} )) | |
348 | done | |
349 | ||
350 | echo "${value}${unit}" | |
351 | } | |
943e3f7e MT |
352 | |
353 | function module_load() { | |
354 | local module=${1} | |
355 | ||
356 | if ! grep -q "^${module}" /proc/modules; then | |
357 | log DEBUG "Loading module '${module}'." | |
358 | modprobe ${module} | |
359 | fi | |
360 | } |