]>
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 | ||
97cb552e MT |
22 | # A simple print statement |
23 | function print() { | |
acc9efd5 MT |
24 | local fmt=${1}; shift |
25 | ||
40e3553f | 26 | printf -- "${fmt}\n" "$@" |
97cb552e MT |
27 | } |
28 | ||
cb965348 MT |
29 | # The args() function takes a number of arguments like |
30 | # var1="abc d" var2="abc" var3="abcd e" | |
31 | # and splits them into several arguments, devided by newline | |
32 | function args() { | |
33 | echo "$@" | xargs printf "%s\n" | |
34 | } | |
35 | ||
04854c77 MT |
36 | function unquote() { |
37 | local var="$@" | |
38 | ||
39 | if [ "${var:0:1}" = "\"" ]; then | |
40 | var=${var:1} | |
41 | fi | |
42 | ||
43 | local last=$(( ${#var} - 1 )) | |
44 | if [ ${last} -ge 0 ] && [ "${var:${last}:1}" = "\"" ]; then | |
45 | var=${var:0:${last}} | |
46 | fi | |
47 | ||
48 | print "${var}" | |
49 | } | |
50 | ||
51 | function quote() { | |
52 | print "\"%s\"" "$@" | |
53 | } | |
54 | ||
fe52c5e0 MT |
55 | function strip() { |
56 | local value="$@" | |
57 | ||
58 | # remove leading whitespace characters | |
59 | value="${value#"${value%%[![:space:]]*}"}" | |
60 | ||
61 | # remove trailing whitespace characters | |
62 | value="${value%"${value##*[![:space:]]}"}" | |
63 | ||
64 | print "${value}" | |
65 | } | |
66 | ||
1848564d MT |
67 | # Print a pretty error message |
68 | function error() { | |
fcbf6823 | 69 | echo -e " ${CLR_RED_B}ERROR${CLR_RESET} : $@" >&2 |
1848564d MT |
70 | } |
71 | ||
1b7a1578 | 72 | function error_log() { |
1b7a1578 MT |
73 | log ERROR "$@" |
74 | } | |
75 | ||
1848564d MT |
76 | # Print a pretty warn message |
77 | function warning() { | |
fcbf6823 | 78 | echo -e " ${CLR_YELLOW_B}WARNING${CLR_RESET}: $@" >&2 |
1848564d MT |
79 | } |
80 | ||
1b7a1578 | 81 | function warning_log() { |
1b7a1578 MT |
82 | log WARNING "$@" |
83 | } | |
84 | ||
e726ef8d MT |
85 | # The next three functions are kept for backwards |
86 | # compatibility. The need to be dropped at some time. | |
1848564d | 87 | function listsort() { |
e726ef8d | 88 | list_sort $@ |
1848564d MT |
89 | } |
90 | ||
711ffac1 | 91 | function listmatch() { |
e726ef8d | 92 | list_match $@ |
711ffac1 MT |
93 | } |
94 | ||
95 | function listlength() { | |
e726ef8d | 96 | list_length $@ |
711ffac1 MT |
97 | } |
98 | ||
1848564d MT |
99 | # Speedup function to avoid a call of the basename binary |
100 | function basename() { | |
101 | echo "${1##*/}" | |
102 | } | |
103 | ||
e5651e17 MT |
104 | function format() { |
105 | local key=${1} | |
106 | assert isset key | |
107 | ||
108 | local format=${2} | |
109 | assert isset format | |
110 | ||
111 | shift 2 | |
112 | ||
113 | printf -v "${key}" "${format}" "$@" | |
114 | } | |
115 | ||
b79ad79b MT |
116 | function assign() { |
117 | local key=${1} | |
118 | assert isset key | |
119 | shift | |
120 | ||
e5651e17 | 121 | format "${key}" "%s" "$@" |
b79ad79b MT |
122 | } |
123 | ||
124 | function fread() { | |
125 | local file=${1} | |
126 | assert isset file | |
127 | ||
128 | [ -r "${file}" ] || return ${EXIT_ERROR} | |
129 | ||
130 | print "$(<${file})" | |
131 | } | |
132 | ||
133 | function fwrite() { | |
134 | local file=${1} | |
135 | assert isset file | |
136 | shift | |
137 | ||
138 | print "%s" "$@" >> ${file} | |
139 | } | |
140 | ||
1848564d MT |
141 | function enabled() { |
142 | local param=${1} | |
143 | ||
e726ef8d | 144 | list_match "${!param}" yes on true 1 |
1848564d MT |
145 | } |
146 | ||
147 | function mac_generate() { | |
790b7ec9 MT |
148 | # Get a bunch of random hex digits |
149 | # and remove all dashes from the input. | |
150 | local random=$(</proc/sys/kernel/random/uuid) | |
151 | random=${random//-/} | |
152 | assert isset random | |
1848564d MT |
153 | |
154 | local output | |
790b7ec9 MT |
155 | |
156 | local i o | |
157 | for i in $(seq 0 5); do | |
158 | o="0x${random:0:2}" | |
159 | random="${random:2:${#random}}" | |
160 | ||
161 | case "${i}" in | |
162 | 0) | |
163 | # Remove multicast bit | |
164 | # and set address is software assigned | |
165 | o=$(( ${o} & 0xfe )) | |
166 | o=$(( ${o} | 0x02 )) | |
167 | ||
168 | printf -v output "%02x" "${o}" | |
169 | ;; | |
170 | *) | |
171 | printf -v output "%s:%02x" "${output}" "${o}" | |
172 | ;; | |
173 | esac | |
1848564d MT |
174 | done |
175 | ||
176 | # Check if output is valid | |
177 | assert mac_is_valid ${output} | |
178 | ||
790b7ec9 | 179 | echo "${output}" |
1848564d MT |
180 | } |
181 | ||
18b43372 MT |
182 | function mac_format() { |
183 | local mac=${1} | |
48bc31eb | 184 | assert isset mac |
18b43372 | 185 | |
48bc31eb MT |
186 | # Remove all colons and make the rest lowercase. |
187 | mac=${mac//:/} | |
188 | mac=${mac,,} | |
18b43372 | 189 | |
48bc31eb | 190 | local output |
18b43372 MT |
191 | if [ "${#mac}" = "12" ]; then |
192 | # Add colons (:) to mac address | |
193 | output=${mac:0:2} | |
194 | local i | |
195 | for i in 2 4 6 8 10; do | |
196 | output="${output}:${mac:${i}:2}" | |
197 | done | |
48bc31eb MT |
198 | else |
199 | output=${mac} | |
18b43372 MT |
200 | fi |
201 | ||
202 | assert mac_is_valid ${output} | |
203 | ||
48bc31eb | 204 | print "${output}" |
18b43372 MT |
205 | } |
206 | ||
1848564d MT |
207 | function mac_is_valid() { |
208 | local mac=${1} | |
209 | ||
210 | [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]] | |
211 | } | |
212 | ||
213 | function uuid() { | |
de543653 | 214 | echo $(</proc/sys/kernel/random/uuid) |
1848564d MT |
215 | } |
216 | ||
217 | function isset() { | |
218 | local var=${1} | |
219 | ||
220 | [ -n "${!var}" ] | |
221 | } | |
222 | ||
223 | function isoneof() { | |
224 | local var=${!1} | |
225 | shift | |
226 | ||
e726ef8d | 227 | list_match "${var}" "$@" |
1848564d MT |
228 | } |
229 | ||
230 | function isbool() { | |
231 | local var=${1} | |
232 | ||
233 | isoneof ${var} 0 1 no yes on off | |
234 | } | |
235 | ||
236 | function isinteger() { | |
237 | local var=${!1} | |
238 | ||
239 | [[ ${var} =~ ^[0-9]+$ ]] | |
240 | } | |
241 | ||
242 | function ismac() { | |
243 | local mac=${!1} | |
244 | ||
245 | mac_is_valid ${mac} | |
246 | } | |
247 | ||
fef4edaf MT |
248 | function isipaddress() { |
249 | local addr=${!1} | |
250 | ||
251 | ip_is_valid ${addr} | |
252 | } | |
253 | ||
711ffac1 MT |
254 | function backtrace() { |
255 | local start=1 | |
256 | ||
257 | echo # Empty line | |
258 | error_log "Backtrace (most recent call in first line):" | |
259 | ||
04608623 | 260 | local i source |
711ffac1 MT |
261 | for i in $(seq ${start} ${#BASH_SOURCE[*]}); do |
262 | [ -z "${FUNCNAME[${i}]}" ] && continue | |
6396ccab MT |
263 | |
264 | # Print called binary with arguments. | |
265 | if [ "${FUNCNAME[${i}]}" == "main" ]; then | |
266 | local args="$(list_reverse ${BASH_ARGV[*]})" | |
267 | printf -v source "%20s" "$0" | |
268 | error_log " ${source} ${args}" | |
269 | continue | |
270 | fi | |
711ffac1 | 271 | |
04608623 MT |
272 | source=${BASH_SOURCE[$(( ${i} + 1 ))]} |
273 | error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}" | |
711ffac1 MT |
274 | done |
275 | } | |
276 | ||
1848564d MT |
277 | function assert() { |
278 | local assertion="$@" | |
279 | ||
280 | if ! ${assertion}; then | |
4c670d7c | 281 | error_log "Assertion '${assertion}' failed." |
711ffac1 | 282 | backtrace |
cfbe0802 | 283 | exit ${EXIT_ERROR_ASSERT} |
1848564d MT |
284 | fi |
285 | ||
286 | return ${EXIT_OK} | |
287 | } | |
cad8bd85 | 288 | |
b0b2f995 MT |
289 | # This function checks, if the given argument is an assert error |
290 | # exit code. If this is the case, the script will halt immediately. | |
291 | function assert_check_retval() { | |
292 | local ret=${1} | |
293 | ||
294 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
295 | exit ${EXIT_ERROR_ASSERT} | |
296 | fi | |
297 | ||
298 | return ${ret} | |
299 | } | |
300 | ||
711ffac1 MT |
301 | function exec_cmd() { |
302 | local cmd=$@ | |
303 | ||
304 | log DEBUG "Running command: ${cmd}" | |
305 | ||
b816e04b | 306 | DEBUG=${DEBUG} \ |
8c63fa13 MT |
307 | LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \ |
308 | LOG_FACILITY="${LOG_FACILITY}" \ | |
b816e04b | 309 | ${SHELL} ${cmd} |
711ffac1 MT |
310 | local ret=$? |
311 | ||
312 | #log DEBUG "Returned with code '${ret}'" | |
313 | ||
314 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
315 | error_log "Stopping parent process due to assertion error in child process: ${cmd}" | |
316 | exit ${EXIT_ERROR_ASSERT} | |
317 | fi | |
318 | ||
319 | return ${ret} | |
320 | } | |
321 | ||
b816e04b MT |
322 | function cmd() { |
323 | local cmd=$@ | |
324 | ||
325 | log DEBUG "Running command: ${cmd}" | |
326 | ||
327 | ${cmd} | |
328 | local ret=$? | |
329 | ||
330 | log DEBUG "Returned with code '${ret}'" | |
331 | ||
332 | return ${ret} | |
333 | } | |
334 | ||
98146c00 MT |
335 | function cmd_quiet() { |
336 | cmd $@ &>/dev/null | |
3efecbb3 MT |
337 | } |
338 | ||
f80ce052 MT |
339 | function cmd_exec() { |
340 | local cmd=$@ | |
341 | ||
342 | log DEBUG "Exec'ing command: ${cmd}" | |
343 | ||
344 | exec ${cmd} | |
345 | ||
346 | log ERROR "Could not exec-ute: ${cmd}" | |
347 | exit ${EXIT_ERROR} | |
348 | } | |
349 | ||
2181765d MT |
350 | function cmd_not_implemented() { |
351 | assert false "not implemented" | |
352 | } | |
353 | ||
3efecbb3 MT |
354 | function seq() { |
355 | if [ $# -eq 2 ]; then | |
356 | eval echo {${1}..${2}} | |
357 | elif [ $# -eq 3 ]; then | |
358 | eval echo {${1}..${3}..${2}} | |
359 | fi | |
360 | } | |
361 | ||
76e6cd51 MT |
362 | function which() { |
363 | type -P $@ | |
364 | } | |
365 | ||
fe52c5e0 MT |
366 | # Prints the number of seconds since epoch. |
367 | function timestamp() { | |
368 | date -u "+%s" | |
369 | } | |
370 | ||
d82cf370 MT |
371 | function beautify_time() { |
372 | local value=${1} | |
373 | ||
374 | local unit | |
375 | local limit | |
376 | for unit in s m h d w; do | |
377 | case "${unit}" in | |
378 | s|m|h) | |
379 | limit=60 | |
380 | ;; | |
381 | d) | |
382 | limit=24 | |
383 | ;; | |
384 | w) | |
385 | limit=7 | |
386 | ;; | |
387 | esac | |
388 | ||
389 | [ ${value} -lt ${limit} ] && break | |
390 | ||
391 | value=$(( ${value} / ${limit} )) | |
392 | done | |
393 | ||
394 | echo "${value}${unit}" | |
395 | } | |
711ffac1 MT |
396 | |
397 | function beautify_bytes() { | |
398 | local value=${1} | |
399 | ||
400 | local unit | |
401 | local limit=1024 | |
402 | for unit in B k M G T; do | |
403 | [ ${value} -lt ${limit} ] && break | |
404 | value=$(( ${value} / ${limit} )) | |
405 | done | |
406 | ||
407 | echo "${value}${unit}" | |
408 | } | |
943e3f7e MT |
409 | |
410 | function module_load() { | |
411 | local module=${1} | |
412 | ||
413 | if ! grep -q "^${module}" /proc/modules; then | |
414 | log DEBUG "Loading module '${module}'." | |
415 | modprobe ${module} | |
416 | fi | |
417 | } | |
6b3f9c85 MT |
418 | |
419 | function binary_exists() { | |
420 | local binary=${1} | |
421 | ||
422 | if [ -n "$(type -p ${binary})" ]; then | |
423 | return ${EXIT_OK} | |
424 | fi | |
425 | ||
426 | return ${EXIT_ERROR} | |
427 | } | |
d76f5107 MT |
428 | |
429 | function process_kill() { | |
430 | local process=${1} | |
431 | ||
432 | if ! isinteger process; then | |
433 | process=$(pidof ${process}) | |
434 | fi | |
435 | ||
436 | local pid | |
437 | local sig | |
438 | for pid in ${process}; do | |
439 | for sig in 15 9; do | |
440 | [ -d "/proc/${pid}" ] || break | |
441 | ||
442 | kill -${sig} ${pid} | |
443 | sleep 1 | |
444 | done | |
445 | done | |
446 | } | |
feb76eaf MT |
447 | |
448 | function dec() { | |
449 | local hex=${1} | |
450 | ||
451 | if [ "${hex:0:2}" != "0x" ]; then | |
452 | hex="0x${hex}" | |
453 | fi | |
454 | ||
455 | printf "%d\n" "${hex}" | |
456 | } | |
3a7fef62 | 457 | |
5cf0edf9 MT |
458 | function chr() { |
459 | local char="${1}" | |
460 | ||
461 | [ ${char} -lt 256 ] || return ${EXIT_ERROR} | |
462 | ||
463 | printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n" | |
464 | } | |
465 | ||
466 | function ord() { | |
467 | LC_CTYPE="C" printf "%d\n" "'${1}" | |
468 | } | |
469 | ||
470 | function hex() { | |
471 | printf "%X\n" "${1}" | |
472 | } | |
473 | ||
3a7fef62 MT |
474 | function network_is_running() { |
475 | # Check, if the network service is running. | |
476 | service_is_active network | |
477 | } | |
f80ce052 MT |
478 | |
479 | function contains_spaces() { | |
480 | local var="$@" | |
481 | ||
482 | # Eliminate spaces. | |
483 | local var2=${var// /} | |
484 | ||
485 | if [ ${#var} -ne ${#var2} ]; then | |
486 | return ${EXIT_TRUE} | |
487 | fi | |
488 | ||
489 | return ${EXIT_FALSE} | |
490 | } | |
5cf0edf9 MT |
491 | |
492 | function string_split() { | |
493 | local string="$@" | |
494 | ||
495 | local pos=0 | |
496 | while [ ${pos} -lt ${#string} ]; do | |
497 | print "${string:${pos}:1}" | |
498 | pos=$(( ${pos} + 1 )) | |
499 | done | |
500 | ||
501 | return ${EXIT_OK} | |
502 | } | |
503 | ||
504 | function string_reverse() { | |
505 | local string="$@" | |
506 | ||
507 | local output | |
508 | local pos=0 | |
509 | while [ ${pos} -lt ${#string} ]; do | |
510 | output="${string:${pos}:1}${output}" | |
511 | pos=$(( ${pos} + 1 )) | |
512 | done | |
513 | ||
514 | print "${output}" | |
515 | return ${EXIT_OK} | |
516 | } | |
517 | ||
518 | function dec2bin() { | |
519 | local number="${1}" | |
520 | ||
521 | local output | |
522 | ||
523 | local i div | |
524 | for i in 7 6 5 4 3 2 1; do | |
525 | div=$(( 2 ** ${i} )) | |
526 | ||
527 | if [ $(( ${number} / ${div} )) -eq 1 ]; then | |
528 | output="${output}1" | |
529 | else | |
530 | output="${output}0" | |
531 | fi | |
532 | number="$(( ${number} % ${div} ))" | |
533 | done | |
534 | ||
535 | if [ $(( ${number} % 2 )) -eq 1 ]; then | |
536 | output="${output}1" | |
537 | else | |
538 | output="${output}0" | |
539 | fi | |
540 | ||
541 | print "${output}" | |
542 | } | |
543 | ||
544 | function bin2dec() { | |
545 | local string="${1}" | |
546 | local number=0 | |
547 | ||
548 | local pos=0 char | |
549 | while [ ${pos} -lt ${#string} ]; do | |
550 | char="${string:${pos}:1}" | |
551 | pos=$(( ${pos} + 1 )) | |
552 | ||
553 | number=$(( ${number} << 1 )) | |
554 | ||
555 | case "${char}" in | |
556 | 0) ;; | |
557 | 1) | |
558 | number=$(( ${number} + 1 )) | |
559 | ;; | |
560 | *) | |
561 | assert false "Invalid character: ${char}" | |
562 | ;; | |
563 | esac | |
564 | done | |
565 | ||
566 | print "${number}" | |
567 | return ${EXIT_OK} | |
568 | } | |
569 | ||
570 | function char2bin() { | |
571 | local dec="$(ord "${1}")" | |
572 | ||
573 | dec2bin "${dec}" | |
574 | } | |
575 | ||
576 | function bin2char() { | |
577 | local dec="$(bin2dec "$@")" | |
578 | ||
579 | chr "${dec}" | |
580 | } | |
581 | ||
582 | function bin2hex() { | |
583 | local dec="$(bin2dec "$@")" | |
584 | ||
585 | dec2hex "${dec}" | |
586 | } | |
587 | ||
588 | function hex2bin() { | |
589 | local dec="$(hex2dec "$@")" | |
590 | ||
591 | dec2bin "${dec}" | |
592 | } | |
593 | ||
594 | function hex2dec() { | |
595 | local hex="${1}" | |
596 | ||
597 | # Prepend 0x if necessary. | |
598 | [ "${hex:0:2}" = "0x" ] || hex="0x${hex}" | |
599 | ||
600 | printf "%d\n" "${hex}" | |
601 | } | |
602 | ||
603 | function dec2hex() { | |
604 | printf "%02x\n" "${1}" | |
605 | } |