blob: 06427ebf895ba42fa5961017a02471f27e21ab3d [file] [log] [blame] [raw]
#!/bin/sh
# Drop server
# Copyright 2015-2019 Rivoreo
# 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.
DROP_TARGET=drop
# Usage: drop { [-m <mode>] <file-name> | -c "drop [-m <mode>] <file-name>" }
set -f
set -e
[ -f /etc/drop.cfg ] && . /etc/drop.cfg
[ -f "$HOME/.config/drop.cfg" ] && . "$HOME/.config/drop.cfg"
if [ $# = 2 -a "$1" = -c ] || { [ $# = 0 ] && [ -n "$SSH_ORIGINAL_COMMAND" ]; }; then
[ $# = 2 ] && set -- $2 || set -- $SSH_ORIGINAL_COMMAND
if [ $# -lt 2 -o "$1" != drop ]; then
echo "incorrect command line" 1>&2
exit 255
fi
shift
elif [ $# -lt 1 ]; then
echo "incorrect command line" 1>&2
exit 255
fi
mode=
if [ $# = 3 -a "$1" = -m ]; then
mode="$2"
shift 2
elif [ $# != 1 ]; then
echo "incorrect command line" 1>&2
exit 255
fi
if [ "$1" = . -o "$1" = .. ] || printf %s | grep -Fq /; then
echo "invalid file name" 1>&2
exit 1
fi
[ -d "$DROP_TARGET" ] || mkdir "$DROP_TARGET"
# XXX; need a portable way to get msec
new_file_name="`date -u +%F.%H-%M-%S`.$1"
# TODO: database
if [ -n "$mode" ]; then
chmod -- "$mode" "$DROP_TARGET/$new_file_name" > "$DROP_TARGET/$new_file_name"
fi
exec cat > "$DROP_TARGET/$new_file_name"