Add new environment variable SENDMAIL_ARGS to allow user to pass arguments to sendmai...
[pgp-tools.git] / gpg-mailkeys / gpg-mailkeys
1 #! /bin/sh
2 #
3 # gpg-mailkeys: mail out just signed keys to their owners
4 #
5 # $Id$
6
7 set -e
8
9 VERSION='$Rev$'
10
11 # Define the charset used in the text message of the mail
12 LOCAL_CHARSET=""
13
14 ##
15 # Get the local charset.
16 #
17 # The local charset is deduced from the charset used by both ~/.gpg-mailkeysrc
18 # and ~/.signature. If none of these files exist, the local charset is assumed
19 # to be us-ascii.
20
21 get_local_charset ()
22 {
23 local charset="us-ascii"
24 local file_list="$HOME/.signature $HOME/.gpg-mailkeysrc"
25
26 for filename in $file_list; do
27
28 if [ -e $filename ]; then
29 charset=`file --mime-encoding $filename | cut -d ' ' -f 2`
30 break
31 fi
32
33 done;
34
35 LOCAL_CHARSET=$charset
36 }
37
38 if [ -z "$*" ]; then
39 printf "Send people their newly signed GPG key by mail.\n"
40 printf "Usage: $0 keyid ...\n"
41 exit 1
42 fi
43
44 if [ -e ~/.gpg-mailkeysrc ] ; then
45 . ~/.gpg-mailkeysrc
46 fi
47 if [ -n "$EMAIL" ]; then
48 FROM="$EMAIL"
49 fi
50 if [ -z "$SUBJECT" ]; then
51 SUBJECT="Your signed GPG key"
52 fi
53 if [ -z "$NAME" ]; then
54 NAME=`getent passwd $USER | cut -d: -f5 | cut -d, -f1`
55 fi
56 if [ -z "$TEXT" ]; then
57 TEXT="Hi,
58
59 Here is your signed GPG key.
60
61 Enjoy,
62 $NAME"
63 fi
64 if [ -z "$SENDMAIL_ARGS" ]; then
65 SENDMAIL_ARGS=""
66 fi
67
68 get_local_charset
69
70 FAILKEYS=
71
72 while [ -n "$1" ]; do
73 printf "[$1] "
74 TEMPFILE=`mktemp -t gpg2mail.XXXXXX`
75 ADDR=`gpg --with-colons --fixed-list-mode --list-key $1 | sed -e 's/^uid:[^re][^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:\([^:<]*<[^:>]*>\):.*/@@uid@@ \1/' -e '/^@@uid@@ /!d' -e 's/([^)]*)//g' -e 's/ */ /g' -e 's/^@@uid@@ //' | head -1`
76 if [ -z "$ADDR" ]; then
77 printf "(no usable user ids)\n"
78 FAILKEYS="$FAILKEYS:$1"
79 shift 1
80 continue
81 fi
82 NANOTIME=`date +%s-%N`
83 BOUNDARY="ksp-$$-boundary-$NANOTIME"
84
85 printf "$ADDR:"
86 if [ $FROM ]; then
87 printf >$TEMPFILE "From: $NAME <$FROM>\n"
88 fi
89 cat << EOM >> $TEMPFILE
90 To: $ADDR
91 Subject: $SUBJECT
92 User-Agent: gpg-mailkeys/$VERSION
93 MIME-Version: 1.0
94 Content-Type: multipart/mixed; micalg=pgp-sha1;
95 boundary="$BOUNDARY"
96 Content-Disposition: inline
97
98
99
100 --$BOUNDARY
101 Content-Type: text/plain; charset=$LOCAL_CHARSET
102 Content-Disposition: inline
103 Content-Transfer-Encoding: quoted-printable
104
105
106 `echo "$TEXT" | qprint -e`
107
108 EOM
109
110 if [ -f ~/.signature ];
111 then printf -- "--=20\n" >> $TEMPFILE
112 qprint -e ~/.signature >> $TEMPFILE
113 fi
114
115 cat << EOM >> $TEMPFILE
116
117 --$BOUNDARY
118 Content-Type: application/pgp-keys
119 Content-Disposition: attachment; filename="$1.asc"
120
121 `gpg --armor --export $1`
122
123 --$BOUNDARY--
124 EOM
125 printf " sending"
126 /usr/sbin/sendmail $SENDMAIL_ARGS -ti <$TEMPFILE
127 rm $TEMPFILE
128 printf " done.\n"
129 shift 1
130 done
131
132 if [ -n "$FAILKEYS" ]; then
133 printf "\nNote: The following keys could not be sent:\n"
134 printf "$FAILKEYS\n" | tr ':' '\n' | sed -e '/^ *$/d' -e 's/^/ /'
135 fi