source: server/common/oursrc/whoisd/whoisd.tac @ 528

Last change on this file since 528 was 528, checked in by quentin, 15 years ago
Implements a basic whois server for finding out who owns vhosts
File size: 2.5 KB
Line 
1from twisted.application import internet, service
2from twisted.internet import protocol, reactor, defer
3from twisted.protocols import basic
4import os, sys, glob
5
6class WhoisProtocol(basic.LineReceiver):
7    def lineReceived(self, hostname):
8        self.factory.getWhois(hostname
9        ).addErrback(lambda _: "Internal error in server"
10        ).addCallback(lambda m:
11                      (self.transport.write(m+"\r\n"),
12                       self.transport.loseConnection()))
13class WhoisFactory(protocol.ServerFactory):
14    protocol = WhoisProtocol
15    def __init__(self, vhostDir):
16        self.vhostDir = vhostDir
17        self.vhosts = {}
18        self.rescanVhosts()
19    def rescanVhosts(self):
20        newVhosts = {}
21        for f in glob.iglob(os.path.join(self.vhostDir, "*.conf")):
22            locker = os.path.splitext(os.path.basename(f))[0]
23            newVhosts.update(self.parseApacheConf(file(f)))
24        self.vhosts = newVhosts
25        self.vhostTime = os.stat(self.vhostDir).st_mtime
26    def parseApacheConf(self, f):
27        vhosts = {}
28        hostnames = []
29        locker = None
30        docroot = None
31        for l in f:
32            parts = l.split()
33            if not parts: continue
34            command = parts.pop(0)
35            if command in ("ServerName", "ServerAlias"):
36                hostnames.extend(parts)
37            elif command in ("SuExecUserGroup",):
38                locker = parts[0]
39            elif command in ("DocumentRoot",):
40                docroot = parts[0]
41            elif command == "</VirtualHost>":
42                d = {'locker': locker, 'docroot': docroot, 'canonical': hostnames[0]}
43                for h in hostnames: vhosts[h] = d
44                hostnames = []
45                locker = None
46                docroot = None
47        return vhosts
48    def canonicalize(self, vhost):
49        vhost = vhost.lower().rstrip(".")
50        return vhost
51#        if vhost.endswith(".mit.edu"):
52#            return vhost
53#        else:
54#            return vhost + ".mit.edu"
55    def getWhois(self, vhost):
56        vhost = self.canonicalize(vhost)
57        info = self.vhosts.get(vhost)
58        if info:
59            ret = "Hostname: %s\nAlias: %s\nLocker: %s\nDocument Root: %s" % \
60                (info['canonical'], vhost, info['locker'], info['docroot'])
61        else:
62            ret = "No such hostname"
63        return defer.succeed(ret)
64
65application = service.Application('whois', uid=99, gid=99)
66factory = WhoisFactory("/etc/httpd/vhosts.d")
67internet.TCPServer(43, factory).setServiceParent(
68    service.IServiceCollection(application))
Note: See TracBrowser for help on using the repository browser.