2 Unix SMB/CIFS implementation.
4 Copyright (C) Tim Potter 2000-2001,
5 Copyright (C) Andrew Tridgell 1992-1997,2000,
6 Copyright (C) Rafal Szczesniak 2002
7 Copyright (C) Jeremy Allison 2005.
8 Copyright (C) Michael Adam 2007.
9 Copyright (C) Guenther Deschner 2008.
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/>.
26 #include "../librpc/gen_ndr/ndr_lsa_c.h"
27 #include "rpc_client/cli_lsarpc.h"
28 #include "rpc_client/init_lsa.h"
29 #include "../libcli/security/security.h"
31 /** @defgroup lsa LSA - Local Security Architecture
40 * RPC client routines for the LSA RPC pipe. LSA means "local
41 * security authority", which is half of a password database.
44 NTSTATUS dcerpc_lsa_open_policy(struct dcerpc_binding_handle *h,
48 struct policy_handle *pol,
51 struct lsa_ObjectAttribute attr;
52 struct lsa_QosInfo qos;
53 uint16_t system_name = '\\';
61 qos.impersonation_level = 2;
63 qos.effective_only = 0;
68 return dcerpc_lsa_OpenPolicy(h,
77 /** Open a LSA policy handle
79 * @param cli Handle on an initialised SMB connection */
81 NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
83 bool sec_qos, uint32 des_access,
84 struct policy_handle *pol)
87 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
89 status = dcerpc_lsa_open_policy(cli->binding_handle,
95 if (!NT_STATUS_IS_OK(status)) {
102 NTSTATUS dcerpc_lsa_open_policy2(struct dcerpc_binding_handle *h,
104 const char *srv_name_slash,
107 struct policy_handle *pol,
110 struct lsa_ObjectAttribute attr;
111 struct lsa_QosInfo qos;
119 qos.impersonation_level = 2;
120 qos.context_mode = 1;
121 qos.effective_only = 0;
126 return dcerpc_lsa_OpenPolicy2(h,
135 /** Open a LSA policy handle
137 * @param cli Handle on an initialised SMB connection
140 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
141 TALLOC_CTX *mem_ctx, bool sec_qos,
142 uint32 des_access, struct policy_handle *pol)
145 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
147 status = dcerpc_lsa_open_policy(cli->binding_handle,
153 if (!NT_STATUS_IS_OK(status)) {
160 /* Lookup a list of sids
162 * internal version withOUT memory allocation of the target arrays.
163 * this assumes sufficiently sized arrays to store domains, names and types. */
165 static NTSTATUS dcerpc_lsa_lookup_sids_noalloc(struct dcerpc_binding_handle *h,
167 struct policy_handle *pol,
169 const struct dom_sid *sids,
172 enum lsa_SidType *types,
173 bool use_lookupsids3,
176 NTSTATUS status = NT_STATUS_OK;
177 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
178 struct lsa_SidArray sid_array;
179 struct lsa_RefDomainList *ref_domains = NULL;
180 struct lsa_TransNameArray lsa_names;
181 enum lsa_LookupNamesLevel level = LSA_LOOKUP_NAMES_ALL;
185 ZERO_STRUCT(lsa_names);
187 sid_array.num_sids = num_sids;
188 sid_array.sids = TALLOC_ARRAY(mem_ctx, struct lsa_SidPtr, num_sids);
189 if (sid_array.sids == NULL) {
190 return NT_STATUS_NO_MEMORY;
193 for (i = 0; i<num_sids; i++) {
194 sid_array.sids[i].sid = dom_sid_dup(mem_ctx, &sids[i]);
195 if (!sid_array.sids[i].sid) {
196 return NT_STATUS_NO_MEMORY;
200 if (use_lookupsids3) {
201 struct lsa_TransNameArray2 lsa_names2;
204 ZERO_STRUCT(lsa_names2);
206 status = dcerpc_lsa_LookupSids3(h,
213 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES,
214 LSA_CLIENT_REVISION_2,
216 if (!NT_STATUS_IS_OK(status)) {
220 if(!NT_STATUS_IS_ERR(result)) {
221 lsa_names.count = lsa_names2.count;
222 lsa_names.names = talloc_array(mem_ctx,
223 struct lsa_TranslatedName,
225 if (lsa_names.names == NULL) {
226 return NT_STATUS_NO_MEMORY;
228 for (n=0; n < lsa_names.count; n++) {
229 lsa_names.names[n].sid_type = lsa_names2.names[n].sid_type;
230 lsa_names.names[n].name = lsa_names2.names[n].name;
231 lsa_names.names[n].sid_index = lsa_names2.names[n].sid_index;
236 status = dcerpc_lsa_LookupSids(h,
247 DEBUG(10, ("LSA_LOOKUPSIDS returned status: '%s', result: '%s', "
248 "mapped count = %d'\n",
249 nt_errstr(status), nt_errstr(result), count));
251 if (!NT_STATUS_IS_OK(status)) {
255 if (!NT_STATUS_IS_OK(result) &&
256 !NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
257 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
263 /* Return output parameters */
264 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
267 for (i = 0; i < num_sids; i++) {
270 (types)[i] = SID_NAME_UNKNOWN;
272 *presult = NT_STATUS_NONE_MAPPED;
276 for (i = 0; i < num_sids; i++) {
277 const char *name, *dom_name;
278 uint32_t dom_idx = lsa_names.names[i].sid_index;
280 /* Translate optimised name through domain index array */
282 if (dom_idx != 0xffffffff) {
284 dom_name = ref_domains->domains[dom_idx].name.string;
285 name = lsa_names.names[i].name.string;
288 (names)[i] = talloc_strdup(names, name);
289 if ((names)[i] == NULL) {
290 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
291 *presult = NT_STATUS_UNSUCCESSFUL;
297 domains[i] = talloc_strdup(domains,
298 dom_name ? dom_name : "");
299 (types)[i] = lsa_names.names[i].sid_type;
300 if (((domains)[i] == NULL)) {
301 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
302 *presult = NT_STATUS_UNSUCCESSFUL;
309 (types)[i] = SID_NAME_UNKNOWN;
313 *presult = NT_STATUS_OK;
317 /* Lookup a list of sids
319 * do it the right way: there is a limit (of 20480 for w2k3) entries
320 * returned by this call. when the sids list contains more entries,
321 * empty lists are returned. This version of lsa_lookup_sids passes
322 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
324 /* This constant defines the limit of how many sids to look up
325 * in one call (maximum). the limit from the server side is
326 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
327 #define LOOKUP_SIDS_HUNK_SIZE 1000
329 static NTSTATUS dcerpc_lsa_lookup_sids_generic(struct dcerpc_binding_handle *h,
331 struct policy_handle *pol,
333 const struct dom_sid *sids,
336 enum lsa_SidType **ptypes,
337 bool use_lookupsids3,
340 NTSTATUS status = NT_STATUS_OK;
341 NTSTATUS result = NT_STATUS_OK;
343 int sids_processed = 0;
344 const struct dom_sid *hunk_sids = sids;
347 enum lsa_SidType *hunk_types;
348 char **domains = NULL;
350 enum lsa_SidType *types = NULL;
351 bool have_mapped = false;
352 bool have_unmapped = false;
355 if (!(domains = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
356 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
357 status = NT_STATUS_NO_MEMORY;
361 if (!(names = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
362 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
363 status = NT_STATUS_NO_MEMORY;
367 if (!(types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
368 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
369 status = NT_STATUS_NO_MEMORY;
374 sids_left = num_sids;
375 hunk_domains = domains;
379 while (sids_left > 0) {
381 NTSTATUS hunk_result = NT_STATUS_UNSUCCESSFUL;
383 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
384 ? LOOKUP_SIDS_HUNK_SIZE
387 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
390 sids_processed + hunk_num_sids - 1,
393 status = dcerpc_lsa_lookup_sids_noalloc(h,
403 if (!NT_STATUS_IS_OK(status)) {
407 if (!NT_STATUS_IS_OK(hunk_result) &&
408 !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
409 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
411 /* An actual error occured */
412 *presult = hunk_result;
416 if (NT_STATUS_IS_OK(hunk_result)) {
419 if (NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)) {
420 have_unmapped = true;
422 if (NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED)) {
424 for (i=0; i<hunk_num_sids; i++) {
425 if (hunk_types[i] == SID_NAME_UNKNOWN) {
426 have_unmapped = true;
433 sids_left -= hunk_num_sids;
434 sids_processed += hunk_num_sids; /* only used in DEBUG */
435 hunk_sids += hunk_num_sids;
436 hunk_domains += hunk_num_sids;
437 hunk_names += hunk_num_sids;
438 hunk_types += hunk_num_sids;
446 result = NT_STATUS_NONE_MAPPED;
449 result = STATUS_SOME_UNMAPPED;
456 TALLOC_FREE(domains);
463 NTSTATUS dcerpc_lsa_lookup_sids(struct dcerpc_binding_handle *h,
465 struct policy_handle *pol,
467 const struct dom_sid *sids,
470 enum lsa_SidType **ptypes,
473 return dcerpc_lsa_lookup_sids_generic(h,
485 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
487 struct policy_handle *pol,
489 const struct dom_sid *sids,
492 enum lsa_SidType **ptypes)
495 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
497 status = dcerpc_lsa_lookup_sids_generic(cli->binding_handle,
507 if (!NT_STATUS_IS_OK(status)) {
514 NTSTATUS dcerpc_lsa_lookup_sids3(struct dcerpc_binding_handle *h,
516 struct policy_handle *pol,
518 const struct dom_sid *sids,
521 enum lsa_SidType **ptypes,
524 return dcerpc_lsa_lookup_sids_generic(h,
536 NTSTATUS rpccli_lsa_lookup_sids3(struct rpc_pipe_client *cli,
538 struct policy_handle *pol,
540 const struct dom_sid *sids,
543 enum lsa_SidType **ptypes)
546 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
548 status = dcerpc_lsa_lookup_sids_generic(cli->binding_handle,
558 if (!NT_STATUS_IS_OK(status)) {
565 /** Lookup a list of names */
567 static NTSTATUS dcerpc_lsa_lookup_names_generic(struct dcerpc_binding_handle *h,
569 struct policy_handle *pol,
572 const char ***dom_names,
573 enum lsa_LookupNamesLevel level,
574 struct dom_sid **sids,
575 enum lsa_SidType **types,
576 bool use_lookupnames4,
580 struct lsa_String *lsa_names = NULL;
581 struct lsa_RefDomainList *domains = NULL;
582 struct lsa_TransSidArray sid_array;
583 struct lsa_TransSidArray3 sid_array3;
587 ZERO_STRUCT(sid_array);
588 ZERO_STRUCT(sid_array3);
590 lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
591 if (lsa_names == NULL) {
592 return NT_STATUS_NO_MEMORY;
595 for (i = 0; i < num_names; i++) {
596 init_lsa_String(&lsa_names[i], names[i]);
599 if (use_lookupnames4) {
600 status = dcerpc_lsa_LookupNames4(h,
608 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES,
609 LSA_CLIENT_REVISION_2,
612 status = dcerpc_lsa_LookupNames(h,
623 if (!NT_STATUS_IS_OK(status)) {
627 if (!NT_STATUS_IS_OK(*presult) &&
628 !NT_STATUS_EQUAL(*presult, STATUS_SOME_UNMAPPED)) {
629 /* An actual error occured */
633 /* Return output parameters */
635 *presult = NT_STATUS_NONE_MAPPED;
640 if (!((*sids = TALLOC_ARRAY(mem_ctx, struct dom_sid, num_names)))) {
641 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
642 *presult = NT_STATUS_NO_MEMORY;
646 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
647 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
648 *presult = NT_STATUS_NO_MEMORY;
652 if (dom_names != NULL) {
653 *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
654 if (*dom_names == NULL) {
655 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
656 *presult = NT_STATUS_NO_MEMORY;
663 if (dom_names != NULL) {
668 for (i = 0; i < num_names; i++) {
670 struct dom_sid *sid = &(*sids)[i];
672 if (use_lookupnames4) {
673 dom_idx = sid_array3.sids[i].sid_index;
674 (*types)[i] = sid_array3.sids[i].sid_type;
676 dom_idx = sid_array.sids[i].sid_index;
677 (*types)[i] = sid_array.sids[i].sid_type;
680 /* Translate optimised sid through domain index array */
682 if (dom_idx == 0xffffffff) {
683 /* Nothing to do, this is unknown */
685 (*types)[i] = SID_NAME_UNKNOWN;
689 if (use_lookupnames4) {
690 sid_copy(sid, sid_array3.sids[i].sid);
692 sid_copy(sid, domains->domains[dom_idx].sid);
694 if (sid_array.sids[i].rid != 0xffffffff) {
695 sid_append_rid(sid, sid_array.sids[i].rid);
699 if (dom_names == NULL) {
703 (*dom_names)[i] = domains->domains[dom_idx].name.string;
710 NTSTATUS dcerpc_lsa_lookup_names(struct dcerpc_binding_handle *h,
712 struct policy_handle *pol,
715 const char ***dom_names,
716 enum lsa_LookupNamesLevel level,
717 struct dom_sid **sids,
718 enum lsa_SidType **types,
721 return dcerpc_lsa_lookup_names_generic(h,
734 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
736 struct policy_handle *pol,
739 const char ***dom_names,
741 struct dom_sid **sids,
742 enum lsa_SidType **types)
745 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
747 status = dcerpc_lsa_lookup_names(cli->binding_handle,
757 if (!NT_STATUS_IS_OK(status)) {
764 NTSTATUS dcerpc_lsa_lookup_names4(struct dcerpc_binding_handle *h,
766 struct policy_handle *pol,
769 const char ***dom_names,
770 enum lsa_LookupNamesLevel level,
771 struct dom_sid **sids,
772 enum lsa_SidType **types,
775 return dcerpc_lsa_lookup_names_generic(h,
788 NTSTATUS rpccli_lsa_lookup_names4(struct rpc_pipe_client *cli,
790 struct policy_handle *pol,
793 const char ***dom_names,
795 struct dom_sid **sids,
796 enum lsa_SidType **types)
799 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
801 status = dcerpc_lsa_lookup_names4(cli->binding_handle,
811 if (!NT_STATUS_IS_OK(status)) {