s3-rpc_client: add rpccli_lsa_lookup_sids3 wrapper.
[Samba.git] / source / rpc_client / cli_lsarpc.c
blob00412bc5e4083488a4c29fe12be743819f8d475b
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
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/>.
25 #include "includes.h"
27 /** @defgroup lsa LSA - Local Security Architecture
28 * @ingroup rpc_client
30 * @{
31 **/
33 /**
34 * @file cli_lsarpc.c
36 * RPC client routines for the LSA RPC pipe. LSA means "local
37 * security authority", which is half of a password database.
38 **/
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,
45 TALLOC_CTX *mem_ctx,
46 bool sec_qos, uint32 des_access,
47 POLICY_HND *pol)
49 struct lsa_ObjectAttribute attr;
50 struct lsa_QosInfo qos;
51 uint16_t system_name = '\\';
53 if (sec_qos) {
54 init_lsa_sec_qos(&qos, 0xc, 2, 1, 0);
55 init_lsa_obj_attr(&attr,
56 0x18,
57 NULL,
58 NULL,
60 NULL,
61 &qos);
62 } else {
63 init_lsa_obj_attr(&attr,
64 0x18,
65 NULL,
66 NULL,
68 NULL,
69 NULL);
72 return rpccli_lsa_OpenPolicy(cli, mem_ctx,
73 &system_name,
74 &attr,
75 des_access,
76 pol);
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;
91 if (sec_qos) {
92 init_lsa_sec_qos(&qos, 0xc, 2, 1, 0);
93 init_lsa_obj_attr(&attr,
94 0x18,
95 NULL,
96 NULL,
98 NULL,
99 &qos);
100 } else {
101 init_lsa_obj_attr(&attr,
102 0x18,
103 NULL,
104 NULL,
106 NULL,
107 NULL);
110 return rpccli_lsa_OpenPolicy2(cli, mem_ctx,
111 cli->srv_name_slash,
112 &attr,
113 des_access,
114 pol);
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,
123 TALLOC_CTX *mem_ctx,
124 POLICY_HND *pol,
125 int num_sids,
126 const DOM_SID *sids,
127 char **domains,
128 char **names,
129 enum lsa_SidType *types,
130 bool use_lookupsids3)
132 NTSTATUS result = NT_STATUS_OK;
133 TALLOC_CTX *tmp_ctx = NULL;
134 int i;
135 struct lsa_SidArray sid_array;
136 struct lsa_RefDomainList *ref_domains = NULL;
137 struct lsa_TransNameArray lsa_names;
138 uint32_t count = 0;
139 uint16_t level = 1;
141 ZERO_STRUCT(lsa_names);
143 tmp_ctx = talloc_new(mem_ctx);
144 if (!tmp_ctx) {
145 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
146 result = NT_STATUS_UNSUCCESSFUL;
147 goto done;
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;
165 uint32_t n;
167 result = rpccli_lsa_LookupSids3(cli, mem_ctx,
168 &sid_array,
169 &ref_domains,
170 &lsa_names2,
171 level,
172 &count,
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;
189 } else {
190 result = rpccli_lsa_LookupSids(cli, mem_ctx,
191 pol,
192 &sid_array,
193 &ref_domains,
194 &lsa_names,
195 level,
196 &count);
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 */
207 goto done;
210 /* Return output parameters */
212 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
213 (count == 0))
215 for (i = 0; i < num_sids; i++) {
216 (names)[i] = NULL;
217 (domains)[i] = NULL;
218 (types)[i] = SID_NAME_UNKNOWN;
220 result = NT_STATUS_NONE_MAPPED;
221 goto done;
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;
235 if (name) {
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;
240 goto done;
242 } else {
243 (names)[i] = NULL;
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;
250 goto done;
253 } else {
254 (names)[i] = NULL;
255 (domains)[i] = NULL;
256 (types)[i] = SID_NAME_UNKNOWN;
260 done:
261 TALLOC_FREE(tmp_ctx);
262 return result;
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,
278 TALLOC_CTX *mem_ctx,
279 struct policy_handle *pol,
280 int num_sids,
281 const DOM_SID *sids,
282 char ***pdomains,
283 char ***pnames,
284 enum lsa_SidType **ptypes,
285 bool use_lookupsids3)
287 NTSTATUS result = NT_STATUS_OK;
288 int sids_left = 0;
289 int sids_processed = 0;
290 const DOM_SID *hunk_sids = sids;
291 char **hunk_domains;
292 char **hunk_names;
293 enum lsa_SidType *hunk_types;
294 char **domains = NULL;
295 char **names = NULL;
296 enum lsa_SidType *types = NULL;
298 if (num_sids) {
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;
302 goto fail;
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;
308 goto fail;
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;
314 goto fail;
318 sids_left = num_sids;
319 hunk_domains = domains;
320 hunk_names = names;
321 hunk_types = types;
323 while (sids_left > 0) {
324 int hunk_num_sids;
325 NTSTATUS hunk_result = NT_STATUS_OK;
327 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
328 ? LOOKUP_SIDS_HUNK_SIZE
329 : sids_left);
331 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
332 "%d -- %d of %d.\n",
333 sids_processed,
334 sids_processed + hunk_num_sids - 1,
335 num_sids));
337 hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
338 mem_ctx,
339 pol,
340 hunk_num_sids,
341 hunk_sids,
342 hunk_domains,
343 hunk_names,
344 hunk_types,
345 use_lookupsids3);
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;
353 goto fail;
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;
374 *pdomains = domains;
375 *pnames = names;
376 *ptypes = types;
377 return result;
379 fail:
380 TALLOC_FREE(domains);
381 TALLOC_FREE(names);
382 TALLOC_FREE(types);
383 return result;
386 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
387 TALLOC_CTX *mem_ctx,
388 struct policy_handle *pol,
389 int num_sids,
390 const DOM_SID *sids,
391 char ***pdomains,
392 char ***pnames,
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,
400 TALLOC_CTX *mem_ctx,
401 struct policy_handle *pol,
402 int num_sids,
403 const DOM_SID *sids,
404 char ***pdomains,
405 char ***pnames,
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,
415 TALLOC_CTX *mem_ctx,
416 struct policy_handle *pol, int num_names,
417 const char **names,
418 const char ***dom_names,
419 int level,
420 DOM_SID **sids,
421 enum lsa_SidType **types,
422 bool use_lookupnames4)
424 NTSTATUS result;
425 int i;
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;
430 uint32_t count = 0;
432 ZERO_STRUCT(sid_array);
433 ZERO_STRUCT(sid_array3);
435 lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
436 if (!lsa_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,
446 num_names,
447 lsa_names,
448 &domains,
449 &sid_array3,
450 level,
451 &count,
454 } else {
455 result = rpccli_lsa_LookupNames(cli, mem_ctx,
456 pol,
457 num_names,
458 lsa_names,
459 &domains,
460 &sid_array,
461 level,
462 &count);
465 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
466 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
468 /* An actual error occured */
470 goto done;
473 /* Return output parameters */
475 if (count == 0) {
476 result = NT_STATUS_NONE_MAPPED;
477 goto done;
480 if (num_names) {
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;
484 goto done;
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;
490 goto done;
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;
498 goto done;
501 } else {
502 *sids = NULL;
503 *types = NULL;
504 if (dom_names != NULL) {
505 *dom_names = NULL;
509 for (i = 0; i < num_names; i++) {
510 uint32_t dom_idx;
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;
516 } else {
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 */
525 ZERO_STRUCTP(sid);
526 (*types)[i] = SID_NAME_UNKNOWN;
527 continue;
530 if (use_lookupnames4) {
531 sid_copy(sid, sid_array3.sids[i].sid);
532 } else {
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) {
541 continue;
544 (*dom_names)[i] = domains->domains[dom_idx].name.string;
547 done:
549 return result;
552 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
553 TALLOC_CTX *mem_ctx,
554 struct policy_handle *pol, int num_names,
555 const char **names,
556 const char ***dom_names,
557 int level,
558 DOM_SID **sids,
559 enum lsa_SidType **types)
561 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
562 names, dom_names, level, sids,
563 types, false);
566 NTSTATUS rpccli_lsa_lookup_names4(struct rpc_pipe_client *cli,
567 TALLOC_CTX *mem_ctx,
568 struct policy_handle *pol, int num_names,
569 const char **names,
570 const char ***dom_names,
571 int level,
572 DOM_SID **sids,
573 enum lsa_SidType **types)
575 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
576 names, dom_names, level, sids,
577 types, true);