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
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.
12 usage="Usage: mhsign [-encrypt] [-mime] file"
19 # find out the signing key
21 if [ "x$userid" = "x" ] ; then
22 userid="`mhparam pgpkey`"
24 userid="`gpg --list-secret-keys --with-colons 2>/dev/null |
25 sed -n '/^sec/{p;q;}' | cut -d: -f5`"
26 if [ "x$userid" = x ] ; then
27 echo "No secret key found" >&2
31 # find out the file of recipient key exceptions (for encrypt only)
32 keyfile="${MMH:-$HOME/.mmh}/pgpkeys"
33 if [ ! -r "$keyfile" ] ; then
34 keyfile="${GNUPGHOME:-$HOME/.gnupg}/pgpkeys"
35 if [ ! -r "$keyfile" ] ; then
40 # prepend the default options from the profile
41 set -- `mhparam -nocomp ${0##*/}` "$@"
52 echo "mhsign has no own version number, thus this instead:"
66 if [ $# -ne 1 ] ; then
74 trap "rm -rf $TEMP" 0 1 2 15
76 ### lookupkeyfile address -- lookup one address in our database
78 key=`grep -i "^[^#].*[ ]$1\$" "$keyfile" 2>/dev/null`
82 echo "$key" | sed 's/[ ].*//;q'
86 ### lookupkeyring address -- lookup one address in keyring
88 key=`gpg --list-keys --with-colons "<$1>" 2>/dev/null`
92 echo "$key" | sed -n '/^pub/{p;q;}' | cut -d: -f5
96 ### lookupkeys file -- set $KL to list of recipient keys
100 if whom -ali -notocc -bcc "$1" >/dev/null ; then
101 echo "Encryption is not supported for BCCs" >&2
105 for i in `whom -ali -tocc -nobcc "$1"` ; do
107 '|'*) echo "Ignoring pipe address" >&2
110 *) i="$i@`hostname -f`" ;;
112 if k=`lookupkeyfile "$i"` ; then
114 elif k=`lookupkeyring "$i"` ; then
117 echo "Could not find key for <$i>" >&2
124 ### getheader headername msgfile
126 HDR=`sed -n -e '/^-*$/q' -e 's/^\([^ :]*\):.*/\1/p' $2 |
127 grep -i '^'"$1"'$' | head -1`
128 if [ "$HDR" = "" ] ; then return 1 ; fi
142 ### headbody msgfile # separate msgfile into $TEMP/head $TEMP/body
144 sed -n '1,/^\-*$/p' "$1" > $TEMP/head
145 sed '1,/^-*$/d' "$1" > $TEMP/body
148 ### fixheaders -- remove Content headers, add newheaders
152 /^[Cc][Oo][Nn][Tt][Ee][Nn][Tt]-/b r
161 grep "^-" $TEMP/head || echo ""
164 ### newboundary -- output a suitable boundary marker
166 b=$$_`date|sed 's/[ : ]/_/g'`
167 for i in 0 x '=' _ + , Z 9 4 ; do
168 if grep "^--$b" $TEMP/body >/dev/null 2>&1 ; then
169 ## oops, bad boundary -- try again
171 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456780=+,_' \
172 '3Ba+c98bdACmzXpqR,tTuMDSs_hLkwZ0ef7PQrW=2x5l6E14ZKivIVgOjoJnGNUyHF'`
178 echo "Failed to generate unique mime boundary" >&2
182 ### detachsign -- sign $TEMP/body, output in $TEMP/body.asc
184 gpg -u "$userid" --armor --textmode --detach-sign \
185 <$TEMP/body >$TEMP/body.asc
188 ### sign --- inline signature for $TEMP/body, output in $TEMP/body.asc
190 gpg -u "$userid" --armor --textmode --clearsign \
191 <$TEMP/body >$TEMP/body.asc
194 ### encrypt recipients -- encrypt $TEMP/body to recipients
200 gpg --no-encrypt-to -u "$userid" --armor --textmode \
201 --always-trust --output $TEMP/body.asc \
202 -r "$userid" $R --sign --encrypt $TEMP/body
205 ### Mainline processing
207 FILE="$1" ## we assume a disk file
208 if [ ! -r "$FILE" ] ; then
209 echo "cannot read $FILE" >&2
215 lookupkeys "$FILE" || exit 1
218 cp "$FILE" "$FILE.orig"
223 if grep -i "^mime-version:" $TEMP/head >/dev/null 2>&1 ; then
225 if CT=`getheader content-type $TEMP/head` ; then
226 echo "$CT" >$TEMP/newbody
227 if grep -i multipart $TEMP/newbody >/dev/null 2>&1 ; then
228 usemime=y # Force MIME if already multi-part
230 getheader content-transfer-encoding $TEMP/head \
236 echo "Mime-Version: 1.0" >$TEMP/newheaders
239 if [ "$usemime" = n ] ; then
247 cat $TEMP/head $TEMP/body.asc >$outfile || exit 1
255 if [ "$CT" = "" ] ; then
256 echo "Content-Type: text/plain; charset=us-ascii" >$TEMP/newbody
262 sed 's/^From / &/; s/[
\r ]*$//' $TEMP/body >>$TEMP/newbody
263 if grep "^From " $TEMP/body >/dev/null 2>&1 ; then
264 echo 'Warning: "From " lines in message body have been indented' >&2
266 if grep "[
\r ]$" $TEMP/body >/dev/null 2>&1 ; then
267 echo 'Warning: trailing blanks removed from message body' >&2
269 echo 'Content-Type: multipart/signed; protocol="application/pgp-signature";' >>$TEMP/newheaders
270 echo " micalg=pgp-sha1"'; boundary="'"$BDRY"'"' >>$TEMP/newheaders
272 sed -e 's/$/
\r/' "$TEMP/newbody" >"$TEMP/body"
279 echo "Content-Type: application/pgp-signature"
289 cat $TEMP/body >>$TEMP/newbody
290 echo 'Content-Type: multipart/encrypted; protocol="application/pgp-encrypted";' >>$TEMP/newheaders
291 echo " boundary=\"$BDRY\"" >> $TEMP/newheaders
293 mv $TEMP/newbody $TEMP/body || exit 1
297 echo "Content-Type: application/pgp-encrypted"
302 echo "Content-Type: application/octet-stream"
312 fixheaders | cat - $TEMP/body >"$outfile"