s3-talloc Change TALLOC_ARRAY() to talloc_array()
[samba.git] / source3 / passdb / pdb_ldap.c
index 2e48023d8bd227778c0deeb93fb3b067386dc666..8167e3799eefee1a0cfc83d33c11f36f8a56f0ac 100644 (file)
 */
 
 #include "includes.h"
+#include "passdb.h"
 #include "../libcli/auth/libcli_auth.h"
+#include "secrets.h"
+#include "idmap_cache.h"
+#include "../libcli/security/security.h"
+#include "../lib/util/util_pw.h"
+#include "lib/winbind_util.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_PASSDB
 #include <lber.h>
 #include <ldap.h>
 
-/*
- * Work around versions of the LDAP client libs that don't have the OIDs
- * defined, or have them defined under the old name.  
- * This functionality is really a factor of the server, not the client 
- *
- */
-
-#if defined(LDAP_EXOP_X_MODIFY_PASSWD) && !defined(LDAP_EXOP_MODIFY_PASSWD)
-#define LDAP_EXOP_MODIFY_PASSWD LDAP_EXOP_X_MODIFY_PASSWD
-#elif !defined(LDAP_EXOP_MODIFY_PASSWD)
-#define LDAP_EXOP_MODIFY_PASSWD "1.3.6.1.4.1.4203.1.11.1"
-#endif
-
-#if defined(LDAP_EXOP_X_MODIFY_PASSWD_ID) && !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
-#define LDAP_TAG_EXOP_MODIFY_PASSWD_ID LDAP_EXOP_X_MODIFY_PASSWD_ID
-#elif !defined(LDAP_EXOP_MODIFY_PASSWD_ID)
-#define LDAP_TAG_EXOP_MODIFY_PASSWD_ID        ((ber_tag_t) 0x80U)
-#endif
-
-#if defined(LDAP_EXOP_X_MODIFY_PASSWD_NEW) && !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
-#define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW LDAP_EXOP_X_MODIFY_PASSWD_NEW
-#elif !defined(LDAP_EXOP_MODIFY_PASSWD_NEW)
-#define LDAP_TAG_EXOP_MODIFY_PASSWD_NEW       ((ber_tag_t) 0x82U)
-#endif
-
 
 #include "smbldap.h"
 
@@ -84,7 +65,7 @@
  Simple helper function to make stuff better readable
  **********************************************************************/
 
-static LDAP *priv2ld(struct ldapsam_privates *priv)
+LDAP *priv2ld(struct ldapsam_privates *priv)
 {
        return priv->smbldap_state->ldap_struct;
 }
