# $Id$
# message only echo | disabled in quiet mode
mecho()
{
local COLCYAN="\033[1;36m"
local COLDEFAULT="\033[0m"
local opts
local webcrlf
# print nothing if quiet mode was requested
[[ ${QUIET} = 1 ]] && return
if [[ ${NOCOLORS} = 1 ]]
then
COLCYAN=""
COLDEFAULT=""
fi
[[ ${WEBCRLF} = 1 ]] && webcrlf="
"
# respect -n
case $1 in
-n) shift; opts="n" ;;
esac
echo -e${opts} "${COLCYAN}$@${COLDEFAULT}${webcrlf}"
}
# prints error messages | enabled even in quiet mode
eecho()
{
local COLRED="\033[1;31m"
local COLDEFAULT="\033[0m"
local opts
local webcrlf
if [[ ${NOCOLORS} = 1 ]]
then
COLRED=""
COLDEFAULT=""
fi
[[ ${WEBCRLF} = 1 ]] && webcrlf="
"
# respect -n
case $1 in
-n) shift; opts="n" ;;
esac
echo -e${opts} "${COLRED}$@${COLDEFAULT}${webcrlf}"
}
# prints return values of get | enabled even in quiet mode
rvecho()
{
local COLPURPLE="\033[1;35m"
local COLDEFAULT="\033[0m"
local opts
local webcrlf
if [[ ${NOCOLORS} = 1 ]]
then
COLPURPLE=""
COLDEFAULT=""
fi
[[ ${WEBCRLF} = 1 ]] && webcrlf="
"
# respect -n
case $1 in
-n) shift; opts="n" ;;
esac
echo -e${opts} "${COLPURPLE}$@${COLDEFAULT}${webcrlf}"
}
# prints debug messages if requested | enabled even in quiet mode
decho()
{
# print nothing if debug mode was *not* requested
[[ ${DEBUG} != 1 ]] && return
eecho "DEBUG: ${@}"
}
# source a file with debug information
include()
{
local retval
if [ -f $@ ]
then
decho "including '$@'"
source $@
retval=$?
else
decho "include: '$@' not found"
retval=1
fi
return ${retval}
}
# adds a line to a configuration file defined by the $CONFIG variable
# $CONFIG="/etc/conf.d/mcore" addconfig 'LIBDIR="/usr/lib"'
addconfig()
{
local opts
if [[ -z ${CONFIG} ]]
then
eecho "You must define \$CONFIG varibale first!"
return 1
fi
if [[ ! -d $(dirname ${CONFIG}) ]]
then
install -d $(dirname ${CONFIG})
fi
# check for opts
case $1 in
-n) shift; opts=" -n" ;;
-e) shift; opts=" -e" ;;
esac
echo ${opts} "$@" >> ${CONFIG}
}
# creates or clears a configuration file defined by the $CONFIG variable
# CONFIG="/etc/conf.d/mcore" clearconfig
clearconfig()
{
if [[ -z ${CONFIG} ]]
then
eecho "You must define \$CONFIG varibale first!"
return 1
fi
if [[ ! -d $(dirname ${CONFIG}) ]]
then
install -d $(dirname ${CONFIG})
fi
: > ${CONFIG}
}
# root is not allowed to run progs in a user session with newer xorg-servers
# this wrapper runs a command in the xsession of the unpriv_user
x11runas()
{
if [[ -n $(pidof X) ]] || [[ -n $(pidof Xorg) ]] || [[ -n $(pidof Xorg.bin) ]]
then
su - "${MCORE_UNPRIV_USER}" -c "DISPLAY=${MCORE_XORG_DISPLAY} $@"
else
decho "x11runas(): No running X, Xorg or Xorg.bin process found"
fi
}
# no_duplicate $list $item
no_duplicate()
{
local i
local list="$1"
local item="$2"
for i in ${list}
do
[[ ${i} = ${item} ]] && return 1
done
return 0
}
# checks if given path is empty
path_not_empty()
{
local path="$1"
[[ -z ${path} ]] && eecho "path_not_empty(): no path given!" && return 1
# return ERR if path does not exist
[[ ! -d ${path} ]] && return 1
# return ERR if path empty
[[ -z $(find "${path}" -mindepth 1 -maxdepth 1) ]] && return 1
# every thing went ok, directory not empty
return 0
}
# list all files in a given directory
list_files_in_directory()
{
local i
local retval
local path
local opts
local type
# basic getops
for i in $*
do
case $1 in
-mindepth) shift; opts+=" -mindepth $1" ;;
-maxdepth) shift; opts+=" -maxdepth $1" ;;
-type) shift; type="$1" ;;
-name) shift; opts+=" -name $1" ;;
'') continue ;;
*) path="$1" ;;
esac
shift
done
if [[ -z ${path} ]]
then
eecho "No path given."
return 1
fi
if [[ ! -d ${path} ]]
then
eecho "Directory '${path}' does not exist."
return 1
fi
# default to files
[[ -z ${type} ]] && type=f
for i in $(find ${path} ${opts} -type ${type} -printf '%f\n' | sort)
do
if [[ -z ${retval} ]]
then
retval="${i}"
else
retval+=" ${i}"
fi
done
rvecho "${retval}"
}
# runs a command in the chroot of $MROOT
system_chroot()
{
local cmd="$@"
if [[ -z ${MROOT} ]]
then
echo "system_chroot(): \$MROOT was not set, doing nothing!"
return 1
fi
if [ ! -d ${MROOT} ]
then
eecho "system_chroot(): MROOT='${MROOT}' does not exist."
return 1
fi
chroot ${MROOT} ${cmd}
}
# gets interface used to reach given ip
iface_for_remote_addr()
{
set -- $(ip -o route get to $1)
echo $5
}
validate_ip_addr()
{
local ip="$1"
local retval=1
local _ifs
if [[ ${ip} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
then
_ifs=$IFS
IFS='.'
# convert to an array
ip=($ip)
IFS=$_ifs
if [[ ${ip[0]} -le 255 ]] &&
[[ ${ip[1]} -le 255 ]] &&
[[ ${ip[2]} -le 255 ]] &&
[[ ${ip[3]} -le 255 ]]
then
retval=$?
fi
fi
return ${retval}
}
# get ip from dns name
dns_to_ip()
{
if ! validate_ip_addr $1
then
set -- $(getent hosts $1)
fi
echo $1
}
iface_for_ip()
{
set -- $(ip -o addr show to $1)
echo $2
}
iface_for_mac()
{
local interface="" mac="$(echo $1 | sed 'y/ABCDEF/abcdef/')"
for interface in /sys/class/net/*; do
if [ $(cat $interface/address) = "$mac" ]; then
echo ${interface##*/}
fi
done
}
mac_for_iface()
{
local iface="$1"
if [ -f /sys/class/net/${iface}/address ]
then
cat /sys/class/net/${iface}/address
fi
}
certificate_fingerprint()
{
local cert_fingerprint
local retval
if [[ ! -f ${MCORE_CERT_FILE} ]]
then
eecho "MCORE_CERT_FILE '${MCORE_CERT_FILE}' does not exist."
return 1
fi
cert_fingerprint=$(openssl x509 -noout -modulus -in "${MCORE_CERT_FILE}" | openssl sha1 | sed 's:(stdin)=\ ::')
retval="$?"
if [[ ${retval} != 0 ]]
then
eecho "Error '${retval}' while generating cert_fingerprint."
return 1
fi
if [[ -z ${cert_fingerprint} ]]
then
eecho "Error: cert_fingerprint is empty"
return 1
else
echo "${cert_fingerprint}"
fi
}
key_fingerprint()
{
local key_fingerprint
local retval
if [[ ! -f ${MCORE_KEY_FILE} ]]
then
eecho "MCORE_KEY_FILE '${MCORE_KEY_FILE}' does not exist."
return 1
fi
key_fingerprint=$(openssl rsa -noout -modulus -in "${MCORE_KEY_FILE}" | openssl sha1 | sed 's:(stdin)=\ ::')
retval="$?"
if [[ ${retval} != 0 ]]
then
eecho "Error '${retval}' while generating key_fingerprint."
return 1
fi
if [[ -z ${key_fingerprint} ]]
then
eecho "Error: key_fingerprint is empty"
return 1
else
echo "${key_fingerprint}"
fi
}
nsslsay()
{
nssl "${SSLSAY_IP}" "${SSLSAY_PORT}" << EOF
auth ${SSLSAY_USER} ${SSLSAY_PASS}
$@
quit
EOF
}
nsslsay_fingerprint()
{
nssl "${SSLSAY_IP}" "${SSLSAY_PORT}" << EOF
certauth $(certificate_fingerprint)
$@
quit
EOF
}
nsslsay_queue_init()
{
SSLSAY_QUEUE=()
}
nsslsay_queue_add()
{
SSLSAY_QUEUE+=( "$@" )
}
nsslsay_queue_print()
{
local count
local i
count="${#SSLSAY_QUEUE[*]}"
for ((i=0; i < count; i++))
do
echo "${SSLSAY_QUEUE[${i}]}"
done
}
nsslsay_queue_run()
{
nsslsay "$(nsslsay_queue_print)"
}
nsslsay_queue_run_fingerprint()
{
nsslsay_fingerprint "$(nsslsay_queue_print)"
}
# read_cmdline "$variable"
# eg: read_cmdline "lang="
# returns the value of the cmdline variable lang
# eg: read_cmdline "rd.info"
# returns bool 1 if the variable was defined
#
read_cmdline()
{
local variable="$1"
local retval
local i
if [[ -z ${variable} ]]
then
eecho "no variable given"
return 1
fi
if [ ! -e /proc/cmdline ]
then
eecho "read_cmdline(): /proc/cmdline does not exists"
return 1
fi
for i in $(