samldb: Allow automatic generation of mAPIIDs
[sfrench/samba-autobuild/.git] / source4 / lib / policy / gp_ldap.c
index ea86fb8ac56a5a08b06e0cb804328cca675eaeb5..5a1ee7d8ed65e18316634be84eeddc5d902e579d 100644 (file)
  */
 #include "includes.h"
 #include "param/param.h"
-#include "lib/ldb/include/ldb.h"
-#include "lib/ldb_wrap.h"
+#include <ldb.h>
+#include "lib/ldb-samba/ldb_wrap.h"
 #include "auth/credentials/credentials.h"
 #include "../librpc/gen_ndr/nbt.h"
 #include "libcli/libcli.h"
 #include "libnet/libnet.h"
 #include "../librpc/gen_ndr/ndr_security.h"
-#include "../libcli/security/dom_sid.h"
-#include "libcli/security/security.h"
+#include "../libcli/security/security.h"
+#include "libcli/ldap/ldap_ndr.h"
 #include "../lib/talloc/talloc.h"
 #include "lib/policy/policy.h"
 
@@ -45,12 +45,6 @@ static const struct gpo_stringmap gpo_flags [] = {
        { "GPO_FLAG_MACHINE_DISABLE", GPO_FLAG_MACHINE_DISABLE },
        { NULL, 0 }
 };
