Magellan Linux

Contents of /mcore-src/trunk/mcore-tools/src/include/common.global.class.in

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2633 - (show annotations) (download)
Tue Sep 29 10:37:48 2015 UTC (8 years, 7 months ago) by niro
File size: 6698 byte(s)
-add more debug messages
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 to stderr
52 echo -e${opts} "${COLRED}$@${COLDEFAULT}${webcrlf}" 1>&2
53 }
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 if [[ ${NOCOLORS} = 1 ]]
64 then
65 COLPURPLE=""
66 COLDEFAULT=""
67 fi
68
69 [[ ${WEBCRLF} = 1 ]] && webcrlf="<br>"
70
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 # 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 # 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 if [[ -n $(pidof X) ]] || [[ -n $(pidof Xorg) ]] || [[ -n $(pidof Xorg.bin) ]]
154 then
155 su - "${MCORE_UNPRIV_USER}" -c "DISPLAY=${MCORE_XORG_DISPLAY} $@"
156 else
157 decho "x11runas(): No running X, Xorg or Xorg.bin process found"
158 fi
159 }
160
161 # no_duplicate $list $item
162 no_duplicate()
163 {
164 local i
165 local list="$1"
166 local item="$2"
167
168 for i in ${list}
169 do
170 [[ ${i} = ${item} ]] && return 1
171 done
172
173 return 0
174 }
175
176 # checks if given path is empty
177 path_not_empty()
178 {
179 local path="$1"
180 [[ -z ${path} ]] && eecho "path_not_empty(): no path given!" && return 1
181
182 # return ERR if path does not exist
183 [[ ! -d ${path} ]] && return 1
184 # return ERR if path empty
185 [[ -z $(find "${path}" -mindepth 1 -maxdepth 1) ]] && return 1
186
187 # every thing went ok, directory not empty
188 return 0
189 }
190
191 # list all files in a given directory
192 list_files_in_directory()
193 {
194 local i
195 local retval
196 local path
197 local opts
198 local type
199
200 # basic getops
201 for i in $*
202 do
203 case $1 in
204 -mindepth) shift; opts+=" -mindepth $1" ;;
205 -maxdepth) shift; opts+=" -maxdepth $1" ;;
206 -type) shift; type="$1" ;;
207 -name) shift; opts+=" -name $1" ;;
208 '') continue ;;
209 *) path="$1" ;;
210 esac
211 shift
212 done
213
214 if [[ -z ${path} ]]
215 then
216 eecho "No path given."
217 return 1
218 fi
219
220 if [[ ! -d ${path} ]]
221 then
222 eecho "Directory '${path}' does not exist."
223 return 1
224 fi
225
226 # default to files
227 [[ -z ${type} ]] && type=f
228
229 for i in $(find ${path} ${opts} -type ${type} -printf '%f\n' | sort)
230 do
231 if [[ -z ${retval} ]]
232 then
233 retval="${i}"
234 else
235 retval+=" ${i}"
236 fi
237 done
238
239 rvecho "${retval}"
240 }
241
242 # runs a command in the chroot of $MROOT
243 system_chroot()
244 {
245 local cmd="$@"
246 if [[ -z ${MROOT} ]]
247 then
248 echo "system_chroot(): \$MROOT was not set, doing nothing!"
249 return 1
250 fi
251 if [ ! -d ${MROOT} ]
252 then
253 eecho "system_chroot(): MROOT='${MROOT}' does not exist."
254 return 1
255 fi
256
257 chroot ${MROOT} ${cmd}
258 }
259
260 # gets interface used to reach given ip
261 iface_for_remote_addr()
262 {
263 set -- $(ip -o route get to $1)
264 echo $5
265 }
266
267 # get ip from dns name
268 dns_to_ip()
269 {
270 set -- $(getent hosts $1)
271 echo $1
272 }
273
274 iface_for_ip()
275 {
276 set -- $(ip -o addr show to $1)
277 echo $2
278 }
279
280 iface_for_mac()
281 {
282 local interface="" mac="$(echo $1 | sed 'y/ABCDEF/abcdef/')"
283 for interface in /sys/class/net/*; do
284 if [ $(cat $interface/address) = "$mac" ]; then
285 echo ${interface##*/}
286 fi
287 done
288 }
289
290 mac_for_iface()
291 {
292 local iface="$1"
293 if [ -f /sys/class/net/${iface}/address ]
294 then
295 cat /sys/class/net/${iface}/address
296 fi
297 }
298
299 certificate_fingerprint()
300 {
301 local cert_fingerprint
302 local retval
303
304 if [[ ! -f ${MCORE_CERT_FILE} ]]
305 then
306 eecho "MCORE_CERT_FILE '${MCORE_CERT_FILE}' does not exist."
307 return 1
308 fi
309
310 cert_fingerprint=$(openssl x509 -noout -modulus -in "${MCORE_CERT_FILE}" | openssl sha1 | sed 's:(stdin)=\ ::')
311 retval="$?"
312
313 if [[ ${retval} != 0 ]]
314 then
315 eecho "Error '${retval}' while generating cert_fingerprint."
316 return 1
317 fi
318
319 if [[ -z ${cert_fingerprint} ]]
320 then
321 eecho "Error: cert_fingerprint is empty"
322 return 1
323 else
324 echo "${cert_fingerprint}"
325 fi
326 }
327
328 key_fingerprint()
329 {
330 local key_fingerprint
331 local retval
332
333 if [[ ! -f ${MCORE_KEY_FILE} ]]
334 then
335 eecho "MCORE_KEY_FILE '${MCORE_KEY_FILE}' does not exist."
336 return 1
337 fi
338
339 key_fingerprint=$(openssl rsa -noout -modulus -in "${MCORE_KEY_FILE}" | openssl sha1 | sed 's:(stdin)=\ ::')
340 retval="$?"
341
342 if [[ ${retval} != 0 ]]
343 then
344 eecho "Error '${retval}' while generating key_fingerprint."
345 return 1
346 fi
347
348 if [[ -z ${key_fingerprint} ]]
349 then
350 eecho "Error: key_fingerprint is empty"
351 return 1
352 else
353 echo "${key_fingerprint}"
354 fi
355 }
356
357 nsslsay()
358 {
359 nssl "${SSLSAY_IP}" "${SSLSAY_PORT}" << EOF
360 auth ${SSLSAY_USER} ${SSLSAY_PASS}
361 $@
362 quit
363 EOF
364 }
365
366 nsslsay_fingerprint()
367 {
368 nssl "${SSLSAY_IP}" "${SSLSAY_PORT}" << EOF
369 certauth $(certificate_fingerprint)
370 $@
371 quit
372 EOF
373 }
374
375 nsslsay_queue_init()
376 {
377 SSLSAY_QUEUE=()
378 }
379
380 nsslsay_queue_add()
381 {
382 SSLSAY_QUEUE+=( "$@" )
383 }
384
385 nsslsay_queue_print()
386 {
387 local count
388 local i
389
390 count="${#SSLSAY_QUEUE[*]}"
391 for ((i=0; i < count; i++))
392 do
393 echo "${SSLSAY_QUEUE[${i}]}"
394 done
395 }
396
397 nsslsay_queue_run()
398 {
399 nsslsay "$(nsslsay_queue_print)"
400 }
401
402 nsslsay_queue_run_fingerprint()
403 {
404 nsslsay_fingerprint "$(nsslsay_queue_print)"
405 }