comment_email_milter.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Milter calls methods of your class at milter events.
  4. # Return REJECT,TEMPFAIL,ACCEPT to short circuit processing for a message.
  5. # You can also add/del recipients, replacebody, add/del headers, etc.
  6. from __future__ import print_function, unicode_literals, absolute_import
  7. import base64
  8. import email
  9. import hashlib
  10. import os
  11. import sys
  12. import time
  13. from io import BytesIO
  14. from multiprocessing import Process as Thread, Queue
  15. import Milter
  16. import requests
  17. import six
  18. from Milter.utils import parse_addr
  19. import pagure.config
  20. import pagure.lib.model_base
  21. import pagure.lib.query
  22. if "PAGURE_CONFIG" not in os.environ and os.path.exists(
  23. "/etc/pagure/pagure.cfg"
  24. ):
  25. os.environ["PAGURE_CONFIG"] = "/etc/pagure/pagure.cfg"
  26. logq = Queue(maxsize=4)
  27. _config = pagure.config.reload_config()
  28. def get_email_body(emailobj):
  29. """ Return the body of the email, preferably in text.
  30. """
  31. def _get_body(emailobj):
  32. """ Return the first text/plain body found if the email is multipart
  33. or just the regular payload otherwise.
  34. """
  35. if emailobj.is_multipart():
  36. for payload in emailobj.get_payload():
  37. # If the message comes with a signature it can be that this
  38. # payload itself has multiple parts, so just return the
  39. # first one
  40. if payload.is_multipart():
  41. return _get_body(payload)
  42. body = payload.get_payload()
  43. if payload.get_content_type() == "text/plain":
  44. return body
  45. else:
  46. return emailobj.get_payload()
  47. body = _get_body(emailobj)
  48. enc = emailobj["Content-Transfer-Encoding"]
  49. if enc == "base64":
  50. body = base64.decodestring(body)
  51. return body
  52. def clean_item(item):
  53. """ For an item provided as <item> return the content, if there are no
  54. <> then return the string.
  55. """
  56. if "<" in item:
  57. item = item.split("<")[1]
  58. if ">" in item:
  59. item = item.split(">")[0]
  60. return item
  61. class PagureMilter(Milter.Base):
  62. def __init__(self): # A new instance with each new connection.
  63. self.id = Milter.uniqueID() # Integer incremented with each call.
  64. self.fp = None
  65. def log(self, message):
  66. print(message)
  67. sys.stdout.flush()
  68. def envfrom(self, mailfrom, *str):
  69. self.log("mail from: %s - %s" % (mailfrom, str))
  70. self.fromparms = Milter.dictfromlist(str)
  71. # NOTE: self.fp is only an *internal* copy of message data. You
  72. # must use addheader, chgheader, replacebody to change the message
  73. # on the MTA.
  74. self.fp = BytesIO()
  75. self.canon_from = "@".join(parse_addr(mailfrom))
  76. from_txt = "From %s %s\n" % (self.canon_from, time.ctime())
  77. self.fp.write(from_txt.encode("utf-8"))
  78. return Milter.CONTINUE
  79. @Milter.noreply
  80. def header(self, name, hval):
  81. """ Headers """
  82. # add header to buffer
  83. header_txt = "%s: %s\n" % (name, hval)
  84. self.fp.write(header_txt.encode("utf-8"))
  85. return Milter.CONTINUE
  86. @Milter.noreply
  87. def eoh(self):
  88. """ End of Headers """
  89. self.fp.write(b"\n")
  90. return Milter.CONTINUE
  91. @Milter.noreply
  92. def body(self, chunk):
  93. """ Body """
  94. self.fp.write(chunk)
  95. return Milter.CONTINUE
  96. @Milter.noreply
  97. def envrcpt(self, to, *str):
  98. rcptinfo = to, Milter.dictfromlist(str)
  99. print(rcptinfo)
  100. return Milter.CONTINUE
  101. def eom(self):
  102. """ End of Message """
  103. self.fp.seek(0)
  104. if six.PY3:
  105. msg = email.message_from_binary_file(self.fp)
  106. else:
  107. msg = email.message_from_file(self.fp)
  108. self.log("To %s" % msg["to"])
  109. self.log("Cc %s" % msg.get("cc"))
  110. self.log("From %s" % msg["From"])
  111. # First check whether the message is addressed to this milter.
  112. email_address = msg["to"]
  113. if "reply+" in msg.get("cc", ""):
  114. email_address = msg["cc"]
  115. if "reply+" not in email_address:
  116. # The message is not addressed to this milter so don't touch it.
  117. self.log(
  118. "No valid recipient email found in To/Cc: %s" % email_address
  119. )
  120. return Milter.ACCEPT
  121. if msg["From"] and msg["From"] == _config.get("FROM_EMAIL"):
  122. self.log("Let's not process the email we send")
  123. return Milter.ACCEPT
  124. msg_id = msg.get("In-Reply-To", None)
  125. if msg_id is None:
  126. self.log("No In-Reply-To, can't process this message.")
  127. self.setreply(
  128. "554",
  129. xcode="5.5.0",
  130. msg="Replies to Pagure must have an In-Reply-To header field."
  131. )
  132. return Milter.REJECT
  133. # Ensure we don't get extra lines in the message-id
  134. msg_id = msg_id.split("\n")[0].strip()
  135. self.log("msg-id %s" % msg_id)
  136. # Ensure the user replied to his/her own notification, not that
  137. # they are trying to forge their ID into someone else's
  138. salt = _config.get("SALT_EMAIL")
  139. from_email = clean_item(msg["From"])
  140. session = pagure.lib.model_base.create_session(_config["DB_URL"])
  141. try:
  142. user = pagure.lib.query.get_user(session, from_email)
  143. except:
  144. self.log(
  145. "Could not find an user in the DB associated with %s"
  146. % from_email
  147. )
  148. session.remove()
  149. self.setreply(
  150. "550",
  151. xcode="5.7.1",
  152. msg="The sender address <%s> isn't recognized." % from_email
  153. )
  154. return Milter.REJECT
  155. hashes = []
  156. for email_obj in user.emails:
  157. m = hashlib.sha512(
  158. b"%s%s%s"
  159. % (
  160. msg_id.encode("utf-8"),
  161. salt.encode("utf-8"),
  162. email_obj.email.encode("utf-8"),
  163. )
  164. )
  165. hashes.append(m.hexdigest())
  166. tohash = email_address.split("@")[0].split("+")[-1]
  167. if tohash not in hashes:
  168. self.log("hash list: %s" % hashes)
  169. self.log("tohash: %s" % tohash)
  170. self.log("Hash does not correspond to the destination")
  171. session.remove()
  172. self.setreply(
  173. "550", xcode="5.7.1", msg="Reply authentication failed."
  174. )
  175. return Milter.REJECT
  176. msg_id = clean_item(msg_id)
  177. try:
  178. if msg_id and "-ticket-" in msg_id:
  179. self.log("Processing issue")
  180. session.remove()
  181. return self.handle_ticket_email(msg, msg_id)
  182. elif msg_id and "-pull-request-" in msg_id:
  183. self.log("Processing pull-request")
  184. session.remove()
  185. return self.handle_request_email(msg, msg_id)
  186. else:
  187. # msg_id passed the hash check, and yet wasn't recognized as
  188. # a message ID generated by Pagure. This is probably a bug,
  189. # because it should be impossible unless an attacker has
  190. # acquired the secret "salt" or broken the hash algorithm.
  191. self.log(
  192. "Not a pagure ticket or pull-request email, rejecting it."
  193. )
  194. session.remove()
  195. self.setreply(
  196. "554",
  197. xcode="5.3.5",
  198. msg="Pagure couldn't determine how to handle the message."
  199. )
  200. return Milter.REJECT
  201. except requests.ReadTimeout as e:
  202. self.setreply(
  203. "451",
  204. xcode="4.4.2",
  205. msg="The comment couldn't be added: " + str(e)
  206. )
  207. return Milter.TEMPFAIL
  208. except requests.ConnectionError as e:
  209. self.setreply(
  210. "451",
  211. xcode="4.4.1",
  212. msg="The comment couldn't be added: " + str(e)
  213. )
  214. return Milter.TEMPFAIL
  215. except requests.RequestException as e:
  216. self.setreply(
  217. "554",
  218. xcode="5.3.0",
  219. msg="The comment couldn't be added: " + str(e)
  220. )
  221. return Milter.REJECT
  222. def handle_ticket_email(self, emailobj, msg_id):
  223. """ Add the email as a comment on a ticket. """
  224. uid = msg_id.split("-ticket-")[-1].split("@")[0]
  225. parent_id = None
  226. if "-" in uid:
  227. uid, parent_id = uid.rsplit("-", 1)
  228. if "/" in uid:
  229. uid = uid.split("/")[0]
  230. self.log("uid %s" % uid)
  231. self.log("parent_id %s" % parent_id)
  232. data = {
  233. "objid": uid,
  234. "comment": get_email_body(emailobj),
  235. "useremail": clean_item(emailobj["From"]),
  236. }
  237. url = _config.get("APP_URL")
  238. if url.endswith("/"):
  239. url = url[:-1]
  240. url = "%s/pv/ticket/comment/" % url
  241. self.log("Calling URL: %s" % url)
  242. req = requests.put(url, data=data)
  243. if req.status_code == 200:
  244. self.log("Comment added")
  245. # The message is now effectively delivered. Tell the MTA to accept
  246. # and discard it.
  247. # If you want the message to be processed by another milter after
  248. # this one, or delivered to a mailbox the usual way, then change
  249. # DROP to ACCEPT.
  250. return Milter.DROP
  251. self.log("Could not add the comment to ticket to pagure")
  252. self.log(req.text)
  253. self.setreply(
  254. "554",
  255. xcode="5.3.0",
  256. msg=(
  257. "The comment couldn't be added to the issue. "
  258. + "HTTP status: %d %s." % (req.status_code, req.reason)
  259. )
  260. )
  261. return Milter.REJECT
  262. def handle_request_email(self, emailobj, msg_id):
  263. """ Add the email as a comment on a request. """
  264. uid = msg_id.split("-pull-request-")[-1].split("@")[0]
  265. parent_id = None
  266. if "-" in uid:
  267. uid, parent_id = uid.rsplit("-", 1)
  268. if "/" in uid:
  269. uid = uid.split("/")[0]
  270. self.log("uid %s" % uid)
  271. self.log("parent_id %s" % parent_id)
  272. data = {
  273. "objid": uid,
  274. "comment": get_email_body(emailobj),
  275. "useremail": clean_item(emailobj["From"]),
  276. }
  277. url = _config.get("APP_URL")
  278. if url.endswith("/"):
  279. url = url[:-1]
  280. url = "%s/pv/pull-request/comment/" % url
  281. self.log("Calling URL: %s" % url)
  282. req = requests.put(url, data=data)
  283. if req.status_code == 200:
  284. self.log("Comment added on PR")
  285. # The message is now effectively delivered. Tell the MTA to accept
  286. # and discard it.
  287. # If you want the message to be processed by another milter after
  288. # this one, or delivered to a mailbox the usual way, then change
  289. # DROP to ACCEPT.
  290. return Milter.DROP
  291. self.log("Could not add the comment to PR to pagure")
  292. self.log(req.text)
  293. self.setreply(
  294. "554",
  295. xcode="5.3.0",
  296. msg=(
  297. "The comment couldn't be added to the pull request. "
  298. + "HTTP status: %d %s." % (req.status_code, req.reason)
  299. )
  300. )
  301. return Milter.REJECT
  302. def background():
  303. while True:
  304. t = logq.get()
  305. if not t:
  306. break
  307. msg, id, ts = t
  308. print(
  309. "%s [%d]"
  310. % (time.strftime("%Y%b%d %H:%M:%S", time.localtime(ts)), id)
  311. )
  312. # 2005Oct13 02:34:11 [1] msg1 msg2 msg3 ...
  313. for i in msg:
  314. print(i)
  315. print
  316. def main():
  317. bt = Thread(target=background)
  318. bt.start()
  319. socketname = "/var/run/pagure/paguresock"
  320. timeout = 600
  321. # Register to have the Milter factory create instances of your class:
  322. Milter.factory = PagureMilter
  323. print("%s pagure milter startup" % time.strftime("%Y%b%d %H:%M:%S"))
  324. sys.stdout.flush()
  325. Milter.runmilter("paguremilter", socketname, timeout)
  326. logq.put(None)
  327. bt.join()
  328. print("%s pagure milter shutdown" % time.strftime("%Y%b%d %H:%M:%S"))
  329. if __name__ == "__main__":
  330. main()