s3: Support "country_code" in pdb_ads
[kai/samba.git] / source3 / passdb / pdb_ads.c
index f6cf06f1b26bc56829a72e3584be09c12854ac67..d45706a0e73ff35f0749b525b7105eaa6b9b5cc2 100644 (file)
 */
 
 #include "includes.h"
+#include "tldap.h"
+#include "tldap_util.h"
+#include "../libds/common/flags.h"
+#include "secrets.h"
+#include "../librpc/gen_ndr/samr.h"
+#include "../libcli/ldap/ldap_ndr.h"
+#include "../libcli/security/security.h"
 
 struct pdb_ads_state {
        struct sockaddr_un socket_address;
        struct tldap_context *ld;
        struct dom_sid domainsid;
+       struct GUID domainguid;
        char *domaindn;
        char *configdn;
        char *netbiosname;
 };
 
-static NTSTATUS pdb_ads_getsampwsid(struct pdb_methods *m,
-                                   struct samu *sam_acct,
-                                   const DOM_SID *sid);
+struct pdb_ads_samu_private {
+       char *dn;
+       struct tldap_message *ldapmsg;
+};
+
 static bool pdb_ads_gid_to_sid(struct pdb_methods *m, gid_t gid,
-                              DOM_SID *sid);
+                              struct dom_sid *sid);
 static bool pdb_ads_dnblob2sid(struct pdb_ads_state *state, DATA_BLOB *dnblob,
                               struct dom_sid *psid);
 static NTSTATUS pdb_ads_sid2dn(struct pdb_ads_state *state,
@@ -44,6 +54,10 @@ static int pdb_ads_search_fmt(struct pdb_ads_state *state, const char *base,
                              int attrsonly,
                              TALLOC_CTX *mem_ctx, struct tldap_message ***res,
                              const char *fmt, ...);
+static NTSTATUS pdb_ads_getsamupriv(struct pdb_ads_state *state,
+                                   const char *filter,
+                                   TALLOC_CTX *mem_ctx,
+                                   struct pdb_ads_samu_private **presult);
 
 static bool pdb_ads_pull_time(struct tldap_message *msg, const char *attr,
                              time_t *ptime)
@@ -64,42 +78,73 @@ static gid_t pdb_ads_sid2gid(const struct dom_sid *sid)
        return rid;
 }
 
-struct pdb_ads_samu_private {
-       char *dn;
-       struct tldap_message *ldapmsg;
-};
+static char *pdb_ads_domaindn2dns(TALLOC_CTX *mem_ctx, char *dn)
+{
+       char *result, *p;
 
-static struct samu *pdb_ads_init_guest(TALLOC_CTX *mem_ctx,
-                                      struct pdb_methods *m)
+       result = talloc_string_sub2(mem_ctx, dn, "DC=", "", false, false,
+                                   true);
+       if (result == NULL) {
+               return NULL;
+       }
+
+       while ((p = strchr_m(result, ',')) != NULL) {
+               *p = '.';
+       }
+
+       return result;
+}
+
+static struct pdb_domain_info *pdb_ads_get_domain_info(
+       struct pdb_methods *m, TALLOC_CTX *mem_ctx)
 {
        struct pdb_ads_state *state = talloc_get_type_abort(
                m->private_data, struct pdb_ads_state);
-       struct dom_sid guest_sid;
-       struct samu *guest;
-       NTSTATUS status;
-
-       sid_compose(&guest_sid, &state->domainsid, DOMAIN_USER_RID_GUEST);
+       struct pdb_domain_info *info;
+       struct tldap_message *rootdse;
+       char *tmp;
 
-       guest = samu_new(mem_ctx);
-       if (guest == NULL) {
+       info = talloc(mem_ctx, struct pdb_domain_info);
+       if (info == NULL) {
                return NULL;
        }
+       info->name = talloc_strdup(info, state->netbiosname);
+       if (info->name == NULL) {
+               goto fail;
+       }
+       info->dns_domain = pdb_ads_domaindn2dns(info, state->domaindn);
+       if (info->dns_domain == NULL) {
+               goto fail;
+       }
 
-       status = pdb_ads_getsampwsid(m, guest, &guest_sid);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(10, ("Could not init guest account: %s\n",
-                          nt_errstr(status)));
-               TALLOC_FREE(guest);
-               return NULL;
+       rootdse = tldap_rootdse(state->ld);
+       tmp = tldap_talloc_single_attribute(rootdse, "rootDomainNamingContext",
+                                           talloc_tos());
+       if (tmp == NULL) {
+               goto fail;
        }
-       return guest;
+       info->dns_forest = pdb_ads_domaindn2dns(info, tmp);
+       TALLOC_FREE(tmp);
+       if (info->dns_forest == NULL) {
+               goto fail;
+       }
+       info->sid = state->domainsid;
+       info->guid = state->domainguid;
+       return info;
+
+fail:
+       TALLOC_FREE(info);
+       return NULL;
 }
 
 static struct pdb_ads_samu_private *pdb_ads_get_samu_private(
        struct pdb_methods *m, struct samu *sam)
 {
+       struct pdb_ads_state *state = talloc_get_type_abort(
+               m->private_data, struct pdb_ads_state);
        struct pdb_ads_samu_private *result;
-       uint32_t rid;
+       char *sidstr, *filter;
+       NTSTATUS status;
 
        result = (struct pdb_ads_samu_private *)
                pdb_get_backend_private_data(sam, m);
@@ -109,56 +154,43 @@ static struct pdb_ads_samu_private *pdb_ads_get_samu_private(
                        result, struct pdb_ads_samu_private);
        }
 
-       /*
-        * This is now a weirdness of the passdb API. For the guest user we
-        * are not asked first.
-        */
-       sid_peek_rid(pdb_get_user_sid(sam), &rid);
+       sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), pdb_get_user_sid(sam));
+       if (sidstr == NULL) {
+               return NULL;
+       }
 
-       if (rid == DOMAIN_USER_RID_GUEST) {
-               struct samu *guest = pdb_ads_init_guest(talloc_tos(), m);
+       filter = talloc_asprintf(
+               talloc_tos(), "(&(objectsid=%s)(objectclass=user))", sidstr);
+       TALLOC_FREE(sidstr);
+       if (filter == NULL) {
+               return NULL;
+       }
 
-               if (guest == NULL) {
-                       return NULL;
-               }
-               result = talloc_get_type_abort(
-                       pdb_get_backend_private_data(guest, m),
-                       struct pdb_ads_samu_private);
-               pdb_set_backend_private_data(
-                       sam, talloc_move(sam, &result), NULL, m, PDB_SET);
-               TALLOC_FREE(guest);
-               return talloc_get_type_abort(
-                       pdb_get_backend_private_data(sam, m),
-                       struct pdb_ads_samu_private);
+       status = pdb_ads_getsamupriv(state, filter, sam, &result);
+       TALLOC_FREE(filter);
+       if (!NT_STATUS_IS_OK(status)) {
+               return NULL;
        }
 
-       return NULL;
+       return result;
 }
 
