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 "../librpc/gen_ndr/cli_lsa.h"
28 /** @defgroup lsa LSA - Local Security Architecture
37 * RPC client routines for the LSA RPC pipe. LSA means "local
38 * security authority", which is half of a password database.
41 /** Open a LSA policy handle
43 * @param cli Handle on an initialised SMB connection */
45 NTSTATUS
rpccli_lsa_open_policy(struct rpc_pipe_client
*cli
,
47 bool sec_qos
, uint32 des_access
,
48 struct policy_handle
*pol
)
50 struct lsa_ObjectAttribute attr
;
51 struct lsa_QosInfo qos
;
52 uint16_t system_name
= '\\';
60 qos
.impersonation_level
= 2;
62 qos
.effective_only
= 0;
67 return rpccli_lsa_OpenPolicy(cli
, mem_ctx
,
74 /** Open a LSA policy handle
76 * @param cli Handle on an initialised SMB connection
79 NTSTATUS
rpccli_lsa_open_policy2(struct rpc_pipe_client
*cli
,
80 TALLOC_CTX
*mem_ctx
, bool sec_qos
,
81 uint32 des_access
, struct policy_handle
*pol
)
83 struct lsa_ObjectAttribute attr
;
84 struct lsa_QosInfo qos
;
92 qos
.impersonation_level
= 2;
94 qos
.effective_only
= 0;
99 return rpccli_lsa_OpenPolicy2(cli
, mem_ctx
,
106 /* Lookup a list of sids
108 * internal version withOUT memory allocation of the target arrays.
109 * this assumes suffciently sized arrays to store domains, names and types. */
111 static NTSTATUS
rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client
*cli
,
113 struct policy_handle
*pol
,
118 enum lsa_SidType
*types
,
119 bool use_lookupsids3
)
121 NTSTATUS result
= NT_STATUS_OK
;
122 TALLOC_CTX
*tmp_ctx
= NULL
;
124 struct lsa_SidArray sid_array
;
125 struct lsa_RefDomainList
*ref_domains
= NULL
;
126 struct lsa_TransNameArray lsa_names
;
130 ZERO_STRUCT(lsa_names
);
132 tmp_ctx
= talloc_new(mem_ctx
);
134 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
135 result
= NT_STATUS_UNSUCCESSFUL
;
139 sid_array
.num_sids
= num_sids
;
140 sid_array
.sids
= TALLOC_ARRAY(mem_ctx
, struct lsa_SidPtr
, num_sids
);
141 if (!sid_array
.sids
) {
142 return NT_STATUS_NO_MEMORY
;
145 for (i
= 0; i
<num_sids
; i
++) {
146 sid_array
.sids
[i
].sid
= sid_dup_talloc(mem_ctx
, &sids
[i
]);
147 if (!sid_array
.sids
[i
].sid
) {
148 return NT_STATUS_NO_MEMORY
;
152 if (use_lookupsids3
) {
153 struct lsa_TransNameArray2 lsa_names2
;
156 ZERO_STRUCT(lsa_names2
);
158 result
= rpccli_lsa_LookupSids3(cli
, mem_ctx
,
167 if (!NT_STATUS_IS_ERR(result
)) {
168 lsa_names
.count
= lsa_names2
.count
;
169 lsa_names
.names
= talloc_array(mem_ctx
, struct lsa_TranslatedName
, lsa_names
.count
);
170 if (!lsa_names
.names
) {
171 return NT_STATUS_NO_MEMORY
;
173 for (n
=0; n
< lsa_names
.count
; n
++) {
174 lsa_names
.names
[n
].sid_type
= lsa_names2
.names
[n
].sid_type
;
175 lsa_names
.names
[n
].name
= lsa_names2
.names
[n
].name
;
176 lsa_names
.names
[n
].sid_index
= lsa_names2
.names
[n
].sid_index
;
181 result
= rpccli_lsa_LookupSids(cli
, mem_ctx
,
190 DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
191 nt_errstr(result
), count
));
193 if (!NT_STATUS_IS_OK(result
) &&
194 !NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) &&
195 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
197 /* An actual error occured */
201 /* Return output parameters */
203 if (NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) ||
206 for (i
= 0; i
< num_sids
; i
++) {
209 (types
)[i
] = SID_NAME_UNKNOWN
;
211 result
= NT_STATUS_NONE_MAPPED
;
215 for (i
= 0; i
< num_sids
; i
++) {
216 const char *name
, *dom_name
;
217 uint32_t dom_idx
= lsa_names
.names
[i
].sid_index
;
219 /* Translate optimised name through domain index array */
221 if (dom_idx
!= 0xffffffff) {
223 dom_name
= ref_domains
->domains
[dom_idx
].name
.string
;
224 name
= lsa_names
.names
[i
].name
.string
;
227 (names
)[i
] = talloc_strdup(mem_ctx
, name
);
228 if ((names
)[i
] == NULL
) {
229 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
230 result
= NT_STATUS_UNSUCCESSFUL
;
236 domains
[i
] = talloc_strdup(
237 mem_ctx
, dom_name
? dom_name
: "");
238 (types
)[i
] = lsa_names
.names
[i
].sid_type
;
239 if (((domains
)[i
] == NULL
)) {
240 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
241 result
= NT_STATUS_UNSUCCESSFUL
;
248 (types
)[i
] = SID_NAME_UNKNOWN
;
253 TALLOC_FREE(tmp_ctx
);
257 /* Lookup a list of sids
259 * do it the right way: there is a limit (of 20480 for w2k3) entries
260 * returned by this call. when the sids list contains more entries,
261 * empty lists are returned. This version of lsa_lookup_sids passes
262 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
264 /* This constant defines the limit of how many sids to look up
265 * in one call (maximum). the limit from the server side is
266 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
267 #define LOOKUP_SIDS_HUNK_SIZE 1000
269 static NTSTATUS
rpccli_lsa_lookup_sids_generic(struct rpc_pipe_client
*cli
,
271 struct policy_handle
*pol
,
276 enum lsa_SidType
**ptypes
,
277 bool use_lookupsids3
)
279 NTSTATUS result
= NT_STATUS_OK
;
281 int sids_processed
= 0;
282 const DOM_SID
*hunk_sids
= sids
;
285 enum lsa_SidType
*hunk_types
;
286 char **domains
= NULL
;
288 enum lsa_SidType
*types
= NULL
;
291 if (!(domains
= TALLOC_ARRAY(mem_ctx
, char *, num_sids
))) {
292 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
293 result
= NT_STATUS_NO_MEMORY
;
297 if (!(names
= TALLOC_ARRAY(mem_ctx
, char *, num_sids
))) {
298 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
299 result
= NT_STATUS_NO_MEMORY
;
303 if (!(types
= TALLOC_ARRAY(mem_ctx
, enum lsa_SidType
, num_sids
))) {
304 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
305 result
= NT_STATUS_NO_MEMORY
;
310 sids_left
= num_sids
;
311 hunk_domains
= domains
;
315 while (sids_left
> 0) {
317 NTSTATUS hunk_result
= NT_STATUS_OK
;
319 hunk_num_sids
= ((sids_left
> LOOKUP_SIDS_HUNK_SIZE
)
320 ? LOOKUP_SIDS_HUNK_SIZE
323 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
326 sids_processed
+ hunk_num_sids
- 1,
329 hunk_result
= rpccli_lsa_lookup_sids_noalloc(cli
,
339 if (!NT_STATUS_IS_OK(hunk_result
) &&
340 !NT_STATUS_EQUAL(hunk_result
, STATUS_SOME_UNMAPPED
) &&
341 !NT_STATUS_EQUAL(hunk_result
, NT_STATUS_NONE_MAPPED
))
343 /* An actual error occured */
344 result
= hunk_result
;
348 /* adapt overall result */
349 if (( NT_STATUS_IS_OK(result
) &&
350 !NT_STATUS_IS_OK(hunk_result
))
352 ( NT_STATUS_EQUAL(result
, NT_STATUS_NONE_MAPPED
) &&
353 !NT_STATUS_EQUAL(hunk_result
, NT_STATUS_NONE_MAPPED
)))
355 result
= STATUS_SOME_UNMAPPED
;
358 sids_left
-= hunk_num_sids
;
359 sids_processed
+= hunk_num_sids
; /* only used in DEBUG */
360 hunk_sids
+= hunk_num_sids
;
361 hunk_domains
+= hunk_num_sids
;
362 hunk_names
+= hunk_num_sids
;
363 hunk_types
+= hunk_num_sids
;
372 TALLOC_FREE(domains
);
378 NTSTATUS
rpccli_lsa_lookup_sids(struct rpc_pipe_client
*cli
,
380 struct policy_handle
*pol
,
385 enum lsa_SidType
**ptypes
)
387 return rpccli_lsa_lookup_sids_generic(cli
, mem_ctx
, pol
, num_sids
, sids
,
388 pdomains
, pnames
, ptypes
, false);
391 NTSTATUS
rpccli_lsa_lookup_sids3(struct rpc_pipe_client
*cli
,
393 struct policy_handle
*pol
,
398 enum lsa_SidType
**ptypes
)
400 return rpccli_lsa_lookup_sids_generic(cli
, mem_ctx
, pol
, num_sids
, sids
,
401 pdomains
, pnames
, ptypes
, true);
404 /** Lookup a list of names */
406 static NTSTATUS
rpccli_lsa_lookup_names_generic(struct rpc_pipe_client
*cli
,
408 struct policy_handle
*pol
, int num_names
,
410 const char ***dom_names
,
413 enum lsa_SidType
**types
,
414 bool use_lookupnames4
)
418 struct lsa_String
*lsa_names
= NULL
;
419 struct lsa_RefDomainList
*domains
= NULL
;
420 struct lsa_TransSidArray sid_array
;
421 struct lsa_TransSidArray3 sid_array3
;
424 ZERO_STRUCT(sid_array
);
425 ZERO_STRUCT(sid_array3
);
427 lsa_names
= TALLOC_ARRAY(mem_ctx
, struct lsa_String
, num_names
);
429 return NT_STATUS_NO_MEMORY
;
432 for (i
=0; i
<num_names
; i
++) {
433 init_lsa_String(&lsa_names
[i
], names
[i
]);
436 if (use_lookupnames4
) {
437 result
= rpccli_lsa_LookupNames4(cli
, mem_ctx
,
447 result
= rpccli_lsa_LookupNames(cli
, mem_ctx
,
457 if (!NT_STATUS_IS_OK(result
) && NT_STATUS_V(result
) !=
458 NT_STATUS_V(STATUS_SOME_UNMAPPED
)) {
460 /* An actual error occured */
465 /* Return output parameters */
468 result
= NT_STATUS_NONE_MAPPED
;
473 if (!((*sids
= TALLOC_ARRAY(mem_ctx
, DOM_SID
, num_names
)))) {
474 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
475 result
= NT_STATUS_NO_MEMORY
;
479 if (!((*types
= TALLOC_ARRAY(mem_ctx
, enum lsa_SidType
, num_names
)))) {
480 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
481 result
= NT_STATUS_NO_MEMORY
;
485 if (dom_names
!= NULL
) {
486 *dom_names
= TALLOC_ARRAY(mem_ctx
, const char *, num_names
);
487 if (*dom_names
== NULL
) {
488 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
489 result
= NT_STATUS_NO_MEMORY
;
496 if (dom_names
!= NULL
) {
501 for (i
= 0; i
< num_names
; i
++) {
503 DOM_SID
*sid
= &(*sids
)[i
];
505 if (use_lookupnames4
) {
506 dom_idx
= sid_array3
.sids
[i
].sid_index
;
507 (*types
)[i
] = sid_array3
.sids
[i
].sid_type
;
509 dom_idx
= sid_array
.sids
[i
].sid_index
;
510 (*types
)[i
] = sid_array
.sids
[i
].sid_type
;
513 /* Translate optimised sid through domain index array */
515 if (dom_idx
== 0xffffffff) {
516 /* Nothing to do, this is unknown */
518 (*types
)[i
] = SID_NAME_UNKNOWN
;
522 if (use_lookupnames4
) {
523 sid_copy(sid
, sid_array3
.sids
[i
].sid
);
525 sid_copy(sid
, domains
->domains
[dom_idx
].sid
);
527 if (sid_array
.sids
[i
].rid
!= 0xffffffff) {
528 sid_append_rid(sid
, sid_array
.sids
[i
].rid
);
532 if (dom_names
== NULL
) {
536 (*dom_names
)[i
] = domains
->domains
[dom_idx
].name
.string
;
544 NTSTATUS
rpccli_lsa_lookup_names(struct rpc_pipe_client
*cli
,
546 struct policy_handle
*pol
, int num_names
,
548 const char ***dom_names
,
551 enum lsa_SidType
**types
)
553 return rpccli_lsa_lookup_names_generic(cli
, mem_ctx
, pol
, num_names
,
554 names
, dom_names
, level
, sids
,
558 NTSTATUS
rpccli_lsa_lookup_names4(struct rpc_pipe_client
*cli
,
560 struct policy_handle
*pol
, int num_names
,
562 const char ***dom_names
,
565 enum lsa_SidType
**types
)
567 return rpccli_lsa_lookup_names_generic(cli
, mem_ctx
, pol
, num_names
,
568 names
, dom_names
, level
, sids
,