From 6baa834ebe710d73cfd54e465479a2b2de9d2476 Mon Sep 17 00:00:00 2001
From: Andrew Tridgell <tridge@samba.org>
Date: Tue, 17 Aug 2010 11:21:11 +1000
Subject: s4-ldb: use LDB_FLAG_MOD_TYPE() to extract element type from messages

The flags field of message elements is part of a set of flags. We had
LDB_FLAG_MOD_MASK for extracting the type, but it was only rarely
being used (only 1 call used it correctly). This adds
LDB_FLAG_MOD_MASK() to make it more obvious what is going on.

This will allow us to use some of the other flags bits for internal
markers on elements

Pair-Programmed-With: Andrew Bartlett <abartlet@samba.org>
---
 source4/dsdb/common/util.c                         |  6 +++---
 source4/dsdb/samdb/ldb_modules/acl.c               |  6 +++---
 source4/dsdb/samdb/ldb_modules/extended_dn_store.c |  2 +-
 source4/dsdb/samdb/ldb_modules/password_hash.c     | 10 +++++-----
 source4/dsdb/samdb/ldb_modules/samldb.c            | 12 ++++++------
 source4/lib/ldb/include/ldb.h                      |  5 +++++
 source4/lib/registry/ldb.c                         |  2 +-
 source4/rpc_server/lsa/dcesrv_lsa.c                |  8 ++++----
 8 files changed, 28 insertions(+), 23 deletions(-)

(limited to 'source4')

