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 TALLOC_CTX
*domains_ctx
,
170 TALLOC_CTX
*names_ctx
,
171 struct policy_handle
*pol
,
173 const struct dom_sid
*sids
,
176 enum lsa_SidType
*types
,
177 bool use_lookupsids3
,
180 NTSTATUS status
= NT_STATUS_OK
;
181 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
182 struct lsa_SidArray sid_array
;
183 struct lsa_RefDomainList
*ref_domains
= NULL
;
184 struct lsa_TransNameArray lsa_names
;
185 enum lsa_LookupNamesLevel level
= LSA_LOOKUP_NAMES_ALL
;
189 ZERO_STRUCT(lsa_names
);
191 sid_array
.num_sids
= num_sids
;
192 sid_array
.sids
= talloc_array(mem_ctx
, struct lsa_SidPtr
, num_sids
);
193 if (sid_array
.sids
== NULL
) {
194 return NT_STATUS_NO_MEMORY
;
197 for (i
= 0; i
<num_sids
; i
++) {
198 sid_array
.sids
[i
].sid
= dom_sid_dup(mem_ctx
, &sids
[i
]);
199 if (!sid_array
.sids
[i
].sid
) {
200 return NT_STATUS_NO_MEMORY
;
204 if (use_lookupsids3
) {
205 struct lsa_TransNameArray2 lsa_names2
;
208 ZERO_STRUCT(lsa_names2
);
210 status
= dcerpc_lsa_LookupSids3(h
,
217 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
,
218 LSA_CLIENT_REVISION_2
,
220 if (!NT_STATUS_IS_OK(status
)) {
224 if(!NT_STATUS_IS_ERR(result
)) {
225 lsa_names
.count
= lsa_names2
.count
;
226 lsa_names
.names
= talloc_array(mem_ctx
,
227 struct lsa_TranslatedName
,
229 if (lsa_names
.names
== NULL
) {
230 return NT_STATUS_NO_MEMORY
;
232 for (n
=0; n
< lsa_names
.count
; n
++) {
233 lsa_names
.names
[n
].sid_type
= lsa_names2
.names
[n
].sid_type
;
234 lsa_names
.names
[n
].name
= lsa_names2
.names
[n
].name
;
235 lsa_names
.names
[n
].sid_index
= lsa_names2
.names
[n
].sid_index
;
240 status
= dcerpc_lsa_LookupSids(h
,
251 DEBUG(10, ("LSA_LOOKUPSIDS returned status: '%s', result: '%s', "
252 "mapped count = %d'\n",
253 nt_errstr(status
), nt_errstr(result
), count
));
255 if (!NT_STATUS_IS_OK(status
)) {
259 if (!NT_STATUS_IS_OK(result
) &&
260 !NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) &&
261 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
267 /* Return output parameters */
268 if (NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) ||
271 for (i
= 0; i
< num_sids
; i
++) {
274 (types
)[i
] = SID_NAME_UNKNOWN
;
276 *presult
= NT_STATUS_NONE_MAPPED
;
280 for (i
= 0; i
< num_sids
; i
++) {
281 const char *name
, *dom_name
;
282 uint32_t dom_idx
= lsa_names
.names
[i
].sid_index
;
284 /* Translate optimised name through domain index array */
286 if (dom_idx
!= 0xffffffff) {
288 dom_name
= ref_domains
->domains
[dom_idx
].name
.string
;
289 name
= lsa_names
.names
[i
].name
.string
;
292 (names
)[i
] = talloc_strdup(names_ctx
, name
);
293 if ((names
)[i
] == NULL
) {
294 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
295 *presult
= NT_STATUS_UNSUCCESSFUL
;
301 domains
[i
] = talloc_strdup(domains_ctx
,
302 dom_name
? dom_name
: "");
303 (types
)[i
] = lsa_names
.names
[i
].sid_type
;
304 if ((domains
)[i
] == NULL
) {
305 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
306 *presult
= NT_STATUS_UNSUCCESSFUL
;
313 (types
)[i
] = SID_NAME_UNKNOWN
;
317 *presult
= NT_STATUS_OK
;
321 /* Lookup a list of sids
323 * do it the right way: there is a limit (of 20480 for w2k3) entries
324 * returned by this call. when the sids list contains more entries,
325 * empty lists are returned. This version of lsa_lookup_sids passes
326 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
328 /* This constant defines the limit of how many sids to look up
329 * in one call (maximum). the limit from the server side is
330 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
331 #define LOOKUP_SIDS_HUNK_SIZE 1000
333 NTSTATUS
dcerpc_lsa_lookup_sids_generic(struct dcerpc_binding_handle
*h
,
335 struct policy_handle
*pol
,
337 const struct dom_sid
*sids
,
340 enum lsa_SidType
**ptypes
,
341 bool use_lookupsids3
,
344 NTSTATUS status
= NT_STATUS_OK
;
345 NTSTATUS result
= NT_STATUS_OK
;
347 int sids_processed
= 0;
348 const struct dom_sid
*hunk_sids
= sids
;
351 enum lsa_SidType
*hunk_types
;
352 char **domains
= NULL
;
354 enum lsa_SidType
*types
= NULL
;
355 bool have_mapped
= false;
356 bool have_unmapped
= false;
359 if (!(domains
= talloc_array(mem_ctx
, char *, num_sids
))) {
360 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
361 status
= NT_STATUS_NO_MEMORY
;
365 if (!(names
= talloc_array(mem_ctx
, char *, num_sids
))) {
366 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
367 status
= NT_STATUS_NO_MEMORY
;
371 if (!(types
= talloc_array(mem_ctx
, enum lsa_SidType
, num_sids
))) {
372 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
373 status
= NT_STATUS_NO_MEMORY
;
378 sids_left
= num_sids
;
379 hunk_domains
= domains
;
383 while (sids_left
> 0) {
385 NTSTATUS hunk_result
= NT_STATUS_UNSUCCESSFUL
;
387 hunk_num_sids
= ((sids_left
> LOOKUP_SIDS_HUNK_SIZE
)
388 ? LOOKUP_SIDS_HUNK_SIZE
391 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
394 sids_processed
+ hunk_num_sids
- 1,
397 status
= dcerpc_lsa_lookup_sids_noalloc(h
,
399 (TALLOC_CTX
*)domains
,
409 if (!NT_STATUS_IS_OK(status
)) {
413 if (!NT_STATUS_IS_OK(hunk_result
) &&
414 !NT_STATUS_EQUAL(hunk_result
, STATUS_SOME_UNMAPPED
) &&
415 !NT_STATUS_EQUAL(hunk_result
, NT_STATUS_NONE_MAPPED
))
417 /* An actual error occured */
418 *presult
= hunk_result
;
422 if (NT_STATUS_IS_OK(hunk_result
)) {
425 if (NT_STATUS_EQUAL(hunk_result
, NT_STATUS_NONE_MAPPED
)) {
426 have_unmapped
= true;
428 if (NT_STATUS_EQUAL(hunk_result
, STATUS_SOME_UNMAPPED
)) {
430 for (i
=0; i
<hunk_num_sids
; i
++) {
431 if (hunk_types
[i
] == SID_NAME_UNKNOWN
) {
432 have_unmapped
= true;
439 sids_left
-= hunk_num_sids
;
440 sids_processed
+= hunk_num_sids
;
441 hunk_sids
+= hunk_num_sids
;
442 hunk_domains
+= hunk_num_sids
;
443 hunk_names
+= hunk_num_sids
;
444 hunk_types
+= hunk_num_sids
;
452 result
= NT_STATUS_NONE_MAPPED
;
455 result
= STATUS_SOME_UNMAPPED
;
462 TALLOC_FREE(domains
);
469 NTSTATUS
dcerpc_lsa_lookup_sids(struct dcerpc_binding_handle
*h
,
471 struct policy_handle
*pol
,
473 const struct dom_sid
*sids
,
476 enum lsa_SidType
**ptypes
,
479 return dcerpc_lsa_lookup_sids_generic(h
,
491 NTSTATUS
rpccli_lsa_lookup_sids(struct rpc_pipe_client
*cli
,
493 struct policy_handle
*pol
,
495 const struct dom_sid
*sids
,
498 enum lsa_SidType
**ptypes
)
501 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
503 status
= dcerpc_lsa_lookup_sids_generic(cli
->binding_handle
,
513 if (!NT_STATUS_IS_OK(status
)) {
520 NTSTATUS
dcerpc_lsa_lookup_sids3(struct dcerpc_binding_handle
*h
,
522 struct policy_handle
*pol
,
524 const struct dom_sid
*sids
,
527 enum lsa_SidType
**ptypes
,
530 return dcerpc_lsa_lookup_sids_generic(h
,
542 /** Lookup a list of names */
544 static NTSTATUS
dcerpc_lsa_lookup_names_generic(struct dcerpc_binding_handle
*h
,
546 struct policy_handle
*pol
,
549 const char ***dom_names
,
550 enum lsa_LookupNamesLevel level
,
551 struct dom_sid
**sids
,
552 enum lsa_SidType
**types
,
553 bool use_lookupnames4
,
557 struct lsa_String
*lsa_names
= NULL
;
558 struct lsa_RefDomainList
*domains
= NULL
;
559 struct lsa_TransSidArray sid_array
;
560 struct lsa_TransSidArray3 sid_array3
;
564 ZERO_STRUCT(sid_array
);
565 ZERO_STRUCT(sid_array3
);
567 lsa_names
= talloc_array(mem_ctx
, struct lsa_String
, num_names
);
568 if (lsa_names
== NULL
) {
569 return NT_STATUS_NO_MEMORY
;
572 for (i
= 0; i
< num_names
; i
++) {
573 init_lsa_String(&lsa_names
[i
], names
[i
]);
576 if (use_lookupnames4
) {
577 status
= dcerpc_lsa_LookupNames4(h
,
585 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
,
586 LSA_CLIENT_REVISION_2
,
589 status
= dcerpc_lsa_LookupNames(h
,
600 if (!NT_STATUS_IS_OK(status
)) {
604 if (!NT_STATUS_IS_OK(*presult
) &&
605 !NT_STATUS_EQUAL(*presult
, STATUS_SOME_UNMAPPED
)) {
606 /* An actual error occured */
610 /* Return output parameters */
612 *presult
= NT_STATUS_NONE_MAPPED
;
617 if (!((*sids
= talloc_array(mem_ctx
, struct dom_sid
, num_names
)))) {
618 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
619 *presult
= NT_STATUS_NO_MEMORY
;
623 if (!((*types
= talloc_array(mem_ctx
, enum lsa_SidType
, num_names
)))) {
624 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
625 *presult
= NT_STATUS_NO_MEMORY
;
629 if (dom_names
!= NULL
) {
630 *dom_names
= talloc_array(mem_ctx
, const char *, num_names
);
631 if (*dom_names
== NULL
) {
632 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
633 *presult
= NT_STATUS_NO_MEMORY
;
640 if (dom_names
!= NULL
) {
645 for (i
= 0; i
< num_names
; i
++) {
647 struct dom_sid
*sid
= &(*sids
)[i
];
649 if (use_lookupnames4
) {
650 dom_idx
= sid_array3
.sids
[i
].sid_index
;
651 (*types
)[i
] = sid_array3
.sids
[i
].sid_type
;
653 dom_idx
= sid_array
.sids
[i
].sid_index
;
654 (*types
)[i
] = sid_array
.sids
[i
].sid_type
;
657 /* Translate optimised sid through domain index array */
659 if (dom_idx
== 0xffffffff) {
660 /* Nothing to do, this is unknown */
662 (*types
)[i
] = SID_NAME_UNKNOWN
;
666 if (use_lookupnames4
) {
667 sid_copy(sid
, sid_array3
.sids
[i
].sid
);
669 sid_copy(sid
, domains
->domains
[dom_idx
].sid
);
671 if (sid_array
.sids
[i
].rid
!= 0xffffffff) {
672 sid_append_rid(sid
, sid_array
.sids
[i
].rid
);
676 if (dom_names
== NULL
) {
680 (*dom_names
)[i
] = domains
->domains
[dom_idx
].name
.string
;
687 NTSTATUS
dcerpc_lsa_lookup_names(struct dcerpc_binding_handle
*h
,
689 struct policy_handle
*pol
,
692 const char ***dom_names
,
693 enum lsa_LookupNamesLevel level
,
694 struct dom_sid
**sids
,
695 enum lsa_SidType
**types
,
698 return dcerpc_lsa_lookup_names_generic(h
,
711 NTSTATUS
rpccli_lsa_lookup_names(struct rpc_pipe_client
*cli
,
713 struct policy_handle
*pol
,
716 const char ***dom_names
,
718 struct dom_sid
**sids
,
719 enum lsa_SidType
**types
)
722 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
724 status
= dcerpc_lsa_lookup_names(cli
->binding_handle
,
734 if (!NT_STATUS_IS_OK(status
)) {
741 NTSTATUS
dcerpc_lsa_lookup_names4(struct dcerpc_binding_handle
*h
,
743 struct policy_handle
*pol
,
746 const char ***dom_names
,
747 enum lsa_LookupNamesLevel level
,
748 struct dom_sid
**sids
,
749 enum lsa_SidType
**types
,
752 return dcerpc_lsa_lookup_names_generic(h
,