#!@BASH@ # |\_ # B A C K U P N I N J A /()/ # `\| # # Copyright (C) 2004-05 riseup.net -- property is theft. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # ##################################################### ## FUNCTIONS setupcolors () { BLUE="\033[34;01m" GREEN="\033[32;01m" YELLOW="\033[33;01m" PURPLE="\033[35;01m" RED="\033[31;01m" OFF="\033[0m" CYAN="\033[36;01m" } colorize () { if [ "$usecolors" == "yes" ]; then local typestr=`echo "$@" | sed 's/\(^[^:]*\).*$/\1/'` [ "$typestr" == "Debug" ] && COLOR=$BLUE [ "$typestr" == "Info" ] && COLOR=$GREEN [ "$typestr" == "Warning" ] && COLOR=$YELLOW [ "$typestr" == "Error" ] && COLOR=$RED [ "$typestr" == "Fatal" ] && COLOR=$PURPLE endcolor=$OFF echo -e "$COLOR$@$endcolor" else echo -e "$@" fi } # We have the following message levels: # 0 - debug - blue # 1 - normal messages - green # 2 - warnings - yellow # 3 - errors - red # 4 - fatal - purple # First variable passed is the error level, all others are printed # if 1, echo out all warnings, errors, or fatal # used to capture output from handlers echo_debug_msg=0 usecolors=yes function printmsg() { [ ${#@} -gt 1 ] || return type=$1 shift if [ $type == 100 ]; then typestr=`echo "$@" | sed 's/\(^[^:]*\).*$/\1/'` [ "$typestr" == "Debug" ] && type=0 [ "$typestr" == "Info" ] && type=1 [ "$typestr" == "Warning" ] && type=2 [ "$typestr" == "Error" ] && type=3 [ "$typestr" == "Fatal" ] && type=4 typestr="" else types=(Debug Info Warning Error Fatal) typestr="${types[$type]}: " fi print=$[4-type] if [ $echo_debug_msg == 1 ]; then echo -e "$typestr$@" >&2 elif [ $debug ]; then colorize "$typestr$@" >&2 fi if [ $print -lt $loglevel ]; then logmsg "$typestr$@" fi } function logmsg() { if [ -w "$logfile" ]; then echo -e `date "+%h %d %H:%M:%S"` "$@" >> $logfile fi } function passthru() { printmsg 100 "$@" } function debug() { printmsg 0 "$@" } function info() { printmsg 1 "$@" } function warning() { printmsg 2 "$@" } function error() { printmsg 3 "$@" } function fatal() { printmsg 4 "$@" exit 2 } msgcount=0 function msg { messages[$msgcount]=$1 let "msgcount += 1" } function setfile() { CURRENT_CONF_FILE=$1 } function setsection() { CURRENT_SECTION=$1 } # # create a temporary file in a secure way. # function maketemp() { if [ -x /bin/mktemp ] then local tempfile=`mktemp /tmp/$1.XXXXXXXX` else DATE=`date` sectmp=`echo $DATE | /usr/bin/md5sum | cut -d- -f1` local tempfile=/tmp/$1.$sectmp fi echo $tempfile } # # sets a global var with name equal to $1 # to the value of the configuration parameter $1 # $2 is the default. # function getconf() { CURRENT_PARAM=$1 ret=`awk -f $scriptdir/parseini S=$CURRENT_SECTION P=$CURRENT_PARAM $CURRENT_CONF_FILE` # if nothing is returned, set the default if [ "$ret" == "" -a "$2" != "" ]; then ret="$2" fi # replace * with %, so that it is not globbed. ret="${ret//\\*/__star__}" # this is weird, but single quotes are needed to # allow for returned values with spaces. $ret is still expanded # because it is in an 'eval' statement. eval $1='$ret' } # # enforces very strict permissions on configuration file $file. # function check_perms() { local file=$1 local perms=`ls -ld $file` perms=${perms:4:6} if [ "$perms" != "------" ]; then echo "Configuration files must not be group or world writable/readable! Dying on file $file" fatal "Configuration files must not be group or world writable/readable! Dying on file $file" fi if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then echo "Configuration files must be owned by root! Dying on file $file" fatal "Configuration files must be owned by root! Dying on file $file" fi } # simple lowercase function function tolower() { echo "$1" | tr [:upper:] [:lower:] } # simple to integer function function toint() { echo "$1" | tr [:alpha:] -d } # # function isnow(): returns 1 if the time/day passed as $1 matches # the current time/day. # # format is at