#!/bin/sh
# autosshd-conf      shell for autosshd that export variables from root to _anyssh user


init_autossh(){
    # Source function library.
    . /etc/init.d/functions

    # AutoSSHDaemon configuration
    SYSCONFIGFILE="/etc/sysconfig/autosshd"
    SourceIfNotEmpty $SYSCONFIGFILE
    AUTOSSH_PIDFILE=${PIDFILEDIR}/$(basename $1 .conf).pid
    AUTOSSH_LOCKFILE=${LOCKFILEDIR}/$(basename $1 .conf).lck
    AUTOSSH_LOGFILE=/var/lib/autosshd/$(basename $1 .conf).log
    export AUTOSSH_LOGFILE AUTOSSH_LOCKFILE AUTOSSH_PIDFILE AUTOSSH_LOGLEVEL AUTOSSH_POLL AUTOSSH_GATETIME AUTOSSH_DEBUG

    VERBOSE=false
}

check_permissions(){
    AUTOSSH_FILES="$AUTOSSH_LOGFILE" # "$AUTOSSH_LOCKFILE $AUTOSSH_PIDFILE"
    for var in $AUTOSSH_FILES ; do
	if  [ -w "$var" ] || [ ! -e "$var" ]; then
	    is_ok $var
	else
	    echo "Remove $var file"
	    #sudo rm -f $var
	fi
    done

    DSAFILES="~/.ssh/id_dsa*"
    if ls $DSAFILES &> /dev/null ; then
	is_ok $DSAFILES
    else
	echo "No dsa files in $(realpath $DSAFILES)"
    fi
    
    for i in $DSAFILES ; do
	if [ "$(stat -c "%a" $i)" == "600" ]; then
	    is_ok $i
	else
	    echo "Permissions $(stat -c "%a" $i) for $i are too open."
	fi
    done
}

is_ok(){
    $VERBOSE && echo "File $1 permission is ok"
}

run_autossh(){
    autossh $@
}

#Fix for import init function
if [ -n "$1" ] ; then
    init_autossh $1
    check_permissions
    shift
    run_autossh $@
fi


