Advertisement
Guest User

mail_decrypt_verify.sh

a guest
Nov 6th, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.03 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. umask 077
  4.  
  5. fail_common () {
  6.     echo '' >&2
  7.     echo '-------------------------- Sleeping for 10 seconds -------------------------' >&2
  8.     sleep 10
  9.     exit 1
  10. }
  11.  
  12. pgp_failed () {
  13.     echo "$0: Your pgp command failed" >&2
  14.     echo '' >&2
  15.     cat ${TDIR}/stderr >&2
  16.     fail_common
  17. }
  18.  
  19. unencrypted_msg () {
  20.     cat ${TDIR}/f* > ${TDIR}/msgcopy
  21.     if grep -q -- '^-----BEGIN PGP SIGNED MESSAGE-----' ${TDIR}/msgcopy; then
  22.         gpg --batch --no-tty --decrypt ${TDIR}/msgcopy >${TDIR}/vrfdmsg 2>>${TDIR}/stderr || pgp_failed
  23.         cat ${TDIR}/msgcopy |
  24.             sed -e '/^-----BEGIN PGP SIGNED MESSAGE-----/,/^-----END PGP SIGNATURE-----/ d' \
  25.                 > ${TDIR}/msgstrip
  26.         cat ${TDIR}/msgstrip
  27.         cat ${TDIR}/vrfdmsg
  28.     elif grep -q -m1 '^Content-Type:.*multipart/signed' ${TDIR}/msgcopy; then
  29.         bndr=$(cat ${TDIR}/msgcopy |
  30.         sed -n -e '/^Content-Type:.*multipart\/signed/,/boundary=/ p; /boundary=/ q' |
  31.         grep -P -o '(?<=boundary=")[[:graph:]]*?(?=")' |
  32.         sed -e 's:[]\[\^\$\.\*\/]:\\&:g')
  33.         cat ${TDIR}/msgcopy |
  34.         sed -n -e '/^--'"${bndr}"'\r*$/,/^--'"${bndr}"'\r*$/{/^--'"${bndr}"'\r*$/b;/^--'"${bndr}"'\r*$/b;p;}' |
  35.         sed -e '$d' > ${TDIR}/msgbody
  36.         cat ${TDIR}/msgcopy |
  37.         sed -n -e '/^-----BEGIN PGP SIGNATURE-----/,/^-----END PGP SIGNATURE-----/ p' > ${TDIR}/msgsign
  38.         gpg --verify ${TDIR}/msgsign ${TDIR}/msgbody 2>${TDIR}/stderr || pgp_failed
  39.         cat ${TDIR}/msgcopy |
  40.         sed -n -e '1,/^Content-Type:.*multipart\/signed/{/^Content-Type:.*multipart\/signed/b;p;}' > ${TDIR}/msgstrip
  41.         sed -e 's/^=2E/./' -e 's/^=2D/-/' -e 's/^=46/F/' -e 's/^=66/f/' ${TDIR}/msgbody |
  42.         sed ':a
  43. N
  44. $!ba
  45. s/=^M\n//g' |
  46.         sed -e 's,=20, ,g' -e 's,=3D,=,g' |
  47.         sed -e 's/\x0D$//g' > ${TDIR}/vrfdmsg
  48.         cat ${TDIR}/msgstrip
  49.         cat ${TDIR}/vrfdmsg
  50.     else
  51.         cat ${TDIR}/msgcopy
  52.     fi
  53.     exit 0
  54. }
  55.  
  56. decrypt_mime () {
  57.     cat ${TDIR}/f* | awk '
  58. BEGIN {
  59.     FS=":"
  60.     ORS="|"
  61. }
  62. {
  63.     if ( $0 == "" ) exit;
  64.     if ( $0 ~ /^[[:graph:]].*?:/ && $0 !~ /^Content-[[:graph:]].*?:/ ) print $1;
  65. }
  66. ' | sed 's/|$//' > ${TDIR}/hdrnms
  67.  
  68.     grep -P -hzo '(?ms)^('$(cat ${TDIR}/hdrnms)'):.*?$(\n( ){1,}.*?$){0,}' ${TDIR}/f0[01] > ${TDIR}/hdrs
  69.  
  70.     if grep -q -m1 '^Content-Type:.*multipart/signed' ${TDIR}/message; then
  71.         bndr=$(cat ${TDIR}/message |
  72.         sed -n -e '/^Content-Type:.*multipart\/signed/,/boundary=/ p; /boundary=/ q' |
  73.         grep -P -o '(?<=boundary=")[[:graph:]]*?(?=")' |
  74.         sed -e 's:[]\[\^\$\.\*\/]:\\&:g')
  75.         cat ${TDIR}/message |
  76.         sed -n -e '/^--'"${bndr}"'\r*$/,/^--'"${bndr}"'\r*$/{/^--'"${bndr}"'\r*$/b;/^--'"${bndr}"'\r*$/b;p;}' |
  77.         sed -e '$d' > ${TDIR}/msgbody
  78.         cat ${TDIR}/message |
  79.         sed -n -e '/^-----BEGIN PGP SIGNATURE-----/,/^-----END PGP SIGNATURE-----/ p' > ${TDIR}/msgsign
  80.         gpg --verify ${TDIR}/msgsign ${TDIR}/msgbody 2>${TDIR}/stderr || pgp_failed
  81.             sed -e 's/^=2E/./' -e 's/^=2D/-/' -e 's/^=46/F/' -e 's/^=66/f/' ${TDIR}/msgbody |
  82.             sed ':a
  83. N
  84. $!ba
  85. s/=^M\n//g' |
  86.             sed -e 's,=20, ,g' -e 's,=3D,=,g' |
  87.             sed -e 's/\x0D$//g' > ${TDIR}/dcrptmsg
  88.     else
  89.         sed -e 's/^=2E/./' -e 's/^=2D/-/' -e 's/^=46/F/' -e 's/^=66/f/' ${TDIR}/message |
  90.         sed ':a
  91. N
  92. $!ba
  93. s/=^M\n//g' |
  94.         sed -e 's,=20, ,g' -e 's,=3D,=,g' |
  95.         sed -e 's/\x0D$//g' > ${TDIR}/dcrptmsg
  96.     fi
  97.  
  98.     ctype=$(grep -E -o -m1 '^Content-Type: [a-z]*?/[a-z]*?' ${TDIR}/dcrptmsg | sed -e 's/^Content-Type: //')
  99.     if [ "$ctype" = "text/plain" ]; then
  100.         grep -P -zo '(?ms)^Content-.*?:.*?$(\n( ){1,}.*?$){0,}' ${TDIR}/dcrptmsg >> ${TDIR}/hdrs
  101.     else
  102.         sed -n -e '/boundary=/ p; /boundary=/ q; /^Content-Type:/ p' ${TDIR}/dcrptmsg >> ${TDIR}/hdrs
  103.     fi
  104.  
  105.     cat ${TDIR}/hdrs
  106.     echo ''
  107.     cat ${TDIR}/dcrptmsg | sed -e '1,/^$/ d'
  108.     exit 0
  109. }
  110.  
  111. decrypt_inline () {
  112.         cat ${TDIR}/f0? | awk '
  113. BEGIN {
  114.        FS=":"
  115.        ORS="|"
  116. }
  117. {
  118.        if ( $0 == "" ) exit;
  119.     if ( $0 ~ /^[[:graph:]].*?:/ ) print $1;
  120. }
  121. ' | sed -e 's/|$//' > ${TDIR}/hdrnms
  122.  
  123.     grep -P -hzo '(?ms)^('$(cat ${TDIR}/hdrnms)'):.*?$(\n( ){1,}.*?$){0,}' ${TDIR}/f0[01] > ${TDIR}/hdrs
  124.  
  125.     cat ${TDIR}/message | grep -q -- '^-----BEGIN PGP SIGNED MESSAGE-----$' && \
  126.         ( gpg --batch --no-tty --decrypt ${TDIR}/message >${TDIR}/vrfdmsg 2>>${TDIR}/stderr \
  127.         || pgp_failed ) || cat ${TDIR}/message >${TDIR}/vrfdmsg
  128.  
  129.     cat ${TDIR}/hdrs
  130.     echo ''
  131.     cat ${TDIR}/vrfdmsg
  132.     exit 0
  133. }
  134.  
  135. : ${TMPDIR:=/tmp}
  136. TDIR=`mktemp -d ${TMPDIR}/ppf_mime_decrypt.XXXXXXXX` ||
  137.     {   echo '' >&2
  138.     echo "$0: mktemp failed, exiting" >&2
  139.     fail_common;}
  140.  
  141. trap "rm -f ${TDIR}/* ; rmdir ${TDIR} ; exit" 0 1 2 15
  142.  
  143. csplit -s -k -f ${TDIR}/f - '/^Content-Type: /' '{9}' 2>/dev/null
  144.  
  145. # Reliably find the encrypted file
  146. cfile=`grep -l '^-----BEGIN PGP MESSAGE-----$' ${TDIR}/f*`
  147. case "$cfile" in
  148. '') unencrypted_msg
  149. ;;
  150. esac
  151.  
  152. /usr/bin/gpg --batch --no-tty --passphrase %yourpassphrase% -o${TDIR}/message --decrypt $cfile 2>${TDIR}/stderr || pgp_failed
  153.  
  154. # Determine if we're decrypting MIME message or not
  155. mfile=$( cat ${TDIR}/f0[01] | grep -E -o -m1 '^Content-Type: [a-z]*?/[a-z]*?' | sed -e 's/^Content-Type: //')
  156. case "$mfile" in
  157. "multipart/encrypted")
  158.     decrypt_mime
  159. ;;
  160. "text/plain")
  161.     decrypt_inline
  162. ;;
  163. *)
  164.     echo "$0: unrecognized Content-Type, terminating" >&2
  165.     fail_common
  166. ;;
  167. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement