samba-tool:dns: Add zone create/delete commands
[ddiss/samba.git] / source4 / scripting / python / samba / netcmd / dns.py
index 3c1bb98160141da5864bba3aaf20f72ef865de32..3dca36fcd3c184acfd8d817f23375286ec864f5f 100644 (file)
@@ -139,6 +139,8 @@ def dns_type_flag(rec_type):
         record_type = dnsp.DNS_TYPE_A
     elif rtype == 'AAAA':
         record_type = dnsp.DNS_TYPE_AAAA
+    elif rtype == 'PTR':
+        record_type = dnsp.DNS_TYPE_PTR
     elif rtype == 'NS':
         record_type = dnsp.DNS_TYPE_NS
     elif rtype == 'CNAME':
@@ -326,6 +328,10 @@ def print_dns_record(outf, rec):
     mesg = 'Unknown: '
     if rec.wType == dnsp.DNS_TYPE_A:
         mesg = 'A: %s' % (rec.data)
+    elif rec.wType == dnsp.DNS_TYPE_AAAA:
+        mesg = 'AAAA: %s' % (rec.data)
+    elif rec.wType == dnsp.DNS_TYPE_PTR:
+        mesg = 'PTR: %s' % (rec.data.str)
     elif rec.wType == dnsp.DNS_TYPE_NS:
         mesg = 'NS: %s' % (rec.data.str)
     elif rec.wType == dnsp.DNS_TYPE_CNAME:
@@ -376,6 +382,19 @@ class AAAARecord(dnsserver.DNS_RPC_RECORD):
         self.dwTtlSeconds = ttl
         self.data = ip6_addr
 
+class PTRRecord(dnsserver.DNS_RPC_RECORD):
+    def __init__(self, ptr, serial=1, ttl=900, rank=dnsp.DNS_RANK_ZONE,
+                 node_flag=0):
+        super(PTRRecord, self).__init__()
+        self.wType = dnsp.DNS_TYPE_PTR
+        self.dwFlags = rank | node_flag
+        self.dwSerial = serial
+        self.dwTtleSeconds = ttl
+        ptr_name = dnsserver.DNS_RPC_NAME()
+        ptr_name.str = ptr
+        ptr_name.len = len(ptr)
+        self.data = ptr_name
+
 class CNameRecord(dnsserver.DNS_RPC_RECORD):
     def __init__(self, cname, serial=1, ttl=900, rank=dnsp.DNS_RANK_ZONE,
                     node_flag=0):
@@ -471,11 +490,14 @@ def dns_record_match(dns_conn, server, zone, name, record_type, data):
         elif record_type == dnsp.DNS_TYPE_AAAA:
             if rec_match.data == data:
                 found = True
+        elif record_type == dnsp.DNS_TYPE_PTR:
+            if rec_match.data.str.rstrip('.') == data.rstrip('.'):
+                found = True
         elif record_type == dnsp.DNS_TYPE_CNAME:
-            if rec_match.data == data:
+            if rec_match.data.str.rstrip('.') == data.rstrip('.'):
                 found = True
         elif record_type == dnsp.DNS_TYPE_NS:
-            if rec_match.data == data:
+            if rec_match.data.str.rstrip('.') == data.rstrip('.'):
                 found = True
 
         if found:
@@ -615,6 +637,89 @@ class cmd_zonelist(Command):
         print_enumzones(self.outf, typeid, res)
 
 
+class cmd_zonecreate(Command):
+    """Create a zone"""
+
+    synopsis = '%prog <server> <zone> [options]'
+
+    takes_args = [ 'server', 'zone' ]
+
+    takes_options = [
+        Option('--client-version', help='Client Version',
+                default='longhorn', metavar='w2k|dotnet|longhorn',
+                choices=['w2k','dotnet','longhorn'], dest='cli_ver')
+    ]
+
+    def run(self, server, zone, cli_ver, sambaopts=None, credopts=None,
+            versionopts=None):
+
+        self.lp = sambaopts.get_loadparm()
+        self.creds = credopts.get_credentials(self.lp)
+        dns_conn = dns_connect(server, self.lp, self.creds)
+
+        zone = zone.lower()
+
+        client_version = dns_client_version(cli_ver)
+        if client_version == dnsserver.DNS_CLIENT_VERSION_W2K:
+            typeid = dnsserver.DNSSRV_TYPEID_ZONE_CREATE_W2K
+            zone_create_info = dnsserver.DNS_RPC_ZONE_CREATE_INFO_W2K()
+            zone_create_info.pszZoneName = zone
+            zone_create_info.dwZoneType = dnsp.DNS_ZONE_TYPE_PRIMARY
+            zone_create_info.fAllowUpdate = dnsp.DNS_ZONE_UPDATE_SECURE
+            zone_create_info.fAging = 0
+        elif client_version == dnsserver.DNS_CLIENT_VERSION_DOTNET:
+            typeid = dnsserver.DNSSRV_TYPEID_ZONE_CREATE_DOTNET
+            zone_create_info = dnsserver.DNS_RPC_ZONE_CREATE_INFO_DOTNET()
+            zone_create_info.pszZoneName = zone
+            zone_create_info.dwZoneType = dnsp.DNS_ZONE_TYPE_PRIMARY
+            zone_create_info.fAllowUpdate = dnsp.DNS_ZONE_UPDATE_SECURE
+            zone_create_info.fAging = 0
+            zone_create_info.dwDpFlags = dnsserver.DNS_DP_DOMAIN_DEFAULT
+        else:
+            typeid = dnsserver.DNSSRV_TYPEID_ZONE_CREATE
+            zone_create_info = dnsserver.DNS_RPC_ZONE_CREATE_INFO_LONGHORN()
+            zone_create_info.pszZoneName = zone
+            zone_create_info.dwZoneType = dnsp.DNS_ZONE_TYPE_PRIMARY
+            zone_create_info.fAllowUpdate = dnsp.DNS_ZONE_UPDATE_SECURE
+            zone_create_info.fAging = 0
+            zone_create_info.dwDpFlags = dnsserver.DNS_DP_DOMAIN_DEFAULT
+
+        res = dns_conn.DnssrvOperation2(client_version,
+                                        0,
+                                        server,
+                                        None,
+                                        0,
+                                        'ZoneCreate',
+                                        typeid,
+                                        zone_create_info)
+        self.outf.write('Zone %s created successfully\n' % zone)
+
+
+class cmd_zonedelete(Command):
+    """Delete a zone"""
+
+    synopsis = '%prog <server> <zone> [options]'
+
+    takes_args = [ 'server', 'zone' ]
+
+    def run(self, server, zone, sambaopts=None, credopts=None, versionopts=None):
+
+        self.lp = sambaopts.get_loadparm()
+        self.creds = credopts.get_credentials(self.lp)
+        dns_conn = dns_connect(server, self.lp, self.creds)
+
+        zone = zone.lower()
+        res = dns_conn.DnssrvOperation2(dnsserver.DNS_CLIENT_VERSION_LONGHORN,
+                                        0,
+                                        server,
+                                        zone,
+                                        0,
+                                        'DeleteZoneFromDs',
+                                        dnsserver.DNSSRV_TYPEID_NULL,
+                                        None)
+        self.outf.write('Zone %s delete successfully\n' % zone)
+
+
 class cmd_query(Command):
     """Query a name."""
 
