WHATSNEW. Update changes since 3.3.8.
[Samba.git] / source / rpc_client / cli_lsarpc.c
blobb2ae16724f62687865dd3edba6adf6d63d18c540
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 ZERO_STRUCT(lsa_names2);
169 result = rpccli_lsa_LookupSids3(cli, mem_ctx,
170 &sid_array,
171 &ref_domains,
172 &lsa_names2,
173 level,
174 &count,
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;
191 } else {
192 result = rpccli_lsa_LookupSids(cli, mem_ctx,
193 pol,
194 &sid_array,
195 &ref_domains,
196 &lsa_names,
197 level,
198 &count);
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 */
209 goto done;
212 /* Return output parameters */
214 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
215 (count == 0))
217 for (i = 0; i < num_sids; i++) {
218 (names)[i] = NULL;
219 (domains)[i] = NULL;
220 (types)[i] = SID_NAME_UNKNOWN;
222 result = NT_STATUS_NONE_MAPPED;
223 goto done;
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;
237 if (name) {
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;
242 goto done;
244 } else {
245 (names)[i] = NULL;
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;
252 goto done;
255 } else {
256 (names)[i] = NULL;
257 (domains)[i] = NULL;
258 (types)[i] = SID_NAME_UNKNOWN;
262 done:
263 TALLOC_FREE(tmp_ctx);
264 return result;
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,
280 TALLOC_CTX *mem_ctx,
281 struct policy_handle *pol,
282 int num_sids,
283 const DOM_SID *sids,
284 char ***pdomains,
285 char ***pnames,
286 enum lsa_SidType **ptypes,
287 bool use_lookupsids3)
289 NTSTATUS result = NT_STATUS_OK;
290 int sids_left = 0;
291 int sids_processed = 0;
292 const DOM_SID *hunk_sids = sids;
293 char **hunk_domains;
294 char **hunk_names;
295 enum lsa_SidType *hunk_types;
296 char **domains = NULL;
297 char **names = NULL;
298 enum lsa_SidType *types = NULL;
300 if (num_sids) {
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;
304 goto fail;
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;
310 goto fail;
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;
316 goto fail;
320 sids_left = num_sids;
321 hunk_domains = domains;
322 hunk_names = names;
323 hunk_types = types;
325 while (sids_left > 0) {
326 int hunk_num_sids;
327 NTSTATUS hunk_result = NT_STATUS_OK;
329 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
330 ? LOOKUP_SIDS_HUNK_SIZE
331 : sids_left);
333 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
334 "%d -- %d of %d.\n",
335 sids_processed,
336 sids_processed + hunk_num_sids - 1,
337 num_sids));
339 hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
340 mem_ctx,
341 pol,
342 hunk_num_sids,
343 hunk_sids,
344 hunk_domains,
345 hunk_names,
346 hunk_types,
347 use_lookupsids3);
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;
355 goto fail;
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;
376 *pdomains = domains;
377 *pnames = names;
378 *ptypes = types;
379 return result;
381 fail:
382 TALLOC_FREE(domains);
383 TALLOC_FREE(names);
384 TALLOC_FREE(types);
385 return result;
388 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
389 TALLOC_CTX *mem_ctx,
390 struct policy_handle *pol,
391 int num_sids,
392 const DOM_SID *sids,
393 char ***pdomains,
394 char ***pnames,
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,
402 TALLOC_CTX *mem_ctx,
403 struct policy_handle *pol,
404 int num_sids,
405 const DOM_SID *sids,
406 char ***pdomains,
407 char ***pnames,
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,
417 TALLOC_CTX *mem_ctx,
418 struct policy_handle *pol, int num_names,
419 const char **names,
420 const char ***dom_names,
421 int level,
422 DOM_SID **sids,
423 enum lsa_SidType **types,
424 bool use_lookupnames4)
426 NTSTATUS result;
427 int i;
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;
432 uint32_t count = 0;
434 ZERO_STRUCT(sid_array);
435 ZERO_STRUCT(sid_array3);
437 lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
438 if (!lsa_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,
448 num_names,
449 lsa_names,
450 &domains,
451 &sid_array3,
452 level,
453 &count,
456 } else {
457 result = rpccli_lsa_LookupNames(cli, mem_ctx,
458 pol,
459 num_names,
460 lsa_names,
461 &domains,
462 &sid_array,
463 level,
464 &count);
467 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
468 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
470 /* An actual error occured */
472 goto done;
475 /* Return output parameters */
477 if (count == 0) {
478 result = NT_STATUS_NONE_MAPPED;
479 goto done;
482 if (num_names) {
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;
486 goto done;
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;
492 goto done;
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;
500 goto done;
503 } else {
504 *sids = NULL;
505 *types = NULL;
506 if (dom_names != NULL) {
507 *dom_names = NULL;
511 for (i = 0; i < num_names; i++) {
512 uint32_t dom_idx;
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;
518 } else {
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 */
527 ZERO_STRUCTP(sid);
528 (*types)[i] = SID_NAME_UNKNOWN;
529 continue;
532 if (use_lookupnames4) {
533 sid_copy(sid, sid_array3.sids[i].sid);
534 } else {
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) {
543 continue;
546 (*dom_names)[i] = domains->domains[dom_idx].name.string;
549 done:
551 return result;
554 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
555 TALLOC_CTX *mem_ctx,
556 struct policy_handle *pol, int num_names,
557 const char **names,
558 const char ***dom_names,
559 int level,
560 DOM_SID **sids,
561 enum lsa_SidType **types)
563 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
564 names, dom_names, level, sids,
565 types, false);
568 NTSTATUS rpccli_lsa_lookup_names4(struct rpc_pipe_client *cli,
569 TALLOC_CTX *mem_ctx,
570 struct policy_handle *pol, int num_names,
571 const char **names,
572 const char ***dom_names,
573 int level,
574 DOM_SID **sids,
575 enum lsa_SidType **types)
577 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
578 names, dom_names, level, sids,
579 types, true);