[PATCH] mail: retrain hostname for sslutil.wrapsocket (issue5203)

timeless timeless at fmr.im
Fri Apr 15 18:18:46 UTC 2016


# HG changeset patch
# User timeless <timeless at mozdev.org>
# Date 1460742227 0
#      Fri Apr 15 17:43:47 2016 +0000
# Node ID 67ba2a0619ae1d49a8aa8957e2c6c481be816ac2
# Parent  531dea16f4f754fd76e2f6434662b7dc154e704a
# Available At bb://timeless/mercurial-crew
#              hg pull bb://timeless/mercurial-crew -r 67ba2a0619ae
mail: retrain hostname for sslutil.wrapsocket (issue5203)

SMTPS + STARTTLS need to provide serverhostname,
and we can't store it in sslkwargs because that breaks
something involving the https protocol.

diff -r 531dea16f4f7 -r 67ba2a0619ae mercurial/mail.py
--- a/mercurial/mail.py	Tue Apr 12 14:43:36 2016 +0000
+++ b/mercurial/mail.py	Fri Apr 15 17:43:47 2016 +0000
@@ -48,9 +48,10 @@
 
     This class allows to pass any keyword arguments to SSL socket creation.
     '''
-    def __init__(self, sslkwargs, **kwargs):
+    def __init__(self, sslkwargs, host=None, **kwargs):
         smtplib.SMTP.__init__(self, **kwargs)
         self._sslkwargs = sslkwargs
+        self._host = host
 
     def starttls(self, keyfile=None, certfile=None):
         if not self.has_extn("starttls"):
@@ -59,6 +60,7 @@
         (resp, reply) = self.docmd("STARTTLS")
         if resp == 220:
             self.sock = sslutil.wrapsocket(self.sock, keyfile, certfile,
+                                           serverhostname=self._host,
                                            **self._sslkwargs)
             self.file = smtplib.SSLFakeFile(self.sock)
             self.helo_resp = None
@@ -72,10 +74,12 @@
 
     This class allows to pass any keyword arguments to SSL socket creation.
     '''
-    def __init__(self, sslkwargs, keyfile=None, certfile=None, **kwargs):
+    def __init__(self, sslkwargs, keyfile=None, certfile=None, host=None,
+                 **kwargs):
         self.keyfile = keyfile
         self.certfile = certfile
         smtplib.SMTP.__init__(self, **kwargs)
+        self._host = host
         self.default_port = smtplib.SMTP_SSL_PORT
         self._sslkwargs = sslkwargs
 
@@ -85,6 +89,7 @@
         new_socket = socket.create_connection((host, port), timeout)
         new_socket = sslutil.wrapsocket(new_socket,
                                         self.keyfile, self.certfile,
+                                        serverhostname=self._host,
                                         **self._sslkwargs)
         self.file = smtplib.SSLFakeFile(new_socket)
         return new_socket
@@ -114,9 +119,9 @@
         sslkwargs = {'ui': ui}
     if smtps:
         ui.note(_('(using smtps)\n'))
-        s = SMTPS(sslkwargs, local_hostname=local_hostname)
+        s = SMTPS(sslkwargs, local_hostname=local_hostname, host=mailhost)
     elif starttls:
-        s = STARTTLS(sslkwargs, local_hostname=local_hostname)
+        s = STARTTLS(sslkwargs, local_hostname=local_hostname, host=mailhost)
     else:
         s = smtplib.SMTP(local_hostname=local_hostname)
     if smtps:


More information about the Mercurial-devel mailing list