Annotation of /mcore-src/trunk/mcore-tools/src/include/common.global.class.in
Parent Directory | Revision Log
Revision 2632 -
(hide annotations)
(download)
Tue Sep 29 10:36:33 2015 UTC (8 years, 7 months ago) by niro
File size: 6625 byte(s)
Tue Sep 29 10:36:33 2015 UTC (8 years, 7 months ago) by niro
File size: 6625 byte(s)
-honor all flavors of xorg-server
1 | niro | 2140 | # $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 | niro | 2479 | [[ ${QUIET} = 1 ]] && return |
13 | niro | 2140 | |
14 | niro | 2479 | if [[ ${NOCOLORS} = 1 ]] |
15 | niro | 2140 | then |
16 | COLCYAN="" | ||
17 | COLDEFAULT="" | ||
18 | fi | ||
19 | |||
20 | niro | 2479 | [[ ${WEBCRLF} = 1 ]] && webcrlf="<br>" |
21 | niro | 2140 | |
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 | niro | 2479 | if [[ ${NOCOLORS} = 1 ]] |
39 | niro | 2140 | then |
40 | COLRED="" | ||
41 | COLDEFAULT="" | ||
42 | fi | ||
43 | |||
44 | niro | 2479 | [[ ${WEBCRLF} = 1 ]] && webcrlf="<br>" |
45 | niro | 2140 | |
46 | # respect -n | ||
47 | case $1 in | ||
48 | -n) shift; opts="n" ;; | ||
49 | esac | ||
50 | |||
51 | niro | 2631 | # echo to stderr |
52 | echo -e${opts} "${COLRED}$@${COLDEFAULT}${webcrlf}" 1>&2 | ||
53 | niro | 2140 | } |
54 | |||
55 | # prints return values of get | enabled even in quiet mode | ||
56 | rvecho() | ||
57 | { | ||
58 | local COLPURPLE="\033[1;35m" | ||
59 | local COLDEFAULT="\033[0m" | ||
60 | local opts | ||
61 | local webcrlf | ||
62 | |||
63 | niro | 2479 | if [[ ${NOCOLORS} = 1 ]] |
64 | niro | 2140 | then |
65 | COLPURPLE="" | ||
66 | COLDEFAULT="" | ||
67 | fi | ||
68 | |||
69 | niro | 2479 | [[ ${WEBCRLF} = 1 ]] && webcrlf="<br>" |
70 | niro | 2140 | |
71 | # respect -n | ||
72 | case $1 in | ||
73 | -n) shift; opts="n" ;; | ||
74 | esac | ||
75 | |||
76 | echo -e${opts} "${COLPURPLE}$@${COLDEFAULT}${webcrlf}" | ||
77 | } | ||
78 | |||
79 | # prints debug messages if requested | enabled even in quiet mode | ||
80 | decho() | ||
81 | { | ||
82 | # print nothing if debug mode was *not* requested | ||
83 | [[ ${DEBUG} != 1 ]] && return | ||
84 | |||
85 | eecho "DEBUG: ${@}" | ||
86 | } | ||
87 | |||
88 | niro | 2257 | # source a file with debug information |
89 | include() | ||
90 | { | ||
91 | local retval | ||
92 | |||
93 | if [ -f $@ ] | ||
94 | then | ||
95 | decho "including '$@'" | ||
96 | source $@ | ||
97 | retval=$? | ||
98 | else | ||
99 | decho "include: '$@' not found" | ||
100 | retval=1 | ||
101 | fi | ||
102 | |||
103 | return ${retval} | ||
104 | } | ||
105 | |||
106 | niro | 2140 | # adds a line to a configuration file defined by the $CONFIG variable |
107 | # $CONFIG="/etc/conf.d/mcore" addconfig 'LIBDIR="/usr/lib"' | ||
108 | addconfig() | ||
109 | { | ||
110 | local opts | ||
111 | |||
112 | if [[ -z ${CONFIG} ]] | ||
113 | then | ||
114 | eecho "You must define \$CONFIG varibale first!" | ||
115 | return 1 | ||
116 | fi | ||
117 | |||
118 | if [[ ! -d $(dirname ${CONFIG}) ]] | ||
119 | then | ||
120 | install -d $(dirname ${CONFIG}) | ||
121 | fi | ||
122 | |||
123 | # check for opts | ||
124 | case $1 in | ||
125 | -n) shift; opts=" -n" ;; | ||
126 | -e) shift; opts=" -e" ;; | ||
127 | esac | ||
128 | |||
129 | echo ${opts} "$@" >> ${CONFIG} | ||
130 | } | ||
131 | |||
132 | # creates or clears a configuration file defined by the $CONFIG variable | ||
133 | # CONFIG="/etc/conf.d/mcore" clearconfig | ||
134 | clearconfig() | ||
135 | { | ||
136 | if [[ -z ${CONFIG} ]] | ||
137 | then | ||
138 | eecho "You must define \$CONFIG varibale first!" | ||
139 | return 1 | ||
140 | fi | ||
141 | |||
142 | if [[ ! -d $(dirname ${CONFIG}) ]] | ||
143 | then | ||
144 | install -d $(dirname ${CONFIG}) | ||
145 | fi | ||
146 | : > ${CONFIG} | ||
147 | } | ||
148 | |||
149 | # root is not allowed to run progs in a user session with newer xorg-servers | ||
150 | # this wrapper runs a command in the xsession of the unpriv_user | ||
151 | x11runas() | ||
152 | { | ||
153 | niro | 2632 | if [[ -n $(pidof X) ]] || [[ -n $(pidof Xorg) ]] || [[ -n $(pidof Xorg.bin) ]] |
154 | niro | 2140 | then |
155 | niro | 2351 | su - "${MCORE_UNPRIV_USER}" -c "DISPLAY=${MCORE_XORG_DISPLAY} $@" |
156 | niro | 2140 | fi |
157 | } | ||
158 | |||
159 | # no_duplicate $list $item | ||
160 | no_duplicate() | ||
161 | { | ||
162 | local i | ||
163 | local list="$1" | ||
164 | local item="$2" | ||
165 | |||
166 | for i in ${list} | ||
167 | do | ||
168 | [[ ${i} = ${item} ]] && return 1 | ||
169 | done | ||
170 | |||
171 | return 0 | ||
172 | } | ||
173 | |||
174 | # checks if given path is empty | ||
175 | path_not_empty() | ||
176 | { | ||
177 | local path="$1" | ||
178 | [[ -z ${path} ]] && eecho "path_not_empty(): no path given!" && return 1 | ||
179 | |||
180 | # return ERR if path does not exist | ||
181 | [[ ! -d ${path} ]] && return 1 | ||
182 | # return ERR if path empty | ||
183 | [[ -z $(find "${path}" -mindepth 1 -maxdepth 1) ]] && return 1 | ||
184 | |||
185 | # every thing went ok, directory not empty | ||
186 | return 0 | ||
187 | } | ||
188 | |||
189 | # list all files in a given directory | ||
190 | list_files_in_directory() | ||
191 | { | ||
192 | local i | ||
193 | local retval | ||
194 | local path | ||
195 | local opts | ||
196 | local type | ||
197 | |||
198 | # basic getops | ||
199 | for i in $* | ||
200 | do | ||
201 | case $1 in | ||
202 | -mindepth) shift; opts+=" -mindepth $1" ;; | ||
203 | -maxdepth) shift; opts+=" -maxdepth $1" ;; | ||
204 | -type) shift; type="$1" ;; | ||
205 | -name) shift; opts+=" -name $1" ;; | ||
206 | '') continue ;; | ||
207 | *) path="$1" ;; | ||
208 | esac | ||
209 | shift | ||
210 | done | ||
211 | |||
212 | if [[ -z ${path} ]] | ||
213 | then | ||
214 | eecho "No path given." | ||
215 | return 1 | ||
216 | fi | ||
217 | |||
218 | if [[ ! -d ${path} ]] | ||
219 | then | ||
220 | eecho "Directory '${path}' does not exist." | ||
221 | return 1 | ||
222 | fi | ||
223 | |||
224 | # default to files | ||
225 | [[ -z ${type} ]] && type=f | ||
226 | |||
227 | for i in $(find ${path} ${opts} -type ${type} -printf '%f\n' | sort) | ||
228 | do | ||
229 | if [[ -z ${retval} ]] | ||
230 | then | ||
231 | retval="${i}" | ||
232 | else | ||
233 | retval+=" ${i}" | ||
234 | fi | ||
235 | done | ||
236 | |||
237 | rvecho "${retval}" | ||
238 | } | ||
239 | |||
240 | # runs a command in the chroot of $MROOT | ||
241 | system_chroot() | ||
242 | { | ||
243 | local cmd="$@" | ||
244 | if [[ -z ${MROOT} ]] | ||
245 | then | ||
246 | echo "system_chroot(): \$MROOT was not set, doing nothing!" | ||
247 | return 1 | ||
248 | fi | ||
249 | if [ ! -d ${MROOT} ] | ||
250 | then | ||
251 | eecho "system_chroot(): MROOT='${MROOT}' does not exist." | ||
252 | return 1 | ||
253 | fi | ||
254 | |||
255 | chroot ${MROOT} ${cmd} | ||
256 | } | ||
257 | niro | 2407 | |
258 | # gets interface used to reach given ip | ||
259 | iface_for_remote_addr() | ||
260 | { | ||
261 | set -- $(ip -o route get to $1) | ||
262 | echo $5 | ||
263 | } | ||
264 | |||
265 | # get ip from dns name | ||
266 | dns_to_ip() | ||
267 | { | ||
268 | set -- $(getent hosts $1) | ||
269 | echo $1 | ||
270 | } | ||
271 | niro | 2420 | |
272 | iface_for_ip() | ||
273 | { | ||
274 | set -- $(ip -o addr show to $1) | ||
275 | echo $2 | ||
276 | } | ||
277 | |||
278 | iface_for_mac() | ||
279 | { | ||
280 | local interface="" mac="$(echo $1 | sed 'y/ABCDEF/abcdef/')" | ||
281 | for interface in /sys/class/net/*; do | ||
282 | if [ $(cat $interface/address) = "$mac" ]; then | ||
283 | echo ${interface##*/} | ||
284 | fi | ||
285 | done | ||
286 | } | ||
287 | |||
288 | mac_for_iface() | ||
289 | { | ||
290 | local iface="$1" | ||
291 | if [ -f /sys/class/net/${iface}/address ] | ||
292 | then | ||
293 | cat /sys/class/net/${iface}/address | ||
294 | fi | ||
295 | } | ||
296 | niro | 2428 | |
297 | certificate_fingerprint() | ||
298 | { | ||
299 | local cert_fingerprint | ||
300 | local retval | ||
301 | |||
302 | if [[ ! -f ${MCORE_CERT_FILE} ]] | ||
303 | then | ||
304 | eecho "MCORE_CERT_FILE '${MCORE_CERT_FILE}' does not exist." | ||
305 | return 1 | ||
306 | fi | ||
307 | |||
308 | cert_fingerprint=$(openssl x509 -noout -modulus -in "${MCORE_CERT_FILE}" | openssl sha1 | sed 's:(stdin)=\ ::') | ||
309 | retval="$?" | ||
310 | |||
311 | if [[ ${retval} != 0 ]] | ||
312 | then | ||
313 | eecho "Error '${retval}' while generating cert_fingerprint." | ||
314 | return 1 | ||
315 | fi | ||
316 | |||
317 | if [[ -z ${cert_fingerprint} ]] | ||
318 | then | ||
319 | eecho "Error: cert_fingerprint is empty" | ||
320 | return 1 | ||
321 | else | ||
322 | echo "${cert_fingerprint}" | ||
323 | fi | ||
324 | } | ||
325 | |||
326 | key_fingerprint() | ||
327 | { | ||
328 | local key_fingerprint | ||
329 | local retval | ||
330 | |||
331 | if [[ ! -f ${MCORE_KEY_FILE} ]] | ||
332 | then | ||
333 | eecho "MCORE_KEY_FILE '${MCORE_KEY_FILE}' does not exist." | ||
334 | return 1 | ||
335 | fi | ||
336 | |||
337 | key_fingerprint=$(openssl rsa -noout -modulus -in "${MCORE_KEY_FILE}" | openssl sha1 | sed 's:(stdin)=\ ::') | ||
338 | retval="$?" | ||
339 | |||
340 | if [[ ${retval} != 0 ]] | ||
341 | then | ||
342 | eecho "Error '${retval}' while generating key_fingerprint." | ||
343 | return 1 | ||
344 | fi | ||
345 | |||
346 | if [[ -z ${key_fingerprint} ]] | ||
347 | then | ||
348 | eecho "Error: key_fingerprint is empty" | ||
349 | return 1 | ||
350 | else | ||
351 | echo "${key_fingerprint}" | ||
352 | fi | ||
353 | } | ||
354 | |||
355 | niro | 2429 | nsslsay() |
356 | { | ||
357 | nssl "${SSLSAY_IP}" "${SSLSAY_PORT}" << EOF | ||
358 | auth ${SSLSAY_USER} ${SSLSAY_PASS} | ||
359 | $@ | ||
360 | quit | ||
361 | EOF | ||
362 | } | ||
363 | |||
364 | nsslsay_fingerprint() | ||
365 | { | ||
366 | nssl "${SSLSAY_IP}" "${SSLSAY_PORT}" << EOF | ||
367 | certauth $(certificate_fingerprint) | ||
368 | $@ | ||
369 | quit | ||
370 | EOF | ||
371 | } | ||
372 | niro | 2485 | |
373 | nsslsay_queue_init() | ||
374 | { | ||
375 | SSLSAY_QUEUE=() | ||
376 | } | ||
377 | |||
378 | nsslsay_queue_add() | ||
379 | { | ||
380 | SSLSAY_QUEUE+=( "$@" ) | ||
381 | } | ||
382 | |||
383 | nsslsay_queue_print() | ||
384 | { | ||
385 | local count | ||
386 | local i | ||
387 | |||
388 | count="${#SSLSAY_QUEUE[*]}" | ||
389 | for ((i=0; i < count; i++)) | ||
390 | do | ||
391 | echo "${SSLSAY_QUEUE[${i}]}" | ||
392 | done | ||
393 | } | ||
394 | |||
395 | nsslsay_queue_run() | ||
396 | { | ||
397 | nsslsay "$(nsslsay_queue_print)" | ||
398 | } | ||
399 | |||
400 | nsslsay_queue_run_fingerprint() | ||
401 | { | ||
402 | nsslsay_fingerprint "$(nsslsay_queue_print)" | ||
403 | } |