s4 dns: Implement RFC-compatible update prescan
[Samba/gebeck_regimport.git] / source3 / rpc_client / cli_lsarpc.c
blob66192bdf54f9ec8c8c51c121518731fb7e0aea33
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
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/>.
25 #include "includes.h"
26 #include "rpc_client/rpc_client.h"
27 #include "../librpc/gen_ndr/ndr_lsa_c.h"
28 #include "rpc_client/cli_lsarpc.h"
29 #include "rpc_client/init_lsa.h"
30 #include "../libcli/security/security.h"
32 /** @defgroup lsa LSA - Local Security Architecture
33 * @ingroup rpc_client
35 * @{
36 **/
38 /**
39 * @file cli_lsarpc.c
41 * RPC client routines for the LSA RPC pipe. LSA means "local
42 * security authority", which is half of a password database.
43 **/
45 NTSTATUS dcerpc_lsa_open_policy(struct dcerpc_binding_handle *h,
46 TALLOC_CTX *mem_ctx,
47 bool sec_qos,
48 uint32_t des_access,
49 struct policy_handle *pol,
50 NTSTATUS *result)
52 struct lsa_ObjectAttribute attr;
53 struct lsa_QosInfo qos;
54 uint16_t system_name = '\\';
56 ZERO_STRUCT(attr);
58 attr.len = 0x18;
60 if (sec_qos) {
61 qos.len = 0xc;
62 qos.impersonation_level = 2;
63 qos.context_mode = 1;
64 qos.effective_only = 0;
66 attr.sec_qos = &qos;
69 return dcerpc_lsa_OpenPolicy(h,
70 mem_ctx,
71 &system_name,
72 &attr,
73 des_access,
74 pol,
75 result);
78 /** Open a LSA policy handle
80 * @param cli Handle on an initialised SMB connection */
82 NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
83 TALLOC_CTX *mem_ctx,
84 bool sec_qos, uint32 des_access,
85 struct policy_handle *pol)
87 NTSTATUS status;
88 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
90 status = dcerpc_lsa_open_policy(cli->binding_handle,
91 mem_ctx,
92 sec_qos,
93 des_access,
94 pol,
95 &result);
96 if (!NT_STATUS_IS_OK(status)) {
97 return status;
100 return result;
103 NTSTATUS dcerpc_lsa_open_policy2(struct dcerpc_binding_handle *h,
104 TALLOC_CTX *mem_ctx,
105 const char *srv_name_slash,
106 bool sec_qos,
107 uint32_t des_access,
108 struct policy_handle *pol,
109 NTSTATUS *result)
111 struct lsa_ObjectAttribute attr;
112 struct lsa_QosInfo qos;
114 ZERO_STRUCT(attr);
116 attr.len = 0x18;
118 if (sec_qos) {
119 qos.len = 0xc;
120 qos.impersonation_level = 2;
121 qos.context_mode = 1;
122 qos.effective_only = 0;
124 attr.sec_qos = &qos;
127 return dcerpc_lsa_OpenPolicy2(h,
128 mem_ctx,
129 srv_name_slash,
130 &attr,
131 des_access,
132 pol,
133 result);
136 /** Open a LSA policy handle
138 * @param cli Handle on an initialised SMB connection
141 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
142 TALLOC_CTX *mem_ctx, bool sec_qos,
143 uint32 des_access, struct policy_handle *pol)
145 NTSTATUS status;
146 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
148 status = dcerpc_lsa_open_policy2(cli->binding_handle,
149 mem_ctx,
150 cli->srv_name_slash,
151 sec_qos,
152 des_access,
153 pol,
154 &result);
155 if (!NT_STATUS_IS_OK(status)) {
156 return status;
159 return result;
162 /* Lookup a list of sids
164 * internal version withOUT memory allocation of the target arrays.
165 * this assumes sufficiently sized arrays to store domains, names and types. */
167 static NTSTATUS dcerpc_lsa_lookup_sids_noalloc(struct dcerpc_binding_handle *h,
168 TALLOC_CTX *mem_ctx,
169 struct policy_handle *pol,
170 int num_sids,
171 const struct dom_sid *sids,
172 char **domains,
173 char **names,
174 enum lsa_SidType *types,
175 bool use_lookupsids3,
176 NTSTATUS *presult)
178 NTSTATUS status = NT_STATUS_OK;
179 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
180 struct lsa_SidArray sid_array;
181 struct lsa_RefDomainList *ref_domains = NULL;
182 struct lsa_TransNameArray lsa_names;
183 enum lsa_LookupNamesLevel level = LSA_LOOKUP_NAMES_ALL;
184 uint32_t count = 0;
185 int i;
187 ZERO_STRUCT(lsa_names);
189 sid_array.num_sids = num_sids;
190 sid_array.sids = talloc_array(mem_ctx, struct lsa_SidPtr, num_sids);
191 if (sid_array.sids == NULL) {
192 return NT_STATUS_NO_MEMORY;
195 for (i = 0; i<num_sids; i++) {
196 sid_array.sids[i].sid = dom_sid_dup(mem_ctx, &sids[i]);
197 if (!sid_array.sids[i].sid) {
198 return NT_STATUS_NO_MEMORY;
202 if (use_lookupsids3) {
203 struct lsa_TransNameArray2 lsa_names2;
204 uint32_t n;
206 ZERO_STRUCT(lsa_names2);
208 status = dcerpc_lsa_LookupSids3(h,
209 mem_ctx,
210 &sid_array,
211 &ref_domains,
212 &lsa_names2,
213 level,
214 &count,
215 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES,
216 LSA_CLIENT_REVISION_2,
217 &result);
218 if (!NT_STATUS_IS_OK(status)) {
219 return status;
222 if(!NT_STATUS_IS_ERR(result)) {
223 lsa_names.count = lsa_names2.count;
224 lsa_names.names = talloc_array(mem_ctx,
225 struct lsa_TranslatedName,
226 lsa_names.count);
227 if (lsa_names.names == NULL) {
228 return NT_STATUS_NO_MEMORY;
230 for (n=0; n < lsa_names.count; n++) {
231 lsa_names.names[n].sid_type = lsa_names2.names[n].sid_type;
232 lsa_names.names[n].name = lsa_names2.names[n].name;
233 lsa_names.names[n].sid_index = lsa_names2.names[n].sid_index;
237 } else {
238 status = dcerpc_lsa_LookupSids(h,
239 mem_ctx,
240 pol,
241 &sid_array,
242 &ref_domains,
243 &lsa_names,
244 level,
245 &count,
246 &result);
249 DEBUG(10, ("LSA_LOOKUPSIDS returned status: '%s', result: '%s', "
250 "mapped count = %d'\n",
251 nt_errstr(status), nt_errstr(result), count));
253 if (!NT_STATUS_IS_OK(status)) {
254 return status;
257 if (!NT_STATUS_IS_OK(result) &&
258 !NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
259 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
261 *presult = result;
262 return status;
265 /* Return output parameters */
266 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
267 (count == 0))
269 for (i = 0; i < num_sids; i++) {
270 (names)[i] = NULL;
271 (domains)[i] = NULL;
272 (types)[i] = SID_NAME_UNKNOWN;
274 *presult = NT_STATUS_NONE_MAPPED;
275 return status;
278 for (i = 0; i < num_sids; i++) {
279 const char *name, *dom_name;
280 uint32_t dom_idx = lsa_names.names[i].sid_index;
282 /* Translate optimised name through domain index array */
284 if (dom_idx != 0xffffffff) {
286 dom_name = ref_domains->domains[dom_idx].name.string;
287 name = lsa_names.names[i].name.string;
289 if (name) {
290 (names)[i] = talloc_strdup(names, name);
291 if ((names)[i] == NULL) {
292 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
293 *presult = NT_STATUS_UNSUCCESSFUL;
294 return status;
296 } else {
297 (names)[i] = NULL;
299 domains[i] = talloc_strdup(domains,
300 dom_name ? dom_name : "");
301 (types)[i] = lsa_names.names[i].sid_type;
302 if (((domains)[i] == NULL)) {
303 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
304 *presult = NT_STATUS_UNSUCCESSFUL;
305 return status;
308 } else {
309 (names)[i] = NULL;
310 (domains)[i] = NULL;
311 (types)[i] = SID_NAME_UNKNOWN;
315 *presult = NT_STATUS_OK;
316 return status;
319 /* Lookup a list of sids
321 * do it the right way: there is a limit (of 20480 for w2k3) entries
322 * returned by this call. when the sids list contains more entries,
323 * empty lists are returned. This version of lsa_lookup_sids passes
324 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
326 /* This constant defines the limit of how many sids to look up
327 * in one call (maximum). the limit from the server side is
328 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
329 #define LOOKUP_SIDS_HUNK_SIZE 1000
331 static NTSTATUS dcerpc_lsa_lookup_sids_generic(struct dcerpc_binding_handle *h,
332 TALLOC_CTX *mem_ctx,
333 struct policy_handle *pol,
334 int num_sids,
335 const struct dom_sid *sids,
336 char ***pdomains,
337 char ***pnames,
338 enum lsa_SidType **ptypes,
339 bool use_lookupsids3,
340 NTSTATUS *presult)
342 NTSTATUS status = NT_STATUS_OK;
343 NTSTATUS result = NT_STATUS_OK;
344 int sids_left = 0;
345 int sids_processed = 0;
346 const struct dom_sid *hunk_sids = sids;
347 char **hunk_domains;
348 char **hunk_names;
349 enum lsa_SidType *hunk_types;
350 char **domains = NULL;
351 char **names = NULL;
352 enum lsa_SidType *types = NULL;
353 bool have_mapped = false;
354 bool have_unmapped = false;
356 if (num_sids) {
357 if (!(domains = talloc_array(mem_ctx, char *, num_sids))) {
358 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
359 status = NT_STATUS_NO_MEMORY;
360 goto fail;
363 if (!(names = talloc_array(mem_ctx, char *, num_sids))) {
364 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
365 status = NT_STATUS_NO_MEMORY;
366 goto fail;
369 if (!(types = talloc_array(mem_ctx, enum lsa_SidType, num_sids))) {
370 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
371 status = NT_STATUS_NO_MEMORY;
372 goto fail;
376 sids_left = num_sids;
377 hunk_domains = domains;
378 hunk_names = names;
379 hunk_types = types;
381 while (sids_left > 0) {
382 int hunk_num_sids;
383 NTSTATUS hunk_result = NT_STATUS_UNSUCCESSFUL;
385 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
386 ? LOOKUP_SIDS_HUNK_SIZE
387 : sids_left);
389 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
390 "%d -- %d of %d.\n",
391 sids_processed,
392 sids_processed + hunk_num_sids - 1,
393 num_sids));
395 status = dcerpc_lsa_lookup_sids_noalloc(h,
396 mem_ctx,
397 pol,
398 hunk_num_sids,
399 hunk_sids,
400 hunk_domains,
401 hunk_names,
402 hunk_types,
403 use_lookupsids3,
404 &hunk_result);
405 if (!NT_STATUS_IS_OK(status)) {
406 goto fail;
409 if (!NT_STATUS_IS_OK(hunk_result) &&
410 !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
411 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
413 /* An actual error occured */
414 *presult = hunk_result;
415 goto fail;
418 if (NT_STATUS_IS_OK(hunk_result)) {
419 have_mapped = true;
421 if (NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)) {
422 have_unmapped = true;
424 if (NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED)) {
425 int i;
426 for (i=0; i<hunk_num_sids; i++) {
427 if (hunk_types[i] == SID_NAME_UNKNOWN) {
428 have_unmapped = true;
429 } else {
430 have_mapped = true;
435 sids_left -= hunk_num_sids;
436 sids_processed += hunk_num_sids; /* only used in DEBUG */
437 hunk_sids += hunk_num_sids;
438 hunk_domains += hunk_num_sids;
439 hunk_names += hunk_num_sids;
440 hunk_types += hunk_num_sids;
443 *pdomains = domains;
444 *pnames = names;
445 *ptypes = types;
447 if (!have_mapped) {
448 result = NT_STATUS_NONE_MAPPED;
450 if (have_unmapped) {
451 result = STATUS_SOME_UNMAPPED;
453 *presult = result;
455 return status;
457 fail:
458 TALLOC_FREE(domains);
459 TALLOC_FREE(names);
460 TALLOC_FREE(types);
462 return status;
465 NTSTATUS dcerpc_lsa_lookup_sids(struct dcerpc_binding_handle *h,
466 TALLOC_CTX *mem_ctx,
467 struct policy_handle *pol,
468 int num_sids,
469 const struct dom_sid *sids,
470 char ***pdomains,
471 char ***pnames,
472 enum lsa_SidType **ptypes,
473 NTSTATUS *result)
475 return dcerpc_lsa_lookup_sids_generic(h,
476 mem_ctx,
477 pol,
478 num_sids,
479 sids,
480 pdomains,
481 pnames,
482 ptypes,
483 false,
484 result);
487 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
488 TALLOC_CTX *mem_ctx,
489 struct policy_handle *pol,
490 int num_sids,
491 const struct dom_sid *sids,
492 char ***pdomains,
493 char ***pnames,
494 enum lsa_SidType **ptypes)
496 NTSTATUS status;
497 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
499 status = dcerpc_lsa_lookup_sids_generic(cli->binding_handle,
500 mem_ctx,
501 pol,
502 num_sids,
503 sids,
504 pdomains,
505 pnames,
506 ptypes,
507 false,
508 &result);
509 if (!NT_STATUS_IS_OK(status)) {
510 return status;
513 return result;
516 NTSTATUS dcerpc_lsa_lookup_sids3(struct dcerpc_binding_handle *h,
517 TALLOC_CTX *mem_ctx,
518 struct policy_handle *pol,
519 int num_sids,
520 const struct dom_sid *sids,
521 char ***pdomains,
522 char ***pnames,
523 enum lsa_SidType **ptypes,
524 NTSTATUS *result)
526 return dcerpc_lsa_lookup_sids_generic(h,
527 mem_ctx,
528 pol,
529 num_sids,
530 sids,
531 pdomains,
532 pnames,
533 ptypes,
534 true,
535 result);
538 NTSTATUS rpccli_lsa_lookup_sids3(struct rpc_pipe_client *cli,
539 TALLOC_CTX *mem_ctx,
540 struct policy_handle *pol,
541 int num_sids,
542 const struct dom_sid *sids,
543 char ***pdomains,
544 char ***pnames,
545 enum lsa_SidType **ptypes)
547 NTSTATUS status;
548 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
550 status = dcerpc_lsa_lookup_sids_generic(cli->binding_handle,
551 mem_ctx,
552 pol,
553 num_sids,
554 sids,
555 pdomains,
556 pnames,
557 ptypes,
558 true,
559 &result);
560 if (!NT_STATUS_IS_OK(status)) {
561 return status;
564 return result;
567 /** Lookup a list of names */
569 static NTSTATUS dcerpc_lsa_lookup_names_generic(struct dcerpc_binding_handle *h,
570 TALLOC_CTX *mem_ctx,
571 struct policy_handle *pol,
572 uint32_t num_names,
573 const char **names,
574 const char ***dom_names,
575 enum lsa_LookupNamesLevel level,
576 struct dom_sid **sids,
577 enum lsa_SidType **types,
578 bool use_lookupnames4,
579 NTSTATUS *presult)
581 NTSTATUS status;
582 struct lsa_String *lsa_names = NULL;
583 struct lsa_RefDomainList *domains = NULL;
584 struct lsa_TransSidArray sid_array;
585 struct lsa_TransSidArray3 sid_array3;
586 uint32_t count = 0;
587 uint32_t i;
589 ZERO_STRUCT(sid_array);
590 ZERO_STRUCT(sid_array3);
592 lsa_names = talloc_array(mem_ctx, struct lsa_String, num_names);
593 if (lsa_names == NULL) {
594 return NT_STATUS_NO_MEMORY;
597 for (i = 0; i < num_names; i++) {
598 init_lsa_String(&lsa_names[i], names[i]);
601 if (use_lookupnames4) {
602 status = dcerpc_lsa_LookupNames4(h,
603 mem_ctx,
604 num_names,
605 lsa_names,
606 &domains,
607 &sid_array3,
608 level,
609 &count,
610 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES,
611 LSA_CLIENT_REVISION_2,
612 presult);
613 } else {
614 status = dcerpc_lsa_LookupNames(h,
615 mem_ctx,
616 pol,
617 num_names,
618 lsa_names,
619 &domains,
620 &sid_array,
621 level,
622 &count,
623 presult);
625 if (!NT_STATUS_IS_OK(status)) {
626 goto done;
629 if (!NT_STATUS_IS_OK(*presult) &&
630 !NT_STATUS_EQUAL(*presult, STATUS_SOME_UNMAPPED)) {
631 /* An actual error occured */
632 goto done;
635 /* Return output parameters */
636 if (count == 0) {
637 *presult = NT_STATUS_NONE_MAPPED;
638 goto done;
641 if (num_names) {
642 if (!((*sids = talloc_array(mem_ctx, struct dom_sid, num_names)))) {
643 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
644 *presult = NT_STATUS_NO_MEMORY;
645 goto done;
648 if (!((*types = talloc_array(mem_ctx, enum lsa_SidType, num_names)))) {
649 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
650 *presult = NT_STATUS_NO_MEMORY;
651 goto done;
654 if (dom_names != NULL) {
655 *dom_names = talloc_array(mem_ctx, const char *, num_names);
656 if (*dom_names == NULL) {
657 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
658 *presult = NT_STATUS_NO_MEMORY;
659 goto done;
662 } else {
663 *sids = NULL;
664 *types = NULL;
665 if (dom_names != NULL) {
666 *dom_names = NULL;
670 for (i = 0; i < num_names; i++) {
671 uint32_t dom_idx;
672 struct dom_sid *sid = &(*sids)[i];
674 if (use_lookupnames4) {
675 dom_idx = sid_array3.sids[i].sid_index;
676 (*types)[i] = sid_array3.sids[i].sid_type;
677 } else {
678 dom_idx = sid_array.sids[i].sid_index;
679 (*types)[i] = sid_array.sids[i].sid_type;
682 /* Translate optimised sid through domain index array */
684 if (dom_idx == 0xffffffff) {
685 /* Nothing to do, this is unknown */
686 ZERO_STRUCTP(sid);
687 (*types)[i] = SID_NAME_UNKNOWN;
688 continue;
691 if (use_lookupnames4) {
692 sid_copy(sid, sid_array3.sids[i].sid);
693 } else {
694 sid_copy(sid, domains->domains[dom_idx].sid);
696 if (sid_array.sids[i].rid != 0xffffffff) {
697 sid_append_rid(sid, sid_array.sids[i].rid);
701 if (dom_names == NULL) {
702 continue;
705 (*dom_names)[i] = domains->domains[dom_idx].name.string;
708 done:
709 return status;
712 NTSTATUS dcerpc_lsa_lookup_names(struct dcerpc_binding_handle *h,
713 TALLOC_CTX *mem_ctx,
714 struct policy_handle *pol,
715 uint32_t num_names,
716 const char **names,
717 const char ***dom_names,
718 enum lsa_LookupNamesLevel level,
719 struct dom_sid **sids,
720 enum lsa_SidType **types,
721 NTSTATUS *result)
723 return dcerpc_lsa_lookup_names_generic(h,
724 mem_ctx,
725 pol,
726 num_names,
727 names,
728 dom_names,
729 level,
730 sids,
731 types,
732 false,
733 result);
736 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
737 TALLOC_CTX *mem_ctx,
738 struct policy_handle *pol,
739 int num_names,
740 const char **names,
741 const char ***dom_names,
742 int level,
743 struct dom_sid **sids,
744 enum lsa_SidType **types)
746 NTSTATUS status;
747 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
749 status = dcerpc_lsa_lookup_names(cli->binding_handle,
750 mem_ctx,
751 pol,
752 num_names,
753 names,
754 dom_names,
755 level,
756 sids,
757 types,
758 &result);
759 if (!NT_STATUS_IS_OK(status)) {
760 return status;
763 return result;
766 NTSTATUS dcerpc_lsa_lookup_names4(struct dcerpc_binding_handle *h,
767 TALLOC_CTX *mem_ctx,
768 struct policy_handle *pol,
769 uint32_t num_names,
770 const char **names,
771 const char ***dom_names,
772 enum lsa_LookupNamesLevel level,
773 struct dom_sid **sids,
774 enum lsa_SidType **types,
775 NTSTATUS *result)
777 return dcerpc_lsa_lookup_names_generic(h,
778 mem_ctx,
779 pol,
780 num_names,
781 names,
782 dom_names,
783 level,
784 sids,
785 types,
786 true,
787 result);
790 NTSTATUS rpccli_lsa_lookup_names4(struct rpc_pipe_client *cli,
791 TALLOC_CTX *mem_ctx,
792 struct policy_handle *pol,
793 int num_names,
794 const char **names,
795 const char ***dom_names,
796 int level,
797 struct dom_sid **sids,
798 enum lsa_SidType **types)
800 NTSTATUS status;
801 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
803 status = dcerpc_lsa_lookup_names4(cli->binding_handle,
804 mem_ctx,
805 pol,
806 num_names,
807 names,
808 dom_names,
809 level,
810 sids,
811 types,
812 &result);
813 if (!NT_STATUS_IS_OK(status)) {
814 return status;
817 return result;