]>
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 | ||
1848564d MT |
55 | # Print a pretty error message |
56 | function error() { | |
fcbf6823 | 57 | echo -e " ${CLR_RED_B}ERROR${CLR_RESET} : $@" >&2 |
1848564d MT |
58 | } |
59 | ||
1b7a1578 | 60 | function error_log() { |
1b7a1578 MT |
61 | log ERROR "$@" |
62 | } | |
63 | ||
1848564d MT |
64 | # Print a pretty warn message |
65 | function warning() { | |
fcbf6823 | 66 | echo -e " ${CLR_YELLOW_B}WARNING${CLR_RESET}: $@" >&2 |
1848564d MT |
67 | } |
68 | ||
1b7a1578 | 69 | function warning_log() { |
1b7a1578 MT |
70 | log WARNING "$@" |
71 | } | |
72 | ||
e726ef8d MT |
73 | # The next three functions are kept for backwards |
74 | # compatibility. The need to be dropped at some time. | |
1848564d | 75 | function listsort() { |
e726ef8d | 76 | list_sort $@ |
1848564d MT |
77 | } |
78 | ||
711ffac1 | 79 | function listmatch() { |
e726ef8d | 80 | list_match $@ |
711ffac1 MT |
81 | } |
82 | ||
83 | function listlength() { | |
e726ef8d | 84 | list_length $@ |
711ffac1 MT |
85 | } |
86 | ||
1848564d MT |
87 | # Speedup function to avoid a call of the basename binary |
88 | function basename() { | |
89 | echo "${1##*/}" | |
90 | } | |
91 | ||
b79ad79b MT |
92 | function assign() { |
93 | local key=${1} | |
94 | assert isset key | |
95 | shift | |
96 | ||
97 | printf -v "${key}" "%s" "$@" | |
98 | } | |
99 | ||
100 | function fread() { | |
101 | local file=${1} | |
102 | assert isset file | |
103 | ||
104 | [ -r "${file}" ] || return ${EXIT_ERROR} | |
105 | ||
106 | print "$(<${file})" | |
107 | } | |
108 | ||
109 | function fwrite() { | |
110 | local file=${1} | |
111 | assert isset file | |
112 | shift | |
113 | ||
114 | print "%s" "$@" >> ${file} | |
115 | } | |
116 | ||
1848564d MT |
117 | function enabled() { |
118 | local param=${1} | |
119 | ||
e726ef8d | 120 | list_match "${!param}" yes on true 1 |
1848564d MT |
121 | } |
122 | ||
123 | function mac_generate() { | |
790b7ec9 MT |
124 | # Get a bunch of random hex digits |
125 | # and remove all dashes from the input. | |
126 | local random=$(</proc/sys/kernel/random/uuid) | |
127 | random=${random//-/} | |
128 | assert isset random | |
1848564d MT |
129 | |
130 | local output | |
790b7ec9 MT |
131 | |
132 | local i o | |
133 | for i in $(seq 0 5); do | |
134 | o="0x${random:0:2}" | |
135 | random="${random:2:${#random}}" | |
136 | ||
137 | case "${i}" in | |
138 | 0) | |
139 | # Remove multicast bit | |
140 | # and set address is software assigned | |
141 | o=$(( ${o} & 0xfe )) | |
142 | o=$(( ${o} | 0x02 )) | |
143 | ||
144 | printf -v output "%02x" "${o}" | |
145 | ;; | |
146 | *) | |
147 | printf -v output "%s:%02x" "${output}" "${o}" | |
148 | ;; | |
149 | esac | |
1848564d MT |
150 | done |
151 | ||
152 | # Check if output is valid | |
153 | assert mac_is_valid ${output} | |
154 | ||
790b7ec9 | 155 | echo "${output}" |
1848564d MT |
156 | } |
157 | ||
18b43372 MT |
158 | function mac_format() { |
159 | local mac=${1} | |
48bc31eb | 160 | assert isset mac |
18b43372 | 161 | |
48bc31eb MT |
162 | # Remove all colons and make the rest lowercase. |
163 | mac=${mac//:/} | |
164 | mac=${mac,,} | |
18b43372 | 165 | |
48bc31eb | 166 | local output |
18b43372 MT |
167 | if [ "${#mac}" = "12" ]; then |
168 | # Add colons (:) to mac address | |
169 | output=${mac:0:2} | |
170 | local i | |
171 | for i in 2 4 6 8 10; do | |
172 | output="${output}:${mac:${i}:2}" | |
173 | done | |
48bc31eb MT |
174 | else |
175 | output=${mac} | |
18b43372 MT |
176 | fi |
177 | ||
178 | assert mac_is_valid ${output} | |
179 | ||
48bc31eb | 180 | print "${output}" |
18b43372 MT |
181 | } |
182 | ||
1848564d MT |
183 | function mac_is_valid() { |
184 | local mac=${1} | |
185 | ||
186 | [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]] | |
187 | } | |
188 | ||
189 | function uuid() { | |
de543653 | 190 | echo $(</proc/sys/kernel/random/uuid) |
1848564d MT |
191 | } |
192 | ||
193 | function isset() { | |
194 | local var=${1} | |
195 | ||
196 | [ -n "${!var}" ] | |
197 | } | |
198 | ||
199 | function isoneof() { | |
200 | local var=${!1} | |
201 | shift | |
202 | ||
e726ef8d | 203 | list_match "${var}" "$@" |
1848564d MT |
204 | } |
205 | ||
206 | function isbool() { | |
207 | local var=${1} | |
208 | ||
209 | isoneof ${var} 0 1 no yes on off | |
210 | } | |
211 | ||
212 | function isinteger() { | |
213 | local var=${!1} | |
214 | ||
215 | [[ ${var} =~ ^[0-9]+$ ]] | |
216 | } | |
217 | ||
218 | function ismac() { | |
219 | local mac=${!1} | |
220 | ||
221 | mac_is_valid ${mac} | |
222 | } | |
223 | ||
fef4edaf MT |
224 | function isipaddress() { |
225 | local addr=${!1} | |
226 | ||
227 | ip_is_valid ${addr} | |
228 | } | |
229 | ||
711ffac1 MT |
230 | function backtrace() { |
231 | local start=1 | |
232 | ||
233 | echo # Empty line | |
234 | error_log "Backtrace (most recent call in first line):" | |
235 | ||
04608623 | 236 | local i source |
711ffac1 MT |
237 | for i in $(seq ${start} ${#BASH_SOURCE[*]}); do |
238 | [ -z "${FUNCNAME[${i}]}" ] && continue | |
239 | [ "${FUNCNAME[${i}]}" == "main" ] && continue | |
240 | ||
04608623 MT |
241 | source=${BASH_SOURCE[$(( ${i} + 1 ))]} |
242 | error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}" | |
711ffac1 MT |
243 | done |
244 | } | |
245 | ||
1848564d MT |
246 | function assert() { |
247 | local assertion="$@" | |
248 | ||
249 | if ! ${assertion}; then | |
4c670d7c | 250 | error_log "Assertion '${assertion}' failed." |
711ffac1 | 251 | backtrace |
cfbe0802 | 252 | exit ${EXIT_ERROR_ASSERT} |
1848564d MT |
253 | fi |
254 | ||
255 | return ${EXIT_OK} | |
256 | } | |
cad8bd85 | 257 | |
b0b2f995 MT |
258 | # This function checks, if the given argument is an assert error |
259 | # exit code. If this is the case, the script will halt immediately. | |
260 | function assert_check_retval() { | |
261 | local ret=${1} | |
262 | ||
263 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
264 | exit ${EXIT_ERROR_ASSERT} | |
265 | fi | |
266 | ||
267 | return ${ret} | |
268 | } | |
269 | ||
711ffac1 MT |
270 | function exec_cmd() { |
271 | local cmd=$@ | |
272 | ||
273 | log DEBUG "Running command: ${cmd}" | |
274 | ||
b816e04b | 275 | DEBUG=${DEBUG} \ |
8c63fa13 MT |
276 | LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \ |
277 | LOG_FACILITY="${LOG_FACILITY}" \ | |
b816e04b | 278 | ${SHELL} ${cmd} |
711ffac1 MT |
279 | local ret=$? |
280 | ||
281 | #log DEBUG "Returned with code '${ret}'" | |
282 | ||
283 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
284 | error_log "Stopping parent process due to assertion error in child process: ${cmd}" | |
285 | exit ${EXIT_ERROR_ASSERT} | |
286 | fi | |
287 | ||
288 | return ${ret} | |
289 | } | |
290 | ||
b816e04b MT |
291 | function cmd() { |
292 | local cmd=$@ | |
293 | ||
294 | log DEBUG "Running command: ${cmd}" | |
295 | ||
296 | ${cmd} | |
297 | local ret=$? | |
298 | ||
299 | log DEBUG "Returned with code '${ret}'" | |
300 | ||
301 | return ${ret} | |
302 | } | |
303 | ||
98146c00 MT |
304 | function cmd_quiet() { |
305 | cmd $@ &>/dev/null | |
3efecbb3 MT |
306 | } |
307 | ||
f80ce052 MT |
308 | function cmd_exec() { |
309 | local cmd=$@ | |
310 | ||
311 | log DEBUG "Exec'ing command: ${cmd}" | |
312 | ||
313 | exec ${cmd} | |
314 | ||
315 | log ERROR "Could not exec-ute: ${cmd}" | |
316 | exit ${EXIT_ERROR} | |
317 | } | |
318 | ||
3efecbb3 MT |
319 | function seq() { |
320 | if [ $# -eq 2 ]; then | |
321 | eval echo {${1}..${2}} | |
322 | elif [ $# -eq 3 ]; then | |
323 | eval echo {${1}..${3}..${2}} | |
324 | fi | |
325 | } | |
326 | ||
76e6cd51 MT |
327 | function which() { |
328 | type -P $@ | |
329 | } | |
330 | ||
d82cf370 MT |
331 | function beautify_time() { |
332 | local value=${1} | |
333 | ||
334 | local unit | |
335 | local limit | |
336 | for unit in s m h d w; do | |
337 | case "${unit}" in | |
338 | s|m|h) | |
339 | limit=60 | |
340 | ;; | |
341 | d) | |
342 | limit=24 | |
343 | ;; | |
344 | w) | |
345 | limit=7 | |
346 | ;; | |
347 | esac | |
348 | ||
349 | [ ${value} -lt ${limit} ] && break | |
350 | ||
351 | value=$(( ${value} / ${limit} )) | |
352 | done | |
353 | ||
354 | echo "${value}${unit}" | |
355 | } | |
711ffac1 MT |
356 | |
357 | function beautify_bytes() { | |
358 | local value=${1} | |
359 | ||
360 | local unit | |
361 | local limit=1024 | |
362 | for unit in B k M G T; do | |
363 | [ ${value} -lt ${limit} ] && break | |
364 | value=$(( ${value} / ${limit} )) | |
365 | done | |
366 | ||
367 | echo "${value}${unit}" | |
368 | } | |
943e3f7e MT |
369 | |
370 | function module_load() { | |
371 | local module=${1} | |
372 | ||
373 | if ! grep -q "^${module}" /proc/modules; then | |
374 | log DEBUG "Loading module '${module}'." | |
375 | modprobe ${module} | |
376 | fi | |
377 | } | |
6b3f9c85 MT |
378 | |
379 | function binary_exists() { | |
380 | local binary=${1} | |
381 | ||
382 | if [ -n "$(type -p ${binary})" ]; then | |
383 | return ${EXIT_OK} | |
384 | fi | |
385 | ||
386 | return ${EXIT_ERROR} | |
387 | } | |
d76f5107 MT |
388 | |
389 | function process_kill() { | |
390 | local process=${1} | |
391 | ||
392 | if ! isinteger process; then | |
393 | process=$(pidof ${process}) | |
394 | fi | |
395 | ||
396 | local pid | |
397 | local sig | |
398 | for pid in ${process}; do | |
399 | for sig in 15 9; do | |
400 | [ -d "/proc/${pid}" ] || break | |
401 | ||
402 | kill -${sig} ${pid} | |
403 | sleep 1 | |
404 | done | |
405 | done | |
406 | } | |
feb76eaf MT |
407 | |
408 | function dec() { | |
409 | local hex=${1} | |
410 | ||
411 | if [ "${hex:0:2}" != "0x" ]; then | |
412 | hex="0x${hex}" | |
413 | fi | |
414 | ||
415 | printf "%d\n" "${hex}" | |
416 | } | |
3a7fef62 MT |
417 | |
418 | function network_is_running() { | |
419 | # Check, if the network service is running. | |
420 | service_is_active network | |
421 | } | |
f80ce052 MT |
422 | |
423 | function contains_spaces() { | |
424 | local var="$@" | |
425 | ||
426 | # Eliminate spaces. | |
427 | local var2=${var// /} | |
428 | ||
429 | if [ ${#var} -ne ${#var2} ]; then | |
430 | return ${EXIT_TRUE} | |
431 | fi | |
432 | ||
433 | return ${EXIT_FALSE} | |
434 | } |