# SliTaz package receipt.

PACKED_SIZE="56.0K"
UNPACKED_SIZE="136.0K"
PACKAGE="tinyssh"
VERSION="20190101"
CATEGORY="security"
SHORT_DESC="Light SSH server with key authentication only"
MAINTAINER="pascal.bellard@slitaz.org"
LICENSE="PublicDomain"
TARBALL="$PACKAGE-$VERSION.tar.gz"
[ -n "$TARGET" ] || TARGET="i486"
DEPENDS="busybox-net"
BUILD_DEPENDS="uclibc-cross-compiler-$TARGET"
WEB_SITE="https://tinyssh.org/"
WGET_URL="https://github.com/janmojzis/tinyssh/archive/$VERSION.tar.gz"
CONFIG_FILES="/etc/tinyssh"
TAGS="ssh"

# Rules to configure and make the package.
compile_rules()
{
	make &&
	make DESTDIR=$DESTDIR install
}

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
	mkdir -p $fs/usr $fs/etc/tinyssh
	cp -a $install/usr/sbin $fs/usr
	for i in $fs/usr/sbin/tinysshd-*; do
		ln -f $fs/usr/sbin/tinysshd $i
	done
}

# Post message when installing.
post_install()
{
	if [ "$STARTDAEMON" = "ON" ]; then
		grep -q inetd $1/etc/rcS.conf ||
		sed -i 's/^RUN_DAEMONS="/&inetd /' $1/etc/rcS.conf
		grep -q dropbear $1/etc/inetd.conf || cat >> $1/etc/inetd.conf <<EOT
ssh	stream	tcp	nowait	root	/usr/sbin/tinysshd	tinysshd -s -o -l -v /etc/tinyssh/sshkeydir
EOT
	fi
	dos2unix <<EOT | uudecode - > $1/etc/tinyssh/sshkeydir/ed25519.pk
$PUBKEY
EOT
	dos2unix <<EOT | uudecode - > $1/etc/tinyssh/sshkeydir/.ed25519.sk
$SECKEY
EOT
	chmod 600 $1/etc/tinyssh/sshkeydir/.ed25519.sk
	if [ "$REMOTEUSERS" ]; then
		mkdir $1/root/.ssh
		cat >> $1/root/.ssh/authorized_keys <<EOT
$REMOTEUSERS
EOT
		chmod 600 $1/root/.ssh/authorized_keys
	fi
}

config_form()
{
	mkdir /tmp/mktinyssh$$
	if [ -z "$PUBKEY" ]; then
		tinysshd-makekey /tmp/mktinyssh$$ > /dev/null 2>&1
		PUBKEY="$(uuencode -m - < /tmp/mktinyssh$$/ed25519.pk)"
		SECKEY="$(uuencode -m - < /tmp/mktinyssh$$/.ed25519.sk)"
	fi
	uudecode - > /tmp/mktinyssh$$/ed25519.pk <<EOT
$PUBKEY
EOT
	uudecode - > /tmp/mktinyssh$$/.ed25519.sk <<EOT
$SECKEY
EOT
	cat <<EOT
<input type="checkbox" name="STARTDAEMON" value="ON" ${STARTDAEMON:+checked="checked" }/>
Start server during boot<br>
Tinyssh use public-key authentication only (no password or hostbased authentication)
You should declare the public keys for each remote user
<textarea name="REMOTEUSERS" cols="60" rows="13" wrap="off">
$REMOTEUSERS
</textarea>
<br>
Current public key is <pre>$(tinysshd-printkey /tmp/mktinyssh$$)</pre>
<table>
<td>Public key</td>
<td><textarea name="PUBKEY" cols="60" rows="13" wrap="off">
$PUBKEY
</textarea></td>
</tr>
<tr>
<td>Secret key</td>
<td><textarea name="SECKEY" cols="60" rows="12" wrap="off">
$SECKEY
</textarea></td>
</tr>
</table>
EOT
	rm -rf /tmp/mktinyssh$$
}