blob: 73e7777fa001b98407ebb40bbfb140223fb365d8 [file] [log] [blame]
Barry Warsaw7e0d9562001-01-31 22:51:35 +00001#! /usr/bin/env python
Barry Warsaw406d46e2001-08-13 21:18:01 +00002"""An RFC 2821 smtp proxy.
Barry Warsaw7e0d9562001-01-31 22:51:35 +00003
Barry Warsaw0e8427e2001-10-04 16:27:04 +00004Usage: %(program)s [options] [localhost:localport [remotehost:remoteport]]
Barry Warsaw7e0d9562001-01-31 22:51:35 +00005
6Options:
7
8 --nosetuid
9 -n
10 This program generally tries to setuid `nobody', unless this flag is
11 set. The setuid call will fail if this program is not run as root (in
12 which case, use this flag).
13
14 --version
15 -V
16 Print the version number and exit.
17
18 --class classname
19 -c classname
Barry Warsawf267b622004-10-09 21:44:13 +000020 Use `classname' as the concrete SMTP proxy class. Uses `PureProxy' by
Barry Warsaw7e0d9562001-01-31 22:51:35 +000021 default.
22
23 --debug
24 -d
25 Turn on debugging prints.
26
27 --help
28 -h
29 Print this message and exit.
30
31Version: %(__version__)s
32
Barry Warsaw0e8427e2001-10-04 16:27:04 +000033If localhost is not given then `localhost' is used, and if localport is not
34given then 8025 is used. If remotehost is not given then `localhost' is used,
35and if remoteport is not given, then 25 is used.
Barry Warsaw7e0d9562001-01-31 22:51:35 +000036"""
37
Barry Warsaw0e8427e2001-10-04 16:27:04 +000038
Barry Warsaw7e0d9562001-01-31 22:51:35 +000039# Overview:
40#
41# This file implements the minimal SMTP protocol as defined in RFC 821. It
42# has a hierarchy of classes which implement the backend functionality for the
43# smtpd. A number of classes are provided:
44#
Guido van Rossumb8b45ea2001-04-15 13:06:04 +000045# SMTPServer - the base class for the backend. Raises NotImplementedError
Barry Warsaw7e0d9562001-01-31 22:51:35 +000046# if you try to use it.
47#
48# DebuggingServer - simply prints each message it receives on stdout.
49#
50# PureProxy - Proxies all messages to a real smtpd which does final
51# delivery. One known problem with this class is that it doesn't handle
52# SMTP errors from the backend server at all. This should be fixed
53# (contributions are welcome!).
54#
55# MailmanProxy - An experimental hack to work with GNU Mailman
56# <www.list.org>. Using this server as your real incoming smtpd, your
57# mailhost will automatically recognize and accept mail destined to Mailman
58# lists when those lists are created. Every message not destined for a list
59# gets forwarded to a real backend smtpd, as with PureProxy. Again, errors
60# are not handled correctly yet.
61#
62# Please note that this script requires Python 2.0
63#
Barry Warsawb1027642004-07-12 23:10:08 +000064# Author: Barry Warsaw <barry@python.org>
Barry Warsaw7e0d9562001-01-31 22:51:35 +000065#
66# TODO:
67#
68# - support mailbox delivery
69# - alias files
70# - ESMTP
71# - handle error codes from the backend smtpd
72
73import sys
74import os
75import errno
76import getopt
77import time
78import socket
79import asyncore
80import asynchat
81
Skip Montanaro0de65802001-02-15 22:15:14 +000082__all__ = ["SMTPServer","DebuggingServer","PureProxy","MailmanProxy"]
Barry Warsaw7e0d9562001-01-31 22:51:35 +000083
84program = sys.argv[0]
85__version__ = 'Python SMTP proxy version 0.2'
86
87
88class Devnull:
89 def write(self, msg): pass
90 def flush(self): pass
91
92
93DEBUGSTREAM = Devnull()
94NEWLINE = '\n'
95EMPTYSTRING = ''
Barry Warsaw0e8427e2001-10-04 16:27:04 +000096COMMASPACE = ', '
Barry Warsaw7e0d9562001-01-31 22:51:35 +000097
98
Barry Warsaw0e8427e2001-10-04 16:27:04 +000099
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000100def usage(code, msg=''):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000101 print(__doc__ % globals(), file=sys.stderr)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000102 if msg:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000103 print(msg, file=sys.stderr)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000104 sys.exit(code)
105
106
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000107
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000108class SMTPChannel(asynchat.async_chat):
109 COMMAND = 0
110 DATA = 1
111
112 def __init__(self, server, conn, addr):
113 asynchat.async_chat.__init__(self, conn)
114 self.__server = server
115 self.__conn = conn
116 self.__addr = addr
117 self.__line = []
118 self.__state = self.COMMAND
119 self.__greeting = 0
120 self.__mailfrom = None
121 self.__rcpttos = []
122 self.__data = ''
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000123 self.__fqdn = socket.getfqdn()
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000124 self.__peer = conn.getpeername()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000125 print('Peer:', repr(self.__peer), file=DEBUGSTREAM)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000126 self.push('220 %s %s' % (self.__fqdn, __version__))
127 self.set_terminator('\r\n')
128
129 # Overrides base class for convenience
130 def push(self, msg):
131 asynchat.async_chat.push(self, msg + '\r\n')
132
133 # Implementation of base class abstract method
134 def collect_incoming_data(self, data):
135 self.__line.append(data)
136
137 # Implementation of base class abstract method
138 def found_terminator(self):
139 line = EMPTYSTRING.join(self.__line)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000140 print('Data:', repr(line), file=DEBUGSTREAM)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000141 self.__line = []
142 if self.__state == self.COMMAND:
143 if not line:
144 self.push('500 Error: bad syntax')
145 return
146 method = None
147 i = line.find(' ')
148 if i < 0:
149 command = line.upper()
150 arg = None
151 else:
152 command = line[:i].upper()
153 arg = line[i+1:].strip()
154 method = getattr(self, 'smtp_' + command, None)
155 if not method:
156 self.push('502 Error: command "%s" not implemented' % command)
157 return
158 method(arg)
159 return
160 else:
Guido van Rossum4ba3d652001-03-02 06:42:34 +0000161 if self.__state != self.DATA:
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000162 self.push('451 Internal confusion')
163 return
164 # Remove extraneous carriage returns and de-transparency according
165 # to RFC 821, Section 4.5.2.
166 data = []
167 for text in line.split('\r\n'):
168 if text and text[0] == '.':
169 data.append(text[1:])
170 else:
171 data.append(text)
172 self.__data = NEWLINE.join(data)
173 status = self.__server.process_message(self.__peer,
174 self.__mailfrom,
175 self.__rcpttos,
176 self.__data)
177 self.__rcpttos = []
178 self.__mailfrom = None
179 self.__state = self.COMMAND
180 self.set_terminator('\r\n')
181 if not status:
182 self.push('250 Ok')
183 else:
184 self.push(status)
185
186 # SMTP and ESMTP commands
187 def smtp_HELO(self, arg):
188 if not arg:
189 self.push('501 Syntax: HELO hostname')
190 return
191 if self.__greeting:
192 self.push('503 Duplicate HELO/EHLO')
193 else:
194 self.__greeting = arg
195 self.push('250 %s' % self.__fqdn)
196
197 def smtp_NOOP(self, arg):
198 if arg:
199 self.push('501 Syntax: NOOP')
200 else:
201 self.push('250 Ok')
202
203 def smtp_QUIT(self, arg):
204 # args is ignored
205 self.push('221 Bye')
206 self.close_when_done()
207
208 # factored
209 def __getaddr(self, keyword, arg):
210 address = None
211 keylen = len(keyword)
212 if arg[:keylen].upper() == keyword:
213 address = arg[keylen:].strip()
Barry Warsawebf54272001-11-04 03:04:25 +0000214 if not address:
215 pass
216 elif address[0] == '<' and address[-1] == '>' and address != '<>':
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000217 # Addresses can be in the form <person@dom.com> but watch out
218 # for null address, e.g. <>
219 address = address[1:-1]
220 return address
221
222 def smtp_MAIL(self, arg):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000223 print('===> MAIL', arg, file=DEBUGSTREAM)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000224 address = self.__getaddr('FROM:', arg)
225 if not address:
226 self.push('501 Syntax: MAIL FROM:<address>')
227 return
228 if self.__mailfrom:
229 self.push('503 Error: nested MAIL command')
230 return
231 self.__mailfrom = address
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000232 print('sender:', self.__mailfrom, file=DEBUGSTREAM)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000233 self.push('250 Ok')
234
235 def smtp_RCPT(self, arg):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000236 print('===> RCPT', arg, file=DEBUGSTREAM)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000237 if not self.__mailfrom:
238 self.push('503 Error: need MAIL command')
239 return
240 address = self.__getaddr('TO:', arg)
241 if not address:
242 self.push('501 Syntax: RCPT TO: <address>')
243 return
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000244 self.__rcpttos.append(address)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000245 print('recips:', self.__rcpttos, file=DEBUGSTREAM)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000246 self.push('250 Ok')
247
248 def smtp_RSET(self, arg):
249 if arg:
250 self.push('501 Syntax: RSET')
251 return
252 # Resets the sender, recipients, and data, but not the greeting
253 self.__mailfrom = None
254 self.__rcpttos = []
255 self.__data = ''
256 self.__state = self.COMMAND
257 self.push('250 Ok')
258
259 def smtp_DATA(self, arg):
260 if not self.__rcpttos:
261 self.push('503 Error: need RCPT command')
262 return
263 if arg:
264 self.push('501 Syntax: DATA')
265 return
266 self.__state = self.DATA
267 self.set_terminator('\r\n.\r\n')
268 self.push('354 End data with <CR><LF>.<CR><LF>')
269
270
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000271
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000272class SMTPServer(asyncore.dispatcher):
273 def __init__(self, localaddr, remoteaddr):
274 self._localaddr = localaddr
275 self._remoteaddr = remoteaddr
276 asyncore.dispatcher.__init__(self)
277 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
278 # try to re-use a server port if possible
Barry Warsaw93a63272001-10-09 15:46:31 +0000279 self.set_reuse_addr()
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000280 self.bind(localaddr)
281 self.listen(5)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000282 print('%s started at %s\n\tLocal addr: %s\n\tRemote addr:%s' % (
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000283 self.__class__.__name__, time.ctime(time.time()),
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000284 localaddr, remoteaddr), file=DEBUGSTREAM)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000285
286 def handle_accept(self):
287 conn, addr = self.accept()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000288 print('Incoming connection from %s' % repr(addr), file=DEBUGSTREAM)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000289 channel = SMTPChannel(self, conn, addr)
290
291 # API for "doing something useful with the message"
292 def process_message(self, peer, mailfrom, rcpttos, data):
293 """Override this abstract method to handle messages from the client.
294
295 peer is a tuple containing (ipaddr, port) of the client that made the
296 socket connection to our smtp port.
297
298 mailfrom is the raw address the client claims the message is coming
299 from.
300
301 rcpttos is a list of raw addresses the client wishes to deliver the
302 message to.
303
304 data is a string containing the entire full text of the message,
305 headers (if supplied) and all. It has been `de-transparencied'
306 according to RFC 821, Section 4.5.2. In other words, a line
307 containing a `.' followed by other text has had the leading dot
308 removed.
309
310 This function should return None, for a normal `250 Ok' response;
311 otherwise it returns the desired response string in RFC 821 format.
312
313 """
Guido van Rossumb8b45ea2001-04-15 13:06:04 +0000314 raise NotImplementedError
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000315
Tim Peters658cba62001-02-09 20:06:00 +0000316
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000317
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000318class DebuggingServer(SMTPServer):
319 # Do something with the gathered message
320 def process_message(self, peer, mailfrom, rcpttos, data):
321 inheaders = 1
322 lines = data.split('\n')
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000323 print('---------- MESSAGE FOLLOWS ----------')
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000324 for line in lines:
325 # headers first
326 if inheaders and not line:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000327 print('X-Peer:', peer[0])
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000328 inheaders = 0
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000329 print(line)
330 print('------------ END MESSAGE ------------')
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000331
332
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000333
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000334class PureProxy(SMTPServer):
335 def process_message(self, peer, mailfrom, rcpttos, data):
336 lines = data.split('\n')
337 # Look for the last header
338 i = 0
339 for line in lines:
340 if not line:
341 break
342 i += 1
343 lines.insert(i, 'X-Peer: %s' % peer[0])
344 data = NEWLINE.join(lines)
345 refused = self._deliver(mailfrom, rcpttos, data)
346 # TBD: what to do with refused addresses?
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000347 print('we got some refusals:', refused, file=DEBUGSTREAM)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000348
349 def _deliver(self, mailfrom, rcpttos, data):
350 import smtplib
351 refused = {}
352 try:
353 s = smtplib.SMTP()
354 s.connect(self._remoteaddr[0], self._remoteaddr[1])
355 try:
356 refused = s.sendmail(mailfrom, rcpttos, data)
357 finally:
358 s.quit()
Guido van Rossumb940e112007-01-10 16:19:56 +0000359 except smtplib.SMTPRecipientsRefused as e:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000360 print('got SMTPRecipientsRefused', file=DEBUGSTREAM)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000361 refused = e.recipients
Guido van Rossumb940e112007-01-10 16:19:56 +0000362 except (socket.error, smtplib.SMTPException) as e:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000363 print('got', e.__class__, file=DEBUGSTREAM)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000364 # All recipients were refused. If the exception had an associated
365 # error code, use it. Otherwise,fake it with a non-triggering
366 # exception code.
367 errcode = getattr(e, 'smtp_code', -1)
368 errmsg = getattr(e, 'smtp_error', 'ignore')
369 for r in rcpttos:
370 refused[r] = (errcode, errmsg)
371 return refused
372
373
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000374
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000375class MailmanProxy(PureProxy):
376 def process_message(self, peer, mailfrom, rcpttos, data):
377 from cStringIO import StringIO
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000378 from Mailman import Utils
379 from Mailman import Message
380 from Mailman import MailList
381 # If the message is to a Mailman mailing list, then we'll invoke the
382 # Mailman script directly, without going through the real smtpd.
383 # Otherwise we'll forward it to the local proxy for disposition.
384 listnames = []
385 for rcpt in rcpttos:
386 local = rcpt.lower().split('@')[0]
387 # We allow the following variations on the theme
388 # listname
389 # listname-admin
390 # listname-owner
391 # listname-request
392 # listname-join
393 # listname-leave
394 parts = local.split('-')
395 if len(parts) > 2:
396 continue
397 listname = parts[0]
398 if len(parts) == 2:
399 command = parts[1]
400 else:
401 command = ''
402 if not Utils.list_exists(listname) or command not in (
403 '', 'admin', 'owner', 'request', 'join', 'leave'):
404 continue
405 listnames.append((rcpt, listname, command))
406 # Remove all list recipients from rcpttos and forward what we're not
407 # going to take care of ourselves. Linear removal should be fine
408 # since we don't expect a large number of recipients.
409 for rcpt, listname, command in listnames:
410 rcpttos.remove(rcpt)
Tim Peters658cba62001-02-09 20:06:00 +0000411 # If there's any non-list destined recipients left,
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000412 print('forwarding recips:', ' '.join(rcpttos), file=DEBUGSTREAM)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000413 if rcpttos:
414 refused = self._deliver(mailfrom, rcpttos, data)
415 # TBD: what to do with refused addresses?
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000416 print('we got refusals:', refused, file=DEBUGSTREAM)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000417 # Now deliver directly to the list commands
418 mlists = {}
419 s = StringIO(data)
420 msg = Message.Message(s)
421 # These headers are required for the proper execution of Mailman. All
422 # MTAs in existance seem to add these if the original message doesn't
423 # have them.
424 if not msg.getheader('from'):
425 msg['From'] = mailfrom
426 if not msg.getheader('date'):
427 msg['Date'] = time.ctime(time.time())
428 for rcpt, listname, command in listnames:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000429 print('sending message to', rcpt, file=DEBUGSTREAM)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000430 mlist = mlists.get(listname)
431 if not mlist:
432 mlist = MailList.MailList(listname, lock=0)
433 mlists[listname] = mlist
434 # dispatch on the type of command
435 if command == '':
436 # post
437 msg.Enqueue(mlist, tolist=1)
438 elif command == 'admin':
439 msg.Enqueue(mlist, toadmin=1)
440 elif command == 'owner':
441 msg.Enqueue(mlist, toowner=1)
442 elif command == 'request':
443 msg.Enqueue(mlist, torequest=1)
444 elif command in ('join', 'leave'):
445 # TBD: this is a hack!
446 if command == 'join':
447 msg['Subject'] = 'subscribe'
448 else:
449 msg['Subject'] = 'unsubscribe'
450 msg.Enqueue(mlist, torequest=1)
451
452
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000453
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000454class Options:
455 setuid = 1
456 classname = 'PureProxy'
457
458
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000459
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000460def parseargs():
461 global DEBUGSTREAM
462 try:
463 opts, args = getopt.getopt(
464 sys.argv[1:], 'nVhc:d',
465 ['class=', 'nosetuid', 'version', 'help', 'debug'])
Guido van Rossumb940e112007-01-10 16:19:56 +0000466 except getopt.error as e:
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000467 usage(1, e)
468
469 options = Options()
470 for opt, arg in opts:
471 if opt in ('-h', '--help'):
472 usage(0)
473 elif opt in ('-V', '--version'):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000474 print(__version__, file=sys.stderr)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000475 sys.exit(0)
476 elif opt in ('-n', '--nosetuid'):
477 options.setuid = 0
478 elif opt in ('-c', '--class'):
479 options.classname = arg
480 elif opt in ('-d', '--debug'):
481 DEBUGSTREAM = sys.stderr
482
483 # parse the rest of the arguments
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000484 if len(args) < 1:
485 localspec = 'localhost:8025'
486 remotespec = 'localhost:25'
487 elif len(args) < 2:
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000488 localspec = args[0]
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000489 remotespec = 'localhost:25'
Barry Warsawebf54272001-11-04 03:04:25 +0000490 elif len(args) < 3:
491 localspec = args[0]
492 remotespec = args[1]
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000493 else:
494 usage(1, 'Invalid arguments: %s' % COMMASPACE.join(args))
495
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000496 # split into host/port pairs
497 i = localspec.find(':')
498 if i < 0:
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000499 usage(1, 'Bad local spec: %s' % localspec)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000500 options.localhost = localspec[:i]
501 try:
502 options.localport = int(localspec[i+1:])
503 except ValueError:
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000504 usage(1, 'Bad local port: %s' % localspec)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000505 i = remotespec.find(':')
506 if i < 0:
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000507 usage(1, 'Bad remote spec: %s' % remotespec)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000508 options.remotehost = remotespec[:i]
509 try:
510 options.remoteport = int(remotespec[i+1:])
511 except ValueError:
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000512 usage(1, 'Bad remote port: %s' % remotespec)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000513 return options
514
515
Barry Warsaw0e8427e2001-10-04 16:27:04 +0000516
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000517if __name__ == '__main__':
518 options = parseargs()
519 # Become nobody
520 if options.setuid:
521 try:
522 import pwd
523 except ImportError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000524 print('Cannot import module "pwd"; try running with -n option.', file=sys.stderr)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000525 sys.exit(1)
526 nobody = pwd.getpwnam('nobody')[2]
527 try:
528 os.setuid(nobody)
Guido van Rossumb940e112007-01-10 16:19:56 +0000529 except OSError as e:
Guido van Rossum4ba3d652001-03-02 06:42:34 +0000530 if e.errno != errno.EPERM: raise
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000531 print('Cannot setuid "nobody"; try running with -n option.', file=sys.stderr)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000532 sys.exit(1)
Skip Montanaro90e01532004-06-26 19:18:49 +0000533 classname = options.classname
534 if "." in classname:
535 lastdot = classname.rfind(".")
536 mod = __import__(classname[:lastdot], globals(), locals(), [""])
537 classname = classname[lastdot+1:]
538 else:
539 import __main__ as mod
Skip Montanaro90e01532004-06-26 19:18:49 +0000540 class_ = getattr(mod, classname)
Barry Warsaw7e0d9562001-01-31 22:51:35 +0000541 proxy = class_((options.localhost, options.localport),
542 (options.remotehost, options.remoteport))
543 try:
544 asyncore.loop()
545 except KeyboardInterrupt:
546 pass