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/>.
27 /** @defgroup lsa LSA - Local Security Architecture
36 * RPC client routines for the LSA RPC pipe. LSA means "local
37 * security authority", which is half of a password database.
40 /** Open a LSA policy handle
42 * @param cli Handle on an initialised SMB connection */
44 NTSTATUS
rpccli_lsa_open_policy(struct rpc_pipe_client
*cli
,
46 bool sec_qos
, uint32 des_access
,
49 struct lsa_ObjectAttribute attr
;
50 struct lsa_QosInfo qos
;
51 uint16_t system_name
= '\\';
54 init_lsa_sec_qos(&qos
, 0xc, 2, 1, 0);
55 init_lsa_obj_attr(&attr
,
63 init_lsa_obj_attr(&attr
,
72 return rpccli_lsa_OpenPolicy(cli
, mem_ctx
,
79 /** Open a LSA policy handle
81 * @param cli Handle on an initialised SMB connection
84 NTSTATUS
rpccli_lsa_open_policy2(struct rpc_pipe_client
*cli
,
85 TALLOC_CTX
*mem_ctx
, bool sec_qos
,
86 uint32 des_access
, POLICY_HND
*pol
)
88 struct lsa_ObjectAttribute attr
;
89 struct lsa_QosInfo qos
;
92 init_lsa_sec_qos(&qos
, 0xc, 2, 1, 0);
93 init_lsa_obj_attr(&attr
,
101 init_lsa_obj_attr(&attr
,
110 return rpccli_lsa_OpenPolicy2(cli
, mem_ctx
,
117 /* Lookup a list of sids
119 * internal version withOUT memory allocation of the target arrays.
120 * this assumes suffciently sized arrays to store domains, names and types. */
122 static NTSTATUS
rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client
*cli
,
129 enum lsa_SidType
*types
,
130 bool use_lookupsids3
)
132 NTSTATUS result
= NT_STATUS_OK
;
133 TALLOC_CTX
*tmp_ctx
= NULL
;
135 struct lsa_SidArray sid_array
;
136 struct lsa_RefDomainList
*ref_domains
= NULL
;
137 struct lsa_TransNameArray lsa_names
;
141 ZERO_STRUCT(lsa_names
);
143 tmp_ctx
= talloc_new(mem_ctx
);
145 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
146 result
= NT_STATUS_UNSUCCESSFUL
;
150 sid_array
.num_sids
= num_sids
;
151 sid_array
.sids
= TALLOC_ARRAY(mem_ctx
, struct lsa_SidPtr
, num_sids
);
152 if (!sid_array
.sids
) {
153 return NT_STATUS_NO_MEMORY
;
156 for (i
= 0; i
<num_sids
; i
++) {
157 sid_array
.sids
[i
].sid
= sid_dup_talloc(mem_ctx
, &sids
[i
]);
158 if (!sid_array
.sids
[i
].sid
) {
159 return NT_STATUS_NO_MEMORY
;
163 if (use_lookupsids3
) {
164 struct lsa_TransNameArray2 lsa_names2
;
167 ZERO_STRUCT(lsa_names2
);
169 result
= rpccli_lsa_LookupSids3(cli
, mem_ctx
,
178 if (!NT_STATUS_IS_ERR(result
)) {
179 lsa_names
.count
= lsa_names2
.count
;
180 lsa_names
.names
= talloc_array(mem_ctx
, struct lsa_TranslatedName
, lsa_names
.count
);
181 if (!lsa_names
.names
) {
182 return NT_STATUS_NO_MEMORY
;
184 for (n
=0; n
< lsa_names
.count
; n
++) {
185 lsa_names
.names
[n
].sid_type
= lsa_names2
.names
[n
].sid_type
;
186 lsa_names
.names
[n
].name
= lsa_names2
.names
[n
].name
;
187 lsa_names
.names
[n
].sid_index
= lsa_names2
.names
[n
].sid_index
;
192 result
= rpccli_lsa_LookupSids(cli
, mem_ctx
,
201 DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
202 nt_errstr(result
), count
));
204 if (!NT_STATUS_IS_OK(result
) &&
205 !NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) &&
206 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
208 /* An actual error occured */
212 /* Return output parameters */
214 if (NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) ||
217 for (i
= 0; i
< num_sids
; i
++) {
220 (types
)[i
] = SID_NAME_UNKNOWN
;
222 result
= NT_STATUS_NONE_MAPPED
;
226 for (i
= 0; i
< num_sids
; i
++) {
227 const char *name
, *dom_name
;
228 uint32_t dom_idx
= lsa_names
.names
[i
].sid_index
;
230 /* Translate optimised name through domain index array */
232 if (dom_idx
!= 0xffffffff) {
234 dom_name
= ref_domains
->domains
[dom_idx
].name
.string
;
235 name
= lsa_names
.names
[i
].name
.string
;
238 (names
)[i
] = talloc_strdup(mem_ctx
, name
);
239 if ((names
)[i
] == NULL
) {
240 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
241 result
= NT_STATUS_UNSUCCESSFUL
;
247 (domains
)[i
] = talloc_strdup(mem_ctx
, dom_name
);
248 (types
)[i
] = lsa_names
.names
[i
].sid_type
;
249 if (((domains
)[i
] == NULL
)) {
250 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
251 result
= NT_STATUS_UNSUCCESSFUL
;
258 (types
)[i
] = SID_NAME_UNKNOWN
;
263 TALLOC_FREE(tmp_ctx
);
267 /* Lookup a list of sids
269 * do it the right way: there is a limit (of 20480 for w2k3) entries
270 * returned by this call. when the sids list contains more entries,
271 * empty lists are returned. This version of lsa_lookup_sids passes
272 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
274 /* This constant defines the limit of how many sids to look up
275 * in one call (maximum). the limit from the server side is
276 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
277 #define LOOKUP_SIDS_HUNK_SIZE 1000
279 static NTSTATUS
rpccli_lsa_lookup_sids_generic(struct rpc_pipe_client
*cli
,
281 struct policy_handle
*pol
,
286 enum lsa_SidType
**ptypes
,
287 bool use_lookupsids3
)
289 NTSTATUS result
= NT_STATUS_OK
;
291 int sids_processed
= 0;
292 const DOM_SID
*hunk_sids
= sids
;
295 enum lsa_SidType
*hunk_types
;
296 char **domains
= NULL
;
298 enum lsa_SidType
*types
= NULL
;
301 if (!(domains
= TALLOC_ARRAY(mem_ctx
, char *, num_sids
))) {
302 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
303 result
= NT_STATUS_NO_MEMORY
;
307 if (!(names
= TALLOC_ARRAY(mem_ctx
, char *, num_sids
))) {
308 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
309 result
= NT_STATUS_NO_MEMORY
;
313 if (!(types
= TALLOC_ARRAY(mem_ctx
, enum lsa_SidType
, num_sids
))) {
314 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
315 result
= NT_STATUS_NO_MEMORY
;
320 sids_left
= num_sids
;
321 hunk_domains
= domains
;
325 while (sids_left
> 0) {
327 NTSTATUS hunk_result
= NT_STATUS_OK
;
329 hunk_num_sids
= ((sids_left
> LOOKUP_SIDS_HUNK_SIZE
)
330 ? LOOKUP_SIDS_HUNK_SIZE
333 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
336 sids_processed
+ hunk_num_sids
- 1,
339 hunk_result
= rpccli_lsa_lookup_sids_noalloc(cli
,
349 if (!NT_STATUS_IS_OK(hunk_result
) &&
350 !NT_STATUS_EQUAL(hunk_result
, STATUS_SOME_UNMAPPED
) &&
351 !NT_STATUS_EQUAL(hunk_result
, NT_STATUS_NONE_MAPPED
))
353 /* An actual error occured */
354 result
= hunk_result
;
358 /* adapt overall result */
359 if (( NT_STATUS_IS_OK(result
) &&
360 !NT_STATUS_IS_OK(hunk_result
))
362 ( NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) &&
363 !NT_STATUS_EQUAL(hunk_result
, NT_STATUS_NONE_MAPPED
)))
365 result
= STATUS_SOME_UNMAPPED
;
368 sids_left
-= hunk_num_sids
;
369 sids_processed
+= hunk_num_sids
; /* only used in DEBUG */
370 hunk_sids
+= hunk_num_sids
;
371 hunk_domains
+= hunk_num_sids
;
372 hunk_names
+= hunk_num_sids
;
373 hunk_types
+= hunk_num_sids
;
382 TALLOC_FREE(domains
);
388 NTSTATUS
rpccli_lsa_lookup_sids(struct rpc_pipe_client
*cli
,
390 struct policy_handle
*pol
,
395 enum lsa_SidType
**ptypes
)
397 return rpccli_lsa_lookup_sids_generic(cli
, mem_ctx
, pol
, num_sids
, sids
,
398 pdomains
, pnames
, ptypes
, false);
401 NTSTATUS
rpccli_lsa_lookup_sids3(struct rpc_pipe_client
*cli
,
403 struct policy_handle
*pol
,
408 enum lsa_SidType
**ptypes
)
410 return rpccli_lsa_lookup_sids_generic(cli
, mem_ctx
, pol
, num_sids
, sids
,
411 pdomains
, pnames
, ptypes
, true);
414 /** Lookup a list of names */
416 static NTSTATUS
rpccli_lsa_lookup_names_generic(struct rpc_pipe_client
*cli
,
418 struct policy_handle
*pol
, int num_names
,
420 const char ***dom_names
,
423 enum lsa_SidType
**types
,
424 bool use_lookupnames4
)
428 struct lsa_String
*lsa_names
= NULL
;
429 struct lsa_RefDomainList
*domains
= NULL
;
430 struct lsa_TransSidArray sid_array
;
431 struct lsa_TransSidArray3 sid_array3
;
434 ZERO_STRUCT(sid_array
);
435 ZERO_STRUCT(sid_array3
);
437 lsa_names
= TALLOC_ARRAY(mem_ctx
, struct lsa_String
, num_names
);
439 return NT_STATUS_NO_MEMORY
;
442 for (i
=0; i
<num_names
; i
++) {
443 init_lsa_String(&lsa_names
[i
], names
[i
]);
446 if (use_lookupnames4
) {
447 result
= rpccli_lsa_LookupNames4(cli
, mem_ctx
,
457 result
= rpccli_lsa_LookupNames(cli
, mem_ctx
,
467 if (!NT_STATUS_IS_OK(result
) && NT_STATUS_V(result
) !=
468 NT_STATUS_V(STATUS_SOME_UNMAPPED
)) {
470 /* An actual error occured */
475 /* Return output parameters */
478 result
= NT_STATUS_NONE_MAPPED
;
483 if (!((*sids
= TALLOC_ARRAY(mem_ctx
, DOM_SID
, num_names
)))) {
484 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
485 result
= NT_STATUS_NO_MEMORY
;
489 if (!((*types
= TALLOC_ARRAY(mem_ctx
, enum lsa_SidType
, num_names
)))) {
490 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
491 result
= NT_STATUS_NO_MEMORY
;
495 if (dom_names
!= NULL
) {
496 *dom_names
= TALLOC_ARRAY(mem_ctx
, const char *, num_names
);
497 if (*dom_names
== NULL
) {
498 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
499 result
= NT_STATUS_NO_MEMORY
;
506 if (dom_names
!= NULL
) {
511 for (i
= 0; i
< num_names
; i
++) {
513 DOM_SID
*sid
= &(*sids
)[i
];
515 if (use_lookupnames4
) {
516 dom_idx
= sid_array3
.sids
[i
].sid_index
;
517 (*types
)[i
] = sid_array3
.sids
[i
].sid_type
;
519 dom_idx
= sid_array
.sids
[i
].sid_index
;
520 (*types
)[i
] = sid_array
.sids
[i
].sid_type
;
523 /* Translate optimised sid through domain index array */
525 if (dom_idx
== 0xffffffff) {
526 /* Nothing to do, this is unknown */
528 (*types
)[i
] = SID_NAME_UNKNOWN
;
532 if (use_lookupnames4
) {
533 sid_copy(sid
, sid_array3
.sids
[i
].sid
);
535 sid_copy(sid
, domains
->domains
[dom_idx
].sid
);
537 if (sid_array
.sids
[i
].rid
!= 0xffffffff) {
538 sid_append_rid(sid
, sid_array
.sids
[i
].rid
);
542 if (dom_names
== NULL
) {
546 (*dom_names
)[i
] = domains
->domains
[dom_idx
].name
.string
;
554 NTSTATUS
rpccli_lsa_lookup_names(struct rpc_pipe_client
*cli
,
556 struct policy_handle
*pol
, int num_names
,
558 const char ***dom_names
,
561 enum lsa_SidType
**types
)
563 return rpccli_lsa_lookup_names_generic(cli
, mem_ctx
, pol
, num_names
,
564 names
, dom_names
, level
, sids
,
568 NTSTATUS
rpccli_lsa_lookup_names4(struct rpc_pipe_client
*cli
,
570 struct policy_handle
*pol
, int num_names
,
572 const char ***dom_names
,
575 enum lsa_SidType
**types
)
577 return rpccli_lsa_lookup_names_generic(cli
, mem_ctx
, pol
, num_names
,
578 names
, dom_names
, level
, sids
,