s4:security Change struct security_token->sids from struct dom_sid * to struct dom_sid
[sfrench/samba-autobuild/.git] / source4 / lib / policy / gp_ldap.c
index 730c4d8e0b7a48c34deee173b56432e9597f70b3..87fde9dbd78f4d22c6c129f0308954d44bf5f93e 100644 (file)
@@ -20,7 +20,7 @@
 #include "includes.h"
 #include "param/param.h"
 #include "lib/ldb/include/ldb.h"
-#include "lib/ldb_wrap.h"
+#include "lib/ldb-samba/ldb_wrap.h"
 #include "auth/credentials/credentials.h"
 #include "../librpc/gen_ndr/nbt.h"
 #include "libcli/libcli.h"
@@ -58,29 +58,32 @@ static NTSTATUS parse_gpo(TALLOC_CTX *mem_ctx, struct ldb_message *msg, struct g
        enum ndr_err_code ndr_err;
        const DATA_BLOB *data;
 
-       gpo->dn = talloc_steal(mem_ctx, ldb_dn_get_linearized(msg->dn));
+       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);
 
        DEBUG(9, ("Parsing GPO LDAP data for %s\n", gpo->dn));
 
-       gpo->display_name = ldb_msg_find_attr_as_string(msg, "displayName", "");
-       gpo->name = ldb_msg_find_attr_as_string(msg, "name", "");
-       gpo->flags = ldb_msg_find_attr_as_uint(msg, "name", 0);
-       gpo->version = ldb_msg_find_attr_as_uint(msg, "version", 0);
-       gpo->file_sys_path = ldb_msg_find_attr_as_string(msg, "gPCFileSysPath", "");
+       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);
+
+       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->display_name[0] != '\0')
-               gpo->display_name = talloc_steal(mem_ctx, gpo->display_name);
-       if (gpo->name[0] != '\0')
-               gpo->name = talloc_steal(mem_ctx, gpo->name);
-       if (gpo->file_sys_path[0] != '\0')
-               gpo->file_sys_path = talloc_steal(mem_ctx, gpo->file_sys_path);
+       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);
 
        /* Pull the security descriptor through the NDR library */
        data = ldb_msg_find_ldb_val(msg, "nTSecurityDescriptor");