-static NTSTATUS pdb_ads_init_sam_from_ads(struct pdb_methods *m,
-                                         struct samu *sam,
-                                         struct tldap_message *entry)
+static NTSTATUS pdb_ads_init_sam_from_priv(struct pdb_methods *m,
+                                          struct samu *sam,
+                                          struct pdb_ads_samu_private *priv)
 {
        struct pdb_ads_state *state = talloc_get_type_abort(
                m->private_data, struct pdb_ads_state);
        TALLOC_CTX *frame = talloc_stackframe();
-       struct pdb_ads_samu_private *priv;
        NTSTATUS status = NT_STATUS_INTERNAL_DB_CORRUPTION;
+       struct tldap_message *entry = priv->ldapmsg;
        char *str;
        time_t tmp_time;
        struct dom_sid sid;
        uint64_t n;
+       uint32_t i;
        DATA_BLOB blob;
 
-       priv = talloc(sam, struct pdb_ads_samu_private);
-       if (priv == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-       if (!tldap_entry_dn(entry, &priv->dn)) {
-               TALLOC_FREE(priv);
-               return NT_STATUS_INTERNAL_DB_CORRUPTION;
-       }
-
        str = tldap_talloc_single_attribute(entry, "samAccountName", sam);
        if (str == NULL) {
                DEBUG(10, ("no samAccountName\n"));
@@ -176,7 +208,7 @@ static NTSTATUS pdb_ads_init_sam_from_ads(struct pdb_methods *m,
                pdb_set_pass_last_set_time(sam, tmp_time, PDB_SET);
        }
        if (pdb_ads_pull_time(entry, "accountExpires", &tmp_time)) {
-               pdb_set_pass_last_set_time(sam, tmp_time, PDB_SET);
+               pdb_set_kickoff_time(sam, tmp_time, PDB_SET);
        }
 
        str = tldap_talloc_single_attribute(entry, "displayName",
@@ -213,6 +245,30 @@ static NTSTATUS pdb_ads_init_sam_from_ads(struct pdb_methods *m,
                pdb_set_profile_path(sam, str, PDB_SET);
        }
 
+       str = tldap_talloc_single_attribute(entry, "comment",
+                                           talloc_tos());
+       if (str != NULL) {
+               pdb_set_comment(sam, str, PDB_SET);
+       }
+
+       str = tldap_talloc_single_attribute(entry, "description",
+                                           talloc_tos());
+       if (str != NULL) {
+               pdb_set_acct_desc(sam, str, PDB_SET);
+       }
+
+       str = tldap_talloc_single_attribute(entry, "userWorkstations",
+                                           talloc_tos());
+       if (str != NULL) {
+               pdb_set_workstations(sam, str, PDB_SET);
+       }
+
+       str = tldap_talloc_single_attribute(entry, "userParameters",
+                                           talloc_tos());
+       if (str != NULL) {
+               pdb_set_munged_dial(sam, str, PDB_SET);
+       }
+
        if (!tldap_pull_binsid(entry, "objectSid", &sid)) {
                DEBUG(10, ("Could not pull SID\n"));
                goto fail;
@@ -223,7 +279,7 @@ static NTSTATUS pdb_ads_init_sam_from_ads(struct pdb_methods *m,
                DEBUG(10, ("Could not pull userAccountControl\n"));
                goto fail;
        }
-       pdb_set_acct_ctrl(sam, ads_uf2acb(n), PDB_SET);
+       pdb_set_acct_ctrl(sam, ds_uf2acb(n), PDB_SET);
 
        if (tldap_get_single_valueblob(entry, "unicodePwd", &blob)) {
                if (blob.length != NT_HASH_LEN) {
@@ -249,8 +305,9 @@ static NTSTATUS pdb_ads_init_sam_from_ads(struct pdb_methods *m,
 
        }
 
-       priv->ldapmsg = talloc_move(priv, &entry);
-       pdb_set_backend_private_data(sam, priv, NULL, m, PDB_SET);
+       if (tldap_pull_uint32(entry, "countryCode", &i)) {
+               pdb_set_country_code(sam, i, PDB_SET);
+       }
 
        status = NT_STATUS_OK;
 fail:
@@ -261,57 +318,100 @@ fail:
 static bool pdb_ads_init_ads_from_sam(struct pdb_ads_state *state,
                                      struct tldap_message *existing,
                                      TALLOC_CTX *mem_ctx,
-                                     int *pnum_mods, struct tldap_mod **pmods,
+                                     struct tldap_mod **pmods, int *pnum_mods,
                                      struct samu *sam)
 {
        bool ret = true;
        DATA_BLOB blob;
+       const char *pw;
 
        /* TODO: All fields :-) */
 
        ret &= tldap_make_mod_fmt(
-               existing, mem_ctx, pnum_mods, pmods, "displayName",
+               existing, mem_ctx, pmods, pnum_mods, "displayName",
                "%s", pdb_get_fullname(sam));
 
-       blob = data_blob_const(pdb_get_nt_passwd(sam), NT_HASH_LEN);
-       if (blob.data != NULL) {
-               ret &= tldap_add_mod_blobs(mem_ctx, pmods, TLDAP_MOD_REPLACE,
-                                          "unicodePwd", 1, &blob);
-       }
+       pw = pdb_get_plaintext_passwd(sam);
 
-       blob = data_blob_const(pdb_get_lanman_passwd(sam), NT_HASH_LEN);
-       if (blob.data != NULL) {
-               ret &= tldap_add_mod_blobs(mem_ctx, pmods, TLDAP_MOD_REPLACE,
-                                          "dBCSPwd", 1, &blob);
+       /*
+        * If we have the plain text pw, this is probably about to be
+        * set. Is this true always?
+        */
+       if (pw != NULL) {
+               char *pw_quote;
+               uint8_t *pw_utf16;
+               size_t pw_utf16_len;
+
+               pw_quote = talloc_asprintf(talloc_tos(), "\"%s\"", pw);
+               if (pw_quote == NULL) {
+                       ret = false;
+                       goto fail;
+               }
+
+               ret &= convert_string_talloc(talloc_tos(),
+                                            CH_UNIX, CH_UTF16LE,
+                                            pw_quote, strlen(pw_quote),
+                                            &pw_utf16, &pw_utf16_len, false);
+               if (!ret) {
+                       goto fail;
+               }
+               blob = data_blob_const(pw_utf16, pw_utf16_len);
+
+               ret &= tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods,
+                                          TLDAP_MOD_REPLACE,
+                                          "unicodePwd", &blob, 1);
+               TALLOC_FREE(pw_utf16);
+               TALLOC_FREE(pw_quote);
        }
 
        ret &= tldap_make_mod_fmt(
-               existing, mem_ctx, pnum_mods, pmods, "userAccountControl",
-               "%d", ads_acb2uf(pdb_get_acct_ctrl(sam)));
+               existing, mem_ctx, pmods, pnum_mods, "userAccountControl",
+               "%d", ds_acb2uf(pdb_get_acct_ctrl(sam)));
 
        ret &= tldap_make_mod_fmt(
-               existing, mem_ctx, pnum_mods, pmods, "homeDirectory",
+               existing, mem_ctx, pmods, pnum_mods, "homeDirectory",
                "%s", pdb_get_homedir(sam));
 
        ret &= tldap_make_mod_fmt(
-               existing, mem_ctx, pnum_mods, pmods, "homeDrive",
+               existing, mem_ctx, pmods, pnum_mods, "homeDrive",
                "%s", pdb_get_dir_drive(sam));
 
        ret &= tldap_make_mod_fmt(
-               existing, mem_ctx, pnum_mods, pmods, "scriptPath",
+               existing, mem_ctx, pmods, pnum_mods, "scriptPath",
                "%s", pdb_get_logon_script(sam));
 
        ret &= tldap_make_mod_fmt(
-               existing, mem_ctx, pnum_mods, pmods, "profilePath",
+               existing, mem_ctx, pmods, pnum_mods, "profilePath",
                "%s", pdb_get_profile_path(sam));
 
+       ret &= tldap_make_mod_fmt(
+               existing, mem_ctx, pmods, pnum_mods, "comment",
+               "%s", pdb_get_comment(sam));
+
+       ret &= tldap_make_mod_fmt(
+               existing, mem_ctx, pmods, pnum_mods, "description",
+               "%s", pdb_get_acct_desc(sam));
+
+       ret &= tldap_make_mod_fmt(
+               existing, mem_ctx, pmods, pnum_mods, "userWorkstations",
+               "%s", pdb_get_workstations(sam));
+
+       ret &= tldap_make_mod_fmt(
+               existing, mem_ctx, pmods, pnum_mods, "userParameters",
+               "%s", pdb_get_munged_dial(sam));
+
+       ret &= tldap_make_mod_fmt(
+               existing, mem_ctx, pmods, pnum_mods, "countryCode",
+               "%i", (int)pdb_get_country_code(sam));
+
+fail:
        return ret;
 }
 
-static NTSTATUS pdb_ads_getsampwfilter(struct pdb_methods *m,
-                                      struct pdb_ads_state *state,
-                                      struct samu *sam_acct,
-                                      const char *filter)
+static NTSTATUS pdb_ads_getsamupriv(struct pdb_ads_state *state,
+                                   const char *filter,
+                                   TALLOC_CTX *mem_ctx,
+                                   struct pdb_ads_samu_private **presult)
 {
        const char * attrs[] = {
                "lastLogon", "lastLogoff", "pwdLastSet", "accountExpires",
@@ -323,23 +423,66 @@ static NTSTATUS pdb_ads_getsampwfilter(struct pdb_methods *m,
                "unicodePwd", "dBCSPwd" };
        struct tldap_message **users;
        int rc, count;
+       struct pdb_ads_samu_private *result;
+
+       result = talloc(mem_ctx, struct pdb_ads_samu_private);
+       if (result == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
        rc = pdb_ads_search_fmt(state, state->domaindn, TLDAP_SCOPE_SUB,
-                               attrs, ARRAY_SIZE(attrs), 0, talloc_tos(),
+                               attrs, ARRAY_SIZE(attrs), 0, result,
                                &users, "%s", filter);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_search failed %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
+               TALLOC_FREE(result);
                return NT_STATUS_LDAP(rc);
        }
 
        count = talloc_array_length(users);
        if (count != 1) {
                DEBUG(10, ("Expected 1 user, got %d\n", count));
+               TALLOC_FREE(result);
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+
+       result->ldapmsg = users[0];
+       if (!tldap_entry_dn(result->ldapmsg, &result->dn)) {
+               DEBUG(10, ("Could not extract dn\n"));
+               TALLOC_FREE(result);
                return NT_STATUS_INTERNAL_DB_CORRUPTION;
        }
 
-       return pdb_ads_init_sam_from_ads(m, sam_acct, users[0]);
+       *presult = result;
+       return NT_STATUS_OK;
+}
+
+static NTSTATUS pdb_ads_getsampwfilter(struct pdb_methods *m,
+                                      struct pdb_ads_state *state,
+                                      struct samu *sam_acct,
+                                      const char *filter)
+{
+       struct pdb_ads_samu_private *priv;
+       NTSTATUS status;
+
+       status = pdb_ads_getsamupriv(state, filter, sam_acct, &priv);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(10, ("pdb_ads_getsamupriv failed: %s\n",
+                          nt_errstr(status)));
+               return status;
+       }
+
+       status = pdb_ads_init_sam_from_priv(m, sam_acct, priv);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(10, ("pdb_ads_init_sam_from_priv failed: %s\n",
+                          nt_errstr(status)));
+               TALLOC_FREE(priv);
+               return status;
+       }
+
+       pdb_set_backend_private_data(sam_acct, priv, NULL, m, PDB_SET);
+       return NT_STATUS_OK;
 }
 
 static NTSTATUS pdb_ads_getsampwnam(struct pdb_methods *m,
@@ -360,13 +503,13 @@ static NTSTATUS pdb_ads_getsampwnam(struct pdb_methods *m,
 
 static NTSTATUS pdb_ads_getsampwsid(struct pdb_methods *m,
                                    struct samu *sam_acct,
-                                   const DOM_SID *sid)
+                                   const struct dom_sid *sid)
 {
        struct pdb_ads_state *state = talloc_get_type_abort(
                m->private_data, struct pdb_ads_state);
        char *sidstr, *filter;
 
-       sidstr = sid_binstring(talloc_tos(), sid);
+       sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), sid);
        NT_STATUS_HAVE_NO_MEMORY(sidstr);
 
        filter = talloc_asprintf(
@@ -409,19 +552,19 @@ static NTSTATUS pdb_ads_create_user(struct pdb_methods *m,
 
        ok = true;
        ok &= tldap_make_mod_fmt(
-               NULL, talloc_tos(), &num_mods, &mods, "objectClass", "user");
+               NULL, talloc_tos(), &mods, &num_mods, "objectClass", "user");
        ok &= tldap_make_mod_fmt(
-               NULL, talloc_tos(), &num_mods, &mods, "samAccountName", "%s",
+               NULL, talloc_tos(), &mods, &num_mods, "samAccountName", "%s",
                name);
        if (!ok) {
                return NT_STATUS_NO_MEMORY;
        }
 
 
-       rc = tldap_add(ld, dn, num_mods, mods, NULL, 0, NULL, 0);
+       rc = tldap_add(ld, dn, mods, num_mods, NULL, 0, NULL, 0);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_add failed %s\n",
-                          tldap_errstr(debug_ctx(), ld, rc)));
+                          tldap_errstr(talloc_tos(), ld, rc)));
                TALLOC_FREE(dn);
                return NT_STATUS_LDAP(rc);
        }
@@ -433,7 +576,7 @@ static NTSTATUS pdb_ads_create_user(struct pdb_methods *m,
                                name);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("Could not find just created user %s: %s\n",
-                          name, tldap_errstr(debug_ctx(), state->ld, rc)));
+                          name, tldap_errstr(talloc_tos(), state->ld, rc)));
                TALLOC_FREE(dn);
                return NT_STATUS_LDAP(rc);
        }
