Skip to content

Instantly share code, notes, and snippets.

@tzwenn
Created January 25, 2017 16:41
Show Gist options
  • Save tzwenn/6f2e347c267290313678b65063270b4a to your computer and use it in GitHub Desktop.
Save tzwenn/6f2e347c267290313678b65063270b4a to your computer and use it in GitHub Desktop.
Postfix Admin: Create a new <foo>@example.com alias to your e-mail address by direct postgres insert over SSH.
#!/bin/bash
DOMAIN=example.com
TARGET=mail@$DOMAIN
SSH_HOST=example.com
SSH_USER=$USER
DBNAME=postfixadmin
if [ $# -lt 1 ]; then
echo "Usage: $0 <alias>";
exit 1
fi
FORBIDDEN="\'\"\\\;\:\#\[\]\{\}\(\)\|\&\^\$\@\!\?\,\ \*\<\>" # '"\;:#[]{}()|&^$@!?, *<>
if grep -qP "[$FORBIDDEN]" <<<$1; then
echo "Alias may not contain any of $FORBIDDEN (of course unescaped)"
exit 2
fi
ALIAS=$1@$DOMAIN
CMD="\"INSERT INTO alias VALUES ('$ALIAS', '$TARGET', '$DOMAIN');\""
set -x
ssh -t $SSH_USER@$SSH_HOST sudo -u postgres psql $DBNAME -c "$CMD"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment