Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

mcore-src/trunk/mcore-tools/daemon/include/common.global.class revision 2140 by niro, Fri Jan 10 12:02:07 2014 UTC mcore-src/trunk/mcore-tools/src/include/common.global.class.in revision 2809 by niro, Fri Apr 7 07:17:48 2017 UTC
# Line 9  mecho() Line 9  mecho()
9   local webcrlf   local webcrlf
10    
11   # print nothing if quiet mode was requested   # print nothing if quiet mode was requested
12   [[ ${QUIET} = true ]] && return   [[ ${QUIET} = 1 ]] && return
13    
14   if [[ ${NOCOLORS} = true ]]   if [[ ${NOCOLORS} = 1 ]]
15   then   then
16   COLCYAN=""   COLCYAN=""
17   COLDEFAULT=""   COLDEFAULT=""
18   fi   fi
19    
20   [[ ${WEBCRLF} = true ]] && webcrlf="<br>"   [[ ${WEBCRLF} = 1 ]] && webcrlf="<br>"
21    
22   # respect -n   # respect -n
23   case $1 in   case $1 in
# Line 35  eecho() Line 35  eecho()
35   local opts   local opts
36   local webcrlf   local webcrlf
37    
38   if [[ ${NOCOLORS} = true ]]   if [[ ${NOCOLORS} = 1 ]]
39   then   then
40   COLRED=""   COLRED=""
41   COLDEFAULT=""   COLDEFAULT=""
42   fi   fi
43    
44   [[ ${WEBCRLF} = true ]] && webcrlf="<br>"   [[ ${WEBCRLF} = 1 ]] && webcrlf="<br>"
45    
46   # respect -n   # respect -n
47   case $1 in   case $1 in
# Line 59  rvecho() Line 59  rvecho()
59   local opts   local opts
60   local webcrlf   local webcrlf
61    
62   if [[ ${NOCOLORS} = true ]]   if [[ ${NOCOLORS} = 1 ]]
63   then   then
64   COLPURPLE=""   COLPURPLE=""
65   COLDEFAULT=""   COLDEFAULT=""
66   fi   fi
67    
68   [[ ${WEBCRLF} = true ]] && webcrlf="<br>"   [[ ${WEBCRLF} = 1 ]] && webcrlf="<br>"
69    
70   # respect -n   # respect -n
71   case $1 in   case $1 in
# Line 84  decho() Line 84  decho()
84   eecho "DEBUG: ${@}"   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  # adds a line to a configuration file defined by the $CONFIG variable
106  # $CONFIG="/etc/conf.d/mcore" addconfig 'LIBDIR="/usr/lib"'  # $CONFIG="/etc/conf.d/mcore" addconfig 'LIBDIR="/usr/lib"'
107  addconfig()  addconfig()
# Line 131  clearconfig() Line 149  clearconfig()
149  # this wrapper runs a command in the xsession of the unpriv_user  # this wrapper runs a command in the xsession of the unpriv_user
150  x11runas()  x11runas()
151  {  {
152   if [[ -n $(pidof X) ]]   if [[ -n $(pidof X) ]] || [[ -n $(pidof Xorg) ]] || [[ -n $(pidof Xorg.bin) ]]
153   then   then
154   su - "${MCORE_UNPRIV_USER}" -c "DISPLAY=${MCORE_XORG_DISPLAY} $@"   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   fi
158  }  }
159    
# Line 235  system_chroot() Line 255  system_chroot()
255    
256   chroot ${MROOT} ${cmd}   chroot ${MROOT} ${cmd}
257  }  }
258    
259    # gets interface used to reach given ip
260    iface_for_remote_addr()
261    {
262     set -- $(ip -o route get to $1)
263     echo $5
264    }
265    
266    validate_ip_addr()
267    {
268     local ip="$1"
269     local retval=1
270     local _ifs
271    
272     if [[ ${ip} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
273     then
274     _ifs=$IFS
275     IFS='.'
276     # convert to an array
277     ip=($ip)
278     IFS=$_ifs
279    
280     if [[ ${ip[0]} -le 255 ]] &&
281     [[ ${ip[1]} -le 255 ]] &&
282     [[ ${ip[2]} -le 255 ]] &&
283     [[ ${ip[3]} -le 255 ]]
284     then
285     retval=$?
286     fi
287     fi
288    
289     return ${retval}
290    }
291    
292    # get ip from dns name
293    dns_to_ip()
294    {
295     if ! validate_ip_addr $1
296     then
297     set -- $(getent hosts $1)
298     fi
299     echo $1
300    }
301    
302    iface_for_ip()
303    {
304     set -- $(ip -o addr show to $1)
305     echo $2
306    }
307    
308    iface_for_mac()
309    {
310     local interface="" mac="$(echo $1 | sed 'y/ABCDEF/abcdef/')"
311     for interface in /sys/class/net/*; do
312     if [ $(cat $interface/address) = "$mac" ]; then
313     echo ${interface##*/}
314     fi
315     done
316    }
317    
318    mac_for_iface()
319    {
320     local iface="$1"
321     if [ -f /sys/class/net/${iface}/address ]
322     then
323     cat /sys/class/net/${iface}/address
324     fi
325    }
326    
327    certificate_fingerprint()
328    {
329     local cert_fingerprint
330     local retval
331    
332     if [[ ! -f ${MCORE_CERT_FILE} ]]
333     then
334     eecho "MCORE_CERT_FILE '${MCORE_CERT_FILE}' does not exist."
335     return 1
336     fi
337    
338     cert_fingerprint=$(openssl x509 -noout -modulus -in "${MCORE_CERT_FILE}" | openssl sha1 | sed 's:(stdin)=\ ::')
339     retval="$?"
340    
341     if [[ ${retval} != 0 ]]
342     then
343     eecho "Error '${retval}' while generating cert_fingerprint."
344     return 1
345     fi
346    
347     if [[ -z ${cert_fingerprint} ]]
348     then
349     eecho "Error: cert_fingerprint is empty"
350     return 1
351     else
352     echo "${cert_fingerprint}"
353     fi
354    }
355    
356    key_fingerprint()
357    {
358     local key_fingerprint
359     local retval
360    
361     if [[ ! -f ${MCORE_KEY_FILE} ]]
362     then
363     eecho "MCORE_KEY_FILE '${MCORE_KEY_FILE}' does not exist."
364     return 1
365     fi
366    
367     key_fingerprint=$(openssl rsa -noout -modulus -in "${MCORE_KEY_FILE}" | openssl sha1 | sed 's:(stdin)=\ ::')
368     retval="$?"
369    
370     if [[ ${retval} != 0 ]]
371     then
372     eecho "Error '${retval}' while generating key_fingerprint."
373     return 1
374     fi
375    
376     if [[ -z ${key_fingerprint} ]]
377     then
378     eecho "Error: key_fingerprint is empty"
379     return 1
380     else
381     echo "${key_fingerprint}"
382     fi
383    }
384    
385    nsslsay()
386    {
387     nssl "${SSLSAY_IP}" "${SSLSAY_PORT}" << EOF
388    auth ${SSLSAY_USER} ${SSLSAY_PASS}
389    $@
390    quit
391    EOF
392    }
393    
394    nsslsay_fingerprint()
395    {
396     nssl "${SSLSAY_IP}" "${SSLSAY_PORT}" << EOF
397    certauth $(certificate_fingerprint)
398    $@
399    quit
400    EOF
401    }
402    
403    nsslsay_queue_init()
404    {
405     SSLSAY_QUEUE=()
406    }
407    
408    nsslsay_queue_add()
409    {
410     SSLSAY_QUEUE+=( "$@" )
411    }
412    
413    nsslsay_queue_print()
414    {
415     local count
416     local i
417    
418     count="${#SSLSAY_QUEUE[*]}"
419     for ((i=0; i < count; i++))
420     do
421     echo "${SSLSAY_QUEUE[${i}]}"
422     done
423    }
424    
425    nsslsay_queue_run()
426    {
427     nsslsay "$(nsslsay_queue_print)"
428    }
429    
430    nsslsay_queue_run_fingerprint()
431    {
432     nsslsay_fingerprint "$(nsslsay_queue_print)"
433    }
434    
435    # read_cmdline "$variable"
436    # eg: read_cmdline "lang="
437    #       returns the value of the cmdline variable lang
438    # eg: read_cmdline "rd.info"
439    #      returns bool 1 if the variable was defined
440    #
441    read_cmdline()
442    {
443     local variable="$1"
444     local retval
445     local i
446    
447     if [[ -z ${variable} ]]
448     then
449     eecho "no variable given"
450     return 1
451     fi
452    
453     if [ ! -e /proc/cmdline ]
454     then
455     eecho "read_cmdline(): /proc/cmdline does not exists"
456     return 1
457     fi
458    
459     for i in $(</proc/cmdline)
460     do
461     if [[ ${i} = ${variable}* ]]
462     then
463     case ${variable} in
464     *=*) retval="${i#*=}" ;;
465     *) retval=1 ;; # bool
466     esac
467     fi
468     done
469    
470     echo "${retval}"
471     return 0
472    }

Legend:
Removed from v.2140  
changed lines
  Added in v.2809