@@ -227,7 +208,7 @@ static NTSTATUS ldapsam_get_seq_num(struct pdb_methods *my_methods, time_t *seq_
        if (mem_ctx == NULL)
                return NT_STATUS_NO_MEMORY;
 
-       if ((attrs = TALLOC_ARRAY(mem_ctx, const char *, 2)) == NULL) {
+       if ((attrs = talloc_array(mem_ctx, const char *, 2)) == NULL) {
                ntstatus = NT_STATUS_NO_MEMORY;
                goto done;
        }
@@ -906,7 +887,7 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
        if (pwHistLen > 0){
                uint8 *pwhist = NULL;
                int i;
-               char *history_string = TALLOC_ARRAY(ctx, char,
+               char *history_string = talloc_array(ctx, char,
                                                MAX_PW_HISTORY_LEN*64);
 
                if (!history_string) {
@@ -915,7 +896,7 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
 
                pwHistLen = MIN(pwHistLen, MAX_PW_HISTORY_LEN);
 
-               pwhist = TALLOC_ARRAY(ctx, uint8,
+               pwhist = talloc_array(ctx, uint8,
                                      pwHistLen * PW_HISTORY_ENTRY_LEN);
                if (pwhist == NULL) {
                        DEBUG(0, ("init_sam_from_ldap: talloc failed!\n"));
@@ -1024,7 +1005,7 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
        if (temp) {
                pdb_gethexhours(temp, hours);
                memset((char *)temp, '\0', strlen(temp) +1);
-               pdb_set_hours(sampass, hours, PDB_SET);
+               pdb_set_hours(sampass, hours, hours_len, PDB_SET);
                ZERO_STRUCT(hours);
        }
 
@@ -1104,7 +1085,7 @@ static bool init_sam_from_ldap(struct ldapsam_privates *ldap_state,
 
                gid_to_sid(&mapped_gsid, sampass->unix_pw->pw_gid);
                primary_gsid = pdb_get_group_sid(sampass);
-               if (primary_gsid && sid_equal(primary_gsid, &mapped_gsid)) {
+               if (primary_gsid && dom_sid_equal(primary_gsid, &mapped_gsid)) {
                        store_gid_sid_cache(primary_gsid,
                                            sampass->unix_pw->pw_gid);
                        idmap_cache_set_sid2gid(primary_gsid,
@@ -1551,7 +1532,7 @@ static void append_attr(TALLOC_CTX *mem_ctx, const char ***attr_list,
                ;
        }
 
-       (*attr_list) = TALLOC_REALLOC_ARRAY(mem_ctx, (*attr_list),
+       (*attr_list) = talloc_realloc(mem_ctx, (*attr_list),
                                            const char *,  i+2);
        SMB_ASSERT((*attr_list) != NULL);
        (*attr_list)[i] = talloc_strdup((*attr_list), new_attr);
@@ -2077,7 +2058,7 @@ static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
                                               struct samu *user,
                                               struct dom_sid **pp_sids,
                                               gid_t **pp_gids,
-                                              size_t *p_num_groups);
+                                              uint32_t *p_num_groups);
 
 static NTSTATUS ldapsam_rename_sam_account(struct pdb_methods *my_methods,
                                           struct samu *old_acct,
@@ -2680,7 +2661,7 @@ static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
                return False;
        }
 
-       if (sid_compare_domain(&sid, domain_sid) != 0) {
+       if (dom_sid_compare_domain(&sid, domain_sid) != 0) {
                DEBUG(10, ("SID %s is not in expected domain %s\n",
                           str, sid_string_dbg(domain_sid)));
                return False;
@@ -2899,7 +2880,7 @@ static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
                                               struct samu *user,
                                               struct dom_sid **pp_sids,
                                               gid_t **pp_gids,
-                                              size_t *p_num_groups)
+                                              uint32_t *p_num_groups)
 {
        struct ldapsam_privates *ldap_state =
                (struct ldapsam_privates *)methods->private_data;
@@ -2911,7 +2892,8 @@ static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
        LDAPMessage *result = NULL;
        LDAPMessage *entry;
        NTSTATUS ret = NT_STATUS_UNSUCCESSFUL;
-       size_t num_sids, num_gids;
+       uint32_t num_sids;
+       uint32_t num_gids;
        char *gidstr;
        gid_t primary_gid = -1;
 
@@ -3052,7 +3034,7 @@ static NTSTATUS ldapsam_enum_group_memberships(struct pdb_methods *methods,
                }
        }
 
-       if (sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
+       if (dom_sid_compare(&global_sid_NULL, &(*pp_sids)[0]) == 0) {
                DEBUG(3, ("primary group of [%s] not found\n",
                          pdb_get_username(user)));
                goto done;
@@ -3512,7 +3494,7 @@ static NTSTATUS ldapsam_enum_group_mapping(struct pdb_methods *methods,
                                           size_t *p_num_entries,
                                           bool unix_only)
 {
-       GROUP_MAP map;
+       GROUP_MAP map = { 0, };
        size_t entries = 0;
 
        *p_num_entries = 0;
@@ -3690,7 +3672,7 @@ static NTSTATUS ldapsam_enum_aliasmem(struct pdb_methods *methods,
        char **values = NULL;
        int i;
        char *filter = NULL;
-       size_t num_members = 0;
+       uint32_t num_members = 0;
        enum lsa_SidType type = SID_NAME_USE_NONE;
        fstring tmp;
 
@@ -4358,7 +4340,7 @@ static const char **talloc_attrs(TALLOC_CTX *mem_ctx, ...)
                num += 1;
        va_end(ap);
 
-       if ((result = TALLOC_ARRAY(mem_ctx, const char *, num+1)) == NULL) {
+       if ((result = talloc_array(mem_ctx, const char *, num+1)) == NULL) {
                return NULL;
        }
 
@@ -4582,7 +4564,7 @@ static bool ldapuser2displayentry(struct ldap_search_state *state,
                return False;
        }
        if (!pull_utf8_talloc(mem_ctx,
-                             CONST_DISCARD(char **, &result->account_name),
+                             discard_const_p(char *, &result->account_name),
                              vals[0], &converted_size))
        {
                DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
@@ -4595,7 +4577,7 @@ static bool ldapuser2displayentry(struct ldap_search_state *state,
        if ((vals == NULL) || (vals[0] == NULL))
                DEBUG(8, ("\"displayName\" not found\n"));
        else if (!pull_utf8_talloc(mem_ctx,
-                                  CONST_DISCARD(char **, &result->fullname),
+                                  discard_const_p(char *, &result->fullname),
                                   vals[0], &converted_size))
        {
                DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
@@ -4608,7 +4590,7 @@ static bool ldapuser2displayentry(struct ldap_search_state *state,
        if ((vals == NULL) || (vals[0] == NULL))
                DEBUG(8, ("\"description\" not found\n"));
        else if (!pull_utf8_talloc(mem_ctx,
-                                  CONST_DISCARD(char **, &result->description),
+                                  discard_const_p(char *, &result->description),
                                   vals[0], &converted_size))
        {
                DEBUG(0,("ldapuser2displayentry: pull_utf8_talloc failed: %s",
@@ -4742,7 +4724,7 @@ static bool ldapgroup2displayentry(struct ldap_search_state *state,
                        return False;
                }
                if (!pull_utf8_talloc(mem_ctx,
-                                     CONST_DISCARD(char **,
+                                     discard_const_p(char *,
                                                    &result->account_name),
                                      vals[0], &converted_size))
                {
@@ -4751,7 +4733,7 @@ static bool ldapgroup2displayentry(struct ldap_search_state *state,
                }
        }
        else if (!pull_utf8_talloc(mem_ctx,
-                                  CONST_DISCARD(char **,
+                                  discard_const_p(char *,
                                                 &result->account_name),
                                   vals[0], &converted_size))
        {
@@ -4765,7 +4747,7 @@ static bool ldapgroup2displayentry(struct ldap_search_state *state,
        if ((vals == NULL) || (vals[0] == NULL))
                DEBUG(8, ("\"description\" not found\n"));
        else if (!pull_utf8_talloc(mem_ctx,
-                                  CONST_DISCARD(char **, &result->description),
+                                  discard_const_p(char *, &result->description),
                                   vals[0], &converted_size))
        {
                DEBUG(0,("ldapgroup2displayentry: pull_utf8_talloc failed: %s",
@@ -4810,7 +4792,7 @@ static bool ldapgroup2displayentry(struct ldap_search_state *state,
                        break;
 
                default:
-                       DEBUG(0,("unkown group type: %d\n", group_type));
+                       DEBUG(0,("unknown group type: %d\n", group_type));
                        return False;
        }
 
@@ -5385,7 +5367,7 @@ static NTSTATUS ldapsam_create_user(struct pdb_methods *my_methods,
                return NT_STATUS_UNSUCCESSFUL;
        }
 
-       if (!init_ldap_from_sam(ldap_state, NULL, &mods, user, element_is_set_or_changed)) {
+       if (!init_ldap_from_sam(ldap_state, entry, &mods, user, element_is_set_or_changed)) {
                DEBUG(1,("ldapsam_create_user: Unable to fill user structs\n"));
                return NT_STATUS_UNSUCCESSFUL;
        }
@@ -5532,7 +5514,7 @@ static NTSTATUS ldapsam_delete_user(struct pdb_methods *my_methods, TALLOC_CTX *
                NTSTATUS status;
                struct dom_sid *sids = NULL;
                gid_t *gids = NULL;
-               size_t num_groups = 0;
+               uint32_t num_groups = 0;
                int i;
                uint32_t user_rid = pdb_get_user_rid(sam_acct);
 
@@ -5654,12 +5636,35 @@ static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
        }
 
        if (num_result == 0) {
+               is_new_entry = true;
+       }
+
+       if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
+               DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
+               return ret;
+       }
+
+       sid_compose(&group_sid, get_global_sam_sid(), *rid);
+
+       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) {
+               DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
+       smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
+       smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
+       smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
+
+       if (is_new_entry) {
                char *escape_name;
 
                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"));
@@ -5688,26 +5693,6 @@ static NTSTATUS ldapsam_create_dom_group(struct pdb_methods *my_methods,
                smbldap_set_mod(&mods, LDAP_MOD_ADD, "gidNumber", gidstr);
        }
 
-       if (!NT_STATUS_IS_OK((ret = ldapsam_new_rid_internal(my_methods, rid)))) {
-               DEBUG(1, ("ldapsam_create_group: Could not allocate a new RID\n"));
-               return ret;
-       }
-
-       sid_compose(&group_sid, get_global_sam_sid(), *rid);
-
-       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) {
-               DEBUG(0,("ldapsam_create_group: Out of memory!\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectClass", LDAP_OBJ_GROUPMAP);
-       smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaSid", groupsidstr);
-       smbldap_set_mod(&mods, LDAP_MOD_ADD, "sambaGroupType", grouptype);
-       smbldap_set_mod(&mods, LDAP_MOD_ADD, "displayName", name);
        talloc_autofree_ldapmod(tmp_ctx, mods);
 
        if (is_new_entry) {     
@@ -6371,7 +6356,7 @@ static NTSTATUS ldapsam_enum_trusteddoms(struct pdb_methods *methods,
        }
 
        *num_domains = 0;
-       if (!(*domains = TALLOC_ARRAY(mem_ctx, struct trustdom_info *, 1))) {
+       if (!(*domains = talloc_array(mem_ctx, struct trustdom_info *, 1))) {
                DEBUG(1, ("talloc failed\n"));
                return NT_STATUS_NO_MEMORY;
        }
@@ -6618,7 +6603,7 @@ NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
                          "info, nor add one to the domain\n"));
                DEBUGADD(2, ("pdb_init_ldapsam: Continuing on regardless, "
                             "will be unable to allocate new users/groups, "
-                            "and will risk BDCs having inconsistant SIDs\n"));
+                            "and will risk BDCs having inconsistent SIDs\n"));
                sid_copy(&ldap_state->domain_sid, get_global_sam_sid());
                return NT_STATUS_OK;
        }
@@ -6662,7 +6647,7 @@ NTSTATUS pdb_init_ldapsam(struct pdb_methods **pdb_method, const char *location)
                }
                found_sid = secrets_fetch_domain_sid(ldap_state->domain_name,
                                                     &secrets_domain_sid);
-               if (!found_sid || !sid_equal(&secrets_domain_sid,
+               if (!found_sid || !dom_sid_equal(&secrets_domain_sid,
                                             &ldap_domain_sid)) {
                        DEBUG(1, ("pdb_init_ldapsam: Resetting SID for domain "
                                  "%s based on pdb_ldap results %s -> %s\n",
@@ -6715,5 +6700,7 @@ NTSTATUS pdb_ldap_init(void)
        /* Let pdb_nds register backends */
        pdb_nds_init();
 
+       pdb_ipa_init();
+
        return NT_STATUS_OK;
 }