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