Guido van Rossum | 07a272d | 1995-04-10 11:40:52 +0000 | [diff] [blame] | 1 | """RPC Server module.""" |
| 2 | |
| 3 | import sys |
| 4 | import socket |
| 5 | import pickle |
| 6 | from fnmatch import fnmatch |
Alexandre Vassalotti | 1f2ba4b | 2008-05-16 07:12:44 +0000 | [diff] [blame^] | 7 | from reprlib import repr |
Guido van Rossum | 07a272d | 1995-04-10 11:40:52 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | # Default verbosity (0 = silent, 1 = print connections, 2 = print requests too) |
| 11 | VERBOSE = 1 |
| 12 | |
| 13 | |
| 14 | class Server: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 15 | |
| 16 | """RPC Server class. Derive a class to implement a particular service.""" |
| 17 | |
| 18 | def __init__(self, address, verbose = VERBOSE): |
| 19 | if type(address) == type(0): |
| 20 | address = ('', address) |
| 21 | self._address = address |
| 22 | self._verbose = verbose |
| 23 | self._socket = None |
| 24 | self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 25 | self._socket.bind(address) |
| 26 | self._socket.listen(1) |
| 27 | self._listening = 1 |
| 28 | |
| 29 | def _setverbose(self, verbose): |
| 30 | self._verbose = verbose |
| 31 | |
| 32 | def __del__(self): |
| 33 | self._close() |
| 34 | |
| 35 | def _close(self): |
| 36 | self._listening = 0 |
| 37 | if self._socket: |
| 38 | self._socket.close() |
| 39 | self._socket = None |
| 40 | |
| 41 | def _serverloop(self): |
| 42 | while self._listening: |
| 43 | self._serve() |
| 44 | |
| 45 | def _serve(self): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 46 | if self._verbose: print("Wait for connection ...") |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 47 | conn, address = self._socket.accept() |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 48 | if self._verbose: print("Accepted connection from %s" % repr(address)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 49 | if not self._verify(conn, address): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 50 | print("*** Connection from %s refused" % repr(address)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 51 | conn.close() |
| 52 | return |
| 53 | rf = conn.makefile('r') |
| 54 | wf = conn.makefile('w') |
| 55 | ok = 1 |
| 56 | while ok: |
| 57 | wf.flush() |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 58 | if self._verbose > 1: print("Wait for next request ...") |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 59 | ok = self._dorequest(rf, wf) |
| 60 | |
| 61 | _valid = ['192.16.201.*', '192.16.197.*', '132.151.1.*', '129.6.64.*'] |
| 62 | |
| 63 | def _verify(self, conn, address): |
| 64 | host, port = address |
| 65 | for pat in self._valid: |
| 66 | if fnmatch(host, pat): return 1 |
| 67 | return 0 |
| 68 | |
| 69 | def _dorequest(self, rf, wf): |
| 70 | rp = pickle.Unpickler(rf) |
| 71 | try: |
| 72 | request = rp.load() |
| 73 | except EOFError: |
| 74 | return 0 |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 75 | if self._verbose > 1: print("Got request: %s" % repr(request)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 76 | try: |
| 77 | methodname, args, id = request |
| 78 | if '.' in methodname: |
| 79 | reply = (None, self._special(methodname, args), id) |
| 80 | elif methodname[0] == '_': |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 81 | raise NameError("illegal method name %s" % repr(methodname)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 82 | else: |
| 83 | method = getattr(self, methodname) |
Neal Norwitz | d910855 | 2006-03-17 08:00:19 +0000 | [diff] [blame] | 84 | reply = (None, method(*args), id) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 85 | except: |
Neal Norwitz | ac3625f | 2006-03-17 05:49:33 +0000 | [diff] [blame] | 86 | reply = (sys.exc_info()[:2], id) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 87 | if id < 0 and reply[:2] == (None, None): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 88 | if self._verbose > 1: print("Suppress reply") |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 89 | return 1 |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 90 | if self._verbose > 1: print("Send reply: %s" % repr(reply)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 91 | wp = pickle.Pickler(wf) |
| 92 | wp.dump(reply) |
| 93 | return 1 |
| 94 | |
| 95 | def _special(self, methodname, args): |
| 96 | if methodname == '.methods': |
| 97 | if not hasattr(self, '_methods'): |
| 98 | self._methods = tuple(self._listmethods()) |
| 99 | return self._methods |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 100 | raise NameError("unrecognized special method name %s" % repr(methodname)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 101 | |
| 102 | def _listmethods(self, cl=None): |
| 103 | if not cl: cl = self.__class__ |
Skip Montanaro | 1e8ce58 | 2007-08-06 21:07:53 +0000 | [diff] [blame] | 104 | names = sorted([x for x in cl.__dict__.keys() if x[0] != '_']) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 105 | for base in cl.__bases__: |
| 106 | basenames = self._listmethods(base) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 107 | basenames = list(filter(lambda x, names=names: x not in names, basenames)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 108 | names[len(names):] = basenames |
| 109 | return names |
Guido van Rossum | 45babef | 1995-06-21 01:00:17 +0000 | [diff] [blame] | 110 | |
| 111 | |
| 112 | from security import Security |
| 113 | |
| 114 | |
| 115 | class SecureServer(Server, Security): |
| 116 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 117 | def __init__(self, *args): |
Neal Norwitz | d910855 | 2006-03-17 08:00:19 +0000 | [diff] [blame] | 118 | Server.__init__(self, *args) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 119 | Security.__init__(self) |
Guido van Rossum | 45babef | 1995-06-21 01:00:17 +0000 | [diff] [blame] | 120 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 121 | def _verify(self, conn, address): |
| 122 | import string |
| 123 | challenge = self._generate_challenge() |
| 124 | conn.send("%d\n" % challenge) |
| 125 | response = "" |
| 126 | while "\n" not in response and len(response) < 100: |
| 127 | data = conn.recv(100) |
| 128 | if not data: |
| 129 | break |
| 130 | response = response + data |
| 131 | try: |
| 132 | response = string.atol(string.strip(response)) |
| 133 | except string.atol_error: |
| 134 | if self._verbose > 0: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 135 | print("Invalid response syntax", repr(response)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 136 | return 0 |
| 137 | if not self._compare_challenge_response(challenge, response): |
| 138 | if self._verbose > 0: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 139 | print("Invalid response value", repr(response)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 140 | return 0 |
| 141 | if self._verbose > 1: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 142 | print("Response matches challenge. Go ahead!") |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 143 | return 1 |