s3: rename sid_check_is_in_our_domain() to sid_check_is_in_our_sam()
[mat/samba.git] / source3 / passdb / pdb_ldap.c
index 7ce5edcefa9d91dc7e9c1a1c7a2b96c674fee65b..68ced934d36b391d80bab77ab5510e601cd1fa2a 100644 (file)
@@ -51,6 +51,7 @@
 #include "../libcli/security/security.h"
 #include "../lib/util/util_pw.h"
 #include "lib/winbind_util.h"
+#include "librpc/gen_ndr/idmap.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_PASSDB
 
 
 #include "smbldap.h"
+#include "passdb/pdb_ldap.h"
+#include "passdb/pdb_nds.h"
+#include "passdb/pdb_ipa.h"
+#include "passdb/pdb_ldap_util.h"
+#include "passdb/pdb_ldap_schema.h"
 
 /**********************************************************************
  Simple helper function to make stuff better readable
@@ -77,9 +83,6 @@ LDAP *priv2ld(struct ldapsam_privates *priv)
 static const char* get_userattr_key2string( int schema_ver, int key )
 {
        switch ( schema_ver ) {
-               case SCHEMAVER_SAMBAACCOUNT:
-                       return get_attr_key2string( attrib_map_v22, key );
-
                case SCHEMAVER_SAMBASAMACCOUNT:
                        return get_attr_key2string( attrib_map_v30, key );
 
@@ -97,9 +100,6 @@ static const char* get_userattr_key2string( int schema_ver, int key )
 const char** get_userattr_list( TALLOC_CTX *mem_ctx, int schema_ver )
 {
        switch ( schema_ver ) {
-               case SCHEMAVER_SAMBAACCOUNT:
-                       return get_attr_list( mem_ctx, attrib_map_v22 );
-
                case SCHEMAVER_SAMBASAMACCOUNT:
                        return get_attr_list( mem_ctx, attrib_map_v30 );
                default:
@@ -118,10 +118,6 @@ static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
                                              int schema_ver )
 {
        switch ( schema_ver ) {
-               case SCHEMAVER_SAMBAACCOUNT:
-                       return get_attr_list( mem_ctx,
-                                             attrib_map_to_delete_v22 );
-
                case SCHEMAVER_SAMBASAMACCOUNT:
                        return get_attr_list( mem_ctx,
                                              attrib_map_to_delete_v30 );
@@ -145,9 +141,6 @@ static const char* get_objclass_filter( int schema_ver )
        char *result;
 
        switch( schema_ver ) {
-               case SCHEMAVER_SAMBAACCOUNT:
-                       fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
-                       break;
                case SCHEMAVER_SAMBASAMACCOUNT:
                        fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBASAMACCOUNT );
                        break;
@@ -208,7 +201,7 @@ static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_
        if (mem_ctx == NULL)
                return NT_STATUS_NO_MEMORY;
 
-       if ((attrs = TALLOC_ARRAY(mem_ctx, const char *, 2)) == NULL) {
+       if ((attrs = talloc_array(mem_ctx, const char *, 2)) == NULL) {
                ntstatus = NT_STATUS_NO_MEMORY;
                goto done;
        }
@@ -496,7 +489,6 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
                        kickoff_time,
                        pass_last_set_time,
                        pass_can_change_time,
-                       pass_must_change_time,
                        ldap_entry_time,
                        bad_password_time;
        char *username = NULL,
@@ -657,18 +649,6 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
                                pass_can_change_time, PDB_SET);
        }
 
-       temp = smbldap_talloc_single_attribute(
-                       ldap_state->smbldap_state->ldap_struct,
-                       entry,
-                       get_userattr_key2string(ldap_state->schema_ver,
-                               LDAP_ATTR_PWD_MUST_CHANGE),
-                       ctx);
-       if (temp) {
-               pass_must_change_time = (time_t) atol(temp);
-               pdb_set_pass_must_change_time(sampass,
-                               pass_must_change_time, PDB_SET);
-       }
-
        /* recommend that 'gecos' and 'displayName' should refer to the same
         * attribute OID.  userFullName depreciated, only used by Samba
         * primary rules of LDAP: don't make a new attribute when one is already defined
@@ -887,7 +867,7 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
        if (pwHistLen > 0){
                uint8 *pwhist = NULL;
                int i;
-               char *history_string = TALLOC_ARRAY(ctx, char,
+               char *history_string = talloc_array(ctx, char,
                                                MAX_PW_HISTORY_LEN*64);
 
                if (!history_string) {
@@ -896,7 +876,7 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
 
                pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
 
-               pwhist = TALLOC_ARRAY(ctx, uint8,
+               pwhist = talloc_array(ctx, uint8,
                                      pwHistLen * PW_HISTORY_ENTRY_LEN);
                if (pwhist == NULL) {
                        DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
@@ -1015,6 +995,7 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
                bool have_gid = false;
                struct dom_sid mapped_gsid;
                const struct dom_sid *primary_gsid;
+               struct unixid id;
 
                ZERO_STRUCT(unix_pw);
 
@@ -1078,18 +1059,18 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
                        goto fn_exit;
                }
 
-               store_uid_sid_cache(pdb_get_user_sid(sampass),
-                                   sampass->unix_pw->pw_uid);
-               idmap_cache_set_sid2uid(pdb_get_user_sid(sampass),
-                                       sampass->unix_pw->pw_uid);
+               id.id = sampass->unix_pw->pw_uid;
+               id.type = ID_TYPE_UID;
+
+               idmap_cache_set_sid2unixid(pdb_get_user_sid(sampass), &id);
 
                gid_to_sid(&mapped_gsid, sampass->unix_pw->pw_gid);
                primary_gsid = pdb_get_group_sid(sampass);
                if (primary_gsid && dom_sid_equal(primary_gsid, &mapped_gsid)) {
-                       store_gid_sid_cache(primary_gsid,
-                                           sampass->unix_pw->pw_gid);
-                       idmap_cache_set_sid2gid(primary_gsid,
-                                               sampass->unix_pw->pw_gid);
+                       id.id = sampass->unix_pw->pw_gid;
+                       id.type = ID_TYPE_GID;
+
+                       idmap_cache_set_sid2unixid(primary_gsid, &id);
                }
        }
 
@@ -1185,23 +1166,6 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
                const struct dom_sid *user_sid = pdb_get_user_sid(sampass);
 
                switch ( ldap_state->schema_ver ) {
-                       case SCHEMAVER_SAMBAACCOUNT:
-                               if (!sid_peek_check_rid(&ldap_state->domain_sid, user_sid, &rid)) {
-                                       DEBUG(1, ("init_ldap_from_sam: User's SID (%s) is not for this domain (%s), cannot add to LDAP!\n", 
-                                                 sid_string_dbg(user_sid),
-                                                 sid_string_dbg(
-                                                         &ldap_state->domain_sid)));
-                                       return False;
-                               }
-                               if (asprintf(&temp, "%i", rid) < 0) {
-                                       return false;
-                               }
-                               smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
-                                       get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_RID), 
-                                       temp);
-                               SAFE_FREE(temp);
-                               break;
-
                        case SCHEMAVER_SAMBASAMACCOUNT:
                                smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
                                        get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_USER_SID), 
@@ -1222,24 +1186,6 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
                const struct dom_sid *group_sid = pdb_get_group_sid(sampass);
 
                switch ( ldap_state->schema_ver ) {
-                       case SCHEMAVER_SAMBAACCOUNT:
-                               if (!sid_peek_check_rid(&ldap_state->domain_sid, group_sid, &rid)) {
-                                       DEBUG(1, ("init_ldap_from_sam: User's Primary Group SID (%s) is not for this domain (%s), cannot add to LDAP!\n",
-                                                 sid_string_dbg(group_sid),
-                                                 sid_string_dbg(
-                                                         &ldap_state->domain_sid)));
-                                       return False;
-                               }
-
-                               if (asprintf(&temp, "%i", rid) < 0) {
-                                       return false;
-                               }
-                               smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
-                                       get_userattr_key2string(ldap_state->schema_ver, 
-                                       LDAP_ATTR_PRIMARY_GROUP_RID), temp);
-                               SAFE_FREE(temp);
-                               break;
-
                        case SCHEMAVER_SAMBASAMACCOUNT:
                                smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
                                        get_userattr_key2string(ldap_state->schema_ver, 
@@ -1333,14 +1279,6 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
                        get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_CAN_CHANGE), temp);
        SAFE_FREE(temp);
 
-       if (asprintf(&temp, "%li", (long int)pdb_get_pass_must_change_time(sampass)) < 0) {
-               return false;
-       }
-       if (need_update(sampass, PDB_MUSTCHANGETIME))
-               smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
-                       get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PWD_MUST_CHANGE), temp);
-       SAFE_FREE(temp);
-
        if ((pdb_get_acct_ctrl(sampass)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST))
                        || (lp_ldap_passwd_sync()!=LDAP_PASSWD_SYNC_ONLY)) {
 
@@ -1532,7 +1470,7 @@ static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
                ;
        }
 
-       (*attr_list) = TALLOC_REALLOC_ARRAY(mem_ctx, (*attr_list),
+       (*attr_list) = talloc_realloc(mem_ctx, (*attr_list),
                                            const char *,  i+2);
        SMB_ASSERT((*attr_list) != NULL);
        (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
@@ -1634,18 +1572,8 @@ static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
                        break;
                }
 
-               case SCHEMAVER_SAMBAACCOUNT:
-                       if (!sid_peek_check_rid(&ldap_state->domain_sid, sid, &rid)) {
-                               return rc;
-                       }
-
-                       attr_list = get_userattr_list(NULL,
-                                                     ldap_state->schema_ver);
-                       rc = ldapsam_search_suffix_by_rid(ldap_state, rid, result, attr_list );
-                       TALLOC_FREE( attr_list );
-
-                       if ( rc != LDAP_SUCCESS ) 
-                               return rc;
+               default:
+                       DEBUG(0,("Invalid schema version specified\n"));
                        break;
        }
        return rc;
@@ -1930,7 +1858,7 @@ static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
        rc = ldapsam_delete_entry(
                priv, mem_ctx, entry,
                priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
-               LDAP_OBJ_SAMBASAMACCOUNT : LDAP_OBJ_SAMBAACCOUNT,
+               LDAP_OBJ_SAMBASAMACCOUNT : 0,
                attr_list);
 
        result = (rc == LDAP_SUCCESS) ?
@@ -1941,17 +1869,6 @@ static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
        return result;
 }
 
-/**********************************************************************
- Helper function to determine for update_sam_account whether
- we need LDAP modification.
-*********************************************************************/
-
-static bool element_is_changed(const struct samu *sampass,
-                              enum pdb_elements element)
-{
-       return IS_SAM_CHANGED(sampass, element);
-}
-
 /**********************************************************************
  Update struct samu.
 *********************************************************************/
