* gpg-mailkeys: (Closes: #545186)
[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
65 get_local_charset
66
67 FAILKEYS=
68
69 while [ -n "$1" ]; do
70 printf "[$1] "
71 TEMPFILE=`mktemp -t gpg2mail.XXXXXX`
72 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`
73 if [ -z "$ADDR" ]; then
74 printf "(no usable user ids)\n"
75 FAILKEYS="$FAILKEYS:$1"
76 shift 1
77 continue
78 fi
79 NANOTIME=`date +%s-%N`
80 BOUNDARY="ksp-$$-boundary-$NANOTIME"
81
82 printf "$ADDR:"
83 if [ $FROM ]; then
84 printf >$TEMPFILE "From: $NAME <$FROM>\n"
85 fi
86 cat << EOM >> $TEMPFILE
87 To: $ADDR
88 Subject: $SUBJECT
89 User-Agent: gpg-mailkeys/$VERSION
90 MIME-Version: 1.0
91 Content-Type: multipart/mixed; micalg=pgp-sha1;
92 boundary="$BOUNDARY"
93 Content-Disposition: inline
94
95
96
97 --$BOUNDARY
98 Content-Type: text/plain; charset=$LOCAL_CHARSET
99 Content-Disposition: inline
100 Content-Transfer-Encoding: quoted-printable
101
102
103 `echo "$TEXT" | qprint -e`
104
105 EOM
106
107 if [ -f ~/.signature ];
108 then printf -- "--=20\n" >> $TEMPFILE
109 qprint -e ~/.signature >> $TEMPFILE
110 fi
111
112 cat << EOM >> $TEMPFILE
113
114 --$BOUNDARY
115 Content-Type: application/pgp-keys
116 Content-Disposition: attachment; filename="$1.asc"
117
118 `gpg --armor --export $1`
119
120 --$BOUNDARY--
121 EOM
122 printf " sending"
123 /usr/sbin/sendmail -ti <$TEMPFILE
124 rm $TEMPFILE
125 printf " done.\n"
126 shift 1
127 done
128
129 if [ -n "$FAILKEYS" ]; then
130 printf "\nNote: The following keys could not be sent:\n"
131 printf "$FAILKEYS\n" | tr ':' '\n' | sed -e '/^ *$/d' -e 's/^/ /'
132 fi