Ignore:
Timestamp:
Feb 3, 2008, 5:46:34 AM (17 years ago)
Author:
quentin
Message:
Support LDAP vhosts in whois server
File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/common/oursrc/whoisd/whoisd.tac

    r528 r627  
    22from twisted.internet import protocol, reactor, defer
    33from twisted.protocols import basic
    4 import os, sys, glob
     4import ldap, ldap.filter
     5import os, sys, pwd, glob
    56
    67class WhoisProtocol(basic.LineReceiver):
     
    1314class WhoisFactory(protocol.ServerFactory):
    1415    protocol = WhoisProtocol
    15     def __init__(self, vhostDir):
     16    def __init__(self, vhostDir, ldap_URL, ldap_base):
    1617        self.vhostDir = vhostDir
     18        self.ldap_URL = ldap_URL
     19        self.ldap = ldap.initialize(self.ldap_URL)
     20        self.ldap_base = ldap_base
    1721        self.vhosts = {}
    1822        self.rescanVhosts()
     
    4044                docroot = parts[0]
    4145            elif command == "</VirtualHost>":
    42                 d = {'locker': locker, 'docroot': docroot, 'canonical': hostnames[0]}
     46                d = {'locker': locker, 'apacheDocumentRoot': docroot, 'apacheServerName': hostnames[0]}
    4347                for h in hostnames: vhosts[h] = d
    4448                hostnames = []
     
    5357#        else:
    5458#            return vhost + ".mit.edu"
     59    def searchLDAP(self, vhost):
     60        results = self.ldap.search_s(self.ldap_base, ldap.SCOPE_SUBTREE,
     61            ldap.filter.filter_format(
     62                '(|(apacheServername=%s)(apacheServerAlias=%s))', (vhost,)*2))
     63        if len(results) >= 1:
     64            result = results[0]
     65            attrs = result[1]
     66            for attr in ('apacheServerName','apacheDocumentRoot', 'apacheSuexecUid', 'apacheSuexecGid'):
     67                attrs[attr] = attrs[attr][0]
     68            user = pwd.getpwuid(int(attrs['apacheSuexecUid']))
     69            if user:
     70                attrs['locker'] = user.pw_name
     71            else:
     72                attrs['locker'] = None
     73            return attrs
     74        else:
     75            return None
    5576    def getWhois(self, vhost):
    5677        vhost = self.canonicalize(vhost)
    5778        info = self.vhosts.get(vhost)
     79        if not info:
     80            info = self.searchLDAP(vhost)
    5881        if info:
    5982            ret = "Hostname: %s\nAlias: %s\nLocker: %s\nDocument Root: %s" % \
    60                 (info['canonical'], vhost, info['locker'], info['docroot'])
     83                (info['apacheServerName'], vhost, info['locker'], info['apacheDocumentRoot'])
    6184        else:
    6285            ret = "No such hostname"
     
    6487
    6588application = service.Application('whois', uid=99, gid=99)
    66 factory = WhoisFactory("/etc/httpd/vhosts.d")
     89factory = WhoisFactory("/etc/httpd/vhosts.d",
     90    "ldap://localhost", "ou=VirtualHosts,dc=scripts,dc=mit,dc=edu")
    6791internet.TCPServer(43, factory).setServiceParent(
    6892    service.IServiceCollection(application))
Note: See TracChangeset for help on using the changeset viewer.