#!/bin/sh
#
# Starts dropbear sshd.
#

# Make sure the dropbearkey progam exists
[ -f /usr/sbin/dropbearkey ] || exit 0

FILE_PID=$(/sbin/uci get -q dropbear.FilePaths.PID)
if [ "$FILE_PID" == "" ] ; then
	FILE_PID=/var/run/dropbear.pid
fi

start() {	
	# get start params
	SSH_Port=$(/sbin/uci get -q dropbear.SSH.Port)
	SSH_RootLogin=$(/sbin/uci get -q dropbear.SSH.RootLogin)
	SSH_RootPasswdAuth=$(/sbin/uci get -q dropbear.SSH.RootPasswdAuth)
	SSH_IdleTimeout=$(/sbin/uci get -q dropbear.SSH.IdleTimeout)
	SSH_WindowBuffer=$(/sbin/uci get -q dropbear.SSH.WindowBuffer)
	SSH_KeepAlive=$(/sbin/uci get -q dropbear.SSH.KeepAlive)
	SSH_DisableLocalPortFwd=$(/sbin/uci get -q dropbear.SSH.DisableLocalPortFwd)
	SSH_DisableRemotePortFwd=$(/sbin/uci get -q dropbear.SSH.DisableRemotePortFwd)
	SSH_AllowRemoteHosts=$(/sbin/uci get -q dropbear.SSH.AllowRemoteHosts)
	SSH_DisablePasswdLogins=$(/sbin/uci get -q dropbear.SSH.DisablePasswdLogins)
	
	DROPBEAR_FOLDER=$(/sbin/uci get -q dropbear.FilePaths.dropbear_folder)
	if [ "$DROPBEAR_FOLDER" == "" ] ; then
		DROPBEAR_FOLDER=/etc/dropbear
	fi

	FILE_rsakey=$DROPBEAR_FOLDER/$(/sbin/uci get -q dropbear.FilePaths.rsakey)
	FILE_dsskey=$DROPBEAR_FOLDER/$(/sbin/uci get -q dropbear.FilePaths.dsskey)
	FILE_banner=$DROPBEAR_FOLDER/$(/sbin/uci get -q dropbear.FilePaths.banner)


	DROPBEAR_CMD=/usr/sbin/dropbear
	DROPBEAR_OPTS=""

	# compute the exec command string ...
	if [ "$SSH_Port" != "" ] ; then
		DROPBEAR_OPTS+=" -p $SSH_Port"
	fi
	if [ "$SSH_DisablePasswdLogins" == "1" ] ; then
		DROPBEAR_OPTS+=" -s"
	fi
	if [ "$SSH_RootLogin" == "0" ] ; then
		DROPBEAR_OPTS+=" -w"
	fi
	if [ "$SSH_RootPasswdAuth" == "0" ] ; then
		DROPBEAR_OPTS+=" -g"
	fi
	if [ "$SSH_IdleTimeout" != "" ] ; then
		DROPBEAR_OPTS+=" -I $SSH_IdleTimeout"
	fi
	if [ "$SSH_WindowBuffer" != "" ] ; then
		DROPBEAR_OPTS+=" -W $SSH_WindowBuffer"
	fi
	if [ "$SSH_KeepAlive" != "" ] ; then
		DROPBEAR_OPTS+=" -K $SSH_KeepAlive"
	fi
	if [ "$SSH_DisableLocalPortFwd" == "1" ] ; then
		DROPBEAR_OPTS+=" -j"
	fi
	if [ "$SSH_DisableRemotePortFwd" == "1" ] ; then
		DROPBEAR_OPTS+=" -k"
	fi
	if [ "$SSH_AllowRemoteHosts" == "1" ] ; then
		DROPBEAR_OPTS+=" -a"
	fi

	if [ "$FILE_dsskey" != "" ] ; then
		DROPBEAR_OPTS+=" -d $FILE_dsskey"
	fi

	if [ "$FILE_rsakey" != "" ] ; then
		DROPBEAR_OPTS+=" -r $FILE_rsakey"
	fi

	DROPBEAR_OPTS+=" -P $FILE_PID "
	#DROPBEAR_OPTS+=" -B "

	# Create lastlog to show the users last login
	touch /var/log/lastlog

 	echo -n "Starting dropbear sshd: "
	# Make sure dropbear directory exists
	if [ ! -d $DROPBEAR_FOLDER ] ; then
		mkdir -p $DROPBEAR_FOLDER
	fi
	# Check for the Dropbear RSA key
	if [ ! -f $FILE_rsakey ] ; then
		echo -n "generating rsa key ... "
		/usr/sbin/dropbearkey -t rsa -f $FILE_rsakey > /dev/null 2>&1
	fi

	# Check for the Dropbear DSS key
	if [ ! -f $FILE_dsskey ] ; then
		echo -n "generating dsa key ... "
		/usr/sbin/dropbearkey -t dss -f $FILE_dsskey > /dev/null 2>&1
	fi
	umask 077
	
	start-stop-daemon -S -q -p "$FILE_PID" -x "$DROPBEAR_CMD" -- $DROPBEAR_OPTS

	# the line:
	# [ -s "$FILE_PID" ] && echo "OK" || echo "SSH service FAILED to start"
	# works on command line but not from here, so do it the complicated way:
	PROCESS=$(ps ax | grep "$DROPBEAR_CMD" | grep -v grep)
	if [ "$PROCESS" != "" ] ; then
		echo "OK" 
	else
		echo "SSH service FAILED to start"
	fi
}
stop() {
	echo -n "Stopping dropbear sshd: "
	start-stop-daemon -K -q -p $FILE_PID
	echo "OK"
}
restart() {
	stop
	start
}

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart|reload)
  	restart
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?

