s3-pdb_ldap: Make ldapsam_alias_memberships behave like the tdbsam equivalent.
[ira/wip.git] / source3 / passdb / pdb_ldap.c
index 205ca68fe3e5651a324bab18c68b8085046166d4..fc736c2e3eb41ba50e581d311d99f5a08ccac923 100644 (file)
@@ -7,20 +7,20 @@
    Copyright (C) Andrew Bartlett               2002-2003
    Copyright (C) Stefan (metze) Metzmacher     2002-2003
    Copyright (C) Simo Sorce                    2006
-    
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-   
+
 */
 
 /* TODO:
@@ -44,6 +44,7 @@
 */
 
 #include "includes.h"
+#include "../libcli/auth/libcli_auth.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_PASSDB
@@ -97,10 +98,10 @@ 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 );
-                       
+
                default:
                        DEBUG(0,("get_userattr_key2string: unknown schema version specified\n"));
                        break;
@@ -117,14 +118,14 @@ 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:
                        DEBUG(0,("get_userattr_list: unknown schema version specified!\n"));
                        break;
        }
-       
+
        return NULL;
 }
 
@@ -139,7 +140,7 @@ static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
                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 );
@@ -147,7 +148,7 @@ static const char** get_userattr_delete_list( TALLOC_CTX *mem_ctx,
                        DEBUG(0,("get_userattr_delete_list: unknown schema version specified!\n"));
                        break;
        }
-       
+
        return NULL;
 }
 
@@ -161,7 +162,7 @@ static const char* get_objclass_filter( int schema_ver )
 {
        fstring objclass_filter;
        char *result;
-       
+
        switch( schema_ver ) {
                case SCHEMAVER_SAMBAACCOUNT:
                        fstr_sprintf( objclass_filter, "(objectclass=%s)", LDAP_OBJ_SAMBAACCOUNT );
@@ -174,7 +175,7 @@ static const char* get_objclass_filter( int schema_ver )
                        objclass_filter[0] = '\0';
                        break;
        }
-       
+
        result = talloc_strdup(talloc_tos(), objclass_filter);
        SMB_ASSERT(result != NULL);
        return result;
@@ -335,7 +336,7 @@ int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
                                          const char **attr)
 {
        char *filter = NULL;
-       char *escape_user = escape_ldap_string_alloc(user);
+       char *escape_user = escape_ldap_string(talloc_tos(), user);
        int ret = -1;
 
        if (!escape_user) {
@@ -349,6 +350,7 @@ int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
        filter = talloc_asprintf(talloc_tos(), "(&%s%s)", "(uid=%u)",
                get_objclass_filter(ldap_state->schema_ver));
        if (!filter) {
+               TALLOC_FREE(escape_user);
                return LDAP_NO_MEMORY;
        }
        /*
@@ -358,10 +360,10 @@ int ldapsam_search_suffix_by_name(struct ldapsam_privates *ldap_state,
 
        filter = talloc_all_string_sub(talloc_tos(),
                                filter, "%u", escape_user);
+       TALLOC_FREE(escape_user);
        if (!filter) {
                return LDAP_NO_MEMORY;
        }
-       SAFE_FREE(escape_user);
 
        ret = smbldap_search_suffix(ldap_state->smbldap_state,
                        filter, attr, result);
@@ -407,7 +409,7 @@ static int ldapsam_search_suffix_by_sid (struct ldapsam_privates *ldap_state,
        filter = talloc_asprintf(talloc_tos(), "(&(%s=%s)%s)",
                get_userattr_key2string(ldap_state->schema_ver,
                        LDAP_ATTR_USER_SID),
-               sid_to_string(sid_string, sid),
+               sid_to_fstring(sid_string, sid),
                get_objclass_filter(ldap_state->schema_ver));
        if (!filter) {
                return LDAP_NO_MEMORY;
@@ -446,7 +448,7 @@ static int ldapsam_delete_entry(struct ldapsam_privates *priv,
        }
 
        /* Ok, delete only the SAM attributes */
-       
+
        for (name = ldap_first_attribute(priv2ld(priv), entry, &ptr);
             name != NULL;
             name = ldap_next_attribute(priv2ld(priv), entry, ptr)) {
@@ -558,7 +560,7 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
                goto fn_exit;
        }
 
-       if (!(username = smbldap_talloc_single_attribute(priv2ld(ldap_state),
+       if (!(username = smbldap_talloc_smallest_attribute(priv2ld(ldap_state),
                                        entry,
                                        "uid",
                                        ctx))) {
@@ -839,27 +841,27 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
 
                /* Make call to Novell eDirectory ldap extension to get clear text password.
                        NOTE: This will only work if we have an SSL connection to eDirectory. */
-               user_dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
+               user_dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
                if (user_dn != NULL) {
-                       DEBUG(3, ("init_sam_from_ldap: smbldap_get_dn(%s) returned '%s'\n", username, user_dn));
+                       DEBUG(3, ("init_sam_from_ldap: smbldap_talloc_dn(ctx, %s) returned '%s'\n", username, user_dn));
 
                        pwd_len = sizeof(clear_text_pw);
                        if (pdb_nds_get_password(ldap_state->smbldap_state, user_dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {
                                nt_lm_owf_gen(clear_text_pw, smbntpwd, smblmpwd);
                                if (!pdb_set_lanman_passwd(sampass, smblmpwd, PDB_SET)) {
-                                       SAFE_FREE(user_dn);
+                                       TALLOC_FREE(user_dn);
                                        return False;
                                }
                                ZERO_STRUCT(smblmpwd);
                                if (!pdb_set_nt_passwd(sampass, smbntpwd, PDB_SET)) {
-                                       SAFE_FREE(user_dn);
+                                       TALLOC_FREE(user_dn);
                                        return False;
                                }
                                ZERO_STRUCT(smbntpwd);
                                use_samba_attrs = False;
                        }
 
-                       SAFE_FREE(user_dn);
+                       TALLOC_FREE(user_dn);
 
                } else {
                        DEBUG(0, ("init_sam_from_ldap: failed to get user_dn for '%s'\n", username));
@@ -900,7 +902,7 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
 
        pwHistLen = 0;
 
-       pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
+       pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
        if (pwHistLen > 0){
                uint8 *pwhist = NULL;
                int i;
@@ -945,7 +947,7 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
                                }
                        }
                        if (hex_failed) {
-                               DEBUG(0,("init_sam_from_ldap: Failed to get password history for user %s\n",
+                               DEBUG(2,("init_sam_from_ldap: Failed to get password history for user %s\n",
                                        username));
                                memset(pwhist, '\0', pwHistLen * PW_HISTORY_ENTRY_LEN);
                        }
@@ -1135,8 +1137,9 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
                        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_static(user_sid),
-                                                 sid_string_static(&ldap_state->domain_sid)));
+                                                 sid_string_dbg(user_sid),
+                                                 sid_string_dbg(
+                                                         &ldap_state->domain_sid)));
                                        return False;
                                }
                                if (asprintf(&temp, "%i", rid) < 0) {
@@ -1151,7 +1154,7 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
                        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), 
-                                       sid_to_string(sid_string, user_sid));
+                                       sid_to_fstring(sid_string, user_sid));
                                break;
 
                        default:
@@ -1171,8 +1174,9 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
                        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_static(group_sid),
-                                                 sid_string_static(&ldap_state->domain_sid)));
+                                                 sid_string_dbg(group_sid),
+                                                 sid_string_dbg(
+                                                         &ldap_state->domain_sid)));
                                        return False;
                                }
 
@@ -1188,7 +1192,7 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
                        case SCHEMAVER_SAMBASAMACCOUNT:
                                smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
                                        get_userattr_key2string(ldap_state->schema_ver, 
-                                       LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_string(sid_string, group_sid));
+                                       LDAP_ATTR_PRIMARY_GROUP_SID), sid_to_fstring(sid_string, group_sid));
                                break;
 
                        default:
@@ -1246,7 +1250,7 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
                        get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_PROFILE_PATH), 
                        pdb_get_profile_path(sampass));
 
-       if (asprintf(&temp, "%li", pdb_get_logon_time(sampass)) < 0) {
+       if (asprintf(&temp, "%li", (long int)pdb_get_logon_time(sampass)) < 0) {
                return false;
        }
        if (need_update(sampass, PDB_LOGONTIME))
@@ -1254,7 +1258,7 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
                        get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGON_TIME), temp);
        SAFE_FREE(temp);
 
-       if (asprintf(&temp, "%li", pdb_get_logoff_time(sampass)) < 0) {
+       if (asprintf(&temp, "%li", (long int)pdb_get_logoff_time(sampass)) < 0) {
                return false;
        }
        if (need_update(sampass, PDB_LOGOFFTIME))
@@ -1262,7 +1266,7 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
                        get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_LOGOFF_TIME), temp);
        SAFE_FREE(temp);
 
-       if (asprintf(&temp, "%li", pdb_get_kickoff_time(sampass)) < 0) {
+       if (asprintf(&temp, "%li", (long int)pdb_get_kickoff_time(sampass)) < 0) {
                return false;
        }
        if (need_update(sampass, PDB_KICKOFFTIME))
@@ -1270,7 +1274,7 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
                        get_userattr_key2string(ldap_state->schema_ver, LDAP_ATTR_KICKOFF_TIME), temp);
        SAFE_FREE(temp);
 