-static const struct gpo_stringmap gpo_inheritance [] = {
-       { "GPO_INHERIT", GPO_INHERIT },
-       { "GPO_BLOCK_INHERITANCE", GPO_BLOCK_INHERITANCE },
-       { NULL, 0 }
-};
-
 
 static NTSTATUS parse_gpo(TALLOC_CTX *mem_ctx, struct ldb_message *msg, struct gp_object **ret)
 {
@@ -61,26 +55,41 @@ static NTSTATUS parse_gpo(TALLOC_CTX *mem_ctx, struct ldb_message *msg, struct g
        NT_STATUS_HAVE_NO_MEMORY(gpo);
 
        gpo->dn = talloc_strdup(mem_ctx, ldb_dn_get_linearized(msg->dn));
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->dn, gpo);
+       if (gpo->dn == NULL) {
+               TALLOC_FREE(gpo);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        DEBUG(9, ("Parsing GPO LDAP data for %s\n", gpo->dn));
 
        gpo->display_name = talloc_strdup(gpo, ldb_msg_find_attr_as_string(msg, "displayName", ""));
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->display_name, gpo);
+       if (gpo->display_name == NULL) {
+               TALLOC_FREE(gpo);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        gpo->name = talloc_strdup(gpo, ldb_msg_find_attr_as_string(msg, "name", ""));
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->name, gpo);
+       if (gpo->name == NULL) {
+               TALLOC_FREE(gpo);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        gpo->flags = ldb_msg_find_attr_as_uint(msg, "flags", 0);
        gpo->version = ldb_msg_find_attr_as_uint(msg, "versionNumber", 0);
 
        gpo->file_sys_path = talloc_strdup(gpo, ldb_msg_find_attr_as_string(msg, "gPCFileSysPath", ""));
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->file_sys_path, gpo);
+       if (gpo->file_sys_path == NULL) {
+               TALLOC_FREE(gpo);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        /* Pull the security descriptor through the NDR library */
        data = ldb_msg_find_ldb_val(msg, "nTSecurityDescriptor");
        gpo->security_descriptor = talloc(gpo, struct security_descriptor);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->security_descriptor, gpo);
+       if (gpo->security_descriptor == NULL) {
+               TALLOC_FREE(gpo);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        ndr_err = ndr_pull_struct_blob(data,
                        mem_ctx,
@@ -158,7 +167,7 @@ NTSTATUS gp_init(TALLOC_CTX *mem_ctx,
        io = talloc_zero(mem_ctx, struct libnet_LookupDCs);
        NT_STATUS_HAVE_NO_MEMORY(io);
        io->in.name_type = NBT_NAME_PDC;
-       io->in.domain_name = lp_workgroup(lp_ctx);
+       io->in.domain_name = lpcfg_workgroup(lp_ctx);
 
        /* Find Active DC's */
        rv = libnet_LookupDCs(net_ctx, mem_ctx, io);
@@ -177,7 +186,6 @@ NTSTATUS gp_init(TALLOC_CTX *mem_ctx,
                return NT_STATUS_UNSUCCESSFUL;
        }
 
-
        *gp_ctx = talloc_zero(mem_ctx, struct gp_context);
        NT_STATUS_HAVE_NO_MEMORY(gp_ctx);
 
@@ -185,7 +193,7 @@ NTSTATUS gp_init(TALLOC_CTX *mem_ctx,
        (*gp_ctx)->credentials = credentials;
        (*gp_ctx)->ev_ctx = ev_ctx;
        (*gp_ctx)->ldb_ctx = ldb_ctx;
-       (*gp_ctx)->active_dc = io->out.dcs[0];
+       (*gp_ctx)->active_dc = talloc_reference(*gp_ctx, &io->out.dcs[0]);
 
        /* We don't need to keep the libnet context */
        talloc_free(net_ctx);
@@ -219,7 +227,10 @@ NTSTATUS gp_list_all_gpos(struct gp_context *gp_ctx, struct gp_object ***ret)
        DEBUG(10, ("Searching for policies in DN: %s\n", ldb_dn_get_linearized(dn)));
 
        attrs = talloc_array(mem_ctx, const char *, 7);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(attrs, mem_ctx);
+       if (attrs == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        attrs[0] = "nTSecurityDescriptor";
        attrs[1] = "versionNumber";
@@ -237,7 +248,10 @@ NTSTATUS gp_list_all_gpos(struct gp_context *gp_ctx, struct gp_object ***ret)
        }
 
        gpo = talloc_array(gp_ctx, struct gp_object *, result->count+1);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo, mem_ctx);
+       if (gpo == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        gpo[result->count] = NULL;
 
@@ -274,7 +288,10 @@ NTSTATUS gp_get_gpo_info(struct gp_context *gp_ctx, const char *dn_str, struct g
        dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, dn_str);
 
        attrs = talloc_array(mem_ctx, const char *, 7);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(attrs, mem_ctx);
+       if (attrs == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        attrs[0] = "nTSecurityDescriptor";
        attrs[1] = "versionNumber";
@@ -342,12 +359,18 @@ static NTSTATUS parse_gplink (TALLOC_CTX *mem_ctx, const char *gplink_str, struc
                        gplinks[idx]->dn = talloc_strndup(mem_ctx,
                                                          gplink_str + start,
                                                          pos - start);
-                       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gplinks[idx]->dn, gplinks);
+                       if (gplinks[idx]->dn == NULL) {
+                               TALLOC_FREE(gplinks);
+                               return NT_STATUS_NO_MEMORY;
+                       }
 
                        for (start = pos + 1; gplink_str[pos] != ']'; pos++);
 
                        buf = talloc_strndup(gplinks, gplink_str + start, pos - start);
-                       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(buf, gplinks);
+                       if (buf == NULL) {
+                               TALLOC_FREE(gplinks);
+                               return NT_STATUS_NO_MEMORY;
+                       }
                        gplinks[idx]->options = (uint32_t) strtoll(buf, &end, 0);
                        talloc_free(buf);
 
@@ -398,13 +421,19 @@ NTSTATUS gp_get_gplinks(struct gp_context *gp_ctx, const char *dn_str, struct gp
                        if (strcmp(element->name, "gPLink") == 0) {
                                SMB_ASSERT(element->num_values > 0);
                                gplink_str = talloc_strdup(mem_ctx, (char *) element->values[0].data);
-                               NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gplink_str, mem_ctx);
+                               if (gplink_str == NULL) {
+                                       TALLOC_FREE(mem_ctx);
+                                       return NT_STATUS_NO_MEMORY;
+                               }
                                goto found;
                        }
                }
        }
        gplink_str = talloc_strdup(mem_ctx, "");
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gplink_str, mem_ctx);
+       if (gplink_str == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        found:
 
@@ -425,7 +454,7 @@ NTSTATUS gp_list_gpos(struct gp_context *gp_ctx, struct security_token *token, c
        TALLOC_CTX *mem_ctx;
        const char **gpos;
        struct ldb_result *result;
-       const char *sid;
+       char *sid;
        struct ldb_dn *dn;
        struct ldb_message_element *element;
        bool inherit;
@@ -443,7 +472,9 @@ NTSTATUS gp_list_gpos(struct gp_context *gp_ctx, struct security_token *token, c
        mem_ctx = talloc_new(gp_ctx);
        NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
 
-       sid = dom_sid_string(mem_ctx, token->user_sid);
+       sid = ldap_encode_ndr_dom_sid(mem_ctx,
+                                     &token->sids[PRIMARY_USER_SID_INDEX]);
+       NT_STATUS_HAVE_NO_MEMORY(sid);
 
        /* Find the user DN and objectclass via the sid from the security token */
        rv = ldb_search(gp_ctx->ldb_ctx,
@@ -478,7 +509,10 @@ NTSTATUS gp_list_gpos(struct gp_context *gp_ctx, struct security_token *token, c
        }
 
        gpos = talloc_array(gp_ctx, const char *, 1);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpos, mem_ctx);
+       if (gpos == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
        gpos[0] = NULL;
 
        /* Walk through the containers until we hit the root */
@@ -538,7 +572,7 @@ NTSTATUS gp_list_gpos(struct gp_context *gp_ctx, struct security_token *token, c
 
                        /* If the account does not have read access, this GPO does not apply
                         * to this account */
-                       status = sec_access_check(gpo->security_descriptor,
+                       status = se_access_check(gpo->security_descriptor,
                                        token,
                                        (SEC_STD_READ_CONTROL | SEC_ADS_LIST | SEC_ADS_READ_PROP),
                                        &access_granted);
@@ -558,9 +592,15 @@ NTSTATUS gp_list_gpos(struct gp_context *gp_ctx, struct security_token *token, c
 
                        /* Add the GPO to the list */
                        gpos = talloc_realloc(gp_ctx, gpos, const char *, count+2);
-                       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpos, mem_ctx);
+                       if (gpos == NULL) {
+                               TALLOC_FREE(mem_ctx);
+                               return NT_STATUS_NO_MEMORY;
+                       }
                        gpos[count] = talloc_strdup(gp_ctx, gplinks[i]->dn);
-                       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpos[count], mem_ctx);
+                       if (gpos[count] == NULL) {
+                               TALLOC_FREE(mem_ctx);
+                               return NT_STATUS_NO_MEMORY;
+                       }
                        gpos[count+1] = NULL;
                        count++;
 
@@ -623,23 +663,32 @@ NTSTATUS gp_set_gplink(struct gp_context *gp_ctx, const char *dn_str, struct gp_
                        start++;
                }
                gplink_str = talloc_asprintf(mem_ctx, "%s;%d%s", gplink_str, gplink->options, start);
-               NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gplink_str, mem_ctx);
+               if (gplink_str == NULL) {
+                       TALLOC_FREE(mem_ctx);
+                       return NT_STATUS_NO_MEMORY;
+               }
 
        } else {
                /* Prepend the new GPO link to the string. This list is backwards in priority. */
                gplink_str = talloc_asprintf(mem_ctx, "[LDAP://%s;%d]%s", gplink->dn, gplink->options, gplink_str);
-               NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gplink_str, mem_ctx);
+               if (gplink_str == NULL) {
+                       TALLOC_FREE(mem_ctx);
+                       return NT_STATUS_NO_MEMORY;
+               }
        }
 
 
 
        msg = ldb_msg_new(mem_ctx);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg, mem_ctx);
+       if (msg == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        msg->dn = dn;
 
        rv = ldb_msg_add_string(msg, "gPLink", gplink_str);
-       if (rv != 0) {
+       if (rv != LDB_SUCCESS) {
                DEBUG(0, ("LDB message add string failed: %s\n", ldb_strerror(rv)));
                talloc_free(mem_ctx);
                return NT_STATUS_UNSUCCESSFUL;
@@ -647,7 +696,7 @@ NTSTATUS gp_set_gplink(struct gp_context *gp_ctx, const char *dn_str, struct gp_
        msg->elements[0].flags = LDB_FLAG_MOD_REPLACE;
 
        rv = ldb_modify(gp_ctx->ldb_ctx, msg);
-       if (rv != 0) {
+       if (rv != LDB_SUCCESS) {
                DEBUG(0, ("LDB modify failed: %s\n", ldb_strerror(rv)));
                talloc_free(mem_ctx);
                return NT_STATUS_UNSUCCESSFUL;
@@ -690,7 +739,10 @@ NTSTATUS gp_del_gplink(struct gp_context *gp_ctx, const char *dn_str, const char
 
        /* If this GPO link already exists, alter the options, else add it */
        search_string = talloc_asprintf(mem_ctx, "[LDAP://%s]", gplink_dn);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(search_string, mem_ctx);
+       if (search_string == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        p = strcasestr(gplink_str, search_string);
        if (p == NULL) {
@@ -705,24 +757,30 @@ NTSTATUS gp_del_gplink(struct gp_context *gp_ctx, const char *dn_str, const char
        }
        p++;
        gplink_str = talloc_asprintf(mem_ctx, "%s%s", gplink_str, p);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gplink_str, mem_ctx);
+       if (gplink_str == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
 
        msg = ldb_msg_new(mem_ctx);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg, mem_ctx);
+       if (msg == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        msg->dn = dn;
 
        if (strcmp(gplink_str, "") == 0) {
                rv = ldb_msg_add_empty(msg, "gPLink", LDB_FLAG_MOD_DELETE, NULL);
-               if (rv != 0) {
+               if (rv != LDB_SUCCESS) {
                        DEBUG(0, ("LDB message add empty element failed: %s\n", ldb_strerror(rv)));
                        talloc_free(mem_ctx);
                        return NT_STATUS_UNSUCCESSFUL;
                }
        } else {
                rv = ldb_msg_add_string(msg, "gPLink", gplink_str);
-               if (rv != 0) {
+               if (rv != LDB_SUCCESS) {
                        DEBUG(0, ("LDB message add string failed: %s\n", ldb_strerror(rv)));
                        talloc_free(mem_ctx);
                        return NT_STATUS_UNSUCCESSFUL;
@@ -730,7 +788,7 @@ NTSTATUS gp_del_gplink(struct gp_context *gp_ctx, const char *dn_str, const char
                msg->elements[0].flags = LDB_FLAG_MOD_REPLACE;
        }
        rv = ldb_modify(gp_ctx->ldb_ctx, msg);
-       if (rv != 0) {
+       if (rv != LDB_SUCCESS) {
                DEBUG(0, ("LDB modify failed: %s\n", ldb_strerror(rv)));
                talloc_free(mem_ctx);
                return NT_STATUS_UNSUCCESSFUL;
@@ -784,10 +842,13 @@ NTSTATUS gp_set_inheritance(struct gp_context *gp_ctx, const char *dn_str, enum
        msg->dn = ldb_dn_new(msg, gp_ctx->ldb_ctx, dn_str);
 
        inheritance_string = talloc_asprintf(msg, "%d", inheritance);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(inheritance_string, msg);
+       if (inheritance_string == NULL) {
+               TALLOC_FREE(msg);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        rv = ldb_msg_add_string(msg, "gPOptions", inheritance_string);
-       if (rv != 0) {
+       if (rv != LDB_SUCCESS) {
                DEBUG(0, ("LDB message add string failed: %s\n", ldb_strerror(rv)));
                talloc_free(msg);
                return NT_STATUS_UNSUCCESSFUL;
@@ -795,7 +856,7 @@ NTSTATUS gp_set_inheritance(struct gp_context *gp_ctx, const char *dn_str, enum
        msg->elements[0].flags = LDB_FLAG_MOD_REPLACE;
 
        rv = ldb_modify(gp_ctx->ldb_ctx, msg);
-       if (rv != 0) {
+       if (rv != LDB_SUCCESS) {
                DEBUG(0, ("LDB modify failed: %s\n", ldb_strerror(rv)));
                talloc_free(msg);
                return NT_STATUS_UNSUCCESSFUL;
@@ -818,21 +879,33 @@ NTSTATUS gp_create_ldap_gpo(struct gp_context *gp_ctx, struct gp_object *gpo)
 
        /* CN={GUID} */
        msg = ldb_msg_new(mem_ctx);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg, mem_ctx);
+       if (msg == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        msg->dn = ldb_get_default_basedn(gp_ctx->ldb_ctx);
        dn_str = talloc_asprintf(mem_ctx, "CN=%s,CN=Policies,CN=System", gpo->name);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(dn_str, mem_ctx);
+       if (dn_str == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        child_dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, dn_str);
        rv = ldb_dn_add_child(msg->dn, child_dn);
        if (!rv) goto ldb_msg_add_error;
 
        flags_str = talloc_asprintf(mem_ctx, "%d", gpo->flags);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(flags_str, mem_ctx);
+       if (flags_str == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        version_str = talloc_asprintf(mem_ctx, "%d", gpo->version);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(version_str, mem_ctx);
+       if (version_str == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        rv = ldb_msg_add_string(msg, "objectClass", "top");
        if (rv != LDB_SUCCESS) goto ldb_msg_add_error;
@@ -868,7 +941,10 @@ NTSTATUS gp_create_ldap_gpo(struct gp_context *gp_ctx, struct gp_object *gpo)
 
        /* CN=User */
        msg = ldb_msg_new(mem_ctx);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg, mem_ctx);
+       if (msg == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        msg->dn = ldb_dn_copy(mem_ctx, gpo_dn);
        child_dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, "CN=User");
@@ -895,7 +971,10 @@ NTSTATUS gp_create_ldap_gpo(struct gp_context *gp_ctx, struct gp_object *gpo)
 
        /* CN=Machine */
        msg = ldb_msg_new(mem_ctx);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg, mem_ctx);
+       if (msg == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        msg->dn = ldb_dn_copy(mem_ctx, gpo_dn);
        child_dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, "CN=Machine");
@@ -921,7 +1000,10 @@ NTSTATUS gp_create_ldap_gpo(struct gp_context *gp_ctx, struct gp_object *gpo)
        }
 
        gpo->dn = talloc_strdup(gpo, ldb_dn_get_linearized(gpo_dn));
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->dn, mem_ctx);
+       if (gpo->dn == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        talloc_free(mem_ctx);
        return NT_STATUS_OK;
@@ -956,12 +1038,15 @@ NTSTATUS gp_set_ads_acl (struct gp_context *gp_ctx, const char *dn_str, const st
 
        /* Create a LDB message */
        msg = ldb_msg_new(mem_ctx);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg, mem_ctx);
+       if (msg == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        msg->dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, dn_str);
 
        rv = ldb_msg_add_value(msg, "nTSecurityDescriptor", &data, NULL);
-       if (rv != 0) {
+       if (rv != LDB_SUCCESS) {
                DEBUG(0, ("LDB message add element failed for adding nTSecurityDescriptor: %s\n", ldb_strerror(rv)));
                talloc_free(mem_ctx);
                return NT_STATUS_UNSUCCESSFUL;
@@ -969,7 +1054,7 @@ NTSTATUS gp_set_ads_acl (struct gp_context *gp_ctx, const char *dn_str, const st
        msg->elements[0].flags = LDB_FLAG_MOD_REPLACE;
 
        rv = ldb_modify(gp_ctx->ldb_ctx, msg);
-       if (rv != 0) {
+       if (rv != LDB_SUCCESS) {
                DEBUG(0, ("LDB modify failed: %s\n", ldb_strerror(rv)));
                talloc_free(mem_ctx);
                return NT_STATUS_UNSUCCESSFUL;
@@ -990,18 +1075,27 @@ NTSTATUS gp_set_ldap_gpo(struct gp_context *gp_ctx, struct gp_object *gpo)
        mem_ctx = talloc_new(gp_ctx);
 
        msg = ldb_msg_new(mem_ctx);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg, mem_ctx);
+       if (msg == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        msg->dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, gpo->dn);
 
        version_str = talloc_asprintf(mem_ctx, "%d", gpo->version);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg, mem_ctx);
+       if (msg == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        flags_str = talloc_asprintf(mem_ctx, "%d", gpo->flags);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg, mem_ctx);
+       if (msg == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
 
        rv = ldb_msg_add_string(msg, "flags", flags_str);
-       if (rv != 0) {
+       if (rv != LDB_SUCCESS) {
                DEBUG(0, ("LDB message add string failed for flags: %s\n", ldb_strerror(rv)));
                talloc_free(mem_ctx);
                return NT_STATUS_UNSUCCESSFUL;
@@ -1009,7 +1103,7 @@ NTSTATUS gp_set_ldap_gpo(struct gp_context *gp_ctx, struct gp_object *gpo)
        msg->elements[0].flags = LDB_FLAG_MOD_REPLACE;
 
        rv = ldb_msg_add_string(msg, "version", version_str);
-       if (rv != 0) {
+       if (rv != LDB_SUCCESS) {
                DEBUG(0, ("LDB message add string failed for version: %s\n", ldb_strerror(rv)));
                talloc_free(mem_ctx);
                return NT_STATUS_UNSUCCESSFUL;
@@ -1017,7 +1111,7 @@ NTSTATUS gp_set_ldap_gpo(struct gp_context *gp_ctx, struct gp_object *gpo)
        msg->elements[1].flags = LDB_FLAG_MOD_REPLACE;
 
        rv = ldb_msg_add_string(msg, "displayName", gpo->display_name);
-       if (rv != 0) {
+       if (rv != LDB_SUCCESS) {
                DEBUG(0, ("LDB message add string failed for displayName: %s\n", ldb_strerror(rv)));
                talloc_free(mem_ctx);
                return NT_STATUS_UNSUCCESSFUL;
@@ -1025,7 +1119,7 @@ NTSTATUS gp_set_ldap_gpo(struct gp_context *gp_ctx, struct gp_object *gpo)
        msg->elements[2].flags = LDB_FLAG_MOD_REPLACE;
 
        rv = ldb_modify(gp_ctx->ldb_ctx, msg);
-       if (rv != 0) {
+       if (rv != LDB_SUCCESS) {
                DEBUG(0, ("LDB modify failed: %s\n", ldb_strerror(rv)));
                talloc_free(mem_ctx);
                return NT_STATUS_UNSUCCESSFUL;