]> git.ipfire.org Git - people/ms/network.git/blame - functions.util
Throw the old GREEN/RED/ORANGE schema over board.
[people/ms/network.git] / functions.util
CommitLineData
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
23function error() {
2ab7f50f 24 echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL} : $@" >&2
1848564d
MT
25}
26
1b7a1578 27function error_log() {
1b7a1578
MT
28 log ERROR "$@"
29}
30
1848564d
MT
31# Print a pretty warn message
32function warning() {
2ab7f50f 33 echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2
1848564d
MT
34}
35
1b7a1578 36function 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 43function 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
66function 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
80function 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
91function 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
102function 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
119function config_print() {
120 local param
121
122 for param in $(listsort $@); do
123 printf "%-16s = %s\n" "${param}" "${!param}"
124 done
125}
126
127function 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
133function 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
143function 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
161function 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 166
32b58568
MT
167 if [ -n "${debug}" ]; then
168 DEBUG=${debug}
169 fi
1848564d
MT
170}
171
172function network_config_write() {
173 config_write ${CONFIG_FILE} ${CONFIG_FILE_PARAMS}
174}
175
176function network_config_print() {
177 config_print ${CONFIG_FILE_PARAMS}
178}
179
180# Speedup function to avoid a call of the basename binary
181function basename() {
182 echo "${1##*/}"
183}
184
185function enabled() {
186 local param=${1}
187
188 [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ]
189}
190
191function mac_generate() {
192 local mac=()
193 for i in $(seq 0 5); do
21dbdbb9
MT
194 mac[i]="$(uuid)"
195 mac[i]="0x${mac[i]:0:2}"
1848564d
MT
196 done
197
198 # Remove multicast bit
199 # and set address is software assigned
200 # XXX must doublecheck if this works
201 mac[0]=$((mac[0] & 0xfe))
202 mac[0]=$((mac[0] | 0x02))
203
204 local output
205 for i in ${mac[*]}; do
21dbdbb9
MT
206 if [ -n "${output}" ]; then
207 output="${output}:"
208 fi
1848564d 209
21dbdbb9 210 output="${output}$(printf "%02x" ${i})"
1848564d
MT
211 done
212
213 # Check if output is valid
214 assert mac_is_valid ${output}
215
216 echo ${output}
217}
218
18b43372
MT
219function mac_format() {
220 local mac=${1}
221
222 local output
223
224 if [ "${#mac}" = "12" ]; then
225 # Add colons (:) to mac address
226 output=${mac:0:2}
227 local i
228 for i in 2 4 6 8 10; do
229 output="${output}:${mac:${i}:2}"
230 done
231 fi
232
233 assert mac_is_valid ${output}
234
235 echo "${output}"
236}
237
1848564d
MT
238function mac_is_valid() {
239 local mac=${1}
240
241 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
242}
243
244function uuid() {
de543653 245 echo $(</proc/sys/kernel/random/uuid)
1848564d
MT
246}
247
248function isset() {
249 local var=${1}
250
251 [ -n "${!var}" ]
252}
253
943e3f7e 254# XXX Nearly same as listmatch
1848564d
MT
255function isoneof() {
256 local var=${!1}
257 shift
258
259 for i in $@; do
260 [ "${var}" = "${i}" ] && return ${EXIT_OK}
261 done
262
263 return ${EXIT_ERROR}
264}
265
266function isbool() {
267 local var=${1}
268
269 isoneof ${var} 0 1 no yes on off
270}
271
272function isinteger() {
273 local var=${!1}
274
275 [[ ${var} =~ ^[0-9]+$ ]]
276}
277
278function ismac() {
279 local mac=${!1}
280
281 mac_is_valid ${mac}
282}
283
711ffac1
MT
284function backtrace() {
285 local start=1
286
287 echo # Empty line
288 error_log "Backtrace (most recent call in first line):"
289
290 local i
291 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
292 [ -z "${FUNCNAME[${i}]}" ] && continue
293 [ "${FUNCNAME[${i}]}" == "main" ] && continue
294
295 error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
296 done
297}
298
1848564d
MT
299function assert() {
300 local assertion="$@"
301
302 if ! ${assertion}; then
4c670d7c 303 error_log "Assertion '${assertion}' failed."
711ffac1 304 backtrace
1848564d
MT
305 exit ${EXIT_ERROR}
306 fi
307
308 return ${EXIT_OK}
309}
cad8bd85 310
711ffac1
MT
311function exec_cmd() {
312 local cmd=$@
313
314 log DEBUG "Running command: ${cmd}"
315
b816e04b
MT
316 DEBUG=${DEBUG} \
317 ${SHELL} ${cmd}
711ffac1
MT
318 local ret=$?
319
320 #log DEBUG "Returned with code '${ret}'"
321
322 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
323 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
324 exit ${EXIT_ERROR_ASSERT}
325 fi
326
327 return ${ret}
328}
329
b816e04b
MT
330function cmd() {
331 local cmd=$@
332
333 log DEBUG "Running command: ${cmd}"
334
335 ${cmd}
336 local ret=$?
337
338 log DEBUG "Returned with code '${ret}'"
339
340 return ${ret}
341}
342
cad8bd85
MT
343function uppercase() {
344 local input
345 read input
346 echo "${input^^}"
347}
d82cf370 348
3efecbb3
MT
349function lowercase() {
350 local input
351 read input
352 echo "${input,,}"
353}
354
355function seq() {
356 if [ $# -eq 2 ]; then
357 eval echo {${1}..${2}}
358 elif [ $# -eq 3 ]; then
359 eval echo {${1}..${3}..${2}}
360 fi
361}
362
d82cf370
MT
363function beautify_time() {
364 local value=${1}
365
366 local unit
367 local limit
368 for unit in s m h d w; do
369 case "${unit}" in
370 s|m|h)
371 limit=60
372 ;;
373 d)
374 limit=24
375 ;;
376 w)
377 limit=7
378 ;;
379 esac
380
381 [ ${value} -lt ${limit} ] && break
382
383 value=$(( ${value} / ${limit} ))
384 done
385
386 echo "${value}${unit}"
387}
711ffac1
MT
388
389function beautify_bytes() {
390 local value=${1}
391
392 local unit
393 local limit=1024
394 for unit in B k M G T; do
395 [ ${value} -lt ${limit} ] && break
396 value=$(( ${value} / ${limit} ))
397 done
398
399 echo "${value}${unit}"
400}
943e3f7e
MT
401
402function module_load() {
403 local module=${1}
404
405 if ! grep -q "^${module}" /proc/modules; then
406 log DEBUG "Loading module '${module}'."
407 modprobe ${module}
408 fi
409}
6b3f9c85
MT
410
411function binary_exists() {
412 local binary=${1}
413
414 if [ -n "$(type -p ${binary})" ]; then
415 return ${EXIT_OK}
416 fi
417
418 return ${EXIT_ERROR}
419}
d76f5107
MT
420
421function process_kill() {
422 local process=${1}
423
424 if ! isinteger process; then
425 process=$(pidof ${process})
426 fi
427
428 local pid
429 local sig
430 for pid in ${process}; do
431 for sig in 15 9; do
432 [ -d "/proc/${pid}" ] || break
433
434 kill -${sig} ${pid}
435 sleep 1
436 done
437 done
438}
feb76eaf
MT
439
440function dec() {
441 local hex=${1}
442
443 if [ "${hex:0:2}" != "0x" ]; then
444 hex="0x${hex}"
445 fi
446
447 printf "%d\n" "${hex}"
448}
3a7fef62
MT
449
450function network_is_running() {
451 # Check, if the network service is running.
452 service_is_active network
453}