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"
27 #include "rpc_client/cli_lsarpc.h"
29 /** @defgroup lsa LSA - Local Security Architecture
38 * RPC client routines for the LSA RPC pipe. LSA means "local
39 * security authority", which is half of a password database.
42 /** Open a LSA policy handle
44 * @param cli Handle on an initialised SMB connection */
46 NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
48 bool sec_qos, uint32 des_access,
49 struct policy_handle *pol)
51 struct lsa_ObjectAttribute attr;
52 struct lsa_QosInfo qos;
53 uint16_t system_name = '\\';
61 qos.impersonation_level = 2;
63 qos.effective_only = 0;
68 return rpccli_lsa_OpenPolicy(cli, mem_ctx,
75 /** Open a LSA policy handle
77 * @param cli Handle on an initialised SMB connection
80 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
81 TALLOC_CTX *mem_ctx, bool sec_qos,
82 uint32 des_access, struct policy_handle *pol)
84 struct lsa_ObjectAttribute attr;
85 struct lsa_QosInfo qos;
93 qos.impersonation_level = 2;
95 qos.effective_only = 0;
100 return rpccli_lsa_OpenPolicy2(cli, mem_ctx,
107 /* Lookup a list of sids
109 * internal version withOUT memory allocation of the target arrays.
110 * this assumes suffciently sized arrays to store domains, names and types. */
112 static NTSTATUS rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client *cli,
114 struct policy_handle *pol,
119 enum lsa_SidType *types,
120 bool use_lookupsids3)
122 NTSTATUS result = NT_STATUS_OK;
123 TALLOC_CTX *tmp_ctx = NULL;
125 struct lsa_SidArray sid_array;
126 struct lsa_RefDomainList *ref_domains = NULL;
127 struct lsa_TransNameArray lsa_names;
131 ZERO_STRUCT(lsa_names);
133 tmp_ctx = talloc_new(mem_ctx);
135 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
136 result = NT_STATUS_UNSUCCESSFUL;
140 sid_array.num_sids = num_sids;
141 sid_array.sids = TALLOC_ARRAY(mem_ctx, struct lsa_SidPtr, num_sids);
142 if (!sid_array.sids) {
143 return NT_STATUS_NO_MEMORY;
146 for (i = 0; i<num_sids; i++) {
147 sid_array.sids[i].sid = sid_dup_talloc(mem_ctx, &sids[i]);
148 if (!sid_array.sids[i].sid) {
149 return NT_STATUS_NO_MEMORY;
153 if (use_lookupsids3) {
154 struct lsa_TransNameArray2 lsa_names2;
157 ZERO_STRUCT(lsa_names2);
159 result = rpccli_lsa_LookupSids3(cli, mem_ctx,
168 if (!NT_STATUS_IS_ERR(result)) {
169 lsa_names.count = lsa_names2.count;
170 lsa_names.names = talloc_array(mem_ctx, struct lsa_TranslatedName, lsa_names.count);
171 if (!lsa_names.names) {
172 return NT_STATUS_NO_MEMORY;
174 for (n=0; n < lsa_names.count; n++) {
175 lsa_names.names[n].sid_type = lsa_names2.names[n].sid_type;
176 lsa_names.names[n].name = lsa_names2.names[n].name;
177 lsa_names.names[n].sid_index = lsa_names2.names[n].sid_index;
182 result = rpccli_lsa_LookupSids(cli, mem_ctx,
191 DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
192 nt_errstr(result), count));
194 if (!NT_STATUS_IS_OK(result) &&
195 !NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
196 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
198 /* An actual error occured */
202 /* Return output parameters */
204 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
207 for (i = 0; i < num_sids; i++) {
210 (types)[i] = SID_NAME_UNKNOWN;
212 result = NT_STATUS_NONE_MAPPED;
216 for (i = 0; i < num_sids; i++) {
217 const char *name, *dom_name;
218 uint32_t dom_idx = lsa_names.names[i].sid_index;
220 /* Translate optimised name through domain index array */
222 if (dom_idx != 0xffffffff) {
224 dom_name = ref_domains->domains[dom_idx].name.string;
225 name = lsa_names.names[i].name.string;
228 (names)[i] = talloc_strdup(mem_ctx, name);
229 if ((names)[i] == NULL) {
230 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
231 result = NT_STATUS_UNSUCCESSFUL;
237 domains[i] = talloc_strdup(
238 mem_ctx, dom_name ? dom_name : "");
239 (types)[i] = lsa_names.names[i].sid_type;
240 if (((domains)[i] == NULL)) {
241 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
242 result = NT_STATUS_UNSUCCESSFUL;
249 (types)[i] = SID_NAME_UNKNOWN;
254 TALLOC_FREE(tmp_ctx);
258 /* Lookup a list of sids
260 * do it the right way: there is a limit (of 20480 for w2k3) entries
261 * returned by this call. when the sids list contains more entries,
262 * empty lists are returned. This version of lsa_lookup_sids passes
263 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
265 /* This constant defines the limit of how many sids to look up
266 * in one call (maximum). the limit from the server side is
267 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
268 #define LOOKUP_SIDS_HUNK_SIZE 1000
270 static NTSTATUS rpccli_lsa_lookup_sids_generic(struct rpc_pipe_client *cli,
272 struct policy_handle *pol,
277 enum lsa_SidType **ptypes,
278 bool use_lookupsids3)
280 NTSTATUS result = NT_STATUS_OK;
282 int sids_processed = 0;
283 const DOM_SID *hunk_sids = sids;
286 enum lsa_SidType *hunk_types;
287 char **domains = NULL;
289 enum lsa_SidType *types = NULL;
292 if (!(domains = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
293 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
294 result = NT_STATUS_NO_MEMORY;
298 if (!(names = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
299 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
300 result = NT_STATUS_NO_MEMORY;
304 if (!(types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
305 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
306 result = NT_STATUS_NO_MEMORY;
311 sids_left = num_sids;
312 hunk_domains = domains;
316 while (sids_left > 0) {
318 NTSTATUS hunk_result = NT_STATUS_OK;
320 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
321 ? LOOKUP_SIDS_HUNK_SIZE
324 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
327 sids_processed + hunk_num_sids - 1,
330 hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
340 if (!NT_STATUS_IS_OK(hunk_result) &&
341 !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
342 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
344 /* An actual error occured */
345 result = hunk_result;
349 /* adapt overall result */
350 if (( NT_STATUS_IS_OK(result) &&
351 !NT_STATUS_IS_OK(hunk_result))
353 ( NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
354 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)))
356 result = STATUS_SOME_UNMAPPED;
359 sids_left -= hunk_num_sids;
360 sids_processed += hunk_num_sids; /* only used in DEBUG */
361 hunk_sids += hunk_num_sids;
362 hunk_domains += hunk_num_sids;
363 hunk_names += hunk_num_sids;
364 hunk_types += hunk_num_sids;
373 TALLOC_FREE(domains);
379 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
381 struct policy_handle *pol,
386 enum lsa_SidType **ptypes)
388 return rpccli_lsa_lookup_sids_generic(cli, mem_ctx, pol, num_sids, sids,
389 pdomains, pnames, ptypes, false);
392 NTSTATUS rpccli_lsa_lookup_sids3(struct rpc_pipe_client *cli,
394 struct policy_handle *pol,
399 enum lsa_SidType **ptypes)
401 return rpccli_lsa_lookup_sids_generic(cli, mem_ctx, pol, num_sids, sids,
402 pdomains, pnames, ptypes, true);
405 /** Lookup a list of names */
407 static NTSTATUS rpccli_lsa_lookup_names_generic(struct rpc_pipe_client *cli,
409 struct policy_handle *pol, int num_names,
411 const char ***dom_names,
414 enum lsa_SidType **types,
415 bool use_lookupnames4)
419 struct lsa_String *lsa_names = NULL;
420 struct lsa_RefDomainList *domains = NULL;
421 struct lsa_TransSidArray sid_array;
422 struct lsa_TransSidArray3 sid_array3;
425 ZERO_STRUCT(sid_array);
426 ZERO_STRUCT(sid_array3);
428 lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
430 return NT_STATUS_NO_MEMORY;
433 for (i=0; i<num_names; i++) {
434 init_lsa_String(&lsa_names[i], names[i]);
437 if (use_lookupnames4) {
438 result = rpccli_lsa_LookupNames4(cli, mem_ctx,
448 result = rpccli_lsa_LookupNames(cli, mem_ctx,
458 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
459 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
461 /* An actual error occured */
466 /* Return output parameters */
469 result = NT_STATUS_NONE_MAPPED;
474 if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
475 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
476 result = NT_STATUS_NO_MEMORY;
480 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
481 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
482 result = NT_STATUS_NO_MEMORY;
486 if (dom_names != NULL) {
487 *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
488 if (*dom_names == NULL) {
489 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
490 result = NT_STATUS_NO_MEMORY;
497 if (dom_names != NULL) {
502 for (i = 0; i < num_names; i++) {
504 DOM_SID *sid = &(*sids)[i];
506 if (use_lookupnames4) {
507 dom_idx = sid_array3.sids[i].sid_index;
508 (*types)[i] = sid_array3.sids[i].sid_type;
510 dom_idx = sid_array.sids[i].sid_index;
511 (*types)[i] = sid_array.sids[i].sid_type;
514 /* Translate optimised sid through domain index array */
516 if (dom_idx == 0xffffffff) {
517 /* Nothing to do, this is unknown */
519 (*types)[i] = SID_NAME_UNKNOWN;
523 if (use_lookupnames4) {
524 sid_copy(sid, sid_array3.sids[i].sid);
526 sid_copy(sid, domains->domains[dom_idx].sid);
528 if (sid_array.sids[i].rid != 0xffffffff) {
529 sid_append_rid(sid, sid_array.sids[i].rid);
533 if (dom_names == NULL) {
537 (*dom_names)[i] = domains->domains[dom_idx].name.string;
545 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
547 struct policy_handle *pol, int num_names,
549 const char ***dom_names,
552 enum lsa_SidType **types)
554 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
555 names, dom_names, level, sids,
559 NTSTATUS rpccli_lsa_lookup_names4(struct rpc_pipe_client *cli,
561 struct policy_handle *pol, int num_names,
563 const char ***dom_names,
566 enum lsa_SidType **types)
568 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
569 names, dom_names, level, sids,