From 59534686e95006296bf603bc6da4b7cccd4f1f37 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 30 Apr 2021 20:25:09 +0200 Subject: [PATCH] Automating aliases blogpost --- content/posts/automating-aliases.md | 74 +++++++++++++++++++ ...utomatizing-tags.md => automating-tags.md} | 2 +- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 content/posts/automating-aliases.md rename content/posts/{automatizing-tags.md => automating-tags.md} (98%) diff --git a/content/posts/automating-aliases.md b/content/posts/automating-aliases.md new file mode 100644 index 0000000..199299c --- /dev/null +++ b/content/posts/automating-aliases.md @@ -0,0 +1,74 @@ +--- +title: "Automating email aliases using Mailinabox and curl" +date: 2021-04-30 +draft: false +--- + +With every piece of furniture, website and bowl of instant ramen asking you to register a user account, it's getting hard to remember anything. Furthermore, the companies which so gladly hoard our contact information are having a hard time protecting it, and it seems like the age of password managers and email aliases is upon us. + +One cool thing about email aliases is that they let you know which company is sending most spam your way. As a big fan of self-hosting, I have a Mailinabox and I recently found out about its [REST API](https://mailinabox.email/api-docs.html). In my pursuit for comfyness, I tried to automate the creation of aliases. + +# The credentials + +First of all, all of Mailinabox's API needs admin authentication, either an `api_key` or a `user:password` tuple. Since the former needs the later, I'll just use user and password. Even though `curl` tries its best to hide its command line arguments from terminal history, it's just not good practice to pass `-u admin@mydomain:mypassword` directly. Reading a bit in its manual, I found out that option `-K -` lets you pass config parameters from stdin, like so: + +```$ gpg -qd credentials.gpg | curl -X GET "https://{host}/admin/mail/users?format="``` + +Where `credentials.gpg` is an encrypted file with the text `-u mail:password`. This way, you can safely use this without fear of your credentials leaking into logs or `ps` output. If everything worked just right, you should be able to see a list of all of your mails. + +Note that for GPG to be able to ask for secret key's password interactively, you may need to define `GPG_TTY` in your `.bashrc` or equivalent. + +# Creating the alias + +The command for alias creation would be something like this: + +```bash +$ gpg -qd credentials.gpg | curl -X POST "https://{host}/admin/mail/aliases/add" \ +-d "address=" \ +-d "forward_to=" +``` + +This is fine and usable, if a bit tedious. I like to set my aliases to random strings, so malicious actors cannot deduce the true email address. A simple shell script that creates a random email alias would look something like this: + +```bash +#!/bin/sh + +DOMAIN="@" +HOST="" +NEW_ADDRESS=$(tr -dc a-z0-9