python: Use generated WERROR definitions
authorBob Campbell <bobcampbell@catalyst.net.nz>
Sun, 12 Feb 2017 22:12:54 +0000 (11:12 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 14 Feb 2017 08:46:23 +0000 (09:46 +0100)
Previously we either defined WERRORs locally or compared them against
strings where we needed to use them.

Signed-off-by: Bob Campbell <bobcampbell@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
python/samba/join.py
python/samba/netcmd/dns.py
python/samba/netcmd/domain.py
python/samba/remove_dc.py
python/samba/tests/dns.py

index 4eb8c58ec4a42cfe8d1f1c74013ab2192fdb9d5f..60773c49a6ce1472514a869cd202ed9c1eb43560 100644 (file)
@@ -33,6 +33,7 @@ from samba import descriptor
 from samba.net import Net
 from samba.provision.sambadns import setup_bind9_dns
 from samba import read_and_sub_file
+from samba import werror
 from base64 import b64encode
 import logging
 import talloc
@@ -484,13 +485,13 @@ class dc_join(object):
             if ctr.dir_err != drsuapi.DRSUAPI_DIRERR_OK:
                 print("DsAddEntry failed with dir_err %u" % ctr.dir_err)
                 raise RuntimeError("DsAddEntry failed")
-            if ctr.extended_err != (0, 'WERR_OK'):
+            if ctr.extended_err[0] != werror.WERR_SUCCESS:
                 print("DsAddEntry failed with status %s info %s" % (ctr.extended_err))
                 raise RuntimeError("DsAddEntry failed")
         if level == 3:
             if ctr.err_ver != 1:
                 raise RuntimeError("expected err_ver 1, got %u" % ctr.err_ver)
-            if ctr.err_data.status != (0, 'WERR_OK'):
+            if ctr.err_data.status[0] != werror.WERR_SUCCESS:
                 print("DsAddEntry failed with status %s info %s" % (ctr.err_data.status,
                                                                     ctr.err_data.info.extended_err))
                 raise RuntimeError("DsAddEntry failed")
index fe5a2930dd90eb48c21926140e841c909299b56c..caacb0a8913203ad557f12e61cf346e484576178 100644 (file)
@@ -18,6 +18,7 @@
 
 import samba.getopt as options
 from samba import WERRORError
+from samba import werror
 from struct import pack
 from socket import inet_ntoa
 from socket import inet_ntop
@@ -624,7 +625,7 @@ def dns_record_match(dns_conn, server, zone, name, record_type, data):
             dnsserver.DNS_CLIENT_VERSION_LONGHORN, 0, server, zone, name, None,
             record_type, select_flags, None, None)
     except WERRORError as e:
-        if e.args[1] == 'WERR_DNS_ERROR_NAME_DOES_NOT_EXIST':
+        if e.args[0] == werror.WERR_DNS_ERROR_NAME_DOES_NOT_EXIST:
             # Either the zone doesn't exist, or there were no records.
             # We can't differentiate the two.
             return None
@@ -900,7 +901,7 @@ class cmd_zonecreate(Command):
                                             0, 'ResetDwordProperty', typeid,
                                             name_and_param)
         except WERRORError as e:
-            if e.args[1] == 'WERR_DNS_ERROR_ZONE_ALREADY_EXISTS':
+            if e.args[0] == werror.WERR_DNS_ERROR_ZONE_ALREADY_EXISTS:
                 self.outf.write('Zone already exists.')
             raise e
 
@@ -934,7 +935,7 @@ class cmd_zonedelete(Command):
                                             dnsserver.DNSSRV_TYPEID_NULL,
                                             None)
         except WERRORError as e:
-            if e.args[1] == 'WERR_DNS_ERROR_ZONE_DOES_NOT_EXIST':
+            if e.args[0] == werror.WERR_DNS_ERROR_ZONE_DOES_NOT_EXIST:
                 self.outf.write('Zone does not exist and so could not be deleted.')
             raise e
 