@@ -483,7 +626,7 @@ static NTSTATUS pdb_ads_delete_user(struct pdb_methods *m,
        TALLOC_FREE(dn);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_delete for %s failed: %s\n", dn,
-                          tldap_errstr(debug_ctx(), ld, rc)));
+                          tldap_errstr(talloc_tos(), ld, rc)));
                return NT_STATUS_LDAP(rc);
        }
        return NT_STATUS_OK;
@@ -511,7 +654,7 @@ static NTSTATUS pdb_ads_update_sam_account(struct pdb_methods *m,
        }
 
        if (!pdb_ads_init_ads_from_sam(state, priv->ldapmsg, talloc_tos(),
-                                      &num_mods, &mods, sam)) {
+                                      &mods, &num_mods, sam)) {
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -520,12 +663,12 @@ static NTSTATUS pdb_ads_update_sam_account(struct pdb_methods *m,
                return NT_STATUS_OK;
        }
 
-       rc = tldap_modify(ld, priv->dn, num_mods, mods, NULL, 0,
+       rc = tldap_modify(ld, priv->dn, mods, num_mods, NULL, 0,
                          NULL, 0);
        TALLOC_FREE(mods);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_modify for %s failed: %s\n", priv->dn,
-                          tldap_errstr(debug_ctx(), ld, rc)));
+                          tldap_errstr(talloc_tos(), ld, rc)));
                return NT_STATUS_LDAP(rc);
        }
 
