1#!/bin/sh 2 3# $Id: ppf_mime,v 1.11 2010/07/28 00:20:06 dougb Exp $ 4 5# Please see detailed Copyright below 6 7PATH=/bin:/usr/bin:@@PREFIX@@/bin ; export PATH 8umask 077 9 10: ${TMPDIR:=/tmp} 11TDIR=`mktemp -d ${TMPDIR}/ppf_mime.XXXXXXXX` || 12 { echo '' >&2 13 echo "$0: mktemp failed, exiting" >&2 14 echo '' >&2 15 exit 1;} 16 17trap "rm -f ${TDIR}/* ; rmdir ${TDIR} ; exit" 0 1 2 15 18 19pgp_failed () { 20 echo "$0: Your pgp command failed" >&2 21 echo '' >&2 22 cat ${TDIR}/stderr >&2 23 exit 1 24} 25 26cat - >${TDIR}/raw 27 28set_boundary () { 29 boundary="${1##*boundary=}" 30 boundary="${boundary#\"}" 31 boundary="${boundary%%[:;\"]*}" 32} 33 34space_twiddle=space_twiddle 35ll='-e $d' 36 37while read line; do 38 case "$line" in 39 *micalg\=[Pp][Gg][Pp]*boundary\=*) 40 set_boundary "$line" 41 if [ -z "$apple_ver" ]; then 42 break 43 else 44 micalg=true 45 fi ;; 46 *boundary\=*) if [ -z "$apple_ver" ]; then 47 set_boundary "$line" 48 else 49 [ -z "$micalg" ] && set_boundary "$line" 50 fi ;; 51 *delsp\=yes*) delsp=delsp ; break ;; 52 *Apple\ Message\ framework*) 53 apple_ver="${line##* v}" 54 apple_ver="${apple_ver%\)}" 55 apple_ver="${apple_ver%\.*}" 56 [ $apple_ver -ge 929 ] && unset space_twiddle 57 ;; 58 User-Agent\:\ KMail*) unset space_twiddle ;; 59 X-Mailer\:\ exmh*) unset space_twiddle ;; 60 X-Mailer\:\ Mulberry*|X-Mailer\:\ The\ Bat*) rlcm="-e \$s/.\$//" ; unset ll ;; 61 *micalg\=[Pp][Gg][Pp]*) micalg=true ;; 62 --${boundary}*) [ -n "$micalg" -a -z "$apple_ver" ] && break ;; 63 -----BEGIN\ PGP*) break ;; # Last gasp if Apple but not delsp 64 esac 65done < ${TDIR}/raw 66 67if [ -z "$boundary" ]; then 68 echo '' >&2 69 echo 'This does not appear to be a MIME-encoded message' >&2 70 echo '' >&2 71 exit 1 72fi 73 74csplit -s -k -f ${TDIR}/f ${TDIR}/raw "/--${boundary}/" '{9}' 2>/dev/null 75 76# Reliably find the message body and signature 77sigfile=`grep -l '^-----BEGIN PGP SIGNATURE-----$' ${TDIR}/f*` 78if [ -z "$sigfile" ]; then 79 # The Bat 80 sigfile=`grep -l '^-----BEGIN PGP MESSAGE-----$' ${TDIR}/f*` 81 82 if [ -z "$sigfile" ]; then 83 echo 'No PGP signature found' > ${TDIR}/stderr 84 pgp_failed 85 fi 86fi 87 88# Assuming the signature is in body part < 10 (which is very likely) 89filenum="${sigfile##*f0}" 90msgfile="f0$(( $filenum - 1 ))" 91 92# The first and last lines of the file are added by MIME encoding, 93# so they must be stripped in order for the signature to verify. 94# The other sed patterns canonicalize the EOL characters per RFC 3156. 95if [ -n "$space_twiddle" ]; then 96 sed -e '1d' $ll -e 's, 97$,,g' -e 's,[ ]*$,,' -e 's,$, 98,g' $rlcm ${TDIR}/$msgfile >${TDIR}/message 99 100 # The Bat! and Mulberry clients need the last linefeed character removed, which sed cannot do 101 if [ -n "$rlcm" ]; then 102 awk 'NR > 1 { print h } { h = $0 } END { printf "%s", $0 }' ${TDIR}/message > ${TDIR}/awked 103 mv ${TDIR}/awked ${TDIR}/message 104 fi 105else 106 sed -e '1d' -e '$d' -e 's, 107$,,g' -e 's,$, 108,g' ${TDIR}/$msgfile >${TDIR}/message 109fi 110 111@@GPG@@ --verify $sigfile ${TDIR}/message 2>${TDIR}/stderr || pgp_failed 112 113# Output will be 19 characters total so adjust status message length accordingly 114date=`date +"%Y-%m-%d %H:%M:%S"` 115 116egrep -hi '^(From|Resent-From|To|Reply-To|Resent-To|Cc|Resent-cc|Bcc|Newsgroups|Followup-To|Date|Resent-Date|Organi[sz]ation|X-Mailer|User-Agent|Subject|Resent-Subject):' ${TDIR}/f0[01] 117echo '' 118echo '---------------------------- PGP Command Output ----------------------------' 119grep -v '^$' ${TDIR}/stderr 120echo "----------- Begin PGP Signed Message Verified $date ----------" 121 122# Remove the MIME headers from the message, and display it as a side effect 123if [ -z "$delsp" ]; then 124sed -e 's/^=2E/./' -e 's/^=2D/-/' -e 's/^=46/F/' -e 's/^=66/f/' ${TDIR}/message | 125sed ':a 126N 127$!ba 128s/= 129\n//g' | 130sed -e '1,4 s/^Content-.*/76trombones/' -e '1,4 s/^[ ].*[a-z]=.*/76trombones/' -e '/^76trombones$/d' \ 131 -e 's,=20, ,g' -e 's,=3D,=,g' 132else 133sed -e '1,4 s/^Content-.*/76trombones/' -e '1,4 s/^[ ].*[a-z]=.*/76trombones/' -e '/^76trombones$/d' \ 134 -e 's,=20, ,g' -e 's,=3D,=,g' ${TDIR}/message 135fi 136 137echo '' 138echo "------------ End PGP Signed Message Verified $date -----------" 139 140exit 0 141 142# Copyright (c) 2003-2010 Douglas Barton 143# All rights reserved. 144# 145# Redistribution and use in source and binary forms, with or without 146# modification, are permitted provided that the following conditions 147# are met: 148# 1. Redistributions of source code must retain the above copyright 149# notice, this list of conditions and the following disclaimer. 150# 2. Redistributions in binary form must reproduce the above copyright 151# notice, this list of conditions and the following disclaimer in the 152# documentation and/or other materials provided with the distribution. 153# 154# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 155# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 156# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 157# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 158# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 159# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 160# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 161# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 162# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 163# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 164# SUCH DAMAGE. 165