r25223: Fix behaviour of rpccli_lsa_lookupsids_all() when
[Samba.git] / source / rpc_client / cli_lsarpc.c
blob1f0677ee03137de59a1f03b0581c4d52821e2dc2
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.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
25 /** @defgroup lsa LSA - Local Security Architecture
26 * @ingroup rpc_client
28 * @{
29 **/
31 /**
32 * @file cli_lsarpc.c
34 * RPC client routines for the LSA RPC pipe. LSA means "local
35 * security authority", which is half of a password database.
36 **/
38 /** Open a LSA policy handle
40 * @param cli Handle on an initialised SMB connection */
42 NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
43 TALLOC_CTX *mem_ctx,
44 BOOL sec_qos, uint32 des_access,
45 POLICY_HND *pol)
47 prs_struct qbuf, rbuf;
48 LSA_Q_OPEN_POL q;
49 LSA_R_OPEN_POL r;
50 LSA_SEC_QOS qos;
51 NTSTATUS result;
53 ZERO_STRUCT(q);
54 ZERO_STRUCT(r);
56 /* Initialise input parameters */
58 if (sec_qos) {
59 init_lsa_sec_qos(&qos, 2, 1, 0);
60 init_q_open_pol(&q, '\\', 0, des_access, &qos);
61 } else {
62 init_q_open_pol(&q, '\\', 0, des_access, NULL);
65 /* Marshall data and send request */
67 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENPOLICY,
68 q, r,
69 qbuf, rbuf,
70 lsa_io_q_open_pol,
71 lsa_io_r_open_pol,
72 NT_STATUS_UNSUCCESSFUL );
74 /* Return output parameters */
76 result = r.status;
78 if (NT_STATUS_IS_OK(result)) {
79 *pol = r.pol;
82 return result;
85 /** Open a LSA policy handle
87 * @param cli Handle on an initialised SMB connection
90 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
91 TALLOC_CTX *mem_ctx, BOOL sec_qos,
92 uint32 des_access, POLICY_HND *pol)
94 prs_struct qbuf, rbuf;
95 LSA_Q_OPEN_POL2 q;
96 LSA_R_OPEN_POL2 r;
97 LSA_SEC_QOS qos;
98 NTSTATUS result;
99 char *srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
101 ZERO_STRUCT(q);
102 ZERO_STRUCT(r);
104 if (sec_qos) {
105 init_lsa_sec_qos(&qos, 2, 1, 0);
106 init_q_open_pol2(&q, srv_name_slash, 0, des_access, &qos);
107 } else {
108 init_q_open_pol2(&q, srv_name_slash, 0, des_access, NULL);
111 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENPOLICY2,
112 q, r,
113 qbuf, rbuf,
114 lsa_io_q_open_pol2,
115 lsa_io_r_open_pol2,
116 NT_STATUS_UNSUCCESSFUL );
118 /* Return output parameters */
120 result = r.status;
122 if (NT_STATUS_IS_OK(result)) {
123 *pol = r.pol;
126 return result;
129 /* Lookup a list of sids
131 * internal version withOUT memory allocation of the target arrays.
132 * this assumes suffciently sized arrays to store domains, names and types. */
134 static NTSTATUS rpccli_lsa_lookup_sids_noalloc(struct rpc_pipe_client *cli,
135 TALLOC_CTX *mem_ctx,
136 POLICY_HND *pol,
137 int num_sids,
138 const DOM_SID *sids,
139 char **domains,
140 char **names,
141 enum lsa_SidType *types)
143 prs_struct qbuf, rbuf;
144 LSA_Q_LOOKUP_SIDS q;
145 LSA_R_LOOKUP_SIDS r;
146 DOM_R_REF ref;
147 NTSTATUS result = NT_STATUS_OK;
148 TALLOC_CTX *tmp_ctx = NULL;
149 int i;
151 tmp_ctx = talloc_new(mem_ctx);
152 if (!tmp_ctx) {
153 DEBUG(0, ("rpccli_lsa_lookup_sids_noalloc: out of memory!\n"));
154 result = NT_STATUS_UNSUCCESSFUL;
155 goto done;
158 ZERO_STRUCT(q);
159 ZERO_STRUCT(r);
161 init_q_lookup_sids(tmp_ctx, &q, pol, num_sids, sids, 1);
163 ZERO_STRUCT(ref);
165 r.dom_ref = &ref;
167 CLI_DO_RPC( cli, tmp_ctx, PI_LSARPC, LSA_LOOKUPSIDS,
168 q, r,
169 qbuf, rbuf,
170 lsa_io_q_lookup_sids,
171 lsa_io_r_lookup_sids,
172 NT_STATUS_UNSUCCESSFUL );
174 if (!NT_STATUS_IS_OK(r.status) &&
175 !NT_STATUS_EQUAL(r.status, STATUS_SOME_UNMAPPED))
177 /* An actual error occured */
178 result = r.status;
179 goto done;
182 /* Return output parameters */
184 if (r.mapped_count == 0) {
185 for (i = 0; i < num_sids; i++) {
186 (names)[i] = NULL;
187 (domains)[i] = NULL;
188 (types)[i] = SID_NAME_UNKNOWN;
190 result = NT_STATUS_NONE_MAPPED;
191 goto done;
194 for (i = 0; i < num_sids; i++) {
195 fstring name, dom_name;
196 uint32 dom_idx = r.names.name[i].domain_idx;
198 /* Translate optimised name through domain index array */
200 if (dom_idx != 0xffffffff) {
202 rpcstr_pull_unistr2_fstring(
203 dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
204 rpcstr_pull_unistr2_fstring(
205 name, &r.names.uni_name[i]);
207 (names)[i] = talloc_strdup(mem_ctx, name);
208 (domains)[i] = talloc_strdup(mem_ctx, dom_name);
209 (types)[i] = (enum lsa_SidType)r.names.name[i].sid_name_use;
211 if (((names)[i] == NULL) || ((domains)[i] == NULL)) {
212 DEBUG(0, ("cli_lsa_lookup_sids_noalloc(): out of memory\n"));
213 result = NT_STATUS_UNSUCCESSFUL;
214 goto done;
217 } else {
218 (names)[i] = NULL;
219 (domains)[i] = NULL;
220 (types)[i] = SID_NAME_UNKNOWN;
224 done:
225 TALLOC_FREE(tmp_ctx);
226 return result;
229 /* Lookup a list of sids
231 * do it the right way: there is a limit (of 20480 for w2k3) entries
232 * returned by this call. when the sids list contains more entries,
233 * empty lists are returned. This version of lsa_lookup_sids passes
234 * the list of sids in hunks of LOOKUP_SIDS_HUNK_SIZE to the lsa call. */
236 /* This constant defines the limit of how many sids to look up
237 * in one call (maximum). the limit from the server side is
238 * at 20480 for win2k3, but we keep it at a save 1000 for now. */
239 #define LOOKUP_SIDS_HUNK_SIZE 1000
241 NTSTATUS rpccli_lsa_lookup_sids_all(struct rpc_pipe_client *cli,
242 TALLOC_CTX *mem_ctx,
243 POLICY_HND *pol,
244 int num_sids,
245 const DOM_SID *sids,
246 char ***domains,
247 char ***names,
248 enum lsa_SidType **types)
250 NTSTATUS result = NT_STATUS_OK;
251 int sids_left = 0;
252 int sids_processed = 0;
253 const DOM_SID *hunk_sids = sids;
254 char **hunk_domains = NULL;
255 char **hunk_names = NULL;
256 enum lsa_SidType *hunk_types = NULL;
258 if (num_sids) {
259 if (!((*domains) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
260 DEBUG(0, ("rpccli_lsa_lookup_sids_all(): out of memory\n"));
261 result = NT_STATUS_NO_MEMORY;
262 goto fail;
265 if (!((*names) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
266 DEBUG(0, ("rpccli_lsa_lookup_sids_all(): out of memory\n"));
267 result = NT_STATUS_NO_MEMORY;
268 goto fail;
271 if (!((*types) = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
272 DEBUG(0, ("rpccli_lsa_lookup_sids_all(): out of memory\n"));
273 result = NT_STATUS_NO_MEMORY;
274 goto fail;
276 } else {
277 (*domains) = NULL;
278 (*names) = NULL;
279 (*types) = NULL;
282 sids_left = num_sids;
283 hunk_domains = *domains;
284 hunk_names = *names;
285 hunk_types = *types;
287 while (sids_left > 0) {
288 int hunk_num_sids;
289 NTSTATUS hunk_result = NT_STATUS_OK;
291 hunk_num_sids = ((sids_left > LOOKUP_SIDS_HUNK_SIZE)
292 ? LOOKUP_SIDS_HUNK_SIZE
293 : sids_left);
295 DEBUG(10, ("rpccli_lsa_lookup_sids_all: processing items "
296 "%d -- %d of %d.\n",
297 sids_processed,
298 sids_processed + hunk_num_sids - 1,
299 num_sids));
301 hunk_result = rpccli_lsa_lookup_sids_noalloc(cli,
302 mem_ctx,
303 pol,
304 hunk_num_sids,
305 hunk_sids,
306 hunk_domains,
307 hunk_names,
308 hunk_types);
310 if (!NT_STATUS_IS_OK(hunk_result) &&
311 !NT_STATUS_EQUAL(hunk_result, STATUS_SOME_UNMAPPED) &&
312 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED))
314 /* An actual error occured */
315 result = hunk_result;
316 goto fail;
319 /* adapt overall result */
320 if (( NT_STATUS_IS_OK(result) &&
321 !NT_STATUS_IS_OK(hunk_result))
323 ( NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
324 !NT_STATUS_EQUAL(hunk_result, NT_STATUS_NONE_MAPPED)))
326 result = STATUS_SOME_UNMAPPED;
329 sids_left -= hunk_num_sids;
330 sids_processed += hunk_num_sids; /* only used in DEBUG */
331 hunk_sids += hunk_num_sids;
332 hunk_domains += hunk_num_sids;
333 hunk_names += hunk_num_sids;
334 hunk_types += hunk_num_sids;
337 return result;
339 fail:
340 TALLOC_FREE(*domains);
341 TALLOC_FREE(*names);
342 TALLOC_FREE(*types);
343 return result;
346 /** Lookup a list of sids */
348 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
349 TALLOC_CTX *mem_ctx,
350 POLICY_HND *pol, int num_sids,
351 const DOM_SID *sids,
352 char ***domains,
353 char ***names,
354 enum lsa_SidType **types)
356 prs_struct qbuf, rbuf;
357 LSA_Q_LOOKUP_SIDS q;
358 LSA_R_LOOKUP_SIDS r;
359 DOM_R_REF ref;
360 NTSTATUS result = NT_STATUS_OK;
361 int i;
363 ZERO_STRUCT(q);
364 ZERO_STRUCT(r);
366 init_q_lookup_sids(mem_ctx, &q, pol, num_sids, sids, 1);
368 ZERO_STRUCT(ref);
370 r.dom_ref = &ref;
372 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPSIDS,
373 q, r,
374 qbuf, rbuf,
375 lsa_io_q_lookup_sids,
376 lsa_io_r_lookup_sids,
377 NT_STATUS_UNSUCCESSFUL );
379 if (!NT_STATUS_IS_OK(r.status) &&
380 !NT_STATUS_EQUAL(r.status, STATUS_SOME_UNMAPPED)) {
382 /* An actual error occured */
383 result = r.status;
385 goto done;
388 /* Return output parameters */
390 if (r.mapped_count == 0) {
391 result = NT_STATUS_NONE_MAPPED;
392 goto done;
395 if (num_sids) {
396 if (!((*domains) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
397 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
398 result = NT_STATUS_NO_MEMORY;
399 goto done;
402 if (!((*names) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
403 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
404 result = NT_STATUS_NO_MEMORY;
405 goto done;
408 if (!((*types) = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
409 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
410 result = NT_STATUS_NO_MEMORY;
411 goto done;
413 } else {
414 (*domains) = NULL;
415 (*names) = NULL;
416 (*types) = NULL;
419 for (i = 0; i < num_sids; i++) {
420 fstring name, dom_name;
421 uint32 dom_idx = r.names.name[i].domain_idx;
423 /* Translate optimised name through domain index array */
425 if (dom_idx != 0xffffffff) {
427 rpcstr_pull_unistr2_fstring(
428 dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
429 rpcstr_pull_unistr2_fstring(
430 name, &r.names.uni_name[i]);
432 (*names)[i] = talloc_strdup(mem_ctx, name);
433 (*domains)[i] = talloc_strdup(mem_ctx, dom_name);
434 (*types)[i] = (enum lsa_SidType)r.names.name[i].sid_name_use;
436 if (((*names)[i] == NULL) || ((*domains)[i] == NULL)) {
437 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
438 result = NT_STATUS_UNSUCCESSFUL;
439 goto done;
442 } else {
443 (*names)[i] = NULL;
444 (*domains)[i] = NULL;
445 (*types)[i] = SID_NAME_UNKNOWN;
449 done:
451 return result;
454 /** Lookup a list of names */
456 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
457 TALLOC_CTX *mem_ctx,
458 POLICY_HND *pol, int num_names,
459 const char **names,
460 const char ***dom_names,
461 int level,
462 DOM_SID **sids,
463 enum lsa_SidType **types)
465 prs_struct qbuf, rbuf;
466 LSA_Q_LOOKUP_NAMES q;
467 LSA_R_LOOKUP_NAMES r;
468 DOM_R_REF ref;
469 NTSTATUS result;
470 int i;
472 ZERO_STRUCT(q);
473 ZERO_STRUCT(r);
475 ZERO_STRUCT(ref);
476 r.dom_ref = &ref;
478 init_q_lookup_names(mem_ctx, &q, pol, num_names, names, level);
480 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPNAMES,
481 q, r,
482 qbuf, rbuf,
483 lsa_io_q_lookup_names,
484 lsa_io_r_lookup_names,
485 NT_STATUS_UNSUCCESSFUL);
487 result = r.status;
489 if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
490 NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
492 /* An actual error occured */
494 goto done;
497 /* Return output parameters */
499 if (r.mapped_count == 0) {
500 result = NT_STATUS_NONE_MAPPED;
501 goto done;
504 if (num_names) {
505 if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
506 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
507 result = NT_STATUS_NO_MEMORY;
508 goto done;
511 if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
512 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
513 result = NT_STATUS_NO_MEMORY;
514 goto done;
517 if (dom_names != NULL) {
518 *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
519 if (*dom_names == NULL) {
520 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
521 result = NT_STATUS_NO_MEMORY;
522 goto done;
525 } else {
526 *sids = NULL;
527 *types = NULL;
528 if (dom_names != NULL) {
529 *dom_names = NULL;
533 for (i = 0; i < num_names; i++) {
534 DOM_RID *t_rids = r.dom_rid;
535 uint32 dom_idx = t_rids[i].rid_idx;
536 uint32 dom_rid = t_rids[i].rid;
537 DOM_SID *sid = &(*sids)[i];
539 /* Translate optimised sid through domain index array */
541 if (dom_idx == 0xffffffff) {
542 /* Nothing to do, this is unknown */
543 ZERO_STRUCTP(sid);
544 (*types)[i] = SID_NAME_UNKNOWN;
545 continue;
548 sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
550 if (dom_rid != 0xffffffff) {
551 sid_append_rid(sid, dom_rid);
554 (*types)[i] = (enum lsa_SidType)t_rids[i].type;
556 if (dom_names == NULL) {
557 continue;
560 (*dom_names)[i] = rpcstr_pull_unistr2_talloc(
561 *dom_names, &ref.ref_dom[dom_idx].uni_dom_name);
564 done:
566 return result;
569 NTSTATUS rpccli_lsa_query_info_policy_new(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
570 POLICY_HND *pol, uint16 info_class,
571 LSA_INFO_CTR *ctr)
573 prs_struct qbuf, rbuf;
574 LSA_Q_QUERY_INFO q;
575 LSA_R_QUERY_INFO r;
576 NTSTATUS result;
578 ZERO_STRUCT(q);
579 ZERO_STRUCT(r);
581 init_q_query(&q, pol, info_class);
583 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFOPOLICY,
584 q, r,
585 qbuf, rbuf,
586 lsa_io_q_query,
587 lsa_io_r_query,
588 NT_STATUS_UNSUCCESSFUL);
590 result = r.status;
592 if (!NT_STATUS_IS_OK(result)) {
593 goto done;
596 done:
598 *ctr = r.ctr;
600 return result;
603 NTSTATUS rpccli_lsa_query_info_policy2_new(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
604 POLICY_HND *pol, uint16 info_class,
605 LSA_INFO_CTR2 *ctr)
607 prs_struct qbuf, rbuf;
608 LSA_Q_QUERY_INFO2 q;
609 LSA_R_QUERY_INFO2 r;
610 NTSTATUS result;
612 ZERO_STRUCT(q);
613 ZERO_STRUCT(r);
615 init_q_query2(&q, pol, info_class);
617 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFO2,
618 q, r,
619 qbuf, rbuf,
620 lsa_io_q_query_info2,
621 lsa_io_r_query_info2,
622 NT_STATUS_UNSUCCESSFUL);
624 result = r.status;
626 if (!NT_STATUS_IS_OK(result)) {
627 goto done;
630 done:
632 *ctr = r.ctr;
634 return result;
639 /** Query info policy
641 * @param domain_sid - returned remote server's domain sid */
643 NTSTATUS rpccli_lsa_query_info_policy(struct rpc_pipe_client *cli,
644 TALLOC_CTX *mem_ctx,
645 POLICY_HND *pol, uint16 info_class,
646 char **domain_name, DOM_SID **domain_sid)
648 prs_struct qbuf, rbuf;
649 LSA_Q_QUERY_INFO q;
650 LSA_R_QUERY_INFO r;
651 NTSTATUS result;
653 ZERO_STRUCT(q);
654 ZERO_STRUCT(r);
656 init_q_query(&q, pol, info_class);
658 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFOPOLICY,
659 q, r,
660 qbuf, rbuf,
661 lsa_io_q_query,
662 lsa_io_r_query,
663 NT_STATUS_UNSUCCESSFUL);
665 result = r.status;
667 if (!NT_STATUS_IS_OK(result)) {
668 goto done;
671 /* Return output parameters */
673 switch (info_class) {
675 case 3:
676 if (domain_name && (r.ctr.info.id3.buffer_dom_name != 0)) {
677 *domain_name = unistr2_tdup(mem_ctx,
678 &r.ctr.info.id3.
679 uni_domain_name);
680 if (!*domain_name) {
681 return NT_STATUS_NO_MEMORY;
685 if (domain_sid && (r.ctr.info.id3.buffer_dom_sid != 0)) {
686 *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
687 if (!*domain_sid) {
688 return NT_STATUS_NO_MEMORY;
690 sid_copy(*domain_sid, &r.ctr.info.id3.dom_sid.sid);
693 break;
695 case 5:
697 if (domain_name && (r.ctr.info.id5.buffer_dom_name != 0)) {
698 *domain_name = unistr2_tdup(mem_ctx,
699 &r.ctr.info.id5.
700 uni_domain_name);
701 if (!*domain_name) {
702 return NT_STATUS_NO_MEMORY;
706 if (domain_sid && (r.ctr.info.id5.buffer_dom_sid != 0)) {
707 *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
708 if (!*domain_sid) {
709 return NT_STATUS_NO_MEMORY;
711 sid_copy(*domain_sid, &r.ctr.info.id5.dom_sid.sid);
713 break;
715 default:
716 DEBUG(3, ("unknown info class %d\n", info_class));
717 break;
720 done:
722 return result;
725 /** Query info policy2
727 * @param domain_name - returned remote server's domain name
728 * @param dns_name - returned remote server's dns domain name
729 * @param forest_name - returned remote server's forest name
730 * @param domain_guid - returned remote server's domain guid
731 * @param domain_sid - returned remote server's domain sid */
733 NTSTATUS rpccli_lsa_query_info_policy2(struct rpc_pipe_client *cli,
734 TALLOC_CTX *mem_ctx,
735 POLICY_HND *pol, uint16 info_class,
736 char **domain_name, char **dns_name,
737 char **forest_name,
738 struct GUID **domain_guid,
739 DOM_SID **domain_sid)
741 prs_struct qbuf, rbuf;
742 LSA_Q_QUERY_INFO2 q;
743 LSA_R_QUERY_INFO2 r;
744 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
746 if (info_class != 12)
747 goto done;
749 ZERO_STRUCT(q);
750 ZERO_STRUCT(r);
752 init_q_query2(&q, pol, info_class);
754 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYINFO2,
755 q, r,
756 qbuf, rbuf,
757 lsa_io_q_query_info2,
758 lsa_io_r_query_info2,
759 NT_STATUS_UNSUCCESSFUL);
761 result = r.status;
763 if (!NT_STATUS_IS_OK(result)) {
764 goto done;
767 /* Return output parameters */
769 ZERO_STRUCTP(domain_guid);
771 if (domain_name && r.ctr.info.id12.hdr_nb_dom_name.buffer) {
772 *domain_name = unistr2_tdup(mem_ctx,
773 &r.ctr.info.id12
774 .uni_nb_dom_name);
775 if (!*domain_name) {
776 return NT_STATUS_NO_MEMORY;
779 if (dns_name && r.ctr.info.id12.hdr_dns_dom_name.buffer) {
780 *dns_name = unistr2_tdup(mem_ctx,
781 &r.ctr.info.id12
782 .uni_dns_dom_name);
783 if (!*dns_name) {
784 return NT_STATUS_NO_MEMORY;
787 if (forest_name && r.ctr.info.id12.hdr_forest_name.buffer) {
788 *forest_name = unistr2_tdup(mem_ctx,
789 &r.ctr.info.id12
790 .uni_forest_name);
791 if (!*forest_name) {
792 return NT_STATUS_NO_MEMORY;
796 if (domain_guid) {
797 *domain_guid = TALLOC_P(mem_ctx, struct GUID);
798 if (!*domain_guid) {
799 return NT_STATUS_NO_MEMORY;
801 memcpy(*domain_guid,
802 &r.ctr.info.id12.dom_guid,
803 sizeof(struct GUID));
806 if (domain_sid && r.ctr.info.id12.ptr_dom_sid != 0) {
807 *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
808 if (!*domain_sid) {
809 return NT_STATUS_NO_MEMORY;
811 sid_copy(*domain_sid,
812 &r.ctr.info.id12.dom_sid.sid);
815 done:
817 return result;
820 NTSTATUS rpccli_lsa_set_info_policy(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
821 POLICY_HND *pol, uint16 info_class,
822 LSA_INFO_CTR ctr)
824 prs_struct qbuf, rbuf;
825 LSA_Q_SET_INFO q;
826 LSA_R_SET_INFO r;
827 NTSTATUS result;
829 ZERO_STRUCT(q);
830 ZERO_STRUCT(r);
832 init_q_set(&q, pol, info_class, ctr);
834 CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_SETINFOPOLICY,
835 q, r,
836 qbuf, rbuf,
837 lsa_io_q_set,
838 lsa_io_r_set,
839 NT_STATUS_UNSUCCESSFUL);
841 result = r.status;
843 if (!NT_STATUS_IS_OK(result)) {
844 goto done;
847 /* Return output parameters */
849 done:
851 return result;
856 * Enumerate list of trusted domains
858 * @param cli client state (cli_state) structure of the connection
859 * @param mem_ctx memory context
860 * @param pol opened lsa policy handle
861 * @param enum_ctx enumeration context ie. index of first returned domain entry
862 * @param pref_num_domains preferred max number of entries returned in one response
863 * @param num_domains total number of trusted domains returned by response
864 * @param domain_names returned trusted domain names
865 * @param domain_sids returned trusted domain sids
867 * @return nt status code of response
870 NTSTATUS rpccli_lsa_enum_trust_dom(struct rpc_pipe_client *cli,
871 TALLOC_CTX *mem_ctx,
872 POLICY_HND *pol, uint32 *enum_ctx,
873 uint32 *num_domains,
874 char ***domain_names, DOM_SID **domain_sids)
876 prs_struct qbuf, rbuf;
877 LSA_Q_ENUM_TRUST_DOM in;
878 LSA_R_ENUM_TRUST_DOM out;
879 int i;
880 fstring tmp;
882 ZERO_STRUCT(in);
883 ZERO_STRUCT(out);
885 /* 64k is enough for about 2000 trusted domains */
887 init_q_enum_trust_dom(&in, pol, *enum_ctx, 0x10000);
889 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMTRUSTDOM,
890 in, out,
891 qbuf, rbuf,
892 lsa_io_q_enum_trust_dom,
893 lsa_io_r_enum_trust_dom,
894 NT_STATUS_UNSUCCESSFUL );
897 /* check for an actual error */
899 if ( !NT_STATUS_IS_OK(out.status)
900 && !NT_STATUS_EQUAL(out.status, NT_STATUS_NO_MORE_ENTRIES)
901 && !NT_STATUS_EQUAL(out.status, STATUS_MORE_ENTRIES) )
903 return out.status;
906 /* Return output parameters */
908 *num_domains = out.count;
909 *enum_ctx = out.enum_context;
911 if ( out.count ) {
913 /* Allocate memory for trusted domain names and sids */
915 if ( !(*domain_names = TALLOC_ARRAY(mem_ctx, char *, out.count)) ) {
916 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
917 return NT_STATUS_NO_MEMORY;
920 if ( !(*domain_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, out.count)) ) {
921 DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
922 return NT_STATUS_NO_MEMORY;
925 /* Copy across names and sids */
927 for (i = 0; i < out.count; i++) {
929 rpcstr_pull( tmp, out.domlist->domains[i].name.string->buffer,
930 sizeof(tmp), out.domlist->domains[i].name.length, 0);
931 (*domain_names)[i] = talloc_strdup(mem_ctx, tmp);
933 sid_copy(&(*domain_sids)[i], &out.domlist->domains[i].sid->sid );
937 return out.status;
940 /** Enumerate privileges*/
942 NTSTATUS rpccli_lsa_enum_privilege(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
943 POLICY_HND *pol, uint32 *enum_context, uint32 pref_max_length,
944 uint32 *count, char ***privs_name, uint32 **privs_high, uint32 **privs_low)
946 prs_struct qbuf, rbuf;
947 LSA_Q_ENUM_PRIVS q;
948 LSA_R_ENUM_PRIVS r;
949 NTSTATUS result;
950 int i;
952 ZERO_STRUCT(q);
953 ZERO_STRUCT(r);
955 init_q_enum_privs(&q, pol, *enum_context, pref_max_length);
957 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUM_PRIVS,
958 q, r,
959 qbuf, rbuf,
960 lsa_io_q_enum_privs,
961 lsa_io_r_enum_privs,
962 NT_STATUS_UNSUCCESSFUL);
964 result = r.status;
966 if (!NT_STATUS_IS_OK(result)) {
967 goto done;
970 /* Return output parameters */
972 *enum_context = r.enum_context;
973 *count = r.count;
975 if (r.count) {
976 if (!((*privs_name = TALLOC_ARRAY(mem_ctx, char *, r.count)))) {
977 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
978 result = NT_STATUS_UNSUCCESSFUL;
979 goto done;
982 if (!((*privs_high = TALLOC_ARRAY(mem_ctx, uint32, r.count)))) {
983 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
984 result = NT_STATUS_UNSUCCESSFUL;
985 goto done;
988 if (!((*privs_low = TALLOC_ARRAY(mem_ctx, uint32, r.count)))) {
989 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
990 result = NT_STATUS_UNSUCCESSFUL;
991 goto done;
993 } else {
994 *privs_name = NULL;
995 *privs_high = NULL;
996 *privs_low = NULL;
999 for (i = 0; i < r.count; i++) {
1000 fstring name;
1002 rpcstr_pull_unistr2_fstring( name, &r.privs[i].name);
1004 (*privs_name)[i] = talloc_strdup(mem_ctx, name);
1006 (*privs_high)[i] = r.privs[i].luid_high;
1007 (*privs_low)[i] = r.privs[i].luid_low;
1010 done:
1012 return result;
1015 /** Get privilege name */
1017 NTSTATUS rpccli_lsa_get_dispname(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1018 POLICY_HND *pol, const char *name,
1019 uint16 lang_id, uint16 lang_id_sys,
1020 fstring description, uint16 *lang_id_desc)
1022 prs_struct qbuf, rbuf;
1023 LSA_Q_PRIV_GET_DISPNAME q;
1024 LSA_R_PRIV_GET_DISPNAME r;
1025 NTSTATUS result;
1027 ZERO_STRUCT(q);
1028 ZERO_STRUCT(r);
1030 init_lsa_priv_get_dispname(&q, pol, name, lang_id, lang_id_sys);
1032 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_PRIV_GET_DISPNAME,
1033 q, r,
1034 qbuf, rbuf,
1035 lsa_io_q_priv_get_dispname,
1036 lsa_io_r_priv_get_dispname,
1037 NT_STATUS_UNSUCCESSFUL);
1039 result = r.status;
1041 if (!NT_STATUS_IS_OK(result)) {
1042 goto done;
1045 /* Return output parameters */
1047 rpcstr_pull_unistr2_fstring(description , &r.desc);
1048 *lang_id_desc = r.lang_id;
1050 done:
1052 return result;
1055 /** Enumerate list of SIDs */
1057 NTSTATUS rpccli_lsa_enum_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1058 POLICY_HND *pol, uint32 *enum_ctx, uint32 pref_max_length,
1059 uint32 *num_sids, DOM_SID **sids)
1061 prs_struct qbuf, rbuf;
1062 LSA_Q_ENUM_ACCOUNTS q;
1063 LSA_R_ENUM_ACCOUNTS r;
1064 NTSTATUS result;
1065 int i;
1067 ZERO_STRUCT(q);
1068 ZERO_STRUCT(r);
1070 init_lsa_q_enum_accounts(&q, pol, *enum_ctx, pref_max_length);
1072 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUM_ACCOUNTS,
1073 q, r,
1074 qbuf, rbuf,
1075 lsa_io_q_enum_accounts,
1076 lsa_io_r_enum_accounts,
1077 NT_STATUS_UNSUCCESSFUL);
1079 result = r.status;
1081 if (!NT_STATUS_IS_OK(result)) {
1082 goto done;
1085 if (r.sids.num_entries==0)
1086 goto done;
1088 /* Return output parameters */
1090 *sids = TALLOC_ARRAY(mem_ctx, DOM_SID, r.sids.num_entries);
1091 if (!*sids) {
1092 DEBUG(0, ("(cli_lsa_enum_sids): out of memory\n"));
1093 result = NT_STATUS_UNSUCCESSFUL;
1094 goto done;
1097 /* Copy across names and sids */
1099 for (i = 0; i < r.sids.num_entries; i++) {
1100 sid_copy(&(*sids)[i], &r.sids.sid[i].sid);
1103 *num_sids= r.sids.num_entries;
1104 *enum_ctx = r.enum_context;
1106 done:
1108 return result;
1111 /** Create a LSA user handle
1113 * @param cli Handle on an initialised SMB connection
1115 * FIXME: The code is actually identical to open account
1116 * TODO: Check and code what the function should exactly do
1118 * */
1120 NTSTATUS rpccli_lsa_create_account(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1121 POLICY_HND *dom_pol, DOM_SID *sid, uint32 desired_access,
1122 POLICY_HND *user_pol)
1124 prs_struct qbuf, rbuf;
1125 LSA_Q_CREATEACCOUNT q;
1126 LSA_R_CREATEACCOUNT r;
1127 NTSTATUS result;
1129 ZERO_STRUCT(q);
1130 ZERO_STRUCT(r);
1132 /* Initialise input parameters */
1134 init_lsa_q_create_account(&q, dom_pol, sid, desired_access);
1136 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_CREATEACCOUNT,
1137 q, r,
1138 qbuf, rbuf,
1139 lsa_io_q_create_account,
1140 lsa_io_r_create_account,
1141 NT_STATUS_UNSUCCESSFUL);
1143 /* Return output parameters */
1145 result = r.status;
1147 if (NT_STATUS_IS_OK(result)) {
1148 *user_pol = r.pol;
1151 return result;
1154 /** Open a LSA user handle
1156 * @param cli Handle on an initialised SMB connection */
1158 NTSTATUS rpccli_lsa_open_account(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1159 POLICY_HND *dom_pol, DOM_SID *sid, uint32 des_access,
1160 POLICY_HND *user_pol)
1162 prs_struct qbuf, rbuf;
1163 LSA_Q_OPENACCOUNT q;
1164 LSA_R_OPENACCOUNT r;
1165 NTSTATUS result;
1167 ZERO_STRUCT(q);
1168 ZERO_STRUCT(r);
1170 /* Initialise input parameters */
1172 init_lsa_q_open_account(&q, dom_pol, sid, des_access);
1174 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENACCOUNT,
1175 q, r,
1176 qbuf, rbuf,
1177 lsa_io_q_open_account,
1178 lsa_io_r_open_account,
1179 NT_STATUS_UNSUCCESSFUL);
1181 /* Return output parameters */
1183 result = r.status;
1185 if (NT_STATUS_IS_OK(result)) {
1186 *user_pol = r.pol;
1189 return result;
1192 /** Enumerate user privileges
1194 * @param cli Handle on an initialised SMB connection */
1196 NTSTATUS rpccli_lsa_enum_privsaccount(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1197 POLICY_HND *pol, uint32 *count, LUID_ATTR **set)
1199 prs_struct qbuf, rbuf;
1200 LSA_Q_ENUMPRIVSACCOUNT q;
1201 LSA_R_ENUMPRIVSACCOUNT r;
1202 NTSTATUS result;
1203 int i;
1205 ZERO_STRUCT(q);
1206 ZERO_STRUCT(r);
1208 /* Initialise input parameters */
1210 init_lsa_q_enum_privsaccount(&q, pol);
1212 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMPRIVSACCOUNT,
1213 q, r,
1214 qbuf, rbuf,
1215 lsa_io_q_enum_privsaccount,
1216 lsa_io_r_enum_privsaccount,
1217 NT_STATUS_UNSUCCESSFUL);
1219 /* Return output parameters */
1221 result = r.status;
1223 if (!NT_STATUS_IS_OK(result)) {
1224 goto done;
1227 if (r.count == 0)
1228 goto done;
1230 if (!((*set = TALLOC_ARRAY(mem_ctx, LUID_ATTR, r.count)))) {
1231 DEBUG(0, ("(cli_lsa_enum_privsaccount): out of memory\n"));
1232 result = NT_STATUS_UNSUCCESSFUL;
1233 goto done;
1236 for (i=0; i<r.count; i++) {
1237 (*set)[i].luid.low = r.set.set[i].luid.low;
1238 (*set)[i].luid.high = r.set.set[i].luid.high;
1239 (*set)[i].attr = r.set.set[i].attr;
1242 *count=r.count;
1243 done:
1245 return result;
1248 /** Get a privilege value given its name */
1250 NTSTATUS rpccli_lsa_lookup_priv_value(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1251 POLICY_HND *pol, const char *name, LUID *luid)
1253 prs_struct qbuf, rbuf;
1254 LSA_Q_LOOKUP_PRIV_VALUE q;
1255 LSA_R_LOOKUP_PRIV_VALUE r;
1256 NTSTATUS result;
1258 ZERO_STRUCT(q);
1259 ZERO_STRUCT(r);
1261 /* Marshall data and send request */
1263 init_lsa_q_lookup_priv_value(&q, pol, name);
1265 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPPRIVVALUE,
1266 q, r,
1267 qbuf, rbuf,
1268 lsa_io_q_lookup_priv_value,
1269 lsa_io_r_lookup_priv_value,
1270 NT_STATUS_UNSUCCESSFUL);
1272 result = r.status;
1274 if (!NT_STATUS_IS_OK(result)) {
1275 goto done;
1278 /* Return output parameters */
1280 (*luid).low=r.luid.low;
1281 (*luid).high=r.luid.high;
1283 done:
1285 return result;
1288 /** Query LSA security object */
1290 NTSTATUS rpccli_lsa_query_secobj(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1291 POLICY_HND *pol, uint32 sec_info,
1292 SEC_DESC_BUF **psdb)
1294 prs_struct qbuf, rbuf;
1295 LSA_Q_QUERY_SEC_OBJ q;
1296 LSA_R_QUERY_SEC_OBJ r;
1297 NTSTATUS result;
1299 ZERO_STRUCT(q);
1300 ZERO_STRUCT(r);
1302 /* Marshall data and send request */
1304 init_q_query_sec_obj(&q, pol, sec_info);
1306 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYSECOBJ,
1307 q, r,
1308 qbuf, rbuf,
1309 lsa_io_q_query_sec_obj,
1310 lsa_io_r_query_sec_obj,
1311 NT_STATUS_UNSUCCESSFUL);
1313 result = r.status;
1315 if (!NT_STATUS_IS_OK(result)) {
1316 goto done;
1319 /* Return output parameters */
1321 if (psdb)
1322 *psdb = r.buf;
1324 done:
1326 return result;
1330 /* Enumerate account rights This is similar to enum_privileges but
1331 takes a SID directly, avoiding the open_account call.
1334 NTSTATUS rpccli_lsa_enum_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1335 POLICY_HND *pol, DOM_SID *sid,
1336 uint32 *count, char ***priv_names)
1338 prs_struct qbuf, rbuf;
1339 LSA_Q_ENUM_ACCT_RIGHTS q;
1340 LSA_R_ENUM_ACCT_RIGHTS r;
1341 NTSTATUS result;
1342 int i;
1343 fstring *privileges;
1344 char **names;
1346 ZERO_STRUCT(q);
1347 ZERO_STRUCT(r);
1349 /* Marshall data and send request */
1350 init_q_enum_acct_rights(&q, pol, 2, sid);
1352 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMACCTRIGHTS,
1353 q, r,
1354 qbuf, rbuf,
1355 lsa_io_q_enum_acct_rights,
1356 lsa_io_r_enum_acct_rights,
1357 NT_STATUS_UNSUCCESSFUL);
1359 result = r.status;
1361 if (!NT_STATUS_IS_OK(result)) {
1362 goto done;
1365 *count = r.count;
1366 if (! *count) {
1367 goto done;
1371 privileges = TALLOC_ARRAY( mem_ctx, fstring, *count );
1372 names = TALLOC_ARRAY( mem_ctx, char *, *count );
1374 if ((privileges == NULL) || (names == NULL)) {
1375 TALLOC_FREE(privileges);
1376 TALLOC_FREE(names);
1377 return NT_STATUS_NO_MEMORY;
1380 for ( i=0; i<*count; i++ ) {
1381 UNISTR4 *uni_string = &r.rights->strings[i];
1383 if ( !uni_string->string )
1384 continue;
1386 rpcstr_pull( privileges[i], uni_string->string->buffer, sizeof(privileges[i]), -1, STR_TERMINATE );
1388 /* now copy to the return array */
1389 names[i] = talloc_strdup( mem_ctx, privileges[i] );
1392 *priv_names = names;
1394 done:
1396 return result;
1401 /* add account rights to an account. */
1403 NTSTATUS rpccli_lsa_add_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1404 POLICY_HND *pol, DOM_SID sid,
1405 uint32 count, const char **privs_name)
1407 prs_struct qbuf, rbuf;
1408 LSA_Q_ADD_ACCT_RIGHTS q;
1409 LSA_R_ADD_ACCT_RIGHTS r;
1410 NTSTATUS result;
1412 ZERO_STRUCT(q);
1413 ZERO_STRUCT(r);
1415 /* Marshall data and send request */
1416 init_q_add_acct_rights(&q, pol, &sid, count, privs_name);
1418 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ADDACCTRIGHTS,
1419 q, r,
1420 qbuf, rbuf,
1421 lsa_io_q_add_acct_rights,
1422 lsa_io_r_add_acct_rights,
1423 NT_STATUS_UNSUCCESSFUL);
1425 result = r.status;
1427 if (!NT_STATUS_IS_OK(result)) {
1428 goto done;
1430 done:
1432 return result;
1436 /* remove account rights for an account. */
1438 NTSTATUS rpccli_lsa_remove_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1439 POLICY_HND *pol, DOM_SID sid, BOOL removeall,
1440 uint32 count, const char **privs_name)
1442 prs_struct qbuf, rbuf;
1443 LSA_Q_REMOVE_ACCT_RIGHTS q;
1444 LSA_R_REMOVE_ACCT_RIGHTS r;
1445 NTSTATUS result;
1447 ZERO_STRUCT(q);
1448 ZERO_STRUCT(r);
1450 /* Marshall data and send request */
1451 init_q_remove_acct_rights(&q, pol, &sid, removeall?1:0, count, privs_name);
1453 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_REMOVEACCTRIGHTS,
1454 q, r,
1455 qbuf, rbuf,
1456 lsa_io_q_remove_acct_rights,
1457 lsa_io_r_remove_acct_rights,
1458 NT_STATUS_UNSUCCESSFUL);
1460 result = r.status;
1462 if (!NT_STATUS_IS_OK(result)) {
1463 goto done;
1465 done:
1467 return result;
1471 #if 0
1473 /** An example of how to use the routines in this file. Fetch a DOMAIN
1474 sid. Does complete cli setup / teardown anonymously. */
1476 BOOL fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
1478 extern pstring global_myname;
1479 struct cli_state *cli;
1480 NTSTATUS result;
1481 POLICY_HND lsa_pol;
1482 BOOL ret = False;
1484 ZERO_STRUCT(cli);
1485 if((cli = cli_initialise()) == NULL) {
1486 DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
1487 return False;
1490 if(!resolve_name( remote_machine, &cli->dest_ip, 0x20)) {
1491 DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine));
1492 goto done;
1495 if (!cli_connect(cli, remote_machine, &cli->dest_ip)) {
1496 DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
1497 machine %s. Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1498 goto done;
1501 if (!attempt_netbios_session_request(cli, global_myname, remote_machine, &cli->dest_ip)) {
1502 DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n",
1503 remote_machine));
1504 goto done;
1507 cli->protocol = PROTOCOL_NT1;
1509 if (!cli_negprot(cli)) {
1510 DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
1511 Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1512 goto done;
1515 if (cli->protocol != PROTOCOL_NT1) {
1516 DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
1517 remote_machine));
1518 goto done;
1522 * Do an anonymous session setup.
1525 if (!cli_session_setup(cli, "", "", 0, "", 0, "")) {
1526 DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
1527 Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1528 goto done;
1531 if (!(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
1532 DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
1533 remote_machine));
1534 goto done;
1537 if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
1538 DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
1539 Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1540 goto done;
1543 /* Fetch domain sid */
1545 if (!cli_nt_session_open(cli, PI_LSARPC)) {
1546 DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
1547 goto done;
1550 result = cli_lsa_open_policy(cli, cli->mem_ctx, True, SEC_RIGHTS_QUERY_VALUE, &lsa_pol);
1551 if (!NT_STATUS_IS_OK(result)) {
1552 DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
1553 nt_errstr(result) ));
1554 goto done;
1557 result = cli_lsa_query_info_policy(cli, cli->mem_ctx, &lsa_pol, 5, domain, psid);
1558 if (!NT_STATUS_IS_OK(result)) {
1559 DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
1560 nt_errstr(result) ));
1561 goto done;
1564 ret = True;
1566 done:
1568 cli_shutdown(cli);
1569 return ret;
1572 #endif
1574 NTSTATUS rpccli_lsa_open_trusted_domain(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1575 POLICY_HND *pol, DOM_SID *dom_sid, uint32 access_mask,
1576 POLICY_HND *trustdom_pol)
1578 prs_struct qbuf, rbuf;
1579 LSA_Q_OPEN_TRUSTED_DOMAIN q;
1580 LSA_R_OPEN_TRUSTED_DOMAIN r;
1581 NTSTATUS result;
1583 ZERO_STRUCT(q);
1584 ZERO_STRUCT(r);
1586 /* Initialise input parameters */
1588 init_lsa_q_open_trusted_domain(&q, pol, dom_sid, access_mask);
1590 /* Marshall data and send request */
1592 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENTRUSTDOM,
1593 q, r,
1594 qbuf, rbuf,
1595 lsa_io_q_open_trusted_domain,
1596 lsa_io_r_open_trusted_domain,
1597 NT_STATUS_UNSUCCESSFUL);
1599 /* Return output parameters */
1601 result = r.status;
1603 if (NT_STATUS_IS_OK(result)) {
1604 *trustdom_pol = r.handle;
1607 return result;
1610 NTSTATUS rpccli_lsa_query_trusted_domain_info(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1611 POLICY_HND *pol,
1612 uint16 info_class,
1613 LSA_TRUSTED_DOMAIN_INFO **info)
1615 prs_struct qbuf, rbuf;
1616 LSA_Q_QUERY_TRUSTED_DOMAIN_INFO q;
1617 LSA_R_QUERY_TRUSTED_DOMAIN_INFO r;
1618 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1620 ZERO_STRUCT(q);
1621 ZERO_STRUCT(r);
1623 /* Marshall data and send request */
1625 init_q_query_trusted_domain_info(&q, pol, info_class);
1627 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYTRUSTDOMINFO,
1628 q, r,
1629 qbuf, rbuf,
1630 lsa_io_q_query_trusted_domain_info,
1631 lsa_io_r_query_trusted_domain_info,
1632 NT_STATUS_UNSUCCESSFUL);
1634 result = r.status;
1636 if (!NT_STATUS_IS_OK(result)) {
1637 goto done;
1640 *info = r.info;
1642 done:
1643 return result;
1646 NTSTATUS rpccli_lsa_open_trusted_domain_by_name(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1647 POLICY_HND *pol, const char *name, uint32 access_mask,
1648 POLICY_HND *trustdom_pol)
1650 prs_struct qbuf, rbuf;
1651 LSA_Q_OPEN_TRUSTED_DOMAIN_BY_NAME q;
1652 LSA_R_OPEN_TRUSTED_DOMAIN_BY_NAME r;
1653 NTSTATUS result;
1655 ZERO_STRUCT(q);
1656 ZERO_STRUCT(r);
1658 /* Initialise input parameters */
1660 init_lsa_q_open_trusted_domain_by_name(&q, pol, name, access_mask);
1662 /* Marshall data and send request */
1664 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENTRUSTDOMBYNAME,
1665 q, r,
1666 qbuf, rbuf,
1667 lsa_io_q_open_trusted_domain_by_name,
1668 lsa_io_r_open_trusted_domain_by_name,
1669 NT_STATUS_UNSUCCESSFUL);
1671 /* Return output parameters */
1673 result = r.status;
1675 if (NT_STATUS_IS_OK(result)) {
1676 *trustdom_pol = r.handle;
1679 return result;
1683 NTSTATUS rpccli_lsa_query_trusted_domain_info_by_sid(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1684 POLICY_HND *pol,
1685 uint16 info_class, DOM_SID *dom_sid,
1686 LSA_TRUSTED_DOMAIN_INFO **info)
1688 prs_struct qbuf, rbuf;
1689 LSA_Q_QUERY_TRUSTED_DOMAIN_INFO_BY_SID q;
1690 LSA_R_QUERY_TRUSTED_DOMAIN_INFO r;
1691 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1693 ZERO_STRUCT(q);
1694 ZERO_STRUCT(r);
1696 /* Marshall data and send request */
1698 init_q_query_trusted_domain_info_by_sid(&q, pol, info_class, dom_sid);
1700 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYTRUSTDOMINFOBYSID,
1701 q, r,
1702 qbuf, rbuf,
1703 lsa_io_q_query_trusted_domain_info_by_sid,
1704 lsa_io_r_query_trusted_domain_info,
1705 NT_STATUS_UNSUCCESSFUL);
1707 result = r.status;
1709 if (!NT_STATUS_IS_OK(result)) {
1710 goto done;
1713 *info = r.info;
1715 done:
1717 return result;
1720 NTSTATUS rpccli_lsa_query_trusted_domain_info_by_name(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1721 POLICY_HND *pol,
1722 uint16 info_class, const char *domain_name,
1723 LSA_TRUSTED_DOMAIN_INFO **info)
1725 prs_struct qbuf, rbuf;
1726 LSA_Q_QUERY_TRUSTED_DOMAIN_INFO_BY_NAME q;
1727 LSA_R_QUERY_TRUSTED_DOMAIN_INFO r;
1728 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1730 ZERO_STRUCT(q);
1731 ZERO_STRUCT(r);
1733 /* Marshall data and send request */
1735 init_q_query_trusted_domain_info_by_name(&q, pol, info_class, domain_name);
1737 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYTRUSTDOMINFOBYNAME,
1738 q, r,
1739 qbuf, rbuf,
1740 lsa_io_q_query_trusted_domain_info_by_name,
1741 lsa_io_r_query_trusted_domain_info,
1742 NT_STATUS_UNSUCCESSFUL);
1744 result = r.status;
1746 if (!NT_STATUS_IS_OK(result)) {
1747 goto done;
1750 *info = r.info;
1752 done:
1754 return result;
1757 NTSTATUS cli_lsa_query_domain_info_policy(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1758 POLICY_HND *pol,
1759 uint16 info_class, LSA_DOM_INFO_UNION **info)
1761 prs_struct qbuf, rbuf;
1762 LSA_Q_QUERY_DOM_INFO_POLICY q;
1763 LSA_R_QUERY_DOM_INFO_POLICY r;
1764 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1766 ZERO_STRUCT(q);
1767 ZERO_STRUCT(r);
1769 /* Marshall data and send request */
1771 init_q_query_dom_info(&q, pol, info_class);
1773 CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYDOMINFOPOL,
1774 q, r,
1775 qbuf, rbuf,
1776 lsa_io_q_query_dom_info,
1777 lsa_io_r_query_dom_info,
1778 NT_STATUS_UNSUCCESSFUL);
1780 result = r.status;
1782 if (!NT_STATUS_IS_OK(result)) {
1783 goto done;
1786 *info = r.info;
1788 done:
1789 return result;