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