-       if (asprintf(&temp, "%li", pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
+       if (asprintf(&temp, "%li", (long int)pdb_get_pass_can_change_time_noncalc(sampass)) < 0) {
                return false;
        }
        if (need_update(sampass, PDB_CANCHANGETIME))
@@ -1278,7 +1282,7 @@ 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", pdb_get_pass_must_change_time(sampass)) < 0) {
+       if (asprintf(&temp, "%li", (long int)pdb_get_pass_must_change_time(sampass)) < 0) {
                return false;
        }
        if (need_update(sampass, PDB_MUSTCHANGETIME))
@@ -1323,7 +1327,7 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
                if (need_update(sampass, PDB_PWHISTORY)) {
                        char *pwstr = NULL;
                        uint32 pwHistLen = 0;
-                       pdb_get_account_policy(AP_PASSWORD_HISTORY, &pwHistLen);
+                       pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
 
                        pwstr = SMB_MALLOC_ARRAY(char, 1024);
                        if (!pwstr) {
@@ -1358,7 +1362,7 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
 
                if (need_update(sampass, PDB_PASSLASTSET)) {
                        if (asprintf(&temp, "%li",
-                               pdb_get_pass_last_set_time(sampass)) < 0) {
+                               (long int)pdb_get_pass_last_set_time(sampass)) < 0) {
                                return false;
                        }
                        smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, existing, mods,
@@ -1400,7 +1404,7 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
                uint16 badcount = pdb_get_bad_password_count(sampass);
                time_t badtime = pdb_get_bad_password_time(sampass);
                uint32 pol;
-               pdb_get_account_policy(AP_BAD_ATTEMPT_LOCKOUT, &pol);
+               pdb_get_account_policy(PDB_POLICY_BAD_ATTEMPT_LOCKOUT, &pol);
 
                DEBUG(3, ("updating bad password fields, policy=%u, count=%u, time=%u\n",
                        (unsigned int)pol, (unsigned int)badcount, (unsigned int)badtime));
@@ -1420,7 +1424,7 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
                                temp);
                        SAFE_FREE(temp);
 
-                       if (asprintf(&temp, "%li", badtime) < 0) {
+                       if (asprintf(&temp, "%li", (long int)badtime) < 0) {
                                return false;
                        }
                        smbldap_make_mod(
@@ -1451,79 +1455,6 @@ static bool init_ldap_from_sam (struct ldapsam_privates *ldap_state,
        return True;
 }
 
-/**********************************************************************
- Connect to LDAP server for password enumeration.
-*********************************************************************/
-
-static NTSTATUS ldapsam_setsampwent(struct pdb_methods *my_methods, bool update, uint32 acb_mask)
-{
-       struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
-       int rc;
-       char *filter = NULL;
-       const char *suffix;
-       const char **attr_list;
-       bool machine_mask = False, user_mask = False;
-       NTSTATUS status = NT_STATUS_OK;
-       TALLOC_CTX *ctx = talloc_init("ldapsam_setsampwent");
-
-       if (!ctx) {
-               return NT_STATUS_NO_MEMORY;
-       }
-       filter = talloc_asprintf(ctx, "(&%s%s)", "(uid=%u)",
-               get_objclass_filter(ldap_state->schema_ver));
-       if (!filter) {
-               status = NT_STATUS_NO_MEMORY;
-               goto out;
-       }
-
-       filter = talloc_all_string_sub(ctx, filter, "%u", "*");
-       if (!filter) {
-               status = NT_STATUS_NO_MEMORY;
-               goto out;
-       }
-
-       machine_mask    = ((acb_mask != 0) && (acb_mask & (ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)));
-       user_mask       = ((acb_mask != 0) && (acb_mask & ACB_NORMAL));
-
-       if (machine_mask) {
-               suffix = lp_ldap_machine_suffix();
-       } else if (user_mask) {
-               suffix = lp_ldap_user_suffix();
-       } else {
-               suffix = lp_ldap_suffix();
-       }
-
-       DEBUG(10,("ldapsam_setsampwent: LDAP Query for acb_mask 0x%x will use suffix %s\n", 
-               acb_mask, suffix));
-
-       attr_list = get_userattr_list(NULL, ldap_state->schema_ver);
-       rc = smbldap_search(ldap_state->smbldap_state, suffix, LDAP_SCOPE_SUBTREE, filter, 
-                           attr_list, 0, &ldap_state->result);
-       TALLOC_FREE( attr_list );
-
-       if (rc != LDAP_SUCCESS) {
-               DEBUG(0, ("ldapsam_setsampwent: LDAP search failed: %s\n", ldap_err2string(rc)));
-               DEBUG(3, ("ldapsam_setsampwent: Query was: %s, %s\n", suffix, filter));
-               ldap_msgfree(ldap_state->result);
-               ldap_state->result = NULL;
-               status = NT_STATUS_UNSUCCESSFUL;
-               goto out;
-       }
-
-       DEBUG(2, ("ldapsam_setsampwent: %d entries in the base %s\n",
-               ldap_count_entries(ldap_state->smbldap_state->ldap_struct,
-               ldap_state->result), suffix));
-
-       ldap_state->entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct,
-                                ldap_state->result);
-       ldap_state->index = 0;
-
-  out:
-
-       TALLOC_FREE(ctx);
-       return status;
-}
-
 /**********************************************************************
  End enumeration of the LDAP password list.
 *********************************************************************/
@@ -1537,32 +1468,6 @@ static void ldapsam_endsampwent(struct pdb_methods *my_methods)
        }
 }
 
-/**********************************************************************
-Get the next entry in the LDAP password database.
-*********************************************************************/
-
-static NTSTATUS ldapsam_getsampwent(struct pdb_methods *my_methods,
-                                   struct samu *user)
-{
-       NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
-       struct ldapsam_privates *ldap_state =
-               (struct ldapsam_privates *)my_methods->private_data;
-       bool bret = False;
-
-       while (!bret) {
-               if (!ldap_state->entry)
-                       return ret;
-               
-               ldap_state->index++;
-               bret = init_sam_from_ldap(ldap_state, user, ldap_state->entry);
-               
-               ldap_state->entry = ldap_next_entry(priv2ld(ldap_state),
-                                                   ldap_state->entry); 
-       }
-
-       return NT_STATUS_OK;
-}
-
 static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
                        const char *new_attr)
 {
@@ -1596,7 +1501,7 @@ static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu
        int count;
        const char ** attr_list;
        int rc;
-       
+
        attr_list = get_userattr_list( user, ldap_state->schema_ver );
        append_attr(user, &attr_list,
                    get_userattr_key2string(ldap_state->schema_ver,
@@ -1608,9 +1513,9 @@ static NTSTATUS ldapsam_getsampwnam(struct pdb_methods *my_methods, struct samu
 
        if ( rc != LDAP_SUCCESS ) 
                return NT_STATUS_NO_SUCH_USER;
-       
+
        count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
-       
+
        if (count < 1) {
                DEBUG(4, ("ldapsam_getsampwnam: Unable to locate user [%s] count=%d\n", sname, count));
                ldap_msgfree(result);
@@ -1667,12 +1572,12 @@ static int ldapsam_get_ldap_user_by_sid(struct ldapsam_privates *ldap_state,
                                return rc;
                        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 );
@@ -1696,7 +1601,6 @@ static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu
        LDAPMessage *entry = NULL;
        int count;
        int rc;
-       fstring sid_string;
 
        rc = ldapsam_get_ldap_user_by_sid(ldap_state, 
                                          sid, &result); 
@@ -1704,15 +1608,16 @@ static NTSTATUS ldapsam_getsampwsid(struct pdb_methods *my_methods, struct samu
                return NT_STATUS_NO_SUCH_USER;
 
        count = ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result);
-       
+
        if (count < 1) {
-               DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] count=%d\n", sid_to_string(sid_string, sid),
-                      count));
+               DEBUG(4, ("ldapsam_getsampwsid: Unable to locate SID [%s] "
+                         "count=%d\n", sid_string_dbg(sid), count));
                ldap_msgfree(result);
                return NT_STATUS_NO_SUCH_USER;
        }  else if (count > 1) {
-               DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID [%s]. Failing. count=%d\n", sid_to_string(sid_string, sid),
-                      count));
+               DEBUG(1, ("ldapsam_getsampwsid: More than one user with SID "
+                         "[%s]. Failing. count=%d\n", sid_string_dbg(sid),
+                         count));
                ldap_msgfree(result);
                return NT_STATUS_NO_SUCH_USER;
        }
@@ -1747,11 +1652,11 @@ static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
 {
        struct ldapsam_privates *ldap_state = (struct ldapsam_privates *)my_methods->private_data;
        int rc;
-       
+
        if (!newpwd || !dn) {
                return NT_STATUS_INVALID_PARAMETER;
        }
-       
+
        if (!mods) {
                DEBUG(5,("ldapsam_modify_entry: mods is empty: nothing to modify\n"));
                /* may be password change below however */
@@ -1779,12 +1684,12 @@ static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
                                         ldap_op));
                                return NT_STATUS_INVALID_PARAMETER;
                }
-               
+
                if (rc!=LDAP_SUCCESS) {
                        return NT_STATUS_UNSUCCESSFUL;
                }  
        }
-       
+
        if (!(pdb_get_acct_ctrl(newpwd)&(ACB_WSTRUST|ACB_SVRTRUST|ACB_DOMTRUST)) &&
                        (lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_OFF) &&
                        need_update(newpwd, PDB_PLAINTEXT_PW) &&
@@ -1795,6 +1700,8 @@ static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
                struct berval *retdata = NULL;
                char *utf8_password;
                char *utf8_dn;
+               size_t converted_size;
+               int ret;
 
                if (!ldap_state->is_nds_ldap) {
 
@@ -1806,37 +1713,63 @@ static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
                        }
                }
 
-               if (push_utf8_allocate(&utf8_password, pdb_get_plaintext_passwd(newpwd)) == (size_t)-1) {
+               if (!push_utf8_talloc(talloc_tos(), &utf8_password,
+                                       pdb_get_plaintext_passwd(newpwd),
+                                       &converted_size))
+               {
                        return NT_STATUS_NO_MEMORY;
                }
 
-               if (push_utf8_allocate(&utf8_dn, dn) == (size_t)-1) {
-                       SAFE_FREE(utf8_password);
+               if (!push_utf8_talloc(talloc_tos(), &utf8_dn, dn, &converted_size)) {
+                       TALLOC_FREE(utf8_password);
                        return NT_STATUS_NO_MEMORY;
                }
 
                if ((ber = ber_alloc_t(LBER_USE_DER))==NULL) {
                        DEBUG(0,("ber_alloc_t returns NULL\n"));
-                       SAFE_FREE(utf8_password);
-                       SAFE_FREE(utf8_dn);
+                       TALLOC_FREE(utf8_password);
+                       TALLOC_FREE(utf8_dn);
+                       return NT_STATUS_UNSUCCESSFUL;
+               }
+
+               if ((ber_printf (ber, "{") < 0) ||
+                   (ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
+                                utf8_dn) < 0)) {
+                       DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
+                                "value <0\n"));
+                       ber_free(ber,1);
+                       TALLOC_FREE(utf8_dn);
+                       TALLOC_FREE(utf8_password);
                        return NT_STATUS_UNSUCCESSFUL;
                }
 