@@ -569,7 +712,7 @@ static NTSTATUS pdb_ads_getgrfilter(struct pdb_methods *m, GROUP_MAP *map,
                                &group, "%s", filter);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_search failed %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                return NT_STATUS_LDAP(rc);
        }
        if (talloc_array_length(group) != 1) {
@@ -620,7 +763,7 @@ static NTSTATUS pdb_ads_getgrfilter(struct pdb_methods *m, GROUP_MAP *map,
 }
 
 static NTSTATUS pdb_ads_getgrsid(struct pdb_methods *m, GROUP_MAP *map,
-                                DOM_SID sid)
+                                struct dom_sid sid)
 {
        char *filter;
        NTSTATUS status;
@@ -693,12 +836,12 @@ static NTSTATUS pdb_ads_create_dom_group(struct pdb_methods *m,
        }
 
        ok &= tldap_make_mod_fmt(
-               NULL, talloc_tos(), &num_mods, &mods, "samAccountName", "%s",
+               NULL, talloc_tos(), &mods, &num_mods, "samAccountName", "%s",
                name);
        ok &= tldap_make_mod_fmt(
-               NULL, talloc_tos(), &num_mods, &mods, "objectClass", "group");
+               NULL, talloc_tos(), &mods, &num_mods, "objectClass", "group");
        ok &= tldap_make_mod_fmt(
-               NULL, talloc_tos(), &num_mods, &mods, "groupType",
+               NULL, talloc_tos(), &mods, &num_mods, "groupType",
                "%d", (int)GTYPE_SECURITY_GLOBAL_GROUP);
 
        if (!ok) {
@@ -706,10 +849,10 @@ static NTSTATUS pdb_ads_create_dom_group(struct pdb_methods *m,
                return NT_STATUS_NO_MEMORY;
        }
 
-       rc = tldap_add(ld, dn, num_mods, mods, NULL, 0, NULL, 0);
+       rc = tldap_add(ld, dn, mods, num_mods, NULL, 0, NULL, 0);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_add failed %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                TALLOC_FREE(frame);
                return NT_STATUS_LDAP(rc);
        }
@@ -720,7 +863,7 @@ static NTSTATUS pdb_ads_create_dom_group(struct pdb_methods *m,
                "(&(objectclass=group)(samaccountname=%s))", name);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("Could not find just created alias %s: %s\n",
-                          name, tldap_errstr(debug_ctx(), state->ld, rc)));
+                          name, tldap_errstr(talloc_tos(), state->ld, rc)));
                TALLOC_FREE(frame);
                return NT_STATUS_LDAP(rc);
        }