@@ -1013,7 +1014,7 @@ class cmd_query(Command):
                 dnsserver.DNS_CLIENT_VERSION_LONGHORN, 0, server, zone, name,
                 None, record_type, select_flags, None, None)
         except WERRORError as e:
-            if e.args[1] == 'WERR_DNS_ERROR_NAME_DOES_NOT_EXIST':
+            if e.args[0] == werror.WERR_DNS_ERROR_NAME_DOES_NOT_EXIST:
                 self.outf.write('Record or zone does not exist.')
             raise e
 
@@ -1093,7 +1094,7 @@ class cmd_add_record(Command):
             dns_conn.DnssrvUpdateRecord2(dnsserver.DNS_CLIENT_VERSION_LONGHORN,
                                          0, server, zone, name, add_rec_buf, None)
         except WERRORError as e:
-            if e.args[1] == 'WERR_DNS_ERROR_NAME_DOES_NOT_EXIST':
+            if e.args[0] == werror.WERR_DNS_ERROR_NAME_DOES_NOT_EXIST:
                 self.outf.write('Zone does not exist; record could not be added.\n')
             raise e
 
@@ -1164,7 +1165,7 @@ class cmd_update_record(Command):
                                          add_rec_buf,
                                          del_rec_buf)
         except WERRORError as e:
-            if e.args[1] == 'WERR_DNS_ERROR_NAME_DOES_NOT_EXIST':
+            if e.args[0] == werror.WERR_DNS_ERROR_NAME_DOES_NOT_EXIST:
                 self.outf.write('Zone does not exist; record could not be updated.\n')
             raise e
 
@@ -1219,7 +1220,7 @@ class cmd_delete_record(Command):
                                          None,
                                          del_rec_buf)
         except WERRORError as e:
-            if e.args[1] == 'WERR_DNS_ERROR_NAME_DOES_NOT_EXIST':
+            if e.args[0] == werror.WERR_DNS_ERROR_NAME_DOES_NOT_EXIST:
                 self.outf.write('Zone does not exist; record could not be deleted.\n')
             raise e
 
index 4a8da20c8c970998cc3e6a9f249c66408fdefdc2..24bf4fca190a69e1ecb0b9b4660720699e9697f4 100644 (file)
@@ -35,6 +35,7 @@ import subprocess
 import time
 from samba import ntstatus
 from samba import NTSTATUSError
+from samba import werror
 from getpass import getpass
 from samba.net import Net, LIBNET_JOIN_AUTOMATIC
 import samba.ntacls
@@ -797,7 +798,7 @@ class cmd_domain_demote(Command):
                 try:
                     drsuapiBind.DsReplicaSync(drsuapi_handle, 1, req1)
                 except RuntimeError as (werr, string):
-                    if werr == 8452: #WERR_DS_DRA_NO_REPLICA
+                    if werr == werror.WERR_DS_DRA_NO_REPLICA:
                         pass
                     else:
                         self.errf.write(
@@ -947,7 +948,7 @@ class cmd_domain_demote(Command):
                                                            "userAccountControl")
             remote_samdb.modify(msg)
             remote_samdb.rename(newdn, dc_dn)
-            if werr == 8452: #WERR_DS_DRA_NO_REPLICA
+            if werr == werror.WERR_DS_DRA_NO_REPLICA:
                 raise CommandError("The DC %s is not present on (already removed from) the remote server: " % server_dsa_dn, e)
             else:
                 raise CommandError("Error while sending a removeDsServer of %s: " % server_dsa_dn, e)
@@ -1613,11 +1614,6 @@ class DomainTrustCommand(Command):
         self.remote_binding_string = None
         self.remote_creds = None
 
-    WERR_OK = 0x00000000
-    WERR_INVALID_FUNCTION = 0x00000001
-    WERR_NERR_ACFNOTLOADED = 0x000008B3
-    WERR_RPC_S_PROCNUM_OUT_OF_RANGE = 0x000006D1
-
     def _uint32(self, v):
         return ctypes.c_uint32(v).value
 
