Fix spelling and encoding errors in manpages and an error message
[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         for i in `whom -ali -tocc -nobcc "$1"` ; do
113                 case "$i" in
114                 '|'*)   echo "Ignoring pipe address" >&2
115                         continue ;;
116                 *@*)    ;;
117                 *)      i="$i@`hostname -f`" ;;
118                 esac
119                 if k=`lookupkeyfile "$i"` ; then
120                         KL="$KL $k"
121                 elif k=`lookupkeyring "$i"` ; then
122                         KL="$KL $k"
123                 else
124                         echo "Could not find key for <$i>" >&2
125                         status=1
126                 fi
127         done
128         return $status
129 }
130
131 ### getheader headername msgfile
132 getheader() {
133         HDR=`sed -n -e '/^-*$/q' -e 's/^\([^    :]*\):.*/\1/p' $2 |
134                 grep -i '^'"$1"'$' | head -1`
135         if [ "$HDR" = "" ] ; then return 1 ; fi
136         sed -n -e ':a
137                 /^-*$/q
138                 /^'"$HDR"':/b x
139                 d
140                 b a
141                 :x
142                 p
143                 n
144                 /^[     ]/b x
145                 b a' $2
146                 return 0
147 }
148
149 ### headbody msgfile # separate msgfile into $TEMP/head $TEMP/body
150 headbody() {
151         sed -n '1,/^\-*$/p' "$1" > $TEMP/head
152         sed '1,/^-*$/d' "$1" > $TEMP/body
153 }
154
155 ### fixheaders -- remove Content headers, add newheaders
156 fixheaders() {
157         sed -n ':a
158                 /^-*$/q
159                 /^[Cc][Oo][Nn][Tt][Ee][Nn][Tt]-/b r
160                 p
161                 n
162                 b a
163                 :r
164                 n
165                 /^[     ]/b r
166                 b a' $TEMP/head
167         cat $TEMP/newheaders
168         grep "^-" $TEMP/head || echo ""
169 }
170
171 ### newboundary -- output a suitable boundary marker
172 newboundary() {
173         b=$$_`date|sed 's/[ :   ]/_/g'`
174         for i in 0 x '=' _ + , Z 9 4 ; do
175                 if grep "^--$b" $TEMP/body >/dev/null 2>&1 ; then
176                         ## oops, bad boundary -- try again
177                         b=`echo $i$b | tr \
178 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456780=+,_' \
179 '3Ba+c98bdACmzXpqR,tTuMDSs_hLkwZ0ef7PQrW=2x5l6E14ZKivIVgOjoJnGNUyHF'`
180                 else
181                         echo "$b"
182                         return 0
183                 fi
184         done
185         echo "Failed to generate unique mime boundary" >&2
186         exit 1
187 }
188
189 ### detachsign -- sign $TEMP/body, output in $TEMP/body.asc
190 detachsign() {
191         gpg -u "$userid" --armor --textmode --detach-sign \
192                         <$TEMP/body >$TEMP/body.asc
193 }
194
195 ### sign --- inline signature for $TEMP/body, output in $TEMP/body.asc
196 sign() {
197         gpg -u "$userid" --armor --textmode --clearsign \
198                 <$TEMP/body >$TEMP/body.asc
199 }
200
201 ### encrypt recipients -- encrypt $TEMP/body to recipients
202 encrypt() {
203         R=
204         for i in $KL ; do
205                 R="$R -r $i"
206         done
207         gpg --no-encrypt-to -u "$userid" --armor --textmode \
208                         --always-trust --output $TEMP/body.asc \
209                         -r "$userid" $R --sign --encrypt $TEMP/body
210 }
211
212 ### Mainline processing
213
214 FILE="$1"       ## we assume a disk file
215 if [ ! -r "$FILE" ] ; then
216         echo "cannot read $FILE" >&2
217         exit 1
218 fi
219
220 case "$function" in
221 encrypt)
222         lookupkeys "$FILE" || exit 1
223 esac
224
225 cp "$FILE" "$FILE.orig"
226 outfile="$FILE"
227 headbody "$FILE"
228
229 CT=""
230 if grep -i "^mime-version:" $TEMP/head >/dev/null 2>&1 ; then
231         >$TEMP/newheaders
232         if CT=`getheader content-type $TEMP/head` ; then
233                 echo "$CT" >$TEMP/newbody
234                 if grep -i multipart $TEMP/newbody >/dev/null 2>&1 ; then
235                         usemime=y  # Force MIME if already multi-part
236                 fi
237                 getheader content-transfer-encoding $TEMP/head \
238                                 >>$TEMP/newbody || :
239         else
240                 CT=""
241         fi
242 else
243         echo "Mime-Version: 1.0" >$TEMP/newheaders
244 fi
245
246 if [ "$usemime" = n ] ; then
247         ### non-MIME ###
248         case "$function" in
249         sign)
250                 sign || exit 1 ;;
251         encrypt)
252                 encrypt || exit 1 ;;
253         esac
254         cat $TEMP/head $TEMP/body.asc >$outfile || exit 1
255         exit 0
256 fi
257
258 ### MIME ###
259
260 BDRY="`newboundary`"
261
262 if [ "$CT" = "" ] ; then
263         echo "Content-Type: text/plain; charset=us-ascii" >$TEMP/newbody
264 fi
265 echo >>$TEMP/newbody
266
267 case $function in
268 sign)
269         sed 's/^From / &/; s/[ \r        ]*$//' $TEMP/body >>$TEMP/newbody
270         if grep "^From " $TEMP/body >/dev/null 2>&1 ; then
271                 echo 'Warning: "From " lines in message body have been indented' >&2
272         fi
273         if grep "[ \r    ]$" $TEMP/body >/dev/null 2>&1 ; then
274                 echo 'Warning: trailing blanks removed from message body' >&2
275         fi
276         echo 'Content-Type: multipart/signed; protocol="application/pgp-signature";' >>$TEMP/newheaders
277         echo "  micalg=pgp-sha1"'; boundary="'"$BDRY"'"' >>$TEMP/newheaders
278
279         sed -e 's/$/\r/' "$TEMP/newbody" >"$TEMP/body"
280         detachsign || exit 1
281         (
282                 echo "--$BDRY"
283                 cat $TEMP/newbody
284                 echo
285                 echo "--$BDRY"
286                 echo "Content-Type: application/pgp-signature"
287                 echo
288                 cat $TEMP/body.asc
289                 echo
290                 echo "--$BDRY--"
291                 echo
292         ) >$TEMP/body
293         ;;
294
295 encrypt)
296         cat $TEMP/body >>$TEMP/newbody
297         echo 'Content-Type: multipart/encrypted; protocol="application/pgp-encrypted";' >>$TEMP/newheaders
298         echo "  boundary=\"$BDRY\"" >> $TEMP/newheaders
299
300         mv $TEMP/newbody $TEMP/body || exit 1
301         encrypt || exit 1
302         (
303                 echo "--$BDRY"
304                 echo "Content-Type: application/pgp-encrypted"
305                 echo
306                 echo "Version: 1"
307                 echo
308                 echo "--$BDRY"
309                 echo "Content-Type: application/octet-stream"
310                 echo
311                 cat $TEMP/body.asc
312                 echo
313                 echo "--$BDRY--"
314                 echo
315         ) >"$TEMP/body"
316         ;;
317 esac
318
319 fixheaders | cat - $TEMP/body >"$outfile"