t4t

minimal, consent-based webpaste
git clone git://git.girlpoison.org/t4t
Log | Files | Refs | README | LICENSE

t4t-receive (2423B)


      1 #!/bin/bash
      2 PROGNAME="$(basename $0)"
      3 
      4 panic() { echo "$PROGNAME: $*" >&2; exit 1; }
      5 usage() { panic "usage: $PROGNAME [-B blacklist] [-d dir] [-l limit] [-M | -m mimefile]"\
      6        "[-r root] [-S | -s profile] [-t tmp] [-u url] name"; }
      7 randstring() { tr -dc 'A-Za-z0-9_\-' </dev/urandom | head -c ${1-8}; }
      8 clean() { [ -e "$TMPFILE" ] && rm "$TMPFILE"; }
      9 trap 'clean; exit' INT TERM EXIT QUIT HUP
     10 
     11 OPTS=("BLACKLIST DETECT DIR LIMIT MIME PROFILE ROOT TMP URL")
     12 unset $OPTS
     13 DETECT=1; SOURCE=1
     14 while getopts "B:d:l:Mm:r:Ss:t:u:" OPT; do
     15 	case ${OPT} in
     16 	B) BLACKLIST="${OPTARG}";;
     17 	d) DIR="${OPTARG}";;
     18 	l) LIMIT="${OPTARG}";;
     19 	M) DETECT=0;;
     20 	m) MIME="${OPTARG}";;
     21 	r) ROOT="${OPTARG}";;
     22 	S) SOURCE=0;;
     23 	s) PROFILE="${OPTARG}";;
     24 	t) TMP="${OPTARG}";;
     25 	u) URL="${OPTARG}";;
     26 	*) usage;;
     27 	esac
     28 done; shift $((OPTIND-1))
     29 
     30 [ $SOURCE -ne 0 ] && { source "${PROFILE-"$HOME/.profile"}" || exit 1; }
     31 for V in $OPTS; do eval "$(printf 'T4T_%s=${%s-"$T4T_%s"}\n' "$V" "$V" "$V")"; done
     32 
     33 [ "$T4T_ROOT" ] || panic "T4T_ROOT not set.";
     34 [ -d "$T4T_ROOT" ] || panic "T4T_ROOT: '$T4T_DIR' no such directory"
     35 [ "$T4T_DIR" ] && [ ! -d "$T4T_ROOT/$T4T_DIR" ] && panic "T4T_DIR: '$T4T_ROOT/$T4T_DIR' no such directory"
     36 TARGETDIR="$T4T_ROOT${T4T_DIR:+"/$T4T_DIR"}"
     37 [ "$T4T_URL" ] || panic "T4T_URL not set."
     38 [ "${T4T_MIME:="/usr/share/t4t/mime.types"}" ] && [ ! -f "$T4T_MIME" ] && panic "T4T_MIME: no such file '$T4T_MIME'"
     39 [ -d "${T4T_TMP:=/tmp}" ] || panic "T4T_TMP: '$T4T_TMP' no such directory"
     40 T4T_BLACKLIST=("$T4T_BLACKLIST")
     41 
     42 NAME="$(tr -dc 'A-Za-z0-9_\-.' <<<"${SSH_ORIGINAL_COMMAND:-"$*"}")"
     43 [ "$NAME" ] || NAME="$(randstring 4)"
     44 EXT="$(grep -Eo '\.[a-zA-Z0-9]+$' <<<"$NAME")"
     45 BNAME="$(basename ${EXT:+-s$EXT} "$NAME")"
     46 
     47 for E in $T4T_BLACKLIST; do [ "$EXT" == "$E" ] && unset EXT; done
     48 
     49 TMPFILE="$T4T_TMP/t4t-$(randstring 8)"
     50 head -c ${T4T_LIMIT:--1} > "$TMPFILE"
     51 [ -e "$TMPFILE" ] || panic "could not write temporary file"
     52 [ $(du "$TMPFILE" | cut -f1) -eq 0 ] && { panic "empty file discarded"; }
     53 
     54 if [ $DETECT -ne 0 ] && [ ! "$EXT" ]; then
     55 	TYPE="$(file -b --mime-type "$TMPFILE")"
     56 	EXT=".$(cat "$T4T_MIME" | awk -v t="$TYPE" '$1==t{print $NF; exit}')"
     57 	[ "$EXT" == "." ] && EXT=".txt"
     58 fi
     59 
     60 # security note: test -e $TARGETDIR/. and $TARGETDIR/.. always eval to true
     61 while test -e "$TARGETDIR/$BNAME$EXT"; do BNAME="$BNAME$(randstring 1)"; done
     62 mv "$TMPFILE" "$TARGETDIR/$BNAME$EXT"
     63 
     64 echo "$T4T_URL/${T4T_DIR:+"$T4T_DIR/"}$BNAME$EXT"