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)
44 time(NULL) - ads->last_attempt < ADS_RECONNECT_TIME) {
45 return ADS_ERROR(LDAP_SERVER_DOWN);
48 bp = strdup(bind_path);
51 status = ads_do_search(ads, bp, scope, exp, attrs, res);
52 if (ADS_ERR_OK(status)) {
53 DEBUG(5,("Search for %s gave %d replies\n",
54 exp, ads_count_replies(ads, *res)));
59 if (*res) ads_msgfree(ads, *res);
61 DEBUG(1,("Reopening ads connection to %s after error %s\n",
62 ads->ldap_server, ads_errstr(status)));
67 status = ads_connect(ads);
68 if (!ADS_ERR_OK(status)) {
69 DEBUG(1,("ads_search_retry: failed to reconnect (%s)\n",
78 DEBUG(1,("ads reopen failed after error %s\n", ads_errstr(status)));
83 ADS_STATUS ads_search_retry(ADS_STRUCT *ads, void **res,
87 return ads_do_search_retry(ads, ads->bind_path, LDAP_SCOPE_SUBTREE,
91 ADS_STATUS ads_search_retry_dn(ADS_STRUCT *ads, void **res,
95 return ads_do_search_retry(ads, dn, LDAP_SCOPE_BASE,
96 "(objectclass=*)", attrs, res);
100 return our ads connections structure for a domain. We keep the connection
101 open to make things faster
103 static ADS_STRUCT *ads_cached_connection(struct winbindd_domain *domain)
108 struct in_addr server_ip;
111 if (domain->private) {
112 return (ADS_STRUCT *)domain->private;
115 /* we don't want this to affect the users ccache */
116 ccache = lock_path("winbindd_ccache");
117 SETENV("KRB5CCNAME", ccache, 1);
120 if (resolve_name(domain->name, &server_ip, 0x1b)) {
121 sname = inet_ntoa(server_ip);
123 if (strcasecmp(domain->name, lp_workgroup()) != 0) {
124 DEBUG(1,("can't find domain controller for %s\n", domain->name));
130 ads = ads_init(primary_realm, sname, NULL, NULL);
132 DEBUG(1,("ads_init for domain %s failed\n", domain->name));
136 /* the machine acct password might have change - fetch it every time */
137 SAFE_FREE(ads->password);
138 ads->password = secrets_fetch_machine_password();
140 status = ads_connect(ads);
141 if (!ADS_ERR_OK(status)) {
142 extern struct winbindd_methods msrpc_methods;
143 DEBUG(1,("ads_connect for domain %s failed: %s\n",
144 domain->name, ads_errstr(status)));
147 /* if we get ECONNREFUSED then it might be a NT4
148 server, fall back to MSRPC */
149 if (status.error_type == ADS_ERROR_SYSTEM &&
150 status.rc == ECONNREFUSED) {
151 DEBUG(1,("Trying MSRPC methods\n"));
152 domain->methods = &msrpc_methods;
157 /* remember our primary realm for trusted domain support */
158 if (!primary_realm) {
159 primary_realm = strdup(ads->realm);
162 fstrcpy(domain->full_name, ads->server_realm);
164 domain->private = (void *)ads;
169 static void sid_from_rid(struct winbindd_domain *domain, uint32 rid, DOM_SID *sid)
171 sid_copy(sid, &domain->sid);
172 sid_append_rid(sid, rid);
175 /* turn a sAMAccountType into a SID_NAME_USE */
176 static enum SID_NAME_USE ads_atype_map(uint32 atype)
178 switch (atype & 0xF0000000) {
180 return SID_NAME_DOM_GRP;
182 return SID_NAME_USER;
184 DEBUG(1,("hmm, need to map account type 0x%x\n", atype));
186 return SID_NAME_UNKNOWN;
189 /* Query display info for a realm. This is the basic user list fn */
190 static NTSTATUS query_user_list(struct winbindd_domain *domain,
193 WINBIND_USERINFO **info)
195 ADS_STRUCT *ads = NULL;
196 const char *attrs[] = {"sAMAccountName", "name", "objectSid", "primaryGroupID",
197 "sAMAccountType", NULL};
202 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
206 DEBUG(3,("ads: query_user_list\n"));
208 ads = ads_cached_connection(domain);
211 rc = ads_search_retry(ads, &res, "(objectCategory=user)", attrs);
212 if (!ADS_ERR_OK(rc)) {
213 DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
217 count = ads_count_replies(ads, res);
219 DEBUG(1,("query_user_list: No users found\n"));
223 (*info) = talloc(mem_ctx, count * sizeof(**info));
225 status = NT_STATUS_NO_MEMORY;
231 for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
237 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &atype) ||
238 ads_atype_map(atype) != SID_NAME_USER) {
239 DEBUG(1,("Not a user account? atype=0x%x\n", atype));
243 name = ads_pull_string(ads, mem_ctx, msg, "sAMAccountName");
244 gecos = ads_pull_string(ads, mem_ctx, msg, "name");
245 if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
246 DEBUG(1,("No sid for %s !?\n", name));
249 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &group)) {
250 DEBUG(1,("No primary group for %s !?\n", name));
254 if (!sid_peek_rid(&sid, &rid)) {
255 DEBUG(1,("No rid for %s !?\n", name));
259 (*info)[i].acct_name = name;
260 (*info)[i].full_name = gecos;
261 (*info)[i].user_rid = rid;
262 (*info)[i].group_rid = group;
267 status = NT_STATUS_OK;
269 DEBUG(3,("ads query_user_list gave %d entries\n", (*num_entries)));
272 if (res) ads_msgfree(ads, res);
277 /* list all domain groups */
278 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
281 struct acct_info **info)
283 ADS_STRUCT *ads = NULL;
284 const char *attrs[] = {"sAMAccountName", "name", "objectSid",
285 "sAMAccountType", NULL};
290 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
294 DEBUG(3,("ads: enum_dom_groups\n"));
296 ads = ads_cached_connection(domain);
299 rc = ads_search_retry(ads, &res, "(objectCategory=group)", attrs);
300 if (!ADS_ERR_OK(rc)) {
301 DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
305 count = ads_count_replies(ads, res);
307 DEBUG(1,("query_user_list: No users found\n"));
311 (*info) = talloc(mem_ctx, count * sizeof(**info));
313 status = NT_STATUS_NO_MEMORY;
319 for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
325 if (!ads_pull_uint32(ads, msg, "sAMAccountType",
327 !(account_type & ATYPE_GROUP)) continue;
329 name = ads_pull_string(ads, mem_ctx, msg, "sAMAccountName");
330 gecos = ads_pull_string(ads, mem_ctx, msg, "name");
331 if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
332 DEBUG(1,("No sid for %s !?\n", name));
336 if (!sid_peek_rid(&sid, &rid)) {
337 DEBUG(1,("No rid for %s !?\n", name));
341 fstrcpy((*info)[i].acct_name, name);
342 fstrcpy((*info)[i].acct_desc, gecos);
343 (*info)[i].rid = rid;
349 status = NT_STATUS_OK;
351 DEBUG(3,("ads enum_dom_groups gave %d entries\n", (*num_entries)));
354 if (res) ads_msgfree(ads, res);
360 /* convert a single name to a sid in a domain */
361 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
364 enum SID_NAME_USE *type)
366 ADS_STRUCT *ads = NULL;
367 const char *attrs[] = {"objectSid", "sAMAccountType", NULL};
373 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
375 DEBUG(3,("ads: name_to_sid\n"));
377 ads = ads_cached_connection(domain);
380 asprintf(&exp, "(sAMAccountName=%s)", name);
381 rc = ads_search_retry(ads, &res, exp, attrs);
383 if (!ADS_ERR_OK(rc)) {
384 DEBUG(1,("name_to_sid ads_search: %s\n", ads_errstr(rc)));
388 count = ads_count_replies(ads, res);
390 DEBUG(1,("name_to_sid: %s not found\n", name));
394 if (!ads_pull_sid(ads, res, "objectSid", sid)) {
395 DEBUG(1,("No sid for %s !?\n", name));
399 if (!ads_pull_uint32(ads, res, "sAMAccountType", &t)) {
400 DEBUG(1,("No sAMAccountType for %s !?\n", name));
404 *type = ads_atype_map(t);
406 status = NT_STATUS_OK;
408 DEBUG(3,("ads name_to_sid mapped %s\n", name));
411 if (res) ads_msgfree(ads, res);
416 /* convert a sid to a user or group name */
417 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
421 enum SID_NAME_USE *type)
423 ADS_STRUCT *ads = NULL;
424 const char *attrs[] = {"sAMAccountName", "sAMAccountType", NULL};
431 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
433 DEBUG(3,("ads: sid_to_name\n"));
435 ads = ads_cached_connection(domain);
438 sidstr = sid_binstring(sid);
439 asprintf(&exp, "(objectSid=%s)", sidstr);
440 rc = ads_search_retry(ads, &msg, exp, attrs);
443 if (!ADS_ERR_OK(rc)) {
444 DEBUG(1,("sid_to_name ads_search: %s\n", ads_errstr(rc)));
448 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &atype)) {
452 s = ads_pull_string(ads, mem_ctx, msg, "sAMAccountName");
453 *name = talloc_strdup(mem_ctx, s);
454 *type = ads_atype_map(atype);
456 status = NT_STATUS_OK;
458 DEBUG(3,("ads sid_to_name mapped %s\n", *name));
461 if (msg) ads_msgfree(ads, msg);
467 /* Lookup user information from a rid */
468 static NTSTATUS query_user(struct winbindd_domain *domain,
471 WINBIND_USERINFO *info)
473 ADS_STRUCT *ads = NULL;
474 const char *attrs[] = {"sAMAccountName", "name", "objectSid",
475 "primaryGroupID", NULL};
482 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
484 DEBUG(3,("ads: query_user\n"));
486 sid_from_rid(domain, user_rid, &sid);
488 ads = ads_cached_connection(domain);
491 sidstr = sid_binstring(&sid);
492 asprintf(&exp, "(objectSid=%s)", sidstr);
493 rc = ads_search_retry(ads, &msg, exp, attrs);
496 if (!ADS_ERR_OK(rc)) {
497 DEBUG(1,("query_user(rid=%d) ads_search: %s\n", user_rid, ads_errstr(rc)));
501 count = ads_count_replies(ads, msg);
503 DEBUG(1,("query_user(rid=%d): Not found\n", user_rid));
507 info->acct_name = ads_pull_string(ads, mem_ctx, msg, "sAMAccountName");
508 info->full_name = ads_pull_string(ads, mem_ctx, msg, "name");
509 if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
510 DEBUG(1,("No sid for %d !?\n", user_rid));
513 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &info->group_rid)) {
514 DEBUG(1,("No primary group for %d !?\n", user_rid));
518 if (!sid_peek_rid(&sid, &info->user_rid)) {
519 DEBUG(1,("No rid for %d !?\n", user_rid));
523 status = NT_STATUS_OK;
525 DEBUG(3,("ads query_user gave %s\n", info->acct_name));
527 if (msg) ads_msgfree(ads, msg);
533 /* Lookup groups a user is a member of. */
534 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
537 uint32 *num_groups, uint32 **user_gids)
539 ADS_STRUCT *ads = NULL;
540 const char *attrs[] = {"distinguishedName", NULL};
541 const char *attrs2[] = {"tokenGroups", "primaryGroupID", NULL};
549 uint32 primary_group;
552 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
556 DEBUG(3,("ads: lookup_usergroups\n"));
560 sid_from_rid(domain, user_rid, &sid);
562 ads = ads_cached_connection(domain);
565 sidstr = sid_binstring(&sid);
566 asprintf(&exp, "(objectSid=%s)", sidstr);
567 rc = ads_search_retry(ads, &msg, exp, attrs);
570 if (!ADS_ERR_OK(rc)) {
571 DEBUG(1,("lookup_usergroups(rid=%d) ads_search: %s\n", user_rid, ads_errstr(rc)));
575 user_dn = ads_pull_string(ads, mem_ctx, msg, "distinguishedName");
577 if (msg) ads_msgfree(ads, msg);
579 rc = ads_search_retry_dn(ads, &msg, user_dn, attrs2);
580 if (!ADS_ERR_OK(rc)) {
581 DEBUG(1,("lookup_usergroups(rid=%d) ads_search tokenGroups: %s\n", user_rid, ads_errstr(rc)));
585 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &primary_group)) {
586 DEBUG(1,("%s: No primary group for rid=%d !?\n", domain->name, user_rid));
590 count = ads_pull_sids(ads, mem_ctx, msg, "tokenGroups", &sids) + 1;
591 (*user_gids) = (uint32 *)talloc(mem_ctx, sizeof(uint32) * count);
592 (*user_gids)[(*num_groups)++] = primary_group;
594 for (i=1;i<count;i++) {
596 if (!sid_peek_rid(&sids[i-1], &rid)) continue;
597 (*user_gids)[*num_groups] = rid;
601 status = NT_STATUS_OK;
602 DEBUG(3,("ads lookup_usergroups for rid=%d\n", user_rid));
604 if (msg) ads_msgfree(ads, msg);
610 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
612 uint32 group_rid, uint32 *num_names,
613 uint32 **rid_mem, char ***names,
618 const char *attrs[] = {"sAMAccountName", "objectSid", "sAMAccountType", NULL};
621 void *res=NULL, *msg=NULL;
622 ADS_STRUCT *ads = NULL;
624 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
628 ads = ads_cached_connection(domain);
631 sid_from_rid(domain, group_rid, &group_sid);
632 sidstr = sid_binstring(&group_sid);
633 /* search for all users who have that group sid as primary group or as member */
634 asprintf(&exp, "(&(objectCategory=user)(|(primaryGroupID=%d)(memberOf=%s)))",
636 rc = ads_search_retry(ads, &res, exp, attrs);
639 if (!ADS_ERR_OK(rc)) {
640 DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
644 count = ads_count_replies(ads, res);
646 status = NT_STATUS_OK;
650 (*rid_mem) = talloc(mem_ctx, sizeof(uint32) * count);
651 (*name_types) = talloc(mem_ctx, sizeof(uint32) * count);
652 (*names) = talloc(mem_ctx, sizeof(char *) * count);
654 for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
658 (*names)[*num_names] = ads_pull_string(ads, mem_ctx, msg, "sAMAccountName");
659 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &atype)) {
662 (*name_types)[*num_names] = ads_atype_map(atype);
663 if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
664 DEBUG(1,("No sid for %s !?\n", (*names)[*num_names]));
667 if (!sid_peek_rid(&sid, &rid)) {
668 DEBUG(1,("No rid for %s !?\n", (*names)[*num_names]));
671 (*rid_mem)[*num_names] = rid;
675 status = NT_STATUS_OK;
676 DEBUG(3,("ads lookup_groupmem for rid=%d\n", group_rid));
678 if (res) ads_msgfree(ads, res);
683 /* find the sequence number for a domain */
684 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
686 ADS_STRUCT *ads = NULL;
689 *seq = DOM_SEQUENCE_NONE;
691 ads = ads_cached_connection(domain);
692 if (!ads) return NT_STATUS_UNSUCCESSFUL;
694 rc = ads_USN(ads, seq);
695 return ads_ntstatus(rc);
698 /* get a list of trusted domains */
699 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
711 ads = ads_cached_connection(domain);
712 if (!ads) return NT_STATUS_UNSUCCESSFUL;
714 rc = ads_trusted_domains(ads, mem_ctx, num_domains, names, dom_sids);
716 return ads_ntstatus(rc);
719 /* find the domain sid for a domain */
720 static NTSTATUS domain_sid(struct winbindd_domain *domain, DOM_SID *sid)
725 ads = ads_cached_connection(domain);
726 if (!ads) return NT_STATUS_UNSUCCESSFUL;
728 rc = ads_domain_sid(ads, sid);
730 if (!ADS_ERR_OK(rc)) {
731 /* its a dead connection */
733 domain->private = NULL;
736 return ads_ntstatus(rc);
739 /* the ADS backend methods are exposed via this structure */
740 struct winbindd_methods ads_methods = {