source: branches/samba-3.3.x/source/rpc_client/cli_lsarpc.c

Last change on this file was 342, checked in by Herwig Bauernfeind, 16 years ago

Update 3.3 to 3.3.9

File size: 14.5 KB
Line 
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.
10
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.
15
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.
20
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/>.
23*/
24
25#include "includes.h"
26
27/** @defgroup lsa LSA - Local Security Architecture
28 * @ingroup rpc_client
29 *
30 * @{
31 **/
32
33/**
34 * @file cli_lsarpc.c
35 *
36 * RPC client routines for the LSA RPC pipe. LSA means "local
37 * security authority", which is half of a password database.
38 **/
39
40/** Open a LSA policy handle
41 *
42 * @param cli Handle on an initialised SMB connection */
43
44NTSTATUS 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)
48{
49 struct lsa_ObjectAttribute attr;
50 struct lsa_QosInfo qos;
51 uint16_t system_name = '\\';
52
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,
59 0,
60 NULL,
61 &qos);
62 } else {
63 init_lsa_obj_attr(&attr,
64 0x18,
65 NULL,
66 NULL,
67 0,
68 NULL,
69 NULL);
70 }
71
72 return rpccli_lsa_OpenPolicy(cli, mem_ctx,
73 &system_name,
74 &attr,
75 des_access,
76 pol);
77}
78
79/** Open a LSA policy handle
80 *
81 * @param cli Handle on an initialised SMB connection
82 */
83
84NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
85 TALLOC_CTX *mem_ctx, bool sec_qos,
86 uint32 des_access, POLICY_HND *pol)
87{
88 struct lsa_ObjectAttribute attr;
89 struct lsa_QosInfo qos;
90
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,
97 0,
98 NULL,
99 &qos);
100 } else {
101 init_lsa_obj_attr(&attr,
102 0x18,
103 NULL,
104 NULL,
105 0,
106 NULL,
107 NULL);
108 }
109
110 return rpccli_lsa_OpenPolicy2(cli, mem_ctx,
111 cli->srv_name_slash,
112 &attr,
113 des_access,
114 pol);
115}
116
117/* Lookup a list of sids
118 *
119 * internal version withOUT memory allocation of the target arrays.
120 * this assumes suffciently sized arrays to store domains, names and types. */
121
122static 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)
131{
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;
140
141 ZERO_STRUCT(lsa_names);
142
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;
148 }
149
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;
154 }
155
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;
160 }
161 }
162
163 if (use_lookupsids3) {
164 struct lsa_TransNameArray2 lsa_names2;
165 uint32_t n;
166
167 ZERO_STRUCT(lsa_names2);
168
169 result = rpccli_lsa_LookupSids3(cli, mem_ctx,
170 &sid_array,
171 &ref_domains,
172 &lsa_names2,
173 level,
174 &count,
175 0,
176 0);
177
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;
183 }
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;
188 }
189 }
190
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);
199 }
200
201 DEBUG(10, ("LSA_LOOKUPSIDS returned '%s', mapped count = %d'\n",
202 nt_errstr(result), count));
203
204 if (!NT_STATUS_IS_OK(result) &&
205 !NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
206 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
207 {
208 /* An actual error occured */
209 goto done;
210 }
211
212 /* Return output parameters */
213
214 if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) ||
215 (count == 0))
216 {
217 for (i = 0; i < num_sids; i++) {
218 (names)[i] = NULL;
219 (domains)[i] = NULL;
220 (types)[i] = SID_NAME_UNKNOWN;
221 }
222 result = NT_STATUS_NONE_MAPPED;
223 goto done;
224 }
225
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;
229
230 /* Translate optimised name through domain index array */
231
232 if (dom_idx != 0xffffffff) {
233
234 dom_name = ref_domains->domains[dom_idx].name.string;
235 name = lsa_names.names[i].name.string;
236
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;
243 }
244 } else {
245 (names)[i] = NULL;
246 }
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;
253 }
254
255 } else {
256 (names)[i] = NULL;
257 (domains)[i] = NULL;
258 (types)[i] = SID_NAME_UNKNOWN;
259 }
260 }
261
262done:
263 TALLOC_FREE(tmp_ctx);
264 return result;
265}
266
267/* Lookup a list of sids
268 *
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. */
273
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
278
279static 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)
288{
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;
299
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;
305 }
306
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;
311 }
312
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;
317 }
318 }
319
320 sids_left = num_sids;
321 hunk_domains = domains;
322 hunk_names = names;
323 hunk_types = types;
324
325 while (sids_left > 0) {
326 int hunk_num_sids;
327 NTSTATUS hunk_result = NT_STATUS_OK;
328
329 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
330 ? LOOKUP_SIDS_HUNK_SIZE
331 : sids_left);
332
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));
338
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);
348
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))
352 {
353 /* An actual error occured */
354 result = hunk_result;
355 goto fail;
356 }
357
358 /* adapt overall result */
359 if (( NT_STATUS_IS_OK(result) &&
360 !NT_STATUS_IS_OK(hunk_result))
361 ||
362 ( NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
363 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)))
364 {
365 result = STATUS_SOME_UNMAPPED;
366 }
367
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;
374 }
375
376 *pdomains = domains;
377 *pnames = names;
378 *ptypes = types;
379 return result;
380
381fail:
382 TALLOC_FREE(domains);
383 TALLOC_FREE(names);
384 TALLOC_FREE(types);
385 return result;
386}
387
388NTSTATUS 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)
396{
397 return rpccli_lsa_lookup_sids_generic(cli, mem_ctx, pol, num_sids, sids,
398 pdomains, pnames, ptypes, false);
399}
400
401NTSTATUS 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)
409{
410 return rpccli_lsa_lookup_sids_generic(cli, mem_ctx, pol, num_sids, sids,
411 pdomains, pnames, ptypes, true);
412}
413
414/** Lookup a list of names */
415
416static 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)
425{
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;
433
434 ZERO_STRUCT(sid_array);
435 ZERO_STRUCT(sid_array3);
436
437 lsa_names = TALLOC_ARRAY(mem_ctx, struct lsa_String, num_names);
438 if (!lsa_names) {
439 return NT_STATUS_NO_MEMORY;
440 }
441
442 for (i=0; i<num_names; i++) {
443 init_lsa_String(&lsa_names[i], names[i]);
444 }
445
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,
454 0,
455 0);
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);
465 }
466
467 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
468 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
469
470 /* An actual error occured */
471
472 goto done;
473 }
474
475 /* Return output parameters */
476
477 if (count == 0) {
478 result = NT_STATUS_NONE_MAPPED;
479 goto done;
480 }
481
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;
487 }
488
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;
493 }
494
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;
501 }
502 }
503 } else {
504 *sids = NULL;
505 *types = NULL;
506 if (dom_names != NULL) {
507 *dom_names = NULL;
508 }
509 }
510
511 for (i = 0; i < num_names; i++) {
512 uint32_t dom_idx;
513 DOM_SID *sid = &(*sids)[i];
514
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;
521 }
522
523 /* Translate optimised sid through domain index array */
524
525 if (dom_idx == 0xffffffff) {
526 /* Nothing to do, this is unknown */
527 ZERO_STRUCTP(sid);
528 (*types)[i] = SID_NAME_UNKNOWN;
529 continue;
530 }
531
532 if (use_lookupnames4) {
533 sid_copy(sid, sid_array3.sids[i].sid);
534 } else {
535 sid_copy(sid, domains->domains[dom_idx].sid);
536
537 if (sid_array.sids[i].rid != 0xffffffff) {
538 sid_append_rid(sid, sid_array.sids[i].rid);
539 }
540 }
541
542 if (dom_names == NULL) {
543 continue;
544 }
545
546 (*dom_names)[i] = domains->domains[dom_idx].name.string;
547 }
548
549 done:
550
551 return result;
552}
553
554NTSTATUS 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)
562{
563 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
564 names, dom_names, level, sids,
565 types, false);
566}
567
568NTSTATUS 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)
576{
577 return rpccli_lsa_lookup_names_generic(cli, mem_ctx, pol, num_names,
578 names, dom_names, level, sids,
579 types, true);
580}
Note: See TracBrowser for help on using the repository browser.