-               ber_printf (ber, "{");
-               ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_ID, utf8_dn);
-               ber_printf (ber, "ts", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, utf8_password);
-               ber_printf (ber, "n}");
+               if ((utf8_password != NULL) && (*utf8_password != '\0')) {
+                       ret = ber_printf(ber, "ts}",
+                                        LDAP_TAG_EXOP_MODIFY_PASSWD_NEW,
+                                        utf8_password);
+               } else {
+                       ret = ber_printf(ber, "}");
+               }
+
+               if (ret < 0) {
+                       DEBUG(0,("ldapsam_modify_entry: ber_printf returns a "
+                                "value <0\n"));
+                       ber_free(ber,1);
+                       TALLOC_FREE(utf8_dn);
+                       TALLOC_FREE(utf8_password);
+                       return NT_STATUS_UNSUCCESSFUL;
+               }
 
                if ((rc = ber_flatten (ber, &bv))<0) {
                        DEBUG(0,("ldapsam_modify_entry: ber_flatten returns a value <0\n"));
                        ber_free(ber,1);
-                       SAFE_FREE(utf8_dn);
-                       SAFE_FREE(utf8_password);
+                       TALLOC_FREE(utf8_dn);
+                       TALLOC_FREE(utf8_password);
                        return NT_STATUS_UNSUCCESSFUL;
                }
-               
-               SAFE_FREE(utf8_dn);
-               SAFE_FREE(utf8_password);
+
+               TALLOC_FREE(utf8_dn);
+               TALLOC_FREE(utf8_password);
                ber_free(ber, 1);
 
                if (!ldap_state->is_nds_ldap) {
@@ -1865,6 +1798,10 @@ static NTSTATUS ldapsam_modify_entry(struct pdb_methods *my_methods,
                                pdb_get_username(newpwd), ldap_err2string(rc), ld_error?ld_error:"unknown"));
                        SAFE_FREE(ld_error);
                        ber_bvfree(bv);
+#if defined(LDAP_CONSTRAINT_VIOLATION)
+                       if (rc == LDAP_CONSTRAINT_VIOLATION)
+                               return NT_STATUS_PASSWORD_RESTRICTION;
+#endif
                        return NT_STATUS_UNSUCCESSFUL;
                } else {
                        DEBUG(3,("ldapsam_modify_entry: LDAP Password changed for user %s\n",pdb_get_username(newpwd)));
@@ -1927,7 +1864,7 @@ static NTSTATUS ldapsam_delete_sam_account(struct pdb_methods *my_methods,
                result = NT_STATUS_NO_SUCH_USER;
                goto done;
        }
-       
+
        rc = ldapsam_delete_entry(
                priv, mem_ctx, entry,
                priv->schema_ver == SCHEMAVER_SAMBASAMACCOUNT ?
@@ -1990,7 +1927,7 @@ static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struc
        }
 
        entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);
-       dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
+       dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
        if (!dn) {
                return NT_STATUS_UNSUCCESSFUL;
        }
@@ -2000,22 +1937,27 @@ static NTSTATUS ldapsam_update_sam_account(struct pdb_methods *my_methods, struc
        if (!init_ldap_from_sam(ldap_state, entry, &mods, newpwd,
                                element_is_changed)) {
                DEBUG(0, ("ldapsam_update_sam_account: init_ldap_from_sam failed!\n"));
-               SAFE_FREE(dn);
+               TALLOC_FREE(dn);
                if (mods != NULL)
                        ldap_mods_free(mods,True);
                return NT_STATUS_UNSUCCESSFUL;
        }
-       
-       if (mods == NULL) {
+
+       if ((lp_ldap_passwd_sync() != LDAP_PASSWD_SYNC_ONLY)
+           && (mods == NULL)) {
                DEBUG(4,("ldapsam_update_sam_account: mods is empty: nothing to update for user: %s\n",
                         pdb_get_username(newpwd)));
-               SAFE_FREE(dn);
+               TALLOC_FREE(dn);
                return NT_STATUS_OK;
        }
-       
+
        ret = ldapsam_modify_entry(my_methods,newpwd,dn,mods,LDAP_MOD_REPLACE, element_is_changed);
-       ldap_mods_free(mods,True);
-       SAFE_FREE(dn);
+
+       if (mods != NULL) {
+               ldap_mods_free(mods,True);
+       }
+
+       TALLOC_FREE(dn);
 
        /*
         * We need to set the backend private data to NULL here. For example
@@ -2066,7 +2008,7 @@ static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
 
        /* rename the posix user */
        rename_script = SMB_STRDUP(lp_renameuser_script());
-       if (rename_script) {
+       if (rename_script == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -2091,7 +2033,7 @@ static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
                                        newname_lower,
                                        true,
                                        true);
-       if (rename_script) {
+       if (!rename_script) {
                return NT_STATUS_NO_MEMORY;
        }
        rename_script = realloc_string_sub2(rename_script,
@@ -2148,7 +2090,6 @@ static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct s
        const DOM_SID   *sid = pdb_get_user_sid(newpwd);
        char *filter = NULL;
        char *dn = NULL;
-       fstring         sid_string;
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
        TALLOC_CTX *ctx = talloc_init("ldapsam_add_sam_account");
 
@@ -2184,8 +2125,9 @@ static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct s
                                                  sid, &result);
                if (rc == LDAP_SUCCESS) {
                        if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) != 0) {
-                               DEBUG(0,("ldapsam_add_sam_account: SID '%s' already in the base, with samba attributes\n", 
-                                        sid_to_string(sid_string, sid)));
+                               DEBUG(0,("ldapsam_add_sam_account: SID '%s' "
+                                        "already in the base, with samba "
+                                        "attributes\n", sid_string_dbg(sid)));
                                goto fn_exit;
                        }
                        ldap_msgfree(result);
@@ -2196,18 +2138,18 @@ static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct s
        /* does the entry already exist but without a samba attributes?
           we need to return the samba attributes here */
 
-       escape_user = escape_ldap_string_alloc( username );
+       escape_user = escape_ldap_string(talloc_tos(), username);
        filter = talloc_strdup(attr_list, "(uid=%u)");
        if (!filter) {
                status = NT_STATUS_NO_MEMORY;
                goto fn_exit;
        }
        filter = talloc_all_string_sub(attr_list, filter, "%u", escape_user);
+       TALLOC_FREE(escape_user);
        if (!filter) {
                status = NT_STATUS_NO_MEMORY;
                goto fn_exit;
        }
-       SAFE_FREE(escape_user);
 
        rc = smbldap_search_suffix(ldap_state->smbldap_state,
                                   filter, attr_list, &result);
@@ -2224,17 +2166,10 @@ static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct s
 
        /* Check if we need to update an existing entry */
        if (num_result == 1) {
-               char *tmp;
-
                DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
                ldap_op = LDAP_MOD_REPLACE;
                entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
-               tmp = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
-               if (!tmp) {
-                       goto fn_exit;
-               }
-               dn = talloc_asprintf(ctx, "%s", tmp);
-               SAFE_FREE(tmp);
+               dn = smbldap_talloc_dn(ctx, ldap_state->smbldap_state->ldap_struct, entry);
                if (!dn) {
                        status = NT_STATUS_NO_MEMORY;
                        goto fn_exit;
@@ -2248,7 +2183,7 @@ static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct s
                                "(&(%s=%s)(|(objectClass=%s)(objectClass=%s)))",
                                 get_userattr_key2string(ldap_state->schema_ver,
                                         LDAP_ATTR_USER_SID),
-                                sid_to_string(sid_string, sid),
+                                sid_string_talloc(ctx, sid),
                                 LDAP_OBJ_IDMAP_ENTRY,
                                 LDAP_OBJ_SID_ENTRY);
                if (!filter) {
@@ -2277,17 +2212,11 @@ static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct s
 
                /* Check if we need to update an existing entry */
                if (num_result == 1) {
-                       char *tmp;
 
                        DEBUG(3,("ldapsam_add_sam_account: User exists without samba attributes: adding them\n"));
                        ldap_op = LDAP_MOD_REPLACE;
                        entry = ldap_first_entry (ldap_state->smbldap_state->ldap_struct, result);
-                       tmp = smbldap_get_dn (ldap_state->smbldap_state->ldap_struct, entry);
-                       if (!tmp) {
-                               goto fn_exit;
-                       }
-                       dn = talloc_asprintf(ctx, "%s", tmp);
-                       SAFE_FREE(tmp);
+                       dn = smbldap_talloc_dn (ctx, ldap_state->smbldap_state->ldap_struct, entry);
                        if (!dn) {
                                status = NT_STATUS_NO_MEMORY;
                                goto fn_exit;
@@ -2367,7 +2296,6 @@ static NTSTATUS ldapsam_add_sam_account(struct pdb_methods *my_methods, struct s
   fn_exit:
 
        TALLOC_FREE(ctx);
-       SAFE_FREE(escape_user);
        if (result) {
                ldap_msgfree(result);
        }
@@ -2387,7 +2315,7 @@ static int ldapsam_search_one_group (struct ldapsam_privates *ldap_state,
 
        attr_list = get_attr_list(NULL, groupmap_attr_list);
        rc = smbldap_search(ldap_state->smbldap_state,
-                           lp_ldap_group_suffix (), scope,
+                           lp_ldap_suffix (), scope,
                            filter, attr_list, 0, result);
        TALLOC_FREE(attr_list);
 
@@ -2575,11 +2503,12 @@ static NTSTATUS ldapsam_getgrsid(struct pdb_methods *methods, GROUP_MAP *map,
 {
        char *filter = NULL;
        NTSTATUS status;
+       fstring tmp;
 
        if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))",
                LDAP_OBJ_GROUPMAP,
                get_attr_key2string(groupmap_attr_list, LDAP_ATTR_GROUP_SID),
-               sid_string_static(&sid)) < 0) {
+               sid_to_fstring(tmp, &sid)) < 0) {
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -2616,7 +2545,7 @@ static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
                                 const char *name)
 {
        char *filter = NULL;
-       char *escape_name = escape_ldap_string_alloc(name);
+       char *escape_name = escape_ldap_string(talloc_tos(), name);
        NTSTATUS status;
 
        if (!escape_name) {
@@ -2628,11 +2557,11 @@ static NTSTATUS ldapsam_getgrnam(struct pdb_methods *methods, GROUP_MAP *map,
                get_attr_key2string(groupmap_attr_list, LDAP_ATTR_DISPLAY_NAME), escape_name,
                get_attr_key2string(groupmap_attr_list, LDAP_ATTR_CN),
                escape_name) < 0) {
-               SAFE_FREE(escape_name);
+               TALLOC_FREE(escape_name);
                return NT_STATUS_NO_MEMORY;
        }
 
-       SAFE_FREE(escape_name);
+       TALLOC_FREE(escape_name);
        status = ldapsam_getgroup(methods, filter, map);
        SAFE_FREE(filter);
        return status;
@@ -2659,7 +2588,7 @@ static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
 
        if (sid_compare_domain(&sid, domain_sid) != 0) {
                DEBUG(10, ("SID %s is not in expected domain %s\n",
-                          str, sid_string_static(domain_sid)));
+                          str, sid_string_dbg(domain_sid)));
                return False;
        }
 
@@ -2700,13 +2629,13 @@ static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
                                 "(sambaSID=%s))",
                                 LDAP_OBJ_POSIXGROUP,
                                 LDAP_OBJ_GROUPMAP,
-                                sid_string_static(group));
+                                sid_string_talloc(mem_ctx, group));
        if (filter == NULL) {
                ret = NT_STATUS_NO_MEMORY;
                goto done;
        }
 
-       rc = smbldap_search(conn, lp_ldap_group_suffix(),
+       rc = smbldap_search(conn, lp_ldap_suffix(),
                            LDAP_SCOPE_SUBTREE, filter, id_attrs, 0,
                            &result);
 
@@ -2719,7 +2648,7 @@ static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
 
        if (count > 1) {
                DEBUG(1, ("Found more than one groupmap entry for %s\n",
-                         sid_string_static(group)));
+                         sid_string_dbg(group)));
                ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
                goto done;
        }
@@ -2753,20 +2682,19 @@ static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
                for (memberuid = values; *memberuid != NULL; memberuid += 1) {
                        char *escape_memberuid;
 
-                       escape_memberuid = escape_ldap_string_alloc(*memberuid);
+                       escape_memberuid = escape_ldap_string(talloc_tos(),
+                                                             *memberuid);
                        if (escape_memberuid == NULL) {
                                ret = NT_STATUS_NO_MEMORY;
                                goto done;
                        }
-                       
+
                        filter = talloc_asprintf_append_buffer(filter, "(uid=%s)", escape_memberuid);
+                       TALLOC_FREE(escape_memberuid);
                        if (filter == NULL) {
-                               SAFE_FREE(escape_memberuid);
                                ret = NT_STATUS_NO_MEMORY;
                                goto done;
                        }
-
-                       SAFE_FREE(escape_memberuid);
                }
 
                filter = talloc_asprintf_append_buffer(filter, "))");
@@ -2775,7 +2703,7 @@ static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
                        goto done;
                }
 
-               rc = smbldap_search(conn, lp_ldap_user_suffix(),
+               rc = smbldap_search(conn, lp_ldap_suffix(),
                                    LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
                                    &result);
 
@@ -2799,8 +2727,8 @@ static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
                                                                 entry, "sambaSID",
                                                                 mem_ctx);
                        if (!sidstr) {
-                               DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
-                                         "the sambaSID attribute\n"));
+                               DEBUG(0, ("Severe DB error, %s can't miss the sambaSID"
+                                         "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
                                ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
                                goto done;
                        }
@@ -2831,7 +2759,7 @@ static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
                                 LDAP_OBJ_SAMBASAMACCOUNT,
                                 gidstr);
 
