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"
32 /** @defgroup lsa LSA - Local Security Architecture
41 * RPC client routines for the LSA RPC pipe. LSA means "local
42 * security authority", which is half of a password database.
45 NTSTATUS
dcerpc_lsa_open_policy(struct dcerpc_binding_handle
*h
,
49 struct policy_handle
*pol
,
52 struct lsa_ObjectAttribute attr
;
53 struct lsa_QosInfo qos
;
54 uint16_t system_name
= '\\';
62 qos
.impersonation_level
= 2;
64 qos
.effective_only
= 0;
69 return dcerpc_lsa_OpenPolicy(h
,
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
,
84 bool sec_qos
, uint32 des_access
,
85 struct policy_handle
*pol
)
88 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
90 status
= dcerpc_lsa_open_policy(cli
->binding_handle
,
96 if (!NT_STATUS_IS_OK(status
)) {
103 NTSTATUS
dcerpc_lsa_open_policy2(struct dcerpc_binding_handle
*h
,
105 const char *srv_name_slash
,
108 struct policy_handle
*pol
,
111 struct lsa_ObjectAttribute attr
;
112 struct lsa_QosInfo qos
;
120 qos
.impersonation_level
= 2;
121 qos
.context_mode
= 1;
122 qos
.effective_only
= 0;
127 return dcerpc_lsa_OpenPolicy2(h
,
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
)
146 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
148 status
= dcerpc_lsa_open_policy2(cli
->binding_handle
,
155 if (!NT_STATUS_IS_OK(status
)) {
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
,
169 struct policy_handle
*pol
,
171 const struct dom_sid
*sids
,
174 enum lsa_SidType
*types
,
175 bool use_lookupsids3
,
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
;
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
;
206 ZERO_STRUCT(lsa_names2
);
208 status
= dcerpc_lsa_LookupSids3(h
,
215 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
,
216 LSA_CLIENT_REVISION_2
,
218 if (!NT_STATUS_IS_OK(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
,
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
;
238 status
= dcerpc_lsa_LookupSids(h
,
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
)) {
257 if (!NT_STATUS_IS_OK(result
) &&
258 !NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) &&
259 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
265 /* Return output parameters */
266 if (NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) ||
269 for (i
= 0; i
< num_sids
; i
++) {
272 (types
)[i
] = SID_NAME_UNKNOWN
;
274 *presult
= NT_STATUS_NONE_MAPPED
;
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
;
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
;
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
;
311 (types
)[i
] = SID_NAME_UNKNOWN
;
315 *presult
= NT_STATUS_OK
;
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
,
333 struct policy_handle
*pol
,
335 const struct dom_sid
*sids
,
338 enum lsa_SidType
**ptypes
,
339 bool use_lookupsids3
,
342 NTSTATUS status
= NT_STATUS_OK
;
343 NTSTATUS result
= NT_STATUS_OK
;
345 int sids_processed
= 0;
346 const struct dom_sid
*hunk_sids
= sids
;
349 enum lsa_SidType
*hunk_types
;
350 char **domains
= NULL
;
352 enum lsa_SidType
*types
= NULL
;
353 bool have_mapped
= false;
354 bool have_unmapped
= false;
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
;
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
;
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
;
376 sids_left
= num_sids
;
377 hunk_domains
= domains
;
381 while (sids_left
> 0) {
383 NTSTATUS hunk_result
= NT_STATUS_UNSUCCESSFUL
;
385 hunk_num_sids
= ((sids_left
> LOOKUP_SIDS_HUNK_SIZE
)
386 ? LOOKUP_SIDS_HUNK_SIZE
389 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
392 sids_processed
+ hunk_num_sids
- 1,
395 status
= dcerpc_lsa_lookup_sids_noalloc(h
,
405 if (!NT_STATUS_IS_OK(status
)) {
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
;
418 if (NT_STATUS_IS_OK(hunk_result
)) {
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
)) {
426 for (i
=0; i
<hunk_num_sids
; i
++) {
427 if (hunk_types
[i
] == SID_NAME_UNKNOWN
) {
428 have_unmapped
= 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
;
448 result
= NT_STATUS_NONE_MAPPED
;
451 result
= STATUS_SOME_UNMAPPED
;
458 TALLOC_FREE(domains
);
465 NTSTATUS
dcerpc_lsa_lookup_sids(struct dcerpc_binding_handle
*h
,
467 struct policy_handle
*pol
,
469 const struct dom_sid
*sids
,
472 enum lsa_SidType
**ptypes
,
475 return dcerpc_lsa_lookup_sids_generic(h
,
487 NTSTATUS
rpccli_lsa_lookup_sids(struct rpc_pipe_client
*cli
,
489 struct policy_handle
*pol
,
491 const struct dom_sid
*sids
,
494 enum lsa_SidType
**ptypes
)
497 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
499 status
= dcerpc_lsa_lookup_sids_generic(cli
->binding_handle
,
509 if (!NT_STATUS_IS_OK(status
)) {
516 NTSTATUS
dcerpc_lsa_lookup_sids3(struct dcerpc_binding_handle
*h
,
518 struct policy_handle
*pol
,
520 const struct dom_sid
*sids
,
523 enum lsa_SidType
**ptypes
,
526 return dcerpc_lsa_lookup_sids_generic(h
,
538 NTSTATUS
rpccli_lsa_lookup_sids3(struct rpc_pipe_client
*cli
,
540 struct policy_handle
*pol
,
542 const struct dom_sid
*sids
,
545 enum lsa_SidType
**ptypes
)
548 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
550 status
= dcerpc_lsa_lookup_sids_generic(cli
->binding_handle
,
560 if (!NT_STATUS_IS_OK(status
)) {
567 /** Lookup a list of names */
569 static NTSTATUS
dcerpc_lsa_lookup_names_generic(struct dcerpc_binding_handle
*h
,
571 struct policy_handle
*pol
,
574 const char ***dom_names
,
575 enum lsa_LookupNamesLevel level
,
576 struct dom_sid
**sids
,
577 enum lsa_SidType
**types
,
578 bool use_lookupnames4
,
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
;
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
,
610 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
,
611 LSA_CLIENT_REVISION_2
,
614 status
= dcerpc_lsa_LookupNames(h
,
625 if (!NT_STATUS_IS_OK(status
)) {
629 if (!NT_STATUS_IS_OK(*presult
) &&
630 !NT_STATUS_EQUAL(*presult
, STATUS_SOME_UNMAPPED
)) {
631 /* An actual error occured */
635 /* Return output parameters */
637 *presult
= NT_STATUS_NONE_MAPPED
;
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
;
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
;
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
;
665 if (dom_names
!= NULL
) {
670 for (i
= 0; i
< num_names
; i
++) {
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
;
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 */
687 (*types
)[i
] = SID_NAME_UNKNOWN
;
691 if (use_lookupnames4
) {
692 sid_copy(sid
, sid_array3
.sids
[i
].sid
);
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
) {
705 (*dom_names
)[i
] = domains
->domains
[dom_idx
].name
.string
;
712 NTSTATUS
dcerpc_lsa_lookup_names(struct dcerpc_binding_handle
*h
,
714 struct policy_handle
*pol
,
717 const char ***dom_names
,
718 enum lsa_LookupNamesLevel level
,
719 struct dom_sid
**sids
,
720 enum lsa_SidType
**types
,
723 return dcerpc_lsa_lookup_names_generic(h
,
736 NTSTATUS
rpccli_lsa_lookup_names(struct rpc_pipe_client
*cli
,
738 struct policy_handle
*pol
,
741 const char ***dom_names
,
743 struct dom_sid
**sids
,
744 enum lsa_SidType
**types
)
747 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
749 status
= dcerpc_lsa_lookup_names(cli
->binding_handle
,
759 if (!NT_STATUS_IS_OK(status
)) {
766 NTSTATUS
dcerpc_lsa_lookup_names4(struct dcerpc_binding_handle
*h
,
768 struct policy_handle
*pol
,
771 const char ***dom_names
,
772 enum lsa_LookupNamesLevel level
,
773 struct dom_sid
**sids
,
774 enum lsa_SidType
**types
,
777 return dcerpc_lsa_lookup_names_generic(h
,
790 NTSTATUS
rpccli_lsa_lookup_names4(struct rpc_pipe_client
*cli
,
792 struct policy_handle
*pol
,
795 const char ***dom_names
,
797 struct dom_sid
**sids
,
798 enum lsa_SidType
**types
)
801 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
803 status
= dcerpc_lsa_lookup_names4(cli
->binding_handle
,
813 if (!NT_STATUS_IS_OK(status
)) {