diff --git a/source4/dsdb/common/util.c b/source4/dsdb/common/util.c
index 701040f233b..7fcc3bf0a43 100644
--- a/source4/dsdb/common/util.c
+++ b/source4/dsdb/common/util.c
@@ -766,7 +766,7 @@ int samdb_msg_find_old_and_new_ldb_val(const struct ldb_message *msg,
 
 	for (i = 0; i < msg->num_elements; i++) {
 		if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
-			if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
+			if (LDB_FLAG_MOD_TYPE(msg->elements[i].flags) == LDB_FLAG_MOD_DELETE) {
 				*old_val = &msg->elements[i].values[0];
 			} else {
 				*new_val = &msg->elements[i].values[0];
@@ -874,7 +874,7 @@ int samdb_msg_add_addval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
 	for (i = 0; i < msg->num_elements; i++) {
 		el = &msg->elements[i];
 		if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
-		    (el->flags == LDB_FLAG_MOD_ADD)) {
+		    (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD)) {
 			found = true;
 			break;
 		}
@@ -930,7 +930,7 @@ int samdb_msg_add_delval(struct ldb_context *sam_ldb, TALLOC_CTX *mem_ctx,
 	for (i = 0; i < msg->num_elements; i++) {
 		el = &msg->elements[i];
 		if ((ldb_attr_cmp(el->name, attr_name) == 0) &&
-		    (el->flags == LDB_FLAG_MOD_DELETE)) {
+		    (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
 			found = true;
 			break;
 		}
diff --git a/source4/dsdb/samdb/ldb_modules/acl.c b/source4/dsdb/samdb/ldb_modules/acl.c
index 11fffa4053c..4eb27e9d1ef 100644
--- a/source4/dsdb/samdb/ldb_modules/acl.c
+++ b/source4/dsdb/samdb/ldb_modules/acl.c
@@ -761,13 +761,13 @@ static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
 	}
 	for (l = passwordAttrs; *l != NULL; l++) {
 		while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
-			if (el->flags == LDB_FLAG_MOD_DELETE) {
+			if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
 				++del_attr_cnt;
 			}
-			if (el->flags == LDB_FLAG_MOD_ADD) {
+			if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
 				++add_attr_cnt;
 			}
-			if (el->flags == LDB_FLAG_MOD_REPLACE) {
+			if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
 				++rep_attr_cnt;
 			}
 			ldb_msg_remove_element(msg, el);
diff --git a/source4/dsdb/samdb/ldb_modules/extended_dn_store.c b/source4/dsdb/samdb/ldb_modules/extended_dn_store.c
index 07d106e2225..3f5f451f946 100644
--- a/source4/dsdb/samdb/ldb_modules/extended_dn_store.c
+++ b/source4/dsdb/samdb/ldb_modules/extended_dn_store.c
@@ -425,7 +425,7 @@ static int extended_dn_modify(struct ldb_module *module, struct ldb_request *req
 			 * element, only do a lookup if
 			 * extended_store_replace determines it's an
 			 * input of an extended DN */
-			bool is_delete = ((el->flags & LDB_FLAG_MOD_MASK) == LDB_FLAG_MOD_DELETE);
+			bool is_delete = (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE);
 
 			ret = extended_store_replace(ac, req->op.mod.message->elements, &el->values[j],
 						     is_delete, schema_attr->syntax->ldap_oid);
diff --git a/source4/dsdb/samdb/ldb_modules/password_hash.c b/source4/dsdb/samdb/ldb_modules/password_hash.c
index 60f0c3eb184..a3c06b6f057 100644
--- a/source4/dsdb/samdb/ldb_modules/password_hash.c
+++ b/source4/dsdb/samdb/ldb_modules/password_hash.c
@@ -2450,17 +2450,17 @@ static int password_hash_modify(struct ldb_module *module, struct ldb_request *r
 	rep_attr_cnt = 0;
 	for (l = passwordAttrs; *l != NULL; l++) {
 		while ((passwordAttr = ldb_msg_find_element(msg, *l)) != NULL) {
-			if (passwordAttr->flags == LDB_FLAG_MOD_DELETE) {
+			if (LDB_FLAG_MOD_TYPE(passwordAttr->flags) == LDB_FLAG_MOD_DELETE) {
 				++del_attr_cnt;
 			}
-			if (passwordAttr->flags == LDB_FLAG_MOD_ADD) {
+			if (LDB_FLAG_MOD_TYPE(passwordAttr->flags) == LDB_FLAG_MOD_ADD) {
 				++add_attr_cnt;
 			}
-			if (passwordAttr->flags == LDB_FLAG_MOD_REPLACE) {
+			if (LDB_FLAG_MOD_TYPE(passwordAttr->flags) == LDB_FLAG_MOD_REPLACE) {
 				++rep_attr_cnt;
 			}
 			if ((passwordAttr->num_values != 1) &&
-			    (passwordAttr->flags == LDB_FLAG_MOD_ADD)) {
+			    (LDB_FLAG_MOD_TYPE(passwordAttr->flags) == LDB_FLAG_MOD_ADD)) {
 				talloc_free(ac);
 				ldb_asprintf_errstring(ldb,
 						       "'%s' attribute must have exactly one value on add operations!",
@@ -2468,7 +2468,7 @@ static int password_hash_modify(struct ldb_module *module, struct ldb_request *r
 				return LDB_ERR_CONSTRAINT_VIOLATION;
 			}
 			if ((passwordAttr->num_values > 1) &&
-			    (passwordAttr->flags == LDB_FLAG_MOD_DELETE)) {
+			    (LDB_FLAG_MOD_TYPE(passwordAttr->flags) == LDB_FLAG_MOD_DELETE)) {
 				talloc_free(ac);
 				ldb_asprintf_errstring(ldb,
 						       "'%s' attribute must have zero or one value(s) on delete operations!",
diff --git a/source4/dsdb/samdb/ldb_modules/samldb.c b/source4/dsdb/samdb/ldb_modules/samldb.c
index a7bb0f6db85..2b46867b1ad 100644
--- a/source4/dsdb/samdb/ldb_modules/samldb.c
+++ b/source4/dsdb/samdb/ldb_modules/samldb.c
@@ -1421,7 +1421,7 @@ static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
 	/* TODO: do not modify original request, create a new one */
 
 	el = ldb_msg_find_element(req->op.mod.message, "groupType");
-	if (el && (el->flags == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
+	if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
 		uint32_t group_type;
 
 		req->op.mod.message = msg = ldb_msg_copy_shallow(req,
@@ -1438,12 +1438,12 @@ static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
 		el2 = ldb_msg_find_element(msg, "sAMAccountType");
 		el2->flags = LDB_FLAG_MOD_REPLACE;
 	}
-	if (el && (el->flags == LDB_FLAG_MOD_DELETE)) {
+	if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
 	el = ldb_msg_find_element(req->op.mod.message, "primaryGroupID");
-	if (el && (el->flags == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
+	if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
 		req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
 			req->op.mod.message);
 
@@ -1452,12 +1452,12 @@ static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
 			return ret;
 		}
 	}
-	if (el && (el->flags == LDB_FLAG_MOD_DELETE)) {
+	if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
 	el = ldb_msg_find_element(req->op.mod.message, "userAccountControl");
-	if (el && (el->flags == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
+	if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
 		uint32_t user_account_control;
 
 		req->op.mod.message = msg = ldb_msg_copy_shallow(req,
@@ -1496,7 +1496,7 @@ static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
 			}
 		}
 	}
-	if (el && (el->flags == LDB_FLAG_MOD_DELETE)) {
+	if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
diff --git a/source4/lib/ldb/include/ldb.h b/source4/lib/ldb/include/ldb.h
index f3ec1ed606d..6625d94dbd5 100644
--- a/source4/lib/ldb/include/ldb.h
+++ b/source4/lib/ldb/include/ldb.h
@@ -109,6 +109,11 @@ struct ldb_dn;
 */
 #define LDB_FLAG_MOD_MASK  0x3
 
+/**
+  use this to extract the mod type from the operation
+ */
+#define LDB_FLAG_MOD_TYPE(flags) ((flags) & LDB_FLAG_MOD_MASK)
+
 /**
    Flag value used in ldap_modify() to indicate that attributes are
    being added.
diff --git a/source4/lib/registry/ldb.c b/source4/lib/registry/ldb.c
index c14ee70ace7..91c67639025 100644
--- a/source4/lib/registry/ldb.c
+++ b/source4/lib/registry/ldb.c
@@ -830,7 +830,7 @@ static WERROR ldb_set_value(struct hive_key *parent,
 	if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 		i = 0;
 		while (i < msg->num_elements) {
-			if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
+			if (LDB_FLAG_MOD_TYPE(msg->elements[i].flags) == LDB_FLAG_MOD_DELETE) {
 				ldb_msg_remove_element(msg, &msg->elements[i]);
 			} else {
 				++i;
diff --git a/source4/rpc_server/lsa/dcesrv_lsa.c b/source4/rpc_server/lsa/dcesrv_lsa.c
index 0a94a874809..0a347e07dd8 100644
--- a/source4/rpc_server/lsa/dcesrv_lsa.c
+++ b/source4/rpc_server/lsa/dcesrv_lsa.c
@@ -2600,7 +2600,7 @@ static NTSTATUS dcesrv_lsa_AddRemoveAccountRights(struct dcesrv_call_state *dce_
 	msg->dn = ldb_dn_new(msg, state->pdb, dnstr);
 	NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg->dn, msg);
 
-	if (ldb_flag == LDB_FLAG_MOD_ADD) {
+	if (LDB_FLAG_MOD_TYPE(ldb_flag) == LDB_FLAG_MOD_ADD) {
 		NTSTATUS status;
 
 		r2.in.handle = &state->handle->wire_handle;
@@ -2619,7 +2619,7 @@ static NTSTATUS dcesrv_lsa_AddRemoveAccountRights(struct dcesrv_call_state *dce_
 			return NT_STATUS_NO_SUCH_PRIVILEGE;
 		}
 
-		if (ldb_flag == LDB_FLAG_MOD_ADD) {
+		if (LDB_FLAG_MOD_TYPE(ldb_flag) == LDB_FLAG_MOD_ADD) {
 			uint32_t j;
 			for (j=0;j<r2.out.rights->count;j++) {
 				if (strcasecmp_m(r2.out.rights->names[j].string, 
@@ -2655,12 +2655,12 @@ static NTSTATUS dcesrv_lsa_AddRemoveAccountRights(struct dcesrv_call_state *dce_
 		ret = ldb_add(state->pdb, msg);		
 	}
 	if (ret != LDB_SUCCESS) {
-		if (ldb_flag == LDB_FLAG_MOD_DELETE && ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
+		if (LDB_FLAG_MOD_TYPE(ldb_flag) == LDB_FLAG_MOD_DELETE && ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
 			talloc_free(msg);
 			return NT_STATUS_OK;
 		}
 		DEBUG(3, ("Could not %s attributes from %s: %s", 
-			  ldb_flag == LDB_FLAG_MOD_DELETE ? "delete" : "add",
+			  LDB_FLAG_MOD_TYPE(ldb_flag) == LDB_FLAG_MOD_DELETE ? "delete" : "add",
 			  ldb_dn_get_linearized(msg->dn), ldb_errstring(state->pdb)));
 		talloc_free(msg);
 		return NT_STATUS_UNEXPECTED_IO_ERROR;
-- 
cgit