-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsgdrop
More file actions
executable file
·307 lines (273 loc) · 7.92 KB
/
msgdrop
File metadata and controls
executable file
·307 lines (273 loc) · 7.92 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/sh
#
# 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 3 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2023 Jeremy Brubaker <jbru362@gmail.com>
#
# abstract: use https://ntfy.sh to send and receive encrypted messages
#
# Defaults {{{1
#
# Input data increases in size before sending because it is base64-encoded,
# encrypted and base64-encoded again. MAX_MSGLEN is based on how much of that
# encoded data can be sent at once. It is slightly lower than what ntfy.sh
# actually accepts
MAX_MSGLEN=900
MIN_MSGLEN=50
MIN_DELAY=0
DFLT_KEYPAIR=msgdrop
DFLT_MSGLEN=140
DFLT_DELAY=1
DFLT_KEYDIR=${XDG_CONFIG_HOME:-$HOME/.config}/msgdrop
# Documentation {{{1
#
VERSION='1.0'
print_help() (
[ -n "$2" ] && printf "%s\n" "$2"
case $1 in
send) # {{{
cat <<END
Usage: $0 send [OPTION] [KEYNAME] [FILE]...
Encrypt and send the cat'd contents of [FILE]... or stdin if no files are given
KEYNAME is required and is the path to the recipients age(1) public key, without
any extension
Options:
-d [DELAY] seconds to delay between successive message chunks (Default = $DFLT_DELAY)
-l [LENGTH] maximum length of a message chunk. Must be between $MIN_MSGLEN and $MAX_MSGLEN
(Default = $DFLT_MSGLEN)
-h display this help and exit
END
;; # }}}
recv) # {{{
cat <<END
Usage: $0 recv [OPTION] -k [KEYPAIR]
Read messages and send them to stdout
Options:
-a read all messages
-k [KEYPAIR] KEYPAIR is the path to the keypair to use for decryption, minus
extension. (Default = $DFLT_KEYPAIR)
-h display this help and exit
END
;; # }}}
genkey) # {{{
cat <<END
Usage: $0 genkey [OPTION]
Create a keypair
Options:
-k [KEYPAIR] the created keypair will be named KEYPAIR.{priv,pub}
(Default = $DFLT_KEYPAIR)
-h display this help and exit
END
;; # }}}
*) # {{{
cat <<END
Usage: $0 [OPTION] [COMMAND]
Use https://ntfy.sh to send and receive encrypted messages
Options:
-d DIR where to look for keypairs (default: $DFLT_KEYDIR)
-h display this help and exit
-v output version information and exit
Commands:
send
recv
genkey
END
;; # }}}
esac
) >&2
print_version() (
cat <<END
$0 $VERSION
Copyright (C) 2023
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jeremy Brubaker.
END
) >&2
# Utility functions {{{1
#
isnum() {
case "${1#[-+]}" in
(*[!0-9]*|'') false ;;
(*) true ;;
esac
}
iswriteabledir() { [ -d "$1" ] && [ -w "$1" ]; }
error() { printf "%s\n" "$1" >&2; exit 1; }
# Process command line {{{1
#
keydir=$DFLT_KEYDIR
while getopts ':d:hv' c; do
case $c in
d) keydir=$OPTARG ; iswriteabledir "$keydir" || error "$keydir is not a writeable directory" ;;
h) print_help; exit 0 ;;
v) print_version; exit 0 ;;
?) print_help "" "Invalid option: -$OPTARG"; exit 1 ;;
esac
done
shift $((OPTIND-1))
[ -d "$keydir" ] || mkdir -p "$keydir"
case $1 in
send)
cmd=$1; shift
msglen=$DFLT_MSGLEN
delay=$DFLT_DELAY
while getopts ':d:l:h' c; do
case $c in
d) delay=$OPTARG ; isnum "$delay" || error "$delay is not a number" ;;
l) msglen=$OPTARG ; isnum "$msglen" || error "$msglen is not a number" ;;
h) print_help send; exit 0 ;;
?) print_help "Invalid option: -$OPTARG"; exit 1 ;;
esac
done
shift $((OPTIND-1))
if [ "$msglen" -gt "$MAX_MSGLEN" ] || [ "$msglen" -lt "$MIN_MSGLEN" ]; then
msglen=$MIN_MSGLEN
fi
[ "$delay" -lt "$MIN_DELAY" ] && delay=$MIN_DELAY
[ -n "$1" ] || error "No keypair provided"
keypair=$keydir/$1
[ -r "$keypair.pub" ] || error "Cannot read recipient key: $keypair"
shift
;;
recv)
cmd=$1; shift
all=
keypair=$DFLT_KEYPAIR
while getopts ':ak:h' c; do
case $c in
a) all=y ;;
k) keypair=$OPTARG ;;
h) print_help recv; exit 0 ;;
?) print_help "Invalid option: -$OPTARG"; exit 1 ;;
esac
done
shift $((OPTIND-1))
keypair=$keydir/$keypair
[ -r "$keypair.priv" ] || [ -r "$keypair.pub" ] ||
error "Cannot read your keypair: $keypair"
;;
genkey)
cmd=$1; shift
keypair=$DFLT_KEYPAIR
while getopts ':k:h' c; do
case $c in
k) keypair=$OPTARG ;;
h) print_help genkey; exit 0 ;;
?) print_help "Invalid option: -$OPTARG"; exit 1 ;;
esac
done
shift $((OPTIND-1))
keypair=$keydir/$keypair
;;
*)
printf "Invalid command: %s\n" "$1" >&2
print_help
exit 1
;;
esac
# Functions {{{1
#
# getid {{{2
#
# @description Return a string to use as a ntfy.sh topic ID
#
# @arg $1 string Path to keyfile to use as topic ID
#
# @return string "random" string
#
getid() { md5sum < "$1" | cut -d' ' -f1; }
# have {{{2
#
# @description Check for a binary in PATH
#
# @arg $1 string Binary to search for
#
# @exitcode true if binary is in PATH, otherwise false
#
have() { command -v "$1" >/dev/null; }
# send {{{2
#
# @description Encrypt and send contents of files using ntfy.sh
#
# @arg $@ list of filenames to encrypt and send. Use stdin if no filenames are
# given. The entire message can be broken into chunks
#
# @global keypair string Path to recipient's public key, minus extension
# @global msglen int Size of input message chunks
# @global delay int Seconds to delay between sending message chunks
#
# @return string "random" string
#
send() {
cat "$@" |
while s=$(dd ibs=1 count="$msglen" 2>/dev/null | base64); do
[ -z "$s" ] && break
echo "$s"
sleep "$delay"
done | xargs -I{} sh -c "
echo {} |
age -R $keypair.pub |
base64 |
curl -d@- ntfy.sh/$(getid "$keypair".pub)"
}
# recv {{{2
#
# @description Read and decrypt ntfy.sh messages
#
# @noarg
#
# @global all string Get all messages if == "y"
# @global keypair string Path to your keypair
#
# @stdout Decrypted messages
#
recv() {
if [ -f "$keypair.mark" ] && [ "$all" != "y" ]; then
now=$(stat --printf %Y "$keypair.mark")
human_now=$(stat --printf %y "$keypair.mark")
since="&since=$now"
# Set mark file mtime
touch -d "$human_now" "$keypair.mark"
else
touch "$keypair.mark"
fi
curl -s "ntfy.sh/$(getid "$keypair.pub")/raw?poll=1$since" |
xargs -I{} sh -c "
echo {} |
base64 -d |
age -d -i '$keypair.priv' 2>/dev/null |
base64 -d
exit 0"
}
# genkey {{{2
#
# @description Generate an age(1) keypair
#
# @noarg
#
# @global keypair string Path to keypair to create, minus extension
#
genkey() {
true > "$keypair.priv"
chmod 0600 "$keypair.priv"
age-keygen 2>/dev/null | tee "$keypair.priv" | age-keygen -y > "$keypair.pub"
}
# Main {{{1
#
for b in age age-keygen base64 curl md5sum stat; do
have "$b" || error "'$b' is required and not found"
done
$cmd "$@"