-       gpo->security_descriptor = talloc(mem_ctx, struct security_descriptor);
+       gpo->security_descriptor = talloc(gpo, struct security_descriptor);
+       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->security_descriptor, gpo);
+
        ndr_err = ndr_pull_struct_blob(data,
                        mem_ctx,
-                       NULL,
                        gpo->security_descriptor,
                        (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
@@ -96,11 +99,14 @@ NTSTATUS gp_get_gpo_flags(TALLOC_CTX *mem_ctx, uint32_t flags, const char ***ret
        unsigned int i, count=0;
        const char **flag_strs = talloc_array(mem_ctx, const char *, 1);
 
+       NT_STATUS_HAVE_NO_MEMORY(flag_strs);
+
        flag_strs[0] = NULL;
 
        for (i = 0; gpo_flags[i].str != NULL; i++) {
                if (flags & gpo_flags[i].flags) {
                        flag_strs = talloc_realloc(mem_ctx, flag_strs, const char *, count+2);
+                       NT_STATUS_HAVE_NO_MEMORY(flag_strs);
                        flag_strs[count] = gpo_flags[i].str;
                        flag_strs[count+1] = NULL;
                        count++;
@@ -115,11 +121,13 @@ NTSTATUS gp_get_gplink_options(TALLOC_CTX *mem_ctx, uint32_t options, const char
        unsigned int i, count=0;
        const char **flag_strs = talloc_array(mem_ctx, const char *, 1);
 
+       NT_STATUS_HAVE_NO_MEMORY(flag_strs);
        flag_strs[0] = NULL;
 
        for (i = 0; gplink_options[i].str != NULL; i++) {
                if (options & gplink_options[i].flags) {
                        flag_strs = talloc_realloc(mem_ctx, flag_strs, const char *, count+2);
+                       NT_STATUS_HAVE_NO_MEMORY(flag_strs);
                        flag_strs[count] = gplink_options[i].str;
                        flag_strs[count+1] = NULL;
                        count++;
@@ -148,8 +156,9 @@ NTSTATUS gp_init(TALLOC_CTX *mem_ctx,
 
        /* Prepare libnet lookup structure for looking a DC (PDC is correct). */
        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);
@@ -160,6 +169,7 @@ NTSTATUS gp_init(TALLOC_CTX *mem_ctx,
 
        /* Connect to ldap://DC_NAME with all relevant contexts*/
        url = talloc_asprintf(mem_ctx, "ldap://%s", io->out.dcs[0].name);
+       NT_STATUS_HAVE_NO_MEMORY(url);
        ldb_ctx = ldb_wrap_connect(mem_ctx, net_ctx->event_ctx, lp_ctx,
                        url, NULL, net_ctx->cred, 0);
        if (ldb_ctx == NULL) {
@@ -169,6 +179,8 @@ NTSTATUS gp_init(TALLOC_CTX *mem_ctx,
 
 
        *gp_ctx = talloc_zero(mem_ctx, struct gp_context);
+       NT_STATUS_HAVE_NO_MEMORY(gp_ctx);
+
        (*gp_ctx)->lp_ctx = lp_ctx;
        (*gp_ctx)->credentials = credentials;
        (*gp_ctx)->ev_ctx = ev_ctx;
@@ -193,6 +205,7 @@ NTSTATUS gp_list_all_gpos(struct gp_context *gp_ctx, struct gp_object ***ret)
 
        /* Create a forked memory context, as a base for everything here */
        mem_ctx = talloc_new(gp_ctx);
+       NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
 
        /* Create full ldb dn of the policies base object */
        dn = ldb_get_default_basedn(gp_ctx->ldb_ctx);
@@ -206,6 +219,8 @@ 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);
+
        attrs[0] = "nTSecurityDescriptor";
        attrs[1] = "versionNumber";
        attrs[2] = "flags";
@@ -216,12 +231,14 @@ NTSTATUS gp_list_all_gpos(struct gp_context *gp_ctx, struct gp_object ***ret)
 
        rv = ldb_search(gp_ctx->ldb_ctx, mem_ctx, &result, dn, LDB_SCOPE_ONELEVEL, attrs, "(objectClass=groupPolicyContainer)");
        if (rv != LDB_SUCCESS) {
-               DEBUG(0, ("LDB search failed: %s\n%s\n", ldb_strerror(rv),ldb_errstring(gp_ctx->ldb_ctx)));
+               DEBUG(0, ("LDB search failed: %s\n%s\n", ldb_strerror(rv), ldb_errstring(gp_ctx->ldb_ctx)));
                talloc_free(mem_ctx);
                return NT_STATUS_UNSUCCESSFUL;
        }
 
        gpo = talloc_array(gp_ctx, struct gp_object *, result->count+1);
+       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo, mem_ctx);
+
        gpo[result->count] = NULL;
 
        for (i = 0; i < result->count; i++) {
@@ -251,11 +268,14 @@ NTSTATUS gp_get_gpo_info(struct gp_context *gp_ctx, const char *dn_str, struct g
 
        /* Create a forked memory context, as a base for everything here */
        mem_ctx = talloc_new(gp_ctx);
+       NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
 
        /* Create an ldb dn struct for the dn string */
        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);
+
        attrs[0] = "nTSecurityDescriptor";
        attrs[1] = "versionNumber";
        attrs[2] = "flags";
@@ -272,7 +292,7 @@ NTSTATUS gp_get_gpo_info(struct gp_context *gp_ctx, const char *dn_str, struct g
                        attrs,
                        "objectClass=groupPolicyContainer");
        if (rv != LDB_SUCCESS) {
-               DEBUG(0, ("LDB search failed: %s\n%s\n", ldb_strerror(rv),ldb_errstring(gp_ctx->ldb_ctx)));
+               DEBUG(0, ("LDB search failed: %s\n%s\n", ldb_strerror(rv), ldb_errstring(gp_ctx->ldb_ctx)));
                talloc_free(mem_ctx);
                return NT_STATUS_UNSUCCESSFUL;
        }
@@ -303,24 +323,31 @@ static NTSTATUS parse_gplink (TALLOC_CTX *mem_ctx, const char *gplink_str, struc
        int pos;
        struct gp_link **gplinks;
        char *buf, *end;
+       const char *gplink_start = "[LDAP://";
 
        gplinks = talloc_array(mem_ctx, struct gp_link *, 1);
+       NT_STATUS_HAVE_NO_MEMORY(gplinks);
+
        gplinks[0] = NULL;
 
        /* Assuming every gPLink starts with "[LDAP://" */
-       start = 8;
+       start = strlen(gplink_start);
 
        for (pos = start; pos < strlen(gplink_str); pos++) {
                if (gplink_str[pos] == ';') {
                        gplinks = talloc_realloc(mem_ctx, gplinks, struct gp_link *, idx+2);
+                       NT_STATUS_HAVE_NO_MEMORY(gplinks);
                        gplinks[idx] = talloc(mem_ctx, struct gp_link);
+                       NT_STATUS_HAVE_NO_MEMORY(gplinks[idx]);
                        gplinks[idx]->dn = talloc_strndup(mem_ctx,
                                                          gplink_str + start,
                                                          pos - start);
+                       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gplinks[idx]->dn, gplinks);
 
                        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);
                        gplinks[idx]->options = (uint32_t) strtoll(buf, &end, 0);
                        talloc_free(buf);
 
@@ -330,7 +357,7 @@ static NTSTATUS parse_gplink (TALLOC_CTX *mem_ctx, const char *gplink_str, struc
                        /* Increment the array index, the string position past
                           the next "[LDAP://", and set the start reference */
                        idx++;
-                       pos += 9;
+                       pos += strlen(gplink_start)+1;
                        start = pos;
                }
        }
@@ -353,6 +380,7 @@ NTSTATUS gp_get_gplinks(struct gp_context *gp_ctx, const char *dn_str, struct gp
 
        /* Create a forked memory context, as a base for everything here */
        mem_ctx = talloc_new(gp_ctx);
+       NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
 
        dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, dn_str);
 
@@ -370,11 +398,13 @@ 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);
                                goto found;
                        }
                }
        }
        gplink_str = talloc_strdup(mem_ctx, "");
+       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gplink_str, mem_ctx);
 
        found:
 
@@ -398,7 +428,7 @@ NTSTATUS gp_list_gpos(struct gp_context *gp_ctx, struct security_token *token, c
        const char *sid;
        struct ldb_dn *dn;
        struct ldb_message_element *element;
-       int inherit;
+       bool inherit;
        const char *attrs[] = { "objectClass", NULL };
        int rv;
        NTSTATUS status;
@@ -411,8 +441,9 @@ NTSTATUS gp_list_gpos(struct gp_context *gp_ctx, struct security_token *token, c
 
        /* Create a forked memory context, as a base for everything here */
        mem_ctx = talloc_new(gp_ctx);
+       NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
 
-       sid = dom_sid_string(mem_ctx, token->user_sid);
+       sid = dom_sid_string(mem_ctx, &token->sids[PRIMARY_USER_SID_INDEX]);
 
        /* Find the user DN and objectclass via the sid from the security token */
        rv = ldb_search(gp_ctx->ldb_ctx,
@@ -447,6 +478,7 @@ 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);
        gpos[0] = NULL;
 
        /* Walk through the containers until we hit the root */
@@ -526,7 +558,9 @@ 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);
                        gpos[count] = talloc_strdup(gp_ctx, gplinks[i]->dn);
+                       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpos[count], mem_ctx);
                        gpos[count+1] = NULL;
                        count++;
 
@@ -562,6 +596,7 @@ NTSTATUS gp_set_gplink(struct gp_context *gp_ctx, const char *dn_str, struct gp_
 
        /* Create a forked memory context, as a base for everything here */
        mem_ctx = talloc_new(gp_ctx);
+       NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
 
        dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, dn_str);
 
@@ -588,15 +623,19 @@ 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);
 
        } 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);
        }
 
 
 
        msg = ldb_msg_new(mem_ctx);
+       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg, mem_ctx);
+
        msg->dn = dn;
 
        rv = ldb_msg_add_string(msg, "gPLink", gplink_str);
