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 "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"
33 /** @defgroup lsa LSA - Local Security Architecture
42 * RPC client routines for the LSA RPC pipe. LSA means "local
43 * security authority", which is half of a password database.
46 NTSTATUS
dcerpc_lsa_open_policy(struct dcerpc_binding_handle
*h
,
50 struct policy_handle
*pol
,
53 struct lsa_ObjectAttribute attr
= { .len
= 0x18, };
54 struct lsa_QosInfo qos
;
55 uint16_t system_name
= '\\';
59 qos
.impersonation_level
= 2;
61 qos
.effective_only
= 0;
66 return dcerpc_lsa_OpenPolicy(h
,
75 /** Open a LSA policy handle
77 * @param cli Handle on an initialised SMB connection */
79 NTSTATUS
rpccli_lsa_open_policy(struct rpc_pipe_client
*cli
,
81 bool sec_qos
, uint32_t des_access
,
82 struct policy_handle
*pol
)
85 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
87 status
= dcerpc_lsa_open_policy(cli
->binding_handle
,
93 if (!NT_STATUS_IS_OK(status
)) {
100 NTSTATUS
dcerpc_lsa_open_policy2(struct dcerpc_binding_handle
*h
,
102 const char *srv_name_slash
,
105 struct policy_handle
*pol
,
108 struct lsa_ObjectAttribute attr
= { .len
= 0x18, };
109 struct lsa_QosInfo qos
;
113 qos
.impersonation_level
= 2;
114 qos
.context_mode
= 1;
115 qos
.effective_only
= 0;
120 return dcerpc_lsa_OpenPolicy2(h
,
129 NTSTATUS
dcerpc_lsa_open_policy3(struct dcerpc_binding_handle
*h
,
131 const char *srv_name_slash
,
134 uint32_t *out_version
,
135 union lsa_revision_info
*out_revision_info
,
136 struct policy_handle
*pol
,
139 struct lsa_ObjectAttribute attr
= { .len
= 0x18, };
140 struct lsa_QosInfo qos
;
141 union lsa_revision_info in_revision_info
= {
146 uint32_t in_version
= 1;
150 qos
.impersonation_level
= 2;
151 qos
.context_mode
= 1;
152 qos
.effective_only
= 0;
157 return dcerpc_lsa_OpenPolicy3(h
,
170 NTSTATUS
dcerpc_lsa_open_policy_fallback(struct dcerpc_binding_handle
*h
,
172 const char *srv_name_slash
,
174 uint32_t desired_access
,
175 uint32_t *out_version
,
176 union lsa_revision_info
*out_revision_info
,
177 struct policy_handle
*pol
,
182 status
= dcerpc_lsa_open_policy3(h
,
191 if (NT_STATUS_EQUAL(status
, NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE
)) {
193 *out_revision_info
= (union lsa_revision_info
) {
199 status
= dcerpc_lsa_open_policy2(h
,
211 /* Lookup a list of sids
213 * internal version withOUT memory allocation of the target arrays.
214 * this assumes sufficiently sized arrays to store domains, names and types. */
216 static NTSTATUS
dcerpc_lsa_lookup_sids_noalloc(struct dcerpc_binding_handle
*h
,
218 TALLOC_CTX
*domains_ctx
,
219 TALLOC_CTX
*names_ctx
,
220 struct policy_handle
*pol
,
222 const struct dom_sid
*sids
,
223 enum lsa_LookupNamesLevel level
,
226 enum lsa_SidType
*types
,
227 bool use_lookupsids3
,
231 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
232 struct lsa_SidArray sid_array
;
233 struct lsa_RefDomainList
*ref_domains
= NULL
;
234 struct lsa_TransNameArray lsa_names
= { .count
= 0, };
238 sid_array
.num_sids
= num_sids
;
239 sid_array
.sids
= talloc_array(mem_ctx
, struct lsa_SidPtr
, num_sids
);
240 if (sid_array
.sids
== NULL
) {
241 return NT_STATUS_NO_MEMORY
;
244 for (i
= 0; i
<num_sids
; i
++) {
245 sid_array
.sids
[i
].sid
= dom_sid_dup(mem_ctx
, &sids
[i
]);
246 if (!sid_array
.sids
[i
].sid
) {
247 return NT_STATUS_NO_MEMORY
;
251 if (use_lookupsids3
) {
252 struct lsa_TransNameArray2 lsa_names2
;
255 ZERO_STRUCT(lsa_names2
);
257 status
= dcerpc_lsa_LookupSids3(h
,
264 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
,
265 LSA_CLIENT_REVISION_2
,
267 if (!NT_STATUS_IS_OK(status
)) {
271 if (!NT_STATUS_LOOKUP_ERR(result
)) {
272 lsa_names
.count
= lsa_names2
.count
;
273 lsa_names
.names
= talloc_array(mem_ctx
,
274 struct lsa_TranslatedName
,
276 if (lsa_names
.names
== NULL
) {
277 return NT_STATUS_NO_MEMORY
;
279 for (n
=0; n
< lsa_names
.count
; n
++) {
280 lsa_names
.names
[n
].sid_type
= lsa_names2
.names
[n
].sid_type
;
281 lsa_names
.names
[n
].name
= lsa_names2
.names
[n
].name
;
282 lsa_names
.names
[n
].sid_index
= lsa_names2
.names
[n
].sid_index
;
287 status
= dcerpc_lsa_LookupSids(h
,
298 DEBUG(10, ("LSA_LOOKUPSIDS returned status: '%s', result: '%s', "
299 "mapped count = %d'\n",
300 nt_errstr(status
), nt_errstr(result
), count
));
302 if (!NT_STATUS_IS_OK(status
)) {
306 if (NT_STATUS_LOOKUP_ERR(result
)) {
311 /* Return output parameters */
312 if (NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) ||
315 for (i
= 0; i
< num_sids
; i
++) {
318 (types
)[i
] = SID_NAME_UNKNOWN
;
320 *presult
= NT_STATUS_NONE_MAPPED
;
324 for (i
= 0; i
< num_sids
; i
++) {
325 const char *name
, *dom_name
;
328 if (i
>= lsa_names
.count
) {
329 *presult
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
333 dom_idx
= lsa_names
.names
[i
].sid_index
;
335 /* Translate optimised name through domain index array */
337 if (dom_idx
!= 0xffffffff) {
338 if (ref_domains
== NULL
) {
339 *presult
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
342 if (dom_idx
>= ref_domains
->count
) {
343 *presult
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
347 dom_name
= ref_domains
->domains
[dom_idx
].name
.string
;
348 name
= lsa_names
.names
[i
].name
.string
;
351 (names
)[i
] = talloc_strdup(names_ctx
, name
);
352 if ((names
)[i
] == NULL
) {
353 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
354 *presult
= NT_STATUS_UNSUCCESSFUL
;
360 domains
[i
] = talloc_strdup(domains_ctx
,
361 dom_name
? dom_name
: "");
362 (types
)[i
] = lsa_names
.names
[i
].sid_type
;
363 if ((domains
)[i
] == NULL
) {
364 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
365 *presult
= NT_STATUS_UNSUCCESSFUL
;
372 (types
)[i
] = SID_NAME_UNKNOWN
;
376 *presult
= NT_STATUS_OK
;
380 /* Lookup a list of sids
382 * do it the right way: there is a limit (of 20480 for w2k3) entries
383 * returned by this call. when the sids list contains more entries,
384 * empty lists are returned. This version of lsa_lookup_sids passes
385 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
387 /* This constant defines the limit of how many sids to look up
388 * in one call (maximum). the limit from the server side is
389 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
390 #define LOOKUP_SIDS_HUNK_SIZE 1000
392 NTSTATUS
dcerpc_lsa_lookup_sids_generic(struct dcerpc_binding_handle
*h
,
394 struct policy_handle
*pol
,
396 const struct dom_sid
*sids
,
397 enum lsa_LookupNamesLevel level
,
400 enum lsa_SidType
**ptypes
,
401 bool use_lookupsids3
,
404 NTSTATUS status
= NT_STATUS_OK
;
405 NTSTATUS result
= NT_STATUS_OK
;
407 int sids_processed
= 0;
408 const struct dom_sid
*hunk_sids
= sids
;
411 enum lsa_SidType
*hunk_types
;
412 char **domains
= NULL
;
414 enum lsa_SidType
*types
= NULL
;
415 bool have_mapped
= false;
416 bool have_unmapped
= false;
419 domains
= talloc_zero_array(mem_ctx
, char *, num_sids
);
420 if (domains
== NULL
) {
421 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
422 status
= NT_STATUS_NO_MEMORY
;
426 names
= talloc_zero_array(mem_ctx
, char *, num_sids
);
428 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
429 status
= NT_STATUS_NO_MEMORY
;
433 types
= talloc_zero_array(mem_ctx
, enum lsa_SidType
, num_sids
);
435 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
436 status
= NT_STATUS_NO_MEMORY
;
441 sids_left
= num_sids
;
442 hunk_domains
= domains
;
446 while (sids_left
> 0) {
448 NTSTATUS hunk_result
= NT_STATUS_UNSUCCESSFUL
;
450 hunk_num_sids
= ((sids_left
> LOOKUP_SIDS_HUNK_SIZE
)
451 ? LOOKUP_SIDS_HUNK_SIZE
454 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
457 sids_processed
+ hunk_num_sids
- 1,
460 status
= dcerpc_lsa_lookup_sids_noalloc(h
,
462 (TALLOC_CTX
*)domains
,
473 if (!NT_STATUS_IS_OK(status
)) {
477 if (!NT_STATUS_IS_OK(hunk_result
) &&
478 !NT_STATUS_EQUAL(hunk_result
, STATUS_SOME_UNMAPPED
) &&
479 !NT_STATUS_EQUAL(hunk_result
, NT_STATUS_NONE_MAPPED
))
481 /* An actual error occurred */
482 *presult
= hunk_result
;
486 if (NT_STATUS_IS_OK(hunk_result
)) {
489 if (NT_STATUS_EQUAL(hunk_result
, NT_STATUS_NONE_MAPPED
)) {
490 have_unmapped
= true;
492 if (NT_STATUS_EQUAL(hunk_result
, STATUS_SOME_UNMAPPED
)) {
494 for (i
=0; i
<hunk_num_sids
; i
++) {
495 if (hunk_types
[i
] == SID_NAME_UNKNOWN
) {
496 have_unmapped
= true;
503 sids_left
-= hunk_num_sids
;
504 sids_processed
+= hunk_num_sids
;
505 hunk_sids
+= hunk_num_sids
;
506 hunk_domains
+= hunk_num_sids
;
507 hunk_names
+= hunk_num_sids
;
508 hunk_types
+= hunk_num_sids
;
516 result
= NT_STATUS_NONE_MAPPED
;
519 result
= STATUS_SOME_UNMAPPED
;
526 TALLOC_FREE(domains
);
533 NTSTATUS
dcerpc_lsa_lookup_sids(struct dcerpc_binding_handle
*h
,
535 struct policy_handle
*pol
,
537 const struct dom_sid
*sids
,
540 enum lsa_SidType
**ptypes
,
543 enum lsa_LookupNamesLevel level
= LSA_LOOKUP_NAMES_ALL
;
544 return dcerpc_lsa_lookup_sids_generic(h
,
557 NTSTATUS
rpccli_lsa_lookup_sids(struct rpc_pipe_client
*cli
,
559 struct policy_handle
*pol
,
561 const struct dom_sid
*sids
,
564 enum lsa_SidType
**ptypes
)
567 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
568 enum lsa_LookupNamesLevel level
= LSA_LOOKUP_NAMES_ALL
;
570 status
= dcerpc_lsa_lookup_sids_generic(cli
->binding_handle
,
581 if (!NT_STATUS_IS_OK(status
)) {
588 NTSTATUS
dcerpc_lsa_lookup_sids3(struct dcerpc_binding_handle
*h
,
590 struct policy_handle
*pol
,
592 const struct dom_sid
*sids
,
595 enum lsa_SidType
**ptypes
,
598 enum lsa_LookupNamesLevel level
= LSA_LOOKUP_NAMES_ALL
;
599 return dcerpc_lsa_lookup_sids_generic(h
,
612 /** Lookup a list of names */
614 NTSTATUS
dcerpc_lsa_lookup_names_generic(struct dcerpc_binding_handle
*h
,
616 struct policy_handle
*pol
,
619 const char ***dom_names
,
620 enum lsa_LookupNamesLevel level
,
621 struct dom_sid
**sids
,
622 enum lsa_SidType
**types
,
623 bool use_lookupnames4
,
627 struct lsa_String
*lsa_names
= NULL
;
628 struct lsa_RefDomainList
*domains
= NULL
;
629 struct lsa_TransSidArray sid_array
= { .count
= 0, };
630 struct lsa_TransSidArray3 sid_array3
= { .count
= 0, };
634 lsa_names
= talloc_array(mem_ctx
, struct lsa_String
, num_names
);
635 if (lsa_names
== NULL
) {
636 return NT_STATUS_NO_MEMORY
;
639 for (i
= 0; i
< num_names
; i
++) {
640 init_lsa_String(&lsa_names
[i
], names
[i
]);
643 if (use_lookupnames4
) {
644 status
= dcerpc_lsa_LookupNames4(h
,
652 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
,
653 LSA_CLIENT_REVISION_2
,
656 status
= dcerpc_lsa_LookupNames(h
,
667 if (!NT_STATUS_IS_OK(status
)) {
671 if (!NT_STATUS_IS_OK(*presult
) &&
672 !NT_STATUS_EQUAL(*presult
, STATUS_SOME_UNMAPPED
)) {
673 /* An actual error occurred */
677 /* Return output parameters */
679 *presult
= NT_STATUS_NONE_MAPPED
;
684 *sids
= talloc_zero_array(mem_ctx
, struct dom_sid
, num_names
);
686 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
687 *presult
= NT_STATUS_NO_MEMORY
;
691 *types
= talloc_zero_array(mem_ctx
, enum lsa_SidType
, num_names
);
692 if (*types
== NULL
) {
693 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
694 *presult
= NT_STATUS_NO_MEMORY
;
698 if (dom_names
!= NULL
) {
699 *dom_names
= talloc_zero_array(mem_ctx
, const char *, num_names
);
700 if (*dom_names
== NULL
) {
701 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
702 *presult
= NT_STATUS_NO_MEMORY
;
709 if (dom_names
!= NULL
) {
714 for (i
= 0; i
< num_names
; i
++) {
716 struct dom_sid
*sid
= &(*sids
)[i
];
718 if (use_lookupnames4
) {
719 if (i
>= sid_array3
.count
) {
720 *presult
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
724 dom_idx
= sid_array3
.sids
[i
].sid_index
;
725 (*types
)[i
] = sid_array3
.sids
[i
].sid_type
;
727 if (i
>= sid_array
.count
) {
728 *presult
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
732 dom_idx
= sid_array
.sids
[i
].sid_index
;
733 (*types
)[i
] = sid_array
.sids
[i
].sid_type
;
736 /* Translate optimised sid through domain index array */
738 if (dom_idx
== 0xffffffff) {
739 /* Nothing to do, this is unknown */
741 (*types
)[i
] = SID_NAME_UNKNOWN
;
744 if (domains
== NULL
) {
745 *presult
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
748 if (dom_idx
>= domains
->count
) {
749 *presult
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
753 if (use_lookupnames4
) {
754 if (sid_array3
.sids
[i
].sid
!= NULL
) {
755 sid_copy(sid
, sid_array3
.sids
[i
].sid
);
758 (*types
)[i
] = SID_NAME_UNKNOWN
;
761 if (domains
->domains
[dom_idx
].sid
!= NULL
) {
762 sid_copy(sid
, domains
->domains
[dom_idx
].sid
);
764 if (sid_array
.sids
[i
].rid
!= 0xffffffff) {
766 sid_array
.sids
[i
].rid
);
770 (*types
)[i
] = SID_NAME_UNKNOWN
;
774 if (dom_names
== NULL
) {
778 (*dom_names
)[i
] = domains
->domains
[dom_idx
].name
.string
;
785 NTSTATUS
dcerpc_lsa_lookup_names(struct dcerpc_binding_handle
*h
,
787 struct policy_handle
*pol
,
790 const char ***dom_names
,
791 enum lsa_LookupNamesLevel level
,
792 struct dom_sid
**sids
,
793 enum lsa_SidType
**types
,
796 return dcerpc_lsa_lookup_names_generic(h
,
809 NTSTATUS
rpccli_lsa_lookup_names(struct rpc_pipe_client
*cli
,
811 struct policy_handle
*pol
,
814 const char ***dom_names
,
816 struct dom_sid
**sids
,
817 enum lsa_SidType
**types
)
820 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
822 status
= dcerpc_lsa_lookup_names(cli
->binding_handle
,
832 if (!NT_STATUS_IS_OK(status
)) {
839 NTSTATUS
dcerpc_lsa_lookup_names4(struct dcerpc_binding_handle
*h
,
841 struct policy_handle
*pol
,
844 const char ***dom_names
,
845 enum lsa_LookupNamesLevel level
,
846 struct dom_sid
**sids
,
847 enum lsa_SidType
**types
,
850 return dcerpc_lsa_lookup_names_generic(h
,