Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2429 - (hide annotations) (download)
Thu Sep 3 12:09:58 2015 UTC (8 years, 8 months ago) by niro
File size: 6210 byte(s)
-added initial nsslsay() and nsslsay_fingerprint() functions
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     [[ ${QUIET} = true ]] && return
13    
14     if [[ ${NOCOLORS} = true ]]
15     then
16     COLCYAN=""
17     COLDEFAULT=""
18     fi
19    
20     [[ ${WEBCRLF} = true ]] && 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} = true ]]
39     then
40     COLRED=""
41     COLDEFAULT=""
42     fi
43    
44     [[ ${WEBCRLF} = true ]] && 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} = true ]]
63     then
64     COLPURPLE=""
65     COLDEFAULT=""
66     fi
67    
68     [[ ${WEBCRLF} = true ]] && 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 niro 2257 # 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 niro 2140 # 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 niro 2351 if [[ -n $(pidof X) ]] || [[ -n $(pidof Xorg) ]]
153 niro 2140 then
154 niro 2351 su - "${MCORE_UNPRIV_USER}" -c "DISPLAY=${MCORE_XORG_DISPLAY} $@"
155 niro 2140 fi
156     }
157    
158     # no_duplicate $list $item
159     no_duplicate()
160     {
161     local i
162     local list="$1"
163     local item="$2"
164    
165     for i in ${list}
166     do
167     [[ ${i} = ${item} ]] && return 1
168     done
169    
170     return 0
171     }
172    
173     # checks if given path is empty
174     path_not_empty()
175     {
176     local path="$1"
177     [[ -z ${path} ]] && eecho "path_not_empty(): no path given!" && return 1
178    
179     # return ERR if path does not exist
180     [[ ! -d ${path} ]] && return 1
181     # return ERR if path empty
182     [[ -z $(find "${path}" -mindepth 1 -maxdepth 1) ]] && return 1
183    
184     # every thing went ok, directory not empty
185     return 0
186     }
187    
188     # list all files in a given directory
189     list_files_in_directory()
190     {
191     local i
192     local retval
193     local path
194     local opts
195     local type
196    
197     # basic getops
198     for i in $*
199     do
200     case $1 in
201     -mindepth) shift; opts+=" -mindepth $1" ;;
202     -maxdepth) shift; opts+=" -maxdepth $1" ;;
203     -type) shift; type="$1" ;;
204     -name) shift; opts+=" -name $1" ;;
205     '') continue ;;
206     *) path="$1" ;;
207     esac
208     shift
209     done
210    
211     if [[ -z ${path} ]]
212     then
213     eecho "No path given."
214     return 1
215     fi
216    
217     if [[ ! -d ${path} ]]
218     then
219     eecho "Directory '${path}' does not exist."
220     return 1
221     fi
222    
223     # default to files
224     [[ -z ${type} ]] && type=f
225    
226     for i in $(find ${path} ${opts} -type ${type} -printf '%f\n' | sort)
227     do
228     if [[ -z ${retval} ]]
229     then
230     retval="${i}"
231     else
232     retval+=" ${i}"
233     fi
234     done
235    
236     rvecho "${retval}"
237     }
238    
239     # runs a command in the chroot of $MROOT
240     system_chroot()
241     {
242     local cmd="$@"
243     if [[ -z ${MROOT} ]]
244     then
245     echo "system_chroot(): \$MROOT was not set, doing nothing!"
246     return 1
247     fi
248     if [ ! -d ${MROOT} ]
249     then
250     eecho "system_chroot(): MROOT='${MROOT}' does not exist."
251     return 1
252     fi
253    
254     chroot ${MROOT} ${cmd}
255     }
256 niro 2407
257     # gets interface used to reach given ip
258     iface_for_remote_addr()
259     {
260     set -- $(ip -o route get to $1)
261     echo $5
262     }
263    
264     # get ip from dns name
265     dns_to_ip()
266     {
267     set -- $(getent hosts $1)
268     echo $1
269     }
270 niro 2420
271     iface_for_ip()
272     {
273     set -- $(ip -o addr show to $1)
274     echo $2
275     }
276    
277     iface_for_mac()
278     {
279     local interface="" mac="$(echo $1 | sed 'y/ABCDEF/abcdef/')"
280     for interface in /sys/class/net/*; do
281     if [ $(cat $interface/address) = "$mac" ]; then
282     echo ${interface##*/}
283     fi
284     done
285     }
286    
287     mac_for_iface()
288     {
289     local iface="$1"
290     if [ -f /sys/class/net/${iface}/address ]
291     then
292     cat /sys/class/net/${iface}/address
293     fi
294     }
295 niro 2428
296     certificate_fingerprint()
297     {
298     local cert_fingerprint
299     local retval
300    
301     if [[ ! -f ${MCORE_CERT_FILE} ]]
302     then
303     eecho "MCORE_CERT_FILE '${MCORE_CERT_FILE}' does not exist."
304     return 1
305     fi
306    
307     cert_fingerprint=$(openssl x509 -noout -modulus -in "${MCORE_CERT_FILE}" | openssl sha1 | sed 's:(stdin)=\ ::')
308     retval="$?"
309    
310     if [[ ${retval} != 0 ]]
311     then
312     eecho "Error '${retval}' while generating cert_fingerprint."
313     return 1
314     fi
315    
316     if [[ -z ${cert_fingerprint} ]]
317     then
318     eecho "Error: cert_fingerprint is empty"
319     return 1
320     else
321     echo "${cert_fingerprint}"
322     fi
323     }
324    
325     key_fingerprint()
326     {
327     local key_fingerprint
328     local retval
329    
330     if [[ ! -f ${MCORE_KEY_FILE} ]]
331     then
332     eecho "MCORE_KEY_FILE '${MCORE_KEY_FILE}' does not exist."
333     return 1
334     fi
335    
336     key_fingerprint=$(openssl rsa -noout -modulus -in "${MCORE_KEY_FILE}" | openssl sha1 | sed 's:(stdin)=\ ::')
337     retval="$?"
338    
339     if [[ ${retval} != 0 ]]
340     then
341     eecho "Error '${retval}' while generating key_fingerprint."
342     return 1
343     fi
344    
345     if [[ -z ${key_fingerprint} ]]
346     then
347     eecho "Error: key_fingerprint is empty"
348     return 1
349     else
350     echo "${key_fingerprint}"
351     fi
352     }
353    
354 niro 2429 nsslsay()
355     {
356     nssl "${SSLSAY_IP}" "${SSLSAY_PORT}" << EOF
357     auth ${SSLSAY_USER} ${SSLSAY_PASS}
358     $@
359     quit
360     EOF
361     }
362    
363     nsslsay_fingerprint()
364     {
365     nssl "${SSLSAY_IP}" "${SSLSAY_PORT}" << EOF
366     certauth $(certificate_fingerprint)
367     $@
368     quit
369     EOF
370     }