summaryrefslogtreecommitdiff
path: root/rt_handle_uploaded_key
blob: 99b89c1244a4094731fe59fbb7fc3748d5d69690 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/sh

# rt_handle_uploaded_key: A script to publish tarballs to a web directory
#
# This script (its 'put' command) should be the command of a public
# ssh key used by the support user to upload tarballs to a public web
# server.
# To use it, create a SSH key for the support user and add it to some ssh
# user on the server with a command field:
# command="/usr/local/bin/rt_handle_uploaded_key put" ssh-rsa [...]
#
# On the server side, set the script to copy the tarballs to some
# directory on the wer server, and configure it to allow getting files
# from there (but not browse. E.g. the apache configuration snippet:
#
# Alias /k $TARBALLS_DIR
# <Directory $TARBALLS_DIR>
#   Options None
#   AllowOverride None
#   Order allow,deny
#   allow from all
# </Directory>
#
# Be sure to also delete old tarballs. E.g. a cron job that runs:
#
#  find $TARBALLS_DIR -ctime +3 -exec rm -f '{}' \;
#
# To list all current tarballs, run the command with no extra parameter.
# it will list name, tunnelport, upload time and link URL.

set -e

# Required:
TARGET_DIR="$HOME/ra-tarballs"
# The URL under which tarballs are accessible (for printing it)
BASE_URL="http://updates.xorcom.com/k"

# Emit a random-enough string
rand() {
	dd if=/dev/urandom count=16 2>/dev/null | md5sum | cut -c1-8
}

tar_name() {
	tar xOzf "$1" basename 2>/dev/null || echo NONAME | tr -d -c 'a-zA-Z0-9_'
}

tar_time() {
	 LANG=C tar tvzf "$1" port | awk '{print $4,$5}'
}

tar_port() {
	 tar xOzf "$1" tunnelport 2>/dev/null
}

put_tarball() {
# FIXME: this assumes there will be no collisions
# and no concurrent runs
	rand_str=`rand`
	tar_name="$rand_str.tgz"
	tar_full="$TARGET_DIR/$tar_name"
	tar_url="$BASE_URL/$tar_name"

	dd of="$tar_full" bs=10k count=1 2>/dev/null
	if ! tar tzf "$tar_full" >/dev/null; then
		echo >&2 "Failed upload. Deleting."
		rm "$tar_full"
	fi
	name=`tar_name "$tar_full"`
	port=`tar_port "$tar_full"`
	echo "$port, $name: $tar_url"
}

list() {
	for tarball in $TARGET_DIR/*.tgz; do
		echo $tarball
		port=`tar_port "$tarball"`
		name=`tar_name "$tarball"`
		base_name="${tarball##*/}"
		url="$BASE_URL/$base_name"
		time=`tar_time "$tarball"`
		printf "$name\t$port\t$time $url\n"
		
	done
}

case "$1" in
	put) put_tarball ;;
	*) list ;;
esac