1 | from twisted.application import internet, service |
---|
2 | from twisted.internet import protocol, reactor, defer |
---|
3 | from twisted.protocols import basic |
---|
4 | import ldap, ldap.filter |
---|
5 | import posixpath |
---|
6 | |
---|
7 | class WhoisProtocol(basic.LineReceiver): |
---|
8 | def lineReceived(self, hostname): |
---|
9 | (key, hostname) = hostname.split('=',2) |
---|
10 | if key != self.factory.key: |
---|
11 | self.transport.write("Unauthorized to use whois"+"\r\n") |
---|
12 | self.transport.loseConnection() |
---|
13 | else: |
---|
14 | self.factory.getWhois(hostname |
---|
15 | ).addErrback(lambda _: "Internal error in server" |
---|
16 | ).addCallback(lambda m: |
---|
17 | (self.transport.write(m+"\r\n"), |
---|
18 | self.transport.loseConnection())) |
---|
19 | class WhoisFactory(protocol.ServerFactory): |
---|
20 | protocol = WhoisProtocol |
---|
21 | def __init__(self, ldap_URL, ldap_base, keyFile): |
---|
22 | self.ldap_URL = ldap_URL |
---|
23 | self.ldap = ldap.initialize(self.ldap_URL) |
---|
24 | self.ldap_base = ldap_base |
---|
25 | self.key = file(keyFile).read() |
---|
26 | def canonicalize(self, vhost): |
---|
27 | vhost = vhost.lower().rstrip(".") |
---|
28 | return vhost |
---|
29 | # if vhost.endswith(".mit.edu"): |
---|
30 | # return vhost |
---|
31 | # else: |
---|
32 | # return vhost + ".mit.edu" |
---|
33 | def searchLDAP(self, vhost): |
---|
34 | attrlist = ('scriptsVhostName', 'homeDirectory', 'scriptsVhostDirectory', 'uid') |
---|
35 | results = self.ldap.search_st(self.ldap_base, ldap.SCOPE_SUBTREE, |
---|
36 | ldap.filter.filter_format( |
---|
37 | '(|(scriptsVhostName=%s)(scriptsVhostAlias=%s))', (vhost,)*2), |
---|
38 | attrlist=attrlist, timeout=5) |
---|
39 | if len(results) >= 1: |
---|
40 | result = results[0] |
---|
41 | attrs = result[1] |
---|
42 | for attr in attrlist: |
---|
43 | attrs[attr] = attrs[attr][0] |
---|
44 | return attrs |
---|
45 | else: |
---|
46 | return None |
---|
47 | def getWhois(self, vhost): |
---|
48 | vhost = self.canonicalize(vhost) |
---|
49 | info = None |
---|
50 | tries = 0 |
---|
51 | while (tries < 3) and not info: |
---|
52 | tries += 1 |
---|
53 | try: |
---|
54 | info = self.searchLDAP(vhost) |
---|
55 | break |
---|
56 | except (ldap.TIMEOUT, ldap.SERVER_DOWN): |
---|
57 | self.ldap.unbind() |
---|
58 | self.ldap = ldap.initialize(self.ldap_URL) |
---|
59 | if info: |
---|
60 | ret = "Hostname: %s\nAlias: %s\nLocker: %s\nDocument Root: %s" % \ |
---|
61 | (info['scriptsVhostName'], vhost, info['uid'], |
---|
62 | posixpath.join(info['homeDirectory'], 'web_scripts', info['scriptsVhostDirectory'])) |
---|
63 | elif tries == 3: |
---|
64 | ret = "The whois server is experiencing problems looking up LDAP records.\nPlease contact scripts@mit.edu for help if this problem persists." |
---|
65 | else: |
---|
66 | ret = "No such hostname" |
---|
67 | return defer.succeed(ret) |
---|
68 | |
---|
69 | application = service.Application('whois', uid=99, gid=99) |
---|
70 | factory = WhoisFactory( |
---|
71 | "ldap://localhost", "ou=VirtualHosts,dc=scripts,dc=mit,dc=edu", "/etc/whoisd-password") |
---|
72 | internet.TCPServer(43, factory).setServiceParent( |
---|
73 | service.IServiceCollection(application)) |
---|