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