@@ -625,12 +664,13 @@ NTSTATUS gp_del_gplink(struct gp_context *gp_ctx, const char *dn_str, const char
        struct ldb_dn *dn;
        struct ldb_message *msg;
        const char *attrs[] = { "gPLink", NULL };
-       const char *gplink_str;
+       const char *gplink_str, *search_string;
        int rv;
        char *p;
 
        /* Create a forked memory context, as a base for everything here */
        mem_ctx = talloc_new(gp_ctx);
+       NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
 
        dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, dn_str);
 
@@ -649,7 +689,10 @@ NTSTATUS gp_del_gplink(struct gp_context *gp_ctx, const char *dn_str, const char
        gplink_str = ldb_msg_find_attr_as_string(result->msgs[0], "gPLink", "");
 
        /* If this GPO link already exists, alter the options, else add it */
-       p = strcasestr(gplink_str, talloc_asprintf(mem_ctx, "[LDAP://%s", gplink_dn));
+       search_string = talloc_asprintf(mem_ctx, "[LDAP://%s]", gplink_dn);
+       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(search_string, mem_ctx);
+
+       p = strcasestr(gplink_str, search_string);
        if (p == NULL) {
                talloc_free(mem_ctx);
                return NT_STATUS_NOT_FOUND;
@@ -662,9 +705,12 @@ 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);
 
 
        msg = ldb_msg_new(mem_ctx);
+       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg, mem_ctx);
+
        msg->dn = dn;
 
        if (strcmp(gplink_str, "") == 0) {
@@ -704,6 +750,7 @@ NTSTATUS gp_get_inheritance(struct gp_context *gp_ctx, const char *dn_str, enum
 
        /* Create a forked memory context, as a base for everything here */
        mem_ctx = talloc_new(gp_ctx);
+       NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
 
        dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, dn_str);
 
@@ -732,9 +779,12 @@ NTSTATUS gp_set_inheritance(struct gp_context *gp_ctx, const char *dn_str, enum
        int rv;
 
        msg = ldb_msg_new(gp_ctx);
+       NT_STATUS_HAVE_NO_MEMORY(msg);
+
        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);
 
        rv = ldb_msg_add_string(msg, "gPOptions", inheritance_string);
        if (rv != 0) {
@@ -760,20 +810,30 @@ NTSTATUS gp_create_ldap_gpo(struct gp_context *gp_ctx, struct gp_object *gpo)
        struct ldb_message *msg;
        TALLOC_CTX *mem_ctx;
        int rv;
-       char *dn_str;
+       char *dn_str, *flags_str, *version_str;
        struct ldb_dn *child_dn, *gpo_dn;
 
        mem_ctx = talloc_new(gp_ctx);
+       NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
 
        /* CN={GUID} */
        msg = ldb_msg_new(mem_ctx);
+       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg, mem_ctx);
 
        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);
