s3-winbind: Use rpc_lookup_useraliases in msrpc.
[Samba/bjacke.git] / source3 / winbindd / winbindd_msrpc.c
blob433423a39631aa283d88da49391f533aa2855a47
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind rpc backend functions
6 Copyright (C) Tim Potter 2000-2001,2003
7 Copyright (C) Andrew Tridgell 2001
8 Copyright (C) Volker Lendecke 2005
9 Copyright (C) Guenther Deschner 2008 (pidl conversion)
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "winbindd.h"
27 #include "winbindd_rpc.h"
29 #include "../librpc/gen_ndr/cli_samr.h"
30 #include "rpc_client/cli_samr.h"
31 #include "../librpc/gen_ndr/cli_lsa.h"
32 #include "rpc_client/cli_lsarpc.h"
34 #undef DBGC_CLASS
35 #define DBGC_CLASS DBGC_WINBIND
38 /* Query display info for a domain. This returns enough information plus a
39 bit extra to give an overview of domain users for the User Manager
40 application. */
41 static NTSTATUS msrpc_query_user_list(struct winbindd_domain *domain,
42 TALLOC_CTX *mem_ctx,
43 uint32_t *pnum_info,
44 struct wbint_userinfo **pinfo)
46 struct rpc_pipe_client *samr_pipe = NULL;
47 struct policy_handle dom_pol;
48 struct wbint_userinfo *info = NULL;
49 uint32_t num_info = 0;
50 TALLOC_CTX *tmp_ctx;
51 NTSTATUS status;
53 DEBUG(3,("rpc_query_user_list\n"));
55 if (pnum_info) {
56 *pnum_info = 0;
59 tmp_ctx = talloc_stackframe();
60 if (tmp_ctx == NULL) {
61 return NT_STATUS_NO_MEMORY;
64 if ( !winbindd_can_contact_domain( domain ) ) {
65 DEBUG(10,("query_user_list: No incoming trust for domain %s\n",
66 domain->name));
67 status = NT_STATUS_OK;
68 goto done;
71 status = cm_connect_sam(domain, tmp_ctx, &samr_pipe, &dom_pol);
72 if (!NT_STATUS_IS_OK(status)) {
73 goto done;
76 status = rpc_query_user_list(tmp_ctx,
77 samr_pipe,
78 &dom_pol,
79 &domain->sid,
80 &num_info,
81 &info);
82 if (!NT_STATUS_IS_OK(status)) {
83 goto done;
86 if (pnum_info) {
87 *pnum_info = num_info;
90 if (pinfo) {
91 *pinfo = talloc_move(mem_ctx, &info);
94 done:
95 TALLOC_FREE(tmp_ctx);
96 return status;
99 /* list all domain groups */
100 static NTSTATUS msrpc_enum_dom_groups(struct winbindd_domain *domain,
101 TALLOC_CTX *mem_ctx,
102 uint32_t *pnum_info,
103 struct acct_info **pinfo)
105 struct rpc_pipe_client *samr_pipe;
106 struct policy_handle dom_pol;
107 struct acct_info *info = NULL;
108 uint32_t num_info = 0;
109 TALLOC_CTX *tmp_ctx;
110 NTSTATUS status;
112 DEBUG(3,("msrpc_enum_dom_groups\n"));
114 if (pnum_info) {
115 *pnum_info = 0;
118 tmp_ctx = talloc_stackframe();
119 if (tmp_ctx == NULL) {
120 return NT_STATUS_NO_MEMORY;
123 if ( !winbindd_can_contact_domain( domain ) ) {
124 DEBUG(10,("enum_domain_groups: No incoming trust for domain %s\n",
125 domain->name));
126 status = NT_STATUS_OK;
127 goto done;
130 status = cm_connect_sam(domain, tmp_ctx, &samr_pipe, &dom_pol);
131 if (!NT_STATUS_IS_OK(status)) {
132 goto done;
135 status = rpc_enum_dom_groups(tmp_ctx,
136 samr_pipe,
137 &dom_pol,
138 &num_info,
139 &info);
140 if (!NT_STATUS_IS_OK(status)) {
141 goto done;
144 if (pnum_info) {
145 *pnum_info = num_info;
148 if (pinfo) {
149 *pinfo = talloc_move(mem_ctx, &info);
152 done:
153 TALLOC_FREE(tmp_ctx);
154 return status;
157 /* List all domain groups */
159 static NTSTATUS msrpc_enum_local_groups(struct winbindd_domain *domain,
160 TALLOC_CTX *mem_ctx,
161 uint32_t *pnum_info,
162 struct acct_info **pinfo)
164 struct rpc_pipe_client *samr_pipe;
165 struct policy_handle dom_pol;
166 struct acct_info *info = NULL;
167 uint32_t num_info = 0;
168 TALLOC_CTX *tmp_ctx;
169 NTSTATUS status;
171 DEBUG(3,("msrpc_enum_local_groups\n"));
173 if (pnum_info) {
174 *pnum_info = 0;
177 tmp_ctx = talloc_stackframe();
178 if (tmp_ctx == NULL) {
179 return NT_STATUS_NO_MEMORY;
182 if ( !winbindd_can_contact_domain( domain ) ) {
183 DEBUG(10,("enum_local_groups: No incoming trust for domain %s\n",
184 domain->name));
185 status = NT_STATUS_OK;
186 goto done;
189 status = cm_connect_sam(domain, tmp_ctx, &samr_pipe, &dom_pol);
190 if (!NT_STATUS_IS_OK(status)) {
191 goto done;
194 status = rpc_enum_local_groups(mem_ctx,
195 samr_pipe,
196 &dom_pol,
197 &num_info,
198 &info);
199 if (!NT_STATUS_IS_OK(status)) {
200 goto done;
203 if (pnum_info) {
204 *pnum_info = num_info;
207 if (pinfo) {
208 *pinfo = talloc_move(mem_ctx, &info);
211 done:
212 TALLOC_FREE(tmp_ctx);
213 return status;
216 /* convert a single name to a sid in a domain */
217 static NTSTATUS msrpc_name_to_sid(struct winbindd_domain *domain,
218 TALLOC_CTX *mem_ctx,
219 const char *domain_name,
220 const char *name,
221 uint32_t flags,
222 struct dom_sid *sid,
223 enum lsa_SidType *type)
225 NTSTATUS result;
226 struct dom_sid *sids = NULL;
227 enum lsa_SidType *types = NULL;
228 char *full_name = NULL;
229 NTSTATUS name_map_status = NT_STATUS_UNSUCCESSFUL;
230 char *mapped_name = NULL;
232 if (name == NULL || *name=='\0') {
233 full_name = talloc_asprintf(mem_ctx, "%s", domain_name);
234 } else if (domain_name == NULL || *domain_name == '\0') {
235 full_name = talloc_asprintf(mem_ctx, "%s", name);
236 } else {
237 full_name = talloc_asprintf(mem_ctx, "%s\\%s", domain_name, name);
239 if (!full_name) {
240 DEBUG(0, ("talloc_asprintf failed!\n"));
241 return NT_STATUS_NO_MEMORY;
244 DEBUG(3,("rpc: name_to_sid name=%s\n", full_name));
246 name_map_status = normalize_name_unmap(mem_ctx, full_name,
247 &mapped_name);
249 /* Reset the full_name pointer if we mapped anytthing */
251 if (NT_STATUS_IS_OK(name_map_status) ||
252 NT_STATUS_EQUAL(name_map_status, NT_STATUS_FILE_RENAMED))
254 full_name = mapped_name;
257 DEBUG(3,("name_to_sid [rpc] %s for domain %s\n",
258 full_name?full_name:"", domain_name ));
260 result = winbindd_lookup_names(mem_ctx, domain, 1,
261 (const char **)&full_name, NULL,
262 &sids, &types);
263 if (!NT_STATUS_IS_OK(result))
264 return result;
266 /* Return rid and type if lookup successful */
268 sid_copy(sid, &sids[0]);
269 *type = types[0];
271 return NT_STATUS_OK;
275 convert a domain SID to a user or group name
277 static NTSTATUS msrpc_sid_to_name(struct winbindd_domain *domain,
278 TALLOC_CTX *mem_ctx,
279 const struct dom_sid *sid,
280 char **domain_name,
281 char **name,
282 enum lsa_SidType *type)
284 char **domains;
285 char **names;
286 enum lsa_SidType *types = NULL;
287 NTSTATUS result;
288 NTSTATUS name_map_status = NT_STATUS_UNSUCCESSFUL;
289 char *mapped_name = NULL;
291 DEBUG(3,("sid_to_name [rpc] %s for domain %s\n", sid_string_dbg(sid),
292 domain->name ));
294 result = winbindd_lookup_sids(mem_ctx,
295 domain,
297 sid,
298 &domains,
299 &names,
300 &types);
301 if (!NT_STATUS_IS_OK(result)) {
302 DEBUG(2,("msrpc_sid_to_name: failed to lookup sids: %s\n",
303 nt_errstr(result)));
304 return result;
308 *type = (enum lsa_SidType)types[0];
309 *domain_name = domains[0];
310 *name = names[0];
312 DEBUG(5,("Mapped sid to [%s]\\[%s]\n", domains[0], *name));
314 name_map_status = normalize_name_map(mem_ctx, domain, *name,
315 &mapped_name);
316 if (NT_STATUS_IS_OK(name_map_status) ||
317 NT_STATUS_EQUAL(name_map_status, NT_STATUS_FILE_RENAMED))
319 *name = mapped_name;
320 DEBUG(5,("returning mapped name -- %s\n", *name));
323 return NT_STATUS_OK;
326 static NTSTATUS msrpc_rids_to_names(struct winbindd_domain *domain,
327 TALLOC_CTX *mem_ctx,
328 const struct dom_sid *sid,
329 uint32 *rids,
330 size_t num_rids,
331 char **domain_name,
332 char ***names,
333 enum lsa_SidType **types)
335 char **domains;
336 NTSTATUS result;
337 struct dom_sid *sids;
338 size_t i;
339 char **ret_names;
341 DEBUG(3, ("rids_to_names [rpc] for domain %s\n", domain->name ));
343 if (num_rids) {
344 sids = TALLOC_ARRAY(mem_ctx, struct dom_sid, num_rids);
345 if (sids == NULL) {
346 return NT_STATUS_NO_MEMORY;
348 } else {
349 sids = NULL;
352 for (i=0; i<num_rids; i++) {
353 if (!sid_compose(&sids[i], sid, rids[i])) {
354 return NT_STATUS_INTERNAL_ERROR;
358 result = winbindd_lookup_sids(mem_ctx,
359 domain,
360 num_rids,
361 sids,
362 &domains,
363 names,
364 types);
366 if (!NT_STATUS_IS_OK(result) &&
367 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
368 return result;
371 ret_names = *names;
372 for (i=0; i<num_rids; i++) {
373 NTSTATUS name_map_status = NT_STATUS_UNSUCCESSFUL;
374 char *mapped_name = NULL;
376 if ((*types)[i] != SID_NAME_UNKNOWN) {
377 name_map_status = normalize_name_map(mem_ctx,
378 domain,
379 ret_names[i],
380 &mapped_name);
381 if (NT_STATUS_IS_OK(name_map_status) ||
382 NT_STATUS_EQUAL(name_map_status, NT_STATUS_FILE_RENAMED))
384 ret_names[i] = mapped_name;
387 *domain_name = domains[i];
391 return result;
394 /* Lookup user information from a rid or username. */
395 static NTSTATUS msrpc_query_user(struct winbindd_domain *domain,
396 TALLOC_CTX *mem_ctx,
397 const struct dom_sid *user_sid,
398 struct wbint_userinfo *user_info)
400 struct rpc_pipe_client *samr_pipe;
401 struct policy_handle dom_pol;
402 struct netr_SamInfo3 *user;
403 TALLOC_CTX *tmp_ctx;
404 NTSTATUS status;
406 DEBUG(3,("msrpc_query_user sid=%s\n", sid_string_dbg(user_sid)));
408 tmp_ctx = talloc_stackframe();
409 if (tmp_ctx == NULL) {
410 return NT_STATUS_NO_MEMORY;
413 if (user_info) {
414 user_info->homedir = NULL;
415 user_info->shell = NULL;
416 user_info->primary_gid = (gid_t)-1;
419 /* try netsamlogon cache first */
420 user = netsamlogon_cache_get(tmp_ctx, user_sid);
421 if (user != NULL) {
422 DEBUG(5,("msrpc_query_user: Cache lookup succeeded for %s\n",
423 sid_string_dbg(user_sid)));
425 sid_compose(&user_info->user_sid, &domain->sid, user->base.rid);
426 sid_compose(&user_info->group_sid, &domain->sid,
427 user->base.primary_gid);
429 user_info->acct_name = talloc_strdup(user_info,
430 user->base.account_name.string);
431 user_info->full_name = talloc_strdup(user_info,
432 user->base.full_name.string);
434 status = NT_STATUS_OK;
435 goto done;
438 if ( !winbindd_can_contact_domain( domain ) ) {
439 DEBUG(10,("query_user: No incoming trust for domain %s\n",
440 domain->name));
441 goto done;
444 /* no cache; hit the wire */
445 status = cm_connect_sam(domain, tmp_ctx, &samr_pipe, &dom_pol);
446 if (!NT_STATUS_IS_OK(status)) {
447 goto done;
450 status = rpc_query_user(tmp_ctx,
451 samr_pipe,
452 &dom_pol,
453 &domain->sid,
454 user_sid,
455 user_info);
457 done:
458 TALLOC_FREE(tmp_ctx);
459 return status;
462 /* Lookup groups a user is a member of. I wish Unix had a call like this! */
463 static NTSTATUS msrpc_lookup_usergroups(struct winbindd_domain *domain,
464 TALLOC_CTX *mem_ctx,
465 const struct dom_sid *user_sid,
466 uint32_t *pnum_groups,
467 struct dom_sid **puser_grpsids)
469 struct rpc_pipe_client *samr_pipe;
470 struct policy_handle dom_pol;
471 struct dom_sid *user_grpsids = NULL;
472 uint32_t num_groups = 0;
473 TALLOC_CTX *tmp_ctx;
474 NTSTATUS status;
476 DEBUG(3,("msrpc_lookup_usergroups sid=%s\n", sid_string_dbg(user_sid)));
478 *pnum_groups = 0;
480 tmp_ctx = talloc_stackframe();
481 if (tmp_ctx == NULL) {
482 return NT_STATUS_NO_MEMORY;
485 /* Check if we have a cached user_info_3 */
486 status = lookup_usergroups_cached(domain,
487 tmp_ctx,
488 user_sid,
489 &num_groups,
490 &user_grpsids);
491 if (NT_STATUS_IS_OK(status)) {
492 goto cached;
495 if ( !winbindd_can_contact_domain( domain ) ) {
496 DEBUG(10,("lookup_usergroups: No incoming trust for domain %s\n",
497 domain->name));
499 /* Tell the cache manager not to remember this one */
500 status = NT_STATUS_SYNCHRONIZATION_REQUIRED;
501 goto done;
504 /* no cache; hit the wire */
505 status = cm_connect_sam(domain, tmp_ctx, &samr_pipe, &dom_pol);
506 if (!NT_STATUS_IS_OK(status)) {
507 goto done;
510 status = rpc_lookup_usergroups(tmp_ctx,
511 samr_pipe,
512 &dom_pol,
513 &domain->sid,
514 user_sid,
515 &num_groups,
516 &user_grpsids);
517 if (!NT_STATUS_IS_OK(status)) {
518 goto done;
521 cached:
522 if (pnum_groups) {
523 *pnum_groups = num_groups;
526 if (puser_grpsids) {
527 *puser_grpsids = talloc_move(mem_ctx, &user_grpsids);
530 done:
531 TALLOC_FREE(tmp_ctx);
532 return status;
533 return NT_STATUS_OK;
536 #define MAX_SAM_ENTRIES_W2K 0x400 /* 1024 */
538 static NTSTATUS msrpc_lookup_useraliases(struct winbindd_domain *domain,
539 TALLOC_CTX *mem_ctx,
540 uint32 num_sids, const struct dom_sid *sids,
541 uint32 *pnum_aliases,
542 uint32 **palias_rids)
544 struct rpc_pipe_client *samr_pipe;
545 struct policy_handle dom_pol;
546 uint32_t num_aliases = 0;
547 uint32_t *alias_rids = NULL;
548 TALLOC_CTX *tmp_ctx;
549 NTSTATUS status;
551 DEBUG(3,("msrpc_lookup_useraliases\n"));
553 if (pnum_aliases) {
554 *pnum_aliases = 0;
557 tmp_ctx = talloc_stackframe();
558 if (tmp_ctx == NULL) {
559 return NT_STATUS_NO_MEMORY;
562 if (!winbindd_can_contact_domain(domain)) {
563 DEBUG(10,("msrpc_lookup_useraliases: No incoming trust for domain %s\n",
564 domain->name));
565 goto done;
568 status = cm_connect_sam(domain, tmp_ctx, &samr_pipe, &dom_pol);
569 if (!NT_STATUS_IS_OK(status)) {
570 goto done;
573 status = rpc_lookup_useraliases(tmp_ctx,
574 samr_pipe,
575 &dom_pol,
576 num_sids,
577 sids,
578 &num_aliases,
579 &alias_rids);
580 if (!NT_STATUS_IS_OK(status)) {
581 goto done;
584 if (pnum_aliases) {
585 *pnum_aliases = num_aliases;
588 if (palias_rids) {
589 *palias_rids = talloc_move(mem_ctx, &alias_rids);
592 done:
593 TALLOC_FREE(tmp_ctx);
594 return status;
598 /* Lookup group membership given a rid. */
599 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
600 TALLOC_CTX *mem_ctx,
601 const struct dom_sid *group_sid,
602 enum lsa_SidType type,
603 uint32 *num_names,
604 struct dom_sid **sid_mem, char ***names,
605 uint32 **name_types)
607 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
608 uint32 i, total_names = 0;
609 struct policy_handle dom_pol, group_pol;
610 uint32 des_access = SEC_FLAG_MAXIMUM_ALLOWED;
611 uint32 *rid_mem = NULL;
612 uint32 group_rid;
613 unsigned int j, r;
614 struct rpc_pipe_client *cli;
615 unsigned int orig_timeout;
616 struct samr_RidTypeArray *rids = NULL;
618 DEBUG(10,("rpc: lookup_groupmem %s sid=%s\n", domain->name,
619 sid_string_dbg(group_sid)));
621 if ( !winbindd_can_contact_domain( domain ) ) {
622 DEBUG(10,("lookup_groupmem: No incoming trust for domain %s\n",
623 domain->name));
624 return NT_STATUS_OK;
627 if (!sid_peek_check_rid(&domain->sid, group_sid, &group_rid))
628 return NT_STATUS_UNSUCCESSFUL;
630 *num_names = 0;
632 result = cm_connect_sam(domain, mem_ctx, &cli, &dom_pol);
633 if (!NT_STATUS_IS_OK(result))
634 return result;
636 result = rpccli_samr_OpenGroup(cli, mem_ctx,
637 &dom_pol,
638 des_access,
639 group_rid,
640 &group_pol);
642 if (!NT_STATUS_IS_OK(result))
643 return result;
645 /* Step #1: Get a list of user rids that are the members of the
646 group. */
648 /* This call can take a long time - allow the server to time out.
649 35 seconds should do it. */
651 orig_timeout = rpccli_set_timeout(cli, 35000);
653 result = rpccli_samr_QueryGroupMember(cli, mem_ctx,
654 &group_pol,
655 &rids);
657 /* And restore our original timeout. */
658 rpccli_set_timeout(cli, orig_timeout);
660 rpccli_samr_Close(cli, mem_ctx, &group_pol);
662 if (!NT_STATUS_IS_OK(result))
663 return result;
665 if (!rids || !rids->count) {
666 names = NULL;
667 name_types = NULL;
668 sid_mem = NULL;
669 return NT_STATUS_OK;
672 *num_names = rids->count;
673 rid_mem = rids->rids;
675 /* Step #2: Convert list of rids into list of usernames. Do this
676 in bunches of ~1000 to avoid crashing NT4. It looks like there
677 is a buffer overflow or something like that lurking around
678 somewhere. */
680 #define MAX_LOOKUP_RIDS 900
682 *names = TALLOC_ZERO_ARRAY(mem_ctx, char *, *num_names);
683 *name_types = TALLOC_ZERO_ARRAY(mem_ctx, uint32, *num_names);
684 *sid_mem = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, *num_names);
686 for (j=0;j<(*num_names);j++)
687 sid_compose(&(*sid_mem)[j], &domain->sid, rid_mem[j]);
689 if (*num_names>0 && (!*names || !*name_types))
690 return NT_STATUS_NO_MEMORY;
692 for (i = 0; i < *num_names; i += MAX_LOOKUP_RIDS) {
693 int num_lookup_rids = MIN(*num_names - i, MAX_LOOKUP_RIDS);
694 struct lsa_Strings tmp_names;
695 struct samr_Ids tmp_types;
697 /* Lookup a chunk of rids */
699 result = rpccli_samr_LookupRids(cli, mem_ctx,
700 &dom_pol,
701 num_lookup_rids,
702 &rid_mem[i],
703 &tmp_names,
704 &tmp_types);
706 /* see if we have a real error (and yes the
707 STATUS_SOME_UNMAPPED is the one returned from 2k) */
709 if (!NT_STATUS_IS_OK(result) &&
710 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
711 return result;
713 /* Copy result into array. The talloc system will take
714 care of freeing the temporary arrays later on. */
716 if (tmp_names.count != tmp_types.count) {
717 return NT_STATUS_UNSUCCESSFUL;
720 for (r=0; r<tmp_names.count; r++) {
721 if (tmp_types.ids[r] == SID_NAME_UNKNOWN) {
722 continue;
724 (*names)[total_names] = fill_domain_username_talloc(
725 mem_ctx, domain->name,
726 tmp_names.names[r].string, true);
727 (*name_types)[total_names] = tmp_types.ids[r];
728 total_names += 1;
732 *num_names = total_names;
734 return NT_STATUS_OK;
737 #ifdef HAVE_LDAP
739 #include <ldap.h>
741 static int get_ldap_seq(const char *server, int port, uint32 *seq)
743 int ret = -1;
744 struct timeval to;
745 const char *attrs[] = {"highestCommittedUSN", NULL};
746 LDAPMessage *res = NULL;
747 char **values = NULL;
748 LDAP *ldp = NULL;
750 *seq = DOM_SEQUENCE_NONE;
753 * Parameterised (5) second timeout on open. This is needed as the
754 * search timeout doesn't seem to apply to doing an open as well. JRA.
757 ldp = ldap_open_with_timeout(server, port, lp_ldap_timeout());
758 if (ldp == NULL)
759 return -1;
761 /* Timeout if no response within 20 seconds. */
762 to.tv_sec = 10;
763 to.tv_usec = 0;
765 if (ldap_search_st(ldp, "", LDAP_SCOPE_BASE, "(objectclass=*)",
766 CONST_DISCARD(char **, attrs), 0, &to, &res))
767 goto done;
769 if (ldap_count_entries(ldp, res) != 1)
770 goto done;
772 values = ldap_get_values(ldp, res, "highestCommittedUSN");
773 if (!values || !values[0])
774 goto done;
776 *seq = atoi(values[0]);
777 ret = 0;
779 done:
781 if (values)
782 ldap_value_free(values);
783 if (res)
784 ldap_msgfree(res);
785 if (ldp)
786 ldap_unbind(ldp);
787 return ret;
790 /**********************************************************************
791 Get the sequence number for a Windows AD native mode domain using
792 LDAP queries.
793 **********************************************************************/
795 static int get_ldap_sequence_number(struct winbindd_domain *domain, uint32 *seq)
797 int ret = -1;
798 char addr[INET6_ADDRSTRLEN];
800 print_sockaddr(addr, sizeof(addr), &domain->dcaddr);
801 if ((ret = get_ldap_seq(addr, LDAP_PORT, seq)) == 0) {
802 DEBUG(3, ("get_ldap_sequence_number: Retrieved sequence "
803 "number for Domain (%s) from DC (%s)\n",
804 domain->name, addr));
806 return ret;
809 #endif /* HAVE_LDAP */
811 /* find the sequence number for a domain */
812 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
814 TALLOC_CTX *mem_ctx;
815 union samr_DomainInfo *info = NULL;
816 NTSTATUS result;
817 struct policy_handle dom_pol;
818 bool got_seq_num = False;
819 struct rpc_pipe_client *cli;
821 DEBUG(10,("rpc: fetch sequence_number for %s\n", domain->name));
823 if ( !winbindd_can_contact_domain( domain ) ) {
824 DEBUG(10,("sequence_number: No incoming trust for domain %s\n",
825 domain->name));
826 *seq = time(NULL);
827 return NT_STATUS_OK;
830 *seq = DOM_SEQUENCE_NONE;
832 if (!(mem_ctx = talloc_init("sequence_number[rpc]")))
833 return NT_STATUS_NO_MEMORY;
835 #ifdef HAVE_LDAP
836 if ( domain->active_directory )
838 int res;
840 DEBUG(8,("using get_ldap_seq() to retrieve the "
841 "sequence number\n"));
843 res = get_ldap_sequence_number( domain, seq );
844 if (res == 0)
846 result = NT_STATUS_OK;
847 DEBUG(10,("domain_sequence_number: LDAP for "
848 "domain %s is %u\n",
849 domain->name, *seq));
850 goto done;
853 DEBUG(10,("domain_sequence_number: failed to get LDAP "
854 "sequence number for domain %s\n",
855 domain->name ));
857 #endif /* HAVE_LDAP */
859 result = cm_connect_sam(domain, mem_ctx, &cli, &dom_pol);
860 if (!NT_STATUS_IS_OK(result)) {
861 goto done;
864 /* Query domain info */
866 result = rpccli_samr_QueryDomainInfo(cli, mem_ctx,
867 &dom_pol,
869 &info);
871 if (NT_STATUS_IS_OK(result)) {
872 *seq = info->info8.sequence_num;
873 got_seq_num = True;
874 goto seq_num;
877 /* retry with info-level 2 in case the dc does not support info-level 8
878 * (like all older samba2 and samba3 dc's) - Guenther */
880 result = rpccli_samr_QueryDomainInfo(cli, mem_ctx,
881 &dom_pol,
883 &info);
885 if (NT_STATUS_IS_OK(result)) {
886 *seq = info->general.sequence_num;
887 got_seq_num = True;
890 seq_num:
891 if (got_seq_num) {
892 DEBUG(10,("domain_sequence_number: for domain %s is %u\n",
893 domain->name, (unsigned)*seq));
894 } else {
895 DEBUG(10,("domain_sequence_number: failed to get sequence "
896 "number (%u) for domain %s\n",
897 (unsigned)*seq, domain->name ));
900 done:
902 talloc_destroy(mem_ctx);
904 return result;
907 /* get a list of trusted domains */
908 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
909 TALLOC_CTX *mem_ctx,
910 struct netr_DomainTrustList *trusts)
912 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
913 uint32 enum_ctx = 0;
914 struct rpc_pipe_client *cli;
915 struct policy_handle lsa_policy;
917 DEBUG(3,("rpc: trusted_domains\n"));
919 ZERO_STRUCTP(trusts);
921 result = cm_connect_lsa(domain, mem_ctx, &cli, &lsa_policy);
922 if (!NT_STATUS_IS_OK(result))
923 return result;
925 result = STATUS_MORE_ENTRIES;
927 while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
928 uint32 start_idx;
929 int i;
930 struct lsa_DomainList dom_list;
932 result = rpccli_lsa_EnumTrustDom(cli, mem_ctx,
933 &lsa_policy,
934 &enum_ctx,
935 &dom_list,
936 (uint32_t)-1);
938 if (!NT_STATUS_IS_OK(result) &&
939 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
940 break;
942 start_idx = trusts->count;
943 trusts->count += dom_list.count;
945 trusts->array = talloc_realloc(
946 mem_ctx, trusts->array, struct netr_DomainTrust,
947 trusts->count);
948 if (trusts->array == NULL) {
949 return NT_STATUS_NO_MEMORY;
952 for (i=0; i<dom_list.count; i++) {
953 struct netr_DomainTrust *trust = &trusts->array[i];
954 struct dom_sid *sid;
956 ZERO_STRUCTP(trust);
958 trust->netbios_name = talloc_move(
959 trusts->array,
960 &dom_list.domains[i].name.string);
961 trust->dns_name = NULL;
963 sid = talloc(trusts->array, struct dom_sid);
964 if (sid == NULL) {
965 return NT_STATUS_NO_MEMORY;
967 sid_copy(sid, dom_list.domains[i].sid);
968 trust->sid = sid;
971 return result;
974 /* find the lockout policy for a domain */
975 static NTSTATUS msrpc_lockout_policy(struct winbindd_domain *domain,
976 TALLOC_CTX *mem_ctx,
977 struct samr_DomInfo12 *lockout_policy)
979 NTSTATUS result;
980 struct rpc_pipe_client *cli;
981 struct policy_handle dom_pol;
982 union samr_DomainInfo *info = NULL;
984 DEBUG(10,("rpc: fetch lockout policy for %s\n", domain->name));
986 if ( !winbindd_can_contact_domain( domain ) ) {
987 DEBUG(10,("msrpc_lockout_policy: No incoming trust for domain %s\n",
988 domain->name));
989 return NT_STATUS_NOT_SUPPORTED;
992 result = cm_connect_sam(domain, mem_ctx, &cli, &dom_pol);
993 if (!NT_STATUS_IS_OK(result)) {
994 goto done;
997 result = rpccli_samr_QueryDomainInfo(cli, mem_ctx,
998 &dom_pol,
1000 &info);
1001 if (!NT_STATUS_IS_OK(result)) {
1002 goto done;
1005 *lockout_policy = info->info12;
1007 DEBUG(10,("msrpc_lockout_policy: lockout_threshold %d\n",
1008 info->info12.lockout_threshold));
1010 done:
1012 return result;
1015 /* find the password policy for a domain */
1016 static NTSTATUS msrpc_password_policy(struct winbindd_domain *domain,
1017 TALLOC_CTX *mem_ctx,
1018 struct samr_DomInfo1 *password_policy)
1020 NTSTATUS result;
1021 struct rpc_pipe_client *cli;
1022 struct policy_handle dom_pol;
1023 union samr_DomainInfo *info = NULL;
1025 DEBUG(10,("rpc: fetch password policy for %s\n", domain->name));
1027 if ( !winbindd_can_contact_domain( domain ) ) {
1028 DEBUG(10,("msrpc_password_policy: No incoming trust for domain %s\n",
1029 domain->name));
1030 return NT_STATUS_NOT_SUPPORTED;
1033 result = cm_connect_sam(domain, mem_ctx, &cli, &dom_pol);
1034 if (!NT_STATUS_IS_OK(result)) {
1035 goto done;
1038 result = rpccli_samr_QueryDomainInfo(cli, mem_ctx,
1039 &dom_pol,
1041 &info);
1042 if (!NT_STATUS_IS_OK(result)) {
1043 goto done;
1046 *password_policy = info->info1;
1048 DEBUG(10,("msrpc_password_policy: min_length_password %d\n",
1049 info->info1.min_password_length));
1051 done:
1053 return result;
1056 typedef NTSTATUS (*lookup_sids_fn_t)(struct rpc_pipe_client *cli,
1057 TALLOC_CTX *mem_ctx,
1058 struct policy_handle *pol,
1059 int num_sids,
1060 const struct dom_sid *sids,
1061 char ***pdomains,
1062 char ***pnames,
1063 enum lsa_SidType **ptypes);
1065 NTSTATUS winbindd_lookup_sids(TALLOC_CTX *mem_ctx,
1066 struct winbindd_domain *domain,
1067 uint32_t num_sids,
1068 const struct dom_sid *sids,
1069 char ***domains,
1070 char ***names,
1071 enum lsa_SidType **types)
1073 NTSTATUS status;
1074 struct rpc_pipe_client *cli = NULL;
1075 struct policy_handle lsa_policy;
1076 unsigned int orig_timeout;
1077 lookup_sids_fn_t lookup_sids_fn = rpccli_lsa_lookup_sids;
1079 if (domain->can_do_ncacn_ip_tcp) {
1080 status = cm_connect_lsa_tcp(domain, mem_ctx, &cli);
1081 if (NT_STATUS_IS_OK(status)) {
1082 lookup_sids_fn = rpccli_lsa_lookup_sids3;
1083 goto lookup;
1085 domain->can_do_ncacn_ip_tcp = false;
1087 status = cm_connect_lsa(domain, mem_ctx, &cli, &lsa_policy);
1089 if (!NT_STATUS_IS_OK(status)) {
1090 return status;
1093 lookup:
1095 * This call can take a long time
1096 * allow the server to time out.
1097 * 35 seconds should do it.
1099 orig_timeout = rpccli_set_timeout(cli, 35000);
1101 status = lookup_sids_fn(cli,
1102 mem_ctx,
1103 &lsa_policy,
1104 num_sids,
1105 sids,
1106 domains,
1107 names,
1108 types);
1110 /* And restore our original timeout. */
1111 rpccli_set_timeout(cli, orig_timeout);
1113 if (!NT_STATUS_IS_OK(status)) {
1114 return status;
1117 return status;
1120 typedef NTSTATUS (*lookup_names_fn_t)(struct rpc_pipe_client *cli,
1121 TALLOC_CTX *mem_ctx,
1122 struct policy_handle *pol,
1123 int num_names,
1124 const char **names,
1125 const char ***dom_names,
1126 int level,
1127 struct dom_sid **sids,
1128 enum lsa_SidType **types);
1130 NTSTATUS winbindd_lookup_names(TALLOC_CTX *mem_ctx,
1131 struct winbindd_domain *domain,
1132 uint32_t num_names,
1133 const char **names,
1134 const char ***domains,
1135 struct dom_sid **sids,
1136 enum lsa_SidType **types)
1138 NTSTATUS status;
1139 struct rpc_pipe_client *cli = NULL;
1140 struct policy_handle lsa_policy;
1141 unsigned int orig_timeout = 0;
1142 lookup_names_fn_t lookup_names_fn = rpccli_lsa_lookup_names;
1144 if (domain->can_do_ncacn_ip_tcp) {
1145 status = cm_connect_lsa_tcp(domain, mem_ctx, &cli);
1146 if (NT_STATUS_IS_OK(status)) {
1147 lookup_names_fn = rpccli_lsa_lookup_names4;
1148 goto lookup;
1150 domain->can_do_ncacn_ip_tcp = false;
1152 status = cm_connect_lsa(domain, mem_ctx, &cli, &lsa_policy);
1154 if (!NT_STATUS_IS_OK(status)) {
1155 return status;
1158 lookup:
1161 * This call can take a long time
1162 * allow the server to time out.
1163 * 35 seconds should do it.
1165 orig_timeout = rpccli_set_timeout(cli, 35000);
1167 status = lookup_names_fn(cli,
1168 mem_ctx,
1169 &lsa_policy,
1170 num_names,
1171 (const char **) names,
1172 domains,
1174 sids,
1175 types);
1177 /* And restore our original timeout. */
1178 rpccli_set_timeout(cli, orig_timeout);
1180 if (!NT_STATUS_IS_OK(status)) {
1181 return status;
1184 return status;
1187 /* the rpc backend methods are exposed via this structure */
1188 struct winbindd_methods msrpc_methods = {
1189 False,
1190 msrpc_query_user_list,
1191 msrpc_enum_dom_groups,
1192 msrpc_enum_local_groups,
1193 msrpc_name_to_sid,
1194 msrpc_sid_to_name,
1195 msrpc_rids_to_names,
1196 msrpc_query_user,
1197 msrpc_lookup_usergroups,
1198 msrpc_lookup_useraliases,
1199 lookup_groupmem,
1200 sequence_number,
1201 msrpc_lockout_policy,
1202 msrpc_password_policy,
1203 trusted_domains,