]>
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 | 27 | function error_log() { |
1b7a1578 MT |
28 | log ERROR "$@" |
29 | } | |
30 | ||
1848564d MT |
31 | # Print a pretty warn message |
32 | function warning() { | |
2ab7f50f | 33 | echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2 |
1848564d MT |
34 | } |
35 | ||
1b7a1578 | 36 | function warning_log() { |
1b7a1578 MT |
37 | log WARNING "$@" |
38 | } | |
39 | ||
940e6f36 MT |
40 | # This function does not exist because we cannot use /usr/bin/sort. |
41 | # It implements some kind of bubble sort which is generally very slow | |
42 | # but we only have to sort very small data. | |
1848564d | 43 | function listsort() { |
940e6f36 MT |
44 | local list=($@) |
45 | local list_prev | |
46 | ||
1848564d | 47 | local i |
940e6f36 MT |
48 | local j |
49 | local var | |
50 | while [ "${list[*]}" != "${list_prev}" ]; do | |
51 | list_prev="${list[*]}" | |
52 | for j in $(seq 1 ${#list[*]}); do | |
53 | [ ${j} -ge ${#list[*]} ] && continue | |
54 | i=$(( ${j} - 1 )) | |
55 | if [[ "${list[${j}]}" < "${list[${i}]}" ]]; then | |
56 | var="${list[${i}]}" | |
57 | list[${i}]="${list[${j}]}" | |
58 | list[${j}]="${var}" | |
59 | fi | |
60 | done | |
61 | done | |
62 | ||
63 | echo "${list[*]}" | |
1848564d MT |
64 | } |
65 | ||
711ffac1 MT |
66 | function listmatch() { |
67 | local match=${1} | |
68 | shift | |
69 | ||
70 | assert isset match | |
71 | ||
72 | local i | |
73 | for i in $@; do | |
74 | [ "${match}" = "${i}" ] && return ${EXIT_OK} | |
75 | done | |
76 | ||
77 | return ${EXIT_ERROR} | |
78 | } | |
79 | ||
80 | function listlength() { | |
81 | local length=0 | |
82 | ||
83 | local i | |
84 | for i in $@; do | |
85 | length=$(( ${length} + 1 )) | |
86 | done | |
87 | ||
88 | echo "${length}" | |
89 | } | |
90 | ||
1848564d MT |
91 | function config_read() { |
92 | local config_file=${1} | |
93 | ||
f6ee6bb1 AF |
94 | log DEBUG "Reading configuration: ${config_file}" |
95 | ||
1848564d MT |
96 | if [ -e "${config_file}" ]; then |
97 | . ${config_file} | |
98 | config_check | |
99 | fi | |
100 | } | |
101 | ||
102 | function config_write() { | |
103 | local config_file=${1} | |
104 | shift | |
105 | ||
106 | # Check if all values to be written are sane | |
107 | config_check | |
108 | ||
1b7a1578 MT |
109 | log DEBUG "Writing configuration file ${config_file}." |
110 | ||
1848564d MT |
111 | > ${config_file} |
112 | ||
113 | local param | |
114 | for param in $(listsort $@); do | |
115 | echo "${param}=\"${!param}\"" >> ${config_file} | |
116 | done | |
117 | } | |
118 | ||
119 | function config_print() { | |
120 | local param | |
121 | ||
122 | for param in $(listsort $@); do | |
123 | printf "%-16s = %s\n" "${param}" "${!param}" | |
124 | done | |
125 | } | |
126 | ||
127 | function config_check() { | |
128 | # If there is a function defined that is called __check | |
129 | # we call that function | |
130 | [ -n "$(type -t _check)" ] && _check | |
131 | } | |
132 | ||
31e59f2b MT |
133 | function config_hostname() { |
134 | local hostname=${1} | |
135 | ||
136 | if [ -n "${hostname}" ]; then | |
137 | echo "${hostname}" > ${CONFIG_HOSTNAME} | |
138 | else | |
139 | echo "$(<${CONFIG_HOSTNAME})" | |
140 | fi | |
141 | } | |
142 | ||
1848564d MT |
143 | function network_config_set() { |
144 | while [ $# -gt 0 ]; do | |
145 | case "${1}" in | |
146 | *=*) | |
1b7a1578 MT |
147 | log INFO "Setting configuration option '${1}'". |
148 | eval ${1} | |
1848564d MT |
149 | ;; |
150 | *) | |
151 | warning "Invalid parameter given: ${1}" | |
152 | ;; | |
153 | esac | |
154 | shift | |
155 | done | |
156 | ||
157 | # Write configuration to disk | |
158 | network_config_write | |
159 | } | |
160 | ||
161 | function network_config_read() { | |
b816e04b MT |
162 | # Save state of DEBUG and restore it later. |
163 | local debug=${DEBUG} | |
164 | ||
1848564d | 165 | config_read ${CONFIG_FILE} |
b816e04b MT |
166 | |
167 | DEBUG=${debug} | |
1848564d MT |
168 | } |
169 | ||
170 | function network_config_write() { | |
171 | config_write ${CONFIG_FILE} ${CONFIG_FILE_PARAMS} | |
172 | } | |
173 | ||
174 | function network_config_print() { | |
175 | config_print ${CONFIG_FILE_PARAMS} | |
176 | } | |
177 | ||
178 | # Speedup function to avoid a call of the basename binary | |
179 | function basename() { | |
180 | echo "${1##*/}" | |
181 | } | |
182 | ||
183 | function enabled() { | |
184 | local param=${1} | |
185 | ||
186 | [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ] | |
187 | } | |
188 | ||
189 | function mac_generate() { | |
190 | local mac=() | |
191 | for i in $(seq 0 5); do | |
21dbdbb9 MT |
192 | mac[i]="$(uuid)" |
193 | mac[i]="0x${mac[i]:0:2}" | |
1848564d MT |
194 | done |
195 | ||
196 | # Remove multicast bit | |
197 | # and set address is software assigned | |
198 | # XXX must doublecheck if this works | |
199 | mac[0]=$((mac[0] & 0xfe)) | |
200 | mac[0]=$((mac[0] | 0x02)) | |
201 | ||
202 | local output | |
203 | for i in ${mac[*]}; do | |
21dbdbb9 MT |
204 | if [ -n "${output}" ]; then |
205 | output="${output}:" | |
206 | fi | |
1848564d | 207 | |
21dbdbb9 | 208 | output="${output}$(printf "%02x" ${i})" |
1848564d MT |
209 | done |
210 | ||
211 | # Check if output is valid | |
212 | assert mac_is_valid ${output} | |
213 | ||
214 | echo ${output} | |
215 | } | |
216 | ||
18b43372 MT |
217 | function mac_format() { |
218 | local mac=${1} | |
219 | ||
220 | local output | |
221 | ||
222 | if [ "${#mac}" = "12" ]; then | |
223 | # Add colons (:) to mac address | |
224 | output=${mac:0:2} | |
225 | local i | |
226 | for i in 2 4 6 8 10; do | |
227 | output="${output}:${mac:${i}:2}" | |
228 | done | |
229 | fi | |
230 | ||
231 | assert mac_is_valid ${output} | |
232 | ||
233 | echo "${output}" | |
234 | } | |
235 | ||
1848564d MT |
236 | function mac_is_valid() { |
237 | local mac=${1} | |
238 | ||
239 | [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]] | |
240 | } | |
241 | ||
242 | function uuid() { | |
de543653 | 243 | echo $(</proc/sys/kernel/random/uuid) |
1848564d MT |
244 | } |
245 | ||
246 | function isset() { | |
247 | local var=${1} | |
248 | ||
249 | [ -n "${!var}" ] | |
250 | } | |
251 | ||
943e3f7e | 252 | # XXX Nearly same as listmatch |
1848564d MT |
253 | function isoneof() { |
254 | local var=${!1} | |
255 | shift | |
256 | ||
257 | for i in $@; do | |
258 | [ "${var}" = "${i}" ] && return ${EXIT_OK} | |
259 | done | |
260 | ||
261 | return ${EXIT_ERROR} | |
262 | } | |
263 | ||
264 | function isbool() { | |
265 | local var=${1} | |
266 | ||
267 | isoneof ${var} 0 1 no yes on off | |
268 | } | |
269 | ||
270 | function isinteger() { | |
271 | local var=${!1} | |
272 | ||
273 | [[ ${var} =~ ^[0-9]+$ ]] | |
274 | } | |
275 | ||
276 | function ismac() { | |
277 | local mac=${!1} | |
278 | ||
279 | mac_is_valid ${mac} | |
280 | } | |
281 | ||
711ffac1 MT |
282 | function backtrace() { |
283 | local start=1 | |
284 | ||
285 | echo # Empty line | |
286 | error_log "Backtrace (most recent call in first line):" | |
287 | ||
288 | local i | |
289 | for i in $(seq ${start} ${#BASH_SOURCE[*]}); do | |
290 | [ -z "${FUNCNAME[${i}]}" ] && continue | |
291 | [ "${FUNCNAME[${i}]}" == "main" ] && continue | |
292 | ||
293 | error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}" | |
294 | done | |
295 | } | |
296 | ||
1848564d MT |
297 | function assert() { |
298 | local assertion="$@" | |
299 | ||
300 | if ! ${assertion}; then | |
4c670d7c | 301 | error_log "Assertion '${assertion}' failed." |
711ffac1 | 302 | backtrace |
1848564d MT |
303 | exit ${EXIT_ERROR} |
304 | fi | |
305 | ||
306 | return ${EXIT_OK} | |
307 | } | |
cad8bd85 | 308 | |
711ffac1 MT |
309 | function exec_cmd() { |
310 | local cmd=$@ | |
311 | ||
312 | log DEBUG "Running command: ${cmd}" | |
313 | ||
b816e04b MT |
314 | DEBUG=${DEBUG} \ |
315 | ${SHELL} ${cmd} | |
711ffac1 MT |
316 | local ret=$? |
317 | ||
318 | #log DEBUG "Returned with code '${ret}'" | |
319 | ||
320 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
321 | error_log "Stopping parent process due to assertion error in child process: ${cmd}" | |
322 | exit ${EXIT_ERROR_ASSERT} | |
323 | fi | |
324 | ||
325 | return ${ret} | |
326 | } | |
327 | ||
b816e04b MT |
328 | function cmd() { |
329 | local cmd=$@ | |
330 | ||
331 | log DEBUG "Running command: ${cmd}" | |
332 | ||
333 | ${cmd} | |
334 | local ret=$? | |
335 | ||
336 | log DEBUG "Returned with code '${ret}'" | |
337 | ||
338 | return ${ret} | |
339 | } | |
340 | ||
cad8bd85 MT |
341 | function uppercase() { |
342 | local input | |
343 | read input | |
344 | echo "${input^^}" | |
345 | } | |
d82cf370 | 346 | |
3efecbb3 MT |
347 | function lowercase() { |
348 | local input | |
349 | read input | |
350 | echo "${input,,}" | |
351 | } | |
352 | ||
353 | function seq() { | |
354 | if [ $# -eq 2 ]; then | |
355 | eval echo {${1}..${2}} | |
356 | elif [ $# -eq 3 ]; then | |
357 | eval echo {${1}..${3}..${2}} | |
358 | fi | |
359 | } | |
360 | ||
d82cf370 MT |
361 | function beautify_time() { |
362 | local value=${1} | |
363 | ||
364 | local unit | |
365 | local limit | |
366 | for unit in s m h d w; do | |
367 | case "${unit}" in | |
368 | s|m|h) | |
369 | limit=60 | |
370 | ;; | |
371 | d) | |
372 | limit=24 | |
373 | ;; | |
374 | w) | |
375 | limit=7 | |
376 | ;; | |
377 | esac | |
378 | ||
379 | [ ${value} -lt ${limit} ] && break | |
380 | ||
381 | value=$(( ${value} / ${limit} )) | |
382 | done | |
383 | ||
384 | echo "${value}${unit}" | |
385 | } | |
711ffac1 MT |
386 | |
387 | function beautify_bytes() { | |
388 | local value=${1} | |
389 | ||
390 | local unit | |
391 | local limit=1024 | |
392 | for unit in B k M G T; do | |
393 | [ ${value} -lt ${limit} ] && break | |
394 | value=$(( ${value} / ${limit} )) | |
395 | done | |
396 | ||
397 | echo "${value}${unit}" | |
398 | } | |
943e3f7e MT |
399 | |
400 | function module_load() { | |
401 | local module=${1} | |
402 | ||
403 | if ! grep -q "^${module}" /proc/modules; then | |
404 | log DEBUG "Loading module '${module}'." | |
405 | modprobe ${module} | |
406 | fi | |
407 | } | |
6b3f9c85 MT |
408 | |
409 | function binary_exists() { | |
410 | local binary=${1} | |
411 | ||
412 | if [ -n "$(type -p ${binary})" ]; then | |
413 | return ${EXIT_OK} | |
414 | fi | |
415 | ||
416 | return ${EXIT_ERROR} | |
417 | } | |
d76f5107 MT |
418 | |
419 | function process_kill() { | |
420 | local process=${1} | |
421 | ||
422 | if ! isinteger process; then | |
423 | process=$(pidof ${process}) | |
424 | fi | |
425 | ||
426 | local pid | |
427 | local sig | |
428 | for pid in ${process}; do | |
429 | for sig in 15 9; do | |
430 | [ -d "/proc/${pid}" ] || break | |
431 | ||
432 | kill -${sig} ${pid} | |
433 | sleep 1 | |
434 | done | |
435 | done | |
436 | } | |
feb76eaf MT |
437 | |
438 | function dec() { | |
439 | local hex=${1} | |
440 | ||
441 | if [ "${hex:0:2}" != "0x" ]; then | |
442 | hex="0x${hex}" | |
443 | fi | |
444 | ||
445 | printf "%d\n" "${hex}" | |
446 | } |