@@ -717,7 +822,7 @@ class cmd_roothints(Command):
 class cmd_add_record(Command):
     """Add a DNS record"""
 
-    synopsis = '%prog <server> <zone> <name> <A|AAAA|CNAME|NS> <data>'
+    synopsis = '%prog <server> <zone> <name> <A|AAAA|PTR|CNAME|NS> <data>'
 
     takes_args = [ 'server', 'zone', 'name', 'rtype', 'data' ]
 
@@ -729,6 +834,8 @@ class cmd_add_record(Command):
             rec = ARecord(data)
         elif record_type == dnsp.DNS_TYPE_AAAA:
             rec = AAAARecord(data)
+        elif record_type == dnsp.DNS_TYPE_PTR:
+            rec = PTRRecord(data)
         elif record_type == dnsp.DNS_TYPE_CNAME:
             rec = CNameRecord(data)
         elif record_type == dnsp.DNS_TYPE_NS:
@@ -760,7 +867,7 @@ class cmd_add_record(Command):
 class cmd_update_record(Command):
     """Update a DNS record"""
 
-    synopsis = '%prog <server> <zone> <name> <A|AAAA|CNAME|NS> <olddata> <newdata>'
+    synopsis = '%prog <server> <zone> <name> <A|AAAA|PTR|CNAME|NS> <olddata> <newdata>'
 
     takes_args = [ 'server', 'zone', 'name', 'rtype', 'olddata', 'newdata' ]
 
@@ -772,6 +879,8 @@ class cmd_update_record(Command):
             rec = ARecord(newdata)
         elif record_type == dnsp.DNS_TYPE_AAAA:
             rec = AAAARecord(newdata)
+        elif record_type == dnsp.DNS_TYPE_PTR:
+            rec = PTRRecord(newdata)
         elif record_type == dnsp.DNS_TYPE_CNAME:
             rec = CNameRecord(newdata)
         elif record_type == dnsp.DNS_TYPE_NS:
@@ -812,7 +921,7 @@ class cmd_update_record(Command):
 class cmd_delete_record(Command):
     """Delete a DNS record"""
 
-    synopsis = '%prog <server> <zone> <name> <A|AAAA|CNAME|NS> <data>'
+    synopsis = '%prog <server> <zone> <name> <A|AAAA|PTR|CNAME|NS> <data>'
 
     takes_args = [ 'server', 'zone', 'name', 'rtype', 'data' ]
 
@@ -824,6 +933,8 @@ class cmd_delete_record(Command):
             rec = ARecord(data)
         elif record_type == dnsp.DNS_TYPE_AAAA:
             rec = AAAARecord(data)
+        elif record_type == dnsp.DNS_TYPE_PTR:
+            rec = PTRRecord(data)
         elif record_type == dnsp.DNS_TYPE_CNAME:
             rec = CNameRecord(data)
         elif record_type == dnsp.DNS_TYPE_NS:
@@ -859,6 +970,8 @@ class cmd_dns(SuperCommand):
     subcommands['serverinfo'] = cmd_serverinfo()
     subcommands['zoneinfo'] = cmd_zoneinfo()
     subcommands['zonelist'] = cmd_zonelist()
+    subcommands['zonecreate'] = cmd_zonecreate()
+    subcommands['zonedelete'] = cmd_zonedelete()
     subcommands['query'] = cmd_query()
     subcommands['roothints'] = cmd_roothints()
     subcommands['add'] = cmd_add_record()