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

revision 2407 by niro, Mon Aug 31 09:02:48 2015 UTC revision 2817 by niro, Wed Apr 12 13:21:50 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 149  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) ]] || [[ -n $(pidof Xorg) ]]   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 258  system_chroot() Line 260  system_chroot()
260  iface_for_remote_addr()  iface_for_remote_addr()
261  {  {
262   set -- $(ip -o route get to $1)   set -- $(ip -o route get to $1)
263   echo $5   # honor routes with and without a gateway
264     case $@ in
265     *via*) echo $5 ;;
266     *) echo $3 ;;
267     esac
268    }
269    
270    validate_ip_addr()
271    {
272     local ip="$1"
273     local retval=1
274     local _ifs
275    
276     if [[ ${ip} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
277     then
278     _ifs=$IFS
279     IFS='.'
280     # convert to an array
281     ip=($ip)
282     IFS=$_ifs
283    
284     if [[ ${ip[0]} -le 255 ]] &&
285     [[ ${ip[1]} -le 255 ]] &&
286     [[ ${ip[2]} -le 255 ]] &&
287     [[ ${ip[3]} -le 255 ]]
288     then
289     retval=$?
290     fi
291     fi
292    
293     return ${retval}
294  }  }
295    
296  # get ip from dns name  # get ip from dns name
297  dns_to_ip()  dns_to_ip()
298  {  {
299   set -- $(getent hosts $1)   if ! validate_ip_addr $1
300     then
301     set -- $(getent hosts $1)
302     fi
303   echo $1   echo $1
304  }  }
305    
306    iface_for_ip()
307    {
308     set -- $(ip -o addr show to $1)
309     echo $2
310    }
311    
312    iface_for_mac()
313    {
314     local interface="" mac="$(echo $1 | sed 'y/ABCDEF/abcdef/')"
315     for interface in /sys/class/net/*; do
316     if [ $(cat $interface/address) = "$mac" ]; then
317     echo ${interface##*/}
318     fi
319     done
320    }
321    
322    mac_for_iface()
323    {
324     local iface="$1"
325     if [ -f /sys/class/net/${iface}/address ]
326     then
327     cat /sys/class/net/${iface}/address
328     fi
329    }
330    
331    certificate_fingerprint()
332    {
333     local cert_fingerprint
334     local retval
335    
336     if [[ ! -f ${MCORE_CERT_FILE} ]]
337     then
338     eecho "MCORE_CERT_FILE '${MCORE_CERT_FILE}' does not exist."
339     return 1
340     fi
341    
342     cert_fingerprint=$(openssl x509 -noout -modulus -in "${MCORE_CERT_FILE}" | openssl sha1 | sed 's:(stdin)=\ ::')
343     retval="$?"
344    
345     if [[ ${retval} != 0 ]]
346     then
347     eecho "Error '${retval}' while generating cert_fingerprint."
348     return 1
349     fi
350    
351     if [[ -z ${cert_fingerprint} ]]
352     then
353     eecho "Error: cert_fingerprint is empty"
354     return 1
355     else
356     echo "${cert_fingerprint}"
357     fi
358    }
359    
360    key_fingerprint()
361    {
362     local key_fingerprint
363     local retval
364    
365     if [[ ! -f ${MCORE_KEY_FILE} ]]
366     then
367     eecho "MCORE_KEY_FILE '${MCORE_KEY_FILE}' does not exist."
368     return 1
369     fi
370    
371     key_fingerprint=$(openssl rsa -noout -modulus -in "${MCORE_KEY_FILE}" | openssl sha1 | sed 's:(stdin)=\ ::')
372     retval="$?"
373    
374     if [[ ${retval} != 0 ]]
375     then
376     eecho "Error '${retval}' while generating key_fingerprint."
377     return 1
378     fi
379    
380     if [[ -z ${key_fingerprint} ]]
381     then
382     eecho "Error: key_fingerprint is empty"
383     return 1
384     else
385     echo "${key_fingerprint}"
386     fi
387    }
388    
389    nsslsay()
390    {
391     nssl "${SSLSAY_IP}" "${SSLSAY_PORT}" << EOF
392    auth ${SSLSAY_USER} ${SSLSAY_PASS}
393    $@
394    quit
395    EOF
396    }
397    
398    nsslsay_fingerprint()
399    {
400     nssl "${SSLSAY_IP}" "${SSLSAY_PORT}" << EOF
401    certauth $(certificate_fingerprint)
402    $@
403    quit
404    EOF
405    }
406    
407    nsslsay_queue_init()
408    {
409     SSLSAY_QUEUE=()
410    }
411    
412    nsslsay_queue_add()
413    {
414     SSLSAY_QUEUE+=( "$@" )
415    }
416    
417    nsslsay_queue_print()
418    {
419     local count
420     local i
421    
422     count="${#SSLSAY_QUEUE[*]}"
423     for ((i=0; i < count; i++))
424     do
425     echo "${SSLSAY_QUEUE[${i}]}"
426     done
427    }
428    
429    nsslsay_queue_run()
430    {
431     nsslsay "$(nsslsay_queue_print)"
432    }
433    
434    nsslsay_queue_run_fingerprint()
435    {
436     nsslsay_fingerprint "$(nsslsay_queue_print)"
437    }
438    
439    # read_cmdline "$variable"
440    # eg: read_cmdline "lang="
441    #       returns the value of the cmdline variable lang
442    # eg: read_cmdline "rd.info"
443    #      returns bool 1 if the variable was defined
444    #
445    read_cmdline()
446    {
447     local variable="$1"
448     local retval
449     local i
450    
451     if [[ -z ${variable} ]]
452     then
453     eecho "no variable given"
454     return 1
455     fi
456    
457     if [ ! -e /proc/cmdline ]
458     then
459     eecho "read_cmdline(): /proc/cmdline does not exists"
460     return 1
461     fi
462    
463     for i in $(</proc/cmdline)
464     do
465     if [[ ${i} = ${variable}* ]]
466     then
467     case ${variable} in
468     *=*) retval="${i#*=}" ;;
469     *) retval=1 ;; # bool
470     esac
471     fi
472     done
473    
474     echo "${retval}"
475     return 0
476    }

Legend:
Removed from v.2407  
changed lines
  Added in v.2817