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