r18271: Big change:
[sfrench/samba-autobuild/.git] / source / rpc_client / cli_lsarpc.c
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    
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 2 of the License, or
12    (at your option) any later version.
13    
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.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26 /** @defgroup lsa LSA - Local Security Architecture
27  *  @ingroup rpc_client
28  *
29  * @{
30  **/
31
32 /**
33  * @file cli_lsarpc.c
34  *
35  * RPC client routines for the LSA RPC pipe.  LSA means "local
36  * security authority", which is half of a password database.
37  **/
38
39 /** Open a LSA policy handle
40  *
41  * @param cli Handle on an initialised SMB connection */
42
43 NTSTATUS rpccli_lsa_open_policy(struct rpc_pipe_client *cli,
44                                 TALLOC_CTX *mem_ctx,
45                                 BOOL sec_qos, uint32 des_access,
46                                 POLICY_HND *pol)
47 {
48         prs_struct qbuf, rbuf;
49         LSA_Q_OPEN_POL q;
50         LSA_R_OPEN_POL r;
51         LSA_SEC_QOS qos;
52         NTSTATUS result;
53
54         ZERO_STRUCT(q);
55         ZERO_STRUCT(r);
56
57         /* Initialise input parameters */
58
59         if (sec_qos) {
60                 init_lsa_sec_qos(&qos, 2, 1, 0);
61                 init_q_open_pol(&q, '\\', 0, des_access, &qos);
62         } else {
63                 init_q_open_pol(&q, '\\', 0, des_access, NULL);
64         }
65
66         /* Marshall data and send request */
67
68         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENPOLICY,
69                         q, r,
70                         qbuf, rbuf,
71                         lsa_io_q_open_pol,
72                         lsa_io_r_open_pol,
73                         NT_STATUS_UNSUCCESSFUL );
74
75         /* Return output parameters */
76
77         result = r.status;
78
79         if (NT_STATUS_IS_OK(result)) {
80                 *pol = r.pol;
81 #ifdef __INSURE__
82                 pol->marker = MALLOC(1);
83 #endif
84         }
85
86         return result;
87 }
88
89 /** Open a LSA policy handle
90   *
91   * @param cli Handle on an initialised SMB connection 
92   */
93
94 NTSTATUS rpccli_lsa_open_policy2(struct rpc_pipe_client *cli,
95                                  TALLOC_CTX *mem_ctx, BOOL sec_qos,
96                                  uint32 des_access, POLICY_HND *pol)
97 {
98         prs_struct qbuf, rbuf;
99         LSA_Q_OPEN_POL2 q;
100         LSA_R_OPEN_POL2 r;
101         LSA_SEC_QOS qos;
102         NTSTATUS result;
103         char *srv_name_slash = talloc_asprintf(mem_ctx, "\\\\%s", cli->cli->desthost);
104
105         ZERO_STRUCT(q);
106         ZERO_STRUCT(r);
107
108         if (sec_qos) {
109                 init_lsa_sec_qos(&qos, 2, 1, 0);
110                 init_q_open_pol2(&q, srv_name_slash, 0, des_access, &qos);
111         } else {
112                 init_q_open_pol2(&q, srv_name_slash, 0, des_access, NULL);
113         }
114
115         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENPOLICY2,
116                         q, r,
117                         qbuf, rbuf,
118                         lsa_io_q_open_pol2,
119                         lsa_io_r_open_pol2,
120                         NT_STATUS_UNSUCCESSFUL );
121
122         /* Return output parameters */
123
124         result = r.status;
125
126         if (NT_STATUS_IS_OK(result)) {
127                 *pol = r.pol;
128 #ifdef __INSURE__
129                 pol->marker = (char *)malloc(1);
130 #endif
131         }
132
133         return result;
134 }
135
136 /** Close a LSA policy handle */
137
138 NTSTATUS rpccli_lsa_close(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, 
139                           POLICY_HND *pol)
140 {
141         prs_struct qbuf, rbuf;
142         LSA_Q_CLOSE q;
143         LSA_R_CLOSE r;
144         NTSTATUS result;
145
146         ZERO_STRUCT(q);
147         ZERO_STRUCT(r);
148
149         init_lsa_q_close(&q, pol);
150
151         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_CLOSE,
152                         q, r,
153                         qbuf, rbuf,
154                         lsa_io_q_close,
155                         lsa_io_r_close,
156                         NT_STATUS_UNSUCCESSFUL );
157
158         /* Return output parameters */
159
160         result = r.status;
161
162         if (NT_STATUS_IS_OK(result)) {
163 #ifdef __INSURE__
164                 SAFE_FREE(pol->marker);
165 #endif
166                 *pol = r.pol;
167         }
168
169         return result;
170 }
171
172 /** Lookup a list of sids */
173
174 NTSTATUS rpccli_lsa_lookup_sids(struct rpc_pipe_client *cli,
175                                 TALLOC_CTX *mem_ctx,
176                                 POLICY_HND *pol, int num_sids,
177                                 const DOM_SID *sids, 
178                                 char ***domains,
179                                 char ***names,
180                                 enum lsa_SidType **types)
181 {
182         prs_struct qbuf, rbuf;
183         LSA_Q_LOOKUP_SIDS q;
184         LSA_R_LOOKUP_SIDS r;
185         DOM_R_REF ref;
186         LSA_TRANS_NAME_ENUM t_names;
187         NTSTATUS result = NT_STATUS_OK;
188         int i;
189
190         ZERO_STRUCT(q);
191         ZERO_STRUCT(r);
192
193         init_q_lookup_sids(mem_ctx, &q, pol, num_sids, sids, 1);
194
195         ZERO_STRUCT(ref);
196         ZERO_STRUCT(t_names);
197
198         r.dom_ref = &ref;
199         r.names = &t_names;
200
201         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPSIDS,
202                         q, r,
203                         qbuf, rbuf,
204                         lsa_io_q_lookup_sids,
205                         lsa_io_r_lookup_sids,
206                         NT_STATUS_UNSUCCESSFUL );
207
208         if (!NT_STATUS_IS_OK(r.status) &&
209             NT_STATUS_V(r.status) != NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
210           
211                 /* An actual error occured */
212                 result = r.status;
213
214                 goto done;
215         }
216
217         /* Return output parameters */
218
219         if (r.mapped_count == 0) {
220                 result = NT_STATUS_NONE_MAPPED;
221                 goto done;
222         }
223
224         if (!((*domains) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
225                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
226                 result = NT_STATUS_NO_MEMORY;
227                 goto done;
228         }
229
230         if (!((*names) = TALLOC_ARRAY(mem_ctx, char *, num_sids))) {
231                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
232                 result = NT_STATUS_NO_MEMORY;
233                 goto done;
234         }
235
236         if (!((*types) = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_sids))) {
237                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
238                 result = NT_STATUS_NO_MEMORY;
239                 goto done;
240         }
241                 
242         for (i = 0; i < num_sids; i++) {
243                 fstring name, dom_name;
244                 uint32 dom_idx = t_names.name[i].domain_idx;
245
246                 /* Translate optimised name through domain index array */
247
248                 if (dom_idx != 0xffffffff) {
249
250                         rpcstr_pull_unistr2_fstring(
251                                 dom_name, &ref.ref_dom[dom_idx].uni_dom_name);
252                         rpcstr_pull_unistr2_fstring(
253                                 name, &t_names.uni_name[i]);
254
255                         (*names)[i] = talloc_strdup(mem_ctx, name);
256                         (*domains)[i] = talloc_strdup(mem_ctx, dom_name);
257                         (*types)[i] = (enum lsa_SidType)t_names.name[i].sid_name_use;
258                         
259                         if (((*names)[i] == NULL) || ((*domains)[i] == NULL)) {
260                                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
261                                 result = NT_STATUS_UNSUCCESSFUL;
262                                 goto done;
263                         }
264
265                 } else {
266                         (*names)[i] = NULL;
267                         (*domains)[i] = NULL;
268                         (*types)[i] = SID_NAME_UNKNOWN;
269                 }
270         }
271
272  done:
273
274         return result;
275 }
276
277 /** Lookup a list of names */
278
279 NTSTATUS rpccli_lsa_lookup_names(struct rpc_pipe_client *cli,
280                                  TALLOC_CTX *mem_ctx,
281                                  POLICY_HND *pol, int num_names, 
282                                  const char **names,
283                                  const char ***dom_names,
284                                  DOM_SID **sids,
285                                  enum lsa_SidType **types)
286 {
287         prs_struct qbuf, rbuf;
288         LSA_Q_LOOKUP_NAMES q;
289         LSA_R_LOOKUP_NAMES r;
290         DOM_R_REF ref;
291         NTSTATUS result;
292         int i;
293         
294         ZERO_STRUCT(q);
295         ZERO_STRUCT(r);
296
297         ZERO_STRUCT(ref);
298         r.dom_ref = &ref;
299
300         init_q_lookup_names(mem_ctx, &q, pol, num_names, names);
301
302         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPNAMES,
303                         q, r,
304                         qbuf, rbuf,
305                         lsa_io_q_lookup_names,
306                         lsa_io_r_lookup_names,
307                         NT_STATUS_UNSUCCESSFUL);
308
309         result = r.status;
310
311         if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
312             NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
313
314                 /* An actual error occured */
315
316                 goto done;
317         }
318
319         /* Return output parameters */
320
321         if (r.mapped_count == 0) {
322                 result = NT_STATUS_NONE_MAPPED;
323                 goto done;
324         }
325
326         if (!((*sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_names)))) {
327                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
328                 result = NT_STATUS_NO_MEMORY;
329                 goto done;
330         }
331
332         if (!((*types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_names)))) {
333                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
334                 result = NT_STATUS_NO_MEMORY;
335                 goto done;
336         }
337
338         if (dom_names != NULL) {
339                 *dom_names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
340                 if (*dom_names == NULL) {
341                         DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
342                         result = NT_STATUS_NO_MEMORY;
343                         goto done;
344                 }
345         }
346
347         for (i = 0; i < num_names; i++) {
348                 DOM_RID *t_rids = r.dom_rid;
349                 uint32 dom_idx = t_rids[i].rid_idx;
350                 uint32 dom_rid = t_rids[i].rid;
351                 DOM_SID *sid = &(*sids)[i];
352
353                 /* Translate optimised sid through domain index array */
354
355                 if (dom_idx == 0xffffffff) {
356                         /* Nothing to do, this is unknown */
357                         ZERO_STRUCTP(sid);
358                         (*types)[i] = SID_NAME_UNKNOWN;
359                         continue;
360                 }
361
362                 sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
363
364                 if (dom_rid != 0xffffffff) {
365                         sid_append_rid(sid, dom_rid);
366                 }
367
368                 (*types)[i] = (enum lsa_SidType)t_rids[i].type;
369
370                 if (dom_names == NULL) {
371                         continue;
372                 }
373
374                 (*dom_names)[i] = rpcstr_pull_unistr2_talloc(
375                         *dom_names, &ref.ref_dom[dom_idx].uni_dom_name);
376         }
377
378  done:
379
380         return result;
381 }
382
383 NTSTATUS rpccli_lsa_query_info_policy_new(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
384                                           POLICY_HND *pol, uint16 info_class,
385                                           LSA_INFO_CTR *ctr) 
386 {
387         prs_struct qbuf, rbuf;
388         LSA_Q_QUERY_INFO q;
389         LSA_R_QUERY_INFO r;
390         NTSTATUS result;
391
392         ZERO_STRUCT(q);
393         ZERO_STRUCT(r);
394
395         init_q_query(&q, pol, info_class);
396
397         CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFOPOLICY,
398                 q, r,
399                 qbuf, rbuf,
400                 lsa_io_q_query,
401                 lsa_io_r_query,
402                 NT_STATUS_UNSUCCESSFUL);
403
404         result = r.status;
405
406         if (!NT_STATUS_IS_OK(result)) {
407                 goto done;
408         }
409
410  done:
411
412         *ctr = r.ctr;
413         
414         return result;
415 }
416
417 NTSTATUS rpccli_lsa_query_info_policy2_new(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
418                                           POLICY_HND *pol, uint16 info_class,
419                                           LSA_INFO_CTR2 *ctr) 
420 {
421         prs_struct qbuf, rbuf;
422         LSA_Q_QUERY_INFO2 q;
423         LSA_R_QUERY_INFO2 r;
424         NTSTATUS result;
425
426         ZERO_STRUCT(q);
427         ZERO_STRUCT(r);
428
429         init_q_query2(&q, pol, info_class);
430
431         CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFO2,
432                 q, r,
433                 qbuf, rbuf,
434                 lsa_io_q_query_info2,
435                 lsa_io_r_query_info2,
436                 NT_STATUS_UNSUCCESSFUL);
437
438         result = r.status;
439
440         if (!NT_STATUS_IS_OK(result)) {
441                 goto done;
442         }
443
444  done:
445
446         *ctr = r.ctr;
447         
448         return result;
449 }
450
451
452
453 /** Query info policy
454  *
455  *  @param domain_sid - returned remote server's domain sid */
456
457 NTSTATUS rpccli_lsa_query_info_policy(struct rpc_pipe_client *cli,
458                                       TALLOC_CTX *mem_ctx,
459                                       POLICY_HND *pol, uint16 info_class, 
460                                       char **domain_name, DOM_SID **domain_sid)
461 {
462         prs_struct qbuf, rbuf;
463         LSA_Q_QUERY_INFO q;
464         LSA_R_QUERY_INFO r;
465         NTSTATUS result;
466
467         ZERO_STRUCT(q);
468         ZERO_STRUCT(r);
469
470         init_q_query(&q, pol, info_class);
471
472         CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_QUERYINFOPOLICY,
473                 q, r,
474                 qbuf, rbuf,
475                 lsa_io_q_query,
476                 lsa_io_r_query,
477                 NT_STATUS_UNSUCCESSFUL);
478
479         result = r.status;
480
481         if (!NT_STATUS_IS_OK(result)) {
482                 goto done;
483         }
484
485         /* Return output parameters */
486
487         switch (info_class) {
488
489         case 3:
490                 if (domain_name && (r.ctr.info.id3.buffer_dom_name != 0)) {
491                         *domain_name = unistr2_tdup(mem_ctx, 
492                                                    &r.ctr.info.id3.
493                                                    uni_domain_name);
494                         if (!*domain_name) {
495                                 return NT_STATUS_NO_MEMORY;
496                         }
497                 }
498
499                 if (domain_sid && (r.ctr.info.id3.buffer_dom_sid != 0)) {
500                         *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
501                         if (!*domain_sid) {
502                                 return NT_STATUS_NO_MEMORY;
503                         }
504                         sid_copy(*domain_sid, &r.ctr.info.id3.dom_sid.sid);
505                 }
506
507                 break;
508
509         case 5:
510                 
511                 if (domain_name && (r.ctr.info.id5.buffer_dom_name != 0)) {
512                         *domain_name = unistr2_tdup(mem_ctx, 
513                                                    &r.ctr.info.id5.
514                                                    uni_domain_name);
515                         if (!*domain_name) {
516                                 return NT_STATUS_NO_MEMORY;
517                         }
518                 }
519                         
520                 if (domain_sid && (r.ctr.info.id5.buffer_dom_sid != 0)) {
521                         *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
522                         if (!*domain_sid) {
523                                 return NT_STATUS_NO_MEMORY;
524                         }
525                         sid_copy(*domain_sid, &r.ctr.info.id5.dom_sid.sid);
526                 }
527                 break;
528                         
529         default:
530                 DEBUG(3, ("unknown info class %d\n", info_class));
531                 break;                
532         }
533         
534  done:
535
536         return result;
537 }
538
539 /** Query info policy2
540  *
541  *  @param domain_name - returned remote server's domain name
542  *  @param dns_name - returned remote server's dns domain name
543  *  @param forest_name - returned remote server's forest name
544  *  @param domain_guid - returned remote server's domain guid
545  *  @param domain_sid - returned remote server's domain sid */
546
547 NTSTATUS rpccli_lsa_query_info_policy2(struct rpc_pipe_client *cli,
548                                        TALLOC_CTX *mem_ctx,
549                                        POLICY_HND *pol, uint16 info_class, 
550                                        char **domain_name, char **dns_name,
551                                        char **forest_name,
552                                        struct uuid **domain_guid,
553                                        DOM_SID **domain_sid)
554 {
555         prs_struct qbuf, rbuf;
556         LSA_Q_QUERY_INFO2 q;
557         LSA_R_QUERY_INFO2 r;
558         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
559
560         if (info_class != 12)
561                 goto done;
562
563         ZERO_STRUCT(q);
564         ZERO_STRUCT(r);
565
566         init_q_query2(&q, pol, info_class);
567
568         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYINFO2,
569                 q, r,
570                 qbuf, rbuf,
571                 lsa_io_q_query_info2,
572                 lsa_io_r_query_info2,
573                 NT_STATUS_UNSUCCESSFUL);
574
575         result = r.status;
576
577         if (!NT_STATUS_IS_OK(result)) {
578                 goto done;
579         }
580
581         /* Return output parameters */
582
583         ZERO_STRUCTP(domain_guid);
584
585         if (domain_name && r.ctr.info.id12.hdr_nb_dom_name.buffer) {
586                 *domain_name = unistr2_tdup(mem_ctx, 
587                                             &r.ctr.info.id12
588                                             .uni_nb_dom_name);
589                 if (!*domain_name) {
590                         return NT_STATUS_NO_MEMORY;
591                 }
592         }
593         if (dns_name && r.ctr.info.id12.hdr_dns_dom_name.buffer) {
594                 *dns_name = unistr2_tdup(mem_ctx, 
595                                          &r.ctr.info.id12
596                                          .uni_dns_dom_name);
597                 if (!*dns_name) {
598                         return NT_STATUS_NO_MEMORY;
599                 }
600         }
601         if (forest_name && r.ctr.info.id12.hdr_forest_name.buffer) {
602                 *forest_name = unistr2_tdup(mem_ctx, 
603                                             &r.ctr.info.id12
604                                             .uni_forest_name);
605                 if (!*forest_name) {
606                         return NT_STATUS_NO_MEMORY;
607                 }
608         }
609         
610         if (domain_guid) {
611                 *domain_guid = TALLOC_P(mem_ctx, struct uuid);
612                 if (!*domain_guid) {
613                         return NT_STATUS_NO_MEMORY;
614                 }
615                 memcpy(*domain_guid, 
616                        &r.ctr.info.id12.dom_guid, 
617                        sizeof(struct uuid));
618         }
619
620         if (domain_sid && r.ctr.info.id12.ptr_dom_sid != 0) {
621                 *domain_sid = TALLOC_P(mem_ctx, DOM_SID);
622                 if (!*domain_sid) {
623                         return NT_STATUS_NO_MEMORY;
624                 }
625                 sid_copy(*domain_sid, 
626                          &r.ctr.info.id12.dom_sid.sid);
627         }
628         
629  done:
630
631         return result;
632 }
633
634 NTSTATUS rpccli_lsa_set_info_policy(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
635                                     POLICY_HND *pol, uint16 info_class,
636                                     LSA_INFO_CTR ctr) 
637 {
638         prs_struct qbuf, rbuf;
639         LSA_Q_SET_INFO q;
640         LSA_R_SET_INFO r;
641         NTSTATUS result;
642
643         ZERO_STRUCT(q);
644         ZERO_STRUCT(r);
645
646         init_q_set(&q, pol, info_class, ctr);
647
648         CLI_DO_RPC(cli, mem_ctx, PI_LSARPC, LSA_SETINFOPOLICY,
649                 q, r,
650                 qbuf, rbuf,
651                 lsa_io_q_set,
652                 lsa_io_r_set,
653                 NT_STATUS_UNSUCCESSFUL);
654
655         result = r.status;
656
657         if (!NT_STATUS_IS_OK(result)) {
658                 goto done;
659         }
660
661         /* Return output parameters */
662
663  done:
664
665         return result;
666 }
667
668
669 /**
670  * Enumerate list of trusted domains
671  *
672  * @param cli client state (cli_state) structure of the connection
673  * @param mem_ctx memory context
674  * @param pol opened lsa policy handle
675  * @param enum_ctx enumeration context ie. index of first returned domain entry
676  * @param pref_num_domains preferred max number of entries returned in one response
677  * @param num_domains total number of trusted domains returned by response
678  * @param domain_names returned trusted domain names
679  * @param domain_sids returned trusted domain sids
680  *
681  * @return nt status code of response
682  **/
683
684 NTSTATUS rpccli_lsa_enum_trust_dom(struct rpc_pipe_client *cli,
685                                    TALLOC_CTX *mem_ctx,
686                                    POLICY_HND *pol, uint32 *enum_ctx, 
687                                    uint32 *num_domains,
688                                    char ***domain_names, DOM_SID **domain_sids)
689 {
690         prs_struct qbuf, rbuf;
691         LSA_Q_ENUM_TRUST_DOM in;
692         LSA_R_ENUM_TRUST_DOM out;
693         int i;
694         fstring tmp;
695
696         ZERO_STRUCT(in);
697         ZERO_STRUCT(out);
698
699         /* 64k is enough for about 2000 trusted domains */
700         
701         init_q_enum_trust_dom(&in, pol, *enum_ctx, 0x10000);
702
703         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMTRUSTDOM, 
704                     in, out, 
705                     qbuf, rbuf,
706                     lsa_io_q_enum_trust_dom,
707                     lsa_io_r_enum_trust_dom, 
708                     NT_STATUS_UNSUCCESSFUL );
709
710
711         /* check for an actual error */
712
713         if ( !NT_STATUS_IS_OK(out.status) 
714                 && !NT_STATUS_EQUAL(out.status, NT_STATUS_NO_MORE_ENTRIES) 
715                 && !NT_STATUS_EQUAL(out.status, STATUS_MORE_ENTRIES) )
716         {
717                 return out.status;
718         }
719                 
720         /* Return output parameters */
721
722         *num_domains  = out.count;
723         *enum_ctx     = out.enum_context;
724         
725         if ( out.count ) {
726
727                 /* Allocate memory for trusted domain names and sids */
728
729                 if ( !(*domain_names = TALLOC_ARRAY(mem_ctx, char *, out.count)) ) {
730                         DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
731                         return NT_STATUS_NO_MEMORY;
732                 }
733
734                 if ( !(*domain_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, out.count)) ) {
735                         DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
736                         return NT_STATUS_NO_MEMORY;
737                 }
738
739                 /* Copy across names and sids */
740
741                 for (i = 0; i < out.count; i++) {
742
743                         rpcstr_pull( tmp, out.domlist->domains[i].name.string->buffer, 
744                                 sizeof(tmp), out.domlist->domains[i].name.length, 0);
745                         (*domain_names)[i] = talloc_strdup(mem_ctx, tmp);
746
747                         sid_copy(&(*domain_sids)[i], &out.domlist->domains[i].sid->sid );
748                 }
749         }
750
751         return out.status;
752 }
753
754 /** Enumerate privileges*/
755
756 NTSTATUS rpccli_lsa_enum_privilege(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
757                                 POLICY_HND *pol, uint32 *enum_context, uint32 pref_max_length,
758                                 uint32 *count, char ***privs_name, uint32 **privs_high, uint32 **privs_low)
759 {
760         prs_struct qbuf, rbuf;
761         LSA_Q_ENUM_PRIVS q;
762         LSA_R_ENUM_PRIVS r;
763         NTSTATUS result;
764         int i;
765
766         ZERO_STRUCT(q);
767         ZERO_STRUCT(r);
768
769         init_q_enum_privs(&q, pol, *enum_context, pref_max_length);
770
771         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUM_PRIVS,
772                 q, r,
773                 qbuf, rbuf,
774                 lsa_io_q_enum_privs,
775                 lsa_io_r_enum_privs,
776                 NT_STATUS_UNSUCCESSFUL);
777
778         result = r.status;
779
780         if (!NT_STATUS_IS_OK(result)) {
781                 goto done;
782         }
783
784         /* Return output parameters */
785
786         *enum_context = r.enum_context;
787         *count = r.count;
788
789         if (!((*privs_name = TALLOC_ARRAY(mem_ctx, char *, r.count)))) {
790                 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
791                 result = NT_STATUS_UNSUCCESSFUL;
792                 goto done;
793         }
794
795         if (!((*privs_high = TALLOC_ARRAY(mem_ctx, uint32, r.count)))) {
796                 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
797                 result = NT_STATUS_UNSUCCESSFUL;
798                 goto done;
799         }
800
801         if (!((*privs_low = TALLOC_ARRAY(mem_ctx, uint32, r.count)))) {
802                 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
803                 result = NT_STATUS_UNSUCCESSFUL;
804                 goto done;
805         }
806
807         for (i = 0; i < r.count; i++) {
808                 fstring name;
809
810                 rpcstr_pull_unistr2_fstring( name, &r.privs[i].name);
811
812                 (*privs_name)[i] = talloc_strdup(mem_ctx, name);
813
814                 (*privs_high)[i] = r.privs[i].luid_high;
815                 (*privs_low)[i] = r.privs[i].luid_low;
816         }
817
818  done:
819
820         return result;
821 }
822
823 /** Get privilege name */
824
825 NTSTATUS rpccli_lsa_get_dispname(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
826                               POLICY_HND *pol, const char *name, 
827                               uint16 lang_id, uint16 lang_id_sys,
828                               fstring description, uint16 *lang_id_desc)
829 {
830         prs_struct qbuf, rbuf;
831         LSA_Q_PRIV_GET_DISPNAME q;
832         LSA_R_PRIV_GET_DISPNAME r;
833         NTSTATUS result;
834
835         ZERO_STRUCT(q);
836         ZERO_STRUCT(r);
837
838         init_lsa_priv_get_dispname(&q, pol, name, lang_id, lang_id_sys);
839
840         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_PRIV_GET_DISPNAME,
841                 q, r,
842                 qbuf, rbuf,
843                 lsa_io_q_priv_get_dispname,
844                 lsa_io_r_priv_get_dispname,
845                 NT_STATUS_UNSUCCESSFUL);
846
847         result = r.status;
848
849         if (!NT_STATUS_IS_OK(result)) {
850                 goto done;
851         }
852
853         /* Return output parameters */
854         
855         rpcstr_pull_unistr2_fstring(description , &r.desc);
856         *lang_id_desc = r.lang_id;
857
858  done:
859
860         return result;
861 }
862
863 /** Enumerate list of SIDs  */
864
865 NTSTATUS rpccli_lsa_enum_sids(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
866                                 POLICY_HND *pol, uint32 *enum_ctx, uint32 pref_max_length, 
867                                 uint32 *num_sids, DOM_SID **sids)
868 {
869         prs_struct qbuf, rbuf;
870         LSA_Q_ENUM_ACCOUNTS q;
871         LSA_R_ENUM_ACCOUNTS r;
872         NTSTATUS result;
873         int i;
874
875         ZERO_STRUCT(q);
876         ZERO_STRUCT(r);
877
878         init_lsa_q_enum_accounts(&q, pol, *enum_ctx, pref_max_length);
879
880         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUM_ACCOUNTS,
881                 q, r,
882                 qbuf, rbuf,
883                 lsa_io_q_enum_accounts,
884                 lsa_io_r_enum_accounts,
885                 NT_STATUS_UNSUCCESSFUL);
886
887         result = r.status;
888
889         if (!NT_STATUS_IS_OK(result)) {
890                 goto done;
891         }
892
893         if (r.sids.num_entries==0)
894                 goto done;
895
896         /* Return output parameters */
897
898         *sids = TALLOC_ARRAY(mem_ctx, DOM_SID, r.sids.num_entries);
899         if (!*sids) {
900                 DEBUG(0, ("(cli_lsa_enum_sids): out of memory\n"));
901                 result = NT_STATUS_UNSUCCESSFUL;
902                 goto done;
903         }
904
905         /* Copy across names and sids */
906
907         for (i = 0; i < r.sids.num_entries; i++) {
908                 sid_copy(&(*sids)[i], &r.sids.sid[i].sid);
909         }
910
911         *num_sids= r.sids.num_entries;
912         *enum_ctx = r.enum_context;
913
914  done:
915
916         return result;
917 }
918
919 /** Create a LSA user handle
920  *
921  * @param cli Handle on an initialised SMB connection
922  *
923  * FIXME: The code is actually identical to open account
924  * TODO: Check and code what the function should exactly do
925  *
926  * */
927
928 NTSTATUS rpccli_lsa_create_account(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
929                              POLICY_HND *dom_pol, DOM_SID *sid, uint32 desired_access, 
930                              POLICY_HND *user_pol)
931 {
932         prs_struct qbuf, rbuf;
933         LSA_Q_CREATEACCOUNT q;
934         LSA_R_CREATEACCOUNT r;
935         NTSTATUS result;
936
937         ZERO_STRUCT(q);
938         ZERO_STRUCT(r);
939
940         /* Initialise input parameters */
941
942         init_lsa_q_create_account(&q, dom_pol, sid, desired_access);
943
944         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_CREATEACCOUNT,
945                 q, r,
946                 qbuf, rbuf,
947                 lsa_io_q_create_account,
948                 lsa_io_r_create_account,
949                 NT_STATUS_UNSUCCESSFUL);
950
951         /* Return output parameters */
952
953         result = r.status;
954
955         if (NT_STATUS_IS_OK(result)) {
956                 *user_pol = r.pol;
957         }
958
959         return result;
960 }
961
962 /** Open a LSA user handle
963  *
964  * @param cli Handle on an initialised SMB connection */
965
966 NTSTATUS rpccli_lsa_open_account(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
967                              POLICY_HND *dom_pol, DOM_SID *sid, uint32 des_access, 
968                              POLICY_HND *user_pol)
969 {
970         prs_struct qbuf, rbuf;
971         LSA_Q_OPENACCOUNT q;
972         LSA_R_OPENACCOUNT r;
973         NTSTATUS result;
974
975         ZERO_STRUCT(q);
976         ZERO_STRUCT(r);
977
978         /* Initialise input parameters */
979
980         init_lsa_q_open_account(&q, dom_pol, sid, des_access);
981
982         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENACCOUNT,
983                 q, r,
984                 qbuf, rbuf,
985                 lsa_io_q_open_account,
986                 lsa_io_r_open_account,
987                 NT_STATUS_UNSUCCESSFUL);
988
989         /* Return output parameters */
990
991         result = r.status;
992
993         if (NT_STATUS_IS_OK(result)) {
994                 *user_pol = r.pol;
995         }
996
997         return result;
998 }
999
1000 /** Enumerate user privileges
1001  *
1002  * @param cli Handle on an initialised SMB connection */
1003
1004 NTSTATUS rpccli_lsa_enum_privsaccount(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1005                              POLICY_HND *pol, uint32 *count, LUID_ATTR **set)
1006 {
1007         prs_struct qbuf, rbuf;
1008         LSA_Q_ENUMPRIVSACCOUNT q;
1009         LSA_R_ENUMPRIVSACCOUNT r;
1010         NTSTATUS result;
1011         int i;
1012
1013         ZERO_STRUCT(q);
1014         ZERO_STRUCT(r);
1015
1016         /* Initialise input parameters */
1017
1018         init_lsa_q_enum_privsaccount(&q, pol);
1019
1020         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMPRIVSACCOUNT,
1021                 q, r,
1022                 qbuf, rbuf,
1023                 lsa_io_q_enum_privsaccount,
1024                 lsa_io_r_enum_privsaccount,
1025                 NT_STATUS_UNSUCCESSFUL);
1026
1027         /* Return output parameters */
1028
1029         result = r.status;
1030
1031         if (!NT_STATUS_IS_OK(result)) {
1032                 goto done;
1033         }
1034
1035         if (r.count == 0)
1036                 goto done;
1037
1038         if (!((*set = TALLOC_ARRAY(mem_ctx, LUID_ATTR, r.count)))) {
1039                 DEBUG(0, ("(cli_lsa_enum_privsaccount): out of memory\n"));
1040                 result = NT_STATUS_UNSUCCESSFUL;
1041                 goto done;
1042         }
1043
1044         for (i=0; i<r.count; i++) {
1045                 (*set)[i].luid.low = r.set.set[i].luid.low;
1046                 (*set)[i].luid.high = r.set.set[i].luid.high;
1047                 (*set)[i].attr = r.set.set[i].attr;
1048         }
1049
1050         *count=r.count;
1051  done:
1052
1053         return result;
1054 }
1055
1056 /** Get a privilege value given its name */
1057
1058 NTSTATUS rpccli_lsa_lookup_priv_value(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1059                                  POLICY_HND *pol, const char *name, LUID *luid)
1060 {
1061         prs_struct qbuf, rbuf;
1062         LSA_Q_LOOKUP_PRIV_VALUE q;
1063         LSA_R_LOOKUP_PRIV_VALUE r;
1064         NTSTATUS result;
1065
1066         ZERO_STRUCT(q);
1067         ZERO_STRUCT(r);
1068
1069         /* Marshall data and send request */
1070
1071         init_lsa_q_lookup_priv_value(&q, pol, name);
1072
1073         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_LOOKUPPRIVVALUE,
1074                 q, r,
1075                 qbuf, rbuf,
1076                 lsa_io_q_lookup_priv_value,
1077                 lsa_io_r_lookup_priv_value,
1078                 NT_STATUS_UNSUCCESSFUL);
1079
1080         result = r.status;
1081
1082         if (!NT_STATUS_IS_OK(result)) {
1083                 goto done;
1084         }
1085
1086         /* Return output parameters */
1087
1088         (*luid).low=r.luid.low;
1089         (*luid).high=r.luid.high;
1090
1091  done:
1092
1093         return result;
1094 }
1095
1096 /** Query LSA security object */
1097
1098 NTSTATUS rpccli_lsa_query_secobj(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1099                               POLICY_HND *pol, uint32 sec_info, 
1100                               SEC_DESC_BUF **psdb)
1101 {
1102         prs_struct qbuf, rbuf;
1103         LSA_Q_QUERY_SEC_OBJ q;
1104         LSA_R_QUERY_SEC_OBJ r;
1105         NTSTATUS result;
1106
1107         ZERO_STRUCT(q);
1108         ZERO_STRUCT(r);
1109
1110         /* Marshall data and send request */
1111
1112         init_q_query_sec_obj(&q, pol, sec_info);
1113
1114         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYSECOBJ,
1115                 q, r,
1116                 qbuf, rbuf,
1117                 lsa_io_q_query_sec_obj,
1118                 lsa_io_r_query_sec_obj,
1119                 NT_STATUS_UNSUCCESSFUL);
1120
1121         result = r.status;
1122
1123         if (!NT_STATUS_IS_OK(result)) {
1124                 goto done;
1125         }
1126
1127         /* Return output parameters */
1128
1129         if (psdb)
1130                 *psdb = r.buf;
1131
1132  done:
1133
1134         return result;
1135 }
1136
1137
1138 /* Enumerate account rights This is similar to enum_privileges but
1139    takes a SID directly, avoiding the open_account call.
1140 */
1141
1142 NTSTATUS rpccli_lsa_enum_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1143                                      POLICY_HND *pol, DOM_SID *sid,
1144                                      uint32 *count, char ***priv_names)
1145 {
1146         prs_struct qbuf, rbuf;
1147         LSA_Q_ENUM_ACCT_RIGHTS q;
1148         LSA_R_ENUM_ACCT_RIGHTS r;
1149         NTSTATUS result;
1150         int i;
1151         fstring *privileges;
1152         char **names;
1153
1154         ZERO_STRUCT(q);
1155         ZERO_STRUCT(r);
1156
1157         /* Marshall data and send request */
1158         init_q_enum_acct_rights(&q, pol, 2, sid);
1159
1160         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ENUMACCTRIGHTS,
1161                 q, r,
1162                 qbuf, rbuf,
1163                 lsa_io_q_enum_acct_rights,
1164                 lsa_io_r_enum_acct_rights,
1165                 NT_STATUS_UNSUCCESSFUL);
1166
1167         result = r.status;
1168
1169         if (!NT_STATUS_IS_OK(result)) {
1170                 goto done;
1171         }
1172
1173         *count = r.count;
1174         if (! *count) {
1175                 goto done;
1176         }
1177
1178         
1179         privileges = TALLOC_ARRAY( mem_ctx, fstring, *count );
1180         names      = TALLOC_ARRAY( mem_ctx, char *, *count );
1181
1182         if ((privileges == NULL) || (names == NULL)) {
1183                 TALLOC_FREE(privileges);
1184                 TALLOC_FREE(names);
1185                 return NT_STATUS_NO_MEMORY;
1186         }
1187
1188         for ( i=0; i<*count; i++ ) {
1189                 UNISTR4 *uni_string = &r.rights->strings[i];
1190
1191                 if ( !uni_string->string )
1192                         continue;
1193
1194                 rpcstr_pull( privileges[i], uni_string->string->buffer, sizeof(privileges[i]), -1, STR_TERMINATE );
1195                         
1196                 /* now copy to the return array */
1197                 names[i] = talloc_strdup( mem_ctx, privileges[i] );
1198         }
1199         
1200         *priv_names = names;
1201
1202 done:
1203
1204         return result;
1205 }
1206
1207
1208
1209 /* add account rights to an account. */
1210
1211 NTSTATUS rpccli_lsa_add_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1212                                     POLICY_HND *pol, DOM_SID sid,
1213                                         uint32 count, const char **privs_name)
1214 {
1215         prs_struct qbuf, rbuf;
1216         LSA_Q_ADD_ACCT_RIGHTS q;
1217         LSA_R_ADD_ACCT_RIGHTS r;
1218         NTSTATUS result;
1219
1220         ZERO_STRUCT(q);
1221         ZERO_STRUCT(r);
1222
1223         /* Marshall data and send request */
1224         init_q_add_acct_rights(&q, pol, &sid, count, privs_name);
1225
1226         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_ADDACCTRIGHTS,
1227                 q, r,
1228                 qbuf, rbuf,
1229                 lsa_io_q_add_acct_rights,
1230                 lsa_io_r_add_acct_rights,
1231                 NT_STATUS_UNSUCCESSFUL);
1232
1233         result = r.status;
1234
1235         if (!NT_STATUS_IS_OK(result)) {
1236                 goto done;
1237         }
1238 done:
1239
1240         return result;
1241 }
1242
1243
1244 /* remove account rights for an account. */
1245
1246 NTSTATUS rpccli_lsa_remove_account_rights(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1247                                        POLICY_HND *pol, DOM_SID sid, BOOL removeall,
1248                                        uint32 count, const char **privs_name)
1249 {
1250         prs_struct qbuf, rbuf;
1251         LSA_Q_REMOVE_ACCT_RIGHTS q;
1252         LSA_R_REMOVE_ACCT_RIGHTS r;
1253         NTSTATUS result;
1254
1255         ZERO_STRUCT(q);
1256         ZERO_STRUCT(r);
1257
1258         /* Marshall data and send request */
1259         init_q_remove_acct_rights(&q, pol, &sid, removeall?1:0, count, privs_name);
1260
1261         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_REMOVEACCTRIGHTS,
1262                 q, r,
1263                 qbuf, rbuf,
1264                 lsa_io_q_remove_acct_rights,
1265                 lsa_io_r_remove_acct_rights,
1266                 NT_STATUS_UNSUCCESSFUL);
1267
1268         result = r.status;
1269
1270         if (!NT_STATUS_IS_OK(result)) {
1271                 goto done;
1272         }
1273 done:
1274
1275         return result;
1276 }
1277
1278
1279 #if 0
1280
1281 /** An example of how to use the routines in this file.  Fetch a DOMAIN
1282     sid. Does complete cli setup / teardown anonymously. */
1283
1284 BOOL fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
1285 {
1286         extern pstring global_myname;
1287         struct cli_state *cli;
1288         NTSTATUS result;
1289         POLICY_HND lsa_pol;
1290         BOOL ret = False;
1291  
1292         ZERO_STRUCT(cli);
1293         if((cli = cli_initialise()) == NULL) {
1294                 DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
1295                 return False;
1296         }
1297  
1298         if(!resolve_name( remote_machine, &cli->dest_ip, 0x20)) {
1299                 DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine));
1300                 goto done;
1301         }
1302  
1303         if (!cli_connect(cli, remote_machine, &cli->dest_ip)) {
1304                 DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
1305 machine %s. Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1306                 goto done;
1307         }
1308
1309         if (!attempt_netbios_session_request(cli, global_myname, remote_machine, &cli->dest_ip)) {
1310                 DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n", 
1311                         remote_machine));
1312                 goto done;
1313         }
1314  
1315         cli->protocol = PROTOCOL_NT1;
1316  
1317         if (!cli_negprot(cli)) {
1318                 DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
1319 Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1320                 goto done;
1321         }
1322  
1323         if (cli->protocol != PROTOCOL_NT1) {
1324                 DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
1325                         remote_machine));
1326                 goto done;
1327         }
1328  
1329         /*
1330          * Do an anonymous session setup.
1331          */
1332  
1333         if (!cli_session_setup(cli, "", "", 0, "", 0, "")) {
1334                 DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
1335 Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1336                 goto done;
1337         }
1338  
1339         if (!(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
1340                 DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
1341                         remote_machine));
1342                 goto done;
1343         }
1344
1345         if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
1346                 DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
1347 Error was : %s.\n", remote_machine, cli_errstr(cli) ));
1348                 goto done;
1349         }
1350
1351         /* Fetch domain sid */
1352  
1353         if (!cli_nt_session_open(cli, PI_LSARPC)) {
1354                 DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
1355                 goto done;
1356         }
1357  
1358         result = cli_lsa_open_policy(cli, cli->mem_ctx, True, SEC_RIGHTS_QUERY_VALUE, &lsa_pol);
1359         if (!NT_STATUS_IS_OK(result)) {
1360                 DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
1361                         nt_errstr(result) ));
1362                 goto done;
1363         }
1364  
1365         result = cli_lsa_query_info_policy(cli, cli->mem_ctx, &lsa_pol, 5, domain, psid);
1366         if (!NT_STATUS_IS_OK(result)) {
1367                 DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
1368                         nt_errstr(result) ));
1369                 goto done;
1370         }
1371  
1372         ret = True;
1373
1374   done:
1375
1376         cli_shutdown(cli);
1377         return ret;
1378 }
1379
1380 #endif
1381
1382 NTSTATUS rpccli_lsa_open_trusted_domain(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1383                                      POLICY_HND *pol, DOM_SID *dom_sid, uint32 access_mask,
1384                                      POLICY_HND *trustdom_pol)
1385 {
1386         prs_struct qbuf, rbuf;
1387         LSA_Q_OPEN_TRUSTED_DOMAIN q;
1388         LSA_R_OPEN_TRUSTED_DOMAIN r;
1389         NTSTATUS result;
1390
1391         ZERO_STRUCT(q);
1392         ZERO_STRUCT(r);
1393
1394         /* Initialise input parameters */
1395
1396         init_lsa_q_open_trusted_domain(&q, pol, dom_sid, access_mask);
1397
1398         /* Marshall data and send request */
1399
1400         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENTRUSTDOM,
1401                 q, r,
1402                 qbuf, rbuf,
1403                 lsa_io_q_open_trusted_domain,
1404                 lsa_io_r_open_trusted_domain,
1405                 NT_STATUS_UNSUCCESSFUL);
1406
1407         /* Return output parameters */
1408         
1409         result = r.status;
1410
1411         if (NT_STATUS_IS_OK(result)) {
1412                 *trustdom_pol = r.handle;
1413         }
1414
1415         return result;
1416 }
1417
1418 NTSTATUS rpccli_lsa_query_trusted_domain_info(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1419                                            POLICY_HND *pol, 
1420                                            uint16 info_class,  
1421                                            LSA_TRUSTED_DOMAIN_INFO **info)
1422 {
1423         prs_struct qbuf, rbuf;
1424         LSA_Q_QUERY_TRUSTED_DOMAIN_INFO q;
1425         LSA_R_QUERY_TRUSTED_DOMAIN_INFO r;
1426         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1427
1428         ZERO_STRUCT(q);
1429         ZERO_STRUCT(r);
1430
1431         /* Marshall data and send request */
1432
1433         init_q_query_trusted_domain_info(&q, pol, info_class); 
1434
1435         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYTRUSTDOMINFO,
1436                 q, r,
1437                 qbuf, rbuf,
1438                 lsa_io_q_query_trusted_domain_info,
1439                 lsa_io_r_query_trusted_domain_info,
1440                 NT_STATUS_UNSUCCESSFUL);
1441
1442         result = r.status;
1443
1444         if (!NT_STATUS_IS_OK(result)) {
1445                 goto done;
1446         }
1447
1448         *info = r.info;
1449                 
1450 done:
1451         return result;
1452 }
1453
1454 NTSTATUS rpccli_lsa_open_trusted_domain_by_name(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1455                                                 POLICY_HND *pol, const char *name, uint32 access_mask,
1456                                                 POLICY_HND *trustdom_pol)
1457 {
1458         prs_struct qbuf, rbuf;
1459         LSA_Q_OPEN_TRUSTED_DOMAIN_BY_NAME q;
1460         LSA_R_OPEN_TRUSTED_DOMAIN_BY_NAME r;
1461         NTSTATUS result;
1462
1463         ZERO_STRUCT(q);
1464         ZERO_STRUCT(r);
1465
1466         /* Initialise input parameters */
1467
1468         init_lsa_q_open_trusted_domain_by_name(&q, pol, name, access_mask);
1469
1470         /* Marshall data and send request */
1471
1472         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_OPENTRUSTDOMBYNAME,
1473                 q, r,
1474                 qbuf, rbuf,
1475                 lsa_io_q_open_trusted_domain_by_name,
1476                 lsa_io_r_open_trusted_domain_by_name,
1477                 NT_STATUS_UNSUCCESSFUL);
1478
1479         /* Return output parameters */
1480         
1481         result = r.status;
1482
1483         if (NT_STATUS_IS_OK(result)) {
1484                 *trustdom_pol = r.handle;
1485         }
1486
1487         return result;
1488 }
1489
1490
1491 NTSTATUS rpccli_lsa_query_trusted_domain_info_by_sid(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1492                                                   POLICY_HND *pol, 
1493                                                   uint16 info_class, DOM_SID *dom_sid, 
1494                                                   LSA_TRUSTED_DOMAIN_INFO **info)
1495 {
1496         prs_struct qbuf, rbuf;
1497         LSA_Q_QUERY_TRUSTED_DOMAIN_INFO_BY_SID q;
1498         LSA_R_QUERY_TRUSTED_DOMAIN_INFO r;
1499         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1500
1501         ZERO_STRUCT(q);
1502         ZERO_STRUCT(r);
1503
1504         /* Marshall data and send request */
1505
1506         init_q_query_trusted_domain_info_by_sid(&q, pol, info_class, dom_sid); 
1507
1508         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYTRUSTDOMINFOBYSID,
1509                 q, r,
1510                 qbuf, rbuf,
1511                 lsa_io_q_query_trusted_domain_info_by_sid,
1512                 lsa_io_r_query_trusted_domain_info,
1513                 NT_STATUS_UNSUCCESSFUL);
1514
1515         result = r.status;
1516
1517         if (!NT_STATUS_IS_OK(result)) {
1518                 goto done;
1519         }
1520
1521         *info = r.info;
1522
1523 done:
1524
1525         return result;
1526 }
1527
1528 NTSTATUS rpccli_lsa_query_trusted_domain_info_by_name(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1529                                                    POLICY_HND *pol, 
1530                                                    uint16 info_class, const char *domain_name, 
1531                                                    LSA_TRUSTED_DOMAIN_INFO **info)
1532 {
1533         prs_struct qbuf, rbuf;
1534         LSA_Q_QUERY_TRUSTED_DOMAIN_INFO_BY_NAME q;
1535         LSA_R_QUERY_TRUSTED_DOMAIN_INFO r;
1536         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1537
1538         ZERO_STRUCT(q);
1539         ZERO_STRUCT(r);
1540
1541         /* Marshall data and send request */
1542
1543         init_q_query_trusted_domain_info_by_name(&q, pol, info_class, domain_name); 
1544
1545         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYTRUSTDOMINFOBYNAME,
1546                 q, r,
1547                 qbuf, rbuf,
1548                 lsa_io_q_query_trusted_domain_info_by_name,
1549                 lsa_io_r_query_trusted_domain_info,
1550                 NT_STATUS_UNSUCCESSFUL);
1551
1552         result = r.status;
1553
1554         if (!NT_STATUS_IS_OK(result)) {
1555                 goto done;
1556         }
1557
1558         *info = r.info;
1559
1560 done:
1561         
1562         return result;
1563 }
1564
1565 NTSTATUS cli_lsa_query_domain_info_policy(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
1566                                           POLICY_HND *pol, 
1567                                           uint16 info_class, LSA_DOM_INFO_UNION **info)
1568 {
1569         prs_struct qbuf, rbuf;
1570         LSA_Q_QUERY_DOM_INFO_POLICY q;
1571         LSA_R_QUERY_DOM_INFO_POLICY r;
1572         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1573
1574         ZERO_STRUCT(q);
1575         ZERO_STRUCT(r);
1576
1577         /* Marshall data and send request */
1578
1579         init_q_query_dom_info(&q, pol, info_class); 
1580
1581         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC, LSA_QUERYDOMINFOPOL, 
1582                 q, r,
1583                 qbuf, rbuf,
1584                 lsa_io_q_query_dom_info,
1585                 lsa_io_r_query_dom_info,
1586                 NT_STATUS_UNSUCCESSFUL);
1587
1588         result = r.status;
1589
1590         if (!NT_STATUS_IS_OK(result)) {
1591                 goto done;
1592         }
1593
1594         *info = r.info;
1595
1596 done:
1597         return result;
1598 }
1599