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.
30 extern struct winbindd_methods msrpc_methods, cache_methods;
33 #define DBGC_CLASS DBGC_WINBIND
36 return our ads connections structure for a domain. We keep the connection
37 open to make things faster
39 static ADS_STRUCT *ads_cached_connection(struct winbindd_domain *domain)
44 if (domain->private) {
45 ads = (ADS_STRUCT *)domain->private;
47 /* check for a valid structure */
49 DEBUG(7, ("Current tickets expire at %d, time is now %d\n",
50 (uint32) ads->auth.expire, (uint32) time(NULL)));
51 if ( ads->config.realm && (ads->auth.expire > time(NULL))) {
55 /* we own this ADS_STRUCT so make sure it goes away */
58 ads_kdestroy("MEMORY:winbind_ccache");
59 domain->private = NULL;
63 /* we don't want this to affect the users ccache */
64 setenv("KRB5CCNAME", "MEMORY:winbind_ccache", 1);
66 ads = ads_init(domain->alt_name, domain->name, NULL);
68 DEBUG(1,("ads_init for domain %s failed\n", domain->name));
72 /* the machine acct password might have change - fetch it every time */
73 SAFE_FREE(ads->auth.password);
74 ads->auth.password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL);
76 SAFE_FREE(ads->auth.realm);
77 ads->auth.realm = SMB_STRDUP(lp_realm());
79 status = ads_connect(ads);
80 if (!ADS_ERR_OK(status) || !ads->config.realm) {
81 DEBUG(1,("ads_connect for domain %s failed: %s\n",
82 domain->name, ads_errstr(status)));
85 /* if we get ECONNREFUSED then it might be a NT4
86 server, fall back to MSRPC */
87 if (status.error_type == ENUM_ADS_ERROR_SYSTEM &&
88 status.err.rc == ECONNREFUSED) {
89 DEBUG(1,("Trying MSRPC methods\n"));
90 if (domain->methods == &cache_methods) {
91 domain->backend = &msrpc_methods;
93 domain->methods = &msrpc_methods;
99 /* set the flag that says we don't own the memory even
100 though we do so that ads_destroy() won't destroy the
101 structure we pass back by reference */
103 ads->is_mine = False;
105 domain->private = (void *)ads;
110 /* Query display info for a realm. This is the basic user list fn */
111 static NTSTATUS query_user_list(struct winbindd_domain *domain,
114 WINBIND_USERINFO **info)
116 ADS_STRUCT *ads = NULL;
117 const char *attrs[] = {"userPrincipalName",
119 "name", "objectSid", "primaryGroupID",
120 "sAMAccountType", NULL};
125 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
129 DEBUG(3,("ads: query_user_list\n"));
131 ads = ads_cached_connection(domain);
134 domain->last_status = NT_STATUS_SERVER_DISABLED;
138 rc = ads_search_retry(ads, &res, "(objectClass=user)", attrs);
139 if (!ADS_ERR_OK(rc) || !res) {
140 DEBUG(1,("query_user_list ads_search: %s\n", ads_errstr(rc)));
144 count = ads_count_replies(ads, res);
146 DEBUG(1,("query_user_list: No users found\n"));
150 (*info) = TALLOC_ZERO_ARRAY(mem_ctx, WINBIND_USERINFO, count);
152 status = NT_STATUS_NO_MEMORY;
158 for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
166 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &atype) ||
167 ads_atype_map(atype) != SID_NAME_USER) {
168 DEBUG(1,("Not a user account? atype=0x%x\n", atype));
172 name = ads_pull_username(ads, mem_ctx, msg);
173 gecos = ads_pull_string(ads, mem_ctx, msg, "name");
174 if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
175 DEBUG(1,("No sid for %s !?\n", name));
178 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &group)) {
179 DEBUG(1,("No primary group for %s !?\n", name));
183 sid2 = TALLOC_P(mem_ctx, DOM_SID);
185 status = NT_STATUS_NO_MEMORY;
189 sid_copy(sid2, &sid);
191 group_sid = rid_to_talloced_sid(domain, mem_ctx, group);
193 (*info)[i].acct_name = name;
194 (*info)[i].full_name = gecos;
195 (*info)[i].user_sid = sid2;
196 (*info)[i].group_sid = group_sid;
201 status = NT_STATUS_OK;
203 DEBUG(3,("ads query_user_list gave %d entries\n", (*num_entries)));
207 ads_msgfree(ads, res);
212 /* list all domain groups */
213 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
216 struct acct_info **info)
218 ADS_STRUCT *ads = NULL;
219 const char *attrs[] = {"userPrincipalName", "sAMAccountName",
221 "sAMAccountType", NULL};
226 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
231 DEBUG(3,("ads: enum_dom_groups\n"));
233 ads = ads_cached_connection(domain);
236 domain->last_status = NT_STATUS_SERVER_DISABLED;
240 rc = ads_search_retry(ads, &res, "(objectCategory=group)", attrs);
241 if (!ADS_ERR_OK(rc) || !res) {
242 DEBUG(1,("enum_dom_groups ads_search: %s\n", ads_errstr(rc)));
246 count = ads_count_replies(ads, res);
248 DEBUG(1,("enum_dom_groups: No groups found\n"));
252 (*info) = TALLOC_ZERO_ARRAY(mem_ctx, struct acct_info, count);
254 status = NT_STATUS_NO_MEMORY;
260 group_flags = ATYPE_GLOBAL_GROUP;
262 /* only grab domain local groups for our domain */
263 if ( domain->native_mode && strequal(lp_realm(), domain->alt_name) )
264 group_flags |= ATYPE_LOCAL_GROUP;
266 for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
272 if (!ads_pull_uint32(ads, msg, "sAMAccountType", &account_type) || !(account_type & group_flags) )
275 name = ads_pull_username(ads, mem_ctx, msg);
276 gecos = ads_pull_string(ads, mem_ctx, msg, "name");
277 if (!ads_pull_sid(ads, msg, "objectSid", &sid)) {
278 DEBUG(1,("No sid for %s !?\n", name));
282 if (!sid_peek_check_rid(&domain->sid, &sid, &rid)) {
283 DEBUG(1,("No rid for %s !?\n", name));
287 fstrcpy((*info)[i].acct_name, name);
288 fstrcpy((*info)[i].acct_desc, gecos);
289 (*info)[i].rid = rid;
295 status = NT_STATUS_OK;
297 DEBUG(3,("ads enum_dom_groups gave %d entries\n", (*num_entries)));
301 ads_msgfree(ads, res);
306 /* list all domain local groups */
307 static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
310 struct acct_info **info)
313 * This is a stub function only as we returned the domain
314 * local groups in enum_dom_groups() if the domain->native field
315 * was true. This is a simple performance optimization when
318 * if we ever need to enumerate domain local groups separately,
319 * then this the optimization in enum_dom_groups() will need
327 /* convert a DN to a name, SID and name type
328 this might become a major speed bottleneck if groups have
329 lots of users, in which case we could cache the results
331 static BOOL dn_lookup(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
333 char **name, uint32 *name_type, DOM_SID *sid)
336 const char *attrs[] = {"userPrincipalName", "sAMAccountName",
337 "objectSid", "sAMAccountType", NULL};
340 DEBUG(3,("ads: dn_lookup\n"));
342 rc = ads_search_retry_dn(ads, &res, dn, attrs);
344 if (!ADS_ERR_OK(rc) || !res) {
348 (*name) = ads_pull_username(ads, mem_ctx, res);
350 if (!ads_pull_uint32(ads, res, "sAMAccountType", &atype)) {
353 (*name_type) = ads_atype_map(atype);
355 if (!ads_pull_sid(ads, res, "objectSid", sid)) {
360 ads_msgfree(ads, res);
366 ads_msgfree(ads, res);
371 /* Lookup user information from a rid */
372 static NTSTATUS query_user(struct winbindd_domain *domain,
375 WINBIND_USERINFO *info)
377 ADS_STRUCT *ads = NULL;
378 const char *attrs[] = {"userPrincipalName",
381 "primaryGroupID", NULL};
388 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
392 DEBUG(3,("ads: query_user\n"));
394 ads = ads_cached_connection(domain);
397 domain->last_status = NT_STATUS_SERVER_DISABLED;
401 sidstr = sid_binstring(sid);
402 asprintf(&ldap_exp, "(objectSid=%s)", sidstr);
403 rc = ads_search_retry(ads, &msg, ldap_exp, attrs);
406 if (!ADS_ERR_OK(rc) || !msg) {
407 DEBUG(1,("query_user(sid=%s) ads_search: %s\n", sid_to_string(sid_string, sid), ads_errstr(rc)));
411 count = ads_count_replies(ads, msg);
413 DEBUG(1,("query_user(sid=%s): Not found\n", sid_to_string(sid_string, sid)));
417 info->acct_name = ads_pull_username(ads, mem_ctx, msg);
418 info->full_name = ads_pull_string(ads, mem_ctx, msg, "name");
420 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &group_rid)) {
421 DEBUG(1,("No primary group for %s !?\n", sid_to_string(sid_string, sid)));
425 sid2 = TALLOC_P(mem_ctx, DOM_SID);
427 status = NT_STATUS_NO_MEMORY;
432 info->user_sid = sid2;
434 info->group_sid = rid_to_talloced_sid(domain, mem_ctx, group_rid);
436 status = NT_STATUS_OK;
438 DEBUG(3,("ads query_user gave %s\n", info->acct_name));
441 ads_msgfree(ads, msg);
446 /* Lookup groups a user is a member of - alternate method, for when
447 tokenGroups are not available. */
448 static NTSTATUS lookup_usergroups_alt(struct winbindd_domain *domain,
451 DOM_SID *primary_group,
452 uint32 *num_groups, DOM_SID ***user_gids)
455 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
461 const char *group_attrs[] = {"objectSid", NULL};
464 DEBUG(3,("ads: lookup_usergroups_alt\n"));
466 ads = ads_cached_connection(domain);
469 domain->last_status = NT_STATUS_SERVER_DISABLED;
473 if (!(escaped_dn = escape_ldap_string_alloc(user_dn))) {
474 status = NT_STATUS_NO_MEMORY;
478 /* buggy server, no tokenGroups. Instead lookup what groups this user
479 is a member of by DN search on member*/
481 if (!(ldap_exp = talloc_asprintf(mem_ctx, "(&(member=%s)(objectClass=group))", escaped_dn))) {
482 DEBUG(1,("lookup_usergroups(dn=%s) asprintf failed!\n", user_dn));
483 SAFE_FREE(escaped_dn);
484 status = NT_STATUS_NO_MEMORY;
488 SAFE_FREE(escaped_dn);
490 rc = ads_search_retry(ads, &res, ldap_exp, group_attrs);
492 if (!ADS_ERR_OK(rc) || !res) {
493 DEBUG(1,("lookup_usergroups ads_search member=%s: %s\n", user_dn, ads_errstr(rc)));
494 return ads_ntstatus(rc);
497 count = ads_count_replies(ads, res);
499 DEBUG(5,("lookup_usergroups: No supp groups found\n"));
501 status = ads_ntstatus(rc);
505 (*user_gids) = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID *, count + 1);
506 (*user_gids)[0] = primary_group;
510 for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
513 if (!ads_pull_sid(ads, msg, "objectSid", &group_sid)) {
514 DEBUG(1,("No sid for this group ?!?\n"));
518 if (sid_equal(&group_sid, primary_group)) continue;
520 (*user_gids)[*num_groups] = TALLOC_P(mem_ctx, DOM_SID);
521 if (!(*user_gids)[*num_groups]) {
522 status = NT_STATUS_NO_MEMORY;
526 sid_copy((*user_gids)[*num_groups], &group_sid);
532 status = NT_STATUS_OK;
534 DEBUG(3,("ads lookup_usergroups (alt) for dn=%s\n", user_dn));
537 ads_msgfree(ads, res);
542 /* Lookup groups a user is a member of. */
543 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
546 uint32 *num_groups, DOM_SID ***user_gids)
548 ADS_STRUCT *ads = NULL;
549 const char *attrs[] = {"tokenGroups", "primaryGroupID", NULL};
552 LDAPMessage *msg = NULL;
556 DOM_SID *primary_group;
557 uint32 primary_group_rid;
559 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
561 DEBUG(3,("ads: lookup_usergroups\n"));
564 ads = ads_cached_connection(domain);
567 domain->last_status = NT_STATUS_SERVER_DISABLED;
568 status = NT_STATUS_SERVER_DISABLED;
572 rc = ads_sid_to_dn(ads, mem_ctx, sid, &user_dn);
573 if (!ADS_ERR_OK(rc)) {
574 status = ads_ntstatus(rc);
578 rc = ads_search_retry_dn(ads, (void**)&msg, user_dn, attrs);
579 if (!ADS_ERR_OK(rc)) {
580 status = ads_ntstatus(rc);
581 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: %s\n",
582 sid_to_string(sid_string, sid), ads_errstr(rc)));
587 DEBUG(1,("lookup_usergroups(sid=%s) ads_search tokenGroups: NULL msg\n",
588 sid_to_string(sid_string, sid)));
589 status = NT_STATUS_UNSUCCESSFUL;
593 if (!ads_pull_uint32(ads, msg, "primaryGroupID", &primary_group_rid)) {
594 DEBUG(1,("%s: No primary group for sid=%s !?\n",
595 domain->name, sid_to_string(sid_string, sid)));
599 primary_group = rid_to_talloced_sid(domain, mem_ctx, primary_group_rid);
601 count = ads_pull_sids(ads, mem_ctx, msg, "tokenGroups", &sids);
604 ads_msgfree(ads, msg);
606 /* there must always be at least one group in the token,
607 unless we are talking to a buggy Win2k server */
609 return lookup_usergroups_alt(domain, mem_ctx, user_dn,
611 num_groups, user_gids);
614 (*user_gids) = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID *, count + 1);
615 (*user_gids)[0] = primary_group;
619 for (i=0;i<count;i++) {
620 if (sid_equal(&sids[i], primary_group)) continue;
622 (*user_gids)[*num_groups] = TALLOC_P(mem_ctx, DOM_SID);
623 if (!(*user_gids)[*num_groups]) {
624 status = NT_STATUS_NO_MEMORY;
628 sid_copy((*user_gids)[*num_groups], &sids[i]);
632 status = NT_STATUS_OK;
633 DEBUG(3,("ads lookup_usergroups for sid=%s\n", sid_to_string(sid_string, sid)));
639 find the members of a group, given a group rid and domain
641 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
643 const DOM_SID *group_sid, uint32 *num_names,
644 DOM_SID ***sid_mem, char ***names,
650 ADS_STRUCT *ads = NULL;
652 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
664 DEBUG(10,("ads: lookup_groupmem %s sid=%s\n", domain->name,
665 sid_string_static(group_sid)));
669 ads = ads_cached_connection(domain);
672 domain->last_status = NT_STATUS_SERVER_DISABLED;
676 sidstr = sid_binstring(group_sid);
678 /* search for all members of the group */
679 if (!(ldap_exp = talloc_asprintf(mem_ctx, "(objectSid=%s)",sidstr))) {
681 DEBUG(1, ("ads: lookup_groupmem: tallloc_asprintf for ldap_exp failed!\n"));
682 status = NT_STATUS_NO_MEMORY;
690 attrs = TALLOC_ARRAY(mem_ctx, const char *, 3);
691 attrs[1] = talloc_strdup(mem_ctx, "usnChanged");
695 if (num_members == 0)
696 attrs[0] = talloc_strdup(mem_ctx, "member");
698 DEBUG(10, ("Searching for attrs[0] = %s, attrs[1] = %s\n", attrs[0], attrs[1]));
700 rc = ads_search_retry(ads, &res, ldap_exp, attrs);
702 if (!ADS_ERR_OK(rc) || !res) {
703 DEBUG(1,("ads: lookup_groupmem ads_search: %s\n",
705 status = ads_ntstatus(rc);
709 count = ads_count_replies(ads, res);
713 if (num_members == 0) {
714 if (!ads_pull_uint32(ads, res, "usnChanged", &first_usn)) {
715 DEBUG(1, ("ads: lookup_groupmem could not pull usnChanged!\n"));
720 if (!ads_pull_uint32(ads, res, "usnChanged", ¤t_usn)) {
721 DEBUG(1, ("ads: lookup_groupmem could not pull usnChanged!\n"));
725 if (first_usn != current_usn) {
726 DEBUG(5, ("ads: lookup_groupmem USN on this record changed"
727 " - restarting search\n"));
728 if (num_retries < 5) {
733 DEBUG(5, ("ads: lookup_groupmem USN on this record changed"
734 " - restarted search too many times, aborting!\n"));
735 status = NT_STATUS_UNSUCCESSFUL;
740 members = ads_pull_strings_range(ads, mem_ctx, res,
747 if ((members == NULL) || (num_members == 0))
750 } while (more_values);
752 /* now we need to turn a list of members into rids, names and name types
753 the problem is that the members are in the form of distinguised names
756 (*sid_mem) = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID *, num_members);
757 (*name_types) = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_members);
758 (*names) = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_members);
760 for (i=0;i<num_members;i++) {
765 if (dn_lookup(ads, mem_ctx, members[i], &name, &name_type, &sid)) {
766 (*names)[*num_names] = name;
767 (*name_types)[*num_names] = name_type;
768 (*sid_mem)[*num_names] = TALLOC_P(mem_ctx, DOM_SID);
769 if (!(*sid_mem)[*num_names]) {
770 status = NT_STATUS_NO_MEMORY;
773 sid_copy((*sid_mem)[*num_names], &sid);
778 status = NT_STATUS_OK;
779 DEBUG(3,("ads lookup_groupmem for sid=%s\n", sid_to_string(sid_string, group_sid)));
783 ads_msgfree(ads, res);
788 /* find the sequence number for a domain */
789 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
791 ADS_STRUCT *ads = NULL;
794 DEBUG(3,("ads: fetch sequence_number for %s\n", domain->name));
796 *seq = DOM_SEQUENCE_NONE;
798 ads = ads_cached_connection(domain);
801 domain->last_status = NT_STATUS_SERVER_DISABLED;
802 return NT_STATUS_UNSUCCESSFUL;
805 rc = ads_USN(ads, seq);
807 if (!ADS_ERR_OK(rc)) {
809 /* its a dead connection ; don't destroy it
810 through since ads_USN() has already done
813 domain->private = NULL;
815 return ads_ntstatus(rc);
818 /* get a list of trusted domains */
819 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
826 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
827 struct ds_domain_trust *domains = NULL;
830 struct cli_state *cli = NULL;
831 /* i think we only need our forest and downlevel trusted domains */
832 uint32 flags = DS_DOMAIN_IN_FOREST | DS_DOMAIN_DIRECT_OUTBOUND;
834 DEBUG(3,("ads: trusted_domains\n"));
841 if ( !NT_STATUS_IS_OK(result = cm_fresh_connection(domain, PI_NETLOGON, &cli)) ) {
842 DEBUG(5, ("trusted_domains: Could not open a connection to %s for PIPE_NETLOGON (%s)\n",
843 domain->name, nt_errstr(result)));
844 return NT_STATUS_UNSUCCESSFUL;
847 if ( NT_STATUS_IS_OK(result) )
848 result = cli_ds_enum_domain_trusts( cli, mem_ctx, cli->desthost,
849 flags, &domains, (unsigned int *)&count );
851 if ( NT_STATUS_IS_OK(result) && count) {
853 /* Allocate memory for trusted domain names and sids */
855 if ( !(*names = TALLOC_ARRAY(mem_ctx, char *, count)) ) {
856 DEBUG(0, ("trusted_domains: out of memory\n"));
857 result = NT_STATUS_NO_MEMORY;
861 if ( !(*alt_names = TALLOC_ARRAY(mem_ctx, char *, count)) ) {
862 DEBUG(0, ("trusted_domains: out of memory\n"));
863 result = NT_STATUS_NO_MEMORY;
867 if ( !(*dom_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, count)) ) {
868 DEBUG(0, ("trusted_domains: out of memory\n"));
869 result = 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;
887 /* remove connection; This is a special case to the \NETLOGON pipe */
895 /* find alternate names list for the domain - for ADS this is the
897 static NTSTATUS alternate_name(struct winbindd_domain *domain)
902 const char *workgroup;
904 DEBUG(3,("ads: alternate_name\n"));
906 ads = ads_cached_connection(domain);
909 domain->last_status = NT_STATUS_SERVER_DISABLED;
910 return NT_STATUS_UNSUCCESSFUL;
913 if (!(ctx = talloc_init("alternate_name"))) {
914 return NT_STATUS_NO_MEMORY;
917 rc = ads_workgroup_name(ads, ctx, &workgroup);
919 if (ADS_ERR_OK(rc)) {
920 fstrcpy(domain->name, workgroup);
921 fstrcpy(domain->alt_name, ads->config.realm);
922 strupper_m(domain->alt_name);
923 strupper_m(domain->name);
928 return ads_ntstatus(rc);
931 /* the ADS backend methods are exposed via this structure */
932 struct winbindd_methods ads_methods = {
941 msrpc_lookup_useraliases,