#!/bin/sh
# ============================================================================
#
# This file is part of the 'Dropbear for zypkgs' package
# 
# 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, 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.
#
# Author: Mijzelf <Mijzelf@live.com>
#
# ============================================================================
#
PKGNAME="Dropbear"
PKG_ROOT="/usr/local/zy-pkgs"
CONFIG_DIR="${PKG_ROOT}/config/${PKGNAME}"
CONFIG_FILE="${CONFIG_DIR}/config"

IsUpgrading()
{
	if cat ${PKG_ROOT}/tmp/zypkg.log | grep "\[Dropbear\]" | tail | grep "Upgrade Mode" >/dev/null
	then
		return 0
	fi
	return 1
}

Log()
{
	[ -f /tmp/${PKGNAME}.log ] && echo "` date `: $@" >>/tmp/${PKGNAME}.log
}

GetLink()
{
	echo
}

Status()
{
	if [ ! -f ${CONFIG_FILE} ] ; then
		mkdir -p ${CONFIG_DIR}
		echo "Disabled" >${CONFIG_FILE}
	fi
	cat ${CONFIG_FILE}
}

Startup()
{
	local STATUS=` Status `
	if [ "$STATUS" != "Enabled" ] ; then 
		return 0
	fi

# Dropbear server v2014.63 https://matt.ucc.asn.au/dropbear/dropbear.html
# Usage: ./dropbear [options]
# -b bannerfile	Display the contents of bannerfile before user login
# 		(default: none)
# -r	keyfile  Specify hostkeys (repeatable)
# defaults: 
#	dss /etc/dropbear/dropbear_dss_host_key
#	rsa /etc/dropbear/dropbear_rsa_host_key
#	ecdsa /etc/dropbear/dropbear_ecdsa_host_key
# -R		Create hostkeys as required
# -F		Don't fork into background
# -E		Log to stderr rather than syslog
# -m		Don't display the motd on login
# -w		Disallow root logins
# -s		Disable password logins
# -g		Disable password logins for root
# -B		Allow blank password logins
# -j		Disable local port forwarding
# -k		Disable remote port forwarding
# -a		Allow connections to forwarded ports from any host
# -p 		[address:]port
#	Listen on spefcified tcp port (and optionally address),
#	up to 10 can be specified
#	(default port is 22 if none specified)
# -P PidFile	Create pid file PidFile
# 	(default /var/run/dropbear.pid)
# -i		Start for inetd
# -W <receive_window_buffer> (default 24576, larger may be faster, max 1MB)
# -K <keepalive>  (0 is never, default 0, in seconds)
# -I <idle_timeout>  (0 is never, default 0, in seconds)
# 
	local keyroot=${PKG_ROOT}/etc/dropbear/
	mkdir -p ${keyroot}
	ln -s ${keyroot} /etc/dropbear
	
	for shell in /bin/sh /bin/bash /ffp/bin/sh /ffp/bin/bash
	do
		if ! grep ^${shell} /etc/shells >/dev/null 2>&1
		then
			echo ${shell} >>/etc/shells
		fi
	done
	
	ln -s /usr/local/zy-pkgs/bin/scp /bin/scp
	
	${PKG_ROOT}/sbin/dropbear -R -a -P /var/run/dropbear.pid 
}

Shutdown()
{
	if [ -f /var/run/dropbear.pid ] ; then
		kill ` cat /var/run/dropbear.pid `
	fi
}

Disable()
{
	mkdir -p ${CONFIG_DIR}
	echo "Disabled" >${CONFIG_FILE}
	Shutdown
}

Enable()
{
	mkdir -p ${CONFIG_DIR}
	echo "Enabled" >${CONFIG_FILE}
	Startup 
}

Log $0 $@

case $1 in
	getlink)
		GetLink
		;;
	status)
		Status
		;;
	startup)
		Startup
		;;
	shutdown)
		Shutdown
		;;
	enable)
		Enable
		;;
	disable)
		Disable
		;;
esac

exit 0