Mailpipe: a Simple SSH-based Mail Tunnel

Mailpipe is a method of setting up a reliable mobile mail server. It allows me to have a Postfix server (or any other mail server) running on my laptop, which I configure however I please. A second, stationary mail server is configured to relay both incoming and outgoing messages. Mailpipe sets up two SSH tunnels: one from the mobile server to port 21 on the relay server, and the other from the relay server to port 21 on the mobile server. This is a very flexible and non-intrusive method of mail transport.

Setting up Mailpipe on the Relay Server

Setting up Mailpipe on the Relay Server is very simple.

First, set up a user for ssh to connect as. I will assume that this user is named mailpipe. It could also be your personal account. In any case, you will need to be able to allow logins based on RSA keys.

Second, configure Postfix on the relay server for routing . Add an entry to /etc/postfix/transport: "mobile.mydomain.com smtp:127.0.0.1:2525". Make sure this line is in your main.cf: "transport_maps = hash:/etc/postfix/transport". Run "postmap /etc/postfix/transport".

Finally, configure Postfix on the relay server to hold mail. Add the following lines to /etc/postfix/main.cf: "fast_flush_domains = mobile.mydomain.com", "fast_flush_purge_time = 2w", and "fast_flush_refresh_time = 12h".

That's it. You're done on the relay server.

Setting up Mailpipe on the Mobile Server

First, set up a user to SSH from. This should be a dedicated user for this purpose. We'll call him mailpipe. Create an SSH key (with ssh-keygen), and copy the public key (mailkey.pub) to authorized_keys on the relay server in the .ssh directory of the mailpipe user. Make sure that you can login remotely from the mobile server to the relay server using the private key.

Second, configure Postfix on the mobile server to relay outgoing mail. Add the following lines to the main.cf file: "relayhost = 127.0.0.1:2525".

Finally, install the mailpipe script (this is a link to the mailpipe script), and modify the variables in it. I put it in "/usr/local/sbin/mailpipe". I also added the following line to my /etc/sudoers file: "amcnabb ALL = NOPASSWD: /usr/local/sbin/mailpipe".

Using Mailpipe

I have an Xbindkeys entry to hit Blank-L (that's Mod4-L, or WindowsKey-L for those of you who haven't scratched off the logo) to run "/usr/bin/sudo /usr/local/sbin/mailpipe 2>&- >&-". Every time I hit Blank-L, it starts or restarts the tunnels and issues a command to flush the mail queue on both ends.