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 [ -z "$userid" ] ; then
22 userid="`mhparam pgpkey`"
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 |
28 $7=="" || $7 > "'"\`date +%Y-%m-%d\`"'" {
33 if [ -z "$userid" ] ; then
34 echo "No secret key found" >&2
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
47 # prepend the default options from the profile
48 set -- `mhparam -nocomp ${0##*/}` "$@"
59 echo "mhsign has no own version number, thus this instead:"
73 if [ $# -ne 1 ] ; then
81 trap "rm -rf $TEMP" 0 1 2 15
83 ### lookupkeyfile address -- lookup one address in our database
85 key=`grep -i "^[^#].*[ ]$1\$" "$keyfile" 2>/dev/null`
89 echo "$key" | sed 's/[ ].*//;q'
93 ### lookupkeyring address -- lookup one address in keyring
95 key=`gpg --list-keys --with-colons "<$1>" 2>/dev/null`
99 echo "$key" | sed -n '/^pub:[^idre]:/{p;q;}' | cut -d: -f5
103 ### lookupkeys file -- set $KL to list of recipient keys
107 if whom -ali -notocc -bcc "$1" >/dev/null ; then
108 echo "Encryption is not supported for BCCs" >&2
112 for i in `whom -ali -tocc -nobcc "$1"` ; do
114 '|'*) echo "Ignoring pipe address" >&2
117 *) i="$i@`hostname -f`" ;;
119 if k=`lookupkeyfile "$i"` ; then
121 elif k=`lookupkeyring "$i"` ; then
124 echo "Could not find key for <$i>" >&2
131 ### getheader headername msgfile
133 HDR=`sed -n -e '/^-*$/q' -e 's/^\([^ :]*\):.*/\1/p' $2 |
134 grep -i '^'"$1"'$' | head -1`
135 if [ "$HDR" = "" ] ; then return 1 ; fi
149 ### headbody msgfile # separate msgfile into $TEMP/head $TEMP/body
151 sed -n '1,/^\-*$/p' "$1" > $TEMP/head
152 sed '1,/^-*$/d' "$1" > $TEMP/body
155 ### fixheaders -- remove Content headers, add newheaders
159 /^[Cc][Oo][Nn][Tt][Ee][Nn][Tt]-/b r
168 grep "^-" $TEMP/head || echo ""
171 ### newboundary -- output a suitable boundary marker
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
178 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456780=+,_' \
179 '3Ba+c98bdACmzXpqR,tTuMDSs_hLkwZ0ef7PQrW=2x5l6E14ZKivIVgOjoJnGNUyHF'`
185 echo "Failed to generate unique mime boundary" >&2
189 ### detachsign -- sign $TEMP/body, output in $TEMP/body.asc
191 gpg -u "$userid" --armor --textmode --detach-sign \
192 <$TEMP/body >$TEMP/body.asc
195 ### sign --- inline signature for $TEMP/body, output in $TEMP/body.asc
197 gpg -u "$userid" --armor --textmode --clearsign \
198 <$TEMP/body >$TEMP/body.asc
201 ### encrypt recipients -- encrypt $TEMP/body to recipients
207 gpg --no-encrypt-to -u "$userid" --armor --textmode \
208 --always-trust --output $TEMP/body.asc \
209 -r "$userid" $R --sign --encrypt $TEMP/body
212 ### Mainline processing
214 FILE="$1" ## we assume a disk file
215 if [ ! -r "$FILE" ] ; then
216 echo "cannot read $FILE" >&2
222 lookupkeys "$FILE" || exit 1
225 cp "$FILE" "$FILE.orig"
230 if grep -i "^mime-version:" $TEMP/head >/dev/null 2>&1 ; then
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
237 getheader content-transfer-encoding $TEMP/head \
243 echo "Mime-Version: 1.0" >$TEMP/newheaders
246 if [ "$usemime" = n ] ; then
254 cat $TEMP/head $TEMP/body.asc >$outfile || exit 1
262 if [ "$CT" = "" ] ; then
263 echo "Content-Type: text/plain; charset=us-ascii" >$TEMP/newbody
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
273 if grep "[
\r ]$" $TEMP/body >/dev/null 2>&1 ; then
274 echo 'Warning: trailing blanks removed from message body' >&2
276 echo 'Content-Type: multipart/signed; protocol="application/pgp-signature";' >>$TEMP/newheaders
277 echo " micalg=pgp-sha1"'; boundary="'"$BDRY"'"' >>$TEMP/newheaders
279 sed -e 's/$/
\r/' "$TEMP/newbody" >"$TEMP/body"
286 echo "Content-Type: application/pgp-signature"
296 cat $TEMP/body >>$TEMP/newbody
297 echo 'Content-Type: multipart/encrypted; protocol="application/pgp-encrypted";' >>$TEMP/newheaders
298 echo " boundary=\"$BDRY\"" >> $TEMP/newheaders
300 mv $TEMP/newbody $TEMP/body || exit 1
304 echo "Content-Type: application/pgp-encrypted"
309 echo "Content-Type: application/octet-stream"
319 fixheaders | cat - $TEMP/body >"$outfile"