@@ -2006,7 +2002,7 @@ class cmd_domain_trust_list(DomainTrustCommand):
                                     netlogon.NETR_TRUST_FLAG_OUTBOUND |
                                     netlogon.NETR_TRUST_FLAG_INBOUND)
         except RuntimeError as error:
-            if self.check_runtime_error(error, self.WERR_RPC_S_PROCNUM_OUT_OF_RANGE):
+            if self.check_runtime_error(error, werror.WERR_RPC_S_PROCNUM_OUT_OF_RANGE):
                 # TODO: we could implement a fallback to lsa.EnumTrustDom()
                 raise CommandError("LOCAL_DC[%s]: netr_DsrEnumerateDomainTrusts not supported." % (
                                    self.local_server))
@@ -2623,7 +2619,7 @@ class cmd_domain_trust_create(DomainTrustCommand):
                                        local_trust_verify.tc_connection_status[1],
                                        local_trust_verify.pdc_connection_status[1])
 
-                if local_trust_status != self.WERR_OK or local_conn_status != self.WERR_OK:
+                if local_trust_status != werror.WERR_SUCCESS or local_conn_status != werror.WERR_SUCCESS:
                     raise CommandError(local_validation)
                 else:
                     self.outf.write("OK: %s\n" % local_validation)
@@ -2653,7 +2649,7 @@ class cmd_domain_trust_create(DomainTrustCommand):
                                            remote_trust_verify.tc_connection_status[1],
                                            remote_trust_verify.pdc_connection_status[1])
 
-                    if remote_trust_status != self.WERR_OK or remote_conn_status != self.WERR_OK:
+                    if remote_trust_status != werror.WERR_SUCCESS or remote_conn_status != werror.WERR_SUCCESS:
                         raise CommandError(remote_validation)
                     else:
                         self.outf.write("OK: %s\n" % remote_validation)
@@ -2917,7 +2913,7 @@ class cmd_domain_trust_validate(DomainTrustCommand):
                                local_trust_verify.tc_connection_status[1],
                                local_trust_verify.pdc_connection_status[1])
 
-        if local_trust_status != self.WERR_OK or local_conn_status != self.WERR_OK:
+        if local_trust_status != werror.WERR_SUCCESS or local_conn_status != werror.WERR_SUCCESS:
             raise CommandError(local_validation)
         else:
             self.outf.write("OK: %s\n" % local_validation)
@@ -2937,7 +2933,7 @@ class cmd_domain_trust_validate(DomainTrustCommand):
                                local_trust_rediscover.trusted_dc_name,
                                local_trust_rediscover.tc_connection_status[1])
 
-        if local_conn_status != self.WERR_OK:
+        if local_conn_status != werror.WERR_SUCCESS:
             raise CommandError(local_rediscover)
         else:
             self.outf.write("OK: %s\n" % local_rediscover)
@@ -2975,7 +2971,7 @@ class cmd_domain_trust_validate(DomainTrustCommand):
                                    remote_trust_verify.tc_connection_status[1],
                                    remote_trust_verify.pdc_connection_status[1])
 
-            if remote_trust_status != self.WERR_OK or remote_conn_status != self.WERR_OK:
+            if remote_trust_status != werror.WERR_SUCCESS or remote_conn_status != werror.WERR_SUCCESS:
                 raise CommandError(remote_validation)
             else:
                 self.outf.write("OK: %s\n" % remote_validation)
@@ -2996,7 +2992,7 @@ class cmd_domain_trust_validate(DomainTrustCommand):
                                    remote_trust_rediscover.trusted_dc_name,
                                    remote_trust_rediscover.tc_connection_status[1])
 
-            if remote_conn_status != self.WERR_OK:
+            if remote_conn_status != werror.WERR_SUCCESS:
                 raise CommandError(remote_rediscover)
             else:
                 self.outf.write("OK: %s\n" % remote_rediscover)
