/
share.sh
executable file
·152 lines (127 loc) · 4.4 KB
/
share.sh
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
# Usage: ./share.sh file encryption_key
E_BADARGS=65
TRANSFER_SH_URL="https://transfer.sh"
MAX_DOWNLOADS=1
DAYS_BEFORE_EXPIRATION=1
TMP_FILE="/tmp/$(basename $0).$$.tmp"
# Will be set to 1 if a file is to be downloaded instead of uploaded.
RETRIEVE_MODE=0
###############################################################################
function usage()
{
echo "`basename $0` is a simple script which facilitates secure file transfers."
echo
echo "Upload a file:"
echo " `basename $0` [-m max_downloads] [-d days_before_expiration] file encryption_key"
echo " Example: `basename $0` -m 1 -d 5 ~/secrets.txt \"The!Encryption!Key\""
echo
echo "Retrieve a file:"
echo " `basename $0` -r file encryption_key URL"
}
###############################################################################
# Checks for torify, curl and wget presence.
function detect_capabilities()
{
TORIFY_PRESENT=1
CURL_PRESENT=1
WGET_PRESENT=1
# Check which commands are available on the system.
command -v torify >/dev/null 2>&1 || TORIFY_PRESENT=0
command -v curl >/dev/null 2>&1 || CURL_PRESENT=0
command -v wget >/dev/null 2>&1 || WGET_PRESENT=0
# Default to the onion URL if torify is present on the system.
if [ ${TORIFY_PRESENT} -eq 1 ] ; then
PROXY_COMMAND="torify"
else
PROXY_COMMAND=""
fi
if [ ${CURL_PRESENT} -eq 0 ] && [ ${WGET_PRESENT} -eq 0 ] ; then
echo "Error: neither curl nor wget could be found!"
exit 1
fi
}
###############################################################################
# Uploads the target file to transfer.sh. Uses either wget or curl depending on
# what is available on the system.
function upload()
{
# Compress and encrypt the target file.
gzip -c $1 | openssl enc -aes-256-cbc -k $2 -out ${TMP_FILE}
# Upload the file.
if [ ${CURL_PRESENT} -eq 1 ] ; then
URL=`${PROXY_COMMAND} curl -s -H "Max-Downloads: ${MAX_DOWNLOADS}" -H "Max-Days: ${DAYS_BEFORE_EXPIRATION}" --upload-file ${TMP_FILE} ${TRANSFER_SH_URL}/$$`
elif [ ${WGET_PRESENT} -eq 1 ] ; then
URL=`${PROXY_COMMAND} wget -qO- --header="Max-Downloads: ${MAX_DOWNLOADS}" --header="Max-Days: ${DAYS_BEFORE_EXPIRATION}" --method=PUT --body-file=${TMP_FILE} ${TRANSFER_SH_URL}/$$`
fi
# Verify that the file was uploaded successfully.
if [ ! $? -eq 0 ] ; then
echo "Error uploading $1."
rm ${TMP_FILE}
exit 1
fi
rm ${TMP_FILE}
echo -e "Success! Retrieval command: $0 -r $(basename $1) \"$2\" ${URL}"
exit 0
}
###############################################################################
# Downloads, decrypts and decompresses the file pointed by a URL.
# Usage: download destination_file decryption_key url
function download()
{
# This function has lackluster error handling.
if [ ${CURL_PRESENT} -eq 1 ] ; then
${PROXY_COMMAND} curl -s $3 | openssl enc -d -aes-256-cbc -k $2 | gunzip > $1
elif [ ${WGET_PRESENT} -eq 1 ] ; then
${PROXY_COMMAND} wget -qO- $3 | openssl enc -d -aes-256-cbc -k $2 | gunzip > $1
fi
if [ $? -eq 0 ] ; then
echo "File retrieved successfully!"
else
rm $1 # Delete the empty or corrupted file which was created.
fi
}
###############################################################################
# Assert that there are enough arguments or print usage.
if [ $# -lt 2 ] ; then
usage
exit ${E_BADARGS}
fi
while getopts ":rd:m:h" opt; do
case "$opt" in
r)
RETRIEVE_MODE=1
;;
h)
usage
exit 0
;;
m)
MAX_DOWNLOADS=${OPTARG}
;;
d)
DAYS_BEFORE_EXPIRATION=${OPTARG}
;;
\?)
echo "Invalid option: -${OPTARG}. Use -h for help." >&2
exit ${E_BADARGS}
;;
esac
done
# Upload mode: verify that the target file exists.
if [ ! -e ${@:$OPTIND:1} ] && [ ${RETRIEVE_MODE} -eq 0 ] ; then
echo "${@:$OPTIND:1} not found."
exit ${E_BADARGS}
fi
# Retrieve mode: assert that the target file doesn't already exist.
if [ -e ${@:$OPTIND:1} ] && [ ${RETRIEVE_MODE} -eq 1 ] ; then
echo "Error: ${@:$OPTIND:1} already exists and would be overwritten."
exit ${E_BADARGS}
fi
detect_capabilities
# Launch download or upload.
if [ ${RETRIEVE_MODE} -eq 0 ] ; then
upload ${@:$OPTIND:2}
else
download ${@:$OPTIND:3}
fi