~ubuntu-branches/ubuntu/lucid/landscape-client/lucid-updates

« back to all changes in this revision

Viewing changes to dev/dns-server

  • Committer: Package Import Robot
  • Author(s): Andreas Hasenack
  • Date: 2012-04-10 14:28:48 UTC
  • mfrom: (1.1.27)
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: package-import@ubuntu.com-20120410142848-7xsy4g2xii7y7ntc
ImportĀ upstreamĀ versionĀ 12.04.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
"""
 
3
This script creates a DNS server that has enough functionality to test
 
4
the server autodiscovery feature of landscape client.
 
5
 
 
6
Landscape client uses /etc/resolv.conf to find the location of name servers.
 
7
To have landscape client use this server, make sure that:
 
8
 
 
9
nameserver 127.0.0.1
 
10
 
 
11
is the first nameserver in /etc/resolv.conf.  Linux name lookups only support
 
12
port 53, so this program must run on port 53 in order to work.  Be aware that
 
13
NetworkManager overwrites this file any time your network configuration
 
14
changes.
 
15
 
 
16
This program does not return enough information for a tool like dig to complete
 
17
successfully.  If this is needed, use Bind 9, detailed at
 
18
https://wiki.canonical.com/Landscape/SpecRegistry/0082
 
19
"""
 
20
 
 
21
import argparse
 
22
import sys
 
23
from twisted.internet import reactor, defer
 
24
from twisted.names import dns, common
 
25
from twisted.names.server import DNSServerFactory
 
26
 
 
27
 
 
28
PORT = 5553
 
29
SRV_RESPONSE = 'lds1.mylandscapehost.com'
 
30
A_RESPONSE = '127.0.0.1'
 
31
 
 
32
 
 
33
class SimpleResolver(common.ResolverBase):
 
34
    def _lookup(self, name, cls, typ, timeout):
 
35
        """
 
36
        Respond to DNS requests.  See documentation for
 
37
        L{twisted.names.common.ResolverBase}.
 
38
        """
 
39
        # This nameserver returns the same result all the time, regardless
 
40
        # of what name the client asks for.
 
41
        results = []
 
42
        ttl = 60
 
43
        if typ == dns.SRV:
 
44
            record = dns.Record_SRV(0, 1, 80, SRV_RESPONSE, ttl)
 
45
            owner = '_landscape._tcp.mylandscapehost.com'
 
46
            results.append(dns.RRHeader(owner, record.TYPE, dns.IN, ttl,
 
47
                                        record, auth=True))
 
48
        elif typ == dns.A:
 
49
            record = dns.Record_A(A_RESPONSE)
 
50
            owner = 'landscape.localdomain'
 
51
            results.append(dns.RRHeader(owner, record.TYPE, dns.IN, ttl,
 
52
                                        record, auth=True))
 
53
 
 
54
        authority = []
 
55
        return defer.succeed((results, authority, []))
 
56
 
 
57
 
 
58
def parse_command_line(args):
 
59
    global SRV_RESPONSE, A_RESPONSE, PORT
 
60
    description = """
 
61
    This test tool responds to DNS queries for SRV and A records.  It always
 
62
    responds with the same result regardless of the query string sent by the
 
63
    client.
 
64
 
 
65
    To test this tool, try the following commands:
 
66
    dig -p 5553 @127.0.0.1 SRV _landscape._tcp.mylandscapehost.com
 
67
 
 
68
    dig -p 5553 @127.0.0.1 localhost.localdomain
 
69
    """
 
70
    parser = argparse.ArgumentParser(description=description)
 
71
    parser.add_argument("--srv-response", type=str, default=SRV_RESPONSE,
 
72
                        help="Give this reply to SRV queries (eg: localhost)")
 
73
    parser.add_argument("--a-response", type=str, default=A_RESPONSE,
 
74
                        help="Give this reply to A queries (eg: 127.0.0.1)")
 
75
    parser.add_argument("--port", type=int, default=PORT,
 
76
                        help="Listen on this port (default 5553).  DNS "
 
77
                        "normally runs on port 53")
 
78
 
 
79
    args = vars(parser.parse_args())
 
80
    SRV_RESPONSE = args["srv_response"]
 
81
    A_RESPONSE = args["a_response"]
 
82
    PORT = args["port"]
 
83
 
 
84
 
 
85
def main():
 
86
    parse_command_line(sys.argv)
 
87
 
 
88
    simple_resolver = SimpleResolver()
 
89
    factory = DNSServerFactory(authorities=[simple_resolver], verbose=1)
 
90
    protocol = dns.DNSDatagramProtocol(factory)
 
91
    print "starting reactor on port %s.." % PORT
 
92
    reactor.listenTCP(PORT, factory)
 
93
    reactor.listenUDP(PORT, protocol)
 
94
    reactor.run()
 
95
    print "reactor stopped..."
 
96
 
 
97
 
 
98
if __name__ == "__main__":
 
99
    main()