Contents of /mcore-src/trunk/mcore-tools/src/include/common.global.class.in
Parent Directory | Revision Log
Revision 2818 -
(show annotations)
(download)
Wed Apr 12 13:22:19 2017 UTC (7 years ago) by niro
File size: 7956 byte(s)
Wed Apr 12 13:22:19 2017 UTC (7 years ago) by niro
File size: 7956 byte(s)
-added function get_iface_ip()
1 | # $Id$ |
2 | |
3 | # message only echo | disabled in quiet mode |
4 | mecho() |
5 | { |
6 | local COLCYAN="\033[1;36m" |
7 | local COLDEFAULT="\033[0m" |
8 | local opts |
9 | local webcrlf |
10 | |
11 | # print nothing if quiet mode was requested |
12 | [[ ${QUIET} = 1 ]] && return |
13 | |
14 | if [[ ${NOCOLORS} = 1 ]] |
15 | then |
16 | COLCYAN="" |
17 | COLDEFAULT="" |
18 | fi |
19 | |
20 | [[ ${WEBCRLF} = 1 ]] && webcrlf="<br>" |
21 | |
22 | # respect -n |
23 | case $1 in |
24 | -n) shift; opts="n" ;; |
25 | esac |
26 | |
27 | echo -e${opts} "${COLCYAN}$@${COLDEFAULT}${webcrlf}" |
28 | } |
29 | |
30 | # prints error messages | enabled even in quiet mode |
31 | eecho() |
32 | { |
33 | local COLRED="\033[1;31m" |
34 | local COLDEFAULT="\033[0m" |
35 | local opts |
36 | local webcrlf |
37 | |
38 | if [[ ${NOCOLORS} = 1 ]] |
39 | then |
40 | COLRED="" |
41 | COLDEFAULT="" |
42 | fi |
43 | |
44 | [[ ${WEBCRLF} = 1 ]] && webcrlf="<br>" |
45 | |
46 | # respect -n |
47 | case $1 in |
48 | -n) shift; opts="n" ;; |
49 | esac |
50 | |
51 | echo -e${opts} "${COLRED}$@${COLDEFAULT}${webcrlf}" |
52 | } |
53 | |
54 | # prints return values of get | enabled even in quiet mode |
55 | rvecho() |
56 | { |
57 | local COLPURPLE="\033[1;35m" |
58 | local COLDEFAULT="\033[0m" |
59 | local opts |
60 | local webcrlf |
61 | |
62 | if [[ ${NOCOLORS} = 1 ]] |
63 | then |
64 | COLPURPLE="" |
65 | COLDEFAULT="" |
66 | fi |
67 | |
68 | [[ ${WEBCRLF} = 1 ]] && webcrlf="<br>" |
69 | |
70 | # respect -n |
71 | case $1 in |
72 | -n) shift; opts="n" ;; |
73 | esac |
74 | |
75 | echo -e${opts} "${COLPURPLE}$@${COLDEFAULT}${webcrlf}" |
76 | } |
77 | |
78 | # prints debug messages if requested | enabled even in quiet mode |
79 | decho() |
80 | { |
81 | # print nothing if debug mode was *not* requested |
82 | [[ ${DEBUG} != 1 ]] && return |
83 | |
84 | eecho "DEBUG: ${@}" |
85 | } |
86 | |
87 | # source a file with debug information |
88 | include() |
89 | { |
90 | local retval |
91 | |
92 | if [ -f $@ ] |
93 | then |
94 | decho "including '$@'" |
95 | source $@ |
96 | retval=$? |
97 | else |
98 | decho "include: '$@' not found" |
99 | retval=1 |
100 | fi |
101 | |
102 | return ${retval} |
103 | } |
104 | |
105 | # adds a line to a configuration file defined by the $CONFIG variable |
106 | # $CONFIG="/etc/conf.d/mcore" addconfig 'LIBDIR="/usr/lib"' |
107 | addconfig() |
108 | { |
109 | local opts |
110 | |
111 | if [[ -z ${CONFIG} ]] |
112 | then |
113 | eecho "You must define \$CONFIG varibale first!" |
114 | return 1 |
115 | fi |
116 | |
117 | if [[ ! -d $(dirname ${CONFIG}) ]] |
118 | then |
119 | install -d $(dirname ${CONFIG}) |
120 | fi |
121 | |
122 | # check for opts |
123 | case $1 in |
124 | -n) shift; opts=" -n" ;; |
125 | -e) shift; opts=" -e" ;; |
126 | esac |
127 | |
128 | echo ${opts} "$@" >> ${CONFIG} |
129 | } |
130 | |
131 | # creates or clears a configuration file defined by the $CONFIG variable |
132 | # CONFIG="/etc/conf.d/mcore" clearconfig |
133 | clearconfig() |
134 | { |
135 | if [[ -z ${CONFIG} ]] |
136 | then |
137 | eecho "You must define \$CONFIG varibale first!" |
138 | return 1 |
139 | fi |
140 | |
141 | if [[ ! -d $(dirname ${CONFIG}) ]] |
142 | then |
143 | install -d $(dirname ${CONFIG}) |
144 | fi |
145 | : > ${CONFIG} |
146 | } |
147 | |
148 | # root is not allowed to run progs in a user session with newer xorg-servers |
149 | # this wrapper runs a command in the xsession of the unpriv_user |
150 | x11runas() |
151 | { |
152 | if [[ -n $(pidof X) ]] || [[ -n $(pidof Xorg) ]] || [[ -n $(pidof Xorg.bin) ]] |
153 | then |
154 | su - "${MCORE_UNPRIV_USER}" -c "DISPLAY=${MCORE_XORG_DISPLAY} $@" |
155 | else |
156 | decho "x11runas(): No running X, Xorg or Xorg.bin process found" |
157 | fi |
158 | } |
159 | |
160 | # no_duplicate $list $item |
161 | no_duplicate() |
162 | { |
163 | local i |
164 | local list="$1" |
165 | local item="$2" |
166 | |
167 | for i in ${list} |
168 | do |
169 | [[ ${i} = ${item} ]] && return 1 |
170 | done |
171 | |
172 | return 0 |
173 | } |
174 | |
175 | # checks if given path is empty |
176 | path_not_empty() |
177 | { |
178 | local path="$1" |
179 | [[ -z ${path} ]] && eecho "path_not_empty(): no path given!" && return 1 |
180 | |
181 | # return ERR if path does not exist |
182 | [[ ! -d ${path} ]] && return 1 |
183 | # return ERR if path empty |
184 | [[ -z $(find "${path}" -mindepth 1 -maxdepth 1) ]] && return 1 |
185 | |
186 | # every thing went ok, directory not empty |
187 | return 0 |
188 | } |
189 | |
190 | # list all files in a given directory |
191 | list_files_in_directory() |
192 | { |
193 | local i |
194 | local retval |
195 | local path |
196 | local opts |
197 | local type |
198 | |
199 | # basic getops |
200 | for i in $* |
201 | do |
202 | case $1 in |
203 | -mindepth) shift; opts+=" -mindepth $1" ;; |
204 | -maxdepth) shift; opts+=" -maxdepth $1" ;; |
205 | -type) shift; type="$1" ;; |
206 | -name) shift; opts+=" -name $1" ;; |
207 | '') continue ;; |
208 | *) path="$1" ;; |
209 | esac |
210 | shift |
211 | done |
212 | |
213 | if [[ -z ${path} ]] |
214 | then |
215 | eecho "No path given." |
216 | return 1 |
217 | fi |
218 | |
219 | if [[ ! -d ${path} ]] |
220 | then |
221 | eecho "Directory '${path}' does not exist." |
222 | return 1 |
223 | fi |
224 | |
225 | # default to files |
226 | [[ -z ${type} ]] && type=f |
227 | |
228 | for i in $(find ${path} ${opts} -type ${type} -printf '%f\n' | sort) |
229 | do |
230 | if [[ -z ${retval} ]] |
231 | then |
232 | retval="${i}" |
233 | else |
234 | retval+=" ${i}" |
235 | fi |
236 | done |
237 | |
238 | rvecho "${retval}" |
239 | } |
240 | |
241 | # runs a command in the chroot of $MROOT |
242 | system_chroot() |
243 | { |
244 | local cmd="$@" |
245 | if [[ -z ${MROOT} ]] |
246 | then |
247 | echo "system_chroot(): \$MROOT was not set, doing nothing!" |
248 | return 1 |
249 | fi |
250 | if [ ! -d ${MROOT} ] |
251 | then |
252 | eecho "system_chroot(): MROOT='${MROOT}' does not exist." |
253 | return 1 |
254 | fi |
255 | |
256 | chroot ${MROOT} ${cmd} |
257 | } |
258 | |
259 | # gets ip for given interface |
260 | get_iface_ip() |
261 | { |
262 | local iface="$1" ip="" |
263 | ip=$(ip -o -f inet addr show $iface) |
264 | ip=${ip%%/*} |
265 | ip=${ip##* } |
266 | echo $ip |
267 | } |
268 | |
269 | # gets interface used to reach given ip |
270 | iface_for_remote_addr() |
271 | { |
272 | set -- $(ip -o route get to $1) |
273 | # honor routes with and without a gateway |
274 | case $@ in |
275 | *via*) echo $5 ;; |
276 | *) echo $3 ;; |
277 | esac |
278 | } |
279 | |
280 | validate_ip_addr() |
281 | { |
282 | local ip="$1" |
283 | local retval=1 |
284 | local _ifs |
285 | |
286 | if [[ ${ip} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] |
287 | then |
288 | _ifs=$IFS |
289 | IFS='.' |
290 | # convert to an array |
291 | ip=($ip) |
292 | IFS=$_ifs |
293 | |
294 | if [[ ${ip[0]} -le 255 ]] && |
295 | [[ ${ip[1]} -le 255 ]] && |
296 | [[ ${ip[2]} -le 255 ]] && |
297 | [[ ${ip[3]} -le 255 ]] |
298 | then |
299 | retval=$? |
300 | fi |
301 | fi |
302 | |
303 | return ${retval} |
304 | } |
305 | |
306 | # get ip from dns name |
307 | dns_to_ip() |
308 | { |
309 | if ! validate_ip_addr $1 |
310 | then |
311 | set -- $(getent hosts $1) |
312 | fi |
313 | echo $1 |
314 | } |
315 | |
316 | iface_for_ip() |
317 | { |
318 | set -- $(ip -o addr show to $1) |
319 | echo $2 |
320 | } |
321 | |
322 | iface_for_mac() |
323 | { |
324 | local interface="" mac="$(echo $1 | sed 'y/ABCDEF/abcdef/')" |
325 | for interface in /sys/class/net/*; do |
326 | if [ $(cat $interface/address) = "$mac" ]; then |
327 | echo ${interface##*/} |
328 | fi |
329 | done |
330 | } |
331 | |
332 | mac_for_iface() |
333 | { |
334 | local iface="$1" |
335 | if [ -f /sys/class/net/${iface}/address ] |
336 | then |
337 | cat /sys/class/net/${iface}/address |
338 | fi |
339 | } |
340 | |
341 | certificate_fingerprint() |
342 | { |
343 | local cert_fingerprint |
344 | local retval |
345 | |
346 | if [[ ! -f ${MCORE_CERT_FILE} ]] |
347 | then |
348 | eecho "MCORE_CERT_FILE '${MCORE_CERT_FILE}' does not exist." |
349 | return 1 |
350 | fi |
351 | |
352 | cert_fingerprint=$(openssl x509 -noout -modulus -in "${MCORE_CERT_FILE}" | openssl sha1 | sed 's:(stdin)=\ ::') |
353 | retval="$?" |
354 | |
355 | if [[ ${retval} != 0 ]] |
356 | then |
357 | eecho "Error '${retval}' while generating cert_fingerprint." |
358 | return 1 |
359 | fi |
360 | |
361 | if [[ -z ${cert_fingerprint} ]] |
362 | then |
363 | eecho "Error: cert_fingerprint is empty" |
364 | return 1 |
365 | else |
366 | echo "${cert_fingerprint}" |
367 | fi |
368 | } |
369 | |
370 | key_fingerprint() |
371 | { |
372 | local key_fingerprint |
373 | local retval |
374 | |
375 | if [[ ! -f ${MCORE_KEY_FILE} ]] |
376 | then |
377 | eecho "MCORE_KEY_FILE '${MCORE_KEY_FILE}' does not exist." |
378 | return 1 |
379 | fi |
380 | |
381 | key_fingerprint=$(openssl rsa -noout -modulus -in "${MCORE_KEY_FILE}" | openssl sha1 | sed 's:(stdin)=\ ::') |
382 | retval="$?" |
383 | |
384 | if [[ ${retval} != 0 ]] |
385 | then |
386 | eecho "Error '${retval}' while generating key_fingerprint." |
387 | return 1 |
388 | fi |
389 | |
390 | if [[ -z ${key_fingerprint} ]] |
391 | then |
392 | eecho "Error: key_fingerprint is empty" |
393 | return 1 |
394 | else |
395 | echo "${key_fingerprint}" |
396 | fi |
397 | } |
398 | |
399 | nsslsay() |
400 | { |
401 | nssl "${SSLSAY_IP}" "${SSLSAY_PORT}" << EOF |
402 | auth ${SSLSAY_USER} ${SSLSAY_PASS} |
403 | $@ |
404 | quit |
405 | EOF |
406 | } |
407 | |
408 | nsslsay_fingerprint() |
409 | { |
410 | nssl "${SSLSAY_IP}" "${SSLSAY_PORT}" << EOF |
411 | certauth $(certificate_fingerprint) |
412 | $@ |
413 | quit |
414 | EOF |
415 | } |
416 | |
417 | nsslsay_queue_init() |
418 | { |
419 | SSLSAY_QUEUE=() |
420 | } |
421 | |
422 | nsslsay_queue_add() |
423 | { |
424 | SSLSAY_QUEUE+=( "$@" ) |
425 | } |
426 | |
427 | nsslsay_queue_print() |
428 | { |
429 | local count |
430 | local i |
431 | |
432 | count="${#SSLSAY_QUEUE[*]}" |
433 | for ((i=0; i < count; i++)) |
434 | do |
435 | echo "${SSLSAY_QUEUE[${i}]}" |
436 | done |
437 | } |
438 | |
439 | nsslsay_queue_run() |
440 | { |
441 | nsslsay "$(nsslsay_queue_print)" |
442 | } |
443 | |
444 | nsslsay_queue_run_fingerprint() |
445 | { |
446 | nsslsay_fingerprint "$(nsslsay_queue_print)" |
447 | } |
448 | |
449 | # read_cmdline "$variable" |
450 | # eg: read_cmdline "lang=" |
451 | # returns the value of the cmdline variable lang |
452 | # eg: read_cmdline "rd.info" |
453 | # returns bool 1 if the variable was defined |
454 | # |
455 | read_cmdline() |
456 | { |
457 | local variable="$1" |
458 | local retval |
459 | local i |
460 | |
461 | if [[ -z ${variable} ]] |
462 | then |
463 | eecho "no variable given" |
464 | return 1 |
465 | fi |
466 | |
467 | if [ ! -e /proc/cmdline ] |
468 | then |
469 | eecho "read_cmdline(): /proc/cmdline does not exists" |
470 | return 1 |
471 | fi |
472 | |
473 | for i in $(</proc/cmdline) |
474 | do |
475 | if [[ ${i} = ${variable}* ]] |
476 | then |
477 | case ${variable} in |
478 | *=*) retval="${i#*=}" ;; |
479 | *) retval=1 ;; # bool |
480 | esac |
481 | fi |
482 | done |
483 | |
484 | echo "${retval}" |
485 | return 0 |
486 | } |