-
Notifications
You must be signed in to change notification settings - Fork 5
/
gpgmail-postfix
executable file
·109 lines (85 loc) · 3.78 KB
/
gpgmail-postfix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env bash
# vim: ft=sh fileencoding=utf-8 sts=4 sw=4 et:
# Copyright (C) 2019-2022 J. Nathanael Philipp (jnphilipp) <nathanael@philipp.land>
# postfix filter script for gpgmail
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
usage() {
cat << EOF
usage: gpgmail-postfix (-d|-e|-s|-E) [-H|-S] [-g PATH] [-k KEYID] [-p PASSPHRASE] [-a GPGMAIL_PATH] [-m MAILER_PATH] -r RECIPIENT [MAILER_ARGS]
OPTIONS:
-d / --decrypt Decrypt E-mail
-e / --encrypt Encrypt E-mail
-s / --sign Sign E-mail
-E / --sign-encrypt Sign and encrypt E-mail
-H / --encrypt-headers Encrypt headers
-S / --encrypt-subject Encrypt subject
-g / --gnupghome PATH Path to gnupg home
-k / --key KEYID PGP-key to use
-p / --passphrase PASSPHRASE Passphrase for PGP-key
-a / --path GPGMAIL_PATH Path to gpgmail, defaults to /usr/bin/gpgmail
-r / --recipient RECIPIENT E-mail recipient
-m / --mailer MAILER_PATH Path to a mail sending utility, defaults to /usr/sbin/sendmail
MAILER_ARGS Arguments passed to the mail sending utility
-h / --help Shows this help message
-v / --version Show prgram version number
EOF
exit 0
}
version() {
cat << EOF
gpgmail-postfix v0.8.0
Report bugs to https://github.com/jnphilipp/gpgmail/issues.
Written by J. Nathanael Philipp <nathanael@philipp.land>
EOF
exit 0
}
MAILER=/usr/sbin/sendmail
GPGMAIL=/usr/bin/gpgmail
GPGMAIL_command=
GPGMAIL_gnupghome=
GPGMAIL_key=
GPGMAIL_passphrase=
GPGMAIL_encrypt_headers=
RECIPIENT=
MAILER_ARGS=()
while [[ $# -gt 0 ]]; do
case "$1" in
-d|--decrypt) [ -n "$GPGMAIL_command" ] && usage || GPGMAIL_command="--decrypt" ; shift ;;
-e|--encrypt) [ -n "$GPGMAIL_command" ] && usage || GPGMAIL_command="--encrypt" ; shift ;;
-s|--sign) [ -n "$GPGMAIL_command" ] && usage || GPGMAIL_command="--sign"; shift ;;
-E|--sign-encrypt) [ -n "$GPGMAIL_command" ] && usage || GPGMAIL_command="--sign-encrypt" ; shift ;;
-H|--encrypt-headers) [ -n "$GPGMAIL_encrypt_headers" ] && usage || GPGMAIL_encrypt_headers="--encrypt-headers"; shift ;;
-S|--encrypt-subject) [ -n "$GPGMAIL_encrypt_headers" ] && usage || GPGMAIL_encrypt_headers="--encrypt-subject"; shift ;;
-g|--gnupghome) GPGMAIL_gnupghome="--gnupghome=${2}"; shift 2 ;;
-k|--key) GPGMAIL_key="--key=${2}"; shift 2 ;;
-p|--passphrase) GPGMAIL_passphrase="--passphrase=${2}"; shift 2 ;;
-a|--path) GPGMAIL="${2}"; shift 2 ;;
-r|--recipient) RECIPIENT=$2; shift 2 ;;
-m|--mailer) MAILER=$2; shift 2 ;;
-h|--help) usage ;;
-v|--version) version ;;
*|"") break ;;
esac
done
while [[ $# -gt 0 ]]; do
case "$1" in
"") break ;;
*) MAILER_ARGS+=("${1}"); shift ;;
esac
done
([ -z "$GPGMAIL_command" ] || [ -z "$RECIPIENT" ]) && usage
set -o pipefail
#encrypt and resend directly from stdin
eval ${GPGMAIL} "${GPGMAIL_command}" "${GPGMAIL_encrypt_headers}" "${GPGMAIL_gnupghome}" "${GPGMAIL_key}" "${GPGMAIL_passphrase}" "${RECIPIENT}" | ${MAILER} "${MAILER_ARGS[@]}" "${RECIPIENT}"
exit $?