s3: Lift the server_messaging_context from send_spoolss_notify2_msg
[Samba/gbeck.git] / source3 / rpc_client / cli_lsarpc.c
blob72514e7f5e5693e9e08511d7c21325037c383ede
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"
26 #include "../librpc/gen_ndr/cli_lsa.h"
27 #include "rpc_client/cli_lsarpc.h"
29 /** @defgroup lsa LSA - Local Security Architecture
30 * @ingroup rpc_client
32 * @{
33 **/
35 /**
36 * @file cli_lsarpc.c
38 * RPC client routines for the LSA RPC pipe. LSA means "local
39 * security authority", which is half of a password database.
40 **/
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,
47 TALLOC_CTX *mem_ctx,
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 = '\\';
55 ZERO_STRUCT(attr);
57 attr.len = 0x18;
59 if (sec_qos) {
60 qos.len = 0xc;
61 qos.impersonation_level = 2;
62 qos.context_mode = 1;
63 qos.effective_only = 0;
65 attr.sec_qos = &qos;
68 return rpccli_lsa_OpenPolicy(cli, mem_ctx,
69 &system_name,
70 &attr,
71 des_access,
72 pol);
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;
87 ZERO_STRUCT(attr);
89 attr.len = 0x18;
91 if (sec_qos) {
92 qos.len = 0xc;
93 qos.impersonation_level = 2;
94 qos.context_mode = 1;
95 qos.effective_only = 0;
97 attr.sec_qos = &qos;
100 return rpccli_lsa_OpenPolicy2(cli, mem_ctx,
101 cli->srv_name_slash,
102 &attr,
103 des_access,
104 pol);
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,
113 TALLOC_CTX *mem_ctx,
114 struct policy_handle *pol,
115 int num_sids,
116 const struct dom_sid *sids,
117 char **domains,
118 char **names,
119 enum lsa_SidType *types,
120 bool use_lookupsids3)
122 NTSTATUS result = NT_STATUS_OK;
123 TALLOC_CTX *tmp_ctx = NULL;
124 int i;
125 struct lsa_SidArray sid_array;
126 struct lsa_RefDomainList *ref_domains = NULL;
127 struct lsa_TransNameArray lsa_names;
128 uint32_t count = 0;
129 uint16_t level = 1;
131 ZERO_STRUCT(lsa_names);
133 tmp_ctx = talloc_new(mem_ctx);
134 if (!tmp_ctx) {
135 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
136 result = NT_STATUS_UNSUCCESSFUL;
137 goto done;
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;
155 uint32_t n;
157 ZERO_STRUCT(lsa_names2);
159 result = rpccli_lsa_LookupSids3(cli, mem_ctx,
160 &sid_array,
161 &ref_domains,
162 &lsa_names2,
163 level,
164 &count,
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;
181 } else {
182 result = rpccli_lsa_LookupSids(cli, mem_ctx,
183 pol,
184 &sid_array,
185 &ref_domains,
186 &lsa_names,
187 level,
188 &count);
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 */
199 goto done;
202 /* Return output parameters */
204 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
205 (count == 0))
207 for (i = 0; i < num_sids; i++) {
208 (names)[i] = NULL;
209 (domains)[i] = NULL;
210 (types)[i] = SID_NAME_UNKNOWN;
212 result = NT_STATUS_NONE_MAPPED;
213 goto done;
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;
227 if (name) {
228 (names)[i] = talloc_strdup(names, name);
229 if ((names)[i] == NULL) {
230 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
231 result = NT_STATUS_UNSUCCESSFUL;
232 goto done;
234 } else {
235 (names)[i] = NULL;
237 domains[i] = talloc_strdup(domains,
238 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;
243 goto done;
246 } else {
247 (names)[i] = NULL;
248 (domains)[i] = NULL;
249 (types)[i] = SID_NAME_UNKNOWN;
253 done:
254 TALLOC_FREE(tmp_ctx);
255 return result;
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,
271 TALLOC_CTX *mem_ctx,
272 struct policy_handle *pol,
273 int num_sids,
274 const struct dom_sid *sids,
275 char ***pdomains,
276 char ***pnames,
277 enum lsa_SidType **ptypes,
278 bool use_lookupsids3)
280 NTSTATUS result = NT_STATUS_OK;
281 int sids_left = 0;
282 int sids_processed = 0;
283 const struct dom_sid *hunk_sids = sids;
284 char **hunk_domains;
285 char **hunk_names;
286 enum lsa_SidType *hunk_types;
287 char **domains = NULL;
288 char **names = NULL;
289 enum lsa_SidType *types = NULL;
291 if (num_sids) {
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;
295 goto fail;
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;
301 goto fail;
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;
307 goto fail;
311 sids_left = num_sids;
312 hunk_domains = domains;
313 hunk_names = names;
314 hunk_types = types;
316 while (sids_left > 0) {
317 int hunk_num_sids;
318 NTSTATUS hunk_result = NT_STATUS_OK;
320 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
321 ? LOOKUP_SIDS_HUNK_SIZE
322 : sids_left);
324 DEBUG(10, ("rpccli_lsa_lookup_sids: processing items "
325 "%d -- %d of %d.\n",
326 sids_processed,
327 sids_processed + hunk_num_sids - 1,
328 num_sids));
330 hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
331 mem_ctx,
332 pol,
333 hunk_num_sids,
334 hunk_sids,
335 hunk_domains,
336 hunk_names,
337 hunk_types,
338 use_lookupsids3);
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;
346 goto fail;
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;
367 *pdomains = domains;
368 *pnames = names;
369 *ptypes = types;
370 return result;
372 fail:
373 TALLOC_FREE(domains);
374 TALLOC_FREE(names);
375 TALLOC_FREE(types);
376 return result;
379 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
380 TALLOC_CTX *mem_ctx,
381 struct policy_handle *pol,
382 int num_sids,
383 const struct dom_sid *sids,
384 char ***pdomains,
385 char ***pnames,
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,
393 TALLOC_CTX *mem_ctx,
394 struct policy_handle *pol,
395 int num_sids,
396 const struct dom_sid *sids,
397 char ***pdomains,
398 char ***pnames,
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,
408 TALLOC_CTX *mem_ctx,
409 struct policy_handle *pol, int num_names,
410 const char **names,
411 const char ***dom_names,
412 int level,
413 struct dom_sid **sids,
414 enum lsa_SidType **types,
415 bool use_lookupnames4)
417 NTSTATUS result;
418 int i;
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;
423 uint32_t count = 0;
425 ZERO_STRUCT(sid_array);
426 ZERO_STRUCT(sid_array3);
428 lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
429 if (!lsa_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,
439 num_names,
440 lsa_names,
441 &domains,
442 &sid_array3,
443 level,
444 &count,
447 } else {
448 result = rpccli_lsa_LookupNames(cli, mem_ctx,
449 pol,
450 num_names,
451 lsa_names,
452 &domains,
453 &sid_array,
454 level,
455 &count);
458 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
459 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
461 /* An actual error occured */
463 goto done;
466 /* Return output parameters */
468 if (count == 0) {
469 result = NT_STATUS_NONE_MAPPED;
470 goto done;
473 if (num_names) {
474 if (!((*sids = TALLOC_ARRAY(mem_ctx, struct dom_sid, num_names)))) {
475 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
476 result = NT_STATUS_NO_MEMORY;
477 goto done;
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;
483 goto done;
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;
491 goto done;
494 } else {
495 *sids = NULL;
496 *types = NULL;
497 if (dom_names != NULL) {
498 *dom_names = NULL;
502 for (i = 0; i < num_names; i++) {
503 uint32_t dom_idx;
504 struct 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;
509 } else {
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 */
518 ZERO_STRUCTP(sid);
519 (*types)[i] = SID_NAME_UNKNOWN;
520 continue;
523 if (use_lookupnames4) {
524 sid_copy(sid, sid_array3.sids[i].sid);
525 } else {
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) {
534 continue;
537 (*dom_names)[i] = domains->domains[dom_idx].name.string;
540 done:
542 return result;
545 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
546 TALLOC_CTX *mem_ctx,
547 struct policy_handle *pol, int num_names,
548 const char **names,
549 const char ***dom_names,
550 int level,
551 struct dom_sid **sids,
552 enum lsa_SidType **types)
554 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
555 names, dom_names, level, sids,
556 types, false);
559 NTSTATUS rpccli_lsa_lookup_names4(struct rpc_pipe_client *cli,
560 TALLOC_CTX *mem_ctx,
561 struct policy_handle *pol, int num_names,
562 const char **names,
563 const char ***dom_names,
564 int level,
565 struct dom_sid **sids,
566 enum lsa_SidType **types)
568 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
569 names, dom_names, level, sids,
570 types, true);