summaryrefslogtreecommitdiffstats
path: root/source4/scripting/python/samba/provision/sambadns.py
blob: 6b8561e1b53ecb25ee1ad848fdc916c4c53a045c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Unix SMB/CIFS implementation.
# backend code for provisioning DNS for a Samba4 server
#
# Copyright (C) Kai Blin <kai@samba.org> 2011
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""DNS-related provisioning"""

import os
import ldb
import samba
from samba.ndr import ndr_pack
from samba import read_and_sub_file
from samba.dcerpc import dnsp

class ARecord(dnsp.DnssrvRpcRecord):
    def __init__(self, ip_addr, serial=1, ttl=3600):
        super(ARecord, self).__init__()
        self.wType = dnsp.DNS_TYPE_A
        self.dwSerial = serial
        self.dwTtlSeconds = ttl
        self.data = ip_addr

class AAAARecord(dnsp.DnssrvRpcRecord):
    def __init__(self, ip6_addr, serial=1, ttl=3600):
        super(AAAARecord, self).__init__()
        self.wType = dnsp.DNS_TYPE_AAAA
        self.dwSerial = serial
        self.dwTtlSeconds = ttl
        self.data = ip6_addr

class NSRecord(dnsp.DnssrvRpcRecord):
    def __init__(self, dns_server, serial=1, ttl=3600):
        super(NSRecord, self).__init__()
        self.wType = dnsp.DNS_TYPE_NS
        self.dwSerial = serial
        self.dwTtlSeconds = ttl
        self.data = dns_server

class SOARecord(dnsp.DnssrvRpcRecord):
    def __init__(self, mname, rname, serial=1, refresh=900, retry=600,
                 expire=86400, minimum=3600, ttl=3600):
        super(SOARecord, self).__init__()
        self.wType = dnsp.DNS_TYPE_SOA
        self.dwSerial = serial
        self.dwTtlSeconds = ttl
        soa = dnsp.soa()
        soa.serial = serial
        soa.refresh = refresh
        soa.retry = retry
        soa.expire = expire
        soa.mname = mname
        soa.rname = rname
        self.data = soa

class SRVRecord(dnsp.DnssrvRpcRecord):
    def __init__(self, target, port, priority=0, weight=0, serial=1, ttl=3600):
        super(SRVRecord, self).__init__()
        self.wType = dnsp.DNS_TYPE_SRV
        self.dwSerial = serial
        self.dwTtlSeconds = ttl
        srv = dnsp.srv()
        srv.nameTarget = target
        srv.wPort = port
        srv.wPriority = priority
        srv.wWeight = weight
        self.data = srv

def setup_ad_dns(samdb, names, hostip=None, hostip6=None):
    domaindn = names.domaindn
    dnsdomain = names.dnsdomain.lower()
    hostname = names.netbiosname.lower()
    dnsname = "%s.%s" % (hostname, dnsdomain)
    site = names.sitename

    dns_ldif = os.path.join(samba.param.setup_dir(), "provision_dns_add.ldif")

    dns_data = read_and_sub_file(dns_ldif, {
              "DOMAINDN": domaindn,
              "DNSDOMAIN" : dnsdomain
              })
    samdb.add_ldif(dns_data, ["relax:0"])

    soa_subrecords = []
    dns_records = []

    # @ entry for the domain
    at_soa_record = SOARecord(dnsname, "hostmaster.%s" % dnsdomain)
    soa_subrecords.append(ndr_pack(at_soa_record))

    at_ns_record = NSRecord(dnsname)
    soa_subrecords.append(ndr_pack(at_ns_record))

    if hostip is not None:
        # A record
        at_a_record = ARecord(hostip)
        dns_records.append(ndr_pack(at_a_record))

    if hostip6 is not None:
        at_aaaa_record = AAAARecord(hostip6)
        dns_records.append(ndr_pack(at_aaaa_record))

    msg = ldb.Message(ldb.Dn(samdb, "DC=@,DC=%s,CN=MicrosoftDNS,CN=System,%s" %\
                                    (dnsdomain, domaindn )))
    msg["objectClass"] = ["top", "dnsNode"]
    msg["dnsRecord"] = ldb.MessageElement(soa_subrecords + dns_records,
                                          ldb.FLAG_MOD_ADD, "dnsRecord")
    samdb.add(msg)

    # _gc._tcp record
    gc_tcp_record = SRVRecord(dnsname, 3268)
    msg = ldb.Message(ldb.Dn(samdb,
            "DC=_gc._tcp,DC=%s,CN=MicrosoftDNS,CN=System,%s" % \
                    (dnsdomain, domaindn)))
    msg["objectClass"] = ["top", "dnsNode"]
    msg["dnsRecord"] = [ndr_pack(gc_tcp_record)]
    samdb.add(msg)

    # _gc._tcp.sitename._site record
    msg = ldb.Message(ldb.Dn(samdb,
            "DC=_gc._tcp.%s._sites,DC=%s,CN=MicrosoftDNS,CN=System,%s" % \
                    (names.sitename, dnsdomain, domaindn)))
    msg["objectClass"] = ["top", "dnsNode"]
    msg["dnsRecord"] = [ndr_pack(gc_tcp_record)]
    samdb.add(msg)

    # _kerberos._tcp record
    kerberos_record = SRVRecord(dnsname, 88)
    msg = ldb.Message(ldb.Dn(samdb,
            "DC=_kerberos._tcp,DC=%s,CN=MicrosoftDNS,CN=System,%s" % \
                    (dnsdomain, domaindn)))
    msg["objectClass"] = ["top", "dnsNode"]
    msg["dnsRecord"] = [ndr_pack(kerberos_record)]
    samdb.add(msg)

    # _kerberos._tcp.sitename._site record
    msg = ldb.Message(ldb.Dn(samdb,
            "DC=_kerberos._tcp.%s._sites,DC=%s,CN=MicrosoftDNS,CN=System,%s" % \
                    (site, dnsdomain, domaindn)))
    msg["objectClass"] = ["top", "dnsNode"]
    msg["dnsRecord"] = [ndr_pack(kerberos_record)]
    samdb.add(msg)

    # _kerberos._udp record
    msg = ldb.Message(ldb.Dn(samdb,
            "DC=_kerberos._udp,DC=%s,CN=MicrosoftDNS,CN=System,%s" % \
                    (dnsdomain, domaindn)))
    msg["objectClass"] = ["top", "dnsNode"]
    msg["dnsRecord"] = [ndr_pack(kerberos_record)]
    samdb.add(msg)

    # _kpasswd._tcp record
    kpasswd_record = SRVRecord(dnsname, 464)
    msg = ldb.Message(ldb.Dn(samdb,
            "DC=_kpasswd._tcp,DC=%s,CN=MicrosoftDNS,CN=System,%s" % \
                    (dnsdomain, domaindn)))
    msg["objectClass"] = ["top", "dnsNode"]
    msg["dnsRecord"] = [ndr_pack(kpasswd_record)]
    samdb.add(msg)

    # _kpasswd._udp record
    msg = ldb.Message(ldb.Dn(samdb,
            "DC=_kpasswd._udp,DC=%s,CN=MicrosoftDNS,CN=System,%s" % \
                    (dnsdomain, domaindn)))
    msg["objectClass"] = ["top", "dnsNode"]
    msg["dnsRecord"] = [ndr_pack(kpasswd_record)]
    samdb.add(msg)

    # _ldap._tcp record
    ldap_record = SRVRecord(dnsname, 389)
    msg = ldb.Message(ldb.Dn(samdb,
            "DC=_ldap._tcp,DC=%s,CN=MicrosoftDNS,CN=System,%s" % \
                    (dnsdomain, domaindn)))
    msg["objectClass"] = ["top", "dnsNode"]
    msg["dnsRecord"] = [ndr_pack(ldap_record)]
    samdb.add(msg)

    # _ldap._tcp.sitename._site record
    msg = ldb.Message(ldb.Dn(samdb,
            "DC=_ldap._tcp.%s._site,DC=%s,CN=MicrosoftDNS,CN=System,%s" % \
                    (site, dnsdomain, domaindn)))
    msg["objectClass"] = ["top", "dnsNode"]
    msg["dnsRecord"] = [ndr_pack(ldap_record)]
    samdb.add(msg)

    # _msdcs record
    msdcs_record = NSRecord(dnsname)
    msg = ldb.Message(ldb.Dn(samdb,
            "DC=_msdcs,DC=%s,CN=MicrosoftDNS,CN=System,%s" % \
                    (dnsdomain, domaindn)))
    msg["objectClass"] = ["top", "dnsNode"]
    msg["dnsRecord"] = [ndr_pack(msdcs_record)]
    samdb.add(msg)

    # the host's own record
    # Only do this if there's IP addresses to set up.
    # This is a bit weird, but the samba4.blackbox.provision.py test apparently
    # doesn't set up any IPs
    if len(dns_records) > 0:
        msg = ldb.Message(ldb.Dn(samdb,
                "DC=%s,DC=%s,CN=MicrosoftDNS,CN=System,%s" % \
                        (hostname, dnsdomain, domaindn)))
        msg["objectClass"] = ["top", "dnsNode"]
        msg["dnsRecord"] = ldb.MessageElement(dns_records,
                                              ldb.FLAG_MOD_ADD, "dnsRecord")
        samdb.add(msg)

        # DomainDnsZones record
        msg = ldb.Message(ldb.Dn(samdb,
                "DC=DomainDnsZones,DC=%s,CN=MicrosoftDNS,CN=System,%s" % \
                        (dnsdomain, domaindn)))
        msg["objectClass"] = ["top", "dnsNode"]
        msg["dnsRecord"] = ldb.MessageElement(dns_records,
                                              ldb.FLAG_MOD_ADD, "dnsRecord")

        samdb.add(msg)