possibly fix the 15000 user problem
[sfrench/samba-autobuild/.git] / source3 / nsswitch / winbindd_ads.c
index e144fffc59cbf85e297ffd1e0c553759ffd55831..22bad667c3c55abcb015ec21bef23aed06c6238b 100644 (file)
@@ -1,5 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
+   Unix SMB/CIFS implementation.
 
    Winbind ADS backend functions
 
@@ -38,28 +38,30 @@ ADS_STATUS ads_do_search_retry(ADS_STRUCT *ads, const char *bind_path, int scope
 {
        ADS_STATUS status;
        int count = 3;
+       char *bp;
 
        if (!ads->ld &&
            time(NULL) - ads->last_attempt < ADS_RECONNECT_TIME) {
                return ADS_ERROR(LDAP_SERVER_DOWN);
        }
 
+       bp = strdup(bind_path);
+
        while (count--) {
-               status = ads_do_search(ads, bind_path, scope, exp, attrs, res);
+               status = ads_do_search_all(ads, bp, scope, exp, attrs, res);
                if (ADS_ERR_OK(status)) {
                        DEBUG(5,("Search for %s gave %d replies\n",
                                 exp, ads_count_replies(ads, *res)));
+                       free(bp);
                        return status;
                }
 
                if (*res) ads_msgfree(ads, *res);
                *res = NULL;
-               DEBUG(1,("Reopening ads connection after error %s\n", 
-                        ads_errstr(status)));
+               DEBUG(1,("Reopening ads connection to %s after error %s\n", 
+                        ads->ldap_server, ads_errstr(status)));
                if (ads->ld) {
-                       /* we should unbind here, but that seems to trigger openldap bugs :(
-                          ldap_unbind(ads->ld); 
-                       */
+                       ldap_unbind(ads->ld); 
                }
                ads->ld = NULL;
                status = ads_connect(ads);
@@ -67,9 +69,11 @@ ADS_STATUS ads_do_search_retry(ADS_STRUCT *ads, const char *bind_path, int scope
                        DEBUG(1,("ads_search_retry: failed to reconnect (%s)\n",
                                 ads_errstr(status)));
                        ads_destroy(&ads);
+                       free(bp);
                        return status;
                }
        }
+       free(bp);
 
        DEBUG(1,("ads reopen failed after error %s\n", ads_errstr(status)));
        return status;
@@ -102,6 +106,7 @@ static ADS_STRUCT *ads_cached_connection(struct winbindd_domain *domain)
        ADS_STATUS status;
        char *ccache;
        struct in_addr server_ip;
+       char *sname;
 
        if (domain->private) {
                return (ADS_STRUCT *)domain->private;
@@ -112,12 +117,17 @@ static ADS_STRUCT *ads_cached_connection(struct winbindd_domain *domain)
        SETENV("KRB5CCNAME", ccache, 1);
        unlink(ccache);
 
-       if (!resolve_name(domain->name, &server_ip, 0x1b)) {
-               DEBUG(1,("Can't find PDC for domain %s\n", domain->name));
-               return NULL;
+       if (resolve_name(domain->name, &server_ip, 0x1b)) {
+               sname = inet_ntoa(server_ip);
+       } else {
+               if (strcasecmp(domain->name, lp_workgroup()) != 0) {
+                       DEBUG(1,("can't find domain controller for %s\n", domain->name));
+                       return NULL;
+               }
+               sname = NULL;
        }
 
-       ads = ads_init(primary_realm, inet_ntoa(server_ip), NULL, NULL);
+       ads = ads_init(primary_realm, sname, NULL, NULL);
        if (!ads) {
                DEBUG(1,("ads_init for domain %s failed\n", domain->name));
                return NULL;
@@ -210,7 +220,7 @@ static NTSTATUS query_user_list(struct winbindd_domain *domain,
                goto done;
        }
 
-       (*info) = talloc(mem_ctx, count * sizeof(**info));
+       (*info) = talloc_zero(mem_ctx, count * sizeof(**info));
        if (!*info) {
                status = NT_STATUS_NO_MEMORY;
                goto done;
@@ -298,7 +308,7 @@ static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
                goto done;
        }
 
-       (*info) = talloc(mem_ctx, count * sizeof(**info));
+       (*info) = talloc_zero(mem_ctx, count * sizeof(**info));
        if (!*info) {
                status = NT_STATUS_NO_MEMORY;
                goto done;
@@ -360,22 +370,14 @@ static NTSTATUS name_to_sid(struct winbindd_domain *domain,
        void *res = NULL;
        char *exp;
        uint32 t;
-       fstring name2, dom2, fullname2;
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
 
-       /* sigh. Need to fix interface to give us a raw name */
-       fstrcpy(fullname2, name);
-       fstring_sub(fullname2, "\\", lp_winbind_separator());
-       if (!parse_domain_user(fullname2, dom2, name2)) {
-               goto done;
-       }
-
        DEBUG(3,("ads: name_to_sid\n"));
 
        ads = ads_cached_connection(domain);
        if (!ads) goto done;
 
-       asprintf(&exp, "(sAMAccountName=%s)", name2);
+       asprintf(&exp, "(sAMAccountName=%s)", name);
        rc = ads_search_retry(ads, &res, exp, attrs);
        free(exp);
        if (!ADS_ERR_OK(rc)) {
@@ -425,7 +427,6 @@ static NTSTATUS sid_to_name(struct winbindd_domain *domain,
        char *exp;
        char *sidstr;
        uint32 atype;
-       char *s;
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
 
        DEBUG(3,("ads: sid_to_name\n"));
@@ -447,8 +448,7 @@ static NTSTATUS sid_to_name(struct winbindd_domain *domain,
                goto done;
        }
 
-       s = ads_pull_string(ads, mem_ctx, msg, "sAMAccountName");
-       *name = talloc_asprintf(mem_ctx, "%s%s%s", domain->name, lp_winbind_separator(), s);
+       *name = ads_pull_string(ads, mem_ctx, msg, "sAMAccountName");
        *type = ads_atype_map(atype);
 
        status = NT_STATUS_OK;
@@ -462,6 +462,48 @@ done:
 }
 
 
+/* convert a sid to a distnguished name */
+static NTSTATUS sid_to_distinguished_name(struct winbindd_domain *domain,
+                                         TALLOC_CTX *mem_ctx,
+                                         DOM_SID *sid,
+                                         char **dn)
+{
+       ADS_STRUCT *ads = NULL;
+       const char *attrs[] = {"distinguishedName", NULL};
+       ADS_STATUS rc;
+       void *msg = NULL;
+       char *exp;
+       char *sidstr;
+       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
+
+       DEBUG(3,("ads: sid_to_distinguished_name\n"));
+
+       ads = ads_cached_connection(domain);
+       if (!ads) goto done;
+
+       sidstr = sid_binstring(sid);
+       asprintf(&exp, "(objectSid=%s)", sidstr);
+       rc = ads_search_retry(ads, &msg, exp, attrs);
+       free(exp);
+       free(sidstr);
+       if (!ADS_ERR_OK(rc)) {
+               DEBUG(1,("sid_to_distinguished_name ads_search: %s\n", ads_errstr(rc)));
+               goto done;
+       }
+
+       *dn = ads_pull_string(ads, mem_ctx, msg, "distinguishedName");
+
+       status = NT_STATUS_OK;
+
+       DEBUG(3,("ads sid_to_distinguished_name mapped %s\n", *dn));
+
+done:
+       if (msg) ads_msgfree(ads, msg);
+
+       return status;
+}
+
+
 /* Lookup user information from a rid */
 static NTSTATUS query_user(struct winbindd_domain *domain, 
                           TALLOC_CTX *mem_ctx, 
@@ -586,7 +628,7 @@ static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
        }
 
        count = ads_pull_sids(ads, mem_ctx, msg, "tokenGroups", &sids) + 1;
-       (*user_gids) = (uint32 *)talloc(mem_ctx, sizeof(uint32) * count);
+       (*user_gids) = (uint32 *)talloc_zero(mem_ctx, sizeof(uint32) * count);
        (*user_gids)[(*num_groups)++] = primary_group;
 
        for (i=1;i<count;i++) {
@@ -612,13 +654,12 @@ static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
                                uint32 **name_types)
 {
        DOM_SID group_sid;
-       char *sidstr;
        const char *attrs[] = {"sAMAccountName", "objectSid", "sAMAccountType", NULL};
        ADS_STATUS rc;
        int count;
        void *res=NULL, *msg=NULL;
        ADS_STRUCT *ads = NULL;
-       char *exp;
+       char *exp, *dn = NULL;
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
 
        *num_names = 0;
@@ -627,13 +668,17 @@ static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
        if (!ads) goto done;
 
        sid_from_rid(domain, group_rid, &group_sid);
-       sidstr = sid_binstring(&group_sid);
+       status = sid_to_distinguished_name(domain, mem_ctx, &group_sid, &dn);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(3,("Failed to find distinguishedName for %s\n", sid_string_static(&group_sid)));
+               return status;
+       }
+
        /* search for all users who have that group sid as primary group or as member */
        asprintf(&exp, "(&(objectCategory=user)(|(primaryGroupID=%d)(memberOf=%s)))",
-                group_rid, sidstr);
+                group_rid, dn);
        rc = ads_search_retry(ads, &res, exp, attrs);
        free(exp);
-       free(sidstr);
        if (!ADS_ERR_OK(rc)) {
                DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
                goto done;
@@ -645,9 +690,9 @@ static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
                goto done;
        }
 
-       (*rid_mem) = talloc(mem_ctx, sizeof(uint32) * count);
-       (*name_types) = talloc(mem_ctx, sizeof(uint32) * count);
-       (*names) = talloc(mem_ctx, sizeof(char *) * count);
+       (*rid_mem) = talloc_zero(mem_ctx, sizeof(uint32) * count);
+       (*name_types) = talloc_zero(mem_ctx, sizeof(uint32) * count);
+       (*names) = talloc_zero(mem_ctx, sizeof(char *) * count);
 
        for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
                uint32 atype, rid;
@@ -690,6 +735,11 @@ static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
        if (!ads) return NT_STATUS_UNSUCCESSFUL;
 
        rc = ads_USN(ads, seq);
+       if (!ADS_ERR_OK(rc)) {
+               /* its a dead connection */
+               ads_destroy(&ads);
+               domain->private = NULL;
+       }
        return ads_ntstatus(rc);
 }
 
@@ -700,7 +750,7 @@ static NTSTATUS trusted_domains(struct winbindd_domain *domain,
                                char ***names,
                                DOM_SID **dom_sids)
 {
-       ADS_STRUCT *ads = NULL;
+       ADS_STRUCT *ads;
        ADS_STATUS rc;
 
        *num_domains = 0;
@@ -717,25 +767,21 @@ static NTSTATUS trusted_domains(struct winbindd_domain *domain,
 /* find the domain sid for a domain */
 static NTSTATUS domain_sid(struct winbindd_domain *domain, DOM_SID *sid)
 {
-       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
-       const char *attrs[] = {"objectSid", NULL};
-       ADS_STRUCT *ads = NULL;
-       void *res;
+       ADS_STRUCT *ads;
        ADS_STATUS rc;
 
        ads = ads_cached_connection(domain);
-       if (!ads) goto done;
+       if (!ads) return NT_STATUS_UNSUCCESSFUL;
 
-       rc = ads_do_search(ads, ads->bind_path, LDAP_SCOPE_BASE, "(objectclass=*)", 
-                          attrs, &res);
-       if (!ADS_ERR_OK(rc)) goto done;
-       if (ads_pull_sid(ads, res, "objectSid", sid)) {
-               status = NT_STATUS_OK;
+       rc = ads_domain_sid(ads, sid);
+
+       if (!ADS_ERR_OK(rc)) {
+               /* its a dead connection */
+               ads_destroy(&ads);
+               domain->private = NULL;
        }
-       ads_msgfree(ads, res);
 
-done:
-       return status;
+       return ads_ntstatus(rc);
 }
 
 /* the ADS backend methods are exposed via this structure */