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