@@ -1997,7 +1914,7 @@ static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struc
        DEBUG(4, ("ldapsam_update_sam_account: user %s to be modified has dn: %s\n", pdb_get_username(newpwd), dn));
 
        if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
-                               element_is_changed)) {
+                               pdb_element_is_changed)) {
                DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
                TALLOC_FREE(dn);
                if (mods != NULL)
@@ -2013,7 +1930,7 @@ static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struc
                return NT_STATUS_OK;
        }
 
-       ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, element_is_changed);
+       ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, pdb_element_is_changed);
 
        if (mods != NULL) {
                ldap_mods_free(mods,True);
@@ -2081,13 +1998,13 @@ static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
        oldname = pdb_get_username(old_acct);
 
        /* rename the posix user */
-       rename_script = SMB_STRDUP(lp_renameuser_script());
+       rename_script = talloc_strdup(talloc_tos(), lp_renameuser_script());
        if (rename_script == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
        if (!(*rename_script)) {
-               SAFE_FREE(rename_script);
+               TALLOC_FREE(rename_script);
                return NT_STATUS_ACCESS_DENIED;
        }
 
@@ -2120,7 +2037,7 @@ static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
        DEBUG(rc ? 0 : 3,("Running the command `%s' gave %d\n",
                          rename_script, rc));
 
-       SAFE_FREE(rename_script);
+       TALLOC_FREE(rename_script);
 
        if (rc == 0) {
                smb_nscd_flush_user_cache();
@@ -2132,18 +2049,6 @@ static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
        return NT_STATUS_OK;
 }
 
-/**********************************************************************
- Helper function to determine for update_sam_account whether
- we need LDAP modification.
- *********************************************************************/
-
-static bool element_is_set_or_changed(const struct samu *sampass,
-                                     enum pdb_elements element)
-{
-       return (IS_SAM_SET(sampass, element) ||
-               IS_SAM_CHANGED(sampass, element));
-}
-
 /**********************************************************************
  Add struct samu to LDAP.
 *********************************************************************/
@@ -2194,7 +2099,7 @@ static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct s
        ldap_msgfree(result);
        result = NULL;
 
-       if (element_is_set_or_changed(newpwd, PDB_USERSID)) {
+       if (pdb_element_is_set_or_changed(newpwd, PDB_USERSID)) {
                rc = ldapsam_get_ldap_user_by_sid(ldap_state,
                                                  sid, &result);
                if (rc == LDAP_SUCCESS) {
@@ -2330,7 +2235,7 @@ static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct s
        }
 
        if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
-                               element_is_set_or_changed)) {
+                               pdb_element_is_set_or_changed)) {
                DEBUG(0, ("ldapsam_add_sam_account: init_ldap_from_sam failed!\n"));
                if (mods != NULL) {
                        ldap_mods_free(mods, true);
@@ -2343,9 +2248,6 @@ static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct s
                goto fn_exit;
        }
        switch ( ldap_state->schema_ver ) {
-               case SCHEMAVER_SAMBAACCOUNT:
-                       smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBAACCOUNT);
-                       break;
                case SCHEMAVER_SAMBASAMACCOUNT:
                        smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_SAMBASAMACCOUNT);
                        break;
@@ -2354,7 +2256,7 @@ static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct s
                        break;
        }
 
