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