]> git.ipfire.org Git - network.git/blame - functions.util
DNS: Add options to configure local DNS servers.
[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
97cb552e
MT
22# A simple print statement
23function print() {
acc9efd5
MT
24 local fmt=${1}; shift
25
26 printf "${fmt}\n" "$@"
97cb552e
MT
27}
28
1848564d
MT
29# Print a pretty error message
30function error() {
2ab7f50f 31 echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL} : $@" >&2
1848564d
MT
32}
33
1b7a1578 34function error_log() {
1b7a1578
MT
35 log ERROR "$@"
36}
37
1848564d
MT
38# Print a pretty warn message
39function warning() {
2ab7f50f 40 echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2
1848564d
MT
41}
42
1b7a1578 43function warning_log() {
1b7a1578
MT
44 log WARNING "$@"
45}
46
1848564d
MT
47function listsort() {
48 local i
ae22de29 49 for i in $@; do
ab29f8c4 50 echo "${i}"
ae22de29 51 done | sort | tr '\n' ' '
ab29f8c4 52 echo
1848564d
MT
53}
54
711ffac1
MT
55function listmatch() {
56 local match=${1}
57 shift
58
59 assert isset match
60
61 local i
62 for i in $@; do
63 [ "${match}" = "${i}" ] && return ${EXIT_OK}
64 done
65
66 return ${EXIT_ERROR}
67}
68
69function listlength() {
70 local length=0
71
72 local i
73 for i in $@; do
74 length=$(( ${length} + 1 ))
75 done
76
77 echo "${length}"
78}
79
1848564d
MT
80# Speedup function to avoid a call of the basename binary
81function basename() {
82 echo "${1##*/}"
83}
84
790b7ec9
MT
85function touch() {
86 local file=${1}
87
88 : > ${file}
89}
90
1848564d
MT
91function enabled() {
92 local param=${1}
93
acc9efd5 94 listmatch "${!param}" yes on true 1
1848564d
MT
95}
96
97function mac_generate() {
790b7ec9
MT
98 # Get a bunch of random hex digits
99 # and remove all dashes from the input.
100 local random=$(</proc/sys/kernel/random/uuid)
101 random=${random//-/}
102 assert isset random
1848564d
MT
103
104 local output
790b7ec9
MT
105
106 local i o
107 for i in $(seq 0 5); do
108 o="0x${random:0:2}"
109 random="${random:2:${#random}}"
110
111 case "${i}" in
112 0)
113 # Remove multicast bit
114 # and set address is software assigned
115 o=$(( ${o} & 0xfe ))
116 o=$(( ${o} | 0x02 ))
117
118 printf -v output "%02x" "${o}"
119 ;;
120 *)
121 printf -v output "%s:%02x" "${output}" "${o}"
122 ;;
123 esac
1848564d
MT
124 done
125
126 # Check if output is valid
127 assert mac_is_valid ${output}
128
790b7ec9 129 echo "${output}"
1848564d
MT
130}
131
18b43372
MT
132function mac_format() {
133 local mac=${1}
134
135 local output
136
137 if [ "${#mac}" = "12" ]; then
138 # Add colons (:) to mac address
139 output=${mac:0:2}
140 local i
141 for i in 2 4 6 8 10; do
142 output="${output}:${mac:${i}:2}"
143 done
144 fi
145
146 assert mac_is_valid ${output}
147
148 echo "${output}"
149}
150
1848564d
MT
151function mac_is_valid() {
152 local mac=${1}
153
154 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
155}
156
157function uuid() {
de543653 158 echo $(</proc/sys/kernel/random/uuid)
1848564d
MT
159}
160
161function isset() {
162 local var=${1}
163
164 [ -n "${!var}" ]
165}
166
943e3f7e 167# XXX Nearly same as listmatch
1848564d
MT
168function isoneof() {
169 local var=${!1}
170 shift
171
172 for i in $@; do
173 [ "${var}" = "${i}" ] && return ${EXIT_OK}
174 done
175
176 return ${EXIT_ERROR}
177}
178
179function isbool() {
180 local var=${1}
181
182 isoneof ${var} 0 1 no yes on off
183}
184
185function isinteger() {
186 local var=${!1}
187
188 [[ ${var} =~ ^[0-9]+$ ]]
189}
190
191function ismac() {
192 local mac=${!1}
193
194 mac_is_valid ${mac}
195}
196
711ffac1
MT
197function backtrace() {
198 local start=1
199
200 echo # Empty line
201 error_log "Backtrace (most recent call in first line):"
202
203 local i
204 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
205 [ -z "${FUNCNAME[${i}]}" ] && continue
206 [ "${FUNCNAME[${i}]}" == "main" ] && continue
207
208 error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
209 done
210}
211
1848564d
MT
212function assert() {
213 local assertion="$@"
214
215 if ! ${assertion}; then
4c670d7c 216 error_log "Assertion '${assertion}' failed."
711ffac1 217 backtrace
1848564d
MT
218 exit ${EXIT_ERROR}
219 fi
220
221 return ${EXIT_OK}
222}
cad8bd85 223
711ffac1
MT
224function exec_cmd() {
225 local cmd=$@
226
227 log DEBUG "Running command: ${cmd}"
228
b816e04b 229 DEBUG=${DEBUG} \
8c63fa13
MT
230 LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \
231 LOG_FACILITY="${LOG_FACILITY}" \
b816e04b 232 ${SHELL} ${cmd}
711ffac1
MT
233 local ret=$?
234
235 #log DEBUG "Returned with code '${ret}'"
236
237 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
238 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
239 exit ${EXIT_ERROR_ASSERT}
240 fi
241
242 return ${ret}
243}
244
b816e04b
MT
245function cmd() {
246 local cmd=$@
247
248 log DEBUG "Running command: ${cmd}"
249
250 ${cmd}
251 local ret=$?
252
253 log DEBUG "Returned with code '${ret}'"
254
255 return ${ret}
256}
257
98146c00
MT
258function cmd_quiet() {
259 cmd $@ &>/dev/null
3efecbb3
MT
260}
261
262function seq() {
263 if [ $# -eq 2 ]; then
264 eval echo {${1}..${2}}
265 elif [ $# -eq 3 ]; then
266 eval echo {${1}..${3}..${2}}
267 fi
268}
269
d82cf370
MT
270function beautify_time() {
271 local value=${1}
272
273 local unit
274 local limit
275 for unit in s m h d w; do
276 case "${unit}" in
277 s|m|h)
278 limit=60
279 ;;
280 d)
281 limit=24
282 ;;
283 w)
284 limit=7
285 ;;
286 esac
287
288 [ ${value} -lt ${limit} ] && break
289
290 value=$(( ${value} / ${limit} ))
291 done
292
293 echo "${value}${unit}"
294}
711ffac1
MT
295
296function beautify_bytes() {
297 local value=${1}
298
299 local unit
300 local limit=1024
301 for unit in B k M G T; do
302 [ ${value} -lt ${limit} ] && break
303 value=$(( ${value} / ${limit} ))
304 done
305
306 echo "${value}${unit}"
307}
943e3f7e
MT
308
309function module_load() {
310 local module=${1}
311
312 if ! grep -q "^${module}" /proc/modules; then
313 log DEBUG "Loading module '${module}'."
314 modprobe ${module}
315 fi
316}
6b3f9c85
MT
317
318function binary_exists() {
319 local binary=${1}
320
321 if [ -n "$(type -p ${binary})" ]; then
322 return ${EXIT_OK}
323 fi
324
325 return ${EXIT_ERROR}
326}
d76f5107
MT
327
328function process_kill() {
329 local process=${1}
330
331 if ! isinteger process; then
332 process=$(pidof ${process})
333 fi
334
335 local pid
336 local sig
337 for pid in ${process}; do
338 for sig in 15 9; do
339 [ -d "/proc/${pid}" ] || break
340
341 kill -${sig} ${pid}
342 sleep 1
343 done
344 done
345}
feb76eaf
MT
346
347function dec() {
348 local hex=${1}
349
350 if [ "${hex:0:2}" != "0x" ]; then
351 hex="0x${hex}"
352 fi
353
354 printf "%d\n" "${hex}"
355}
3a7fef62
MT
356
357function network_is_running() {
358 # Check, if the network service is running.
359 service_is_active network
360}