Mercurial > cpython-lru_cache-weakref
annotate Lib/smtpd.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | d2f303861c98 |
| children |
| rev | line source |
|---|---|
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
|
19235
c6129428e053
found_terminator(): Add a debug print showing the data.
Barry Warsaw <barry@python.org>
parents:
19213
diff
changeset
|
2 """An RFC 2821 smtp proxy. |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
3 |
|
20251
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
4 Usage: %(program)s [options] [localhost:localport [remotehost:remoteport]] |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
5 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
6 Options: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
7 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
8 --nosetuid |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
9 -n |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
10 This program generally tries to setuid `nobody', unless this flag is |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
11 set. The setuid call will fail if this program is not run as root (in |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
12 which case, use this flag). |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
13 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
14 --version |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
15 -V |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
16 Print the version number and exit. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
17 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
18 --class classname |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
19 -c classname |
|
33704
295bd8c55a78
Fix for SF bug # 1010102. The default is PureProxy not SMTPProxy.
Barry Warsaw <barry@python.org>
parents:
32640
diff
changeset
|
20 Use `classname' as the concrete SMTP proxy class. Uses `PureProxy' by |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
21 default. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
22 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
23 --debug |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
24 -d |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
25 Turn on debugging prints. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
26 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
27 --help |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
28 -h |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
29 Print this message and exit. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
30 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
31 Version: %(__version__)s |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
32 |
|
20251
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
33 If localhost is not given then `localhost' is used, and if localport is not |
|
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
34 given then 8025 is used. If remotehost is not given then `localhost' is used, |
|
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
35 and if remoteport is not given, then 25 is used. |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
36 """ |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
37 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
38 # Overview: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
39 # |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
40 # This file implements the minimal SMTP protocol as defined in RFC 821. It |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
41 # has a hierarchy of classes which implement the backend functionality for the |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
42 # smtpd. A number of classes are provided: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
43 # |
|
17527
01be08333308
Fix typo in exception name (UnimplementedError should be
Guido van Rossum <guido@python.org>
parents:
16948
diff
changeset
|
44 # SMTPServer - the base class for the backend. Raises NotImplementedError |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
45 # if you try to use it. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
46 # |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
47 # DebuggingServer - simply prints each message it receives on stdout. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
48 # |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
49 # PureProxy - Proxies all messages to a real smtpd which does final |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
50 # delivery. One known problem with this class is that it doesn't handle |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
51 # SMTP errors from the backend server at all. This should be fixed |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
52 # (contributions are welcome!). |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
53 # |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
54 # MailmanProxy - An experimental hack to work with GNU Mailman |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
55 # <www.list.org>. Using this server as your real incoming smtpd, your |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
56 # mailhost will automatically recognize and accept mail destined to Mailman |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
57 # lists when those lists are created. Every message not destined for a list |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
58 # gets forwarded to a real backend smtpd, as with PureProxy. Again, errors |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
59 # are not handled correctly yet. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
60 # |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
61 # Please note that this script requires Python 2.0 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
62 # |
|
32640
da18210e311c
Updated my email address to something that works <wink>.
Barry Warsaw <barry@python.org>
parents:
32415
diff
changeset
|
63 # Author: Barry Warsaw <barry@python.org> |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
64 # |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
65 # TODO: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
66 # |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
67 # - support mailbox delivery |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
68 # - alias files |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
69 # - ESMTP |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
70 # - handle error codes from the backend smtpd |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
71 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
72 import sys |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
73 import os |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
74 import errno |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
75 import getopt |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
76 import time |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
77 import socket |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
78 import asyncore |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
79 import asynchat |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
80 |
| 16613 | 81 __all__ = ["SMTPServer","DebuggingServer","PureProxy","MailmanProxy"] |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
82 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
83 program = sys.argv[0] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
84 __version__ = 'Python SMTP proxy version 0.2' |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
85 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
86 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
87 class Devnull: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
88 def write(self, msg): pass |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
89 def flush(self): pass |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
90 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
91 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
92 DEBUGSTREAM = Devnull() |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
93 NEWLINE = '\n' |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
94 EMPTYSTRING = '' |
|
20251
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
95 COMMASPACE = ', ' |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
96 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
97 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
98 def usage(code, msg=''): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
99 print >> sys.stderr, __doc__ % globals() |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
100 if msg: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
101 print >> sys.stderr, msg |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
102 sys.exit(code) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
103 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
104 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
105 class SMTPChannel(asynchat.async_chat): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
106 COMMAND = 0 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
107 DATA = 1 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
108 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
109 def __init__(self, server, conn, addr): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
110 asynchat.async_chat.__init__(self, conn) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
111 self.__server = server |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
112 self.__conn = conn |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
113 self.__addr = addr |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
114 self.__line = [] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
115 self.__state = self.COMMAND |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
116 self.__greeting = 0 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
117 self.__mailfrom = None |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
118 self.__rcpttos = [] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
119 self.__data = '' |
|
20251
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
120 self.__fqdn = socket.getfqdn() |
|
66785
aa30b16d07bc
backporting security fix of issue 9129 (smtpd module vulnerable to DoS attacks in case of connection bashing)
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
62658
diff
changeset
|
121 try: |
|
aa30b16d07bc
backporting security fix of issue 9129 (smtpd module vulnerable to DoS attacks in case of connection bashing)
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
62658
diff
changeset
|
122 self.__peer = conn.getpeername() |
|
64256
e716aa2094fa
Merged revisions 84289 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
62658
diff
changeset
|
123 except socket.error, err: |
|
66785
aa30b16d07bc
backporting security fix of issue 9129 (smtpd module vulnerable to DoS attacks in case of connection bashing)
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
62658
diff
changeset
|
124 # a race condition may occur if the other end is closing |
|
aa30b16d07bc
backporting security fix of issue 9129 (smtpd module vulnerable to DoS attacks in case of connection bashing)
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
62658
diff
changeset
|
125 # before we can get the peername |
|
aa30b16d07bc
backporting security fix of issue 9129 (smtpd module vulnerable to DoS attacks in case of connection bashing)
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
62658
diff
changeset
|
126 self.close() |
|
64256
e716aa2094fa
Merged revisions 84289 via svnmerge from
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
62658
diff
changeset
|
127 if err[0] != errno.ENOTCONN: |
|
66785
aa30b16d07bc
backporting security fix of issue 9129 (smtpd module vulnerable to DoS attacks in case of connection bashing)
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
62658
diff
changeset
|
128 raise |
|
aa30b16d07bc
backporting security fix of issue 9129 (smtpd module vulnerable to DoS attacks in case of connection bashing)
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
62658
diff
changeset
|
129 return |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
130 print >> DEBUGSTREAM, 'Peer:', repr(self.__peer) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
131 self.push('220 %s %s' % (self.__fqdn, __version__)) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
132 self.set_terminator('\r\n') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
133 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
134 # Overrides base class for convenience |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
135 def push(self, msg): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
136 asynchat.async_chat.push(self, msg + '\r\n') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
137 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
138 # Implementation of base class abstract method |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
139 def collect_incoming_data(self, data): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
140 self.__line.append(data) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
141 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
142 # Implementation of base class abstract method |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
143 def found_terminator(self): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
144 line = EMPTYSTRING.join(self.__line) |
|
19235
c6129428e053
found_terminator(): Add a debug print showing the data.
Barry Warsaw <barry@python.org>
parents:
19213
diff
changeset
|
145 print >> DEBUGSTREAM, 'Data:', repr(line) |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
146 self.__line = [] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
147 if self.__state == self.COMMAND: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
148 if not line: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
149 self.push('500 Error: bad syntax') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
150 return |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
151 method = None |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
152 i = line.find(' ') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
153 if i < 0: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
154 command = line.upper() |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
155 arg = None |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
156 else: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
157 command = line[:i].upper() |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
158 arg = line[i+1:].strip() |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
159 method = getattr(self, 'smtp_' + command, None) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
160 if not method: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
161 self.push('502 Error: command "%s" not implemented' % command) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
162 return |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
163 method(arg) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
164 return |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
165 else: |
|
16948
faa52f384af6
Use != instead of <>. Sorry, Barry.
Guido van Rossum <guido@python.org>
parents:
16613
diff
changeset
|
166 if self.__state != self.DATA: |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
167 self.push('451 Internal confusion') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
168 return |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
169 # Remove extraneous carriage returns and de-transparency according |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
170 # to RFC 821, Section 4.5.2. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
171 data = [] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
172 for text in line.split('\r\n'): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
173 if text and text[0] == '.': |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
174 data.append(text[1:]) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
175 else: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
176 data.append(text) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
177 self.__data = NEWLINE.join(data) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
178 status = self.__server.process_message(self.__peer, |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
179 self.__mailfrom, |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
180 self.__rcpttos, |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
181 self.__data) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
182 self.__rcpttos = [] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
183 self.__mailfrom = None |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
184 self.__state = self.COMMAND |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
185 self.set_terminator('\r\n') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
186 if not status: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
187 self.push('250 Ok') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
188 else: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
189 self.push(status) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
190 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
191 # SMTP and ESMTP commands |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
192 def smtp_HELO(self, arg): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
193 if not arg: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
194 self.push('501 Syntax: HELO hostname') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
195 return |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
196 if self.__greeting: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
197 self.push('503 Duplicate HELO/EHLO') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
198 else: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
199 self.__greeting = arg |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
200 self.push('250 %s' % self.__fqdn) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
201 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
202 def smtp_NOOP(self, arg): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
203 if arg: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
204 self.push('501 Syntax: NOOP') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
205 else: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
206 self.push('250 Ok') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
207 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
208 def smtp_QUIT(self, arg): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
209 # args is ignored |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
210 self.push('221 Bye') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
211 self.close_when_done() |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
212 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
213 # factored |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
214 def __getaddr(self, keyword, arg): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
215 address = None |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
216 keylen = len(keyword) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
217 if arg[:keylen].upper() == keyword: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
218 address = arg[keylen:].strip() |
|
20862
d9b9b5b4815b
Two bug fixes for problems reported by Sverre:
Barry Warsaw <barry@python.org>
parents:
20332
diff
changeset
|
219 if not address: |
|
d9b9b5b4815b
Two bug fixes for problems reported by Sverre:
Barry Warsaw <barry@python.org>
parents:
20332
diff
changeset
|
220 pass |
|
d9b9b5b4815b
Two bug fixes for problems reported by Sverre:
Barry Warsaw <barry@python.org>
parents:
20332
diff
changeset
|
221 elif address[0] == '<' and address[-1] == '>' and address != '<>': |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
222 # Addresses can be in the form <person@dom.com> but watch out |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
223 # for null address, e.g. <> |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
224 address = address[1:-1] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
225 return address |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
226 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
227 def smtp_MAIL(self, arg): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
228 print >> DEBUGSTREAM, '===> MAIL', arg |
| 43641 | 229 address = self.__getaddr('FROM:', arg) if arg else None |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
230 if not address: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
231 self.push('501 Syntax: MAIL FROM:<address>') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
232 return |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
233 if self.__mailfrom: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
234 self.push('503 Error: nested MAIL command') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
235 return |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
236 self.__mailfrom = address |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
237 print >> DEBUGSTREAM, 'sender:', self.__mailfrom |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
238 self.push('250 Ok') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
239 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
240 def smtp_RCPT(self, arg): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
241 print >> DEBUGSTREAM, '===> RCPT', arg |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
242 if not self.__mailfrom: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
243 self.push('503 Error: need MAIL command') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
244 return |
| 43659 | 245 address = self.__getaddr('TO:', arg) if arg else None |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
246 if not address: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
247 self.push('501 Syntax: RCPT TO: <address>') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
248 return |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
249 self.__rcpttos.append(address) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
250 print >> DEBUGSTREAM, 'recips:', self.__rcpttos |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
251 self.push('250 Ok') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
252 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
253 def smtp_RSET(self, arg): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
254 if arg: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
255 self.push('501 Syntax: RSET') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
256 return |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
257 # Resets the sender, recipients, and data, but not the greeting |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
258 self.__mailfrom = None |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
259 self.__rcpttos = [] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
260 self.__data = '' |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
261 self.__state = self.COMMAND |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
262 self.push('250 Ok') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
263 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
264 def smtp_DATA(self, arg): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
265 if not self.__rcpttos: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
266 self.push('503 Error: need RCPT command') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
267 return |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
268 if arg: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
269 self.push('501 Syntax: DATA') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
270 return |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
271 self.__state = self.DATA |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
272 self.set_terminator('\r\n.\r\n') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
273 self.push('354 End data with <CR><LF>.<CR><LF>') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
274 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
275 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
276 class SMTPServer(asyncore.dispatcher): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
277 def __init__(self, localaddr, remoteaddr): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
278 self._localaddr = localaddr |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
279 self._remoteaddr = remoteaddr |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
280 asyncore.dispatcher.__init__(self) |
|
62658
76bc255d29fd
fix issue #6589: cleanup asyncore.socket_map if smtpd.SMTPServer constructor raises an exception
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55049
diff
changeset
|
281 try: |
|
76bc255d29fd
fix issue #6589: cleanup asyncore.socket_map if smtpd.SMTPServer constructor raises an exception
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55049
diff
changeset
|
282 self.create_socket(socket.AF_INET, socket.SOCK_STREAM) |
|
76bc255d29fd
fix issue #6589: cleanup asyncore.socket_map if smtpd.SMTPServer constructor raises an exception
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55049
diff
changeset
|
283 # try to re-use a server port if possible |
|
76bc255d29fd
fix issue #6589: cleanup asyncore.socket_map if smtpd.SMTPServer constructor raises an exception
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55049
diff
changeset
|
284 self.set_reuse_addr() |
|
76bc255d29fd
fix issue #6589: cleanup asyncore.socket_map if smtpd.SMTPServer constructor raises an exception
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55049
diff
changeset
|
285 self.bind(localaddr) |
|
76bc255d29fd
fix issue #6589: cleanup asyncore.socket_map if smtpd.SMTPServer constructor raises an exception
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55049
diff
changeset
|
286 self.listen(5) |
|
76bc255d29fd
fix issue #6589: cleanup asyncore.socket_map if smtpd.SMTPServer constructor raises an exception
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55049
diff
changeset
|
287 except: |
|
76bc255d29fd
fix issue #6589: cleanup asyncore.socket_map if smtpd.SMTPServer constructor raises an exception
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55049
diff
changeset
|
288 # cleanup asyncore.socket_map before raising |
|
76bc255d29fd
fix issue #6589: cleanup asyncore.socket_map if smtpd.SMTPServer constructor raises an exception
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55049
diff
changeset
|
289 self.close() |
|
76bc255d29fd
fix issue #6589: cleanup asyncore.socket_map if smtpd.SMTPServer constructor raises an exception
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55049
diff
changeset
|
290 raise |
|
76bc255d29fd
fix issue #6589: cleanup asyncore.socket_map if smtpd.SMTPServer constructor raises an exception
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55049
diff
changeset
|
291 else: |
|
76bc255d29fd
fix issue #6589: cleanup asyncore.socket_map if smtpd.SMTPServer constructor raises an exception
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55049
diff
changeset
|
292 print >> DEBUGSTREAM, \ |
|
76bc255d29fd
fix issue #6589: cleanup asyncore.socket_map if smtpd.SMTPServer constructor raises an exception
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55049
diff
changeset
|
293 '%s started at %s\n\tLocal addr: %s\n\tRemote addr:%s' % ( |
|
76bc255d29fd
fix issue #6589: cleanup asyncore.socket_map if smtpd.SMTPServer constructor raises an exception
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55049
diff
changeset
|
294 self.__class__.__name__, time.ctime(time.time()), |
|
76bc255d29fd
fix issue #6589: cleanup asyncore.socket_map if smtpd.SMTPServer constructor raises an exception
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
55049
diff
changeset
|
295 localaddr, remoteaddr) |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
296 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
297 def handle_accept(self): |
|
65861
f0300584c824
Fix Issue 6706: return None on connect() in case of EWOULDBLOCK/ECONNABORTED error.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64256
diff
changeset
|
298 pair = self.accept() |
|
f0300584c824
Fix Issue 6706: return None on connect() in case of EWOULDBLOCK/ECONNABORTED error.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64256
diff
changeset
|
299 if pair is not None: |
|
f0300584c824
Fix Issue 6706: return None on connect() in case of EWOULDBLOCK/ECONNABORTED error.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64256
diff
changeset
|
300 conn, addr = pair |
|
f0300584c824
Fix Issue 6706: return None on connect() in case of EWOULDBLOCK/ECONNABORTED error.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64256
diff
changeset
|
301 print >> DEBUGSTREAM, 'Incoming connection from %s' % repr(addr) |
|
f0300584c824
Fix Issue 6706: return None on connect() in case of EWOULDBLOCK/ECONNABORTED error.
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
64256
diff
changeset
|
302 channel = SMTPChannel(self, conn, addr) |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
303 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
304 # API for "doing something useful with the message" |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
305 def process_message(self, peer, mailfrom, rcpttos, data): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
306 """Override this abstract method to handle messages from the client. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
307 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
308 peer is a tuple containing (ipaddr, port) of the client that made the |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
309 socket connection to our smtp port. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
310 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
311 mailfrom is the raw address the client claims the message is coming |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
312 from. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
313 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
314 rcpttos is a list of raw addresses the client wishes to deliver the |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
315 message to. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
316 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
317 data is a string containing the entire full text of the message, |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
318 headers (if supplied) and all. It has been `de-transparencied' |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
319 according to RFC 821, Section 4.5.2. In other words, a line |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
320 containing a `.' followed by other text has had the leading dot |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
321 removed. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
322 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
323 This function should return None, for a normal `250 Ok' response; |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
324 otherwise it returns the desired response string in RFC 821 format. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
325 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
326 """ |
|
17527
01be08333308
Fix typo in exception name (UnimplementedError should be
Guido van Rossum <guido@python.org>
parents:
16948
diff
changeset
|
327 raise NotImplementedError |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
328 |
|
16535
084b3dc9b6ff
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16377
diff
changeset
|
329 |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
330 class DebuggingServer(SMTPServer): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
331 # Do something with the gathered message |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
332 def process_message(self, peer, mailfrom, rcpttos, data): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
333 inheaders = 1 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
334 lines = data.split('\n') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
335 print '---------- MESSAGE FOLLOWS ----------' |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
336 for line in lines: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
337 # headers first |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
338 if inheaders and not line: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
339 print 'X-Peer:', peer[0] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
340 inheaders = 0 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
341 print line |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
342 print '------------ END MESSAGE ------------' |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
343 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
344 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
345 class PureProxy(SMTPServer): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
346 def process_message(self, peer, mailfrom, rcpttos, data): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
347 lines = data.split('\n') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
348 # Look for the last header |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
349 i = 0 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
350 for line in lines: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
351 if not line: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
352 break |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
353 i += 1 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
354 lines.insert(i, 'X-Peer: %s' % peer[0]) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
355 data = NEWLINE.join(lines) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
356 refused = self._deliver(mailfrom, rcpttos, data) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
357 # TBD: what to do with refused addresses? |
|
21920
59bb43b6eb11
SF #515021, print the refused list to the DEBUGSTREAM, so the parameter is used
Neal Norwitz <nnorwitz@gmail.com>
parents:
20862
diff
changeset
|
358 print >> DEBUGSTREAM, 'we got some refusals:', refused |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
359 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
360 def _deliver(self, mailfrom, rcpttos, data): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
361 import smtplib |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
362 refused = {} |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
363 try: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
364 s = smtplib.SMTP() |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
365 s.connect(self._remoteaddr[0], self._remoteaddr[1]) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
366 try: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
367 refused = s.sendmail(mailfrom, rcpttos, data) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
368 finally: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
369 s.quit() |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
370 except smtplib.SMTPRecipientsRefused, e: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
371 print >> DEBUGSTREAM, 'got SMTPRecipientsRefused' |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
372 refused = e.recipients |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
373 except (socket.error, smtplib.SMTPException), e: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
374 print >> DEBUGSTREAM, 'got', e.__class__ |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
375 # All recipients were refused. If the exception had an associated |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
376 # error code, use it. Otherwise,fake it with a non-triggering |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
377 # exception code. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
378 errcode = getattr(e, 'smtp_code', -1) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
379 errmsg = getattr(e, 'smtp_error', 'ignore') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
380 for r in rcpttos: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
381 refused[r] = (errcode, errmsg) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
382 return refused |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
383 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
384 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
385 class MailmanProxy(PureProxy): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
386 def process_message(self, peer, mailfrom, rcpttos, data): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
387 from cStringIO import StringIO |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
388 from Mailman import Utils |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
389 from Mailman import Message |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
390 from Mailman import MailList |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
391 # If the message is to a Mailman mailing list, then we'll invoke the |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
392 # Mailman script directly, without going through the real smtpd. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
393 # Otherwise we'll forward it to the local proxy for disposition. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
394 listnames = [] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
395 for rcpt in rcpttos: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
396 local = rcpt.lower().split('@')[0] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
397 # We allow the following variations on the theme |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
398 # listname |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
399 # listname-admin |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
400 # listname-owner |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
401 # listname-request |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
402 # listname-join |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
403 # listname-leave |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
404 parts = local.split('-') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
405 if len(parts) > 2: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
406 continue |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
407 listname = parts[0] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
408 if len(parts) == 2: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
409 command = parts[1] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
410 else: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
411 command = '' |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
412 if not Utils.list_exists(listname) or command not in ( |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
413 '', 'admin', 'owner', 'request', 'join', 'leave'): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
414 continue |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
415 listnames.append((rcpt, listname, command)) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
416 # Remove all list recipients from rcpttos and forward what we're not |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
417 # going to take care of ourselves. Linear removal should be fine |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
418 # since we don't expect a large number of recipients. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
419 for rcpt, listname, command in listnames: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
420 rcpttos.remove(rcpt) |
|
16535
084b3dc9b6ff
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16377
diff
changeset
|
421 # If there's any non-list destined recipients left, |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
422 print >> DEBUGSTREAM, 'forwarding recips:', ' '.join(rcpttos) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
423 if rcpttos: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
424 refused = self._deliver(mailfrom, rcpttos, data) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
425 # TBD: what to do with refused addresses? |
|
21920
59bb43b6eb11
SF #515021, print the refused list to the DEBUGSTREAM, so the parameter is used
Neal Norwitz <nnorwitz@gmail.com>
parents:
20862
diff
changeset
|
426 print >> DEBUGSTREAM, 'we got refusals:', refused |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
427 # Now deliver directly to the list commands |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
428 mlists = {} |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
429 s = StringIO(data) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
430 msg = Message.Message(s) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
431 # These headers are required for the proper execution of Mailman. All |
|
55049
74c0a78114ae
Merged revisions 69846 via svnmerge from
Jesus Cea <jcea@jcea.es>
parents:
43659
diff
changeset
|
432 # MTAs in existence seem to add these if the original message doesn't |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
433 # have them. |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
434 if not msg.getheader('from'): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
435 msg['From'] = mailfrom |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
436 if not msg.getheader('date'): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
437 msg['Date'] = time.ctime(time.time()) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
438 for rcpt, listname, command in listnames: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
439 print >> DEBUGSTREAM, 'sending message to', rcpt |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
440 mlist = mlists.get(listname) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
441 if not mlist: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
442 mlist = MailList.MailList(listname, lock=0) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
443 mlists[listname] = mlist |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
444 # dispatch on the type of command |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
445 if command == '': |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
446 # post |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
447 msg.Enqueue(mlist, tolist=1) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
448 elif command == 'admin': |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
449 msg.Enqueue(mlist, toadmin=1) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
450 elif command == 'owner': |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
451 msg.Enqueue(mlist, toowner=1) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
452 elif command == 'request': |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
453 msg.Enqueue(mlist, torequest=1) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
454 elif command in ('join', 'leave'): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
455 # TBD: this is a hack! |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
456 if command == 'join': |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
457 msg['Subject'] = 'subscribe' |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
458 else: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
459 msg['Subject'] = 'unsubscribe' |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
460 msg.Enqueue(mlist, torequest=1) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
461 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
462 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
463 class Options: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
464 setuid = 1 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
465 classname = 'PureProxy' |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
466 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
467 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
468 def parseargs(): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
469 global DEBUGSTREAM |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
470 try: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
471 opts, args = getopt.getopt( |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
472 sys.argv[1:], 'nVhc:d', |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
473 ['class=', 'nosetuid', 'version', 'help', 'debug']) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
474 except getopt.error, e: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
475 usage(1, e) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
476 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
477 options = Options() |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
478 for opt, arg in opts: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
479 if opt in ('-h', '--help'): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
480 usage(0) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
481 elif opt in ('-V', '--version'): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
482 print >> sys.stderr, __version__ |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
483 sys.exit(0) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
484 elif opt in ('-n', '--nosetuid'): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
485 options.setuid = 0 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
486 elif opt in ('-c', '--class'): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
487 options.classname = arg |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
488 elif opt in ('-d', '--debug'): |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
489 DEBUGSTREAM = sys.stderr |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
490 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
491 # parse the rest of the arguments |
|
20251
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
492 if len(args) < 1: |
|
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
493 localspec = 'localhost:8025' |
|
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
494 remotespec = 'localhost:25' |
|
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
495 elif len(args) < 2: |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
496 localspec = args[0] |
|
20251
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
497 remotespec = 'localhost:25' |
|
20862
d9b9b5b4815b
Two bug fixes for problems reported by Sverre:
Barry Warsaw <barry@python.org>
parents:
20332
diff
changeset
|
498 elif len(args) < 3: |
|
d9b9b5b4815b
Two bug fixes for problems reported by Sverre:
Barry Warsaw <barry@python.org>
parents:
20332
diff
changeset
|
499 localspec = args[0] |
|
d9b9b5b4815b
Two bug fixes for problems reported by Sverre:
Barry Warsaw <barry@python.org>
parents:
20332
diff
changeset
|
500 remotespec = args[1] |
|
20251
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
501 else: |
|
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
502 usage(1, 'Invalid arguments: %s' % COMMASPACE.join(args)) |
|
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
503 |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
504 # split into host/port pairs |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
505 i = localspec.find(':') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
506 if i < 0: |
|
20251
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
507 usage(1, 'Bad local spec: %s' % localspec) |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
508 options.localhost = localspec[:i] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
509 try: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
510 options.localport = int(localspec[i+1:]) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
511 except ValueError: |
|
20251
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
512 usage(1, 'Bad local port: %s' % localspec) |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
513 i = remotespec.find(':') |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
514 if i < 0: |
|
20251
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
515 usage(1, 'Bad remote spec: %s' % remotespec) |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
516 options.remotehost = remotespec[:i] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
517 try: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
518 options.remoteport = int(remotespec[i+1:]) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
519 except ValueError: |
|
20251
b0ce92561e07
Script arguments localhost:localport and remotehost:remoteport are now
Barry Warsaw <barry@python.org>
parents:
19235
diff
changeset
|
520 usage(1, 'Bad remote port: %s' % remotespec) |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
521 return options |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
522 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
523 |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
524 if __name__ == '__main__': |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
525 options = parseargs() |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
526 # Become nobody |
|
73037
d2f303861c98
Issue #9168: now smtpd is able to bind privileged port.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
68256
diff
changeset
|
527 classname = options.classname |
|
d2f303861c98
Issue #9168: now smtpd is able to bind privileged port.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
68256
diff
changeset
|
528 if "." in classname: |
|
d2f303861c98
Issue #9168: now smtpd is able to bind privileged port.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
68256
diff
changeset
|
529 lastdot = classname.rfind(".") |
|
d2f303861c98
Issue #9168: now smtpd is able to bind privileged port.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
68256
diff
changeset
|
530 mod = __import__(classname[:lastdot], globals(), locals(), [""]) |
|
d2f303861c98
Issue #9168: now smtpd is able to bind privileged port.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
68256
diff
changeset
|
531 classname = classname[lastdot+1:] |
|
d2f303861c98
Issue #9168: now smtpd is able to bind privileged port.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
68256
diff
changeset
|
532 else: |
|
d2f303861c98
Issue #9168: now smtpd is able to bind privileged port.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
68256
diff
changeset
|
533 import __main__ as mod |
|
d2f303861c98
Issue #9168: now smtpd is able to bind privileged port.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
68256
diff
changeset
|
534 class_ = getattr(mod, classname) |
|
d2f303861c98
Issue #9168: now smtpd is able to bind privileged port.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
68256
diff
changeset
|
535 proxy = class_((options.localhost, options.localport), |
|
d2f303861c98
Issue #9168: now smtpd is able to bind privileged port.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
68256
diff
changeset
|
536 (options.remotehost, options.remoteport)) |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
537 if options.setuid: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
538 try: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
539 import pwd |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
540 except ImportError: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
541 print >> sys.stderr, \ |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
542 'Cannot import module "pwd"; try running with -n option.' |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
543 sys.exit(1) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
544 nobody = pwd.getpwnam('nobody')[2] |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
545 try: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
546 os.setuid(nobody) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
547 except OSError, e: |
|
16948
faa52f384af6
Use != instead of <>. Sorry, Barry.
Guido van Rossum <guido@python.org>
parents:
16613
diff
changeset
|
548 if e.errno != errno.EPERM: raise |
|
16377
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
549 print >> sys.stderr, \ |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
550 'Cannot setuid "nobody"; try running with -n option.' |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
551 sys.exit(1) |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
552 try: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
553 asyncore.loop() |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
554 except KeyboardInterrupt: |
|
0d4b6ba5eeec
Long ago, Guido suggested that I add this to the standard library.
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
555 pass |