+
        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);
+
+       version_str = talloc_asprintf(mem_ctx, "%d", gpo->version);
+       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(version_str, mem_ctx);
+
        rv = ldb_msg_add_string(msg, "objectClass", "top");
        if (rv != LDB_SUCCESS) goto ldb_msg_add_error;
        rv = ldb_msg_add_string(msg, "objectClass", "container");
@@ -788,11 +848,9 @@ NTSTATUS gp_create_ldap_gpo(struct gp_context *gp_ctx, struct gp_object *gpo)
        if (rv != LDB_SUCCESS) goto ldb_msg_add_error;
        rv = ldb_msg_add_string(msg, "gPCFileSysPath", gpo->file_sys_path);
        if (rv != LDB_SUCCESS) goto ldb_msg_add_error;
-       rv = ldb_msg_add_string(msg, "flags",
-                       talloc_asprintf(mem_ctx, "%d", gpo->flags));
+       rv = ldb_msg_add_string(msg, "flags", flags_str);
        if (rv != LDB_SUCCESS) goto ldb_msg_add_error;
-       rv = ldb_msg_add_string(msg, "versionNumber",
-                       talloc_asprintf(mem_ctx, "%d", gpo->version));
+       rv = ldb_msg_add_string(msg, "versionNumber", version_str);
        if (rv != LDB_SUCCESS) goto ldb_msg_add_error;
        rv = ldb_msg_add_string(msg, "showInAdvancedViewOnly", "TRUE");
        if (rv != LDB_SUCCESS) goto ldb_msg_add_error;
@@ -810,6 +868,8 @@ 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);
+
        msg->dn = ldb_dn_copy(mem_ctx, gpo_dn);
        child_dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, "CN=User");
        rv = ldb_dn_add_child(msg->dn, child_dn);