-       ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, element_is_set_or_changed);
+       ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,ldap_op, pdb_element_is_set_or_changed);
        if (!NT_STATUS_IS_OK(ret)) {
                DEBUG(0,("ldapsam_add_sam_account: failed to modify/add user with uid = %s (dn = %s)\n",
                         pdb_get_username(newpwd),dn));
@@ -2491,7 +2393,11 @@ for gidNumber(%lu)\n",(unsigned long)map->gid));
                        return false;
                }
        }
-       fstrcpy(map->nt_name, temp);
+       map->nt_name = talloc_strdup(map, temp);
+       if (!map->nt_name) {
+               TALLOC_FREE(ctx);
+               return false;
+       }
 
        TALLOC_FREE(temp);
        temp = smbldap_talloc_single_attribute(
@@ -2507,11 +2413,18 @@ for gidNumber(%lu)\n",(unsigned long)map->gid));
                        return false;
                }
        }
-       fstrcpy(map->comment, temp);
+       map->comment = talloc_strdup(map, temp);
+       if (!map->comment) {
+               TALLOC_FREE(ctx);
+               return false;
+       }
 
        if (lp_parm_bool(-1, "ldapsam", "trusted", false)) {
-               store_gid_sid_cache(&map->sid, map->gid);
-               idmap_cache_set_sid2gid(&map->sid, map->gid);
+               struct unixid id;
+               id.id = map->gid;
+               id.type = ID_TYPE_GID;
+
+               idmap_cache_set_sid2unixid(&map->sid, &id);
        }
 
        TALLOC_FREE(ctx);