@@ -3298,15 +3294,15 @@ class cmd_domain_trust_namespaces(DomainTrustCommand):
                 own_forest_info = local_netlogon.netr_DsRGetForestTrustInformation(local_netlogon_info.dc_unc,
                                                                                    None, 0)
             except RuntimeError as error:
-                if self.check_runtime_error(error, self.WERR_RPC_S_PROCNUM_OUT_OF_RANGE):
+                if self.check_runtime_error(error, werror.WERR_RPC_S_PROCNUM_OUT_OF_RANGE):
                     raise CommandError("LOCAL_DC[%s]: netr_DsRGetForestTrustInformation() not supported." % (
                                        self.local_server))
 
-                if self.check_runtime_error(error, self.WERR_INVALID_FUNCTION):
+                if self.check_runtime_error(error, werror.WERR_INVALID_FUNCTION):
                     raise CommandError("LOCAL_DC[%s]: netr_DsRGetForestTrustInformation() not supported." % (
                                        self.local_server))
 
-                if self.check_runtime_error(error, self.WERR_NERR_ACFNOTLOADED):
+                if self.check_runtime_error(error, werror.WERR_NERR_ACFNOTLOADED):
                     raise CommandError("LOCAL_DC[%s]: netr_DsRGetForestTrustInformation() not supported." % (
                                        self.local_server))
 
index db7bb826c7a48744ccdeb84697b7ea5d883d0da5..eb4cbe06f0386248b4ca40efc65ef5d1a57e8ffb 100644 (file)
@@ -19,6 +19,7 @@
 import uuid
 import ldb
 from ldb import LdbError
+from samba import werror
 from samba.ndr import ndr_unpack
 from samba.dcerpc import misc, dnsp
 from samba.dcerpc.dnsp import DNS_TYPE_NS, DNS_TYPE_A, DNS_TYPE_AAAA, \
@@ -98,7 +99,7 @@ def remove_dns_references(samdb, logger, dnsHostName):
     try:
         primary_recs = samdb.dns_lookup(dnsHostName)
     except RuntimeError as (enum, estr):
-        if enum == 0x000025F2: #WERR_DNS_ERROR_NAME_DOES_NOT_EXIST
+        if enum == werror.WERR_DNS_ERROR_NAME_DOES_NOT_EXIST:
               return
         raise DemoteException("lookup of %s failed: %s" % (dnsHostName, estr))
     samdb.dns_replace(dnsHostName, [])
@@ -141,7 +142,7 @@ def remove_dns_references(samdb, logger, dnsHostName):
             logger.debug("checking for DNS records to remove on %s" % a_name)
             a_recs = samdb.dns_lookup(a_name)
         except RuntimeError as (enum, estr):
-            if enum == 0x000025F2: #WERR_DNS_ERROR_NAME_DOES_NOT_EXIST
+            if enum == werror.WERR_DNS_ERROR_NAME_DOES_NOT_EXIST:
                 return
             raise DemoteException("lookup of %s failed: %s" % (a_name, estr))
 
index 66198b5ac42f1a4f0c1197483abf2d1b71b5331e..b8a2481ae360b5b299b9b9e67a4a4d99d091bdf1 100644 (file)
@@ -26,6 +26,7 @@ from samba.tests import TestCase
 from samba.dcerpc import dns, dnsp, dnsserver
 from samba.netcmd.dns import TXTRecord, dns_record_match, data_to_dns_record
 from samba.tests.subunitrun import SubunitOptions, TestProgram
+from samba import werror
 import samba.getopt as options
 import optparse
 
@@ -983,7 +984,7 @@ class TestZones(DNSTest):
         try:
             self.delete_zone(self.zone)
         except RuntimeError, (num, string):
-            if num != 9601: #WERR_DNS_ERROR_ZONE_DOES_NOT_EXIST
+            if num != werror.WERR_DNS_ERROR_ZONE_DOES_NOT_EXIST:
                 raise
 
     def create_zone(self, zone):