Fix for commit bd02210b2be64956a952f925a2dcd35fb42f6136
[mmh] / uip / mhsign.sh
1 #!/bin/sh
2 # Based on mhsign 1.1.0.9 2007/05/30 14:48:40 by Neil Rickert
3 # Adjusted to mmh by markus schnalke <meillo@marmaro.de>, 2012-07
4
5
6 # mhsign:
7 #   -encrypt:  Encrypt to recipients of message. This implies signing.
8 #   -mime:     Use MIME pgp standard.  For signature, trailing blanks
9 #              will be removed and any "From " line will be indented for
10 #              best compatibility. Enforced for multipart messages.
11
12 usage="Usage: mhsign [-encrypt] [-mime] file"
13
14 # defaults
15 usemime=n
16 function=sign
17
18
19 # find out the signing key
20 userid="$MMHPGPKEY"
21 if [ -z "$userid" ] ; then
22         userid="`mhparam pgpkey`"
23 fi
24 if [ -z "$userid" ] ; then
25         userid="`gpg --list-secret-keys --with-colons 2>/dev/null |
26                         grep '^sec' | sort -t: -k3,3nr -k 6,6nr |
27                         awk -F: '
28                                 $7=="" || $7 > "'"\`date +%Y-%m-%d\`"'" {
29                                         print $5; exit;
30                                 }
31                         '`"
32 fi
33 if [ -z "$userid" ] ; then
34         echo "No secret key found" >&2
35         exit 1
36 fi
37
38 # find out the file of recipient key exceptions (for encrypt only)
39 keyfile="${MMH:-$HOME/.mmh}/pgpkeys"
40 if [ ! -r "$keyfile" ] ; then
41         keyfile="${GNUPGHOME:-$HOME/.gnupg}/pgpkeys"
42         if [ ! -r "$keyfile" ] ; then
43                 keyfile=/dev/null
44         fi
45 fi
46
47 # prepend the default options from the profile
48 set -- `mhparam -nocomp ${0##*/}` "$@"
49
50 while : ; do
51         case "$1" in
52         -e*)
53                 function=encrypt
54                 ;;
55         -m*)
56                 usemime=y
57                 ;;
58         -V*)
59                 echo "mhsign has no own version number, thus this instead:"
60                 folder -Version
61                 exit 0
62                 ;;
63         -h*|-*)
64                 echo "$usage" >&2
65                 exit 1
66                 ;;
67         *)
68                 break
69         esac
70         shift
71 done
72
73 if [ $# -ne 1 ] ; then
74         echo "$usage" >&2
75         exit 1
76 fi
77
78 TEMP=/tmp/${0##*/}.$$
79 umask 077
80 mkdir $TEMP || exit 1
81 trap "rm -rf $TEMP" 0 1 2 15
82
83 ### lookupkeyfile address -- lookup one address in our database
84 lookupkeyfile() {
85         key=`grep -i "^[^#].*[  ]$1\$" "$keyfile" 2>/dev/null`
86         if [ $? != 0 ] ; then
87                 return 1
88         fi
89         echo "$key" | sed 's/[  ].*//;q'
90         return 0
91 }
92
93 ### lookupkeyring address -- lookup one address in keyring
94 lookupkeyring() {
95         key=`gpg --list-keys --with-colons "<$1>" 2>/dev/null`
96         if [ $? != 0 ] ; then
97                 return 1
98         fi
99         echo "$key" | sed -n '/^pub:[^idre]:/{p;q;}' | cut -d: -f5
100         return 0
101 }
102
103 ### lookupkeys file -- set $KL to list of recipient keys
104 lookupkeys() {
105         KL=
106         status=0
107         if whom -ali -notocc -bcc "$1" >/dev/null ; then
108                 echo "Encryption is not supported for BCCs" >&2
109                 return 1
110         fi
111
112         whom -ali -tocc -nobcc "$1" | while read i ; do
113                 case "$i" in
114                 '|'*)   echo "Ignoring pipe address" >&2
115                         continue ;;
116                 *@*)    ;;
117                 *)      i="$i@`hostname -f`" ;;
118                 esac
119                 # extract the actual address
120                 format='%<{error}%{error}: %{text}%|%(addr{text})%>'
121                 i=`%libdir%/ap -form "=$format" "$i"`
122                 if k=`lookupkeyfile "$i"` ; then
123                         KL="$KL $k"
124                 elif k=`lookupkeyring "$i"` ; then
125                         KL="$KL $k"
126                 else
127                         echo "Could not find key for <$i>" >&2
128                         status=1
129                 fi
130         done
131         return $status
132 }
133
134 ### getheader headername msgfile
135 getheader() {
136         HDR=`sed -n -e '/^-*$/q' -e 's/^\([^    :]*\):.*/\1/p' $2 |
137                 grep -i '^'"$1"'$' | head -1`
138         if [ "$HDR" = "" ] ; then return 1 ; fi
139         sed -n -e ':a
140                 /^-*$/q
141                 /^'"$HDR"':/b x
142                 d
143                 b a
144                 :x
145                 p
146                 n
147                 /^[     ]/b x
148                 b a' $2
149                 return 0
150 }
151
152 ### headbody msgfile # separate msgfile into $TEMP/head $TEMP/body
153 headbody() {
154         sed -n '1,/^\-*$/p' "$1" > $TEMP/head
155         sed '1,/^-*$/d' "$1" > $TEMP/body
156 }
157
158 ### fixheaders -- remove Content headers, add newheaders
159 fixheaders() {
160         sed -n ':a
161                 /^-*$/q
162                 /^[Cc][Oo][Nn][Tt][Ee][Nn][Tt]-/b r
163                 p
164                 n
165                 b a
166                 :r
167                 n
168                 /^[     ]/b r
169                 b a' $TEMP/head
170         cat $TEMP/newheaders
171         grep "^-" $TEMP/head || echo ""
172 }
173
174 ### newboundary -- output a suitable boundary marker
175 newboundary() {
176         b=$$_`date|sed 's/[ :   ]/_/g'`
177         for i in 0 x '=' _ + , Z 9 4 ; do
178                 if grep "^--$b" $TEMP/body >/dev/null 2>&1 ; then
179                         ## oops, bad boundary -- try again
180                         b=`echo $i$b | tr \
181 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456780=+,_' \
182 '3Ba+c98bdACmzXpqR,tTuMDSs_hLkwZ0ef7PQrW=2x5l6E14ZKivIVgOjoJnGNUyHF'`
183                 else
184                         echo "$b"
185                         return 0
186                 fi
187         done
188         echo "Failed to generate unique mime boundary" >&2
189         exit 1
190 }
191
192 ### detachsign -- sign $TEMP/body, output in $TEMP/body.asc
193 detachsign() {
194         gpg -u "$userid" --armor --textmode --detach-sign \
195                         <$TEMP/body >$TEMP/body.asc
196 }
197
198 ### sign --- inline signature for $TEMP/body, output in $TEMP/body.asc
199 sign() {
200         gpg -u "$userid" --armor --textmode --clearsign \
201                 <$TEMP/body >$TEMP/body.asc
202 }
203
204 ### encrypt recipients -- encrypt $TEMP/body to recipients
205 encrypt() {
206         R=
207         for i in $KL ; do
208                 R="$R -r $i"
209         done
210         gpg --no-encrypt-to -u "$userid" --armor --textmode \
211                         --always-trust --output $TEMP/body.asc \
212                         -r "$userid" $R --sign --encrypt $TEMP/body
213 }
214
215 ### Mainline processing
216
217 FILE="$1"       ## we assume a disk file
218 if [ ! -r "$FILE" ] ; then
219         echo "cannot read $FILE" >&2
220         exit 1
221 fi
222
223 case "$function" in
224 encrypt)
225         lookupkeys "$FILE" || exit 1
226 esac
227
228 cp "$FILE" "$FILE.orig"
229 outfile="$FILE"
230 headbody "$FILE"
231
232 CT=""
233 if grep -i "^mime-version:" $TEMP/head >/dev/null 2>&1 ; then
234         >$TEMP/newheaders
235         if CT=`getheader content-type $TEMP/head` ; then
236                 echo "$CT" >$TEMP/newbody
237                 if grep -i multipart $TEMP/newbody >/dev/null 2>&1 ; then
238                         usemime=y  # Force MIME if already multi-part
239                 fi
240                 getheader content-transfer-encoding $TEMP/head \
241                                 >>$TEMP/newbody || :
242         else
243                 CT=""
244         fi
245 else
246         echo "Mime-Version: 1.0" >$TEMP/newheaders
247 fi
248
249 if [ "$usemime" = n ] ; then
250         ### non-MIME ###
251         case "$function" in
252         sign)
253                 sign || exit 1 ;;
254         encrypt)
255                 encrypt || exit 1 ;;
256         esac
257         cat $TEMP/head $TEMP/body.asc >$outfile || exit 1
258         exit 0
259 fi
260
261 ### MIME ###
262
263 BDRY="`newboundary`"
264
265 if [ "$CT" = "" ] ; then
266         echo "Content-Type: text/plain; charset=us-ascii" >$TEMP/newbody
267 fi
268 echo >>$TEMP/newbody
269
270 case $function in
271 sign)
272         sed 's/^From / &/; s/[ \r        ]*$//' $TEMP/body >>$TEMP/newbody
273         if grep "^From " $TEMP/body >/dev/null 2>&1 ; then
274                 echo 'Warning: "From " lines in message body have been indented' >&2
275         fi
276         if grep "[ \r    ]$" $TEMP/body >/dev/null 2>&1 ; then
277                 echo 'Warning: trailing blanks removed from message body' >&2
278         fi
279         echo 'Content-Type: multipart/signed; protocol="application/pgp-signature";' >>$TEMP/newheaders
280         echo "  micalg=pgp-sha1"'; boundary="'"$BDRY"'"' >>$TEMP/newheaders
281
282         sed -e 's/$/\r/' "$TEMP/newbody" >"$TEMP/body"
283         detachsign || exit 1
284         (
285                 echo "--$BDRY"
286                 cat $TEMP/newbody
287                 echo
288                 echo "--$BDRY"
289                 echo "Content-Type: application/pgp-signature"
290                 echo
291                 cat $TEMP/body.asc
292                 echo
293                 echo "--$BDRY--"
294                 echo
295         ) >$TEMP/body
296         ;;
297
298 encrypt)
299         cat $TEMP/body >>$TEMP/newbody
300         echo 'Content-Type: multipart/encrypted; protocol="application/pgp-encrypted";' >>$TEMP/newheaders
301         echo "  boundary=\"$BDRY\"" >> $TEMP/newheaders
302
303         mv $TEMP/newbody $TEMP/body || exit 1
304         encrypt || exit 1
305         (
306                 echo "--$BDRY"
307                 echo "Content-Type: application/pgp-encrypted"
308                 echo
309                 echo "Version: 1"
310                 echo
311                 echo "--$BDRY"
312                 echo "Content-Type: application/octet-stream"
313                 echo
314                 cat $TEMP/body.asc
315                 echo
316                 echo "--$BDRY--"
317                 echo
318         ) >"$TEMP/body"
319         ;;
320 esac
321
322 fixheaders | cat - $TEMP/body >"$outfile"