@@ -2811,7 +2724,7 @@ static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
                        if (!string_to_sid(&sid, sidstr))
                                goto done;
 
-                       if (!sid_check_is_in_our_domain(&sid)) {
+                       if (!sid_check_is_in_our_sam(&sid)) {
                                DEBUG(0, ("Inconsistent SAM -- group member uid not "
                                          "in our domain\n"));
                                ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
@@ -3037,6 +2950,7 @@ static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
        if (dom_sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
                DEBUG(3, ("primary group of [%s] not found\n",
                          pdb_get_username(user)));
+               ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
                goto done;
        }
 
@@ -3161,7 +3075,7 @@ static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
                break;
 
        case SID_NAME_ALIAS:
-               if (!sid_check_is_in_our_domain(&map->sid) 
+               if (!sid_check_is_in_our_sam(&map->sid) 
                        && !sid_check_is_in_builtin(&map->sid) ) 
                {
                        DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
@@ -3490,11 +3404,11 @@ static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
 
 static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
                                           const struct dom_sid *domsid, enum lsa_SidType sid_name_use,
-                                          GROUP_MAP **pp_rmap,
+                                          GROUP_MAP ***pp_rmap,
                                           size_t *p_num_entries,
                                           bool unix_only)
 {
-       GROUP_MAP map = { 0, };
+       GROUP_MAP *map = NULL;
        size_t entries = 0;
 
        *p_num_entries = 0;
@@ -3506,31 +3420,44 @@ static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
                return NT_STATUS_ACCESS_DENIED;
        }
 
-       while (NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, &map))) {
+       while (true) {
+
+               map = talloc_zero(NULL, GROUP_MAP);
+               if (!map) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+
+               if (!NT_STATUS_IS_OK(ldapsam_getsamgrent(methods, map))) {
+                       TALLOC_FREE(map);
+                       break;
+               }
+
                if (sid_name_use != SID_NAME_UNKNOWN &&
-                   sid_name_use != map.sid_name_use) {
+                   sid_name_use != map->sid_name_use) {
                        DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
-                                 "not of the requested type\n", map.nt_name));
+                                 "not of the requested type\n",
+                                 map->nt_name));
                        continue;
                }
