Debian Codegraph @

README.md

raw symbols SCIP
1 Restricted sendmail command
2 ===========================
3
4 A safer sendmail command to send email without passwords, over SSH.
5
6 Objective
7 ---------
8
9 This command aims at replacing the builtin `sendmail` command which
10 gives too much privileges to the caller. For example, [Postfix](http://www.postfix.org/)'s
11 [sendmail(1)](http://www.postfix.org/sendmail.1.html) command can list the mail queue (`-bp`), rehash the
12 alias database (`-bi`), start a daemon (`-bl`, `-bd`), or flush the
13 queue (`-q`); all remnants of the old [Sendmail](https://en.wikipedia.org/wiki/Sendmail) binary, which
14 probably is Turing-complete on its own.
15
16 Instead, rsendmail can easily queue mails on a system without giving
17 any extra privileges to the client. In turn, this makes configuring a
18 satellite system like a laptop or a workstation as simple as adding an
19 SSH key to an `authorized_keys` file. That key can then send email,
20 but *only* send email: no shell access or server management.
21
22 This can of course be accomplished by a regular SMTP client, but that
23 requires passwords, and passwords are weak.
24
25 Quickstart
26 ----------
27
28 scp rsendmail.py example.net:/usr/local/bin/rsendmail
29
30 Wherever you would call `sendmail`, you can now call this instead:
31
32 ssh example.net rsendmail
33
34 See below for instructions on how to add a queue for when you're
35 offline, restrict the connection to rsendmail, or integrate with
36 existing MTAs.
37
38 Installation
39 ------------
40
41 This system is made of two parts:
42
43 * `rsendmail.py` - a wrapper script installed on a remote SSH server
44 that restricts the connection to only accepting and relaying mail
45
46 * `sshsendmail.py` - a local [MDA][] that acts as a compatibility shim
47 with the remote rsendmail. this part is optional, as you'll see
48 below.
49
50 [MDA]: https://en.wikipedia.org/wiki/Mail_delivery_agent
51
52 ### Basic configuration
53
54 The following assumes your relay host is `example.net` and is already
55 configured to accept SSH connections on a user called `rsendmail`. It
56 also assumes there's an email `devnull@localhost` that accepts
57 delivery.
58
59 1. find the `$PATH` on the remote host:
60
61 ssh [email protected] 'echo $PATH'
62
63 2. install `rsendmail.py` somewhere in your `$PATH` as `rsendmail`:
64
65 scp rsendmail.py [email protected]:/usr/local/bin/rsendmail
66
67 3. generate an SSH key for rsendmail:
68
69 ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_rsendmail
70
71 4. copy the key to your `authorized_keys` file:
72
73 ( printf 'command="rsendmail",restrict '; cat ~/.ssh/id_ed25519_rsendmail.pub ) | ssh [email protected] 'cat >> .ssh/authorized_keys'
74
75 5. send a test email:
76
77 printf "Subject: test\n\nThis is a test" | ssh -i ~/.ssh/id_ed25519_rsendmail [email protected] rsendmail devnull@localhost
78
79 6. verify the mail was properly delivered and the message content is
80 complete. if so, then `rsendmail` is properly configured
81
82 Now you can send email, but there are some bits missing. Most tools
83 will expect a `sendmail` command to be available and you might want to
84 queue up mails locally to avoid failing when the network is not
85 available. So you need some sort of wrapper, a [MDA][] to actually
86 deliver the mail in a standard way. Queuing will be handled by a
87 [MTA][] that will call the MDA.
88
89 [MTA]: https://en.wikipedia.org/wiki/Message_transfer_agent
90
91 MDA configuration
92 -----------------
93
94 So the next step is to setup a local MDA to talk with
95 `rsendmail`. Here's a quick comparison of the possible configurations
96 documented below:
97
98 | MDA | Advantages | Disadvantages |
99 | ---------- | ---------- | ------------- |
100 | Standalone | Simplest | Single user, no queue |
101 | Nullmailer | Minimalist | Unusual standard, reliability concerns |
102 | Postfix | Well-known | Queue expires |
103
104 Integration with other MTAs are quite possible as well and
105 documentation to accomplish that is welcome.
106
107 ### Standalone
108
109 The simplest configuration is to use a simple wrapper script for an
110 MDA, without any other MTA. For example, here is the content of a
111 possible `sendmail` command:
112
113 #!/bin/sh -e
114
115 exec ssh -i /var/mail/.ssh/id_ed25519 [email protected] rsendmail "$@"
116
117 The above assumes the private key is stored in the `~mail` home
118 directory. The private key needs to be readable by all callers of the
119 command, which might be a security issue for multi-user systems. This
120 also assumes a `rsendmail` user was created on the remote system.
121
122 ### Nullmailer compatibility
123
124 [Nullmailer][] is a "simple relay-only mail transport agent" which
125 some people use to queue up mails locally when the network is
126 unavailable. We can't use a simple wrapper like the above because
127 nullmailer has a non-standard way of passing recipients to MDA. This
128 is where the `sshsendmail.py` wrapper comes in.
129
130 [Nullmailer]: https://untroubled.org/nullmailer/
131
132 1. generate an SSH key for the `mail` user:
133
134 sudo -u mail ssh-keygen -t ed25519
135
136 2. make sure the remote server identity is verified:
137
138 sudo -u mail ssh [email protected] true
139
140 3. install the `nullmailer` package, version at least 2.0:
141
142 apt install -t buster nullmailer
143
144 4. deploy the MDA wrapper:
145
146 install sshsendmail.py /usr/lib/nullmailer/sshsendmail
147
148 5. add it as a remote in `/etc/nullmailer/remotes`:
149
150 example.net sshsendmail --mta=nullmailer --identity=/var/mail/.ssh/id_ed25519 --user=rsendmail
151
152 Again, adapt the `example.net` host and `rsendmail` user to your
153 configuration.
154
155 I have found the wire protocol used by nullmailer to be rather
156 unusual. It seems to be completely non-standard which was annoying to
157 deal with. Worse, the above instructions will only work with
158 Nullmailer 2.x - previous versions had a different protocol which is
159 not supported here. Furthermore, I have concerns over the reliability
160 of the software: during tests, nullmailer segfaulted while failing to
161 handle a bug in `rsendmail`...
162
163 ### Postfix compatibility
164
165 Postfix can talk to a remote `rsendmail` server easily through the
166 [pipe](http://www.postfix.org/pipe.8.html) service. Here are the steps to configure a Postfix client,
167 once `rsendmail` is installed on a server and the `authorized_keys` is
168 setup:
169
170 1. Install Postfix
171
172 apt-get install postfix
173
174 2. configure it as a `satellite` system and use the recommended
175 hostname. as a `relayhost`, use the hostname (and username!)
176 of the SSH server, e.g. `[email protected]`, in other words:
177
178 postconf -e '[email protected]'
179
180 3. configure the `pipe` service in `/etc/postfix/master.cf`:
181
182 rsendmail unix - n n - - pipe
183 user=mail argv=ssh ${nexthop} rsendmail -f ${sender} ${recipient}
184
185 4. configure that transport as the default relay:
186
187 postconf -e 'default_transport=rsendmail:'
188
189 5. Make sure the `mail` user can login to the relay server
190 automatically and send mail:
191
192 sudo -u mail ssh [email protected] rsendmail devnull@localhost < /dev/null
193
194 6. The above will ask for host verification. Once that works, reload
195 Postfix, which should start relaying mail through the other
196 server:
197
198 postfix reload
199
200 Note that the above configuration will bounce messages if SSH cannot
201 reach the remote server. That is because SSH returns non-standard (as
202 per `sysexits.h`) error codes (i.e. `255` on failure) which Postfix
203 cannot directly parse. To handle this correctly, the `sshsendmail.py`
204 wrapper can be installed instead, again in `master.cf`:
205
206 rsendmail unix - n n - - pipe
207 user=mail argv=/usr/local/bin/sshsendmail --host ${nexthop} -f ${sender} ${recipient}
208
209 Example:
210
211 avr 23 20:38:06 curie postfix/pickup[28657]: 61947125AA4: uid=0 from=<root>
212 avr 23 20:38:06 curie postfix/cleanup[28716]: 61947125AA4: message-id=<[email protected]>
213 avr 23 20:38:06 curie postfix/qmgr[28658]: 61947125AA4: from=<[email protected]>, size=386, nrcpt=1 (queue active)
214 avr 23 20:38:06 curie postfix/pipe[28718]: 61947125AA4: to=<[email protected]>, relay=rsendmail, delay=0.49, delays=0.03/0/0/0.46, dsn=2.0.0, status=sent (delivered via rsendmail service (sending message through command: ['sendmail', '-f',
215 avr 23 20:38:06 curie postfix/qmgr[28658]: 61947125AA4: removed
216
217 Note that Postfix bounces emails from the queue after 5 days. If you
218 stay offline longer than that period, you might want to tweak the
219 [`maximal_queue_lifetime`](http://www.postfix.org/postconf.5.html#maximal_queue_lifetime) setting to something larger:
220
221 postconf -e maximal_queue_lifetime=30d
222 postfix reload
223
224 Implementation details
225 ----------------------
226
227 We drastically restrict the number of options accepted from
228 `sendmail`. Only those options are considered valid:
229
230 * `<recipient> [ <recipient> [ ... ] ]` - email addresses to send the
231 email to. those cannot start with a dash and must not contain
232 spaces. each email must be passed as its own separate argument to
233 rsendmail
234 * `-t`: deduce recipients from the `To` or `Cc` email headers. This
235 is passed directly to the underlying sendmail command, no parsing
236 is done by rsendmail directly. This assumes there is no
237 vulnerability in the `-t` option on the other side.
238 * `-f <sender>`: Set the envelope sender address. This is the
239 address where delivery problems are sent to.
240 * `-oi`: Do not treat `.` on its own line specially.
241
242 The following options are deliberately ignored, even though they might
243 eventually be implemented:
244
245 * `-R <return>` and `-N <dsn>`: we do not really care about
246 status. just accept the default from the remote server.
247 * `-r <sender>`: same as `-f`
248 * `-v`: might be useful in the future, but keeping it simple for now
249
250 All other options will cause an error or might be ignored in the
251 future for backwards compatibility purposes, but should never have an
252 effect. Unless otherwise noted, `sendmail` arguments in this document
253 refer to the Postfix [sendmail(1)](http://www.postfix.org/sendmail.1.html) manual page.
254
255 The `mail` logging facility is used to send messages to syslog.
256
257 Pitfalls and caveats
258 --------------------
259
260 * `sshd` makes some noises about `no-pty` and `command=` regarding
261 8-bit clean channels. we assume an 8-bit clean channel, so make
262 sure the `authorized_keys` file has a `no-pty` setting. best is to
263 use the `restrict` argument, but that is available only starting
264 from OpenSSH 7.2
265 * creating a dedicated user might be more appropriate than reusing a
266 privileged account.
267 * Emacs' `sendmail-send-it` function will fail if there is *any*
268 output from the sendmail command, if `mail-interactive` is enabled
269 (the default). This means changing the log level to anything more
270 verbose than `WARNING` will cause Emacs to think there is a failure
271 even if the email is actually sent. This will mean `Fcc` will fail
272 as well and multiple emails be sent if the user doesn't realize the
273 problem.
274 * Armstrong's script uses a (MD5) checksum to ensure the message's
275 integrity. This was introduced in [this commit](https://git.donarmstrong.com/?p=bin.git;a=commitdiff;h=0c9b112effdd642db560859dbae5fb77d1bfea56) as a way to
276 "avoid having a dropped connection send a truncated file". We do
277 not know if rsendmail suffers from this bug.
278
279 Prior art
280 ---------
281
282 * [LMTP](https://en.wikipedia.org/wiki/Local_Mail_Transfer_Protocol) somewhat does what we want here, but there's not a real
283 client that we can run on the other side, so it's not really useful.
284
285 * [msmtp](http://msmtp.sourceforge.net/) is pretty close to what we need, but only talks SMTP, which
286 means storing secrets on the client. We *could* try to pipe an SMTP
287 socket through the SSH connection, but that feels rather messier and
288 less general-purpose-y. It also does not have a local queue.
289
290 * [nullmailer](https://packages.debian.org/sid/nullmailer) is *almost* what we need, but still talks SMTP.
291
292 * [dma](https://github.com/corecode/dma) (DragonFly Mail Agent) is similar and does weird things like
293 modifying the message in flight (e.g. removing `Bcc`).
294
295 * [esmtp](http://esmtp.sourceforge.net/) is more of the above and "no longer being maintained"
296 (accessed on 2018-04-21)
297
298 * [ssmtp](https://tracker.debian.org/pkg/ssmtp) is similar to msmtp except it has no active upstream out of
299 Debian.
300
301 * [masqmail](http://marmaro.de/prog/masqmail/) is yet more of the above, except it seems to have its
302 own alias database and other complicated stuff.
303
304 * [UUCP](https://en.wikipedia.org/wiki/UUCP) (Unix-to-Unix CoPy) is designed with this in mind and
305 sendmail ships a [rmail](https://manpages.debian.org/stretch/rmail/rmail.8.en.html) command that reads emails from UUCP
306 clients, but those have their own idiosyncrasies. Still, it should be
307 possible to configure UUCP clients to send email through an SSH
308 connexion, but that seems needlessly complicated.
309
310 * [NNCP](http://www.nncpgo.org/) (Node to Node copy) "is a collection of utilities
311 simplifying secure store-and-forward files and mail exchanging." It's
312 interesting in theory, but it practice it does much more than what we
313 actually need here. But if I were to redo this, I would probably use
314 it instead of my setup, because it's fairly easy to integrate into
315 Postfix *and* it is more resilient than SSH (e.g. email over Ham radio
316 anyone?)
317
318 * [Don Armstrong](https://donarmstrong.com/) wrote a [nullmailer](https://untroubled.org/nullmailer/) remote called
319 [sshsendmail](https://git.donarmstrong.com/?p=bin.git;a=blob_plain;f=sshsendmail) which basically does what we want, but it injects a
320 nullmailer shim through the SSH connection as a `perl -e`
321 executable. This makes it difficult to restrict the SSH
322 connection. [David Bremner](https://www.cs.unb.ca/~bremner/) repackaged an earlier version of this as
323 [nullmailer-ssh](https://salsa.debian.org/bremner/nullmailer-ssh) which at least does not use `perl -e` but still
324 has a nullmailer-specific dialect in the `rsendmail` command.
325
326 * Some IMAP servers have support for an `Outbox` folder that will send
327 an email that is dropped on that folder through a configured mail
328 server. Only the [Courier MTA](https://www.courier-mta.org/) seems to have that functionality
329 (called [IMAP send](https://www.courier-mta.org/imap/INSTALL.html#imapsend)) and I have stopped using that server a while
330 ago. My server of choice ([Dovecot](https://www.dovecot.org/)) [debated the feature in
331 2006](https://www.dovecot.org/list/dovecot/2006-November/017285.html) but it was never implemented.
332
333 Future work
334 -----------
335
336 This could be made in a Debian package or two: one would be
337 `rsendmail` for the server side and `sshsendmail` for the client side,
338 and maybe plugin packages for the various integration mechanisms. I'm
339 too lazy for this now.
340
341 Piping stuff through SSH makes it difficult to distinguish between
342 temporary failures (e.g. DNS or TCP fails) and configuration errors
343 (SSH key mismatch). I'm not even sure what should bounce, so I have
344 avoided that issue altogether by treating all SSH failures as
345 temporary, but it might be relevant to re-implement this using
346 [Paramiko](http://www.paramiko.org/) or some other library in the future.
347
348 Credits
349 -------
350
351 On top of the above "prior art", I stand on Bremner and Armstrong's
352 shoulders as they provided the basic idea for this program.
353
354 This software was written by Antoine Beaupré in 2018 and is released
355 under the Affero GPLv3.