vfs_ceph: fix strict_allocate_ftruncate()
[Samba.git] / source3 / rpc_client / cli_lsarpc.c
bloba093ee2572c9c36be86be3a17954ba9717b2f893
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"
31 #include "lsa.h"
33 /** @defgroup lsa LSA - Local Security Architecture
34 * @ingroup rpc_client
36 * @{
37 **/
39 /**
40 * @file cli_lsarpc.c
42 * RPC client routines for the LSA RPC pipe. LSA means "local
43 * security authority", which is half of a password database.
44 **/
46 NTSTATUS dcerpc_lsa_open_policy(struct dcerpc_binding_handle *h,
47 TALLOC_CTX *mem_ctx,
48 bool sec_qos,
49 uint32_t des_access,
50 struct policy_handle *pol,
51 NTSTATUS *result)
53 struct lsa_ObjectAttribute attr;
54 struct lsa_QosInfo qos;
55 uint16_t system_name = '\\';
57 ZERO_STRUCT(attr);
59 attr.len = 0x18;
61 if (sec_qos) {
62 qos.len = 0xc;
63 qos.impersonation_level = 2;
64 qos.context_mode = 1;
65 qos.effective_only = 0;
67 attr.sec_qos = &qos;
70 return dcerpc_lsa_OpenPolicy(h,
71 mem_ctx,
72 &system_name,
73 &attr,
74 des_access,
75 pol,
76 result);
79 /** Open a LSA policy handle
81 * @param cli Handle on an initialised SMB connection */
83 NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
84 TALLOC_CTX *mem_ctx,
85 bool sec_qos, uint32_t des_access,
86 struct policy_handle *pol)
88 NTSTATUS status;
89 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
91 status = dcerpc_lsa_open_policy(cli->binding_handle,
92 mem_ctx,
93 sec_qos,
94 des_access,
95 pol,
96 &result);
97 if (!NT_STATUS_IS_OK(status)) {
98 return status;
101 return result;
104 NTSTATUS dcerpc_lsa_open_policy2(struct dcerpc_binding_handle *h,
105 TALLOC_CTX *mem_ctx,
106 const char *srv_name_slash,
107 bool sec_qos,
108 uint32_t des_access,
109 struct policy_handle *pol,
110 NTSTATUS *result)
112 struct lsa_ObjectAttribute attr;
113 struct lsa_QosInfo qos;
115 ZERO_STRUCT(attr);
117 attr.len = 0x18;
119 if (sec_qos) {
120 qos.len = 0xc;
121 qos.impersonation_level = 2;
122 qos.context_mode = 1;
123 qos.effective_only = 0;
125 attr.sec_qos = &qos;
128 return dcerpc_lsa_OpenPolicy2(h,
129 mem_ctx,
130 srv_name_slash,
131 &attr,
132 des_access,
133 pol,
134 result);
137 /** Open a LSA policy handle
139 * @param cli Handle on an initialised SMB connection
142 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
143 TALLOC_CTX *mem_ctx, bool sec_qos,
144 uint32_t des_access, struct policy_handle *pol)
146 NTSTATUS status;
147 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
149 status = dcerpc_lsa_open_policy2(cli->binding_handle,
150 mem_ctx,
151 cli->srv_name_slash,
152 sec_qos,
153 des_access,
154 pol,
155 &result);
156 if (!NT_STATUS_IS_OK(status)) {
157 return status;
160 return result;
163 /* Lookup a list of sids
165 * internal version withOUT memory allocation of the target arrays.
166 * this assumes sufficiently sized arrays to store domains, names and types. */
168 static NTSTATUS dcerpc_lsa_lookup_sids_noalloc(struct dcerpc_binding_handle *h,
169 TALLOC_CTX *mem_ctx,
170 TALLOC_CTX *domains_ctx,
171 TALLOC_CTX *names_ctx,
172 struct policy_handle *pol,
173 int num_sids,
174 const struct dom_sid *sids,
175 enum lsa_LookupNamesLevel level,
176 char **domains,
177 char **names,
178 enum lsa_SidType *types,
179 bool use_lookupsids3,
180 NTSTATUS *presult)
182 NTSTATUS status = NT_STATUS_OK;
183 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
184 struct lsa_SidArray sid_array;
185 struct lsa_RefDomainList *ref_domains = NULL;
186 struct lsa_TransNameArray lsa_names;
187 uint32_t count = 0;
188 int i;
190 ZERO_STRUCT(lsa_names);
192 sid_array.num_sids = num_sids;
193 sid_array.sids = talloc_array(mem_ctx, struct lsa_SidPtr, num_sids);
194 if (sid_array.sids == NULL) {
195 return NT_STATUS_NO_MEMORY;
198 for (i = 0; i<num_sids; i++) {
199 sid_array.sids[i].sid = dom_sid_dup(mem_ctx, &sids[i]);
200 if (!sid_array.sids[i].sid) {
201 return NT_STATUS_NO_MEMORY;
205 if (use_lookupsids3) {
206 struct lsa_TransNameArray2 lsa_names2;
207 uint32_t n;
209 ZERO_STRUCT(lsa_names2);
211 status = dcerpc_lsa_LookupSids3(h,
212 mem_ctx,
213 &sid_array,
214 &ref_domains,
215 &lsa_names2,
216 level,
217 &count,
218 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES,
219 LSA_CLIENT_REVISION_2,
220 &result);
221 if (!NT_STATUS_IS_OK(status)) {
222 return status;
225 if (!NT_STATUS_LOOKUP_ERR(result)) {
226 lsa_names.count = lsa_names2.count;
227 lsa_names.names = talloc_array(mem_ctx,
228 struct lsa_TranslatedName,
229 lsa_names.count);
230 if (lsa_names.names == NULL) {
231 return NT_STATUS_NO_MEMORY;
233 for (n=0; n < lsa_names.count; n++) {
234 lsa_names.names[n].sid_type = lsa_names2.names[n].sid_type;
235 lsa_names.names[n].name = lsa_names2.names[n].name;
236 lsa_names.names[n].sid_index = lsa_names2.names[n].sid_index;
240 } else {
241 status = dcerpc_lsa_LookupSids(h,
242 mem_ctx,
243 pol,
244 &sid_array,
245 &ref_domains,
246 &lsa_names,
247 level,
248 &count,
249 &result);
252 DEBUG(10, ("LSA_LOOKUPSIDS returned status: '%s', result: '%s', "
253 "mapped count = %d'\n",
254 nt_errstr(status), nt_errstr(result), count));
256 if (!NT_STATUS_IS_OK(status)) {
257 return status;
260 if (NT_STATUS_LOOKUP_ERR(result)) {
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;
282 if (i >= lsa_names.count) {
283 *presult = NT_STATUS_INVALID_NETWORK_RESPONSE;
284 return status;
287 dom_idx = lsa_names.names[i].sid_index;
289 /* Translate optimised name through domain index array */
291 if (dom_idx != 0xffffffff) {
292 if (ref_domains == NULL) {
293 *presult = NT_STATUS_INVALID_NETWORK_RESPONSE;
294 return status;
296 if (dom_idx >= ref_domains->count) {
297 *presult = NT_STATUS_INVALID_NETWORK_RESPONSE;
298 return status;
301 dom_name = ref_domains->domains[dom_idx].name.string;
302 name = lsa_names.names[i].name.string;
304 if (name) {
305 (names)[i] = talloc_strdup(names_ctx, name);
306 if ((names)[i] == NULL) {
307 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
308 *presult = NT_STATUS_UNSUCCESSFUL;
309 return status;
311 } else {
312 (names)[i] = NULL;
314 domains[i] = talloc_strdup(domains_ctx,
315 dom_name ? dom_name : "");
316 (types)[i] = lsa_names.names[i].sid_type;
317 if ((domains)[i] == NULL) {
318 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
319 *presult = NT_STATUS_UNSUCCESSFUL;
320 return status;
323 } else {
324 (names)[i] = NULL;
325 (domains)[i] = NULL;
326 (types)[i] = SID_NAME_UNKNOWN;
330 *presult = NT_STATUS_OK;
331 return status;
334 /* Lookup a list of sids
336 * do it the right way: there is a limit (of 20480 for w2k3) entries
337 * returned by this call. when the sids list contains more entries,
338 * empty lists are returned. This version of lsa_lookup_sids passes
339 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
341 /* This constant defines the limit of how many sids to look up
342 * in one call (maximum). the limit from the server side is
343 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
344 #define LOOKUP_SIDS_HUNK_SIZE 1000
346 NTSTATUS dcerpc_lsa_lookup_sids_generic(struct dcerpc_binding_handle *h,
347 TALLOC_CTX *mem_ctx,
348 struct policy_handle *pol,
349 int num_sids,
350 const struct dom_sid *sids,
351 enum lsa_LookupNamesLevel level,
352 char ***pdomains,
353 char ***pnames,
354 enum lsa_SidType **ptypes,
355 bool use_lookupsids3,
356 NTSTATUS *presult)
358 NTSTATUS status = NT_STATUS_OK;
359 NTSTATUS result = NT_STATUS_OK;
360 int sids_left = 0;
361 int sids_processed = 0;
362 const struct dom_sid *hunk_sids = sids;
363 char **hunk_domains;
364 char **hunk_names;
365 enum lsa_SidType *hunk_types;
366 char **domains = NULL;
367 char **names = NULL;
368 enum lsa_SidType *types = NULL;
369 bool have_mapped = false;
370 bool have_unmapped = false;
372 if (num_sids) {
373 domains = talloc_zero_array(mem_ctx, char *, num_sids);
374 if (domains == NULL) {
375 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
376 status = NT_STATUS_NO_MEMORY;
377 goto fail;
380 names = talloc_zero_array(mem_ctx, char *, num_sids);
381 if (names == NULL) {
382 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
383 status = NT_STATUS_NO_MEMORY;
384 goto fail;
387 types = talloc_zero_array(mem_ctx, enum lsa_SidType, num_sids);
388 if (types == NULL) {
389 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
390 status = NT_STATUS_NO_MEMORY;
391 goto fail;
395 sids_left = num_sids;
396 hunk_domains = domains;
397 hunk_names = names;
398 hunk_types = types;
400 while (sids_left > 0) {
401 int hunk_num_sids;
402 NTSTATUS hunk_result = NT_STATUS_UNSUCCESSFUL;
404 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
405 ? LOOKUP_SIDS_HUNK_SIZE
406 : sids_left);
408 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
409 "%d -- %d of %d.\n",
410 sids_processed,
411 sids_processed + hunk_num_sids - 1,
412 num_sids));
414 status = dcerpc_lsa_lookup_sids_noalloc(h,
415 mem_ctx,
416 (TALLOC_CTX *)domains,
417 (TALLOC_CTX *)names,
418 pol,
419 hunk_num_sids,
420 hunk_sids,
421 level,
422 hunk_domains,
423 hunk_names,
424 hunk_types,
425 use_lookupsids3,
426 &hunk_result);
427 if (!NT_STATUS_IS_OK(status)) {
428 goto fail;
431 if (!NT_STATUS_IS_OK(hunk_result) &&
432 !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
433 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
435 /* An actual error occurred */
436 *presult = hunk_result;
437 goto fail;
440 if (NT_STATUS_IS_OK(hunk_result)) {
441 have_mapped = true;
443 if (NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)) {
444 have_unmapped = true;
446 if (NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED)) {
447 int i;
448 for (i=0; i<hunk_num_sids; i++) {
449 if (hunk_types[i] == SID_NAME_UNKNOWN) {
450 have_unmapped = true;
451 } else {
452 have_mapped = true;
457 sids_left -= hunk_num_sids;
458 sids_processed += hunk_num_sids;
459 hunk_sids += hunk_num_sids;
460 hunk_domains += hunk_num_sids;
461 hunk_names += hunk_num_sids;
462 hunk_types += hunk_num_sids;
465 *pdomains = domains;
466 *pnames = names;
467 *ptypes = types;
469 if (!have_mapped) {
470 result = NT_STATUS_NONE_MAPPED;
472 if (have_unmapped) {
473 result = STATUS_SOME_UNMAPPED;
475 *presult = result;
477 return status;
479 fail:
480 TALLOC_FREE(domains);
481 TALLOC_FREE(names);
482 TALLOC_FREE(types);
484 return status;
487 NTSTATUS dcerpc_lsa_lookup_sids(struct dcerpc_binding_handle *h,
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,
495 NTSTATUS *result)
497 enum lsa_LookupNamesLevel level = LSA_LOOKUP_NAMES_ALL;
498 return dcerpc_lsa_lookup_sids_generic(h,
499 mem_ctx,
500 pol,
501 num_sids,
502 sids,
503 level,
504 pdomains,
505 pnames,
506 ptypes,
507 false,
508 result);
511 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
512 TALLOC_CTX *mem_ctx,
513 struct policy_handle *pol,
514 int num_sids,
515 const struct dom_sid *sids,
516 char ***pdomains,
517 char ***pnames,
518 enum lsa_SidType **ptypes)
520 NTSTATUS status;
521 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
522 enum lsa_LookupNamesLevel level = LSA_LOOKUP_NAMES_ALL;
524 status = dcerpc_lsa_lookup_sids_generic(cli->binding_handle,
525 mem_ctx,
526 pol,
527 num_sids,
528 sids,
529 level,
530 pdomains,
531 pnames,
532 ptypes,
533 false,
534 &result);
535 if (!NT_STATUS_IS_OK(status)) {
536 return status;
539 return result;
542 NTSTATUS dcerpc_lsa_lookup_sids3(struct dcerpc_binding_handle *h,
543 TALLOC_CTX *mem_ctx,
544 struct policy_handle *pol,
545 int num_sids,
546 const struct dom_sid *sids,
547 char ***pdomains,
548 char ***pnames,
549 enum lsa_SidType **ptypes,
550 NTSTATUS *result)
552 enum lsa_LookupNamesLevel level = LSA_LOOKUP_NAMES_ALL;
553 return dcerpc_lsa_lookup_sids_generic(h,
554 mem_ctx,
555 pol,
556 num_sids,
557 sids,
558 level,
559 pdomains,
560 pnames,
561 ptypes,
562 true,
563 result);
566 /** Lookup a list of names */
568 NTSTATUS dcerpc_lsa_lookup_names_generic(struct dcerpc_binding_handle *h,
569 TALLOC_CTX *mem_ctx,
570 struct policy_handle *pol,
571 uint32_t num_names,
572 const char **names,
573 const char ***dom_names,
574 enum lsa_LookupNamesLevel level,
575 struct dom_sid **sids,
576 enum lsa_SidType **types,
577 bool use_lookupnames4,
578 NTSTATUS *presult)
580 NTSTATUS status;
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;
585 uint32_t count = 0;
586 uint32_t i;
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,
602 mem_ctx,
603 num_names,
604 lsa_names,
605 &domains,
606 &sid_array3,
607 level,
608 &count,
609 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES,
610 LSA_CLIENT_REVISION_2,
611 presult);
612 } else {
613 status = dcerpc_lsa_LookupNames(h,
614 mem_ctx,
615 pol,
616 num_names,
617 lsa_names,
618 &domains,
619 &sid_array,
620 level,
621 &count,
622 presult);
624 if (!NT_STATUS_IS_OK(status)) {
625 goto done;
628 if (!NT_STATUS_IS_OK(*presult) &&
629 !NT_STATUS_EQUAL(*presult, STATUS_SOME_UNMAPPED)) {
630 /* An actual error occurred */
631 goto done;
634 /* Return output parameters */
635 if (count == 0) {
636 *presult = NT_STATUS_NONE_MAPPED;
637 goto done;
640 if (num_names) {
641 *sids = talloc_zero_array(mem_ctx, struct dom_sid, num_names);
642 if (*sids == NULL) {
643 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
644 *presult = NT_STATUS_NO_MEMORY;
645 goto done;
648 *types = talloc_zero_array(mem_ctx, enum lsa_SidType, num_names);
649 if (*types == NULL) {
650 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
651 *presult = NT_STATUS_NO_MEMORY;
652 goto done;
655 if (dom_names != NULL) {
656 *dom_names = talloc_zero_array(mem_ctx, const char *, num_names);
657 if (*dom_names == NULL) {
658 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
659 *presult = NT_STATUS_NO_MEMORY;
660 goto done;
663 } else {
664 *sids = NULL;
665 *types = NULL;
666 if (dom_names != NULL) {
667 *dom_names = NULL;
671 for (i = 0; i < num_names; i++) {
672 uint32_t dom_idx;
673 struct dom_sid *sid = &(*sids)[i];
675 if (use_lookupnames4) {
676 if (i >= sid_array3.count) {
677 *presult = NT_STATUS_INVALID_NETWORK_RESPONSE;
678 goto done;
681 dom_idx = sid_array3.sids[i].sid_index;
682 (*types)[i] = sid_array3.sids[i].sid_type;
683 } else {
684 if (i >= sid_array.count) {
685 *presult = NT_STATUS_INVALID_NETWORK_RESPONSE;
686 goto done;
689 dom_idx = sid_array.sids[i].sid_index;
690 (*types)[i] = sid_array.sids[i].sid_type;
693 /* Translate optimised sid through domain index array */
695 if (dom_idx == 0xffffffff) {
696 /* Nothing to do, this is unknown */
697 ZERO_STRUCTP(sid);
698 (*types)[i] = SID_NAME_UNKNOWN;
699 continue;
701 if (domains == NULL) {
702 *presult = NT_STATUS_INVALID_NETWORK_RESPONSE;
703 goto done;
705 if (dom_idx >= domains->count) {
706 *presult = NT_STATUS_INVALID_NETWORK_RESPONSE;
707 goto done;
710 if (use_lookupnames4) {
711 sid_copy(sid, sid_array3.sids[i].sid);
712 } else {
713 sid_copy(sid, domains->domains[dom_idx].sid);
715 if (sid_array.sids[i].rid != 0xffffffff) {
716 sid_append_rid(sid, sid_array.sids[i].rid);
720 if (dom_names == NULL) {
721 continue;
724 (*dom_names)[i] = domains->domains[dom_idx].name.string;
727 done:
728 return status;
731 NTSTATUS dcerpc_lsa_lookup_names(struct dcerpc_binding_handle *h,
732 TALLOC_CTX *mem_ctx,
733 struct policy_handle *pol,
734 uint32_t num_names,
735 const char **names,
736 const char ***dom_names,
737 enum lsa_LookupNamesLevel level,
738 struct dom_sid **sids,
739 enum lsa_SidType **types,
740 NTSTATUS *result)
742 return dcerpc_lsa_lookup_names_generic(h,
743 mem_ctx,
744 pol,
745 num_names,
746 names,
747 dom_names,
748 level,
749 sids,
750 types,
751 false,
752 result);
755 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
756 TALLOC_CTX *mem_ctx,
757 struct policy_handle *pol,
758 int num_names,
759 const char **names,
760 const char ***dom_names,
761 int level,
762 struct dom_sid **sids,
763 enum lsa_SidType **types)
765 NTSTATUS status;
766 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
768 status = dcerpc_lsa_lookup_names(cli->binding_handle,
769 mem_ctx,
770 pol,
771 num_names,
772 names,
773 dom_names,
774 level,
775 sids,
776 types,
777 &result);
778 if (!NT_STATUS_IS_OK(status)) {
779 return status;
782 return result;
785 NTSTATUS dcerpc_lsa_lookup_names4(struct dcerpc_binding_handle *h,
786 TALLOC_CTX *mem_ctx,
787 struct policy_handle *pol,
788 uint32_t num_names,
789 const char **names,
790 const char ***dom_names,
791 enum lsa_LookupNamesLevel level,
792 struct dom_sid **sids,
793 enum lsa_SidType **types,
794 NTSTATUS *result)
796 return dcerpc_lsa_lookup_names_generic(h,
797 mem_ctx,
798 pol,
799 num_names,
800 names,
801 dom_names,
802 level,
803 sids,
804 types,
805 true,
806 result);