My Dear Diary

SDF inside termux

Originally posted on my gemini capsule

SDF comes to your pocket: meet Termux

Recently, I stumbled across Termux. It is a combination of terminal emulator and apt based package manager which makes it possible to do development related stuff right at your fingertips everywhere you go.

What you can do inside Termux?

Well, a lot of stuffs. Connecting to sdf via ssh, accessing files stored on sdf clusters, backing up files into sdf clusters, typing markdown text and convert it to html, compiling source code, and more that you can imagine.

For my use case, the main advantage of termux is that it makes it easy to connect to sdf. I can even transfers my emails on sdf for offline consumption inside termux. It even possible to setup email client inside termux to read all those mails transferred from sdf.

mutt inside Termux

Well, first thing to do is to get a copy of mail spool from sdf. This can be done easily using scp or sftp.

# $(whoami) is sdf login name, of course!
scp $(whoami)@tty.sdf.org:/mail/$(whoami) ~/playground/sdf/mail/inbox

Then, mutt inside termux can be configured to read offine mail, synced from sdf.

Here is a snippet from ~/.muttrc to read mails from sdf inside termux.

mailboxes ~/playground/sdf/mail/inbox
set spoolfile=~/playground/sdf/mail/inbox

sending mail from Termux

With those setups, sdf mails come inside Termux. The tricky part: how about sending mails? Do I need to fire up ssh connection into sdf every time I want to send emails?

First workaround: compose locally and postpone the message

Without further trick, this is the simplest possible setup. This is nice, since email can be composed locally, without the need to edit the file via ssh.

FYI, typing something via ssh is somewhat laggy. There is noticeable delay between entering text and the displayed text. Of course, the typed input needs to travel far to sdf and sent back to the screen.

So, for convenience: composing mail locally and postponing the mail is a workaround to get decent typing experience. Later, one needs to login to sdf with mail in hand, ready to send.

With this setup, mutt will append the postponed message to ~/postponed. This file can be transferred to sdf, for later delivery.

This is too cumbersome. I want to send it right from Termux.

Very demanding, aren't I? Well, it is possible to use mx.sdf.org from within Termux, with MetaARPA membership level.

Unfortunately, such membership level is beyond what a mere diary writer can spend, so here I am stuck with user membership.

No problem, I can tell Mutt to send the message with local sendmail script that connect to sdf's sendmail instance.

Here is one line addition inside Termux's ~/.muttrc

set sendmail=~/bin/sdf_sendmail.sh

For this setup, there is configuration needed to get msmtp working. Log into sdf and create ~/.msmtprc.

account sdf
from SDFUSERNAME@sdf.org
user SDFUSERNAME
host mx.sdf.org
port 587
tls on
tls_trust_file ~/.ssl/cacert.pem

account default: sdf

For the trust file, a copy of Termux's ca bundle can be used. Alternatively, curl's ca bundle can be obtained easily to the designated tls_trust_file.

Now back to Termux. A simple ~/bin/sdf_sendmail.sh executable needs to be created so mutt can send mail directly, without postponed workaround above.

Here is the content of ~/bin/sdf_sendmail.sh

#!/bin/sh
SDFUSERNAME="myloginnamehere"
if [ -S "$SSH_AUTH_SOCK" ] ; then
    ssh-add -l | grep sdf && \
    ssh "$SDFUSERNAME"@tty.sdf.org "msmtp $*" || \
    { echo "ssh key is not added to agent" ; \
    echo "refusing operation" ; \
    return 1 ;}
else
    echo "Unable to use ssh-agent"
    echo "Ensure you have started ssh-agent"
    echo "and add the key for tty.sdf.org to the agent."
    echo -e '\a'
    return 1
fi

Give it executable bit and mutt is ready to go.

chmod +x ~/bin/sdf_sendmail.sh

~/bin/sdf_sendmail.sh relies on public key authentication enabled for the account used on sdf, so be sure to start ssh-agent and add the key to agent before sending mail from Termux.

eval $(ssh-agent)
ssh-add ~/.ssh/sdf.key

With the setup, everytime mutt sends a message, ~/bin/sdf_sendmail.sh is launched. If the requirements are met: the presence of running ssh-agent and the key for sdf login has been added to the agent, mutt will send the message via msmtp that is accessible on the sdf cluster.

The benefit of this setup: mail sent from Termux as if one is sending from sdf ssh session. No ip address at home is exposed on the mail and the email will go through the sdf mail server, without any violation of sdf's spf records.

Very convenient, indeed!