| [528] | 1 | from twisted.application import internet, service |
|---|
| 2 | from twisted.internet import protocol, reactor, defer |
|---|
| 3 | from twisted.protocols import basic |
|---|
| [627] | 4 | import ldap, ldap.filter |
|---|
| [2757] | 5 | import posixpath |
|---|
| [528] | 6 | |
|---|
| 7 | class WhoisProtocol(basic.LineReceiver): |
|---|
| 8 | def lineReceived(self, hostname): |
|---|
| [771] | 9 | (key, hostname) = hostname.split('=',2) |
|---|
| [772] | 10 | if key != self.factory.key: |
|---|
| [771] | 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())) |
|---|
| [528] | 19 | class WhoisFactory(protocol.ServerFactory): |
|---|
| 20 | protocol = WhoisProtocol |
|---|
| [2756] | 21 | def __init__(self, ldap_URL, ldap_base, keyFile): |
|---|
| [627] | 22 | self.ldap_URL = ldap_URL |
|---|
| 23 | self.ldap = ldap.initialize(self.ldap_URL) |
|---|
| 24 | self.ldap_base = ldap_base |
|---|
| [772] | 25 | self.key = file(keyFile).read() |
|---|
| [528] | 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" |
|---|
| [627] | 33 | def searchLDAP(self, vhost): |
|---|
| [2757] | 34 | attrlist = ('scriptsVhostName', 'homeDirectory', 'scriptsVhostDirectory', 'uid') |
|---|
| [1741] | 35 | results = self.ldap.search_st(self.ldap_base, ldap.SCOPE_SUBTREE, |
|---|
| [627] | 36 | ldap.filter.filter_format( |
|---|
| [2757] | 37 | '(|(scriptsVhostName=%s)(scriptsVhostAlias=%s))', (vhost,)*2), |
|---|
| 38 | attrlist=attrlist, timeout=5) |
|---|
| [627] | 39 | if len(results) >= 1: |
|---|
| 40 | result = results[0] |
|---|
| 41 | attrs = result[1] |
|---|
| [2757] | 42 | for attr in attrlist: |
|---|
| [627] | 43 | attrs[attr] = attrs[attr][0] |
|---|
| 44 | return attrs |
|---|
| 45 | else: |
|---|
| 46 | return None |
|---|
| [528] | 47 | def getWhois(self, vhost): |
|---|
| 48 | vhost = self.canonicalize(vhost) |
|---|
| [2756] | 49 | info = None |
|---|
| [1741] | 50 | tries = 0 |
|---|
| 51 | while (tries < 3) and not info: |
|---|
| 52 | tries += 1 |
|---|
| 53 | try: |
|---|
| 54 | info = self.searchLDAP(vhost) |
|---|
| [1742] | 55 | break |
|---|
| [1741] | 56 | except (ldap.TIMEOUT, ldap.SERVER_DOWN): |
|---|
| 57 | self.ldap.unbind() |
|---|
| 58 | self.ldap = ldap.initialize(self.ldap_URL) |
|---|
| [528] | 59 | if info: |
|---|
| 60 | ret = "Hostname: %s\nAlias: %s\nLocker: %s\nDocument Root: %s" % \ |
|---|
| [2757] | 61 | (info['scriptsVhostName'], vhost, info['uid'], |
|---|
| 62 | posixpath.join(info['homeDirectory'], 'web_scripts', info['scriptsVhostDirectory'])) |
|---|
| [1741] | 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." |
|---|
| [528] | 65 | else: |
|---|
| 66 | ret = "No such hostname" |
|---|
| 67 | return defer.succeed(ret) |
|---|
| 68 | |
|---|
| 69 | application = service.Application('whois', uid=99, gid=99) |
|---|
| [2756] | 70 | factory = WhoisFactory( |
|---|
| [772] | 71 | "ldap://localhost", "ou=VirtualHosts,dc=scripts,dc=mit,dc=edu", "/etc/whoisd-password") |
|---|
| [528] | 72 | internet.TCPServer(43, factory).setServiceParent( |
|---|
| 73 | service.IServiceCollection(application)) |
|---|