@@ -758,7 +901,7 @@ static NTSTATUS pdb_ads_delete_dom_group(struct pdb_methods *m,
 
        sid_compose(&sid, &state->domainsid, rid);
 
-       sidstr = sid_binstring(talloc_tos(), &sid);
+       sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), &sid);
        NT_STATUS_HAVE_NO_MEMORY(sidstr);
 
        rc = pdb_ads_search_fmt(state, state->domaindn, TLDAP_SCOPE_SUB,
@@ -768,7 +911,7 @@ static NTSTATUS pdb_ads_delete_dom_group(struct pdb_methods *m,
        TALLOC_FREE(sidstr);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_search failed %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                return NT_STATUS_LDAP(rc);
        }
 
@@ -796,7 +939,7 @@ static NTSTATUS pdb_ads_delete_dom_group(struct pdb_methods *m,
        TALLOC_FREE(msg);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_delete failed: %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                return NT_STATUS_LDAP(rc);
        }
 
@@ -816,13 +959,13 @@ static NTSTATUS pdb_ads_update_group_mapping_entry(struct pdb_methods *m,
 }
 
 static NTSTATUS pdb_ads_delete_group_mapping_entry(struct pdb_methods *m,
-                                                  DOM_SID sid)
+                                                  struct dom_sid sid)
 {
        return NT_STATUS_NOT_IMPLEMENTED;
 }
 
 static NTSTATUS pdb_ads_enum_group_mapping(struct pdb_methods *m,
-                                          const DOM_SID *sid,
+                                          const struct dom_sid *sid,
                                           enum lsa_SidType sid_name_use,
                                           GROUP_MAP **pp_rmap,
                                           size_t *p_num_entries,
@@ -833,7 +976,7 @@ static NTSTATUS pdb_ads_enum_group_mapping(struct pdb_methods *m,
 
 static NTSTATUS pdb_ads_enum_group_members(struct pdb_methods *m,
                                           TALLOC_CTX *mem_ctx,
-                                          const DOM_SID *group,
+                                          const struct dom_sid *group,
                                           uint32 **pmembers,
                                           size_t *pnum_members)
 {
@@ -846,7 +989,7 @@ static NTSTATUS pdb_ads_enum_group_members(struct pdb_methods *m,
        DATA_BLOB *blobs;
        uint32_t *members;
 
-       sidstr = sid_binstring(talloc_tos(), group);
+       sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), group);
        NT_STATUS_HAVE_NO_MEMORY(sidstr);
 
        rc = pdb_ads_search_fmt(state, state->domaindn, TLDAP_SCOPE_SUB,
@@ -855,7 +998,7 @@ static NTSTATUS pdb_ads_enum_group_members(struct pdb_methods *m,
        TALLOC_FREE(sidstr);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_search failed %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                return NT_STATUS_LDAP(rc);
        }
        switch talloc_array_length(msg) {
@@ -869,7 +1012,7 @@ static NTSTATUS pdb_ads_enum_group_members(struct pdb_methods *m,
                break;
        }
 
-       if (!tldap_entry_values(msg[0], "member", &num_members, &blobs)) {
+       if (!tldap_entry_values(msg[0], "member", &blobs, &num_members)) {
                return NT_STATUS_INTERNAL_DB_CORRUPTION;
        }
 
@@ -895,14 +1038,13 @@ static NTSTATUS pdb_ads_enum_group_members(struct pdb_methods *m,
 static NTSTATUS pdb_ads_enum_group_memberships(struct pdb_methods *m,
                                               TALLOC_CTX *mem_ctx,
                                               struct samu *user,
-                                              DOM_SID **pp_sids,
+                                              struct dom_sid **pp_sids,
                                               gid_t **pp_gids,
                                               size_t *p_num_groups)
 {
        struct pdb_ads_state *state = talloc_get_type_abort(
                m->private_data, struct pdb_ads_state);
-       struct pdb_ads_samu_private *priv = pdb_ads_get_samu_private(
-               m, user);
+       struct pdb_ads_samu_private *priv;
        const char *attrs[1] = { "objectSid" };
        struct tldap_message **groups;
        int i, rc, count;
@@ -910,29 +1052,44 @@ static NTSTATUS pdb_ads_enum_group_memberships(struct pdb_methods *m,
        struct dom_sid *group_sids;
        gid_t *gids;
 
-       rc = pdb_ads_search_fmt(
-               state, state->domaindn, TLDAP_SCOPE_SUB,
-               attrs, ARRAY_SIZE(attrs), 0, talloc_tos(), &groups,
-               "(&(member=%s)(grouptype=%d)(objectclass=group))",
-               priv->dn, GTYPE_SECURITY_GLOBAL_GROUP);
-       if (rc != TLDAP_SUCCESS) {
-               DEBUG(10, ("ldap_search failed %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
-               return NT_STATUS_LDAP(rc);
+       priv = pdb_ads_get_samu_private(m, user);
+       if (priv != NULL) {
+               rc = pdb_ads_search_fmt(
+                       state, state->domaindn, TLDAP_SCOPE_SUB,
+                       attrs, ARRAY_SIZE(attrs), 0, talloc_tos(), &groups,
+                       "(&(member=%s)(grouptype=%d)(objectclass=group))",
+                       priv->dn, GTYPE_SECURITY_GLOBAL_GROUP);
+               if (rc != TLDAP_SUCCESS) {
+                       DEBUG(10, ("ldap_search failed %s\n",
+                                  tldap_errstr(talloc_tos(), state->ld, rc)));
+                       return NT_STATUS_LDAP(rc);
+               }
+               count = talloc_array_length(groups);
+       } else {
+               /*
+                * This happens for artificial samu users
+                */
+               DEBUG(10, ("Could not get pdb_ads_samu_private\n"));
+               count = 0;
        }
 
-       count = talloc_array_length(groups);
-
-       group_sids = talloc_array(mem_ctx, struct dom_sid, count);
+       group_sids = talloc_array(mem_ctx, struct dom_sid, count+1);
        if (group_sids == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
-       gids = talloc_array(mem_ctx, gid_t, count);
+       gids = talloc_array(mem_ctx, gid_t, count+1);
        if (gids == NULL) {
                TALLOC_FREE(group_sids);
                return NT_STATUS_NO_MEMORY;
        }
-       num_groups = 0;
+
+       sid_copy(&group_sids[0], pdb_get_group_sid(user));
+       if (!sid_to_gid(&group_sids[0], &gids[0])) {
+               TALLOC_FREE(gids);
+               TALLOC_FREE(group_sids);
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+       num_groups = 1;
 
        for (i=0; i<count; i++) {
                if (!tldap_pull_binsid(groups[i], "objectSid",
@@ -972,6 +1129,7 @@ static NTSTATUS pdb_ads_mod_groupmem(struct pdb_methods *m,
        struct dom_sid groupsid, membersid;
        char *groupdn, *memberdn;
        struct tldap_mod *mods;
+       int num_mods;
        int rc;
        NTSTATUS status;
 
@@ -995,18 +1153,19 @@ static NTSTATUS pdb_ads_mod_groupmem(struct pdb_methods *m,
        }
 
        mods = NULL;
+       num_mods = 0;
 
-       if (!tldap_add_mod_str(talloc_tos(), &mods, mod_op,
+       if (!tldap_add_mod_str(talloc_tos(), &mods, &num_mods, mod_op,
                               "member", memberdn)) {
                TALLOC_FREE(frame);
                return NT_STATUS_NO_MEMORY;
        }
 
-       rc = tldap_modify(ld, groupdn, 1, mods, NULL, 0, NULL, 0);
+       rc = tldap_modify(ld, groupdn, mods, num_mods, NULL, 0, NULL, 0);
        TALLOC_FREE(frame);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_modify failed: %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                if (rc == TLDAP_TYPE_OR_VALUE_EXISTS) {
                        return NT_STATUS_MEMBER_IN_GROUP;
                }
@@ -1064,12 +1223,12 @@ static NTSTATUS pdb_ads_create_alias(struct pdb_methods *m,
        }
 
        ok &= tldap_make_mod_fmt(
-               NULL, talloc_tos(), &num_mods, &mods, "samAccountName", "%s",
+               NULL, talloc_tos(), &mods, &num_mods, "samAccountName", "%s",
                name);
        ok &= tldap_make_mod_fmt(
-               NULL, talloc_tos(), &num_mods, &mods, "objectClass", "group");
+               NULL, talloc_tos(), &mods, &num_mods, "objectClass", "group");
        ok &= tldap_make_mod_fmt(
-               NULL, talloc_tos(), &num_mods, &mods, "groupType",
+               NULL, talloc_tos(), &mods, &num_mods, "groupType",
                "%d", (int)GTYPE_SECURITY_DOMAIN_LOCAL_GROUP);
 
        if (!ok) {
@@ -1077,10 +1236,10 @@ static NTSTATUS pdb_ads_create_alias(struct pdb_methods *m,
                return NT_STATUS_NO_MEMORY;
        }
 
-       rc = tldap_add(ld, dn, num_mods, mods, NULL, 0, NULL, 0);
+       rc = tldap_add(ld, dn, mods, num_mods, NULL, 0, NULL, 0);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_add failed %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                TALLOC_FREE(frame);
                return NT_STATUS_LDAP(rc);
        }
@@ -1091,7 +1250,7 @@ static NTSTATUS pdb_ads_create_alias(struct pdb_methods *m,
                "(&(objectclass=group)(samaccountname=%s))", name);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("Could not find just created alias %s: %s\n",
-                          name, tldap_errstr(debug_ctx(), state->ld, rc)));
+                          name, tldap_errstr(talloc_tos(), state->ld, rc)));
                TALLOC_FREE(frame);
                return NT_STATUS_LDAP(rc);
        }
@@ -1116,13 +1275,13 @@ static NTSTATUS pdb_ads_create_alias(struct pdb_methods *m,
 }
 
 static NTSTATUS pdb_ads_delete_alias(struct pdb_methods *m,
-                                    const DOM_SID *sid)
+                                    const struct dom_sid *sid)
 {
        struct pdb_ads_state *state = talloc_get_type_abort(
                m->private_data, struct pdb_ads_state);
        struct tldap_context *ld;
        struct tldap_message **alias;
-       char *sidstr, *dn;
+       char *sidstr, *dn = NULL;
        int rc;
 
        ld = pdb_ads_ld(state);
@@ -1130,7 +1289,7 @@ static NTSTATUS pdb_ads_delete_alias(struct pdb_methods *m,
                return NT_STATUS_LDAP(TLDAP_SERVER_DOWN);
        }
 
-       sidstr = sid_binstring(talloc_tos(), sid);
+       sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), sid);
        if (sidstr == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -1144,8 +1303,7 @@ static NTSTATUS pdb_ads_delete_alias(struct pdb_methods *m,
        TALLOC_FREE(sidstr);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_search failed: %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
-               TALLOC_FREE(dn);
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                return NT_STATUS_LDAP(rc);
        }
        if (talloc_array_length(alias) != 1) {
@@ -1162,8 +1320,7 @@ static NTSTATUS pdb_ads_delete_alias(struct pdb_methods *m,
        rc = tldap_delete(ld, dn, NULL, 0, NULL, 0);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_delete failed: %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
-               TALLOC_FREE(dn);
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                return NT_STATUS_LDAP(rc);
        }
 
@@ -1171,7 +1328,7 @@ static NTSTATUS pdb_ads_delete_alias(struct pdb_methods *m,
 }
 
 static NTSTATUS pdb_ads_set_aliasinfo(struct pdb_methods *m,
-                                     const DOM_SID *sid,
+                                     const struct dom_sid *sid,
                                      struct acct_info *info)
 {
        struct pdb_ads_state *state = talloc_get_type_abort(
@@ -1191,7 +1348,7 @@ static NTSTATUS pdb_ads_set_aliasinfo(struct pdb_methods *m,
                return NT_STATUS_LDAP(TLDAP_SERVER_DOWN);
        }
 
-       sidstr = sid_binstring(talloc_tos(), sid);
+       sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), sid);
        NT_STATUS_HAVE_NO_MEMORY(sidstr);
 
        rc = pdb_ads_search_fmt(state, state->domaindn, TLDAP_SCOPE_SUB,
@@ -1203,7 +1360,7 @@ static NTSTATUS pdb_ads_set_aliasinfo(struct pdb_methods *m,
        TALLOC_FREE(sidstr);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_search failed %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                return NT_STATUS_LDAP(rc);
        }
        switch talloc_array_length(msg) {
@@ -1225,10 +1382,10 @@ static NTSTATUS pdb_ads_set_aliasinfo(struct pdb_methods *m,
        ok = true;
 
        ok &= tldap_make_mod_fmt(
-               msg[0], msg, &num_mods, &mods, "description",
+               msg[0], msg, &mods, &num_mods, "description",
                "%s", info->acct_desc);
        ok &= tldap_make_mod_fmt(
-               msg[0], msg, &num_mods, &mods, "samAccountName",
+               msg[0], msg, &mods, &num_mods, "samAccountName",
                "%s", info->acct_name);
        if (!ok) {
                TALLOC_FREE(msg);
@@ -1240,11 +1397,11 @@ static NTSTATUS pdb_ads_set_aliasinfo(struct pdb_methods *m,
                return NT_STATUS_OK;
        }
 
-       rc = tldap_modify(ld, dn, num_mods, mods, NULL, 0, NULL, 0);
+       rc = tldap_modify(ld, dn, mods, num_mods, NULL, 0, NULL, 0);
        TALLOC_FREE(msg);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_modify failed: %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                return NT_STATUS_LDAP(rc);
        }
        return NT_STATUS_OK;
@@ -1258,7 +1415,7 @@ static NTSTATUS pdb_ads_sid2dn(struct pdb_ads_state *state,
        char *sidstr, *dn;
        int rc;
 
-       sidstr = sid_binstring(talloc_tos(), sid);
+       sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), sid);
        NT_STATUS_HAVE_NO_MEMORY(sidstr);
 
        rc = pdb_ads_search_fmt(state, state->domaindn, TLDAP_SCOPE_SUB,
@@ -1267,7 +1424,7 @@ static NTSTATUS pdb_ads_sid2dn(struct pdb_ads_state *state,
        TALLOC_FREE(sidstr);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_search failed %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                return NT_STATUS_LDAP(rc);
        }
 
@@ -1295,8 +1452,8 @@ static NTSTATUS pdb_ads_sid2dn(struct pdb_ads_state *state,
 }
 
 static NTSTATUS pdb_ads_mod_aliasmem(struct pdb_methods *m,
-                                    const DOM_SID *alias,
-                                    const DOM_SID *member,
+                                    const struct dom_sid *alias,
+                                    const struct dom_sid *member,
                                     int mod_op)
 {
        struct pdb_ads_state *state = talloc_get_type_abort(
@@ -1304,6 +1461,7 @@ static NTSTATUS pdb_ads_mod_aliasmem(struct pdb_methods *m,
        struct tldap_context *ld;
        TALLOC_CTX *frame = talloc_stackframe();
        struct tldap_mod *mods;
+       int num_mods;
        int rc;
        char *aliasdn, *memberdn;
        NTSTATUS status;
@@ -1329,18 +1487,19 @@ static NTSTATUS pdb_ads_mod_aliasmem(struct pdb_methods *m,
        }
 
        mods = NULL;
+       num_mods = 0;
 
-       if (!tldap_add_mod_str(talloc_tos(), &mods, mod_op,
+       if (!tldap_add_mod_str(talloc_tos(), &mods, &num_mods, mod_op,
                               "member", memberdn)) {
                TALLOC_FREE(frame);
                return NT_STATUS_NO_MEMORY;
        }
 
-       rc = tldap_modify(ld, aliasdn, 1, mods, NULL, 0, NULL, 0);
+       rc = tldap_modify(ld, aliasdn, mods, num_mods, NULL, 0, NULL, 0);
        TALLOC_FREE(frame);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_modify failed: %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                if (rc == TLDAP_TYPE_OR_VALUE_EXISTS) {
                        return NT_STATUS_MEMBER_IN_ALIAS;
                }
@@ -1354,15 +1513,15 @@ static NTSTATUS pdb_ads_mod_aliasmem(struct pdb_methods *m,
 }
 
 static NTSTATUS pdb_ads_add_aliasmem(struct pdb_methods *m,
-                                    const DOM_SID *alias,
-                                    const DOM_SID *member)
+                                    const struct dom_sid *alias,
+                                    const struct dom_sid *member)
 {
        return pdb_ads_mod_aliasmem(m, alias, member, TLDAP_MOD_ADD);
 }
 
 static NTSTATUS pdb_ads_del_aliasmem(struct pdb_methods *m,
-                                    const DOM_SID *alias,
-                                    const DOM_SID *member)
+                                    const struct dom_sid *alias,
+                                    const struct dom_sid *member)
 {
        return pdb_ads_mod_aliasmem(m, alias, member, TLDAP_MOD_DELETE);
 }
@@ -1399,9 +1558,9 @@ static bool pdb_ads_dnblob2sid(struct pdb_ads_state *state, DATA_BLOB *dnblob,
 }
 
 static NTSTATUS pdb_ads_enum_aliasmem(struct pdb_methods *m,
-                                     const DOM_SID *alias,
+                                     const struct dom_sid *alias,
                                      TALLOC_CTX *mem_ctx,
-                                     DOM_SID **pmembers,
+                                     struct dom_sid **pmembers,
                                      size_t *pnum_members)
 {
        struct pdb_ads_state *state = talloc_get_type_abort(
@@ -1413,7 +1572,7 @@ static NTSTATUS pdb_ads_enum_aliasmem(struct pdb_methods *m,
        DATA_BLOB *blobs;
        struct dom_sid *members;
 
-       sidstr = sid_binstring(talloc_tos(), alias);
+       sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), alias);
        NT_STATUS_HAVE_NO_MEMORY(sidstr);
 
        rc = pdb_ads_search_fmt(state, state->domaindn, TLDAP_SCOPE_SUB,
@@ -1422,7 +1581,7 @@ static NTSTATUS pdb_ads_enum_aliasmem(struct pdb_methods *m,
        TALLOC_FREE(sidstr);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_search failed %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                return NT_STATUS_LDAP(rc);
        }
        switch talloc_array_length(msg) {
@@ -1436,7 +1595,7 @@ static NTSTATUS pdb_ads_enum_aliasmem(struct pdb_methods *m,
                break;
        }
 
-       if (!tldap_entry_values(msg[0], "member", &num_members, &blobs)) {
+       if (!tldap_entry_values(msg[0], "member", &blobs, &num_members)) {
                return NT_STATUS_INTERNAL_DB_CORRUPTION;
        }
 
@@ -1459,8 +1618,8 @@ static NTSTATUS pdb_ads_enum_aliasmem(struct pdb_methods *m,
 
 static NTSTATUS pdb_ads_enum_alias_memberships(struct pdb_methods *m,
                                               TALLOC_CTX *mem_ctx,
-                                              const DOM_SID *domain_sid,
-                                              const DOM_SID *members,
+                                              const struct dom_sid *domain_sid,
+                                              const struct dom_sid *members,
                                               size_t num_members,
                                               uint32_t **palias_rids,
                                               size_t *pnum_alias_rids)
@@ -1468,7 +1627,7 @@ static NTSTATUS pdb_ads_enum_alias_memberships(struct pdb_methods *m,
        struct pdb_ads_state *state = talloc_get_type_abort(
                m->private_data, struct pdb_ads_state);
        const char *attrs[1] = { "objectSid" };
-       struct tldap_message **msg;
+       struct tldap_message **msg = NULL;
        uint32_t *alias_rids = NULL;
        size_t num_alias_rids = 0;
        int i, rc, count;
@@ -1519,7 +1678,7 @@ static NTSTATUS pdb_ads_enum_alias_memberships(struct pdb_methods *m,
        TALLOC_FREE(filter);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("tldap_search failed %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                return NT_STATUS_LDAP(rc);
        }
 
@@ -1554,7 +1713,7 @@ done:
 }
 
 static NTSTATUS pdb_ads_lookup_rids(struct pdb_methods *m,
-                                   const DOM_SID *domain_sid,
+                                   const struct dom_sid *domain_sid,
                                    int num_rids,
                                    uint32 *rids,
                                    const char **names,
@@ -1582,7 +1741,7 @@ static NTSTATUS pdb_ads_lookup_rids(struct pdb_methods *m,
 
                sid_compose(&sid, domain_sid, rids[i]);
 
-               sidstr = sid_binstring(talloc_tos(), &sid);
+               sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), &sid);
                NT_STATUS_HAVE_NO_MEMORY(sidstr);
 
                rc = pdb_ads_search_fmt(state, state->domaindn,
@@ -1592,7 +1751,7 @@ static NTSTATUS pdb_ads_lookup_rids(struct pdb_methods *m,
                TALLOC_FREE(sidstr);
                if (rc != TLDAP_SUCCESS) {
                        DEBUG(10, ("ldap_search failed %s\n",
-                                  tldap_errstr(debug_ctx(), state->ld, rc)));
+                                  tldap_errstr(talloc_tos(), state->ld, rc)));
                        continue;
                }
 
@@ -1616,7 +1775,7 @@ static NTSTATUS pdb_ads_lookup_rids(struct pdb_methods *m,
                        DEBUG(10, ("no samAccountType"));
                        continue;
                }
-               lsa_attrs[i] = ads_atype_map(attr);
+               lsa_attrs[i] = ds_atype_map(attr);
                num_mapped += 1;
        }
 
@@ -1630,7 +1789,7 @@ static NTSTATUS pdb_ads_lookup_rids(struct pdb_methods *m,
 }
 
 static NTSTATUS pdb_ads_lookup_names(struct pdb_methods *m,
-                                    const DOM_SID *domain_sid,
+                                    const struct dom_sid *domain_sid,
                                     int num_names,
                                     const char **pp_names,
                                     uint32 *rids,
@@ -1640,16 +1799,18 @@ static NTSTATUS pdb_ads_lookup_names(struct pdb_methods *m,
 }
 
 static NTSTATUS pdb_ads_get_account_policy(struct pdb_methods *m,
-                                          int policy_index, uint32 *value)
+                                          enum pdb_policy_type type,
+                                          uint32_t *value)
 {
-       return account_policy_get(policy_index, value)
+       return account_policy_get(type, value)
                ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
 }
 
 static NTSTATUS pdb_ads_set_account_policy(struct pdb_methods *m,
-                                          int policy_index, uint32 value)
+                                          enum pdb_policy_type type,
+                                          uint32_t value)
 {
-       return account_policy_set(policy_index, value)
+       return account_policy_set(type, value)
                ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
 }
 
@@ -1729,7 +1890,7 @@ static bool pdb_ads_search_filter(struct pdb_methods *m,
                "%s", filter);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("ldap_search_ext_s failed: %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                return false;
        }
 
@@ -1791,9 +1952,28 @@ static bool pdb_ads_search_users(struct pdb_methods *m,
                                 uint32 acct_flags)
 {
        struct pdb_ads_search_state *sstate;
+       char *filter;
        bool ret;
 
-       ret = pdb_ads_search_filter(m, search, "(objectclass=user)", &sstate);
+       if (acct_flags & ACB_NORMAL) {
+               filter = talloc_asprintf(
+                       talloc_tos(),
+                       "(&(objectclass=user)(sAMAccountType=%d))",
+                       ATYPE_NORMAL_ACCOUNT);
+       } else if (acct_flags & ACB_WSTRUST) {
+               filter = talloc_asprintf(
+                       talloc_tos(),
+                       "(&(objectclass=user)(sAMAccountType=%d))",
+                       ATYPE_WORKSTATION_TRUST);
+       } else {
+               filter = talloc_strdup(talloc_tos(), "(objectclass=user)");
+       }
+       if (filter == NULL) {
+               return false;
+       }
+
+       ret = pdb_ads_search_filter(m, search, filter, &sstate);
+       TALLOC_FREE(filter);
        if (!ret) {
                return false;
        }
@@ -1825,7 +2005,7 @@ static bool pdb_ads_search_groups(struct pdb_methods *m,
 
 static bool pdb_ads_search_aliases(struct pdb_methods *m,
                                   struct pdb_search *search,
-                                  const DOM_SID *sid)
+                                  const struct dom_sid *sid)
 {
        struct pdb_ads_search_state *sstate;
        char *filter;
@@ -1849,14 +2029,8 @@ static bool pdb_ads_search_aliases(struct pdb_methods *m,
        return true;
 }
 
-static bool pdb_ads_uid_to_rid(struct pdb_methods *m, uid_t uid,
-                              uint32 *rid)
-{
-       return false;
-}
-
 static bool pdb_ads_uid_to_sid(struct pdb_methods *m, uid_t uid,
-                              DOM_SID *sid)
+                              struct dom_sid *sid)
 {
        struct pdb_ads_state *state = talloc_get_type_abort(
                m->private_data, struct pdb_ads_state);
@@ -1865,7 +2039,7 @@ static bool pdb_ads_uid_to_sid(struct pdb_methods *m, uid_t uid,
 }
 
 static bool pdb_ads_gid_to_sid(struct pdb_methods *m, gid_t gid,
-                              DOM_SID *sid)
+                              struct dom_sid *sid)
 {
        struct pdb_ads_state *state = talloc_get_type_abort(
                m->private_data, struct pdb_ads_state);
@@ -1873,7 +2047,7 @@ static bool pdb_ads_gid_to_sid(struct pdb_methods *m, gid_t gid,
        return true;
 }
 
-static bool pdb_ads_sid_to_id(struct pdb_methods *m, const DOM_SID *sid,
+static bool pdb_ads_sid_to_id(struct pdb_methods *m, const struct dom_sid *sid,
                              union unid_t *id, enum lsa_SidType *type)
 {
        struct pdb_ads_state *state = talloc_get_type_abort(
@@ -1889,7 +2063,7 @@ static bool pdb_ads_sid_to_id(struct pdb_methods *m, const DOM_SID *sid,
 
        sid_peek_rid(sid, &rid);
 
-       sidstr = sid_binstring(talloc_tos(), sid);
+       sidstr = ldap_encode_ndr_dom_sid(talloc_tos(), sid);
        if (sidstr == NULL) {
                return false;
        }
@@ -1932,7 +2106,7 @@ static bool pdb_ads_new_rid(struct pdb_methods *m, uint32 *rid)
 
 static bool pdb_ads_get_trusteddom_pw(struct pdb_methods *m,
                                      const char *domain, char** pwd,
-                                     DOM_SID *sid,
+                                     struct dom_sid *sid,
                                      time_t *pass_last_set_time)
 {
        return false;
@@ -1940,7 +2114,7 @@ static bool pdb_ads_get_trusteddom_pw(struct pdb_methods *m,
 
 static bool pdb_ads_set_trusteddom_pw(struct pdb_methods *m,
                                      const char* domain, const char* pwd,
-                                     const DOM_SID *sid)
+                                     const struct dom_sid *sid)
 {
        return false;
 }
@@ -1956,12 +2130,15 @@ static NTSTATUS pdb_ads_enum_trusteddoms(struct pdb_methods *m,
                                         uint32 *num_domains,
                                         struct trustdom_info ***domains)
 {
-       return NT_STATUS_NOT_IMPLEMENTED;
+       *num_domains = 0;
+       *domains = NULL;
+       return NT_STATUS_OK;
 }
 
 static void pdb_ads_init_methods(struct pdb_methods *m)
 {
        m->name = "ads";
+       m->get_domain_info = pdb_ads_get_domain_info;
        m->getsampwnam = pdb_ads_getsampwnam;
        m->getsampwsid = pdb_ads_getsampwsid;
        m->create_user = pdb_ads_create_user;
@@ -2001,7 +2178,6 @@ static void pdb_ads_init_methods(struct pdb_methods *m)
        m->search_users = pdb_ads_search_users;
        m->search_groups = pdb_ads_search_groups;
        m->search_aliases = pdb_ads_search_aliases;
-       m->uid_to_rid = pdb_ads_uid_to_rid;
        m->uid_to_sid = pdb_ads_uid_to_sid;
        m->gid_to_sid = pdb_ads_gid_to_sid;
        m->sid_to_id = pdb_ads_sid_to_id;
@@ -2044,7 +2220,7 @@ static void s3_tldap_debug(void *context, enum tldap_debug_level level,
                samba_level = 2;
                break;
        case TLDAP_DEBUG_TRACE:
-               samba_level = 10;
+               samba_level = 11;
                break;
 
        };
@@ -2126,11 +2302,10 @@ int pdb_ads_search_fmt(struct pdb_ads_state *state, const char *base,
 static NTSTATUS pdb_ads_connect(struct pdb_ads_state *state,
                                const char *location)
 {
-       const char *rootdse_attrs[2] = {
-               "defaultNamingContext", "configurationNamingContext" };
-       const char *domain_attrs[1] = { "objectSid" };
+       const char *domain_attrs[2] = { "objectSid", "objectGUID" };
        const char *ncname_attrs[1] = { "netbiosname" };
-       struct tldap_message **rootdse, **domain, **ncname;
+       struct tldap_context *ld;
+       struct tldap_message *rootdse, **domain, **ncname;
        TALLOC_CTX *frame = talloc_stackframe();
        NTSTATUS status;
        int num_domains;
@@ -2138,26 +2313,26 @@ static NTSTATUS pdb_ads_connect(struct pdb_ads_state *state,
 
        ZERO_STRUCT(state->socket_address);
        state->socket_address.sun_family = AF_UNIX;
-       strncpy(state->socket_address.sun_path, location,
-               sizeof(state->socket_address.sun_path) - 1);
+       strlcpy(state->socket_address.sun_path, location,
+               sizeof(state->socket_address.sun_path));
 
-       rc = pdb_ads_search_fmt(
-               state, "", TLDAP_SCOPE_BASE,
-               rootdse_attrs, ARRAY_SIZE(rootdse_attrs), 0,
-               talloc_tos(), &rootdse, "(objectclass=*)");
+       ld = pdb_ads_ld(state);
+       if (ld == NULL) {
+               status = NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
+               goto done;
+       }
+
+       rc = tldap_fetch_rootdse(ld);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("Could not retrieve rootdse: %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                status = NT_STATUS_LDAP(rc);
                goto done;
        }
-       if (talloc_array_length(rootdse) != 1) {
-               status = NT_STATUS_INTERNAL_DB_CORRUPTION;
-               goto done;
-       }
+       rootdse = tldap_rootdse(state->ld);
 
        state->domaindn = tldap_talloc_single_attribute(
-               rootdse[0], "defaultNamingContext", state);
+               rootdse, "defaultNamingContext", state);
        if (state->domaindn == NULL) {
                DEBUG(10, ("Could not get defaultNamingContext\n"));
                status = NT_STATUS_INTERNAL_DB_CORRUPTION;
@@ -2166,7 +2341,7 @@ static NTSTATUS pdb_ads_connect(struct pdb_ads_state *state,
        DEBUG(10, ("defaultNamingContext = %s\n", state->domaindn));
 
        state->configdn = tldap_talloc_single_attribute(
-               rootdse[0], "configurationNamingContext", state);
+               rootdse, "configurationNamingContext", state);
        if (state->domaindn == NULL) {
                DEBUG(10, ("Could not get configurationNamingContext\n"));
                status = NT_STATUS_INTERNAL_DB_CORRUPTION;
@@ -2183,7 +2358,7 @@ static NTSTATUS pdb_ads_connect(struct pdb_ads_state *state,
                talloc_tos(), &domain, "(objectclass=*)");
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("Could not retrieve domain: %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                status = NT_STATUS_LDAP(rc);
                goto done;
        }
@@ -2199,6 +2374,11 @@ static NTSTATUS pdb_ads_connect(struct pdb_ads_state *state,
                status = NT_STATUS_INTERNAL_DB_CORRUPTION;
                goto done;
        }
+       if (!tldap_pull_guid(domain[0], "objectGUID", &state->domainguid)) {
+               DEBUG(10, ("Could not retrieve domain GUID\n"));
+               status = NT_STATUS_INTERNAL_DB_CORRUPTION;
+               goto done;
+       }
        DEBUG(10, ("Domain SID: %s\n", sid_string_dbg(&state->domainsid)));
 
        /*
@@ -2210,7 +2390,7 @@ static NTSTATUS pdb_ads_connect(struct pdb_ads_state *state,
                talloc_tos(), &ncname, "(ncname=%s)", state->domaindn);
        if (rc != TLDAP_SUCCESS) {
                DEBUG(10, ("Could not retrieve ncname: %s\n",
-                          tldap_errstr(debug_ctx(), state->ld, rc)));
+                          tldap_errstr(talloc_tos(), state->ld, rc)));
                status = NT_STATUS_LDAP(rc);
                goto done;
        }
@@ -2251,7 +2431,7 @@ static NTSTATUS pdb_init_ads(struct pdb_methods **pdb_method,
        char *tmp = NULL;
        NTSTATUS status;
 
-       m = talloc(talloc_autofree_context(), struct pdb_methods);
+       m = talloc(NULL, struct pdb_methods);
        if (m == NULL) {
                return NT_STATUS_NO_MEMORY;
        }