-               if (unix_only==ENUM_ONLY_MAPPED && map.gid==-1) {
+               if (unix_only == ENUM_ONLY_MAPPED && map->gid == -1) {
                        DEBUG(11,("ldapsam_enum_group_mapping: group %s is "
-                                 "non mapped\n", map.nt_name));
+                                 "non mapped\n", map->nt_name));
                        continue;
                }
 
-               (*pp_rmap)=SMB_REALLOC_ARRAY((*pp_rmap), GROUP_MAP, entries+1);
+               *pp_rmap = talloc_realloc(NULL, *pp_rmap,
+                                               GROUP_MAP *, entries + 1);
                if (!(*pp_rmap)) {
                        DEBUG(0,("ldapsam_enum_group_mapping: Unable to "
                                 "enlarge group map!\n"));
                        return NT_STATUS_UNSUCCESSFUL;
                }
 
-               (*pp_rmap)[entries] = map;
+               (*pp_rmap)[entries] = talloc_move((*pp_rmap), &map);
 
                entries += 1;
-
        }
+
        ldapsam_endsamgrent(methods);
 
        *p_num_entries = entries;
@@ -3560,7 +3487,7 @@ static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
                type = SID_NAME_ALIAS;
        }
 
-       if (sid_check_is_in_our_domain(alias)) {
+       if (sid_check_is_in_our_sam(alias)) {
                type = SID_NAME_ALIAS;
        }
 
@@ -3683,7 +3610,7 @@ static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
                type = SID_NAME_ALIAS;
        }
 