-       rc = smbldap_search(conn, lp_ldap_user_suffix(),
+       rc = smbldap_search(conn, lp_ldap_suffix(),
                            LDAP_SCOPE_SUBTREE, filter, sid_attrs, 0,
                            &result);
 
@@ -2850,8 +2778,7 @@ static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
                                                    entry,
                                                    get_global_sam_sid(),
                                                    &rid)) {
-                       DEBUG(0, ("Severe DB error, sambaSamAccount can't miss "
-                                 "the sambaSID attribute\n"));
+                       DEBUG(0, ("Severe DB error, %s can't miss the samba SID"                                                                "attribute\n", LDAP_OBJ_SAMBASAMACCOUNT));
                        ret = NT_STATUS_INTERNAL_DB_CORRUPTION;
                        goto done;
                }
@@ -2864,7 +2791,7 @@ static NTSTATUS ldapsam_enum_group_members(struct pdb_methods *methods,
        }
 
        ret = NT_STATUS_OK;
-       
+
  done:
 
        if (values)
@@ -2901,7 +2828,7 @@ static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       escape_name = escape_ldap_string_alloc(pdb_get_username(user));
+       escape_name = escape_ldap_string(talloc_tos(), pdb_get_username(user));
        if (escape_name == NULL)
                return NT_STATUS_NO_MEMORY;
 
@@ -2915,7 +2842,7 @@ static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
                goto done;
        }
 
-       rc = smbldap_search(conn, lp_ldap_user_suffix(),
+       rc = smbldap_search(conn, lp_ldap_suffix(),
                            LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
 
        if (rc != LDAP_SUCCESS)
@@ -2948,14 +2875,14 @@ static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
        }
 
        filter = talloc_asprintf(mem_ctx,
-                                "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%d)))",
-                                LDAP_OBJ_POSIXGROUP, escape_name, primary_gid);
+                                "(&(objectClass=%s)(|(memberUid=%s)(gidNumber=%u)))",
+                                LDAP_OBJ_POSIXGROUP, escape_name, (unsigned int)primary_gid);
        if (filter == NULL) {
                ret = NT_STATUS_NO_MEMORY;
                goto done;
        }
 
-       rc = smbldap_search(conn, lp_ldap_group_suffix(),
+       rc = smbldap_search(conn, lp_ldap_suffix(),
                            LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
 
        if (rc != LDAP_SUCCESS)
@@ -2978,8 +2905,9 @@ static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
 
        /* This sid will be replaced later */
 
-       if (!add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids, &num_sids)) {
-               ret = NT_STATUS_NO_MEMORY;
+       ret = add_sid_to_array_unique(mem_ctx, &global_sid_NULL, pp_sids,
+                                     &num_sids);
+       if (!NT_STATUS_IS_OK(ret)) {
                goto done;
        }
 
@@ -3018,9 +2946,9 @@ static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
                                ret = NT_STATUS_NO_MEMORY;
                                goto done;
                        }
-                       if (!add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
-                                               &num_sids)) {
-                               ret = NT_STATUS_NO_MEMORY;
+                       ret = add_sid_to_array_unique(mem_ctx, &sid, pp_sids,
+                                                     &num_sids);
+                       if (!NT_STATUS_IS_OK(ret)) {
                                goto done;
                        }
                }
@@ -3038,7 +2966,7 @@ static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
 
  done:
 
-       SAFE_FREE(escape_name);
+       TALLOC_FREE(escape_name);
        return ret;
 }
 
@@ -3056,8 +2984,8 @@ static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
        int rc;
 
        filter = talloc_asprintf(mem_ctx,
-                                "(&(objectClass=posixGroup)(gidNumber=%u))",
-                                map->gid);
+                                "(&(objectClass=%s)(gidNumber=%u))",
+                                LDAP_OBJ_POSIXGROUP, (unsigned int)map->gid);
        if (filter == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -3080,9 +3008,9 @@ static NTSTATUS ldapsam_map_posixgroup(TALLOC_CTX *mem_ctx,
 
        mods = NULL;
        smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass",
-                       "sambaGroupMapping");
+                       LDAP_OBJ_GROUPMAP);
        smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaSid",
-                        sid_string_static(&map->sid));
+                        sid_string_talloc(mem_ctx, &map->sid));
        smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "sambaGroupType",
                         talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
        smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, entry, &mods, "displayName",
@@ -3124,7 +3052,7 @@ static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
        }
 
        filter = talloc_asprintf(mem_ctx, "(sambaSid=%s)",
-                                sid_string_static(&map->sid));
+                                sid_string_talloc(mem_ctx, &map->sid));
        if (filter == NULL) {
                result = NT_STATUS_NO_MEMORY;
                goto done;
@@ -3138,8 +3066,7 @@ static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
            (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, msg) > 0)) {
 
                DEBUG(3, ("SID %s already present in LDAP, refusing to add "
-                         "group mapping entry\n",
-                         sid_string_static(&map->sid)));
+                         "group mapping entry\n", sid_string_dbg(&map->sid)));
                result = NT_STATUS_GROUP_EXISTS;
                goto done;
        }
@@ -3158,7 +3085,7 @@ static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
                        && !sid_check_is_in_builtin(&map->sid) ) 
                {
                        DEBUG(3, ("Refusing to map sid %s as an alias, not in our domain\n",
-                                 sid_string_static(&map->sid)));
+                                 sid_string_dbg(&map->sid)));
                        result = NT_STATUS_INVALID_PARAMETER;
                        goto done;
                }
@@ -3181,8 +3108,8 @@ static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
        }
 
        if (pdb_gid_to_sid(map->gid, &sid)) {
-               DEBUG(3, ("Gid %d is already mapped to SID %s, refusing to "
-                         "add\n", map->gid, sid_string_static(&sid)));
+               DEBUG(3, ("Gid %u is already mapped to SID %s, refusing to "
+                         "add\n", (unsigned int)map->gid, sid_string_dbg(&sid)));
                result = NT_STATUS_GROUP_EXISTS;
                goto done;
        }
@@ -3191,7 +3118,7 @@ static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
         * the best we can get out of LDAP. */
 
        dn = talloc_asprintf(mem_ctx, "sambaSid=%s,%s",
-                            sid_string_static(&map->sid),
+                            sid_string_talloc(mem_ctx, &map->sid),
                             lp_ldap_group_suffix());
        if (dn == NULL) {
                result = NT_STATUS_NO_MEMORY;
@@ -3201,12 +3128,11 @@ static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
        mods = NULL;
 
        smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
-                        "sambaSidEntry");
+                        LDAP_OBJ_SID_ENTRY);
        smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "objectClass",
-                        "sambaGroupMapping");
-
+                        LDAP_OBJ_GROUPMAP);
        smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaSid",
-                        sid_string_static(&map->sid));
+                        sid_string_talloc(mem_ctx, &map->sid));
        smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "sambaGroupType",
                         talloc_asprintf(mem_ctx, "%d", map->sid_name_use));
        smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "displayName",
@@ -3214,7 +3140,7 @@ static NTSTATUS ldapsam_add_group_mapping_entry(struct pdb_methods *methods,
        smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "description",
                         map->comment);
        smbldap_make_mod(ldap_state->smbldap_state->ldap_struct, NULL, &mods, "gidNumber",
-                        talloc_asprintf(mem_ctx, "%u", map->gid));
+                        talloc_asprintf(mem_ctx, "%u", (unsigned int)map->gid));
        talloc_autofree_ldapmod(mem_ctx, mods);
 
        rc = smbldap_add(ldap_state->smbldap_state, dn, mods);
