2 Unix SMB/CIFS implementation.
4 Winbind ADS backend functions
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
8 Copyright (C) Gerald (Jerry) Carter 2004
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #define DBGC_CLASS DBGC_WINBIND
34 return our ads connections structure for a domain. We keep the connection
35 open to make things faster
37 static ADS_STRUCT *ads_cached_connection(struct winbindd_domain *domain)
42 if (domain->private_data) {
43 ads = (ADS_STRUCT *)domain->private_data;
45 /* check for a valid structure */
47 DEBUG(7, ("Current tickets expire at %d, time is now %d\n",
48 (uint32) ads->auth.expire, (uint32) time(NULL)));
49 if ( ads->config.realm && (ads->auth.expire > time(NULL))) {
53 /* we own this ADS_STRUCT so make sure it goes away */
56 ads_kdestroy("MEMORY:winbind_ccache");
57 domain->private_data = NULL;
61 /* we don't want this to affect the users ccache */
62 setenv("KRB5CCNAME", "MEMORY:winbind_ccache", 1);
64 ads = ads_init(domain->alt_name, domain->name, NULL);
66 DEBUG(1,("ads_init for domain %s failed\n", domain->name));
70 /* the machine acct password might have change - fetch it every time */
71 SAFE_FREE(ads->auth.password);
72 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
74 SAFE_FREE(ads->auth.realm);
75 ads->auth.realm = SMB_STRDUP(lp_realm());
77 status = ads_connect(ads);
78 if (!ADS_ERR_OK(status) || !ads->config.realm) {
79 extern struct winbindd_methods msrpc_methods, cache_methods;
80 DEBUG(1,("ads_connect for domain %s failed: %s\n",
81 domain->name, ads_errstr(status)));
84 /* if we get ECONNREFUSED then it might be a NT4
85 server, fall back to MSRPC */
86 if (status.error_type == ENUM_ADS_ERROR_SYSTEM &&
87 status.err.rc == ECONNREFUSED) {
88 DEBUG(1,("Trying MSRPC methods\n"));
89 if (domain->methods == &cache_methods) {
90 domain->backend = &msrpc_methods;
92 domain->methods = &msrpc_methods;
98 if (use_nss_info("sfu") && (!ads_check_sfu_mapping(ads))) {
99 DEBUG(0,("ads_cached_connection: failed to check sfu attributes\n"));
103 /* set the flag that says we don't own the memory even
104 though we do so that ads_destroy() won't destroy the
105 structure we pass back by reference */
107 ads->is_mine = False;
109 domain->private_data = (void *)ads;
114 /* Query display info for a realm. This is the basic user list fn */
115 static NTSTATUS query_user_list(struct winbindd_domain *domain,
118 WINBIND_USERINFO **info)
120 ADS_STRUCT *ads = NULL;
121 const char *attrs[] = {"userPrincipalName",
123 "name", "objectSid", "primaryGroupID",
125 ADS_ATTR_SFU_HOMEDIR_OID,
126 ADS_ATTR_SFU_SHELL_OID,
132 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
136 DEBUG(3,("ads: query_user_list\n"));
138 ads = ads_cached_connection(domain);
141 domain->last_status = NT_STATUS_SERVER_DISABLED;
145 rc = ads_search_retry(ads, &res, "(objectClass=user)", attrs);
146 if (!ADS_ERR_OK(rc) || !res) {
147 DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
151 count = ads_count_replies(ads, res);
153 DEBUG(1,("query_user_list: No users found\n"));
157 (*info) = TALLOC_ZERO_ARRAY(mem_ctx, WINBIND_USERINFO, count);
159 status = NT_STATUS_NO_MEMORY;
165 for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
167 char *homedir = NULL;
172 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &atype) ||
173 ads_atype_map(atype) != SID_NAME_USER) {
174 DEBUG(1,("Not a user account? atype=0x%x\n", atype));
178 name = ads_pull_username(ads, mem_ctx, msg);
179 gecos = ads_pull_string(ads, mem_ctx, msg, "name");
180 if (use_nss_info("sfu")) {
181 homedir = ads_pull_string(ads, mem_ctx, msg, ads->schema.sfu_homedir_attr);
182 shell = ads_pull_string(ads, mem_ctx, msg, ads->schema.sfu_shell_attr);
185 if (!ads_pull_sid(ads, msg, "objectSid",
186 &(*info)[i].user_sid)) {
187 DEBUG(1,("No sid for %s !?\n", name));
190 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &group)) {
191 DEBUG(1,("No primary group for %s !?\n", name));
195 (*info)[i].acct_name = name;
196 (*info)[i].full_name = gecos;
197 (*info)[i].homedir = homedir;
198 (*info)[i].shell = shell;
199 sid_compose(&(*info)[i].group_sid, &domain->sid, group);
204 status = NT_STATUS_OK;
206 DEBUG(3,("ads query_user_list gave %d entries\n", (*num_entries)));
210 ads_msgfree(ads, res);
215 /* list all domain groups */
216 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
219 struct acct_info **info)
221 ADS_STRUCT *ads = NULL;
222 const char *attrs[] = {"userPrincipalName", "sAMAccountName",
224 "sAMAccountType", NULL};
229 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
234 DEBUG(3,("ads: enum_dom_groups\n"));
236 ads = ads_cached_connection(domain);
239 domain->last_status = NT_STATUS_SERVER_DISABLED;
243 rc = ads_search_retry(ads, &res, "(objectCategory=group)", attrs);
244 if (!ADS_ERR_OK(rc) || !res) {
245 DEBUG(1,("enum_dom_groups ads_search: %s\n", ads_errstr(rc)));
249 count = ads_count_replies(ads, res);
251 DEBUG(1,("enum_dom_groups: No groups found\n"));
255 (*info) = TALLOC_ZERO_ARRAY(mem_ctx, struct acct_info, count);
257 status = NT_STATUS_NO_MEMORY;
263 group_flags = ATYPE_GLOBAL_GROUP;
265 /* only grab domain local groups for our domain */
266 if ( domain->native_mode && strequal(lp_realm(), domain->alt_name) )
267 group_flags |= ATYPE_LOCAL_GROUP;
269 for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
275 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &account_type) || !(account_type & group_flags) )
278 name = ads_pull_username(ads, mem_ctx, msg);
279 gecos = ads_pull_string(ads, mem_ctx, msg, "name");
280 if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
281 DEBUG(1,("No sid for %s !?\n", name));
285 if (sid_check_is_in_builtin(&sid))
288 if (!sid_peek_check_rid(&domain->sid, &sid, &rid)) {
289 DEBUG(1,("No rid for %s !?\n", name));
293 fstrcpy((*info)[i].acct_name, name);
294 fstrcpy((*info)[i].acct_desc, gecos);
295 (*info)[i].rid = rid;
301 status = NT_STATUS_OK;
303 DEBUG(3,("ads enum_dom_groups gave %d entries\n", (*num_entries)));
307 ads_msgfree(ads, res);
312 /* list all domain local groups */
313 static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
316 struct acct_info **info)
319 * This is a stub function only as we returned the domain
320 * local groups in enum_dom_groups() if the domain->native field
321 * was true. This is a simple performance optimization when
324 * if we ever need to enumerate domain local groups separately,
325 * then this the optimization in enum_dom_groups() will need
333 /* convert a DN to a name, SID and name type
334 this might become a major speed bottleneck if groups have
335 lots of users, in which case we could cache the results
337 static BOOL dn_lookup(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
339 char **name, uint32 *name_type, DOM_SID *sid)
342 const char *attrs[] = {"userPrincipalName", "sAMAccountName",
343 "objectSid", "sAMAccountType", NULL};
346 DEBUG(3,("ads: dn_lookup\n"));
348 rc = ads_search_retry_dn(ads, &res, dn, attrs);
350 if (!ADS_ERR_OK(rc) || !res) {
354 (*name) = ads_pull_username(ads, mem_ctx, res);
356 if (!ads_pull_uint32(ads, res, "sAMAccountType", &atype)) {
359 (*name_type) = ads_atype_map(atype);
361 if (!ads_pull_sid(ads, res, "objectSid", sid)) {
366 ads_msgfree(ads, res);
372 ads_msgfree(ads, res);
377 /* Lookup user information from a rid */
378 static NTSTATUS query_user(struct winbindd_domain *domain,
381 WINBIND_USERINFO *info)
383 ADS_STRUCT *ads = NULL;
384 const char *attrs[] = {"userPrincipalName",
388 ADS_ATTR_SFU_HOMEDIR_OID,
389 ADS_ATTR_SFU_SHELL_OID,
397 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
399 DEBUG(3,("ads: query_user\n"));
401 ads = ads_cached_connection(domain);
404 domain->last_status = NT_STATUS_SERVER_DISABLED;
408 sidstr = sid_binstring(sid);
409 asprintf(&ldap_exp, "(objectSid=%s)", sidstr);
410 rc = ads_search_retry(ads, &msg, ldap_exp, attrs);
413 if (!ADS_ERR_OK(rc) || !msg) {
414 DEBUG(1,("query_user(sid=%s) ads_search: %s\n",
415 sid_string_static(sid), ads_errstr(rc)));
419 count = ads_count_replies(ads, msg);
421 DEBUG(1,("query_user(sid=%s): Not found\n",
422 sid_string_static(sid)));
426 info->acct_name = ads_pull_username(ads, mem_ctx, msg);
427 info->full_name = ads_pull_string(ads, mem_ctx, msg, "name");
429 if (use_nss_info("sfu")) {
430 info->homedir = ads_pull_string(ads, mem_ctx, msg, ads->schema.sfu_homedir_attr);
431 info->shell = ads_pull_string(ads, mem_ctx, msg, ads->schema.sfu_shell_attr);
434 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &group_rid)) {
435 DEBUG(1,("No primary group for %s !?\n",
436 sid_string_static(sid)));
440 sid_copy(&info->user_sid, sid);
441 sid_compose(&info->group_sid, &domain->sid, group_rid);
443 status = NT_STATUS_OK;
445 DEBUG(3,("ads query_user gave %s\n", info->acct_name));
448 ads_msgfree(ads, msg);
453 /* Lookup groups a user is a member of - alternate method, for when
454 tokenGroups are not available. */
455 static NTSTATUS lookup_usergroups_alt(struct winbindd_domain *domain,
458 DOM_SID *primary_group,
459 uint32 *num_groups, DOM_SID **user_sids)
462 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
468 const char *group_attrs[] = {"objectSid", NULL};
471 DEBUG(3,("ads: lookup_usergroups_alt\n"));
473 ads = ads_cached_connection(domain);
476 domain->last_status = NT_STATUS_SERVER_DISABLED;
480 if (!(escaped_dn = escape_ldap_string_alloc(user_dn))) {
481 status = NT_STATUS_NO_MEMORY;
485 /* buggy server, no tokenGroups. Instead lookup what groups this user
486 is a member of by DN search on member*/
488 if (!(ldap_exp = talloc_asprintf(mem_ctx, "(&(member=%s)(objectClass=group))", escaped_dn))) {
489 DEBUG(1,("lookup_usergroups(dn=%s) asprintf failed!\n", user_dn));
490 SAFE_FREE(escaped_dn);
491 status = NT_STATUS_NO_MEMORY;
495 SAFE_FREE(escaped_dn);
497 rc = ads_search_retry(ads, &res, ldap_exp, group_attrs);
499 if (!ADS_ERR_OK(rc) || !res) {
500 DEBUG(1,("lookup_usergroups ads_search member=%s: %s\n", user_dn, ads_errstr(rc)));
501 return ads_ntstatus(rc);
504 count = ads_count_replies(ads, res);
509 /* always add the primary group to the sid array */
510 add_sid_to_array(mem_ctx, primary_group, user_sids, num_groups);
513 for (msg = ads_first_entry(ads, res); msg;
514 msg = ads_next_entry(ads, msg)) {
517 if (!ads_pull_sid(ads, msg, "objectSid", &group_sid)) {
518 DEBUG(1,("No sid for this group ?!?\n"));
522 add_sid_to_array(mem_ctx, &group_sid, user_sids,
528 status = (user_sids != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
530 DEBUG(3,("ads lookup_usergroups (alt) for dn=%s\n", user_dn));
533 ads_msgfree(ads, res);
538 /* Lookup groups a user is a member of. */
539 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
542 uint32 *num_groups, DOM_SID **user_sids)
544 ADS_STRUCT *ads = NULL;
545 const char *attrs[] = {"tokenGroups", "primaryGroupID", NULL};
548 LDAPMessage *msg = NULL;
552 DOM_SID primary_group;
553 uint32 primary_group_rid;
555 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
557 DEBUG(3,("ads: lookup_usergroups\n"));
560 ads = ads_cached_connection(domain);
563 domain->last_status = NT_STATUS_SERVER_DISABLED;
564 status = NT_STATUS_SERVER_DISABLED;
568 rc = ads_sid_to_dn(ads, mem_ctx, sid, &user_dn);
569 if (!ADS_ERR_OK(rc)) {
570 status = ads_ntstatus(rc);
574 rc = ads_search_retry_dn(ads, (void**)&msg, user_dn, attrs);
575 if (!ADS_ERR_OK(rc)) {
576 status = ads_ntstatus(rc);
577 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: %s\n",
578 sid_to_string(sid_string, sid), ads_errstr(rc)));
583 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: NULL msg\n",
584 sid_to_string(sid_string, sid)));
585 status = NT_STATUS_UNSUCCESSFUL;
589 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &primary_group_rid)) {
590 DEBUG(1,("%s: No primary group for sid=%s !?\n",
591 domain->name, sid_to_string(sid_string, sid)));
595 sid_copy(&primary_group, &domain->sid);
596 sid_append_rid(&primary_group, primary_group_rid);
598 count = ads_pull_sids(ads, mem_ctx, msg, "tokenGroups", &sids);
601 ads_msgfree(ads, msg);
603 /* there must always be at least one group in the token,
604 unless we are talking to a buggy Win2k server */
606 return lookup_usergroups_alt(domain, mem_ctx, user_dn,
608 num_groups, user_sids);
614 add_sid_to_array(mem_ctx, &primary_group, user_sids, num_groups);
616 for (i=0;i<count;i++) {
618 /* ignore Builtin groups from ADS - Guenther */
619 if (sid_check_is_in_builtin(&sids[i])) {
623 add_sid_to_array_unique(mem_ctx, &sids[i],
624 user_sids, num_groups);
627 status = (user_sids != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
629 DEBUG(3,("ads lookup_usergroups for sid=%s\n",
630 sid_to_string(sid_string, sid)));
636 find the members of a group, given a group rid and domain
638 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
640 const DOM_SID *group_sid, uint32 *num_names,
641 DOM_SID **sid_mem, char ***names,
647 ADS_STRUCT *ads = NULL;
649 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
661 DEBUG(10,("ads: lookup_groupmem %s sid=%s\n", domain->name,
662 sid_string_static(group_sid)));
666 ads = ads_cached_connection(domain);
669 domain->last_status = NT_STATUS_SERVER_DISABLED;
673 sidstr = sid_binstring(group_sid);
675 /* search for all members of the group */
676 if (!(ldap_exp = talloc_asprintf(mem_ctx, "(objectSid=%s)",sidstr))) {
678 DEBUG(1, ("ads: lookup_groupmem: tallloc_asprintf for ldap_exp failed!\n"));
679 status = NT_STATUS_NO_MEMORY;
687 attrs = TALLOC_ARRAY(mem_ctx, const char *, 3);
688 attrs[1] = talloc_strdup(mem_ctx, "usnChanged");
692 if (num_members == 0)
693 attrs[0] = talloc_strdup(mem_ctx, "member");
695 DEBUG(10, ("Searching for attrs[0] = %s, attrs[1] = %s\n", attrs[0], attrs[1]));
697 rc = ads_search_retry(ads, &res, ldap_exp, attrs);
699 if (!ADS_ERR_OK(rc) || !res) {
700 DEBUG(1,("ads: lookup_groupmem ads_search: %s\n",
702 status = ads_ntstatus(rc);
706 count = ads_count_replies(ads, res);
710 if (num_members == 0) {
711 if (!ads_pull_uint32(ads, res, "usnChanged", &first_usn)) {
712 DEBUG(1, ("ads: lookup_groupmem could not pull usnChanged!\n"));
717 if (!ads_pull_uint32(ads, res, "usnChanged", ¤t_usn)) {
718 DEBUG(1, ("ads: lookup_groupmem could not pull usnChanged!\n"));
722 if (first_usn != current_usn) {
723 DEBUG(5, ("ads: lookup_groupmem USN on this record changed"
724 " - restarting search\n"));
725 if (num_retries < 5) {
730 DEBUG(5, ("ads: lookup_groupmem USN on this record changed"
731 " - restarted search too many times, aborting!\n"));
732 status = NT_STATUS_UNSUCCESSFUL;
737 members = ads_pull_strings_range(ads, mem_ctx, res,
744 if ((members == NULL) || (num_members == 0))
747 } while (more_values);
749 /* now we need to turn a list of members into rids, names and name types
750 the problem is that the members are in the form of distinguised names
753 (*sid_mem) = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
754 (*name_types) = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_members);
755 (*names) = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_members);
757 for (i=0;i<num_members;i++) {
762 if (dn_lookup(ads, mem_ctx, members[i], &name, &name_type, &sid)) {
763 (*names)[*num_names] = name;
764 (*name_types)[*num_names] = name_type;
765 sid_copy(&(*sid_mem)[*num_names], &sid);
770 status = NT_STATUS_OK;
771 DEBUG(3,("ads lookup_groupmem for sid=%s\n", sid_to_string(sid_string, group_sid)));
775 ads_msgfree(ads, res);
780 /* find the sequence number for a domain */
781 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
783 ADS_STRUCT *ads = NULL;
786 DEBUG(3,("ads: fetch sequence_number for %s\n", domain->name));
788 *seq = DOM_SEQUENCE_NONE;
790 ads = ads_cached_connection(domain);
793 domain->last_status = NT_STATUS_SERVER_DISABLED;
794 return NT_STATUS_UNSUCCESSFUL;
797 rc = ads_USN(ads, seq);
799 if (!ADS_ERR_OK(rc)) {
801 /* its a dead connection ; don't destroy it
802 through since ads_USN() has already done
805 domain->private_data = NULL;
807 return ads_ntstatus(rc);
810 /* get a list of trusted domains */
811 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
818 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
819 struct ds_domain_trust *domains = NULL;
822 /* i think we only need our forest and downlevel trusted domains */
823 uint32 flags = DS_DOMAIN_IN_FOREST | DS_DOMAIN_DIRECT_OUTBOUND;
824 struct rpc_pipe_client *cli;
826 DEBUG(3,("ads: trusted_domains\n"));
834 unsigned char *session_key;
837 result = cm_connect_netlogon(domain, mem_ctx, &cli,
838 &session_key, &creds);
841 if (!NT_STATUS_IS_OK(result)) {
842 DEBUG(5, ("trusted_domains: Could not open a connection to %s "
843 "for PIPE_NETLOGON (%s)\n",
844 domain->name, nt_errstr(result)));
845 return NT_STATUS_UNSUCCESSFUL;
848 if ( NT_STATUS_IS_OK(result) )
849 result = rpccli_ds_enum_domain_trusts(cli, mem_ctx,
852 (unsigned int *)&count);
854 if ( NT_STATUS_IS_OK(result) && count) {
856 /* Allocate memory for trusted domain names and sids */
858 if ( !(*names = TALLOC_ARRAY(mem_ctx, char *, count)) ) {
859 DEBUG(0, ("trusted_domains: out of memory\n"));
860 return NT_STATUS_NO_MEMORY;
863 if ( !(*alt_names = TALLOC_ARRAY(mem_ctx, char *, count)) ) {
864 DEBUG(0, ("trusted_domains: out of memory\n"));
865 return NT_STATUS_NO_MEMORY;
868 if ( !(*dom_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, count)) ) {
869 DEBUG(0, ("trusted_domains: out of memory\n"));
870 return NT_STATUS_NO_MEMORY;
873 /* Copy across names and sids */
875 for (i = 0; i < count; i++) {
876 (*names)[i] = domains[i].netbios_domain;
877 (*alt_names)[i] = domains[i].dns_domain;
879 sid_copy(&(*dom_sids)[i], &domains[i].sid);
882 *num_domains = count;
888 /* find alternate names list for the domain - for ADS this is the
890 static NTSTATUS alternate_name(struct winbindd_domain *domain)
895 const char *workgroup;
897 DEBUG(3,("ads: alternate_name\n"));
899 ads = ads_cached_connection(domain);
902 domain->last_status = NT_STATUS_SERVER_DISABLED;
903 return NT_STATUS_UNSUCCESSFUL;
906 if (!(ctx = talloc_init("alternate_name"))) {
907 return NT_STATUS_NO_MEMORY;
910 rc = ads_workgroup_name(ads, ctx, &workgroup);
912 if (ADS_ERR_OK(rc)) {
913 fstrcpy(domain->name, workgroup);
914 fstrcpy(domain->alt_name, ads->config.realm);
915 strupper_m(domain->alt_name);
916 strupper_m(domain->name);
921 return ads_ntstatus(rc);
924 /* the ADS backend methods are exposed via this structure */
925 struct winbindd_methods ads_methods = {
934 msrpc_lookup_useraliases,