s4:upgradeschema.py Cleanup
[Samba/ekacnet.git] / source3 / rpc_client / cli_lsarpc.c
blob74fd08276c35e45b67bcff2fe7bdd5d01dbd660e
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 struct policy_handle *pol)
49 struct lsa_ObjectAttribute attr;
50 struct lsa_QosInfo qos;
51 uint16_t system_name = '\\';
53 ZERO_STRUCT(attr);
55 attr.len = 0x18;
57 if (sec_qos) {
58 qos.len = 0xc;
59 qos.impersonation_level = 2;
60 qos.context_mode = 1;
61 qos.effective_only = 0;
63 attr.sec_qos = &qos;
66 return rpccli_lsa_OpenPolicy(cli, mem_ctx,
67 &system_name,
68 &attr,
69 des_access,
70 pol);
73 /** Open a LSA policy handle
75 * @param cli Handle on an initialised SMB connection
78 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
79 TALLOC_CTX *mem_ctx, bool sec_qos,
80 uint32 des_access, struct policy_handle *pol)
82 struct lsa_ObjectAttribute attr;
83 struct lsa_QosInfo qos;
85 ZERO_STRUCT(attr);
87 attr.len = 0x18;
89 if (sec_qos) {
90 qos.len = 0xc;
91 qos.impersonation_level = 2;
92 qos.context_mode = 1;
93 qos.effective_only = 0;
95 attr.sec_qos = &qos;
98 return rpccli_lsa_OpenPolicy2(cli, mem_ctx,
99 cli->srv_name_slash,
100 &attr,
101 des_access,
102 pol);
105 /* Lookup a list of sids
107 * internal version withOUT memory allocation of the target arrays.
108 * this assumes suffciently sized arrays to store domains, names and types. */
110 static NTSTATUS rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client *cli,
111 TALLOC_CTX *mem_ctx,
112 struct policy_handle *pol,
113 int num_sids,
114 const DOM_SID *sids,
115 char **domains,
116 char **names,
117 enum lsa_SidType *types,
118 bool use_lookupsids3)
120 NTSTATUS result = NT_STATUS_OK;
121 TALLOC_CTX *tmp_ctx = NULL;
122 int i;
123 struct lsa_SidArray sid_array;
124 struct lsa_RefDomainList *ref_domains = NULL;
125 struct lsa_TransNameArray lsa_names;
126 uint32_t count = 0;
127 uint16_t level = 1;
129 ZERO_STRUCT(lsa_names);
131 tmp_ctx = talloc_new(mem_ctx);
132 if (!tmp_ctx) {
133 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
134 result = NT_STATUS_UNSUCCESSFUL;
135 goto done;
138 sid_array.num_sids = num_sids;
139 sid_array.sids = TALLOC_ARRAY(mem_ctx, struct lsa_SidPtr, num_sids);
140 if (!sid_array.sids) {
141 return NT_STATUS_NO_MEMORY;
144 for (i = 0; i<num_sids; i++) {
145 sid_array.sids[i].sid = sid_dup_talloc(mem_ctx, &sids[i]);
146 if (!sid_array.sids[i].sid) {
147 return NT_STATUS_NO_MEMORY;
151 if (use_lookupsids3) {
152 struct lsa_TransNameArray2 lsa_names2;
153 uint32_t n;
155 ZERO_STRUCT(lsa_names2);
157 result = rpccli_lsa_LookupSids3(cli, mem_ctx,
158 &sid_array,
159 &ref_domains,
160 &lsa_names2,
161 level,
162 &count,
166 if (!NT_STATUS_IS_ERR(result)) {
167 lsa_names.count = lsa_names2.count;
168 lsa_names.names = talloc_array(mem_ctx, struct lsa_TranslatedName, lsa_names.count);
169 if (!lsa_names.names) {
170 return NT_STATUS_NO_MEMORY;
172 for (n=0; n < lsa_names.count; n++) {
173 lsa_names.names[n].sid_type = lsa_names2.names[n].sid_type;
174 lsa_names.names[n].name = lsa_names2.names[n].name;
175 lsa_names.names[n].sid_index = lsa_names2.names[n].sid_index;
179 } else {
180 result = rpccli_lsa_LookupSids(cli, mem_ctx,
181 pol,
182 &sid_array,
183 &ref_domains,
184 &lsa_names,
185 level,
186 &count);
189 DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
190 nt_errstr(result), count));
192 if (!NT_STATUS_IS_OK(result) &&
193 !NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
194 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
196 /* An actual error occured */
197 goto done;
200 /* Return output parameters */
202 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
203 (count == 0))
205 for (i = 0; i < num_sids; i++) {
206 (names)[i] = NULL;
207 (domains)[i] = NULL;
208 (types)[i] = SID_NAME_UNKNOWN;
210 result = NT_STATUS_NONE_MAPPED;
211 goto done;
214 for (i = 0; i < num_sids; i++) {
215 const char *name, *dom_name;
216 uint32_t dom_idx = lsa_names.names[i].sid_index;
218 /* Translate optimised name through domain index array */
220 if (dom_idx != 0xffffffff) {
222 dom_name = ref_domains->domains[dom_idx].name.string;
223 name = lsa_names.names[i].name.string;
225 if (name) {
226 (names)[i] = talloc_strdup(mem_ctx, name);
227 if ((names)[i] == NULL) {
228 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
229 result = NT_STATUS_UNSUCCESSFUL;
230 goto done;
232 } else {
233 (names)[i] = NULL;
235 domains[i] = talloc_strdup(
236 mem_ctx, dom_name ? dom_name : "");
237 (types)[i] = lsa_names.names[i].sid_type;
238 if (((domains)[i] == NULL)) {
239 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
240 result = NT_STATUS_UNSUCCESSFUL;
241 goto done;
244 } else {
245 (names)[i] = NULL;
246 (domains)[i] = NULL;
247 (types)[i] = SID_NAME_UNKNOWN;
251 done:
252 TALLOC_FREE(tmp_ctx);
253 return result;
256 /* Lookup a list of sids
258 * do it the right way: there is a limit (of 20480 for w2k3) entries
259 * returned by this call. when the sids list contains more entries,
260 * empty lists are returned. This version of lsa_lookup_sids passes
261 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
263 /* This constant defines the limit of how many sids to look up
264 * in one call (maximum). the limit from the server side is
265 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
266 #define LOOKUP_SIDS_HUNK_SIZE 1000
268 static NTSTATUS rpccli_lsa_lookup_sids_generic(struct rpc_pipe_client *cli,
269 TALLOC_CTX *mem_ctx,
270 struct policy_handle *pol,
271 int num_sids,
272 const DOM_SID *sids,
273 char ***pdomains,
274 char ***pnames,
275 enum lsa_SidType **ptypes,
276 bool use_lookupsids3)
278 NTSTATUS result = NT_STATUS_OK;
279 int sids_left = 0;
280 int sids_processed = 0;
281 const DOM_SID *hunk_sids = sids;
282 char **hunk_domains;
283 char **hunk_names;
284 enum lsa_SidType *hunk_types;
285 char **domains = NULL;
286 char **names = NULL;
287 enum lsa_SidType *types = NULL;
289 if (num_sids) {
290 if (!(domains = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
291 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
292 result = NT_STATUS_NO_MEMORY;
293 goto fail;
296 if (!(names = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
297 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
298 result = NT_STATUS_NO_MEMORY;
299 goto fail;
302 if (!(types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
303 DEBUG(0, ("rpccli_lsa_lookup_sids(): out of memory\n"));
304 result = NT_STATUS_NO_MEMORY;
305 goto fail;
309 sids_left = num_sids;
310 hunk_domains = domains;
311 hunk_names = names;
312 hunk_types = types;
314 while (sids_left > 0) {
315 int hunk_num_sids;
316 NTSTATUS hunk_result = NT_STATUS_OK;
318 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
319 ? LOOKUP_SIDS_HUNK_SIZE
320 : sids_left);
322 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
323 "%d -- %d of %d.\n",
324 sids_processed,
325 sids_processed + hunk_num_sids - 1,
326 num_sids));
328 hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
329 mem_ctx,
330 pol,
331 hunk_num_sids,
332 hunk_sids,
333 hunk_domains,
334 hunk_names,
335 hunk_types,
336 use_lookupsids3);
338 if (!NT_STATUS_IS_OK(hunk_result) &&
339 !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
340 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
342 /* An actual error occured */
343 result = hunk_result;
344 goto fail;
347 /* adapt overall result */
348 if (( NT_STATUS_IS_OK(result) &&
349 !NT_STATUS_IS_OK(hunk_result))
351 ( NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
352 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)))
354 result = STATUS_SOME_UNMAPPED;
357 sids_left -= hunk_num_sids;
358 sids_processed += hunk_num_sids; /* only used in DEBUG */
359 hunk_sids += hunk_num_sids;
360 hunk_domains += hunk_num_sids;
361 hunk_names += hunk_num_sids;
362 hunk_types += hunk_num_sids;
365 *pdomains = domains;
366 *pnames = names;
367 *ptypes = types;
368 return result;
370 fail:
371 TALLOC_FREE(domains);
372 TALLOC_FREE(names);
373 TALLOC_FREE(types);
374 return result;
377 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
378 TALLOC_CTX *mem_ctx,
379 struct policy_handle *pol,
380 int num_sids,
381 const DOM_SID *sids,
382 char ***pdomains,
383 char ***pnames,
384 enum lsa_SidType **ptypes)
386 return rpccli_lsa_lookup_sids_generic(cli, mem_ctx, pol, num_sids, sids,
387 pdomains, pnames, ptypes, false);
390 NTSTATUS rpccli_lsa_lookup_sids3(struct rpc_pipe_client *cli,
391 TALLOC_CTX *mem_ctx,
392 struct policy_handle *pol,
393 int num_sids,
394 const DOM_SID *sids,
395 char ***pdomains,
396 char ***pnames,
397 enum lsa_SidType **ptypes)
399 return rpccli_lsa_lookup_sids_generic(cli, mem_ctx, pol, num_sids, sids,
400 pdomains, pnames, ptypes, true);
403 /** Lookup a list of names */
405 static NTSTATUS rpccli_lsa_lookup_names_generic(struct rpc_pipe_client *cli,
406 TALLOC_CTX *mem_ctx,
407 struct policy_handle *pol, int num_names,
408 const char **names,
409 const char ***dom_names,
410 int level,
411 DOM_SID **sids,
412 enum lsa_SidType **types,
413 bool use_lookupnames4)
415 NTSTATUS result;
416 int i;
417 struct lsa_String *lsa_names = NULL;
418 struct lsa_RefDomainList *domains = NULL;
419 struct lsa_TransSidArray sid_array;
420 struct lsa_TransSidArray3 sid_array3;
421 uint32_t count = 0;
423 ZERO_STRUCT(sid_array);
424 ZERO_STRUCT(sid_array3);
426 lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
427 if (!lsa_names) {
428 return NT_STATUS_NO_MEMORY;
431 for (i=0; i<num_names; i++) {
432 init_lsa_String(&lsa_names[i], names[i]);
435 if (use_lookupnames4) {
436 result = rpccli_lsa_LookupNames4(cli, mem_ctx,
437 num_names,
438 lsa_names,
439 &domains,
440 &sid_array3,
441 level,
442 &count,
445 } else {
446 result = rpccli_lsa_LookupNames(cli, mem_ctx,
447 pol,
448 num_names,
449 lsa_names,
450 &domains,
451 &sid_array,
452 level,
453 &count);
456 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
457 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
459 /* An actual error occured */
461 goto done;
464 /* Return output parameters */
466 if (count == 0) {
467 result = NT_STATUS_NONE_MAPPED;
468 goto done;
471 if (num_names) {
472 if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
473 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
474 result = NT_STATUS_NO_MEMORY;
475 goto done;
478 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
479 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
480 result = NT_STATUS_NO_MEMORY;
481 goto done;
484 if (dom_names != NULL) {
485 *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
486 if (*dom_names == NULL) {
487 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
488 result = NT_STATUS_NO_MEMORY;
489 goto done;
492 } else {
493 *sids = NULL;
494 *types = NULL;
495 if (dom_names != NULL) {
496 *dom_names = NULL;
500 for (i = 0; i < num_names; i++) {
501 uint32_t dom_idx;
502 DOM_SID *sid = &(*sids)[i];
504 if (use_lookupnames4) {
505 dom_idx = sid_array3.sids[i].sid_index;
506 (*types)[i] = sid_array3.sids[i].sid_type;
507 } else {
508 dom_idx = sid_array.sids[i].sid_index;
509 (*types)[i] = sid_array.sids[i].sid_type;
512 /* Translate optimised sid through domain index array */
514 if (dom_idx == 0xffffffff) {
515 /* Nothing to do, this is unknown */
516 ZERO_STRUCTP(sid);
517 (*types)[i] = SID_NAME_UNKNOWN;
518 continue;
521 if (use_lookupnames4) {
522 sid_copy(sid, sid_array3.sids[i].sid);
523 } else {
524 sid_copy(sid, domains->domains[dom_idx].sid);
526 if (sid_array.sids[i].rid != 0xffffffff) {
527 sid_append_rid(sid, sid_array.sids[i].rid);
531 if (dom_names == NULL) {
532 continue;
535 (*dom_names)[i] = domains->domains[dom_idx].name.string;
538 done:
540 return result;
543 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
544 TALLOC_CTX *mem_ctx,
545 struct policy_handle *pol, int num_names,
546 const char **names,
547 const char ***dom_names,
548 int level,
549 DOM_SID **sids,
550 enum lsa_SidType **types)
552 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
553 names, dom_names, level, sids,
554 types, false);
557 NTSTATUS rpccli_lsa_lookup_names4(struct rpc_pipe_client *cli,
558 TALLOC_CTX *mem_ctx,
559 struct policy_handle *pol, int num_names,
560 const char **names,
561 const char ***dom_names,
562 int level,
563 DOM_SID **sids,
564 enum lsa_SidType **types)
566 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
567 names, dom_names, level, sids,
568 types, true);