| [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 |
|---|
| [2756] | 5 | import pwd |
|---|
| [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): |
|---|
| [1741] | 34 | results = self.ldap.search_st(self.ldap_base, ldap.SCOPE_SUBTREE, |
|---|
| [627] | 35 | ldap.filter.filter_format( |
|---|
| [1741] | 36 | '(|(apacheServername=%s)(apacheServerAlias=%s))', (vhost,)*2), |
|---|
| 37 | timeout=5) |
|---|
| [627] | 38 | if len(results) >= 1: |
|---|
| 39 | result = results[0] |
|---|
| 40 | attrs = result[1] |
|---|
| 41 | for attr in ('apacheServerName','apacheDocumentRoot', 'apacheSuexecUid', 'apacheSuexecGid'): |
|---|
| 42 | attrs[attr] = attrs[attr][0] |
|---|
| 43 | user = pwd.getpwuid(int(attrs['apacheSuexecUid'])) |
|---|
| 44 | if user: |
|---|
| [771] | 45 | attrs['locker'] = user.pw_name |
|---|
| [627] | 46 | else: |
|---|
| 47 | attrs['locker'] = None |
|---|
| 48 | return attrs |
|---|
| 49 | else: |
|---|
| 50 | return None |
|---|
| [528] | 51 | def getWhois(self, vhost): |
|---|
| 52 | vhost = self.canonicalize(vhost) |
|---|
| [2756] | 53 | info = None |
|---|
| [1741] | 54 | tries = 0 |
|---|
| 55 | while (tries < 3) and not info: |
|---|
| 56 | tries += 1 |
|---|
| 57 | try: |
|---|
| 58 | info = self.searchLDAP(vhost) |
|---|
| [1742] | 59 | break |
|---|
| [1741] | 60 | except (ldap.TIMEOUT, ldap.SERVER_DOWN): |
|---|
| 61 | self.ldap.unbind() |
|---|
| 62 | self.ldap = ldap.initialize(self.ldap_URL) |
|---|
| [528] | 63 | if info: |
|---|
| 64 | ret = "Hostname: %s\nAlias: %s\nLocker: %s\nDocument Root: %s" % \ |
|---|
| [627] | 65 | (info['apacheServerName'], vhost, info['locker'], info['apacheDocumentRoot']) |
|---|
| [1741] | 66 | elif tries == 3: |
|---|
| 67 | ret = "The whois server is experiencing problems looking up LDAP records.\nPlease contact scripts@mit.edu for help if this problem persists." |
|---|
| [528] | 68 | else: |
|---|
| 69 | ret = "No such hostname" |
|---|
| 70 | return defer.succeed(ret) |
|---|
| 71 | |
|---|
| 72 | application = service.Application('whois', uid=99, gid=99) |
|---|
| [2756] | 73 | factory = WhoisFactory( |
|---|
| [772] | 74 | "ldap://localhost", "ou=VirtualHosts,dc=scripts,dc=mit,dc=edu", "/etc/whoisd-password") |
|---|
| [528] | 75 | internet.TCPServer(43, factory).setServiceParent( |
|---|
| 76 | service.IServiceCollection(application)) |
|---|