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