#!@BASH@ # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*- # # |\_ # 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 function 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" COLORS=($BLUE $GREEN $YELLOW $RED $PURPLE $CYAN) } function colorize () { if [ "$usecolors" == "yes" ]; then local 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" == "Halt" ] && type=5 color=${COLORS[$type]} 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 # 5 - halt - cyan # 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" == "Halt" ] && type=5 typestr="" else types=(Debug Info Warning Error Fatal Halt) 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 `LC_ALL=C 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 } function halt() { printmsg 5 "$@" exit 2 } msgcount=0 function msg { messages[$msgcount]=$1 let "msgcount += 1" } # # enforces very strict permissions on configuration file $file. # function check_perms() { local file=$1 debug "check_perms $file" local perms local owners perms=($(stat -L --format='%A' $file)) debug "perms: $perms" local gperm=${perms:4:3} debug "gperm: $gperm" local wperm=${perms:7:3} debug "wperm: $wperm" owners=($(stat -L --format='%g %G %u %U' $file)) local gid=${owners[0]} local group=${owners[1]} local owner=${owners[2]} if [ "$owner" != 0 ]; 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 if [ "$wperm" != '---' ]; then echo "Configuration files must not be world writable/readable! Dying on file $file" fatal "Configuration files must not be world writable/readable! Dying on file $file" fi if [ "$gperm" != '---' ]; then case "$admingroup" in $gid|$group) :;; *) if [ "$gid" != 0 ]; then echo "Configuration files must not be writable/readable by group $group! Use the admingroup option in backupninja.conf. Dying on file $file" fatal "Configuration files must not be writable/readable by group $group! Use the admingroup option in backupninja.conf. Dying on file $file" fi ;; esac fi } # simple lowercase function function tolower() { echo "$1" | tr '[:upper:]' '[:lower:]' } # simple to integer function function toint() { echo "$1" | tr -d '[:alpha:]' } # # function isnow(): returns 1 if the time/day passed as $1 matches # the current time/day. # # format is at