@@ -3259,8 +3185,8 @@ static NTSTATUS ldapsam_update_group_mapping_entry(struct pdb_methods *methods,
                                 "(sambaSid=%s)(gidNumber=%u)"
                                 "(sambaGroupType=%d))",
                                 LDAP_OBJ_GROUPMAP,
-                                sid_string_static(&map->sid), map->gid,
-                                map->sid_name_use);
+                                sid_string_talloc(mem_ctx, &map->sid),
+                                (unsigned int)map->gid, map->sid_name_use);
        if (filter == NULL) {
                result = NT_STATUS_NO_MEMORY;
                goto done;
@@ -3338,7 +3264,7 @@ static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
 
        filter = talloc_asprintf(mem_ctx, "(&(objectClass=%s)(%s=%s))",
                                 LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
-                                sid_string_static(&sid));
+                                sid_string_talloc(mem_ctx, &sid));
        if (filter == NULL) {
                result = NT_STATUS_NO_MEMORY;
                goto done;
@@ -3358,8 +3284,9 @@ static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
        rc = ldapsam_delete_entry(priv, mem_ctx, entry, LDAP_OBJ_GROUPMAP,
                                  get_attr_list(mem_ctx,
                                                groupmap_attr_list_to_delete));
+
        if ((rc == LDAP_NAMING_VIOLATION) ||
+           (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
            (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
                const char *attrs[] = { "sambaGroupType", "description",
                                        "displayName", "sambaSIDList",
@@ -3374,6 +3301,7 @@ static NTSTATUS ldapsam_delete_group_mapping_entry(struct pdb_methods *methods,
        }
 
        if ((rc == LDAP_NAMING_VIOLATION) ||
+           (rc == LDAP_NOT_ALLOWED_ON_RDN) ||
            (rc == LDAP_OBJECT_CLASS_VIOLATION)) {
                const char *attrs[] = { "sambaGroupType", "description",
                                        "displayName", "sambaSIDList",
@@ -3412,7 +3340,7 @@ static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
                return NT_STATUS_NO_MEMORY;
        }
        attr_list = get_attr_list( NULL, groupmap_attr_list );
-       rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
+       rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
                            LDAP_SCOPE_SUBTREE, filter,
                            attr_list, 0, &ldap_state->result);
        TALLOC_FREE(attr_list);
@@ -3421,7 +3349,7 @@ static NTSTATUS ldapsam_setsamgrent(struct pdb_methods *my_methods,
                DEBUG(0, ("ldapsam_setsamgrent: LDAP search failed: %s\n",
                          ldap_err2string(rc)));
                DEBUG(3, ("ldapsam_setsamgrent: Query was: %s, %s\n",
-                         lp_ldap_group_suffix(), filter));
+                         lp_ldap_suffix(), filter));
                ldap_msgfree(ldap_state->result);
                ldap_state->result = NULL;
                TALLOC_FREE(filter);
@@ -3464,11 +3392,11 @@ static NTSTATUS ldapsam_getsamgrent(struct pdb_methods *my_methods,
        while (!bret) {
                if (!ldap_state->entry)
                        return ret;
-               
+
                ldap_state->index++;
                bret = init_group_from_ldap(ldap_state, map,
                                            ldap_state->entry);
-               
+
                ldap_state->entry =
                        ldap_next_entry(ldap_state->smbldap_state->ldap_struct,
                                        ldap_state->entry);     
@@ -3544,6 +3472,7 @@ static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
        LDAPMod **mods = NULL;
        int rc;
        enum lsa_SidType type = SID_NAME_USE_NONE;
+       fstring tmp;
 
        char *filter = NULL;
 
@@ -3557,13 +3486,13 @@ static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
 
        if (type == SID_NAME_USE_NONE) {
                DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
-                         sid_string_static(alias)));
+                         sid_string_dbg(alias)));
                return NT_STATUS_NO_SUCH_ALIAS;
        }
 
        if (asprintf(&filter,
                     "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
-                    LDAP_OBJ_GROUPMAP, sid_string_static(alias),
+                    LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
                     type) < 0) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -3602,7 +3531,7 @@ static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
                return NT_STATUS_UNSUCCESSFUL;
        }
 
-       dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
+       dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
        if (!dn) {
                ldap_msgfree(result);
                return NT_STATUS_UNSUCCESSFUL;
@@ -3611,13 +3540,13 @@ static NTSTATUS ldapsam_modify_aliasmem(struct pdb_methods *methods,
        smbldap_set_mod(&mods, modop,
                        get_attr_key2string(groupmap_attr_list,
                                            LDAP_ATTR_SID_LIST),
-                       sid_string_static(member));
+                       sid_to_fstring(tmp, member));
 
        rc = smbldap_modify(ldap_state->smbldap_state, dn, mods);
 
        ldap_mods_free(mods, True);
        ldap_msgfree(result);
-       SAFE_FREE(dn);
+       TALLOC_FREE(dn);
 
        if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
                return NT_STATUS_MEMBER_IN_ALIAS;
@@ -3651,6 +3580,7 @@ static NTSTATUS ldapsam_del_aliasmem(struct pdb_methods *methods,
 
 static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
                                      const DOM_SID *alias,
+                                     TALLOC_CTX *mem_ctx,
                                      DOM_SID **pp_members,
                                      size_t *p_num_members)
 {
@@ -3664,6 +3594,7 @@ static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
        char *filter = NULL;
        size_t num_members = 0;
        enum lsa_SidType type = SID_NAME_USE_NONE;
+       fstring tmp;
 
        *pp_members = NULL;
        *p_num_members = 0;
@@ -3678,13 +3609,13 @@ static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
 
        if (type == SID_NAME_USE_NONE) {
                DEBUG(5, ("SID %s is neither in builtin nor in our domain!\n",
-                         sid_string_static(alias)));
+                         sid_string_dbg(alias)));
                return NT_STATUS_NO_SUCH_ALIAS;
        }
 
        if (asprintf(&filter,
                     "(&(objectClass=%s)(sambaSid=%s)(sambaGroupType=%d))",
-                    LDAP_OBJ_GROUPMAP, sid_string_static(alias),
+                    LDAP_OBJ_GROUPMAP, sid_to_fstring(tmp, alias),
                     type) < 0) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -3737,14 +3668,17 @@ static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
 
        for (i=0; i<count; i++) {
                DOM_SID member;
+               NTSTATUS status;
 
                if (!string_to_sid(&member, values[i]))
                        continue;
 
-               if (!add_sid_to_array(NULL, &member, pp_members, &num_members)) {
+               status = add_sid_to_array(mem_ctx, &member, pp_members,
+                                         &num_members);
+               if (!NT_STATUS_IS_OK(status)) {
                        ldap_value_free(values);
                        ldap_msgfree(result);
-                       return NT_STATUS_NO_MEMORY;
+                       return status;
                }
        }
 
@@ -3776,6 +3710,9 @@ static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
        char *filter;
        enum lsa_SidType type = SID_NAME_USE_NONE;
 
+       *pp_alias_rids = NULL;
+       *p_num_alias_rids = 0;
+
        if (sid_check_is_builtin(domain_sid)) {
                type = SID_NAME_ALIAS;
        }
@@ -3786,10 +3723,14 @@ static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
 
        if (type == SID_NAME_USE_NONE) {
                DEBUG(5, ("SID %s is neither builtin nor domain!\n",
-                         sid_string_static(domain_sid)));
+                         sid_string_dbg(domain_sid)));
                return NT_STATUS_UNSUCCESSFUL;
        }
 
+       if (num_members == 0) {
+               return NT_STATUS_OK;
+       }
+
        filter = talloc_asprintf(mem_ctx,
                                 "(&(|(objectclass=%s)(sambaGroupType=%d))(|",
                                 LDAP_OBJ_GROUPMAP, type);
@@ -3797,7 +3738,8 @@ static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
        for (i=0; i<num_members; i++)
                filter = talloc_asprintf(mem_ctx, "%s(sambaSIDList=%s)",
                                         filter,
-                                        sid_string_static(&members[i]));
+                                        sid_string_talloc(mem_ctx,
+                                                          &members[i]));
 
        filter = talloc_asprintf(mem_ctx, "%s))", filter);
 
@@ -3805,7 +3747,7 @@ static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
                return NT_STATUS_NO_MEMORY;
        }
 
-       rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_group_suffix(),
+       rc = smbldap_search(ldap_state->smbldap_state, lp_ldap_suffix(),
                            LDAP_SCOPE_SUBTREE, filter, attrs, 0, &result);
 
        if (rc != LDAP_SUCCESS)
@@ -3845,7 +3787,7 @@ static NTSTATUS ldapsam_alias_memberships(struct pdb_methods *methods,
 }
 
 static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
