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