-       if (sid_check_is_in_our_domain(alias)) {
+       if (sid_check_is_in_our_sam(alias)) {
                type = SID_NAME_ALIAS;
        }
 
@@ -3800,7 +3727,7 @@ static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
                type = SID_NAME_ALIAS;
        }
 
-       if (sid_check_is_domain(domain_sid)) {
+       if (sid_check_is_our_sam(domain_sid)) {
                type = SID_NAME_ALIAS;
        }
 
@@ -4105,7 +4032,7 @@ static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
        }
 
        if (!sid_check_is_builtin(domain_sid) &&
-           !sid_check_is_domain(domain_sid)) {
+           !sid_check_is_our_sam(domain_sid)) {
                result = NT_STATUS_INVALID_PARAMETER;
                goto done;
        }
@@ -4340,7 +4267,7 @@ static const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
                num += 1;
        va_end(ap);
 
-       if ((result = TALLOC_ARRAY(mem_ctx, const char *, num+1)) == NULL) {
+       if ((result = talloc_array(mem_ctx, const char *, num+1)) == NULL) {
                return NULL;
        }
 
@@ -4564,7 +4491,7 @@ static bool ldapuser2displayentry(struct ldap_search_state *state,
                return False;
        }
        if (!pull_utf8_talloc(mem_ctx,
-                             CONST_DISCARD(char **, &result->account_name),
+                             discard_const_p(char *, &result->account_name),
                              vals[0], &converted_size))
        {
                DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
@@ -4577,7 +4504,7 @@ static bool ldapuser2displayentry(struct ldap_search_state *state,
        if ((vals == NULL) || (vals[0] == NULL))
                DEBUG(8, ("\"displayName\" not found\n"));
        else if (!pull_utf8_talloc(mem_ctx,
-                                  CONST_DISCARD(char **, &result->fullname),
+                                  discard_const_p(char *, &result->fullname),
                                   vals[0], &converted_size))
        {
                DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
@@ -4590,7 +4517,7 @@ static bool ldapuser2displayentry(struct ldap_search_state *state,
        if ((vals == NULL) || (vals[0] == NULL))
                DEBUG(8, ("\"description\" not found\n"));
        else if (!pull_utf8_talloc(mem_ctx,
-                                  CONST_DISCARD(char **, &result->description),
+                                  discard_const_p(char *, &result->description),
                                   vals[0], &converted_size))
        {
                DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
@@ -4724,7 +4651,7 @@ static bool ldapgroup2displayentry(struct ldap_search_state *state,
                        return False;
                }
                if (!pull_utf8_talloc(mem_ctx,
-                                     CONST_DISCARD(char **,
+                                     discard_const_p(char *,
                                                    &result->account_name),
                                      vals[0], &converted_size))
                {
@@ -4733,7 +4660,7 @@ static bool ldapgroup2displayentry(struct ldap_search_state *state,
                }
        }
        else if (!pull_utf8_talloc(mem_ctx,
-                                  CONST_DISCARD(char **,
+                                  discard_const_p(char *,
                                                 &result->account_name),
                                   vals[0], &converted_size))
        {
@@ -4747,7 +4674,7 @@ static bool ldapgroup2displayentry(struct ldap_search_state *state,
        if ((vals == NULL) || (vals[0] == NULL))
                DEBUG(8, ("\"description\" not found\n"));
        else if (!pull_utf8_talloc(mem_ctx,
-                                  CONST_DISCARD(char **, &result->description),
+                                  discard_const_p(char *, &result->description),
                                   vals[0], &converted_size))
        {
                DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
@@ -4992,7 +4919,7 @@ static bool ldapsam_new_rid(struct pdb_methods *methods, uint32_t *rid)
 
 static bool ldapsam_sid_to_id(struct pdb_methods *methods,
                              const struct dom_sid *sid,
-                             union unid_t *id, enum lsa_SidType *type)
+                             struct unixid *id)
 {
        struct ldapsam_privates *priv =
                (struct ldapsam_privates *)methods->private_data;
@@ -5054,10 +4981,9 @@ static bool ldapsam_sid_to_id(struct pdb_methods *methods,
                        goto done;
                }
 
-               id->gid = strtoul(gid_str, NULL, 10);
-               *type = (enum lsa_SidType)strtoul(value, NULL, 10);
-               store_gid_sid_cache(sid, id->gid);
-               idmap_cache_set_sid2gid(sid, id->gid);
+               id->id = strtoul(gid_str, NULL, 10);
+               id->type = ID_TYPE_GID;
+               idmap_cache_set_sid2unixid(sid, id);
                ret = True;
                goto done;
        }
@@ -5072,10 +4998,9 @@ static bool ldapsam_sid_to_id(struct pdb_methods *methods,
                goto done;
        }
 
-       id->uid = strtoul(value, NULL, 10);
-       *type = SID_NAME_USER;
-       store_uid_sid_cache(sid, id->uid);
-       idmap_cache_set_sid2uid(sid, id->uid);
+       id->id = strtoul(value, NULL, 10);
+       id->type = ID_TYPE_UID;
+       idmap_cache_set_sid2unixid(sid, id);
 
        ret = True;
  done:
@@ -5101,6 +5026,7 @@ static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
        struct dom_sid user_sid;
        int rc;
        TALLOC_CTX *tmp_ctx = talloc_stackframe();
+       struct unixid id;
 
        filter = talloc_asprintf(tmp_ctx,
                                 "(&(uidNumber=%u)"
@@ -5145,8 +5071,10 @@ static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
 
        sid_copy(sid, &user_sid);
 
-       store_uid_sid_cache(sid, uid);
-       idmap_cache_set_sid2uid(sid, uid);
+       id.id = uid;
+       id.type = ID_TYPE_UID;
+
+       idmap_cache_set_sid2unixid(sid, &id);
 
        ret = true;
 
@@ -5173,6 +5101,7 @@ static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
        struct dom_sid group_sid;
        int rc;
        TALLOC_CTX *tmp_ctx = talloc_stackframe();
+       struct unixid id;
 
        filter = talloc_asprintf(tmp_ctx,
                                 "(&(gidNumber=%u)"
@@ -5215,8 +5144,10 @@ static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
 
        sid_copy(sid, &group_sid);
 
-       store_gid_sid_cache(sid, gid);
-       idmap_cache_set_sid2gid(sid, gid);
+       id.id = gid;
+       id.type = ID_TYPE_GID;
+
+       idmap_cache_set_sid2unixid(sid, &id);
 
        ret = true;
 
@@ -5367,7 +5298,7 @@ static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
                return NT_STATUS_UNSUCCESSFUL;
        }
 
-       if (!init_ldap_from_sam(ldap_state, entry, &mods, user, element_is_set_or_changed)) {
+       if (!init_ldap_from_sam(ldap_state, entry, &mods, user, pdb_element_is_set_or_changed)) {
                DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
                return NT_STATUS_UNSUCCESSFUL;
        }
@@ -6356,7 +6287,7 @@ static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
        }
 
        *num_domains = 0;
-       if (!(*domains = TALLOC_ARRAY(mem_ctx, struct trustdom_info *, 1))) {
+       if (!(*domains = talloc_array(mem_ctx, struct trustdom_info *, 1))) {
                DEBUG(1, ("talloc failed\n"));
                return NT_STATUS_NO_MEMORY;
        }
@@ -6368,7 +6299,7 @@ static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
                char *dom_name, *dom_sid_str;
                struct trustdom_info *dom_info;
 
-               dom_info = TALLOC_P(*domains, struct trustdom_info);
+               dom_info = talloc(*domains, struct trustdom_info);
                if (dom_info == NULL) {
                        DEBUG(1, ("talloc failed\n"));
                        return NT_STATUS_NO_MEMORY;
@@ -6443,6 +6374,8 @@ static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const c
 {
        NTSTATUS nt_status;
        struct ldapsam_privates *ldap_state;
+       char *bind_dn = NULL;
+       char *bind_secret = NULL;
 
        if (!NT_STATUS_IS_OK(nt_status = make_pdb_method( pdb_method ))) {
                return nt_status;
@@ -6480,14 +6413,22 @@ static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const c
 
        /* TODO: Setup private data and free */
 
-       if ( !(ldap_state = TALLOC_ZERO_P(*pdb_method, struct ldapsam_privates)) ) {
+       if ( !(ldap_state = talloc_zero(*pdb_method, struct ldapsam_privates)) ) {
                DEBUG(0, ("pdb_init_ldapsam_common: talloc() failed for ldapsam private_data!\n"));
                return NT_STATUS_NO_MEMORY;
        }
 
-       nt_status = smbldap_init(*pdb_method, pdb_get_event_context(),
-                                location, &ldap_state->smbldap_state);
+       if (!fetch_ldap_pw(&bind_dn, &bind_secret)) {
+               DEBUG(0, ("pdb_init_ldapsam_common: Failed to retrieve LDAP password from secrets.tdb\n"));
+               return NT_STATUS_NO_MEMORY;
+       }
 
+       nt_status = smbldap_init(*pdb_method, pdb_get_tevent_context(),
+                                location, false, bind_dn, bind_secret,
+                                &ldap_state->smbldap_state);
+       memset(bind_secret, '\0', strlen(bind_secret));
+       SAFE_FREE(bind_secret);
+       SAFE_FREE(bind_dn);
        if ( !NT_STATUS_IS_OK(nt_status) ) {
                return nt_status;
        }
@@ -6503,35 +6444,6 @@ static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const c
        return NT_STATUS_OK;
 }
 
-/**********************************************************************
- Initialise the 'compat' mode for pdb_ldap
- *********************************************************************/
-
-NTSTATUS pdb_init_ldapsam_compat(struct pdb_methods **pdb_method, const char *location)
-{
-       NTSTATUS nt_status;
-       struct ldapsam_privates *ldap_state;
-       char *uri = talloc_strdup( NULL, location );
-
-       trim_char( uri, '\"', '\"' );
-       nt_status = pdb_init_ldapsam_common( pdb_method, uri );
-       if ( uri )
-               TALLOC_FREE( uri );
-
-       if ( !NT_STATUS_IS_OK(nt_status) ) {
-               return nt_status;
-       }
-
-       (*pdb_method)->name = "ldapsam_compat";
-
-       ldap_state = (struct ldapsam_privates *)((*pdb_method)->private_data);
-       ldap_state->schema_ver = SCHEMAVER_SAMBAACCOUNT;
-
-       sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
-
-       return NT_STATUS_OK;
-}
-
 /**********************************************************************
  Initialise the normal mode for pdb_ldap
  *********************************************************************/
@@ -6599,13 +6511,10 @@ NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
                                               ldap_state->domain_name, True);
 
        if ( !NT_STATUS_IS_OK(nt_status) ) {
-               DEBUG(2, ("pdb_init_ldapsam: WARNING: Could not get domain "
-                         "info, nor add one to the domain\n"));
-               DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
-                            "will be unable to allocate new users/groups, "
-                            "and will risk BDCs having inconsistent SIDs\n"));
-               sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
-               return NT_STATUS_OK;
+               DEBUG(0, ("pdb_init_ldapsam: WARNING: Could not get domain "
+                         "info, nor add one to the domain. "
+                         "We cannot work reliably without it.\n"));
+               return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
        }
 
        /* Given that the above might fail, everything below this must be
@@ -6694,9 +6603,6 @@ NTSTATUS pdb_ldap_init(void)
        if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam", pdb_init_ldapsam)))
                return nt_status;
 
-       if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "ldapsam_compat", pdb_init_ldapsam_compat)))
-               return nt_status;
-
        /* Let pdb_nds register backends */
        pdb_nds_init();