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 result
= rpccli_lsa_LookupSids3(cli
, mem_ctx
,
176 if (!NT_STATUS_IS_ERR(result
)) {
177 lsa_names
.count
= lsa_names2
.count
;
178 lsa_names
.names
= talloc_array(mem_ctx
, struct lsa_TranslatedName
, lsa_names
.count
);
179 if (!lsa_names
.names
) {
180 return NT_STATUS_NO_MEMORY
;
182 for (n
=0; n
< lsa_names
.count
; n
++) {
183 lsa_names
.names
[n
].sid_type
= lsa_names2
.names
[n
].sid_type
;
184 lsa_names
.names
[n
].name
= lsa_names2
.names
[n
].name
;
185 lsa_names
.names
[n
].sid_index
= lsa_names2
.names
[n
].sid_index
;
190 result
= rpccli_lsa_LookupSids(cli
, mem_ctx
,
199 DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
200 nt_errstr(result
), count
));
202 if (!NT_STATUS_IS_OK(result
) &&
203 !NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) &&
204 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
206 /* An actual error occured */
210 /* Return output parameters */
212 if (NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) ||
215 for (i
= 0; i
< num_sids
; i
++) {
218 (types
)[i
] = SID_NAME_UNKNOWN
;
220 result
= NT_STATUS_NONE_MAPPED
;
224 for (i
= 0; i
< num_sids
; i
++) {
225 const char *name
, *dom_name
;
226 uint32_t dom_idx
= lsa_names
.names
[i
].sid_index
;
228 /* Translate optimised name through domain index array */
230 if (dom_idx
!= 0xffffffff) {
232 dom_name
= ref_domains
->domains
[dom_idx
].name
.string
;
233 name
= lsa_names
.names
[i
].name
.string
;
236 (names
)[i
] = talloc_strdup(mem_ctx
, name
);
237 if ((names
)[i
] == NULL
) {
238 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
239 result
= NT_STATUS_UNSUCCESSFUL
;
245 (domains
)[i
] = talloc_strdup(mem_ctx
, dom_name
);
246 (types
)[i
] = lsa_names
.names
[i
].sid_type
;
247 if (((domains
)[i
] == NULL
)) {
248 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
249 result
= NT_STATUS_UNSUCCESSFUL
;
256 (types
)[i
] = SID_NAME_UNKNOWN
;
261 TALLOC_FREE(tmp_ctx
);
265 /* Lookup a list of sids
267 * do it the right way: there is a limit (of 20480 for w2k3) entries
268 * returned by this call. when the sids list contains more entries,
269 * empty lists are returned. This version of lsa_lookup_sids passes
270 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
272 /* This constant defines the limit of how many sids to look up
273 * in one call (maximum). the limit from the server side is
274 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
275 #define LOOKUP_SIDS_HUNK_SIZE 1000
277 static NTSTATUS
rpccli_lsa_lookup_sids_generic(struct rpc_pipe_client
*cli
,
279 struct policy_handle
*pol
,
284 enum lsa_SidType
**ptypes
,
285 bool use_lookupsids3
)
287 NTSTATUS result
= NT_STATUS_OK
;
289 int sids_processed
= 0;
290 const DOM_SID
*hunk_sids
= sids
;
293 enum lsa_SidType
*hunk_types
;
294 char **domains
= NULL
;
296 enum lsa_SidType
*types
= NULL
;
299 if (!(domains
= TALLOC_ARRAY(mem_ctx
, char *, num_sids
))) {
300 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
301 result
= NT_STATUS_NO_MEMORY
;
305 if (!(names
= TALLOC_ARRAY(mem_ctx
, char *, num_sids
))) {
306 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
307 result
= NT_STATUS_NO_MEMORY
;
311 if (!(types
= TALLOC_ARRAY(mem_ctx
, enum lsa_SidType
, num_sids
))) {
312 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
313 result
= NT_STATUS_NO_MEMORY
;
318 sids_left
= num_sids
;
319 hunk_domains
= domains
;
323 while (sids_left
> 0) {
325 NTSTATUS hunk_result
= NT_STATUS_OK
;
327 hunk_num_sids
= ((sids_left
> LOOKUP_SIDS_HUNK_SIZE
)
328 ? LOOKUP_SIDS_HUNK_SIZE
331 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
334 sids_processed
+ hunk_num_sids
- 1,
337 hunk_result
= rpccli_lsa_lookup_sids_noalloc(cli
,
347 if (!NT_STATUS_IS_OK(hunk_result
) &&
348 !NT_STATUS_EQUAL(hunk_result
, STATUS_SOME_UNMAPPED
) &&
349 !NT_STATUS_EQUAL(hunk_result
, NT_STATUS_NONE_MAPPED
))
351 /* An actual error occured */
352 result
= hunk_result
;
356 /* adapt overall result */
357 if (( NT_STATUS_IS_OK(result
) &&
358 !NT_STATUS_IS_OK(hunk_result
))
360 ( NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) &&
361 !NT_STATUS_EQUAL(hunk_result
, NT_STATUS_NONE_MAPPED
)))
363 result
= STATUS_SOME_UNMAPPED
;
366 sids_left
-= hunk_num_sids
;
367 sids_processed
+= hunk_num_sids
; /* only used in DEBUG */
368 hunk_sids
+= hunk_num_sids
;
369 hunk_domains
+= hunk_num_sids
;
370 hunk_names
+= hunk_num_sids
;
371 hunk_types
+= hunk_num_sids
;
380 TALLOC_FREE(domains
);
386 NTSTATUS
rpccli_lsa_lookup_sids(struct rpc_pipe_client
*cli
,
388 struct policy_handle
*pol
,
393 enum lsa_SidType
**ptypes
)
395 return rpccli_lsa_lookup_sids_generic(cli
, mem_ctx
, pol
, num_sids
, sids
,
396 pdomains
, pnames
, ptypes
, false);
399 NTSTATUS
rpccli_lsa_lookup_sids3(struct rpc_pipe_client
*cli
,
401 struct policy_handle
*pol
,
406 enum lsa_SidType
**ptypes
)
408 return rpccli_lsa_lookup_sids_generic(cli
, mem_ctx
, pol
, num_sids
, sids
,
409 pdomains
, pnames
, ptypes
, true);
412 /** Lookup a list of names */
414 static NTSTATUS
rpccli_lsa_lookup_names_generic(struct rpc_pipe_client
*cli
,
416 struct policy_handle
*pol
, int num_names
,
418 const char ***dom_names
,
421 enum lsa_SidType
**types
,
422 bool use_lookupnames4
)
426 struct lsa_String
*lsa_names
= NULL
;
427 struct lsa_RefDomainList
*domains
= NULL
;
428 struct lsa_TransSidArray sid_array
;
429 struct lsa_TransSidArray3 sid_array3
;
432 ZERO_STRUCT(sid_array
);
433 ZERO_STRUCT(sid_array3
);
435 lsa_names
= TALLOC_ARRAY(mem_ctx
, struct lsa_String
, num_names
);
437 return NT_STATUS_NO_MEMORY
;
440 for (i
=0; i
<num_names
; i
++) {
441 init_lsa_String(&lsa_names
[i
], names
[i
]);
444 if (use_lookupnames4
) {
445 result
= rpccli_lsa_LookupNames4(cli
, mem_ctx
,
455 result
= rpccli_lsa_LookupNames(cli
, mem_ctx
,
465 if (!NT_STATUS_IS_OK(result
) && NT_STATUS_V(result
) !=
466 NT_STATUS_V(STATUS_SOME_UNMAPPED
)) {
468 /* An actual error occured */
473 /* Return output parameters */
476 result
= NT_STATUS_NONE_MAPPED
;
481 if (!((*sids
= TALLOC_ARRAY(mem_ctx
, DOM_SID
, num_names
)))) {
482 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
483 result
= NT_STATUS_NO_MEMORY
;
487 if (!((*types
= TALLOC_ARRAY(mem_ctx
, enum lsa_SidType
, num_names
)))) {
488 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
489 result
= NT_STATUS_NO_MEMORY
;
493 if (dom_names
!= NULL
) {
494 *dom_names
= TALLOC_ARRAY(mem_ctx
, const char *, num_names
);
495 if (*dom_names
== NULL
) {
496 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
497 result
= NT_STATUS_NO_MEMORY
;
504 if (dom_names
!= NULL
) {
509 for (i
= 0; i
< num_names
; i
++) {
511 DOM_SID
*sid
= &(*sids
)[i
];
513 if (use_lookupnames4
) {
514 dom_idx
= sid_array3
.sids
[i
].sid_index
;
515 (*types
)[i
] = sid_array3
.sids
[i
].sid_type
;
517 dom_idx
= sid_array
.sids
[i
].sid_index
;
518 (*types
)[i
] = sid_array
.sids
[i
].sid_type
;
521 /* Translate optimised sid through domain index array */
523 if (dom_idx
== 0xffffffff) {
524 /* Nothing to do, this is unknown */
526 (*types
)[i
] = SID_NAME_UNKNOWN
;
530 if (use_lookupnames4
) {
531 sid_copy(sid
, sid_array3
.sids
[i
].sid
);
533 sid_copy(sid
, domains
->domains
[dom_idx
].sid
);
535 if (sid_array
.sids
[i
].rid
!= 0xffffffff) {
536 sid_append_rid(sid
, sid_array
.sids
[i
].rid
);
540 if (dom_names
== NULL
) {
544 (*dom_names
)[i
] = domains
->domains
[dom_idx
].name
.string
;
552 NTSTATUS
rpccli_lsa_lookup_names(struct rpc_pipe_client
*cli
,
554 struct policy_handle
*pol
, int num_names
,
556 const char ***dom_names
,
559 enum lsa_SidType
**types
)
561 return rpccli_lsa_lookup_names_generic(cli
, mem_ctx
, pol
, num_names
,
562 names
, dom_names
, level
, sids
,
566 NTSTATUS
rpccli_lsa_lookup_names4(struct rpc_pipe_client
*cli
,
568 struct policy_handle
*pol
, int num_names
,
570 const char ***dom_names
,
573 enum lsa_SidType
**types
)
575 return rpccli_lsa_lookup_names_generic(cli
, mem_ctx
, pol
, num_names
,
576 names
, dom_names
, level
, sids
,