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