Advertisement
Guest User

Untitled

a guest
May 10th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.31 KB | None | 0 0
  1. #!/usr/bin/sh
  2. #mailstat.sh
  3.  
  4. #a couple of temp files
  5. TEMPFILE="/home/peloruso/${RANDOM}${RANDOM}${RANDOM}"
  6. XSRTEMP="/home/peloruso/${RANDOM}${RANDOM}${RANDOM}"
  7.  
  8. #set these to your correct information
  9. SQL_USER=mysqluser
  10. SQL_PW=password
  11. SQL_DB=mysqldatabase
  12. SQL_TABLE=mysqltable
  13.  
  14. #procmail sends the current message to the program in STDIN, so use tee to put the message in
  15. #a file so we can use it over and over
  16. tee ${TEMPFILE}
  17.  
  18.  
  19. #requires the X-SHA1SUM header to exist.
  20. # :0w:${PMDIR}/sha1sum.lock
  21. # SHA1SUM=|/usr/bin/sha1sum
  22. #
  23. # :0fw:${PMDIR}/formail.lock
  24. # | /usr/bin/formail -A "X-SHA1SUM: ${SHA1SUM}"
  25. SHA1SUM=`/usr/bin/formail -x "X-SHA1SUM:"<${TEMPFILE}`
  26. SHA1SUM=`echo ${SHA1SUM}|sed 's/^ *//;s/ -$//'`
  27.  
  28.  
  29. #requires the X-PM-User header to exist.
  30. # :0fw:${PMDIR}/formail.lock
  31. # | /usr/bin/formail -A "X-PM-User: ${USER}"
  32. PMUSER=`/usr/bin/formail -x "X-PM-User:"<${TEMPFILE}`
  33. PMUSER=`echo ${PMUSER}|sed 's/^ *//'`
  34.  
  35.  
  36. #in .spamassassin/local.cf, add the following line to create the necessary header
  37. #add_header all Rules _TESTS(,)_
  38. XSR=`/usr/bin/formail -x "X-Spam-Rules:"<${TEMPFILE}`
  39.  
  40.  
  41. #in .spamassassin/local.cf, add the following line to create the necessary header
  42. #add_header all Score _SCORE_
  43. SCORE=`/usr/bin/formail -x "X-Spam-Score:"<${TEMPFILE}`
  44. SCORE=`echo ${SCORE}|sed 's/^ *//'`
  45.  
  46.  
  47. #requires the X-Orig-Class header to exist.
  48. # :0fw:${PMDIR}/formail.lock
  49. # * X-Spam-Level: RRR
  50. # | /usr/bin/formail -A "X-Orig-Class: SPAM"
  51. #
  52. # :0Efw:${PMDIR}/formail.lock
  53. # | /usr/bin/formail -A "X-Orig-Class: HAM"
  54. ORIG_CLASS=`/usr/bin/formail -x "X-Orig-Class:"<${TEMPFILE}`
  55. ORIG_CLASS=`echo ${ORIG_CLASS}|sed 's/^ *//'`
  56.  
  57.  
  58. #requires the X-Sender-in-whitelist header to exist
  59. # :0fw:${PMDIR}/formail.lock
  60. # * ^X-Spam-Report:.*USER_IN_WHITELIST
  61. # | /usr/bin/formail -A "X-Sender-in-whitelist: Yes"
  62. #
  63. # :0Efw:${PMDIR}/formail.lock
  64. # | /usr/bin/formail -A "X-Sender-in-whitelist: No"
  65. SENDER_IN_WHITELIST=`/usr/bin/formail -x "X-Sender-in-whitelist:"<${TEMPFILE}`
  66. SENDER_IN_WHITELIST=`echo ${SENDER_IN_WHITELIST}|sed 's/^ *//'`
  67.  
  68.  
  69. #requires the X-Sender-in-blacklist header to exist
  70. # :0fw:${PMDIR}/formail.lock
  71. # * ^X-Spam-Report:.*USER_IN_BLACKLIST
  72. # | /usr/bin/formail -A "X-Sender-in-blacklist: Yes"
  73. #
  74. # :0Efw:${PMDIR}/formail.lock
  75. # | /usr/bin/formail -A "X-Sender-in-blacklist: No"
  76. SENDER_IN_BLACKLIST=`/usr/bin/formail -x "X-Sender-in-blacklist:"<${TEMPFILE}`
  77. SENDER_IN_BLACKLIST=`echo ${SENDER_IN_BLACKLIST}|sed 's/^ *//'`
  78.  
  79.  
  80. #pipe the X-Spam-Rules header through 'tr' and 'sed' to format correctly.
  81. #specifically, change all of the commas to newlines using tr (put one rule on each line)
  82. #then make sure there are no blank lines using sed
  83. /usr/bin/formail -x "X-Spam-Rules" <${TEMPFILE}|  tr ',' '\n' |sed '/^$/d'>${XSRTEMP}
  84.  
  85.  
  86. #used to determine if the mail is one of my approved "commercial" emails, or for a mailing list
  87. #for example,
  88. # :0fw:${PMDIR}/formail.lock
  89. # | /usr/bin/formail -A "X-folder-delivered-to: ${DEFAULT}Lists/cur"
  90. DEST=`/usr/bin/formail -x "X-folder-delivered-to:"<${TEMPFILE}`
  91. DEST=`echo ${DEST}|sed 's/^ *//'`
  92. DEST=`echo ${DEST}|sed 's/\/home\/peloruso\/mail\/pelorus.org\/skip\/\.//'`
  93. DEST=`echo ${DEST}|sed 's/\/home\/peloruso\/mail\/pelorus.org\/suzanne\/\.//'`
  94. DEST=`echo ${DEST}|sed 's/\/cur.*//'`
  95.  
  96. #count the number of rules that were hit
  97. RULECOUNT=`wc -l ${XSRTEMP}|grep [0-9]* -o`
  98.  
  99.  
  100. #add the columns to the database.  I "try" to add a column for every rule that was hit.  Of course,
  101. #if the column already exists, it will not recreate the column, so it generates an error in the
  102. #procmail log.  Also build "Q2", which is similar to the ALTER statement, except that it will be
  103. #used in the INSERT statement.  Note the XSRTEMP redirection after the 'done' statement on the
  104. #last line.  So, each time through the do loop, this will try to add a new column with the name
  105. #of the rule.  It will also build Q2 which is used in the QUERY statement a little later.
  106. #Q2 will look something like this (starting with the first comma):
  107. # , 'BAYES_99', 'AWL', 'HTML_MESSAGE'
  108. while read rules
  109. do
  110.    mysql -u ${SQL_USER} --password=${SQL_PW} -e"ALTER TABLE \`$SQL_DB\`.\`$SQL_TABLE\` ADD $rules BOOL NOT NULL DEFAULT 0"
  111.    Q2="$Q2, \`$rules\`"
  112. done <${XSRTEMP}
  113.  
  114.  
  115. #Now build a string that will basically have a ,'1' for each rule that was hit.  The logic here
  116. #is that if the rule was listed here, it was hit, so the value will be '1'.  Since I don't
  117. #update the values of *ALL* columns--just the ones that were actually hit, they all get a '1'
  118. #So, like Q2 above, this will build a string for the QUERY statement below that will look like
  119. #this (starting with the first comma)
  120. # , '1', '1'
  121. for (( i=0; i<$RULECOUNT; i++))
  122. do
  123.   RULESTRING="${RULESTRING}, '1'"
  124. done
  125.  
  126. #Calculate the values of WHITELISTED and BLACKLISTED
  127. if [ "$SENDER_IN_WHITELIST" = Yes ]
  128. then
  129.   WHITELISTED=1
  130. else
  131.   WHITELISTED=0
  132. fi
  133.  
  134. if [ "$SENDER_IN_BLACKLIST" = Yes ]
  135. then
  136.   BLACKLISTED=1
  137. else
  138.   BLACKLISTED=0
  139. fi
  140.  
  141.  
  142. #determine if the message was an approved commercial email, or from a mailing list
  143. #If there are more than two, a case statement would be more appropriate
  144. if [ "$DEST" = Lists ]
  145. then
  146.   MAILING_LIST=1
  147. else
  148.   MAILING_LIST=0
  149. fi
  150.  
  151. if [ "$DEST" = Commercial ]
  152. then
  153.   COMMERCIAL=1
  154. else
  155.   COMMERCIAL=0
  156. fi
  157.  
  158.  
  159. #Now I have all of the variables.  I can build my QUERY.
  160. #A typical query will look like this:
  161. # INSERT INTO `peloruso_mailstats`.`messages` (`sha1sum`, `username`, `orig_class`, `score`,
  162. # `mail_list`, `commercial`, `whitelist`, `blacklist`, `AWL`, `BAYES_20`, `NORMAL_HTTP_TO_IP`,
  163. # `ONLINE_PHARMACY`, `SPF_PASS`, `TVD_VISIT_PHARMA`, `URIBL_JP_SURBL`, `URIBL_OB_SURBL`, `URICOUNTRY_DE`)
  164. # VALUES ('f3e4406dab1313c9586bcfcf01ec8332582a071a', 'skip', 'SPAM', '10.0', '0', '0', '0' , '0' ,
  165. # '1', '1', '1', '1', '1', '1', '1', '1', '1')
  166. QUERY="INSERT INTO \`${SQL_DB}\`.\`${SQL_TABLE}\` (\`sha1sum\`, \`username\`, \`orig_class\`, \`score\`, \`mail_list\`, \`commercial\`, \`whitelist\`, \`blacklist\`$Q2) \
  167.  VALUES ('${SHA1SUM}', '${PMUSER}', '${ORIG_CLASS}', '${SCORE}', '${MAILING_LIST}', '${COMMERCIAL}', '${WHITELISTED}' , '${BLACKLISTED}' ${RULESTRING})"
  168.  
  169. #I do save a copy of the query for debugging
  170. echo "QUERY=${QUERY}">>/home/peloruso/mailstat.log
  171.  
  172. #execute the query
  173. mysql -u ${SQL_USER} --password=${SQL_PW} -e "${QUERY}"
  174.  
  175. #delete the temp files
  176. rm -f ${TEMPFILE} ${XSRTEMP}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement