make scannedonly notify the scanner if no .scanned: file was found during rename
[Samba/gebeck_regimport.git] / source3 / winbindd / winbindd_rpc.c
blob80f7bb69bf05ccd9d7dc996c672dd5bc700e9338
1 /*
2 * Unix SMB/CIFS implementation.
4 * Winbind rpc backend functions
6 * Copyright (c) 2000-2003 Tim Potter
7 * Copyright (c) 2001 Andrew Tridgell
8 * Copyright (c) 2005 Volker Lendecke
9 * Copyright (c) 2008 Guenther Deschner (pidl conversion)
10 * Copyright (c) 2010 Andreas Schneider <asn@samba.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "includes.h"
27 #include "winbindd.h"
28 #include "winbindd_rpc.h"
30 #include "librpc/gen_ndr/cli_samr.h"
31 #include "librpc/gen_ndr/srv_samr.h"
32 #include "librpc/gen_ndr/cli_lsa.h"
33 #include "librpc/gen_ndr/srv_lsa.h"
34 #include "rpc_client/cli_samr.h"
35 #include "rpc_client/cli_lsarpc.h"
37 /* Query display info for a domain */
38 NTSTATUS rpc_query_user_list(TALLOC_CTX *mem_ctx,
39 struct rpc_pipe_client *samr_pipe,
40 struct policy_handle *samr_policy,
41 const struct dom_sid *domain_sid,
42 uint32_t *pnum_info,
43 struct wbint_userinfo **pinfo)
45 struct wbint_userinfo *info = NULL;
46 uint32_t num_info = 0;
47 uint32_t loop_count = 0;
48 uint32_t start_idx = 0;
49 uint32_t i = 0;
50 NTSTATUS status;
52 *pnum_info = 0;
54 do {
55 uint32_t j;
56 uint32_t num_dom_users;
57 uint32_t max_entries, max_size;
58 uint32_t total_size, returned_size;
59 union samr_DispInfo disp_info;
61 get_query_dispinfo_params(loop_count,
62 &max_entries,
63 &max_size);
65 status = rpccli_samr_QueryDisplayInfo(samr_pipe,
66 mem_ctx,
67 samr_policy,
68 1, /* level */
69 start_idx,
70 max_entries,
71 max_size,
72 &total_size,
73 &returned_size,
74 &disp_info);
75 if (!NT_STATUS_IS_OK(status)) {
76 if (!NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
77 return status;
81 /* increment required start query values */
82 start_idx += disp_info.info1.count;
83 loop_count++;
84 num_dom_users = disp_info.info1.count;
86 num_info += num_dom_users;
88 info = TALLOC_REALLOC_ARRAY(mem_ctx,
89 info,
90 struct wbint_userinfo,
91 num_info);
92 if (info == NULL) {
93 return NT_STATUS_NO_MEMORY;
96 for (j = 0; j < num_dom_users; i++, j++) {
97 uint32_t rid = disp_info.info1.entries[j].rid;
98 struct samr_DispEntryGeneral *src;
99 struct wbint_userinfo *dst;
101 src = &(disp_info.info1.entries[j]);
102 dst = &(info[i]);
104 dst->acct_name = talloc_strdup(info,
105 src->account_name.string);
106 if (dst->acct_name == NULL) {
107 return NT_STATUS_NO_MEMORY;
110 dst->full_name = talloc_strdup(info, src->full_name.string);
111 if (dst->full_name == NULL) {
112 return NT_STATUS_NO_MEMORY;
115 dst->homedir = NULL;
116 dst->shell = NULL;
118 sid_compose(&dst->user_sid, domain_sid, rid);
120 /* For the moment we set the primary group for
121 every user to be the Domain Users group.
122 There are serious problems with determining
123 the actual primary group for large domains.
124 This should really be made into a 'winbind
125 force group' smb.conf parameter or
126 something like that. */
127 sid_compose(&dst->group_sid, domain_sid,
128 DOMAIN_RID_USERS);
130 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
132 *pnum_info = num_info;
133 *pinfo = info;
135 return NT_STATUS_OK;
138 /* List all domain groups */
139 NTSTATUS rpc_enum_dom_groups(TALLOC_CTX *mem_ctx,
140 struct rpc_pipe_client *samr_pipe,
141 struct policy_handle *samr_policy,
142 uint32_t *pnum_info,
143 struct acct_info **pinfo)
145 struct acct_info *info = NULL;
146 uint32_t start = 0;
147 uint32_t num_info = 0;
148 NTSTATUS status;
150 *pnum_info = 0;
152 do {
153 struct samr_SamArray *sam_array = NULL;
154 uint32_t count = 0;
155 uint32_t g;
157 /* start is updated by this call. */
158 status = rpccli_samr_EnumDomainGroups(samr_pipe,
159 mem_ctx,
160 samr_policy,
161 &start,
162 &sam_array,
163 0xFFFF, /* buffer size? */
164 &count);
165 if (!NT_STATUS_IS_OK(status)) {
166 if (!NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
167 DEBUG(2,("query_user_list: failed to enum domain groups: %s\n",
168 nt_errstr(status)));
169 return status;
173 info = TALLOC_REALLOC_ARRAY(mem_ctx,
174 info,
175 struct acct_info,
176 num_info + count);
177 if (info == NULL) {
178 return NT_STATUS_NO_MEMORY;
181 for (g = 0; g < count; g++) {
182 fstrcpy(info[num_info + g].acct_name,
183 sam_array->entries[g].name.string);
185 info[num_info + g].rid = sam_array->entries[g].idx;
188 num_info += count;
189 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
191 *pnum_info = num_info;
192 *pinfo = info;
194 return NT_STATUS_OK;
197 NTSTATUS rpc_enum_local_groups(TALLOC_CTX *mem_ctx,
198 struct rpc_pipe_client *samr_pipe,
199 struct policy_handle *samr_policy,
200 uint32_t *pnum_info,
201 struct acct_info **pinfo)
203 struct acct_info *info = NULL;
204 uint32_t num_info = 0;
205 NTSTATUS status;
207 *pnum_info = 0;
209 do {
210 struct samr_SamArray *sam_array = NULL;
211 uint32_t count = 0;
212 uint32_t start = num_info;
213 uint32_t g;
215 status = rpccli_samr_EnumDomainAliases(samr_pipe,
216 mem_ctx,
217 samr_policy,
218 &start,
219 &sam_array,
220 0xFFFF, /* buffer size? */
221 &count);
222 if (!NT_STATUS_IS_OK(status)) {
223 if (!NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
224 return status;
228 info = TALLOC_REALLOC_ARRAY(mem_ctx,
229 info,
230 struct acct_info,
231 num_info + count);
232 if (info == NULL) {
233 return NT_STATUS_NO_MEMORY;
236 for (g = 0; g < count; g++) {
237 fstrcpy(info[num_info + g].acct_name,
238 sam_array->entries[g].name.string);
239 info[num_info + g].rid = sam_array->entries[g].idx;
242 num_info += count;
243 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
245 *pnum_info = num_info;
246 *pinfo = info;
248 return NT_STATUS_OK;
251 /* convert a single name to a sid in a domain */
252 NTSTATUS rpc_name_to_sid(TALLOC_CTX *mem_ctx,
253 struct rpc_pipe_client *lsa_pipe,
254 struct policy_handle *lsa_policy,
255 const char *domain_name,
256 const char *name,
257 uint32_t flags,
258 struct dom_sid *sid,
259 enum lsa_SidType *type)
261 enum lsa_SidType *types = NULL;
262 struct dom_sid *sids = NULL;
263 char *full_name = NULL;
264 char *mapped_name = NULL;
265 NTSTATUS status;
267 if (name == NULL || name[0] == '\0') {
268 full_name = talloc_asprintf(mem_ctx, "%s", domain_name);
269 } else if (domain_name == NULL || domain_name[0] == '\0') {
270 full_name = talloc_asprintf(mem_ctx, "%s", name);
271 } else {
272 full_name = talloc_asprintf(mem_ctx, "%s\\%s", domain_name, name);
275 if (full_name == NULL) {
276 return NT_STATUS_NO_MEMORY;
279 status = normalize_name_unmap(mem_ctx, full_name, &mapped_name);
280 /* Reset the full_name pointer if we mapped anything */
281 if (NT_STATUS_IS_OK(status) ||
282 NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
283 full_name = mapped_name;
286 DEBUG(3,("name_to_sid: %s for domain %s\n",
287 full_name ? full_name : "", domain_name ));
290 * We don't run into deadlocks here, cause winbind_off() is
291 * called in the main function.
293 status = rpccli_lsa_lookup_names(lsa_pipe,
294 mem_ctx,
295 lsa_policy,
296 1, /* num_names */
297 (const char **) &full_name,
298 NULL, /* domains */
299 1, /* level */
300 &sids,
301 &types);
302 if (!NT_STATUS_IS_OK(status)) {
303 DEBUG(2,("name_to_sid: failed to lookup name: %s\n",
304 nt_errstr(status)));
305 return status;
308 sid_copy(sid, &sids[0]);
309 *type = types[0];
311 return NT_STATUS_OK;
314 /* Convert a domain SID to a user or group name */
315 NTSTATUS rpc_sid_to_name(TALLOC_CTX *mem_ctx,
316 struct rpc_pipe_client *lsa_pipe,
317 struct policy_handle *lsa_policy,
318 struct winbindd_domain *domain,
319 const struct dom_sid *sid,
320 char **pdomain_name,
321 char **pname,
322 enum lsa_SidType *ptype)
324 char *mapped_name = NULL;
325 char **domains = NULL;
326 char **names = NULL;
327 enum lsa_SidType *types = NULL;
328 NTSTATUS map_status;
329 NTSTATUS status;
331 status = rpccli_lsa_lookup_sids(lsa_pipe,
332 mem_ctx,
333 lsa_policy,
334 1, /* num_sids */
335 sid,
336 &domains,
337 &names,
338 &types);
339 if (!NT_STATUS_IS_OK(status)) {
340 DEBUG(2,("sid_to_name: failed to lookup sids: %s\n",
341 nt_errstr(status)));
342 return status;
345 *ptype = (enum lsa_SidType) types[0];
347 map_status = normalize_name_map(mem_ctx,
348 domain,
349 *pname,
350 &mapped_name);
351 if (NT_STATUS_IS_OK(map_status) ||
352 NT_STATUS_EQUAL(map_status, NT_STATUS_FILE_RENAMED)) {
353 *pname = talloc_strdup(mem_ctx, mapped_name);
354 DEBUG(5,("returning mapped name -- %s\n", *pname));
355 } else {
356 *pname = talloc_strdup(mem_ctx, names[0]);
358 if (*pname == NULL) {
359 return NT_STATUS_NO_MEMORY;
362 *pdomain_name = talloc_strdup(mem_ctx, domains[0]);
363 if (*pdomain_name == NULL) {
364 return NT_STATUS_NO_MEMORY;
367 return NT_STATUS_OK;
370 /* Convert a bunch of rids to user or group names */
371 NTSTATUS rpc_rids_to_names(TALLOC_CTX *mem_ctx,
372 struct rpc_pipe_client *lsa_pipe,
373 struct policy_handle *lsa_policy,
374 struct winbindd_domain *domain,
375 const struct dom_sid *sid,
376 uint32_t *rids,
377 size_t num_rids,
378 char **pdomain_name,
379 char ***pnames,
380 enum lsa_SidType **ptypes)
382 enum lsa_SidType *types = NULL;
383 char *domain_name = NULL;
384 char **domains = NULL;
385 char **names = NULL;
386 struct dom_sid *sids;
387 size_t i;
388 NTSTATUS status;
390 if (num_rids > 0) {
391 sids = TALLOC_ARRAY(mem_ctx, struct dom_sid, num_rids);
392 if (sids == NULL) {
393 return NT_STATUS_NO_MEMORY;
395 } else {
396 sids = NULL;
399 for (i = 0; i < num_rids; i++) {
400 if (!sid_compose(&sids[i], sid, rids[i])) {
401 return NT_STATUS_INTERNAL_ERROR;
405 status = rpccli_lsa_lookup_sids(lsa_pipe,
406 mem_ctx,
407 lsa_policy,
408 num_rids,
409 sids,
410 &domains,
411 &names,
412 &types);
413 if (!NT_STATUS_IS_OK(status) &&
414 !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
415 DEBUG(2,("rids_to_names: failed to lookup sids: %s\n",
416 nt_errstr(status)));
417 return status;
420 for (i = 0; i < num_rids; i++) {
421 char *mapped_name = NULL;
422 NTSTATUS map_status;
424 if (types[i] != SID_NAME_UNKNOWN) {
425 map_status = normalize_name_map(mem_ctx,
426 domain,
427 names[i],
428 &mapped_name);
429 if (NT_STATUS_IS_OK(map_status) ||
430 NT_STATUS_EQUAL(map_status, NT_STATUS_FILE_RENAMED)) {
431 TALLOC_FREE(names[i]);
432 names[i] = talloc_strdup(names, mapped_name);
433 if (names[i] == NULL) {
434 return NT_STATUS_NO_MEMORY;
438 domain_name = domains[i];
442 *pdomain_name = domain_name;
443 *ptypes = types;
444 *pnames = names;
446 return NT_STATUS_OK;
449 /* Lookup user information from a rid or username. */
450 NTSTATUS rpc_query_user(TALLOC_CTX *mem_ctx,
451 struct rpc_pipe_client *samr_pipe,
452 struct policy_handle *samr_policy,
453 const struct dom_sid *domain_sid,
454 const struct dom_sid *user_sid,
455 struct wbint_userinfo *user_info)
457 struct policy_handle user_policy;
458 union samr_UserInfo *info = NULL;
459 uint32_t user_rid;
460 NTSTATUS status;
462 if (!sid_peek_check_rid(domain_sid, user_sid, &user_rid)) {
463 return NT_STATUS_UNSUCCESSFUL;
466 /* Get user handle */
467 status = rpccli_samr_OpenUser(samr_pipe,
468 mem_ctx,
469 samr_policy,
470 SEC_FLAG_MAXIMUM_ALLOWED,
471 user_rid,
472 &user_policy);
473 if (!NT_STATUS_IS_OK(status)) {
474 return status;
477 /* Get user info */
478 status = rpccli_samr_QueryUserInfo(samr_pipe,
479 mem_ctx,
480 &user_policy,
481 0x15,
482 &info);
484 rpccli_samr_Close(samr_pipe, mem_ctx, &user_policy);
486 if (!NT_STATUS_IS_OK(status)) {
487 return status;
490 sid_compose(&user_info->user_sid, domain_sid, user_rid);
491 sid_compose(&user_info->group_sid, domain_sid,
492 info->info21.primary_gid);
494 user_info->acct_name = talloc_strdup(user_info,
495 info->info21.account_name.string);
496 if (user_info->acct_name == NULL) {
497 return NT_STATUS_NO_MEMORY;
500 user_info->full_name = talloc_strdup(user_info,
501 info->info21.full_name.string);
502 if (user_info->acct_name == NULL) {
503 return NT_STATUS_NO_MEMORY;
506 user_info->homedir = NULL;
507 user_info->shell = NULL;
508 user_info->primary_gid = (gid_t)-1;
510 return NT_STATUS_OK;
513 /* Lookup groups a user is a member of. */
514 NTSTATUS rpc_lookup_usergroups(TALLOC_CTX *mem_ctx,
515 struct rpc_pipe_client *samr_pipe,
516 struct policy_handle *samr_policy,
517 const struct dom_sid *domain_sid,
518 const struct dom_sid *user_sid,
519 uint32_t *pnum_groups,
520 struct dom_sid **puser_grpsids)
522 struct policy_handle user_policy;
523 struct samr_RidWithAttributeArray *rid_array = NULL;
524 struct dom_sid *user_grpsids = NULL;
525 uint32_t num_groups = 0, i;
526 uint32_t user_rid;
527 NTSTATUS status;
529 if (!sid_peek_check_rid(domain_sid, user_sid, &user_rid)) {
530 return NT_STATUS_UNSUCCESSFUL;
533 /* Get user handle */
534 status = rpccli_samr_OpenUser(samr_pipe,
535 mem_ctx,
536 samr_policy,
537 SEC_FLAG_MAXIMUM_ALLOWED,
538 user_rid,
539 &user_policy);
540 if (!NT_STATUS_IS_OK(status)) {
541 return status;
544 /* Query user rids */
545 status = rpccli_samr_GetGroupsForUser(samr_pipe,
546 mem_ctx,
547 &user_policy,
548 &rid_array);
549 num_groups = rid_array->count;
551 rpccli_samr_Close(samr_pipe, mem_ctx, &user_policy);
553 if (!NT_STATUS_IS_OK(status) || num_groups == 0) {
554 return status;
557 user_grpsids = TALLOC_ARRAY(mem_ctx, struct dom_sid, num_groups);
558 if (user_grpsids == NULL) {
559 status = NT_STATUS_NO_MEMORY;
560 return status;
563 for (i = 0; i < num_groups; i++) {
564 sid_compose(&(user_grpsids[i]), domain_sid,
565 rid_array->rids[i].rid);
568 *pnum_groups = num_groups;
570 *puser_grpsids = user_grpsids;
572 return NT_STATUS_OK;
575 NTSTATUS rpc_lookup_useraliases(TALLOC_CTX *mem_ctx,
576 struct rpc_pipe_client *samr_pipe,
577 struct policy_handle *samr_policy,
578 uint32_t num_sids,
579 const struct dom_sid *sids,
580 uint32_t *pnum_aliases,
581 uint32_t **palias_rids)
583 #define MAX_SAM_ENTRIES_W2K 0x400 /* 1024 */
584 uint32_t num_query_sids = 0;
585 uint32_t num_queries = 1;
586 uint32_t num_aliases = 0;
587 uint32_t total_sids = 0;
588 uint32_t *alias_rids = NULL;
589 uint32_t rangesize = MAX_SAM_ENTRIES_W2K;
590 uint32_t i;
591 struct samr_Ids alias_rids_query;
592 NTSTATUS status;
594 do {
595 /* prepare query */
596 struct lsa_SidArray sid_array;
598 ZERO_STRUCT(sid_array);
600 num_query_sids = MIN(num_sids - total_sids, rangesize);
602 DEBUG(10,("rpc: lookup_useraliases: entering query %d for %d sids\n",
603 num_queries, num_query_sids));
605 if (num_query_sids) {
606 sid_array.sids = TALLOC_ZERO_ARRAY(mem_ctx, struct lsa_SidPtr, num_query_sids);
607 if (sid_array.sids == NULL) {
608 return NT_STATUS_NO_MEMORY;
610 } else {
611 sid_array.sids = NULL;
614 for (i = 0; i < num_query_sids; i++) {
615 sid_array.sids[i].sid = sid_dup_talloc(mem_ctx, &sids[total_sids++]);
616 if (sid_array.sids[i].sid == NULL) {
617 return NT_STATUS_NO_MEMORY;
620 sid_array.num_sids = num_query_sids;
622 /* do request */
623 status = rpccli_samr_GetAliasMembership(samr_pipe,
624 mem_ctx,
625 samr_policy,
626 &sid_array,
627 &alias_rids_query);
628 if (!NT_STATUS_IS_OK(status)) {
629 return status;
632 /* process output */
633 for (i = 0; i < alias_rids_query.count; i++) {
634 size_t na = num_aliases;
636 if (!add_rid_to_array_unique(mem_ctx,
637 alias_rids_query.ids[i],
638 &alias_rids,
639 &na)) {
640 return NT_STATUS_NO_MEMORY;
642 num_aliases = na;
645 num_queries++;
647 } while (total_sids < num_sids);
649 DEBUG(10,("rpc: rpc_lookup_useraliases: got %d aliases in %d queries "
650 "(rangesize: %d)\n", num_aliases, num_queries, rangesize));
652 *pnum_aliases = num_aliases;
653 *palias_rids = alias_rids;
655 return NT_STATUS_OK;
656 #undef MAX_SAM_ENTRIES_W2K
659 /* Lookup group membership given a rid. */
660 NTSTATUS rpc_lookup_groupmem(TALLOC_CTX *mem_ctx,
661 struct rpc_pipe_client *samr_pipe,
662 struct policy_handle *samr_policy,
663 const char *domain_name,
664 const struct dom_sid *domain_sid,
665 const struct dom_sid *group_sid,
666 enum lsa_SidType type,
667 uint32_t *pnum_names,
668 struct dom_sid **psid_mem,
669 char ***pnames,
670 uint32_t **pname_types)
672 struct policy_handle group_policy;
673 uint32_t group_rid;
674 uint32_t *rid_mem = NULL;
676 uint32_t num_names = 0;
677 uint32_t total_names = 0;
678 struct dom_sid *sid_mem = NULL;
679 char **names = NULL;
680 uint32_t *name_types = NULL;
682 struct lsa_Strings tmp_names;
683 struct samr_Ids tmp_types;
685 uint32_t j, r;
686 NTSTATUS status;
688 if (!sid_peek_check_rid(domain_sid, group_sid, &group_rid)) {
689 return NT_STATUS_UNSUCCESSFUL;
692 switch(type) {
693 case SID_NAME_DOM_GRP:
695 struct samr_RidTypeArray *rids = NULL;
697 status = rpccli_samr_OpenGroup(samr_pipe,
698 mem_ctx,
699 samr_policy,
700 SEC_FLAG_MAXIMUM_ALLOWED,
701 group_rid,
702 &group_policy);
703 if (!NT_STATUS_IS_OK(status)) {
704 return status;
708 * Step #1: Get a list of user rids that are the members of the group.
710 status = rpccli_samr_QueryGroupMember(samr_pipe,
711 mem_ctx,
712 &group_policy,
713 &rids);
715 rpccli_samr_Close(samr_pipe, mem_ctx, &group_policy);
717 if (!NT_STATUS_IS_OK(status)) {
718 return status;
721 if (rids == NULL || rids->count == 0) {
722 pnum_names = 0;
723 pnames = NULL;
724 pname_types = NULL;
725 psid_mem = NULL;
727 return NT_STATUS_OK;
730 num_names = rids->count;
731 rid_mem = rids->rids;
733 break;
735 case SID_NAME_WKN_GRP:
736 case SID_NAME_ALIAS:
738 struct lsa_SidArray sid_array;
739 struct lsa_SidPtr sid_ptr;
740 struct samr_Ids rids_query;
742 sid_ptr.sid = sid_dup_talloc(mem_ctx, group_sid);
743 if (sid_ptr.sid == NULL) {
744 return NT_STATUS_NO_MEMORY;
747 sid_array.num_sids = 1;
748 sid_array.sids = &sid_ptr;
750 status = rpccli_samr_GetAliasMembership(samr_pipe,
751 mem_ctx,
752 samr_policy,
753 &sid_array,
754 &rids_query);
756 if (rids_query.count == 0) {
757 pnum_names = 0;
758 pnames = NULL;
759 pname_types = NULL;
760 psid_mem = NULL;
762 return NT_STATUS_OK;
765 num_names = rids_query.count;
766 rid_mem = rids_query.ids;
768 break;
770 default:
771 return NT_STATUS_UNSUCCESSFUL;
775 * Step #2: Convert list of rids into list of usernames.
777 if (num_names > 0) {
778 names = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_names);
779 name_types = TALLOC_ZERO_ARRAY(mem_ctx, uint32_t, num_names);
780 sid_mem = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, num_names);
781 if (names == NULL || name_types == NULL || sid_mem == NULL) {
782 return NT_STATUS_NO_MEMORY;
786 for (j = 0; j < num_names; j++) {
787 sid_compose(&sid_mem[j], domain_sid, rid_mem[j]);
790 status = rpccli_samr_LookupRids(samr_pipe,
791 mem_ctx,
792 samr_policy,
793 num_names,
794 rid_mem,
795 &tmp_names,
796 &tmp_types);
797 if (!NT_STATUS_IS_OK(status)) {
798 if (!NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
799 return status;
803 /* Copy result into array. The talloc system will take
804 care of freeing the temporary arrays later on. */
805 if (tmp_names.count != tmp_types.count) {
806 return NT_STATUS_UNSUCCESSFUL;
809 for (r = 0; r < tmp_names.count; r++) {
810 if (tmp_types.ids[r] == SID_NAME_UNKNOWN) {
811 continue;
813 names[total_names] = fill_domain_username_talloc(names,
814 domain_name,
815 tmp_names.names[r].string,
816 true);
817 if (names[total_names] == NULL) {
818 return NT_STATUS_NO_MEMORY;
820 name_types[total_names] = tmp_types.ids[r];
821 total_names++;
824 *pnum_names = total_names;
825 *pnames = names;
826 *pname_types = name_types;
827 *psid_mem = sid_mem;
829 return NT_STATUS_OK;
832 /* Find the sequence number for a domain */
833 NTSTATUS rpc_sequence_number(TALLOC_CTX *mem_ctx,
834 struct rpc_pipe_client *samr_pipe,
835 struct policy_handle *samr_policy,
836 const char *domain_name,
837 uint32_t *pseq)
839 union samr_DomainInfo *info = NULL;
840 bool got_seq_num = false;
841 NTSTATUS status;
843 /* query domain info */
844 status = rpccli_samr_QueryDomainInfo(samr_pipe,
845 mem_ctx,
846 samr_policy,
848 &info);
849 if (NT_STATUS_IS_OK(status)) {
850 *pseq = info->info8.sequence_num;
851 got_seq_num = true;
852 goto seq_num;
855 /* retry with info-level 2 in case the dc does not support info-level 8
856 * (like all older samba2 and samba3 dc's) - Guenther */
857 status = rpccli_samr_QueryDomainInfo(samr_pipe,
858 mem_ctx,
859 samr_policy,
861 &info);
862 if (NT_STATUS_IS_OK(status)) {
863 *pseq = info->general.sequence_num;
864 got_seq_num = true;
867 seq_num:
868 if (got_seq_num) {
869 DEBUG(10,("domain_sequence_number: for domain %s is %u\n",
870 domain_name, (unsigned) *pseq));
871 } else {
872 DEBUG(10,("domain_sequence_number: failed to get sequence "
873 "number (%u) for domain %s\n",
874 (unsigned) *pseq, domain_name ));
875 status = NT_STATUS_OK;
878 return status;
881 /* Get a list of trusted domains */
882 NTSTATUS rpc_trusted_domains(TALLOC_CTX *mem_ctx,
883 struct rpc_pipe_client *lsa_pipe,
884 struct policy_handle *lsa_policy,
885 uint32_t *pnum_trusts,
886 struct netr_DomainTrust **ptrusts)
888 struct netr_DomainTrust *array = NULL;
889 uint32_t enum_ctx = 0;
890 uint32_t count = 0;
891 NTSTATUS status;
893 do {
894 struct lsa_DomainList dom_list;
895 uint32_t start_idx;
896 uint32_t i;
899 * We don't run into deadlocks here, cause winbind_off() is
900 * called in the main function.
902 status = rpccli_lsa_EnumTrustDom(lsa_pipe,
903 mem_ctx,
904 lsa_policy,
905 &enum_ctx,
906 &dom_list,
907 (uint32_t) -1);
908 if (!NT_STATUS_IS_OK(status)) {
909 if (!NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
910 return status;
914 start_idx = count;
915 count += dom_list.count;
917 array = talloc_realloc(mem_ctx,
918 array,
919 struct netr_DomainTrust,
920 count);
921 if (array == NULL) {
922 return NT_STATUS_NO_MEMORY;
925 for (i = 0; i < dom_list.count; i++) {
926 struct netr_DomainTrust *trust = &array[i];
927 struct dom_sid *sid;
929 ZERO_STRUCTP(trust);
931 trust->netbios_name = talloc_move(array,
932 &dom_list.domains[i].name.string);
933 trust->dns_name = NULL;
935 sid = talloc(array, struct dom_sid);
936 if (sid == NULL) {
937 return NT_STATUS_NO_MEMORY;
939 sid_copy(sid, dom_list.domains[i].sid);
940 trust->sid = sid;
942 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
944 *pnum_trusts = count;
945 *ptrusts = array;
947 return NT_STATUS_OK;