Patchwork [v2] mail: retain hostname for sslutil.wrapsocket (issue5203)

login
register
mail settings
Submitter timeless
Date April 15, 2016, 9:18 p.m.
Message ID <0ae265320d1d7f97525e.1460755137@gcc2-power8.osuosl.org>
Download mbox | patch
Permalink /patch/14653/
State Accepted
Headers show

Comments

timeless - April 15, 2016, 9:18 p.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1460742227 0
#      Fri Apr 15 17:43:47 2016 +0000
# Node ID 0ae265320d1d7f97525ec746d6fc955bb95ac9f0
# Parent  531dea16f4f754fd76e2f6434662b7dc154e704a
# Available At bb://timeless/mercurial-crew
#              hg pull bb://timeless/mercurial-crew -r 0ae265320d1d
mail: retain 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.
Pierre-Yves David - April 16, 2016, 12:38 a.m.
Looks good to me, pushed.
Thanks for fixing this.

On 04/15/2016 02:18 PM, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1460742227 0
> #      Fri Apr 15 17:43:47 2016 +0000
> # Node ID 0ae265320d1d7f97525ec746d6fc955bb95ac9f0
> # Parent  531dea16f4f754fd76e2f6434662b7dc154e704a
> # Available At bb://timeless/mercurial-crew
> #              hg pull bb://timeless/mercurial-crew -r 0ae265320d1d
> mail: retain 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 0ae265320d1d 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:
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff -r 531dea16f4f7 -r 0ae265320d1d 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: