]> git.ipfire.org Git - people/stevee/network.git/blob - functions.util
Don't use connection tracking for loopback traffic.
[people/stevee/network.git] / functions.util
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 # A simple print statement
23 function print() {
24 local fmt=${1}; shift
25
26 printf -- "${fmt}\n" "$@"
27 }
28
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
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
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
67 # Print a pretty error message
68 function error() {
69 echo -e " ${CLR_RED_B}ERROR${CLR_RESET} : $@" >&2
70 }
71
72 function error_log() {
73 log ERROR "$@"
74 }
75
76 # Print a pretty warn message
77 function warning() {
78 echo -e " ${CLR_YELLOW_B}WARNING${CLR_RESET}: $@" >&2
79 }
80
81 function warning_log() {
82 log WARNING "$@"
83 }
84
85 # The next three functions are kept for backwards
86 # compatibility. The need to be dropped at some time.
87 function listsort() {
88 list_sort $@
89 }
90
91 function listmatch() {
92 list_match $@
93 }
94
95 function listlength() {
96 list_length $@
97 }
98
99 # Speedup function to avoid a call of the basename binary
100 function basename() {
101 echo "${1##*/}"
102 }
103
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
116 function assign() {
117 local key=${1}
118 assert isset key
119 shift
120
121 format "${key}" "%s" "$@"
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
141 function enabled() {
142 local param=${1}
143
144 list_match "${!param}" yes on true 1
145 }
146
147 function mac_generate() {
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
153
154 local output
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
174 done
175
176 # Check if output is valid
177 assert mac_is_valid ${output}
178
179 echo "${output}"
180 }
181
182 function mac_format() {
183 local mac=${1}
184 assert isset mac
185
186 # Remove all colons and make the rest lowercase.
187 mac=${mac//:/}
188 mac=${mac,,}
189
190 local output
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
198 else
199 output=${mac}
200 fi
201
202 assert mac_is_valid ${output}
203
204 print "${output}"
205 }
206
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() {
214 echo $(</proc/sys/kernel/random/uuid)
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
227 list_match "${var}" "$@"
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
248 function isipaddress() {
249 local addr=${!1}
250
251 ip_is_valid ${addr}
252 }
253
254 function backtrace() {
255 local start=1
256
257 echo # Empty line
258 error_log "Backtrace (most recent call in first line):"
259
260 local i source
261 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
262 [ -z "${FUNCNAME[${i}]}" ] && continue
263 [ "${FUNCNAME[${i}]}" == "main" ] && continue
264
265 source=${BASH_SOURCE[$(( ${i} + 1 ))]}
266 error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
267 done
268 }
269
270 function assert() {
271 local assertion="$@"
272
273 if ! ${assertion}; then
274 error_log "Assertion '${assertion}' failed."
275 backtrace
276 exit ${EXIT_ERROR_ASSERT}
277 fi
278
279 return ${EXIT_OK}
280 }
281
282 # This function checks, if the given argument is an assert error
283 # exit code. If this is the case, the script will halt immediately.
284 function assert_check_retval() {
285 local ret=${1}
286
287 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
288 exit ${EXIT_ERROR_ASSERT}
289 fi
290
291 return ${ret}
292 }
293
294 function exec_cmd() {
295 local cmd=$@
296
297 log DEBUG "Running command: ${cmd}"
298
299 DEBUG=${DEBUG} \
300 LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \
301 LOG_FACILITY="${LOG_FACILITY}" \
302 ${SHELL} ${cmd}
303 local ret=$?
304
305 #log DEBUG "Returned with code '${ret}'"
306
307 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
308 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
309 exit ${EXIT_ERROR_ASSERT}
310 fi
311
312 return ${ret}
313 }
314
315 function cmd() {
316 local cmd=$@
317
318 log DEBUG "Running command: ${cmd}"
319
320 ${cmd}
321 local ret=$?
322
323 log DEBUG "Returned with code '${ret}'"
324
325 return ${ret}
326 }
327
328 function cmd_quiet() {
329 cmd $@ &>/dev/null
330 }
331
332 function cmd_exec() {
333 local cmd=$@
334
335 log DEBUG "Exec'ing command: ${cmd}"
336
337 exec ${cmd}
338
339 log ERROR "Could not exec-ute: ${cmd}"
340 exit ${EXIT_ERROR}
341 }
342
343 function seq() {
344 if [ $# -eq 2 ]; then
345 eval echo {${1}..${2}}
346 elif [ $# -eq 3 ]; then
347 eval echo {${1}..${3}..${2}}
348 fi
349 }
350
351 function which() {
352 type -P $@
353 }
354
355 # Prints the number of seconds since epoch.
356 function timestamp() {
357 date -u "+%s"
358 }
359
360 function beautify_time() {
361 local value=${1}
362
363 local unit
364 local limit
365 for unit in s m h d w; do
366 case "${unit}" in
367 s|m|h)
368 limit=60
369 ;;
370 d)
371 limit=24
372 ;;
373 w)
374 limit=7
375 ;;
376 esac
377
378 [ ${value} -lt ${limit} ] && break
379
380 value=$(( ${value} / ${limit} ))
381 done
382
383 echo "${value}${unit}"
384 }
385
386 function beautify_bytes() {
387 local value=${1}
388
389 local unit
390 local limit=1024
391 for unit in B k M G T; do
392 [ ${value} -lt ${limit} ] && break
393 value=$(( ${value} / ${limit} ))
394 done
395
396 echo "${value}${unit}"
397 }
398
399 function module_load() {
400 local module=${1}
401
402 if ! grep -q "^${module}" /proc/modules; then
403 log DEBUG "Loading module '${module}'."
404 modprobe ${module}
405 fi
406 }
407
408 function binary_exists() {
409 local binary=${1}
410
411 if [ -n "$(type -p ${binary})" ]; then
412 return ${EXIT_OK}
413 fi
414
415 return ${EXIT_ERROR}
416 }
417
418 function process_kill() {
419 local process=${1}
420
421 if ! isinteger process; then
422 process=$(pidof ${process})
423 fi
424
425 local pid
426 local sig
427 for pid in ${process}; do
428 for sig in 15 9; do
429 [ -d "/proc/${pid}" ] || break
430
431 kill -${sig} ${pid}
432 sleep 1
433 done
434 done
435 }
436
437 function dec() {
438 local hex=${1}
439
440 if [ "${hex:0:2}" != "0x" ]; then
441 hex="0x${hex}"
442 fi
443
444 printf "%d\n" "${hex}"
445 }
446
447 function network_is_running() {
448 # Check, if the network service is running.
449 service_is_active network
450 }
451
452 function contains_spaces() {
453 local var="$@"
454
455 # Eliminate spaces.
456 local var2=${var// /}
457
458 if [ ${#var} -ne ${#var2} ]; then
459 return ${EXIT_TRUE}
460 fi
461
462 return ${EXIT_FALSE}
463 }