@@ -835,6 +895,8 @@ 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);
+
        msg->dn = ldb_dn_copy(mem_ctx, gpo_dn);
        child_dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, "CN=Machine");
        rv = ldb_dn_add_child(msg->dn, child_dn);
@@ -858,7 +920,8 @@ NTSTATUS gp_create_ldap_gpo(struct gp_context *gp_ctx, struct gp_object *gpo)
                return NT_STATUS_UNSUCCESSFUL;
        }
 
-       gpo->dn = talloc_steal(gpo, ldb_dn_get_linearized(gpo_dn));
+       gpo->dn = talloc_strdup(gpo, ldb_dn_get_linearized(gpo_dn));
+       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(gpo->dn, mem_ctx);
 
        talloc_free(mem_ctx);
        return NT_STATUS_OK;
@@ -879,11 +942,11 @@ NTSTATUS gp_set_ads_acl (struct gp_context *gp_ctx, const char *dn_str, const st
 
        /* Create a forked memory context to clean up easily */
        mem_ctx = talloc_new(gp_ctx);
+       NT_STATUS_HAVE_NO_MEMORY(mem_ctx);
 
        /* Push the security descriptor through the NDR library */
        ndr_err = ndr_push_struct_blob(&data,
                        mem_ctx,
-                       lp_iconv_convenience(gp_ctx->lp_ctx),
                        sd,
                        (ndr_push_flags_fn_t)ndr_push_security_descriptor);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
@@ -893,6 +956,8 @@ 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);
+
        msg->dn = ldb_dn_new(mem_ctx, gp_ctx->ldb_ctx, dn_str);
 
        rv = ldb_msg_add_value(msg, "nTSecurityDescriptor", &data, NULL);
@@ -913,3 +978,59 @@ NTSTATUS gp_set_ads_acl (struct gp_context *gp_ctx, const char *dn_str, const st
        talloc_free(mem_ctx);
        return NT_STATUS_OK;
 }
+
+/* This function sets flags, version and displayName on a GPO */
+NTSTATUS gp_set_ldap_gpo(struct gp_context *gp_ctx, struct gp_object *gpo)
+{
+       int rv;
+       TALLOC_CTX *mem_ctx;
+       struct ldb_message *msg;
+       char *version_str, *flags_str;
+
+       mem_ctx = talloc_new(gp_ctx);
+
+       msg = ldb_msg_new(mem_ctx);
+       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg, mem_ctx);
+
+       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);
+
+       flags_str = talloc_asprintf(mem_ctx, "%d", gpo->flags);
+       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(msg, mem_ctx);
+
+       rv = ldb_msg_add_string(msg, "flags", flags_str);
+       if (rv != 0) {
+               DEBUG(0, ("LDB message add string failed for flags: %s\n", ldb_strerror(rv)));
+               talloc_free(mem_ctx);
+               return NT_STATUS_UNSUCCESSFUL;
+       }
+       msg->elements[0].flags = LDB_FLAG_MOD_REPLACE;
+
+       rv = ldb_msg_add_string(msg, "version", version_str);
+       if (rv != 0) {
+               DEBUG(0, ("LDB message add string failed for version: %s\n", ldb_strerror(rv)));
+               talloc_free(mem_ctx);
+               return NT_STATUS_UNSUCCESSFUL;
+       }
+       msg->elements[1].flags = LDB_FLAG_MOD_REPLACE;
+
+       rv = ldb_msg_add_string(msg, "displayName", gpo->display_name);
+       if (rv != 0) {
+               DEBUG(0, ("LDB message add string failed for displayName: %s\n", ldb_strerror(rv)));
+               talloc_free(mem_ctx);
+               return NT_STATUS_UNSUCCESSFUL;
+       }
+       msg->elements[2].flags = LDB_FLAG_MOD_REPLACE;
+
+       rv = ldb_modify(gp_ctx->ldb_ctx, msg);
+       if (rv != 0) {
+               DEBUG(0, ("LDB modify failed: %s\n", ldb_strerror(rv)));
+               talloc_free(mem_ctx);
+               return NT_STATUS_UNSUCCESSFUL;
+       }
+
+       talloc_free(mem_ctx);
+       return NT_STATUS_OK;
+}