#!/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 # # Options None # AllowOverride None # Order allow,deny # allow from all # # # 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