46 class STARTTLS(smtplib.SMTP): |
46 class STARTTLS(smtplib.SMTP): |
47 '''Derived class to verify the peer certificate for STARTTLS. |
47 '''Derived class to verify the peer certificate for STARTTLS. |
48 |
48 |
49 This class allows to pass any keyword arguments to SSL socket creation. |
49 This class allows to pass any keyword arguments to SSL socket creation. |
50 ''' |
50 ''' |
51 def __init__(self, sslkwargs, host=None, **kwargs): |
51 def __init__(self, ui, sslkwargs, host=None, **kwargs): |
52 smtplib.SMTP.__init__(self, **kwargs) |
52 smtplib.SMTP.__init__(self, **kwargs) |
|
53 self._ui = ui |
53 self._sslkwargs = sslkwargs |
54 self._sslkwargs = sslkwargs |
54 self._host = host |
55 self._host = host |
55 |
56 |
56 def starttls(self, keyfile=None, certfile=None): |
57 def starttls(self, keyfile=None, certfile=None): |
57 if not self.has_extn("starttls"): |
58 if not self.has_extn("starttls"): |
58 msg = "STARTTLS extension not supported by server" |
59 msg = "STARTTLS extension not supported by server" |
59 raise smtplib.SMTPException(msg) |
60 raise smtplib.SMTPException(msg) |
60 (resp, reply) = self.docmd("STARTTLS") |
61 (resp, reply) = self.docmd("STARTTLS") |
61 if resp == 220: |
62 if resp == 220: |
62 self.sock = sslutil.wrapsocket(self.sock, keyfile, certfile, |
63 self.sock = sslutil.wrapsocket(self.sock, keyfile, certfile, |
|
64 ui=self._ui, |
63 serverhostname=self._host, |
65 serverhostname=self._host, |
64 **self._sslkwargs) |
66 **self._sslkwargs) |
65 self.file = smtplib.SSLFakeFile(self.sock) |
67 self.file = smtplib.SSLFakeFile(self.sock) |
66 self.helo_resp = None |
68 self.helo_resp = None |
67 self.ehlo_resp = None |
69 self.ehlo_resp = None |
72 class SMTPS(smtplib.SMTP): |
74 class SMTPS(smtplib.SMTP): |
73 '''Derived class to verify the peer certificate for SMTPS. |
75 '''Derived class to verify the peer certificate for SMTPS. |
74 |
76 |
75 This class allows to pass any keyword arguments to SSL socket creation. |
77 This class allows to pass any keyword arguments to SSL socket creation. |
76 ''' |
78 ''' |
77 def __init__(self, sslkwargs, keyfile=None, certfile=None, host=None, |
79 def __init__(self, ui, sslkwargs, keyfile=None, certfile=None, host=None, |
78 **kwargs): |
80 **kwargs): |
79 self.keyfile = keyfile |
81 self.keyfile = keyfile |
80 self.certfile = certfile |
82 self.certfile = certfile |
81 smtplib.SMTP.__init__(self, **kwargs) |
83 smtplib.SMTP.__init__(self, **kwargs) |
82 self._host = host |
84 self._host = host |
83 self.default_port = smtplib.SMTP_SSL_PORT |
85 self.default_port = smtplib.SMTP_SSL_PORT |
|
86 self._ui = ui |
84 self._sslkwargs = sslkwargs |
87 self._sslkwargs = sslkwargs |
85 |
88 |
86 def _get_socket(self, host, port, timeout): |
89 def _get_socket(self, host, port, timeout): |
87 if self.debuglevel > 0: |
90 if self.debuglevel > 0: |
88 print('connect:', (host, port), file=sys.stderr) |
91 print('connect:', (host, port), file=sys.stderr) |
89 new_socket = socket.create_connection((host, port), timeout) |
92 new_socket = socket.create_connection((host, port), timeout) |
90 new_socket = sslutil.wrapsocket(new_socket, |
93 new_socket = sslutil.wrapsocket(new_socket, |
91 self.keyfile, self.certfile, |
94 self.keyfile, self.certfile, |
|
95 ui=self._ui, |
92 serverhostname=self._host, |
96 serverhostname=self._host, |
93 **self._sslkwargs) |
97 **self._sslkwargs) |
94 self.file = smtplib.SSLFakeFile(new_socket) |
98 self.file = smtplib.SSLFakeFile(new_socket) |
95 return new_socket |
99 return new_socket |
96 |
100 |
113 % (verifycert)) |
117 % (verifycert)) |
114 verifycert = False |
118 verifycert = False |
115 if (starttls or smtps) and verifycert: |
119 if (starttls or smtps) and verifycert: |
116 sslkwargs = sslutil.sslkwargs(ui, mailhost) |
120 sslkwargs = sslutil.sslkwargs(ui, mailhost) |
117 else: |
121 else: |
118 # 'ui' is required by sslutil.wrapsocket() and set by sslkwargs() |
122 sslkwargs = {} |
119 sslkwargs = {'ui': ui} |
123 |
120 if smtps: |
124 if smtps: |
121 ui.note(_('(using smtps)\n')) |
125 ui.note(_('(using smtps)\n')) |
122 s = SMTPS(sslkwargs, local_hostname=local_hostname, host=mailhost) |
126 s = SMTPS(ui, sslkwargs, local_hostname=local_hostname, host=mailhost) |
123 elif starttls: |
127 elif starttls: |
124 s = STARTTLS(sslkwargs, local_hostname=local_hostname, host=mailhost) |
128 s = STARTTLS(ui, sslkwargs, local_hostname=local_hostname, |
|
129 host=mailhost) |
125 else: |
130 else: |
126 s = smtplib.SMTP(local_hostname=local_hostname) |
131 s = smtplib.SMTP(local_hostname=local_hostname) |
127 if smtps: |
132 if smtps: |
128 defaultport = 465 |
133 defaultport = 465 |
129 else: |
134 else: |