-                                                  int policy_index,
+                                                  enum pdb_policy_type type,
                                                   uint32 value)
 {
        NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
@@ -3863,7 +3805,7 @@ static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       policy_attr = get_account_policy_attr(policy_index);
+       policy_attr = get_account_policy_attr(type);
        if (policy_attr == NULL) {
                DEBUG(0,("ldapsam_set_account_policy_in_ldap: invalid "
                         "policy\n"));
@@ -3883,7 +3825,7 @@ static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
                return ntstatus;
        }
 
-       if (!cache_account_policy_set(policy_index, value)) {
+       if (!cache_account_policy_set(type, value)) {
                DEBUG(0,("ldapsam_set_account_policy_in_ldap: failed to "
                         "update local tdb cache\n"));
                return ntstatus;
@@ -3893,14 +3835,15 @@ static NTSTATUS ldapsam_set_account_policy_in_ldap(struct pdb_methods *methods,
 }
 
 static NTSTATUS ldapsam_set_account_policy(struct pdb_methods *methods,
-                                          int policy_index, uint32 value)
+                                          enum pdb_policy_type type,
+                                          uint32_t value)
 {
-       return ldapsam_set_account_policy_in_ldap(methods, policy_index,
+       return ldapsam_set_account_policy_in_ldap(methods, type,
                                                  value);
 }
 
 static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods,
-                                                    int policy_index,
+                                                    enum pdb_policy_type type,
                                                     uint32 *value)
 {
        NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
@@ -3922,10 +3865,10 @@ static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       policy_attr = get_account_policy_attr(policy_index);
+       policy_attr = get_account_policy_attr(type);
        if (!policy_attr) {
                DEBUG(0,("ldapsam_get_account_policy_from_ldap: invalid "
-                        "policy index: %d\n", policy_index));
+                        "policy index: %d\n", type));
                return ntstatus;
        }
 
@@ -3956,7 +3899,7 @@ static NTSTATUS ldapsam_get_account_policy_from_ldap(struct pdb_methods *methods
        }
 
        *value = (uint32)atol(vals[0]);
-       
+
        ntstatus = NT_STATUS_OK;
 
 out:
@@ -3971,7 +3914,7 @@ out:
 
    - if user hasn't decided to use account policies inside LDAP just reuse the
      old tdb values
-   
+
    - if there is a valid cache entry, return that
    - if there is an LDAP entry, update cache and return 
    - otherwise set to default, update cache and return
@@ -3979,17 +3922,18 @@ out:
    Guenther
 */
 static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
-                                          int policy_index, uint32 *value)
+                                          enum pdb_policy_type type,
+                                          uint32_t *value)
 {
        NTSTATUS ntstatus = NT_STATUS_UNSUCCESSFUL;
 
-       if (cache_account_policy_get(policy_index, value)) {
+       if (cache_account_policy_get(type, value)) {
                DEBUG(11,("ldapsam_get_account_policy: got valid value from "
                          "cache\n"));
                return NT_STATUS_OK;
        }
 
-       ntstatus = ldapsam_get_account_policy_from_ldap(methods, policy_index,
+       ntstatus = ldapsam_get_account_policy_from_ldap(methods, type,
                                                        value);
        if (NT_STATUS_IS_OK(ntstatus)) {
                goto update_cache;
@@ -4000,27 +3944,27 @@ static NTSTATUS ldapsam_get_account_policy(struct pdb_methods *methods,
 
 #if 0
        /* should we automagically migrate old tdb value here ? */
-       if (account_policy_get(policy_index, value))
+       if (account_policy_get(type, value))
                goto update_ldap;
 
        DEBUG(10,("ldapsam_get_account_policy: no tdb for %d, trying "
-                 "default\n", policy_index));
+                 "default\n", type));
 #endif
 
-       if (!account_policy_get_default(policy_index, value)) {
+       if (!account_policy_get_default(type, value)) {
                return ntstatus;
        }
-       
+
 /* update_ldap: */
-       ntstatus = ldapsam_set_account_policy(methods, policy_index, *value);
+
+       ntstatus = ldapsam_set_account_policy(methods, type, *value);
        if (!NT_STATUS_IS_OK(ntstatus)) {
                return ntstatus;
        }
-               
+
  update_cache:
-       if (!cache_account_policy_set(policy_index, *value)) {
+
+       if (!cache_account_policy_set(type, *value)) {
                DEBUG(0,("ldapsam_get_account_policy: failed to update local "
                         "tdb as a cache\n"));
                return NT_STATUS_UNSUCCESSFUL;
@@ -4059,6 +4003,11 @@ static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
                goto done;
        }
 
+       if (num_rids == 0) {
+               result = NT_STATUS_NONE_MAPPED;
+               goto done;
+       }
+
        for (i=0; i<num_rids; i++)
                attrs[i] = SID_NAME_UNKNOWN;
 
@@ -4070,8 +4019,9 @@ static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
        for (i=0; i<num_rids; i++) {
                DOM_SID sid;
                sid_compose(&sid, domain_sid, rids[i]);
-               allsids = talloc_asprintf_append_buffer(allsids, "(sambaSid=%s)",
-                                                sid_string_static(&sid));
+               allsids = talloc_asprintf_append_buffer(
+                       allsids, "(sambaSid=%s)",
+                       sid_string_talloc(mem_ctx, &sid));
                if (allsids == NULL) {
                        goto done;
                }
@@ -4160,7 +4110,7 @@ static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
                }
 
                rc = smbldap_search(ldap_state->smbldap_state,
-                                   lp_ldap_group_suffix(),
+                                   lp_ldap_suffix(),
                                    LDAP_SCOPE_SUBTREE, filter, ldap_attrs, 0,
                                    &msg);
                talloc_autofree_ldapmsg(mem_ctx, msg);
@@ -4255,18 +4205,19 @@ static char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
        char *escaped = NULL;
        char *result = NULL;
 
-       asprintf(&filter, "(&%s(objectclass=sambaSamAccount))",
-                "(uid=%u)");
-       if (filter == NULL) goto done;
+       if (asprintf(&filter, "(&%s(objectclass=%s))",
+                         "(uid=%u)", LDAP_OBJ_SAMBASAMACCOUNT) < 0) {
+               goto done;
+       }
 
-       escaped = escape_ldap_string_alloc(username);
+       escaped = escape_ldap_string(talloc_tos(), username);
        if (escaped == NULL) goto done;
 
        result = talloc_string_sub(mem_ctx, filter, "%u", username);
 
  done:
        SAFE_FREE(filter);
-       SAFE_FREE(escaped);
+       TALLOC_FREE(escaped);
 
        return result;
 }
@@ -4291,6 +4242,7 @@ const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
                result[i] = talloc_strdup(result, va_arg(ap, const char*));
                if (result[i] == NULL) {
                        talloc_free(result);
+                       va_end(ap);
                        return NULL;
                }
        }
@@ -4418,7 +4370,8 @@ static bool ldapsam_search_next_entry(struct pdb_search *search,
            !ldapsam_search_nextpage(search))
                    return False;
 
-       result = state->ldap2displayentry(state, search->mem_ctx, state->connection->ldap_struct,
+       result = state->ldap2displayentry(state, search,
+                                         state->connection->ldap_struct,
                                          state->current_entry, entry);
 
        if (!result) {
@@ -4477,6 +4430,7 @@ static bool ldapuser2displayentry(struct ldap_search_state *state,
                                  struct samr_displayentry *result)
 {
        char **vals;
+       size_t converted_size;
        DOM_SID sid;
        uint32 acct_flags;
 
@@ -4502,27 +4456,40 @@ static bool ldapuser2displayentry(struct ldap_search_state *state,
                DEBUG(5, ("\"uid\" not found\n"));
                return False;
        }
-       pull_utf8_talloc(mem_ctx,
-                        CONST_DISCARD(char **, &result->account_name),
-                        vals[0]);
+       if (!pull_utf8_talloc(mem_ctx,
+                             CONST_DISCARD(char **, &result->account_name),
+                             vals[0], &converted_size))
+       {
+               DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
+                        strerror(errno)));
+       }
+
        ldap_value_free(vals);
 
        vals = ldap_get_values(ld, entry, "displayName");
        if ((vals == NULL) || (vals[0] == NULL))
                DEBUG(8, ("\"displayName\" not found\n"));
-       else
-               pull_utf8_talloc(mem_ctx,
-                                CONST_DISCARD(char **, &result->fullname),
-                                vals[0]);
+       else if (!pull_utf8_talloc(mem_ctx,
+                                  CONST_DISCARD(char **, &result->fullname),
+                                  vals[0], &converted_size))
+       {
+               DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
+                        strerror(errno)));
+       }
+
        ldap_value_free(vals);
 
        vals = ldap_get_values(ld, entry, "description");
        if ((vals == NULL) || (vals[0] == NULL))
                DEBUG(8, ("\"description\" not found\n"));
-       else
-               pull_utf8_talloc(mem_ctx,
-                                CONST_DISCARD(char **, &result->description),
-                                vals[0]);
+       else if (!pull_utf8_talloc(mem_ctx,
+                                  CONST_DISCARD(char **, &result->description),
+                                  vals[0], &converted_size))
+       {
+               DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
+                        strerror(errno)));
+       }
+
        ldap_value_free(vals);
 
        if ((result->account_name == NULL) ||
@@ -4531,7 +4498,7 @@ static bool ldapuser2displayentry(struct ldap_search_state *state,
                DEBUG(0, ("talloc failed\n"));
                return False;
        }
-       
+
        vals = ldap_get_values(ld, entry, "sambaSid");
        if ((vals == NULL) || (vals[0] == NULL)) {
                DEBUG(0, ("\"objectSid\" not found\n"));
@@ -4547,7 +4514,7 @@ static bool ldapuser2displayentry(struct ldap_search_state *state,
 
        if (!sid_peek_check_rid(get_global_sam_sid(), &sid, &result->rid)) {
                DEBUG(0, ("sid %s does not belong to our domain\n",
-                         sid_string_static(&sid)));
+                         sid_string_dbg(&sid)));
                return False;
        }
 
@@ -4563,7 +4530,7 @@ static bool ldapsam_search_users(struct pdb_methods *methods,
                (struct ldapsam_privates *)methods->private_data;
        struct ldap_search_state *state;
 
-       state = TALLOC_P(search->mem_ctx, struct ldap_search_state);
+       state = talloc(search, struct ldap_search_state);
        if (state == NULL) {
                DEBUG(0, ("talloc failed\n"));
                return False;
@@ -4580,10 +4547,10 @@ static bool ldapsam_search_users(struct pdb_methods *methods,
                state->base = lp_ldap_suffix();
 
        state->acct_flags = acct_flags;
-       state->base = talloc_strdup(search->mem_ctx, state->base);
+       state->base = talloc_strdup(search, state->base);
        state->scope = LDAP_SCOPE_SUBTREE;
-       state->filter = get_ldap_filter(search->mem_ctx, "*");
-       state->attrs = talloc_attrs(search->mem_ctx, "uid", "sambaSid",
+       state->filter = get_ldap_filter(search, "*");
+       state->attrs = talloc_attrs(search, "uid", "sambaSid",
                                    "displayName", "description",
                                    "sambaAcctFlags", NULL);
        state->attrsonly = 0;
@@ -4609,6 +4576,7 @@ static bool ldapgroup2displayentry(struct ldap_search_state *state,
                                   struct samr_displayentry *result)
 {
        char **vals;
+       size_t converted_size;
        DOM_SID sid;
        uint16 group_type;
 
@@ -4648,14 +4616,22 @@ static bool ldapgroup2displayentry(struct ldap_search_state *state,
                        DEBUG(5, ("\"cn\" not found\n"));
                        return False;
                }
-               pull_utf8_talloc(mem_ctx,
-                                CONST_DISCARD(char **, &result->account_name),
-                                vals[0]);
+               if (!pull_utf8_talloc(mem_ctx,
+                                     CONST_DISCARD(char **,
+                                                   &result->account_name),
+                                     vals[0], &converted_size))
+               {
+                       DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc "
+                                 "failed: %s", strerror(errno)));
+               }
        }
-       else {
-               pull_utf8_talloc(mem_ctx,
-                                CONST_DISCARD(char **, &result->account_name),
-                                vals[0]);
+       else if (!pull_utf8_talloc(mem_ctx,
+                                  CONST_DISCARD(char **,
+                                                &result->account_name),
+                                  vals[0], &converted_size))
+       {
+               DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
+                         strerror(errno)));
        }
 
        ldap_value_free(vals);
@@ -4663,10 +4639,13 @@ static bool ldapgroup2displayentry(struct ldap_search_state *state,
        vals = ldap_get_values(ld, entry, "description");
        if ((vals == NULL) || (vals[0] == NULL))
                DEBUG(8, ("\"description\" not found\n"));
-       else
-               pull_utf8_talloc(mem_ctx,
-                                CONST_DISCARD(char **, &result->description),
-                                vals[0]);
+       else if (!pull_utf8_talloc(mem_ctx,
+                                  CONST_DISCARD(char **, &result->description),
+                                  vals[0], &converted_size))
+       {
+               DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
+                         strerror(errno)));
+       }
        ldap_value_free(vals);
 
        if ((result->account_name == NULL) ||
@@ -4675,7 +4654,7 @@ static bool ldapgroup2displayentry(struct ldap_search_state *state,
                DEBUG(0, ("talloc failed\n"));
                return False;
        }
-       
+
        vals = ldap_get_values(ld, entry, "sambaSid");
        if ((vals == NULL) || (vals[0] == NULL)) {
                DEBUG(0, ("\"objectSid\" not found\n"));
@@ -4700,16 +4679,18 @@ static bool ldapgroup2displayentry(struct ldap_search_state *state,
                                && !sid_peek_check_rid(&global_sid_Builtin, &sid, &result->rid)) 
                        {
                                DEBUG(0, ("%s is not in our domain\n",
-                                         sid_string_static(&sid)));
+                                         sid_string_dbg(&sid)));
                                return False;
                        }
                        break;
-       
+
                default:
                        DEBUG(0,("unkown group type: %d\n", group_type));
                        return False;
        }
