2 Unix SMB/CIFS implementation.
4 Winbind ADS backend functions
6 Copyright (C) Andrew Tridgell 2001
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 /* the realm of our primary LDAP server */
28 static char *primary_realm;
32 a wrapper around ldap_search_s that retries depending on the error code
33 this is supposed to catch dropped connections and auto-reconnect
35 ADS_STATUS ads_do_search_retry(ADS_STRUCT *ads, const char *bind_path, int scope,
37 const char **attrs, void **res)
43 time(NULL) - ads->last_attempt < ADS_RECONNECT_TIME) {
44 return ADS_ERROR(LDAP_SERVER_DOWN);
48 status = ads_do_search(ads, bind_path, scope, exp, attrs, res);
49 if (ADS_ERR_OK(status)) {
50 DEBUG(5,("Search for %s gave %d replies\n",
51 exp, ads_count_replies(ads, *res)));
55 if (*res) ads_msgfree(ads, *res);
57 DEBUG(1,("Reopening ads connection after error %s\n",
60 /* we should unbind here, but that seems to trigger openldap bugs :(
65 status = ads_connect(ads);
66 if (!ADS_ERR_OK(status)) {
67 DEBUG(1,("ads_search_retry: failed to reconnect (%s)\n",
74 DEBUG(1,("ads reopen failed after error %s\n", ads_errstr(status)));
79 ADS_STATUS ads_search_retry(ADS_STRUCT *ads, void **res,
83 return ads_do_search_retry(ads, ads->bind_path, LDAP_SCOPE_SUBTREE,
87 ADS_STATUS ads_search_retry_dn(ADS_STRUCT *ads, void **res,
91 return ads_do_search_retry(ads, dn, LDAP_SCOPE_BASE,
92 "(objectclass=*)", attrs, res);
96 return our ads connections structure for a domain. We keep the connection
97 open to make things faster
99 static ADS_STRUCT *ads_cached_connection(struct winbindd_domain *domain)
104 struct in_addr server_ip;
107 if (domain->private) {
108 return (ADS_STRUCT *)domain->private;
111 /* we don't want this to affect the users ccache */
112 ccache = lock_path("winbindd_ccache");
113 SETENV("KRB5CCNAME", ccache, 1);
116 if (resolve_name(domain->name, &server_ip, 0x1b)) {
117 sname = inet_ntoa(server_ip);
119 if (strcasecmp(domain->name, lp_workgroup()) != 0) {
120 DEBUG(1,("can't find domain controller for %s\n", domain->name));
126 ads = ads_init(primary_realm, sname, NULL, NULL);
128 DEBUG(1,("ads_init for domain %s failed\n", domain->name));
132 /* the machine acct password might have change - fetch it every time */
133 SAFE_FREE(ads->password);
134 ads->password = secrets_fetch_machine_password();
136 status = ads_connect(ads);
137 if (!ADS_ERR_OK(status)) {
138 extern struct winbindd_methods msrpc_methods;
139 DEBUG(1,("ads_connect for domain %s failed: %s\n",
140 domain->name, ads_errstr(status)));
143 /* if we get ECONNREFUSED then it might be a NT4
144 server, fall back to MSRPC */
145 if (status.error_type == ADS_ERROR_SYSTEM &&
146 status.rc == ECONNREFUSED) {
147 DEBUG(1,("Trying MSRPC methods\n"));
148 domain->methods = &msrpc_methods;
153 /* remember our primary realm for trusted domain support */
154 if (!primary_realm) {
155 primary_realm = strdup(ads->realm);
158 fstrcpy(domain->full_name, ads->server_realm);
160 domain->private = (void *)ads;
165 static void sid_from_rid(struct winbindd_domain *domain, uint32 rid, DOM_SID *sid)
167 sid_copy(sid, &domain->sid);
168 sid_append_rid(sid, rid);
171 /* turn a sAMAccountType into a SID_NAME_USE */
172 static enum SID_NAME_USE ads_atype_map(uint32 atype)
174 switch (atype & 0xF0000000) {
176 return SID_NAME_DOM_GRP;
178 return SID_NAME_USER;
180 DEBUG(1,("hmm, need to map account type 0x%x\n", atype));
182 return SID_NAME_UNKNOWN;
185 /* Query display info for a realm. This is the basic user list fn */
186 static NTSTATUS query_user_list(struct winbindd_domain *domain,
189 WINBIND_USERINFO **info)
191 ADS_STRUCT *ads = NULL;
192 const char *attrs[] = {"sAMAccountName", "name", "objectSid", "primaryGroupID",
193 "sAMAccountType", NULL};
198 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
202 DEBUG(3,("ads: query_user_list\n"));
204 ads = ads_cached_connection(domain);
207 rc = ads_search_retry(ads, &res, "(objectCategory=user)", attrs);
208 if (!ADS_ERR_OK(rc)) {
209 DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
213 count = ads_count_replies(ads, res);
215 DEBUG(1,("query_user_list: No users found\n"));
219 (*info) = talloc(mem_ctx, count * sizeof(**info));
221 status = NT_STATUS_NO_MEMORY;
227 for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
233 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &atype) ||
234 ads_atype_map(atype) != SID_NAME_USER) {
235 DEBUG(1,("Not a user account? atype=0x%x\n", atype));
239 name = ads_pull_string(ads, mem_ctx, msg, "sAMAccountName");
240 gecos = ads_pull_string(ads, mem_ctx, msg, "name");
241 if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
242 DEBUG(1,("No sid for %s !?\n", name));
245 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &group)) {
246 DEBUG(1,("No primary group for %s !?\n", name));
250 if (!sid_peek_rid(&sid, &rid)) {
251 DEBUG(1,("No rid for %s !?\n", name));
255 (*info)[i].acct_name = name;
256 (*info)[i].full_name = gecos;
257 (*info)[i].user_rid = rid;
258 (*info)[i].group_rid = group;
263 status = NT_STATUS_OK;
265 DEBUG(3,("ads query_user_list gave %d entries\n", (*num_entries)));
268 if (res) ads_msgfree(ads, res);
273 /* list all domain groups */
274 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
277 struct acct_info **info)
279 ADS_STRUCT *ads = NULL;
280 const char *attrs[] = {"sAMAccountName", "name", "objectSid",
281 "sAMAccountType", NULL};
286 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
290 DEBUG(3,("ads: enum_dom_groups\n"));
292 ads = ads_cached_connection(domain);
295 rc = ads_search_retry(ads, &res, "(objectCategory=group)", attrs);
296 if (!ADS_ERR_OK(rc)) {
297 DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
301 count = ads_count_replies(ads, res);
303 DEBUG(1,("query_user_list: No users found\n"));
307 (*info) = talloc(mem_ctx, count * sizeof(**info));
309 status = NT_STATUS_NO_MEMORY;
315 for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
321 if (!ads_pull_uint32(ads, msg, "sAMAccountType",
323 !(account_type & ATYPE_GROUP)) continue;
325 name = ads_pull_string(ads, mem_ctx, msg, "sAMAccountName");
326 gecos = ads_pull_string(ads, mem_ctx, msg, "name");
327 if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
328 DEBUG(1,("No sid for %s !?\n", name));
332 if (!sid_peek_rid(&sid, &rid)) {
333 DEBUG(1,("No rid for %s !?\n", name));
337 fstrcpy((*info)[i].acct_name, name);
338 fstrcpy((*info)[i].acct_desc, gecos);
339 (*info)[i].rid = rid;
345 status = NT_STATUS_OK;
347 DEBUG(3,("ads enum_dom_groups gave %d entries\n", (*num_entries)));
350 if (res) ads_msgfree(ads, res);
356 /* convert a single name to a sid in a domain */
357 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
360 enum SID_NAME_USE *type)
362 ADS_STRUCT *ads = NULL;
363 const char *attrs[] = {"objectSid", "sAMAccountType", NULL};
369 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
371 DEBUG(3,("ads: name_to_sid\n"));
373 ads = ads_cached_connection(domain);
376 asprintf(&exp, "(sAMAccountName=%s)", name);
377 rc = ads_search_retry(ads, &res, exp, attrs);
379 if (!ADS_ERR_OK(rc)) {
380 DEBUG(1,("name_to_sid ads_search: %s\n", ads_errstr(rc)));
384 count = ads_count_replies(ads, res);
386 DEBUG(1,("name_to_sid: %s not found\n", name));
390 if (!ads_pull_sid(ads, res, "objectSid", sid)) {
391 DEBUG(1,("No sid for %s !?\n", name));
395 if (!ads_pull_uint32(ads, res, "sAMAccountType", &t)) {
396 DEBUG(1,("No sAMAccountType for %s !?\n", name));
400 *type = ads_atype_map(t);
402 status = NT_STATUS_OK;
404 DEBUG(3,("ads name_to_sid mapped %s\n", name));
407 if (res) ads_msgfree(ads, res);
412 /* convert a sid to a user or group name */
413 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
417 enum SID_NAME_USE *type)
419 ADS_STRUCT *ads = NULL;
420 const char *attrs[] = {"sAMAccountName", "sAMAccountType", NULL};
427 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
429 DEBUG(3,("ads: sid_to_name\n"));
431 ads = ads_cached_connection(domain);
434 sidstr = sid_binstring(sid);
435 asprintf(&exp, "(objectSid=%s)", sidstr);
436 rc = ads_search_retry(ads, &msg, exp, attrs);
439 if (!ADS_ERR_OK(rc)) {
440 DEBUG(1,("sid_to_name ads_search: %s\n", ads_errstr(rc)));
444 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &atype)) {
448 s = ads_pull_string(ads, mem_ctx, msg, "sAMAccountName");
449 *name = talloc_strdup(mem_ctx, s);
450 *type = ads_atype_map(atype);
452 status = NT_STATUS_OK;
454 DEBUG(3,("ads sid_to_name mapped %s\n", *name));
457 if (msg) ads_msgfree(ads, msg);
463 /* Lookup user information from a rid */
464 static NTSTATUS query_user(struct winbindd_domain *domain,
467 WINBIND_USERINFO *info)
469 ADS_STRUCT *ads = NULL;
470 const char *attrs[] = {"sAMAccountName", "name", "objectSid",
471 "primaryGroupID", NULL};
478 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
480 DEBUG(3,("ads: query_user\n"));
482 sid_from_rid(domain, user_rid, &sid);
484 ads = ads_cached_connection(domain);
487 sidstr = sid_binstring(&sid);
488 asprintf(&exp, "(objectSid=%s)", sidstr);
489 rc = ads_search_retry(ads, &msg, exp, attrs);
492 if (!ADS_ERR_OK(rc)) {
493 DEBUG(1,("query_user(rid=%d) ads_search: %s\n", user_rid, ads_errstr(rc)));
497 count = ads_count_replies(ads, msg);
499 DEBUG(1,("query_user(rid=%d): Not found\n", user_rid));
503 info->acct_name = ads_pull_string(ads, mem_ctx, msg, "sAMAccountName");
504 info->full_name = ads_pull_string(ads, mem_ctx, msg, "name");
505 if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
506 DEBUG(1,("No sid for %d !?\n", user_rid));
509 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &info->group_rid)) {
510 DEBUG(1,("No primary group for %d !?\n", user_rid));
514 if (!sid_peek_rid(&sid, &info->user_rid)) {
515 DEBUG(1,("No rid for %d !?\n", user_rid));
519 status = NT_STATUS_OK;
521 DEBUG(3,("ads query_user gave %s\n", info->acct_name));
523 if (msg) ads_msgfree(ads, msg);
529 /* Lookup groups a user is a member of. */
530 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
533 uint32 *num_groups, uint32 **user_gids)
535 ADS_STRUCT *ads = NULL;
536 const char *attrs[] = {"distinguishedName", NULL};
537 const char *attrs2[] = {"tokenGroups", "primaryGroupID", NULL};
545 uint32 primary_group;
548 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
552 DEBUG(3,("ads: lookup_usergroups\n"));
556 sid_from_rid(domain, user_rid, &sid);
558 ads = ads_cached_connection(domain);
561 sidstr = sid_binstring(&sid);
562 asprintf(&exp, "(objectSid=%s)", sidstr);
563 rc = ads_search_retry(ads, &msg, exp, attrs);
566 if (!ADS_ERR_OK(rc)) {
567 DEBUG(1,("lookup_usergroups(rid=%d) ads_search: %s\n", user_rid, ads_errstr(rc)));
571 user_dn = ads_pull_string(ads, mem_ctx, msg, "distinguishedName");
573 if (msg) ads_msgfree(ads, msg);
575 rc = ads_search_retry_dn(ads, &msg, user_dn, attrs2);
576 if (!ADS_ERR_OK(rc)) {
577 DEBUG(1,("lookup_usergroups(rid=%d) ads_search tokenGroups: %s\n", user_rid, ads_errstr(rc)));
581 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &primary_group)) {
582 DEBUG(1,("%s: No primary group for rid=%d !?\n", domain->name, user_rid));
586 count = ads_pull_sids(ads, mem_ctx, msg, "tokenGroups", &sids) + 1;
587 (*user_gids) = (uint32 *)talloc(mem_ctx, sizeof(uint32) * count);
588 (*user_gids)[(*num_groups)++] = primary_group;
590 for (i=1;i<count;i++) {
592 if (!sid_peek_rid(&sids[i-1], &rid)) continue;
593 (*user_gids)[*num_groups] = rid;
597 status = NT_STATUS_OK;
598 DEBUG(3,("ads lookup_usergroups for rid=%d\n", user_rid));
600 if (msg) ads_msgfree(ads, msg);
606 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
608 uint32 group_rid, uint32 *num_names,
609 uint32 **rid_mem, char ***names,
614 const char *attrs[] = {"sAMAccountName", "objectSid", "sAMAccountType", NULL};
617 void *res=NULL, *msg=NULL;
618 ADS_STRUCT *ads = NULL;
620 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
624 ads = ads_cached_connection(domain);
627 sid_from_rid(domain, group_rid, &group_sid);
628 sidstr = sid_binstring(&group_sid);
629 /* search for all users who have that group sid as primary group or as member */
630 asprintf(&exp, "(&(objectCategory=user)(|(primaryGroupID=%d)(memberOf=%s)))",
632 rc = ads_search_retry(ads, &res, exp, attrs);
635 if (!ADS_ERR_OK(rc)) {
636 DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
640 count = ads_count_replies(ads, res);
642 status = NT_STATUS_OK;
646 (*rid_mem) = talloc(mem_ctx, sizeof(uint32) * count);
647 (*name_types) = talloc(mem_ctx, sizeof(uint32) * count);
648 (*names) = talloc(mem_ctx, sizeof(char *) * count);
650 for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
654 (*names)[*num_names] = ads_pull_string(ads, mem_ctx, msg, "sAMAccountName");
655 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &atype)) {
658 (*name_types)[*num_names] = ads_atype_map(atype);
659 if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
660 DEBUG(1,("No sid for %s !?\n", (*names)[*num_names]));
663 if (!sid_peek_rid(&sid, &rid)) {
664 DEBUG(1,("No rid for %s !?\n", (*names)[*num_names]));
667 (*rid_mem)[*num_names] = rid;
671 status = NT_STATUS_OK;
672 DEBUG(3,("ads lookup_groupmem for rid=%d\n", group_rid));
674 if (res) ads_msgfree(ads, res);
679 /* find the sequence number for a domain */
680 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
682 ADS_STRUCT *ads = NULL;
685 *seq = DOM_SEQUENCE_NONE;
687 ads = ads_cached_connection(domain);
688 if (!ads) return NT_STATUS_UNSUCCESSFUL;
690 rc = ads_USN(ads, seq);
691 return ads_ntstatus(rc);
694 /* get a list of trusted domains */
695 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
707 ads = ads_cached_connection(domain);
708 if (!ads) return NT_STATUS_UNSUCCESSFUL;
710 rc = ads_trusted_domains(ads, mem_ctx, num_domains, names, dom_sids);
712 return ads_ntstatus(rc);
715 /* find the domain sid for a domain */
716 static NTSTATUS domain_sid(struct winbindd_domain *domain, DOM_SID *sid)
721 ads = ads_cached_connection(domain);
722 if (!ads) return NT_STATUS_UNSUCCESSFUL;
724 rc = ads_domain_sid(ads, sid);
726 return ads_ntstatus(rc);
729 /* the ADS backend methods are exposed via this structure */
730 struct winbindd_methods ads_methods = {