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_policy2(cli->binding_handle,
154 if (!NT_STATUS_IS_OK(status)) {
161 /* Lookup a list of sids
163 * internal version withOUT memory allocation of the target arrays.
164 * this assumes sufficiently sized arrays to store domains, names and types. */
166 static NTSTATUS dcerpc_lsa_lookup_sids_noalloc(struct dcerpc_binding_handle *h,
168 struct policy_handle *pol,
170 const struct dom_sid *sids,
173 enum lsa_SidType *types,
174 bool use_lookupsids3,
177 NTSTATUS status = NT_STATUS_OK;
178 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
179 struct lsa_SidArray sid_array;
180 struct lsa_RefDomainList *ref_domains = NULL;
181 struct lsa_TransNameArray lsa_names;
182 enum lsa_LookupNamesLevel level = LSA_LOOKUP_NAMES_ALL;
186 ZERO_STRUCT(lsa_names);
188 sid_array.num_sids = num_sids;
189 sid_array.sids = TALLOC_ARRAY(mem_ctx, struct lsa_SidPtr, num_sids);
190 if (sid_array.sids == NULL) {
191 return NT_STATUS_NO_MEMORY;
194 for (i = 0; i<num_sids; i++) {
195 sid_array.sids[i].sid = dom_sid_dup(mem_ctx, &sids[i]);
196 if (!sid_array.sids[i].sid) {
197 return NT_STATUS_NO_MEMORY;
201 if (use_lookupsids3) {
202 struct lsa_TransNameArray2 lsa_names2;
205 ZERO_STRUCT(lsa_names2);
207 status = dcerpc_lsa_LookupSids3(h,
214 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES,
215 LSA_CLIENT_REVISION_2,
217 if (!NT_STATUS_IS_OK(status)) {
221 if(!NT_STATUS_IS_ERR(result)) {
222 lsa_names.count = lsa_names2.count;
223 lsa_names.names = talloc_array(mem_ctx,
224 struct lsa_TranslatedName,
226 if (lsa_names.names == NULL) {
227 return NT_STATUS_NO_MEMORY;
229 for (n=0; n < lsa_names.count; n++) {
230 lsa_names.names[n].sid_type = lsa_names2.names[n].sid_type;
231 lsa_names.names[n].name = lsa_names2.names[n].name;
232 lsa_names.names[n].sid_index = lsa_names2.names[n].sid_index;
237 status = dcerpc_lsa_LookupSids(h,
248 DEBUG(10, ("LSA_LOOKUPSIDS returned status: '%s', result: '%s', "
249 "mapped count = %d'\n",
250 nt_errstr(status), nt_errstr(result), count));
252 if (!NT_STATUS_IS_OK(status)) {
256 if (!NT_STATUS_IS_OK(result) &&
257 !NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
258 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
264 /* Return output parameters */
265 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
268 for (i = 0; i < num_sids; i++) {
271 (types)[i] = SID_NAME_UNKNOWN;
273 *presult = NT_STATUS_NONE_MAPPED;
277 for (i = 0; i < num_sids; i++) {
278 const char *name, *dom_name;
279 uint32_t dom_idx = lsa_names.names[i].sid_index;
281 /* Translate optimised name through domain index array */
283 if (dom_idx != 0xffffffff) {
285 dom_name = ref_domains->domains[dom_idx].name.string;
286 name = lsa_names.names[i].name.string;
289 (names)[i] = talloc_strdup(names, name);
290 if ((names)[i] == NULL) {
291 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
292 *presult = NT_STATUS_UNSUCCESSFUL;
298 domains[i] = talloc_strdup(domains,
299 dom_name ? dom_name : "");
300 (types)[i] = lsa_names.names[i].sid_type;
301 if (((domains)[i] == NULL)) {
302 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
303 *presult = NT_STATUS_UNSUCCESSFUL;
310 (types)[i] = SID_NAME_UNKNOWN;
314 *presult = NT_STATUS_OK;
318 /* Lookup a list of sids
320 * do it the right way: there is a limit (of 20480 for w2k3) entries
321 * returned by this call. when the sids list contains more entries,
322 * empty lists are returned. This version of lsa_lookup_sids passes
323 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
325 /* This constant defines the limit of how many sids to look up
326 * in one call (maximum). the limit from the server side is
327 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
328 #define LOOKUP_SIDS_HUNK_SIZE 1000
330 static NTSTATUS dcerpc_lsa_lookup_sids_generic(struct dcerpc_binding_handle *h,
332 struct policy_handle *pol,
334 const struct dom_sid *sids,
337 enum lsa_SidType **ptypes,
338 bool use_lookupsids3,
341 NTSTATUS status = NT_STATUS_OK;
342 NTSTATUS result = NT_STATUS_OK;
344 int sids_processed = 0;
345 const struct dom_sid *hunk_sids = sids;
348 enum lsa_SidType *hunk_types;
349 char **domains = NULL;
351 enum lsa_SidType *types = NULL;
352 bool have_mapped = false;
353 bool have_unmapped = false;
356 if (!(domains = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
357 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
358 status = NT_STATUS_NO_MEMORY;
362 if (!(names = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
363 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
364 status = NT_STATUS_NO_MEMORY;
368 if (!(types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
369 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
370 status = NT_STATUS_NO_MEMORY;
375 sids_left = num_sids;
376 hunk_domains = domains;
380 while (sids_left > 0) {
382 NTSTATUS hunk_result = NT_STATUS_UNSUCCESSFUL;
384 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
385 ? LOOKUP_SIDS_HUNK_SIZE
388 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
391 sids_processed + hunk_num_sids - 1,
394 status = dcerpc_lsa_lookup_sids_noalloc(h,
404 if (!NT_STATUS_IS_OK(status)) {
408 if (!NT_STATUS_IS_OK(hunk_result) &&
409 !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
410 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
412 /* An actual error occured */
413 *presult = hunk_result;
417 if (NT_STATUS_IS_OK(hunk_result)) {
420 if (NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)) {
421 have_unmapped = true;
423 if (NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED)) {
425 for (i=0; i<hunk_num_sids; i++) {
426 if (hunk_types[i] == SID_NAME_UNKNOWN) {
427 have_unmapped = true;
434 sids_left -= hunk_num_sids;
435 sids_processed += hunk_num_sids; /* only used in DEBUG */
436 hunk_sids += hunk_num_sids;
437 hunk_domains += hunk_num_sids;
438 hunk_names += hunk_num_sids;
439 hunk_types += hunk_num_sids;
447 result = NT_STATUS_NONE_MAPPED;
450 result = STATUS_SOME_UNMAPPED;
457 TALLOC_FREE(domains);
464 NTSTATUS dcerpc_lsa_lookup_sids(struct dcerpc_binding_handle *h,
466 struct policy_handle *pol,
468 const struct dom_sid *sids,
471 enum lsa_SidType **ptypes,
474 return dcerpc_lsa_lookup_sids_generic(h,
486 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
488 struct policy_handle *pol,
490 const struct dom_sid *sids,
493 enum lsa_SidType **ptypes)
496 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
498 status = dcerpc_lsa_lookup_sids_generic(cli->binding_handle,
508 if (!NT_STATUS_IS_OK(status)) {
515 NTSTATUS dcerpc_lsa_lookup_sids3(struct dcerpc_binding_handle *h,
517 struct policy_handle *pol,
519 const struct dom_sid *sids,
522 enum lsa_SidType **ptypes,
525 return dcerpc_lsa_lookup_sids_generic(h,
537 NTSTATUS rpccli_lsa_lookup_sids3(struct rpc_pipe_client *cli,
539 struct policy_handle *pol,
541 const struct dom_sid *sids,
544 enum lsa_SidType **ptypes)
547 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
549 status = dcerpc_lsa_lookup_sids_generic(cli->binding_handle,
559 if (!NT_STATUS_IS_OK(status)) {
566 /** Lookup a list of names */
568 static NTSTATUS dcerpc_lsa_lookup_names_generic(struct dcerpc_binding_handle *h,
570 struct policy_handle *pol,
573 const char ***dom_names,
574 enum lsa_LookupNamesLevel level,
575 struct dom_sid **sids,
576 enum lsa_SidType **types,
577 bool use_lookupnames4,
581 struct lsa_String *lsa_names = NULL;
582 struct lsa_RefDomainList *domains = NULL;
583 struct lsa_TransSidArray sid_array;
584 struct lsa_TransSidArray3 sid_array3;
588 ZERO_STRUCT(sid_array);
589 ZERO_STRUCT(sid_array3);
591 lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
592 if (lsa_names == NULL) {
593 return NT_STATUS_NO_MEMORY;
596 for (i = 0; i < num_names; i++) {
597 init_lsa_String(&lsa_names[i], names[i]);
600 if (use_lookupnames4) {
601 status = dcerpc_lsa_LookupNames4(h,
609 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES,
610 LSA_CLIENT_REVISION_2,
613 status = dcerpc_lsa_LookupNames(h,
624 if (!NT_STATUS_IS_OK(status)) {
628 if (!NT_STATUS_IS_OK(*presult) &&
629 !NT_STATUS_EQUAL(*presult, STATUS_SOME_UNMAPPED)) {
630 /* An actual error occured */
634 /* Return output parameters */
636 *presult = NT_STATUS_NONE_MAPPED;
641 if (!((*sids = TALLOC_ARRAY(mem_ctx, struct dom_sid, num_names)))) {
642 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
643 *presult = NT_STATUS_NO_MEMORY;
647 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
648 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
649 *presult = NT_STATUS_NO_MEMORY;
653 if (dom_names != NULL) {
654 *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
655 if (*dom_names == NULL) {
656 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
657 *presult = NT_STATUS_NO_MEMORY;
664 if (dom_names != NULL) {
669 for (i = 0; i < num_names; i++) {
671 struct dom_sid *sid = &(*sids)[i];
673 if (use_lookupnames4) {
674 dom_idx = sid_array3.sids[i].sid_index;
675 (*types)[i] = sid_array3.sids[i].sid_type;
677 dom_idx = sid_array.sids[i].sid_index;
678 (*types)[i] = sid_array.sids[i].sid_type;
681 /* Translate optimised sid through domain index array */
683 if (dom_idx == 0xffffffff) {
684 /* Nothing to do, this is unknown */
686 (*types)[i] = SID_NAME_UNKNOWN;
690 if (use_lookupnames4) {
691 sid_copy(sid, sid_array3.sids[i].sid);
693 sid_copy(sid, domains->domains[dom_idx].sid);
695 if (sid_array.sids[i].rid != 0xffffffff) {
696 sid_append_rid(sid, sid_array.sids[i].rid);
700 if (dom_names == NULL) {
704 (*dom_names)[i] = domains->domains[dom_idx].name.string;
711 NTSTATUS dcerpc_lsa_lookup_names(struct dcerpc_binding_handle *h,
713 struct policy_handle *pol,
716 const char ***dom_names,
717 enum lsa_LookupNamesLevel level,
718 struct dom_sid **sids,
719 enum lsa_SidType **types,
722 return dcerpc_lsa_lookup_names_generic(h,
735 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
737 struct policy_handle *pol,
740 const char ***dom_names,
742 struct dom_sid **sids,
743 enum lsa_SidType **types)
746 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
748 status = dcerpc_lsa_lookup_names(cli->binding_handle,
758 if (!NT_STATUS_IS_OK(status)) {
765 NTSTATUS dcerpc_lsa_lookup_names4(struct dcerpc_binding_handle *h,
767 struct policy_handle *pol,
770 const char ***dom_names,
771 enum lsa_LookupNamesLevel level,
772 struct dom_sid **sids,
773 enum lsa_SidType **types,
776 return dcerpc_lsa_lookup_names_generic(h,
789 NTSTATUS rpccli_lsa_lookup_names4(struct rpc_pipe_client *cli,
791 struct policy_handle *pol,
794 const char ***dom_names,
796 struct dom_sid **sids,
797 enum lsa_SidType **types)
800 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
802 status = dcerpc_lsa_lookup_names4(cli->binding_handle,
812 if (!NT_STATUS_IS_OK(status)) {