-       
+
+       result->acct_flags = 0;
+
        return True;
 }
 
@@ -4721,8 +4702,9 @@ static bool ldapsam_search_grouptype(struct pdb_methods *methods,
        struct ldapsam_privates *ldap_state =
                (struct ldapsam_privates *)methods->private_data;
        struct ldap_search_state *state;
+       fstring tmp;
 
-       state = TALLOC_P(search->mem_ctx, struct ldap_search_state);
+       state = talloc(search, struct ldap_search_state);
        if (state == NULL) {
                DEBUG(0, ("talloc failed\n"));
                return False;
@@ -4730,14 +4712,14 @@ static bool ldapsam_search_grouptype(struct pdb_methods *methods,
 
        state->connection = ldap_state->smbldap_state;
 
-       state->base = talloc_strdup(search->mem_ctx, lp_ldap_group_suffix());
+       state->base = talloc_strdup(search, lp_ldap_suffix());
        state->connection = ldap_state->smbldap_state;
        state->scope = LDAP_SCOPE_SUBTREE;
-       state->filter = talloc_asprintf(search->mem_ctx,
-                                       "(&(objectclass=sambaGroupMapping)"
-                                       "(sambaGroupType=%d)(sambaSID=%s*))", 
-                                       type, sid_string_static(sid));
-       state->attrs = talloc_attrs(search->mem_ctx, "cn", "sambaSid",
+       state->filter = talloc_asprintf(search, "(&(objectclass=%s)"
+                                       "(sambaGroupType=%d)(sambaSID=%s*))",
+                                        LDAP_OBJ_GROUPMAP,
+                                        type, sid_to_fstring(tmp, sid));
+       state->attrs = talloc_attrs(search, "cn", "sambaSid",
                                    "displayName", "description",
                                    "sambaGroupType", NULL);
        state->attrsonly = 0;
@@ -4771,9 +4753,9 @@ static bool ldapsam_search_aliases(struct pdb_methods *methods,
        return ldapsam_search_grouptype(methods, search, sid, SID_NAME_ALIAS);
 }
 
-static bool ldapsam_rid_algorithm(struct pdb_methods *methods)
+static uint32_t ldapsam_capabilities(struct pdb_methods *methods)
 {
-       return False;
+       return PDB_CAP_STORE_RIDS;
 }
 
 static NTSTATUS ldapsam_get_new_rid(struct ldapsam_privates *priv,
@@ -4927,7 +4909,7 @@ static bool ldapsam_sid_to_id(struct pdb_methods *methods,
        filter = talloc_asprintf(mem_ctx,
                                 "(&(sambaSid=%s)"
                                 "(|(objectClass=%s)(objectClass=%s)))",
-                                sid_string_static(sid),
+                                sid_string_talloc(mem_ctx, sid),
                                 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
        if (filter == NULL) {
                DEBUG(5, ("talloc_asprintf failed\n"));
@@ -5029,7 +5011,7 @@ static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
        uid_t uid = -1;
        NTSTATUS ret;
        int rc;
-       
+
        if (((acb_info & ACB_NORMAL) && name[strlen(name)-1] == '$') ||
              acb_info & ACB_WSTRUST ||
              acb_info & ACB_SVRTRUST ||
@@ -5037,15 +5019,15 @@ static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
                is_machine = True;
        }
 
-       username = escape_ldap_string_alloc(name);
+       username = escape_ldap_string(talloc_tos(), name);
        filter = talloc_asprintf(tmp_ctx, "(&(uid=%s)(objectClass=%s))",
                                 username, LDAP_OBJ_POSIXACCOUNT);
-       SAFE_FREE(username);
+       TALLOC_FREE(username);
 
        rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
        if (rc != LDAP_SUCCESS) {
                DEBUG(0,("ldapsam_create_user: ldap search failed!\n"));
-               return NT_STATUS_UNSUCCESSFUL;
+               return NT_STATUS_ACCESS_DENIED;
        }
        talloc_autofree_ldapmsg(tmp_ctx, result);
 
@@ -5055,7 +5037,7 @@ static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
                DEBUG (0, ("ldapsam_create_user: More than one user with name [%s] ?!\n", name));
                return NT_STATUS_INTERNAL_DB_CORRUPTION;
        }
-       
+
        if (num_result == 1) {
                char *tmp;
                /* check if it is just a posix account.
@@ -5084,7 +5066,7 @@ static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
        if (num_result == 0) {
                add_posix = True;
        }
-       
+
        /* Create the basic samu structure and generate the mods for the ldap commit */
        if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
                DEBUG(1, ("ldapsam_create_user: Could not allocate a new RID\n"));
@@ -5168,8 +5150,8 @@ static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
                        homedir = talloc_sub_specified(tmp_ctx, lp_template_homedir(), name, ldap_state->domain_name, uid, gid);
                        shell = talloc_sub_specified(tmp_ctx, lp_template_shell(), name, ldap_state->domain_name, uid, gid);
                }
-               uidstr = talloc_asprintf(tmp_ctx, "%d", uid);
-               gidstr = talloc_asprintf(tmp_ctx, "%d", gid);
+               uidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)uid);
+               gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
 
                escape_name = escape_rdn_val_string_alloc(name);
                if (!escape_name) {
@@ -5230,7 +5212,7 @@ static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *
        int rc;
 
        DEBUG(0,("ldapsam_delete_user: Attempt to delete user [%s]\n", pdb_get_username(sam_acct)));
-       
+
        filter = talloc_asprintf(tmp_ctx,
                                 "(&(uid=%s)"
                                 "(objectClass=%s)"
@@ -5312,11 +5294,11 @@ static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
        DOM_SID group_sid;
        gid_t gid = -1;
        int rc;
-       
-       groupname = escape_ldap_string_alloc(name);
+
+       groupname = escape_ldap_string(talloc_tos(), name);
        filter = talloc_asprintf(tmp_ctx, "(&(cn=%s)(objectClass=%s))",
                                 groupname, LDAP_OBJ_POSIXGROUP);
-       SAFE_FREE(groupname);
+       TALLOC_FREE(groupname);
 
        rc = smbldap_search_suffix(ldap_state->smbldap_state, filter, NULL, &result);
        if (rc != LDAP_SUCCESS) {
@@ -5331,7 +5313,7 @@ static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
                DEBUG (0, ("ldapsam_create_group: There exists more than one group with name [%s]: bailing out!\n", name));
                return NT_STATUS_INTERNAL_DB_CORRUPTION;
        }
-       
+
        if (num_result == 1) {
                char *tmp;
                /* check if it is just a posix group.
@@ -5355,7 +5337,7 @@ static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
                        DEBUG (1, ("ldapsam_create_group: Couldn't retrieve the gidNumber for [%s]?!?!\n", name));
                        return NT_STATUS_INTERNAL_DB_CORRUPTION;
                }
-               
+
                gid = strtoul(tmp, NULL, 10);
 
                dn = smbldap_talloc_dn(tmp_ctx, priv2ld(ldap_state), entry);
@@ -5371,14 +5353,14 @@ static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
                DEBUG(3,("ldapsam_create_user: Creating new posix group\n"));
 
                is_new_entry = True;
-       
+
                /* lets allocate a new groupid for this group */
                if (!winbind_allocate_gid(&gid)) {
                        DEBUG (0, ("ldapsam_create_group: Unable to allocate a new group id: bailing out!\n"));
                        return NT_STATUS_UNSUCCESSFUL;
                }
 
-               gidstr = talloc_asprintf(tmp_ctx, "%d", gid);
+               gidstr = talloc_asprintf(tmp_ctx, "%u", (unsigned int)gid);
 
                escape_name = escape_rdn_val_string_alloc(name);
                if (!escape_name) {
@@ -5407,7 +5389,8 @@ static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
 
        sid_compose(&group_sid, get_global_sam_sid(), *rid);
 
-       groupsidstr = talloc_strdup(tmp_ctx, sid_string_static(&group_sid));
+       groupsidstr = talloc_strdup(tmp_ctx, sid_string_talloc(tmp_ctx,
+                                                              &group_sid));
        grouptype = talloc_asprintf(tmp_ctx, "%d", SID_NAME_DOM_GRP);
 
        if (!groupsidstr || !grouptype) {
@@ -5464,7 +5447,7 @@ static NTSTATUS ldapsam_delete_dom_group(struct pdb_methods *my_methods, TALLOC_
                                 "(&(sambaSID=%s)"
                                 "(objectClass=%s)"
                                 "(objectClass=%s))",
-                                sid_string_static(&group_sid),
+                                sid_string_talloc(tmp_ctx, &group_sid),
                                 LDAP_OBJ_POSIXGROUP,
                                 LDAP_OBJ_GROUPMAP);
        if (filter == NULL) {
@@ -5567,7 +5550,7 @@ static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
        default:
                return NT_STATUS_UNSUCCESSFUL;
        }
-       
+
        /* get member sid  */
        sid_compose(&member_sid, get_global_sam_sid(), member_rid);
 
@@ -5578,7 +5561,7 @@ static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
                                 "(&(sambaSID=%s)"
                                 "(objectClass=%s)"
                                 "(objectClass=%s))",
-                                sid_string_static(&member_sid),
+                                sid_string_talloc(tmp_ctx, &member_sid),
                                 LDAP_OBJ_POSIXACCOUNT,
                                 LDAP_OBJ_SAMBASAMACCOUNT);
        if (filter == NULL) {
@@ -5614,7 +5597,7 @@ static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
                /* check if we are trying to remove the member from his primary group */
                char *gidstr;
                gid_t user_gid, group_gid;
-               
+
                gidstr = smbldap_talloc_single_attribute(priv2ld(ldap_state), entry, "gidNumber", tmp_ctx);
                if (!gidstr) {
                        DEBUG (0, ("ldapsam_change_groupmem: Unable to find the member's gid!\n"));
@@ -5622,7 +5605,7 @@ static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
                }
 
                user_gid = strtoul(gidstr, NULL, 10);
-       
+
                if (!sid_to_gid(&group_sid, &group_gid)) {
                        DEBUG (0, ("ldapsam_change_groupmem: Unable to get group gid from SID!\n"));
                        return NT_STATUS_UNSUCCESSFUL;
@@ -5645,7 +5628,7 @@ static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
                                 "(&(sambaSID=%s)"
                                 "(objectClass=%s)"
                                 "(objectClass=%s))",
-                                sid_string_static(&group_sid),
+                                sid_string_talloc(tmp_ctx, &group_sid),
                                 LDAP_OBJ_POSIXGROUP,
                                 LDAP_OBJ_GROUPMAP);
 
@@ -5697,7 +5680,7 @@ static NTSTATUS ldapsam_change_groupmem(struct pdb_methods *my_methods,
                }
                return NT_STATUS_UNSUCCESSFUL;
        }
-       
+
        return NT_STATUS_OK;
 }
 
@@ -5735,16 +5718,17 @@ static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
        DEBUG(0,("ldapsam_set_primary_group: Attempt to set primary group for user [%s]\n", pdb_get_username(sampass)));
 
        if (!sid_to_gid(pdb_get_group_sid(sampass), &gid)) {
-               DEBUG(0,("ldapsam_set_primary_group: failed to retieve gid from user's group SID!\n"));
+               DEBUG(0,("ldapsam_set_primary_group: failed to retrieve gid from user's group SID!\n"));
                return NT_STATUS_UNSUCCESSFUL;
        }
-       gidstr = talloc_asprintf(mem_ctx, "%d", gid);
+       gidstr = talloc_asprintf(mem_ctx, "%u", (unsigned int)gid);
        if (!gidstr) {
                DEBUG(0,("ldapsam_set_primary_group: Out of Memory!\n"));
                return NT_STATUS_NO_MEMORY;
        }
 
-       escape_username = escape_ldap_string_alloc(pdb_get_username(sampass));
+       escape_username = escape_ldap_string(talloc_tos(),
+                                            pdb_get_username(sampass));
        if (escape_username== NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -5757,7 +5741,7 @@ static NTSTATUS ldapsam_set_primary_group(struct pdb_methods *my_methods,
                                 LDAP_OBJ_POSIXACCOUNT,
                                 LDAP_OBJ_SAMBASAMACCOUNT);
 
-       SAFE_FREE(escape_username);
+       TALLOC_FREE(escape_username);
 
        if (filter == NULL) {
                return NT_STATUS_NO_MEMORY;
@@ -5827,6 +5811,7 @@ static char *trusteddom_dn(struct ldapsam_privates *ldap_state,
 }
 
 static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
+                                 TALLOC_CTX *mem_ctx,
                                  const char *domain, LDAPMessage **entry)
 {
        int rc;
@@ -5849,6 +5834,10 @@ static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
        rc = smbldap_search(ldap_state->smbldap_state, trusted_dn, scope,
                            filter, attrs, attrsonly, &result);
 
+       if (result != NULL) {
+               talloc_autofree_ldapmsg(mem_ctx, result);
+       }
+
        if (rc == LDAP_NO_SUCH_OBJECT) {
                *entry = NULL;
                return True;
@@ -5862,15 +5851,15 @@ static bool get_trusteddom_pw_int(struct ldapsam_privates *ldap_state,
 
        if (num_result > 1) {
                DEBUG(1, ("ldapsam_get_trusteddom_pw: more than one "
-                         "sambaTrustedDomainPassword object for domain '%s'"
-                         "?!\n", domain));
+                         "%s object for domain '%s'?!\n",
+                         LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
                return False;
        }
 
        if (num_result == 0) {
                DEBUG(1, ("ldapsam_get_trusteddom_pw: no "
-                         "sambaTrustedDomainPassword object for domain %s.\n",
-                         domain));
+                         "%s object for domain %s.\n",
+                         LDAP_OBJ_TRUSTDOM_PASSWORD, domain));
                *entry = NULL;
        } else {
                *entry = ldap_first_entry(priv2ld(ldap_state), result);
@@ -5891,7 +5880,7 @@ static bool ldapsam_get_trusteddom_pw(struct pdb_methods *methods,
 
        DEBUG(10, ("ldapsam_get_trusteddom_pw called for domain %s\n", domain));
 
-       if (!get_trusteddom_pw_int(ldap_state, domain, &entry) ||
+       if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry) ||
            (entry == NULL))
        {
                return False;
@@ -5962,21 +5951,24 @@ static bool ldapsam_set_trusteddom_pw(struct pdb_methods *methods,
         * get the current entry (if there is one) in order to put the
         * current password into the previous password attribute
         */
-       if (!get_trusteddom_pw_int(ldap_state, domain, &entry)) {
+       if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
                return False;
        }
 
        mods = NULL;
        smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "objectClass",
-                        "sambaTrustedDomainPassword");
+                        LDAP_OBJ_TRUSTDOM_PASSWORD);
        smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaDomainName",
                         domain);
        smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaSID",
                         sid_string_tos(sid));
        smbldap_make_mod(priv2ld(ldap_state), entry, &mods, "sambaPwdLastSet",
-                        talloc_asprintf(talloc_tos(), "%li", time(NULL)));
+                        talloc_asprintf(talloc_tos(), "%li", (long int)time(NULL)));
        smbldap_make_mod(priv2ld(ldap_state), entry, &mods,
                         "sambaClearTextPassword", pwd);
+
+       talloc_autofree_ldapmod(talloc_tos(), mods);
+
        if (entry != NULL) {
                prev_pwd = smbldap_talloc_single_attribute(priv2ld(ldap_state),
                                entry, "sambaClearTextPassword", talloc_tos());
@@ -6014,7 +6006,7 @@ static bool ldapsam_del_trusteddom_pw(struct pdb_methods *methods,
        LDAPMessage *entry = NULL;
        const char *trusted_dn;
 
-       if (!get_trusteddom_pw_int(ldap_state, domain, &entry)) {
+       if (!get_trusteddom_pw_int(ldap_state, talloc_tos(), domain, &entry)) {
                return False;
        }
 
@@ -6065,6 +6057,10 @@ static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
                            attrsonly,
                            &result);
 
+       if (result != NULL) {
+               talloc_autofree_ldapmsg(mem_ctx, result);
+       }
+
        if (rc != LDAP_SUCCESS) {
                return NT_STATUS_UNSUCCESSFUL;
        }
@@ -6164,9 +6160,6 @@ static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const c
 
        (*pdb_method)->name = "ldapsam";
 
-       (*pdb_method)->setsampwent = ldapsam_setsampwent;
-       (*pdb_method)->endsampwent = ldapsam_endsampwent;
-       (*pdb_method)->getsampwent = ldapsam_getsampwent;
        (*pdb_method)->getsampwnam = ldapsam_getsampwnam;
        (*pdb_method)->getsampwsid = ldapsam_getsampwsid;
        (*pdb_method)->add_sam_account = ldapsam_add_sam_account;
@@ -6187,7 +6180,7 @@ static NTSTATUS pdb_init_ldapsam_common(struct pdb_methods **pdb_method, const c
 
        (*pdb_method)->get_seq_num = ldapsam_get_seq_num;
 
-       (*pdb_method)->rid_algorithm = ldapsam_rid_algorithm;
+       (*pdb_method)->capabilities = ldapsam_capabilities;
        (*pdb_method)->new_rid = ldapsam_new_rid;
 
        (*pdb_method)->get_trusteddom_pw = ldapsam_get_trusteddom_pw;
@@ -6336,21 +6329,21 @@ NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
                return NT_STATUS_UNSUCCESSFUL;
        }
 
-       dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);
+       dn = smbldap_talloc_dn(talloc_tos(), ldap_state->smbldap_state->ldap_struct, entry);
        if (!dn) {
                ldap_msgfree(result);
                return NT_STATUS_UNSUCCESSFUL;
        }
 
        ldap_state->domain_dn = smb_xstrdup(dn);
-       ldap_memfree(dn);
+       TALLOC_FREE(dn);
 
        domain_sid_string = smbldap_talloc_single_attribute(
                    ldap_state->smbldap_state->ldap_struct,
                    entry,
                    get_userattr_key2string(ldap_state->schema_ver,
                                            LDAP_ATTR_USER_SID),
-                   NULL);
+                   talloc_tos());
 
        if (domain_sid_string) {
                bool found_sid;
@@ -6365,21 +6358,17 @@ NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
                                                     &secrets_domain_sid);
                if (!found_sid || !sid_equal(&secrets_domain_sid,
                                             &ldap_domain_sid)) {
-                       fstring new_sid_str, old_sid_str;
                        DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
                                  "%s based on pdb_ldap results %s -> %s\n",
                                  ldap_state->domain_name,
-                                 sid_to_string(old_sid_str,
-                                               &secrets_domain_sid),
-                                 sid_to_string(new_sid_str,
-                                               &ldap_domain_sid)));
+                                 sid_string_dbg(&secrets_domain_sid),
+                                 sid_string_dbg(&ldap_domain_sid)));
 
                        /* reset secrets.tdb sid */
                        secrets_store_domain_sid(ldap_state->domain_name,
                                                 &ldap_domain_sid);
                        DEBUG(1, ("New global sam SID: %s\n",
-                                 sid_to_string(new_sid_str,
-                                               get_global_sam_sid())));
+                                 sid_string_dbg(get_global_sam_sid())));
                }
                sid_copy(&ldap_state->domain_sid, &ldap_domain_sid);
                TALLOC_FREE(domain_sid_string);
@@ -6390,7 +6379,7 @@ NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
                    entry,
                    get_attr_key2string( dominfo_attr_list,
                                         LDAP_ATTR_ALGORITHMIC_RID_BASE ),
-                   NULL);
+                   talloc_tos());
        if (alg_rid_base_string) {
                alg_rid_base = (uint32)atol(alg_rid_base_string);
                if (alg_rid_base != algorithmic_rid_base()) {