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/>.
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"
36 #include "../libcli/security/dom_sid.h"
38 /* Query display info for a domain */
39 NTSTATUS rpc_query_user_list(TALLOC_CTX *mem_ctx,
40 struct rpc_pipe_client *samr_pipe,
41 struct policy_handle *samr_policy,
42 const struct dom_sid *domain_sid,
44 struct wbint_userinfo **pinfo)
46 struct wbint_userinfo *info = NULL;
47 uint32_t num_info = 0;
48 uint32_t loop_count = 0;
49 uint32_t start_idx = 0;
57 uint32_t num_dom_users;
58 uint32_t max_entries, max_size;
59 uint32_t total_size, returned_size;
60 union samr_DispInfo disp_info;
62 get_query_dispinfo_params(loop_count,
66 status = rpccli_samr_QueryDisplayInfo(samr_pipe,
76 if (!NT_STATUS_IS_OK(status)) {
77 if (!NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
82 /* increment required start query values */
83 start_idx += disp_info.info1.count;
85 num_dom_users = disp_info.info1.count;
87 num_info += num_dom_users;
89 info = TALLOC_REALLOC_ARRAY(mem_ctx,
91 struct wbint_userinfo,
94 return NT_STATUS_NO_MEMORY;
97 for (j = 0; j < num_dom_users; i++, j++) {
98 uint32_t rid = disp_info.info1.entries[j].rid;
99 struct samr_DispEntryGeneral *src;
100 struct wbint_userinfo *dst;
102 src = &(disp_info.info1.entries[j]);
105 dst->acct_name = talloc_strdup(info,
106 src->account_name.string);
107 if (dst->acct_name == NULL) {
108 return NT_STATUS_NO_MEMORY;
111 dst->full_name = talloc_strdup(info, src->full_name.string);
112 if (dst->full_name == NULL) {
113 return NT_STATUS_NO_MEMORY;
119 sid_compose(&dst->user_sid, domain_sid, rid);
121 /* For the moment we set the primary group for
122 every user to be the Domain Users group.
123 There are serious problems with determining
124 the actual primary group for large domains.
125 This should really be made into a 'winbind
126 force group' smb.conf parameter or
127 something like that. */
128 sid_compose(&dst->group_sid, domain_sid,
131 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
133 *pnum_info = num_info;
139 /* List all domain groups */
140 NTSTATUS rpc_enum_dom_groups(TALLOC_CTX *mem_ctx,
141 struct rpc_pipe_client *samr_pipe,
142 struct policy_handle *samr_policy,
144 struct acct_info **pinfo)
146 struct acct_info *info = NULL;
148 uint32_t num_info = 0;
154 struct samr_SamArray *sam_array = NULL;
158 /* start is updated by this call. */
159 status = rpccli_samr_EnumDomainGroups(samr_pipe,
164 0xFFFF, /* buffer size? */
166 if (!NT_STATUS_IS_OK(status)) {
167 if (!NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
168 DEBUG(2,("query_user_list: failed to enum domain groups: %s\n",
174 info = TALLOC_REALLOC_ARRAY(mem_ctx,
179 return NT_STATUS_NO_MEMORY;
182 for (g = 0; g < count; g++) {
183 fstrcpy(info[num_info + g].acct_name,
184 sam_array->entries[g].name.string);
186 info[num_info + g].rid = sam_array->entries[g].idx;
190 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
192 *pnum_info = num_info;
198 NTSTATUS rpc_enum_local_groups(TALLOC_CTX *mem_ctx,
199 struct rpc_pipe_client *samr_pipe,
200 struct policy_handle *samr_policy,
202 struct acct_info **pinfo)
204 struct acct_info *info = NULL;
205 uint32_t num_info = 0;
211 struct samr_SamArray *sam_array = NULL;
213 uint32_t start = num_info;
216 status = rpccli_samr_EnumDomainAliases(samr_pipe,
221 0xFFFF, /* buffer size? */
223 if (!NT_STATUS_IS_OK(status)) {
224 if (!NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
229 info = TALLOC_REALLOC_ARRAY(mem_ctx,
234 return NT_STATUS_NO_MEMORY;
237 for (g = 0; g < count; g++) {
238 fstrcpy(info[num_info + g].acct_name,
239 sam_array->entries[g].name.string);
240 info[num_info + g].rid = sam_array->entries[g].idx;
244 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
246 *pnum_info = num_info;
252 /* convert a single name to a sid in a domain */
253 NTSTATUS rpc_name_to_sid(TALLOC_CTX *mem_ctx,
254 struct rpc_pipe_client *lsa_pipe,
255 struct policy_handle *lsa_policy,
256 const char *domain_name,
260 enum lsa_SidType *type)
262 enum lsa_SidType *types = NULL;
263 struct dom_sid *sids = NULL;
264 char *full_name = NULL;
265 char *mapped_name = NULL;
268 if (name == NULL || name[0] == '\0') {
269 full_name = talloc_asprintf(mem_ctx, "%s", domain_name);
270 } else if (domain_name == NULL || domain_name[0] == '\0') {
271 full_name = talloc_asprintf(mem_ctx, "%s", name);
273 full_name = talloc_asprintf(mem_ctx, "%s\\%s", domain_name, name);
276 if (full_name == NULL) {
277 return NT_STATUS_NO_MEMORY;
280 status = normalize_name_unmap(mem_ctx, full_name, &mapped_name);
281 /* Reset the full_name pointer if we mapped anything */
282 if (NT_STATUS_IS_OK(status) ||
283 NT_STATUS_EQUAL(status, NT_STATUS_FILE_RENAMED)) {
284 full_name = mapped_name;
287 DEBUG(3,("name_to_sid: %s for domain %s\n",
288 full_name ? full_name : "", domain_name ));
291 * We don't run into deadlocks here, cause winbind_off() is
292 * called in the main function.
294 status = rpccli_lsa_lookup_names(lsa_pipe,
298 (const char **) &full_name,
303 if (!NT_STATUS_IS_OK(status)) {
304 DEBUG(2,("name_to_sid: failed to lookup name: %s\n",
309 sid_copy(sid, &sids[0]);
315 /* Convert a domain SID to a user or group name */
316 NTSTATUS rpc_sid_to_name(TALLOC_CTX *mem_ctx,
317 struct rpc_pipe_client *lsa_pipe,
318 struct policy_handle *lsa_policy,
319 struct winbindd_domain *domain,
320 const struct dom_sid *sid,
323 enum lsa_SidType *ptype)
325 char *mapped_name = NULL;
326 char **domains = NULL;
328 enum lsa_SidType *types = NULL;
332 status = rpccli_lsa_lookup_sids(lsa_pipe,
340 if (!NT_STATUS_IS_OK(status)) {
341 DEBUG(2,("sid_to_name: failed to lookup sids: %s\n",
346 *ptype = (enum lsa_SidType) types[0];
348 map_status = normalize_name_map(mem_ctx,
352 if (NT_STATUS_IS_OK(map_status) ||
353 NT_STATUS_EQUAL(map_status, NT_STATUS_FILE_RENAMED)) {
354 *pname = talloc_strdup(mem_ctx, mapped_name);
355 DEBUG(5,("returning mapped name -- %s\n", *pname));
357 *pname = talloc_strdup(mem_ctx, names[0]);
359 if (*pname == NULL) {
360 return NT_STATUS_NO_MEMORY;
363 *pdomain_name = talloc_strdup(mem_ctx, domains[0]);
364 if (*pdomain_name == NULL) {
365 return NT_STATUS_NO_MEMORY;
371 /* Convert a bunch of rids to user or group names */
372 NTSTATUS rpc_rids_to_names(TALLOC_CTX *mem_ctx,
373 struct rpc_pipe_client *lsa_pipe,
374 struct policy_handle *lsa_policy,
375 struct winbindd_domain *domain,
376 const struct dom_sid *sid,
381 enum lsa_SidType **ptypes)
383 enum lsa_SidType *types = NULL;
384 char *domain_name = NULL;
385 char **domains = NULL;
387 struct dom_sid *sids;
392 sids = TALLOC_ARRAY(mem_ctx, struct dom_sid, num_rids);
394 return NT_STATUS_NO_MEMORY;
400 for (i = 0; i < num_rids; i++) {
401 if (!sid_compose(&sids[i], sid, rids[i])) {
402 return NT_STATUS_INTERNAL_ERROR;
406 status = rpccli_lsa_lookup_sids(lsa_pipe,
414 if (!NT_STATUS_IS_OK(status) &&
415 !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
416 DEBUG(2,("rids_to_names: failed to lookup sids: %s\n",
421 for (i = 0; i < num_rids; i++) {
422 char *mapped_name = NULL;
425 if (types[i] != SID_NAME_UNKNOWN) {
426 map_status = normalize_name_map(mem_ctx,
430 if (NT_STATUS_IS_OK(map_status) ||
431 NT_STATUS_EQUAL(map_status, NT_STATUS_FILE_RENAMED)) {
432 TALLOC_FREE(names[i]);
433 names[i] = talloc_strdup(names, mapped_name);
434 if (names[i] == NULL) {
435 return NT_STATUS_NO_MEMORY;
439 domain_name = domains[i];
443 *pdomain_name = domain_name;
450 /* Lookup user information from a rid or username. */
451 NTSTATUS rpc_query_user(TALLOC_CTX *mem_ctx,
452 struct rpc_pipe_client *samr_pipe,
453 struct policy_handle *samr_policy,
454 const struct dom_sid *domain_sid,
455 const struct dom_sid *user_sid,
456 struct wbint_userinfo *user_info)
458 struct policy_handle user_policy;
459 union samr_UserInfo *info = NULL;
463 if (!sid_peek_check_rid(domain_sid, user_sid, &user_rid)) {
464 return NT_STATUS_UNSUCCESSFUL;
467 /* Get user handle */
468 status = rpccli_samr_OpenUser(samr_pipe,
471 SEC_FLAG_MAXIMUM_ALLOWED,
474 if (!NT_STATUS_IS_OK(status)) {
479 status = rpccli_samr_QueryUserInfo(samr_pipe,
485 rpccli_samr_Close(samr_pipe, mem_ctx, &user_policy);
487 if (!NT_STATUS_IS_OK(status)) {
491 sid_compose(&user_info->user_sid, domain_sid, user_rid);
492 sid_compose(&user_info->group_sid, domain_sid,
493 info->info21.primary_gid);
495 user_info->acct_name = talloc_strdup(user_info,
496 info->info21.account_name.string);
497 if (user_info->acct_name == NULL) {
498 return NT_STATUS_NO_MEMORY;
501 user_info->full_name = talloc_strdup(user_info,
502 info->info21.full_name.string);
503 if (user_info->acct_name == NULL) {
504 return NT_STATUS_NO_MEMORY;
507 user_info->homedir = NULL;
508 user_info->shell = NULL;
509 user_info->primary_gid = (gid_t)-1;
514 /* Lookup groups a user is a member of. */
515 NTSTATUS rpc_lookup_usergroups(TALLOC_CTX *mem_ctx,
516 struct rpc_pipe_client *samr_pipe,
517 struct policy_handle *samr_policy,
518 const struct dom_sid *domain_sid,
519 const struct dom_sid *user_sid,
520 uint32_t *pnum_groups,
521 struct dom_sid **puser_grpsids)
523 struct policy_handle user_policy;
524 struct samr_RidWithAttributeArray *rid_array = NULL;
525 struct dom_sid *user_grpsids = NULL;
526 uint32_t num_groups = 0, i;
530 if (!sid_peek_check_rid(domain_sid, user_sid, &user_rid)) {
531 return NT_STATUS_UNSUCCESSFUL;
534 /* Get user handle */
535 status = rpccli_samr_OpenUser(samr_pipe,
538 SEC_FLAG_MAXIMUM_ALLOWED,
541 if (!NT_STATUS_IS_OK(status)) {
545 /* Query user rids */
546 status = rpccli_samr_GetGroupsForUser(samr_pipe,
550 num_groups = rid_array->count;
552 rpccli_samr_Close(samr_pipe, mem_ctx, &user_policy);
554 if (!NT_STATUS_IS_OK(status) || num_groups == 0) {
558 user_grpsids = TALLOC_ARRAY(mem_ctx, struct dom_sid, num_groups);
559 if (user_grpsids == NULL) {
560 status = NT_STATUS_NO_MEMORY;
564 for (i = 0; i < num_groups; i++) {
565 sid_compose(&(user_grpsids[i]), domain_sid,
566 rid_array->rids[i].rid);
569 *pnum_groups = num_groups;
571 *puser_grpsids = user_grpsids;
576 NTSTATUS rpc_lookup_useraliases(TALLOC_CTX *mem_ctx,
577 struct rpc_pipe_client *samr_pipe,
578 struct policy_handle *samr_policy,
580 const struct dom_sid *sids,
581 uint32_t *pnum_aliases,
582 uint32_t **palias_rids)
584 #define MAX_SAM_ENTRIES_W2K 0x400 /* 1024 */
585 uint32_t num_query_sids = 0;
586 uint32_t num_queries = 1;
587 uint32_t num_aliases = 0;
588 uint32_t total_sids = 0;
589 uint32_t *alias_rids = NULL;
590 uint32_t rangesize = MAX_SAM_ENTRIES_W2K;
592 struct samr_Ids alias_rids_query;
597 struct lsa_SidArray sid_array;
599 ZERO_STRUCT(sid_array);
601 num_query_sids = MIN(num_sids - total_sids, rangesize);
603 DEBUG(10,("rpc: lookup_useraliases: entering query %d for %d sids\n",
604 num_queries, num_query_sids));
606 if (num_query_sids) {
607 sid_array.sids = TALLOC_ZERO_ARRAY(mem_ctx, struct lsa_SidPtr, num_query_sids);
608 if (sid_array.sids == NULL) {
609 return NT_STATUS_NO_MEMORY;
612 sid_array.sids = NULL;
615 for (i = 0; i < num_query_sids; i++) {
616 sid_array.sids[i].sid = dom_sid_dup(mem_ctx, &sids[total_sids++]);
617 if (sid_array.sids[i].sid == NULL) {
618 return NT_STATUS_NO_MEMORY;
621 sid_array.num_sids = num_query_sids;
624 status = rpccli_samr_GetAliasMembership(samr_pipe,
629 if (!NT_STATUS_IS_OK(status)) {
634 for (i = 0; i < alias_rids_query.count; i++) {
635 size_t na = num_aliases;
637 if (!add_rid_to_array_unique(mem_ctx,
638 alias_rids_query.ids[i],
641 return NT_STATUS_NO_MEMORY;
648 } while (total_sids < num_sids);
650 DEBUG(10,("rpc: rpc_lookup_useraliases: got %d aliases in %d queries "
651 "(rangesize: %d)\n", num_aliases, num_queries, rangesize));
653 *pnum_aliases = num_aliases;
654 *palias_rids = alias_rids;
657 #undef MAX_SAM_ENTRIES_W2K
660 /* Lookup group membership given a rid. */
661 NTSTATUS rpc_lookup_groupmem(TALLOC_CTX *mem_ctx,
662 struct rpc_pipe_client *samr_pipe,
663 struct policy_handle *samr_policy,
664 const char *domain_name,
665 const struct dom_sid *domain_sid,
666 const struct dom_sid *group_sid,
667 enum lsa_SidType type,
668 uint32_t *pnum_names,
669 struct dom_sid **psid_mem,
671 uint32_t **pname_types)
673 struct policy_handle group_policy;
675 uint32_t *rid_mem = NULL;
677 uint32_t num_names = 0;
678 uint32_t total_names = 0;
679 struct dom_sid *sid_mem = NULL;
681 uint32_t *name_types = NULL;
683 struct lsa_Strings tmp_names;
684 struct samr_Ids tmp_types;
689 if (!sid_peek_check_rid(domain_sid, group_sid, &group_rid)) {
690 return NT_STATUS_UNSUCCESSFUL;
694 case SID_NAME_DOM_GRP:
696 struct samr_RidAttrArray *rids = NULL;
698 status = rpccli_samr_OpenGroup(samr_pipe,
701 SEC_FLAG_MAXIMUM_ALLOWED,
704 if (!NT_STATUS_IS_OK(status)) {
709 * Step #1: Get a list of user rids that are the members of the group.
711 status = rpccli_samr_QueryGroupMember(samr_pipe,
716 rpccli_samr_Close(samr_pipe, mem_ctx, &group_policy);
718 if (!NT_STATUS_IS_OK(status)) {
722 if (rids == NULL || rids->count == 0) {
731 num_names = rids->count;
732 rid_mem = rids->rids;
736 case SID_NAME_WKN_GRP:
739 struct lsa_SidArray sid_array;
740 struct lsa_SidPtr sid_ptr;
741 struct samr_Ids rids_query;
743 sid_ptr.sid = dom_sid_dup(mem_ctx, group_sid);
744 if (sid_ptr.sid == NULL) {
745 return NT_STATUS_NO_MEMORY;
748 sid_array.num_sids = 1;
749 sid_array.sids = &sid_ptr;
751 status = rpccli_samr_GetAliasMembership(samr_pipe,
757 if (rids_query.count == 0) {
766 num_names = rids_query.count;
767 rid_mem = rids_query.ids;
772 return NT_STATUS_UNSUCCESSFUL;
776 * Step #2: Convert list of rids into list of usernames.
779 names = TALLOC_ZERO_ARRAY(mem_ctx, char *, num_names);
780 name_types = TALLOC_ZERO_ARRAY(mem_ctx, uint32_t, num_names);
781 sid_mem = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, num_names);
782 if (names == NULL || name_types == NULL || sid_mem == NULL) {
783 return NT_STATUS_NO_MEMORY;
787 for (j = 0; j < num_names; j++) {
788 sid_compose(&sid_mem[j], domain_sid, rid_mem[j]);
791 status = rpccli_samr_LookupRids(samr_pipe,
798 if (!NT_STATUS_IS_OK(status)) {
799 if (!NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
804 /* Copy result into array. The talloc system will take
805 care of freeing the temporary arrays later on. */
806 if (tmp_names.count != tmp_types.count) {
807 return NT_STATUS_UNSUCCESSFUL;
810 for (r = 0; r < tmp_names.count; r++) {
811 if (tmp_types.ids[r] == SID_NAME_UNKNOWN) {
814 names[total_names] = fill_domain_username_talloc(names,
816 tmp_names.names[r].string,
818 if (names[total_names] == NULL) {
819 return NT_STATUS_NO_MEMORY;
821 name_types[total_names] = tmp_types.ids[r];
825 *pnum_names = total_names;
827 *pname_types = name_types;
833 /* Find the sequence number for a domain */
834 NTSTATUS rpc_sequence_number(TALLOC_CTX *mem_ctx,
835 struct rpc_pipe_client *samr_pipe,
836 struct policy_handle *samr_policy,
837 const char *domain_name,
840 union samr_DomainInfo *info = NULL;
841 bool got_seq_num = false;
844 /* query domain info */
845 status = rpccli_samr_QueryDomainInfo(samr_pipe,
850 if (NT_STATUS_IS_OK(status)) {
851 *pseq = info->info8.sequence_num;
856 /* retry with info-level 2 in case the dc does not support info-level 8
857 * (like all older samba2 and samba3 dc's) - Guenther */
858 status = rpccli_samr_QueryDomainInfo(samr_pipe,
863 if (NT_STATUS_IS_OK(status)) {
864 *pseq = info->general.sequence_num;
870 DEBUG(10,("domain_sequence_number: for domain %s is %u\n",
871 domain_name, (unsigned) *pseq));
873 DEBUG(10,("domain_sequence_number: failed to get sequence "
874 "number (%u) for domain %s\n",
875 (unsigned) *pseq, domain_name ));
876 status = NT_STATUS_OK;
882 /* Get a list of trusted domains */
883 NTSTATUS rpc_trusted_domains(TALLOC_CTX *mem_ctx,
884 struct rpc_pipe_client *lsa_pipe,
885 struct policy_handle *lsa_policy,
886 uint32_t *pnum_trusts,
887 struct netr_DomainTrust **ptrusts)
889 struct netr_DomainTrust *array = NULL;
890 uint32_t enum_ctx = 0;
895 struct lsa_DomainList dom_list;
900 * We don't run into deadlocks here, cause winbind_off() is
901 * called in the main function.
903 status = rpccli_lsa_EnumTrustDom(lsa_pipe,
909 if (!NT_STATUS_IS_OK(status)) {
910 if (!NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
916 count += dom_list.count;
918 array = talloc_realloc(mem_ctx,
920 struct netr_DomainTrust,
923 return NT_STATUS_NO_MEMORY;
926 for (i = 0; i < dom_list.count; i++) {
927 struct netr_DomainTrust *trust = &array[i];
932 trust->netbios_name = talloc_move(array,
933 &dom_list.domains[i].name.string);
934 trust->dns_name = NULL;
936 sid = talloc(array, struct dom_sid);
938 return NT_STATUS_NO_MEMORY;
940 sid_copy(sid, dom_list.domains[i].sid);
943 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
945 *pnum_trusts = count;