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
;
54 struct lsa_QosInfo qos
;
55 uint16_t system_name
= '\\';
63 qos
.impersonation_level
= 2;
65 qos
.effective_only
= 0;
70 return dcerpc_lsa_OpenPolicy(h
,
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
,
85 bool sec_qos
, uint32_t des_access
,
86 struct policy_handle
*pol
)
89 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
91 status
= dcerpc_lsa_open_policy(cli
->binding_handle
,
97 if (!NT_STATUS_IS_OK(status
)) {
104 NTSTATUS
dcerpc_lsa_open_policy2(struct dcerpc_binding_handle
*h
,
106 const char *srv_name_slash
,
109 struct policy_handle
*pol
,
112 struct lsa_ObjectAttribute attr
;
113 struct lsa_QosInfo qos
;
121 qos
.impersonation_level
= 2;
122 qos
.context_mode
= 1;
123 qos
.effective_only
= 0;
128 return dcerpc_lsa_OpenPolicy2(h
,
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
)
147 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
149 status
= dcerpc_lsa_open_policy2(cli
->binding_handle
,
156 if (!NT_STATUS_IS_OK(status
)) {
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
,
170 TALLOC_CTX
*domains_ctx
,
171 TALLOC_CTX
*names_ctx
,
172 struct policy_handle
*pol
,
174 const struct dom_sid
*sids
,
177 enum lsa_SidType
*types
,
178 bool use_lookupsids3
,
181 NTSTATUS status
= NT_STATUS_OK
;
182 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
183 struct lsa_SidArray sid_array
;
184 struct lsa_RefDomainList
*ref_domains
= NULL
;
185 struct lsa_TransNameArray lsa_names
;
186 enum lsa_LookupNamesLevel level
= LSA_LOOKUP_NAMES_ALL
;
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
;
209 ZERO_STRUCT(lsa_names2
);
211 status
= dcerpc_lsa_LookupSids3(h
,
218 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
,
219 LSA_CLIENT_REVISION_2
,
221 if (!NT_STATUS_IS_OK(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
,
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
;
241 status
= dcerpc_lsa_LookupSids(h
,
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
)) {
260 if (NT_STATUS_LOOKUP_ERR(result
)) {
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
;
282 if (i
>= lsa_names
.count
) {
283 *presult
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
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
;
296 if (dom_idx
>= ref_domains
->count
) {
297 *presult
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
301 dom_name
= ref_domains
->domains
[dom_idx
].name
.string
;
302 name
= lsa_names
.names
[i
].name
.string
;
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
;
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
;
326 (types
)[i
] = SID_NAME_UNKNOWN
;
330 *presult
= NT_STATUS_OK
;
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
,
348 struct policy_handle
*pol
,
350 const struct dom_sid
*sids
,
353 enum lsa_SidType
**ptypes
,
354 bool use_lookupsids3
,
357 NTSTATUS status
= NT_STATUS_OK
;
358 NTSTATUS result
= NT_STATUS_OK
;
360 int sids_processed
= 0;
361 const struct dom_sid
*hunk_sids
= sids
;
364 enum lsa_SidType
*hunk_types
;
365 char **domains
= NULL
;
367 enum lsa_SidType
*types
= NULL
;
368 bool have_mapped
= false;
369 bool have_unmapped
= false;
372 if (!(domains
= talloc_array(mem_ctx
, char *, num_sids
))) {
373 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
374 status
= NT_STATUS_NO_MEMORY
;
378 if (!(names
= talloc_array(mem_ctx
, char *, num_sids
))) {
379 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
380 status
= NT_STATUS_NO_MEMORY
;
384 if (!(types
= talloc_array(mem_ctx
, enum lsa_SidType
, num_sids
))) {
385 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
386 status
= NT_STATUS_NO_MEMORY
;
391 sids_left
= num_sids
;
392 hunk_domains
= domains
;
396 while (sids_left
> 0) {
398 NTSTATUS hunk_result
= NT_STATUS_UNSUCCESSFUL
;
400 hunk_num_sids
= ((sids_left
> LOOKUP_SIDS_HUNK_SIZE
)
401 ? LOOKUP_SIDS_HUNK_SIZE
404 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
407 sids_processed
+ hunk_num_sids
- 1,
410 status
= dcerpc_lsa_lookup_sids_noalloc(h
,
412 (TALLOC_CTX
*)domains
,
422 if (!NT_STATUS_IS_OK(status
)) {
426 if (!NT_STATUS_IS_OK(hunk_result
) &&
427 !NT_STATUS_EQUAL(hunk_result
, STATUS_SOME_UNMAPPED
) &&
428 !NT_STATUS_EQUAL(hunk_result
, NT_STATUS_NONE_MAPPED
))
430 /* An actual error occurred */
431 *presult
= hunk_result
;
435 if (NT_STATUS_IS_OK(hunk_result
)) {
438 if (NT_STATUS_EQUAL(hunk_result
, NT_STATUS_NONE_MAPPED
)) {
439 have_unmapped
= true;
441 if (NT_STATUS_EQUAL(hunk_result
, STATUS_SOME_UNMAPPED
)) {
443 for (i
=0; i
<hunk_num_sids
; i
++) {
444 if (hunk_types
[i
] == SID_NAME_UNKNOWN
) {
445 have_unmapped
= true;
452 sids_left
-= hunk_num_sids
;
453 sids_processed
+= hunk_num_sids
;
454 hunk_sids
+= hunk_num_sids
;
455 hunk_domains
+= hunk_num_sids
;
456 hunk_names
+= hunk_num_sids
;
457 hunk_types
+= hunk_num_sids
;
465 result
= NT_STATUS_NONE_MAPPED
;
468 result
= STATUS_SOME_UNMAPPED
;
475 TALLOC_FREE(domains
);
482 NTSTATUS
dcerpc_lsa_lookup_sids(struct dcerpc_binding_handle
*h
,
484 struct policy_handle
*pol
,
486 const struct dom_sid
*sids
,
489 enum lsa_SidType
**ptypes
,
492 return dcerpc_lsa_lookup_sids_generic(h
,
504 NTSTATUS
rpccli_lsa_lookup_sids(struct rpc_pipe_client
*cli
,
506 struct policy_handle
*pol
,
508 const struct dom_sid
*sids
,
511 enum lsa_SidType
**ptypes
)
514 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
516 status
= dcerpc_lsa_lookup_sids_generic(cli
->binding_handle
,
526 if (!NT_STATUS_IS_OK(status
)) {
533 NTSTATUS
dcerpc_lsa_lookup_sids3(struct dcerpc_binding_handle
*h
,
535 struct policy_handle
*pol
,
537 const struct dom_sid
*sids
,
540 enum lsa_SidType
**ptypes
,
543 return dcerpc_lsa_lookup_sids_generic(h
,
555 /** Lookup a list of names */
557 NTSTATUS
dcerpc_lsa_lookup_names_generic(struct dcerpc_binding_handle
*h
,
559 struct policy_handle
*pol
,
562 const char ***dom_names
,
563 enum lsa_LookupNamesLevel level
,
564 struct dom_sid
**sids
,
565 enum lsa_SidType
**types
,
566 bool use_lookupnames4
,
570 struct lsa_String
*lsa_names
= NULL
;
571 struct lsa_RefDomainList
*domains
= NULL
;
572 struct lsa_TransSidArray sid_array
;
573 struct lsa_TransSidArray3 sid_array3
;
577 ZERO_STRUCT(sid_array
);
578 ZERO_STRUCT(sid_array3
);
580 lsa_names
= talloc_array(mem_ctx
, struct lsa_String
, num_names
);
581 if (lsa_names
== NULL
) {
582 return NT_STATUS_NO_MEMORY
;
585 for (i
= 0; i
< num_names
; i
++) {
586 init_lsa_String(&lsa_names
[i
], names
[i
]);
589 if (use_lookupnames4
) {
590 status
= dcerpc_lsa_LookupNames4(h
,
598 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
,
599 LSA_CLIENT_REVISION_2
,
602 status
= dcerpc_lsa_LookupNames(h
,
613 if (!NT_STATUS_IS_OK(status
)) {
617 if (!NT_STATUS_IS_OK(*presult
) &&
618 !NT_STATUS_EQUAL(*presult
, STATUS_SOME_UNMAPPED
)) {
619 /* An actual error occurred */
623 /* Return output parameters */
625 *presult
= NT_STATUS_NONE_MAPPED
;
630 if (!((*sids
= talloc_array(mem_ctx
, struct dom_sid
, num_names
)))) {
631 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
632 *presult
= NT_STATUS_NO_MEMORY
;
636 if (!((*types
= talloc_array(mem_ctx
, enum lsa_SidType
, num_names
)))) {
637 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
638 *presult
= NT_STATUS_NO_MEMORY
;
642 if (dom_names
!= NULL
) {
643 *dom_names
= talloc_array(mem_ctx
, const char *, num_names
);
644 if (*dom_names
== NULL
) {
645 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
646 *presult
= NT_STATUS_NO_MEMORY
;
653 if (dom_names
!= NULL
) {
658 for (i
= 0; i
< num_names
; i
++) {
660 struct dom_sid
*sid
= &(*sids
)[i
];
662 if (use_lookupnames4
) {
663 if (i
>= sid_array3
.count
) {
664 *presult
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
668 dom_idx
= sid_array3
.sids
[i
].sid_index
;
669 (*types
)[i
] = sid_array3
.sids
[i
].sid_type
;
671 if (i
>= sid_array
.count
) {
672 *presult
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
676 dom_idx
= sid_array
.sids
[i
].sid_index
;
677 (*types
)[i
] = sid_array
.sids
[i
].sid_type
;
680 /* Translate optimised sid through domain index array */
682 if (dom_idx
== 0xffffffff) {
683 /* Nothing to do, this is unknown */
685 (*types
)[i
] = SID_NAME_UNKNOWN
;
688 if (domains
== NULL
) {
689 *presult
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
692 if (dom_idx
>= domains
->count
) {
693 *presult
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
697 if (use_lookupnames4
) {
698 sid_copy(sid
, sid_array3
.sids
[i
].sid
);
700 sid_copy(sid
, domains
->domains
[dom_idx
].sid
);
702 if (sid_array
.sids
[i
].rid
!= 0xffffffff) {
703 sid_append_rid(sid
, sid_array
.sids
[i
].rid
);
707 if (dom_names
== NULL
) {
711 (*dom_names
)[i
] = domains
->domains
[dom_idx
].name
.string
;
718 NTSTATUS
dcerpc_lsa_lookup_names(struct dcerpc_binding_handle
*h
,
720 struct policy_handle
*pol
,
723 const char ***dom_names
,
724 enum lsa_LookupNamesLevel level
,
725 struct dom_sid
**sids
,
726 enum lsa_SidType
**types
,
729 return dcerpc_lsa_lookup_names_generic(h
,
742 NTSTATUS
rpccli_lsa_lookup_names(struct rpc_pipe_client
*cli
,
744 struct policy_handle
*pol
,
747 const char ***dom_names
,
749 struct dom_sid
**sids
,
750 enum lsa_SidType
**types
)
753 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
755 status
= dcerpc_lsa_lookup_names(cli
->binding_handle
,
765 if (!NT_STATUS_IS_OK(status
)) {
772 NTSTATUS
dcerpc_lsa_lookup_names4(struct dcerpc_binding_handle
*h
,
774 struct policy_handle
*pol
,
777 const char ***dom_names
,
778 enum lsa_LookupNamesLevel level
,
779 struct dom_sid
**sids
,
780 enum lsa_SidType
**types
,
783 return dcerpc_lsa_lookup_names_generic(h
,