In cli_lsa_lookup_sids don't leave the domain field uninitialized if
[samba.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                         (*domains)[i] = NULL;
326                         (*types)[i] = SID_NAME_UNKNOWN;
327                 }
328         }
329
330  done:
331         prs_mem_free(&qbuf);
332         prs_mem_free(&rbuf);
333
334         return result;
335 }
336
337 /** Lookup a list of names */
338
339 NTSTATUS cli_lsa_lookup_names(struct cli_state *cli, TALLOC_CTX *mem_ctx,
340                               POLICY_HND *pol, int num_names, 
341                               const char **names, DOM_SID **sids, 
342                               uint32 **types)
343 {
344         prs_struct qbuf, rbuf;
345         LSA_Q_LOOKUP_NAMES q;
346         LSA_R_LOOKUP_NAMES r;
347         DOM_R_REF ref;
348         NTSTATUS result;
349         int i;
350         
351         ZERO_STRUCT(q);
352         ZERO_STRUCT(r);
353
354         /* Initialise parse structures */
355
356         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
357         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
358
359         /* Marshall data and send request */
360
361         init_q_lookup_names(mem_ctx, &q, pol, num_names, names);
362
363         if (!lsa_io_q_lookup_names("", &q, &qbuf, 0) ||
364             !rpc_api_pipe_req(cli, LSA_LOOKUPNAMES, &qbuf, &rbuf)) {
365                 result = NT_STATUS_UNSUCCESSFUL;
366                 goto done;
367         }
368         
369         /* Unmarshall response */
370
371         ZERO_STRUCT(ref);
372         r.dom_ref = &ref;
373
374         if (!lsa_io_r_lookup_names("", &r, &rbuf, 0)) {
375                 result = NT_STATUS_UNSUCCESSFUL;
376                 goto done;
377         }
378
379         result = r.status;
380
381         if (!NT_STATUS_IS_OK(result) && NT_STATUS_V(result) !=
382             NT_STATUS_V(STATUS_SOME_UNMAPPED)) {
383
384                 /* An actual error occured */
385
386                 goto done;
387         }
388
389         /* Return output parameters */
390
391         if (r.mapped_count == 0) {
392                 result = NT_STATUS_NONE_MAPPED;
393                 goto done;
394         }
395
396         if (!((*sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
397                                          num_names)))) {
398                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
399                 result = NT_STATUS_UNSUCCESSFUL;
400                 goto done;
401         }
402
403         if (!((*types = (uint32 *)talloc(mem_ctx, sizeof(uint32) *
404                                          num_names)))) {
405                 DEBUG(0, ("cli_lsa_lookup_sids(): out of memory\n"));
406                 result = NT_STATUS_UNSUCCESSFUL;
407                 goto done;
408         }
409
410         for (i = 0; i < num_names; i++) {
411                 DOM_RID2 *t_rids = r.dom_rid;
412                 uint32 dom_idx = t_rids[i].rid_idx;
413                 uint32 dom_rid = t_rids[i].rid;
414                 DOM_SID *sid = &(*sids)[i];
415
416                 /* Translate optimised sid through domain index array */
417
418                 if (dom_idx != 0xffffffff) {
419
420                         sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
421
422                         if (dom_rid != 0xffffffff) {
423                                 sid_append_rid(sid, dom_rid);
424                         }
425
426                         (*types)[i] = t_rids[i].type;
427                 } else {
428                         ZERO_STRUCTP(sid);
429                         (*types)[i] = SID_NAME_UNKNOWN;
430                 }
431         }
432
433  done:
434         prs_mem_free(&qbuf);
435         prs_mem_free(&rbuf);
436
437         return result;
438 }
439
440 /** Query info policy
441  *
442  *  @param domain_sid - returned remote server's domain sid */
443
444 NTSTATUS cli_lsa_query_info_policy(struct cli_state *cli, TALLOC_CTX *mem_ctx,
445                                    POLICY_HND *pol, uint16 info_class, 
446                                    fstring domain_name, DOM_SID *domain_sid)
447 {
448         prs_struct qbuf, rbuf;
449         LSA_Q_QUERY_INFO q;
450         LSA_R_QUERY_INFO r;
451         NTSTATUS result;
452
453         ZERO_STRUCT(q);
454         ZERO_STRUCT(r);
455
456         /* Initialise parse structures */
457
458         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
459         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
460
461         /* Marshall data and send request */
462
463         init_q_query(&q, pol, info_class);
464
465         if (!lsa_io_q_query("", &q, &qbuf, 0) ||
466             !rpc_api_pipe_req(cli, LSA_QUERYINFOPOLICY, &qbuf, &rbuf)) {
467                 result = NT_STATUS_UNSUCCESSFUL;
468                 goto done;
469         }
470
471         /* Unmarshall response */
472
473         if (!lsa_io_r_query("", &r, &rbuf, 0)) {
474                 result = NT_STATUS_UNSUCCESSFUL;
475                 goto done;
476         }
477
478         if (!NT_STATUS_IS_OK(result = r.status)) {
479                 goto done;
480         }
481
482         /* Return output parameters */
483
484         ZERO_STRUCTP(domain_sid);
485         domain_name[0] = '\0';
486
487         switch (info_class) {
488
489         case 3:
490                 if (r.dom.id3.buffer_dom_name != 0) {
491                         unistr2_to_ascii(domain_name,
492                                          &r.dom.id3.
493                                          uni_domain_name,
494                                          sizeof (fstring) - 1);
495                 }
496
497                 if (r.dom.id3.buffer_dom_sid != 0) {
498                         *domain_sid = r.dom.id3.dom_sid.sid;
499                 }
500
501                 break;
502
503         case 5:
504                 
505                 if (r.dom.id5.buffer_dom_name != 0) {
506                         unistr2_to_ascii(domain_name, &r.dom.id5.
507                                          uni_domain_name,
508                                          sizeof (fstring) - 1);
509                 }
510                         
511                 if (r.dom.id5.buffer_dom_sid != 0) {
512                         *domain_sid = r.dom.id5.dom_sid.sid;
513                 }
514
515                 break;
516                 
517         default:
518                 DEBUG(3, ("unknown info class %d\n", info_class));
519                 break;                
520         }
521         
522  done:
523         prs_mem_free(&qbuf);
524         prs_mem_free(&rbuf);
525
526         return result;
527 }
528
529 /** Query info policy2
530  *
531  *  @param domain_name - returned remote server's domain name
532  *  @param dns_name - returned remote server's dns domain name
533  *  @param forest_name - returned remote server's forest name
534  *  @param domain_guid - returned remote server's domain guid
535  *  @param domain_sid - returned remote server's domain sid */
536
537 NTSTATUS cli_lsa_query_info_policy2(struct cli_state *cli, TALLOC_CTX *mem_ctx,
538                                     POLICY_HND *pol, uint16 info_class, 
539                                     fstring domain_name, fstring dns_name,
540                                     fstring forest_name, GUID *domain_guid,
541                                     DOM_SID *domain_sid)
542 {
543         prs_struct qbuf, rbuf;
544         LSA_Q_QUERY_INFO2 q;
545         LSA_R_QUERY_INFO2 r;
546         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
547
548         if (info_class != 12)
549                 goto done;
550
551         ZERO_STRUCT(q);
552         ZERO_STRUCT(r);
553
554         /* Initialise parse structures */
555
556         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
557         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
558
559         /* Marshall data and send request */
560
561         init_q_query2(&q, pol, info_class);
562
563         if (!lsa_io_q_query_info2("", &q, &qbuf, 0) ||
564             !rpc_api_pipe_req(cli, LSA_QUERYINFO2, &qbuf, &rbuf)) {
565                 result = NT_STATUS_UNSUCCESSFUL;
566                 goto done;
567         }
568
569         /* Unmarshall response */
570
571         if (!lsa_io_r_query_info2("", &r, &rbuf, 0)) {
572                 result = NT_STATUS_UNSUCCESSFUL;
573                 goto done;
574         }
575
576         if (!NT_STATUS_IS_OK(result = r.status)) {
577                 goto done;
578         }
579
580         /* Return output parameters */
581
582         ZERO_STRUCTP(domain_sid);
583         ZERO_STRUCTP(domain_guid);
584         domain_name[0] = '\0';
585
586         if (r.info.dns_dom_info.hdr_nb_dom_name.buffer) {
587                 unistr2_to_ascii(domain_name,
588                                  &r.info.dns_dom_info.uni_nb_dom_name,
589                                  sizeof(fstring) - 1);
590         }
591         if (r.info.dns_dom_info.hdr_dns_dom_name.buffer) {
592                 unistr2_to_ascii(dns_name,
593                                  &r.info.dns_dom_info.uni_dns_dom_name,
594                                  sizeof(fstring) - 1);
595         }
596         if (r.info.dns_dom_info.hdr_forest_name.buffer) {
597                 unistr2_to_ascii(forest_name,
598                                  &r.info.dns_dom_info.uni_forest_name,
599                                  sizeof(fstring) - 1);
600         }
601         
602         memcpy(domain_guid, &r.info.dns_dom_info.dom_guid, sizeof(GUID));
603
604         if (r.info.dns_dom_info.ptr_dom_sid != 0) {
605                 *domain_sid = r.info.dns_dom_info.dom_sid.sid;
606         }
607         
608  done:
609         prs_mem_free(&qbuf);
610         prs_mem_free(&rbuf);
611
612         return result;
613 }
614
615 /**
616  * Enumerate list of trusted domains
617  *
618  * @param cli client state (cli_state) structure of the connection
619  * @param mem_ctx memory context
620  * @param pol opened lsa policy handle
621  * @param enum_ctx enumeration context ie. index of first returned domain entry
622  * @param pref_num_domains preferred max number of entries returned in one response
623  * @param num_domains total number of trusted domains returned by response
624  * @param domain_names returned trusted domain names
625  * @param domain_sids returned trusted domain sids
626  *
627  * @return nt status code of response
628  **/
629
630 NTSTATUS cli_lsa_enum_trust_dom(struct cli_state *cli, TALLOC_CTX *mem_ctx,
631                                 POLICY_HND *pol, uint32 *enum_ctx, 
632                                 uint32 *num_domains,
633                                 char ***domain_names, DOM_SID **domain_sids)
634 {
635         prs_struct qbuf, rbuf;
636         LSA_Q_ENUM_TRUST_DOM q;
637         LSA_R_ENUM_TRUST_DOM r;
638         NTSTATUS result;
639         int i;
640
641         ZERO_STRUCT(q);
642         ZERO_STRUCT(r);
643
644         /* Initialise parse structures */
645
646         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
647         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
648
649         /* Marshall data and send request */
650
651         /* 64k is enough for about 2000 trusted domains */
652         init_q_enum_trust_dom(&q, pol, *enum_ctx, 0x10000);
653
654         if (!lsa_io_q_enum_trust_dom("", &q, &qbuf, 0) ||
655             !rpc_api_pipe_req(cli, LSA_ENUMTRUSTDOM, &qbuf, &rbuf)) {
656                 result = NT_STATUS_UNSUCCESSFUL;
657                 goto done;
658         }
659
660         /* Unmarshall response */
661
662         if (!lsa_io_r_enum_trust_dom("", &r, &rbuf, 0)) {
663                 result = NT_STATUS_UNSUCCESSFUL;
664                 goto done;
665         }
666
667         result = r.status;
668
669         if (!NT_STATUS_IS_OK(result) &&
670             !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
671             !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
672
673                 /* An actual error ocured */
674
675                 goto done;
676         }
677
678         /* Return output parameters */
679
680         if (r.num_domains) {
681
682                 /* Allocate memory for trusted domain names and sids */
683
684                 *domain_names = (char **)talloc(mem_ctx, sizeof(char *) *
685                                                 r.num_domains);
686
687                 if (!*domain_names) {
688                         DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
689                         result = NT_STATUS_NO_MEMORY;
690                         goto done;
691                 }
692
693                 *domain_sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) *
694                                                  r.num_domains);
695                 if (!domain_sids) {
696                         DEBUG(0, ("cli_lsa_enum_trust_dom(): out of memory\n"));
697                         result = NT_STATUS_NO_MEMORY;
698                         goto done;
699                 }
700
701                 /* Copy across names and sids */
702
703                 for (i = 0; i < r.num_domains; i++) {
704                         fstring tmp;
705
706                         unistr2_to_ascii(tmp, &r.uni_domain_name[i], 
707                                          sizeof(tmp) - 1);
708                         (*domain_names)[i] = talloc_strdup(mem_ctx, tmp);
709                         sid_copy(&(*domain_sids)[i], &r.domain_sid[i].sid);
710                 }
711         }
712
713         *num_domains = r.num_domains;
714         *enum_ctx = r.enum_context;
715
716  done:
717         prs_mem_free(&qbuf);
718         prs_mem_free(&rbuf);
719
720         return result;
721 }
722
723
724 /** Enumerate privileges*/
725
726 NTSTATUS cli_lsa_enum_privilege(struct cli_state *cli, TALLOC_CTX *mem_ctx,
727                                 POLICY_HND *pol, uint32 *enum_context, uint32 pref_max_length,
728                                 uint32 *count, char ***privs_name, uint32 **privs_high, uint32 **privs_low)
729 {
730         prs_struct qbuf, rbuf;
731         LSA_Q_ENUM_PRIVS q;
732         LSA_R_ENUM_PRIVS r;
733         NTSTATUS result;
734         int i;
735
736         ZERO_STRUCT(q);
737         ZERO_STRUCT(r);
738
739         /* Initialise parse structures */
740
741         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
742         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
743
744         /* Marshall data and send request */
745
746         init_q_enum_privs(&q, pol, *enum_context, pref_max_length);
747
748         if (!lsa_io_q_enum_privs("", &q, &qbuf, 0) ||
749             !rpc_api_pipe_req(cli, LSA_ENUM_PRIVS, &qbuf, &rbuf)) {
750                 result = NT_STATUS_UNSUCCESSFUL;
751                 goto done;
752         }
753
754         /* Unmarshall response */
755
756         if (!lsa_io_r_enum_privs("", &r, &rbuf, 0)) {
757                 result = NT_STATUS_UNSUCCESSFUL;
758                 goto done;
759         }
760
761         if (!NT_STATUS_IS_OK(result = r.status)) {
762                 goto done;
763         }
764
765         /* Return output parameters */
766
767         *enum_context = r.enum_context;
768         *count = r.count;
769
770         if (!((*privs_name = (char **)talloc(mem_ctx, sizeof(char *) * r.count)))) {
771                 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
772                 result = NT_STATUS_UNSUCCESSFUL;
773                 goto done;
774         }
775
776         if (!((*privs_high = (uint32 *)talloc(mem_ctx, sizeof(uint32) * r.count)))) {
777                 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
778                 result = NT_STATUS_UNSUCCESSFUL;
779                 goto done;
780         }
781
782         if (!((*privs_low = (uint32 *)talloc(mem_ctx, sizeof(uint32) * r.count)))) {
783                 DEBUG(0, ("(cli_lsa_enum_privilege): out of memory\n"));
784                 result = NT_STATUS_UNSUCCESSFUL;
785                 goto done;
786         }
787
788         for (i = 0; i < r.count; i++) {
789                 fstring name;
790
791                 rpcstr_pull_unistr2_fstring( name, &r.privs[i].name);
792
793                 (*privs_name)[i] = talloc_strdup(mem_ctx, name);
794
795                 (*privs_high)[i] = r.privs[i].luid_high;
796                 (*privs_low)[i] = r.privs[i].luid_low;
797         }
798
799  done:
800         prs_mem_free(&qbuf);
801         prs_mem_free(&rbuf);
802
803         return result;
804 }
805
806 /** Get privilege name */
807
808 NTSTATUS cli_lsa_get_dispname(struct cli_state *cli, TALLOC_CTX *mem_ctx,
809                               POLICY_HND *pol, const char *name, 
810                               uint16 lang_id, uint16 lang_id_sys,
811                               fstring description, uint16 *lang_id_desc)
812 {
813         prs_struct qbuf, rbuf;
814         LSA_Q_PRIV_GET_DISPNAME q;
815         LSA_R_PRIV_GET_DISPNAME r;
816         NTSTATUS result;
817
818         ZERO_STRUCT(q);
819         ZERO_STRUCT(r);
820
821         /* Initialise parse structures */
822
823         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
824         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
825
826         /* Marshall data and send request */
827
828         init_lsa_priv_get_dispname(&q, pol, name, lang_id, lang_id_sys);
829
830         if (!lsa_io_q_priv_get_dispname("", &q, &qbuf, 0) ||
831             !rpc_api_pipe_req(cli, LSA_PRIV_GET_DISPNAME, &qbuf, &rbuf)) {
832                 result = NT_STATUS_UNSUCCESSFUL;
833                 goto done;
834         }
835
836         /* Unmarshall response */
837
838         if (!lsa_io_r_priv_get_dispname("", &r, &rbuf, 0)) {
839                 result = NT_STATUS_UNSUCCESSFUL;
840                 goto done;
841         }
842
843         if (!NT_STATUS_IS_OK(result = r.status)) {
844                 goto done;
845         }
846
847         /* Return output parameters */
848         
849         rpcstr_pull_unistr2_fstring(description , &r.desc);
850         *lang_id_desc = r.lang_id;
851
852  done:
853         prs_mem_free(&qbuf);
854         prs_mem_free(&rbuf);
855
856         return result;
857 }
858
859 /** Enumerate list of SIDs  */
860
861 NTSTATUS cli_lsa_enum_sids(struct cli_state *cli, TALLOC_CTX *mem_ctx,
862                                 POLICY_HND *pol, uint32 *enum_ctx, uint32 pref_max_length, 
863                                 uint32 *num_sids, DOM_SID **sids)
864 {
865         prs_struct qbuf, rbuf;
866         LSA_Q_ENUM_ACCOUNTS q;
867         LSA_R_ENUM_ACCOUNTS r;
868         NTSTATUS result;
869         int i;
870
871         ZERO_STRUCT(q);
872         ZERO_STRUCT(r);
873
874         /* Initialise parse structures */
875
876         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
877         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
878
879         /* Marshall data and send request */
880
881         init_lsa_q_enum_accounts(&q, pol, *enum_ctx, pref_max_length);
882
883         if (!lsa_io_q_enum_accounts("", &q, &qbuf, 0) ||
884             !rpc_api_pipe_req(cli, LSA_ENUM_ACCOUNTS, &qbuf, &rbuf)) {
885                 result = NT_STATUS_UNSUCCESSFUL;
886                 goto done;
887         }
888
889         /* Unmarshall response */
890
891         if (!lsa_io_r_enum_accounts("", &r, &rbuf, 0)) {
892                 result = NT_STATUS_UNSUCCESSFUL;
893                 goto done;
894         }
895
896         result = r.status;
897
898         if (!NT_STATUS_IS_OK(result = r.status)) {
899                 goto done;
900         }
901
902         if (r.sids.num_entries==0)
903                 goto done;
904
905         /* Return output parameters */
906
907         *sids = (DOM_SID *)talloc(mem_ctx, sizeof(DOM_SID) * r.sids.num_entries);
908         if (!*sids) {
909                 DEBUG(0, ("(cli_lsa_enum_sids): out of memory\n"));
910                 result = NT_STATUS_UNSUCCESSFUL;
911                 goto done;
912         }
913
914         /* Copy across names and sids */
915
916         for (i = 0; i < r.sids.num_entries; i++) {
917                 sid_copy(&(*sids)[i], &r.sids.sid[i].sid);
918         }
919
920         *num_sids= r.sids.num_entries;
921         *enum_ctx = r.enum_context;
922
923  done:
924         prs_mem_free(&qbuf);
925         prs_mem_free(&rbuf);
926
927         return result;
928 }
929
930 /** Open a LSA user handle
931  *
932  * @param cli Handle on an initialised SMB connection */
933
934 NTSTATUS cli_lsa_open_account(struct cli_state *cli, TALLOC_CTX *mem_ctx,
935                              POLICY_HND *dom_pol, DOM_SID *sid, uint32 des_access, 
936                              POLICY_HND *user_pol)
937 {
938         prs_struct qbuf, rbuf;
939         LSA_Q_OPENACCOUNT q;
940         LSA_R_OPENACCOUNT r;
941         NTSTATUS result;
942
943         ZERO_STRUCT(q);
944         ZERO_STRUCT(r);
945
946         /* Initialise parse structures */
947
948         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
949         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
950
951         /* Initialise input parameters */
952
953         init_lsa_q_open_account(&q, dom_pol, sid, des_access);
954
955         /* Marshall data and send request */
956
957         if (!lsa_io_q_open_account("", &q, &qbuf, 0) ||
958             !rpc_api_pipe_req(cli, LSA_OPENACCOUNT, &qbuf, &rbuf)) {
959                 result = NT_STATUS_UNSUCCESSFUL;
960                 goto done;
961         }
962
963         /* Unmarshall response */
964
965         if (!lsa_io_r_open_account("", &r, &rbuf, 0)) {
966                 result = NT_STATUS_UNSUCCESSFUL;
967                 goto done;
968         }
969
970         /* Return output parameters */
971
972         if (NT_STATUS_IS_OK(result = r.status)) {
973                 *user_pol = r.pol;
974         }
975
976  done:
977         prs_mem_free(&qbuf);
978         prs_mem_free(&rbuf);
979
980         return result;
981 }
982
983 /** Enumerate user privileges
984  *
985  * @param cli Handle on an initialised SMB connection */
986
987 NTSTATUS cli_lsa_enum_privsaccount(struct cli_state *cli, TALLOC_CTX *mem_ctx,
988                              POLICY_HND *pol, uint32 *count, LUID_ATTR **set)
989 {
990         prs_struct qbuf, rbuf;
991         LSA_Q_ENUMPRIVSACCOUNT q;
992         LSA_R_ENUMPRIVSACCOUNT r;
993         NTSTATUS result;
994         int i;
995
996         ZERO_STRUCT(q);
997         ZERO_STRUCT(r);
998
999         /* Initialise parse structures */
1000
1001         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1002         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1003
1004         /* Initialise input parameters */
1005
1006         init_lsa_q_enum_privsaccount(&q, pol);
1007
1008         /* Marshall data and send request */
1009
1010         if (!lsa_io_q_enum_privsaccount("", &q, &qbuf, 0) ||
1011             !rpc_api_pipe_req(cli, LSA_ENUMPRIVSACCOUNT, &qbuf, &rbuf)) {
1012                 result = NT_STATUS_UNSUCCESSFUL;
1013                 goto done;
1014         }
1015
1016         /* Unmarshall response */
1017
1018         if (!lsa_io_r_enum_privsaccount("", &r, &rbuf, 0)) {
1019                 result = NT_STATUS_UNSUCCESSFUL;
1020                 goto done;
1021         }
1022
1023         /* Return output parameters */
1024
1025         if (!NT_STATUS_IS_OK(result = r.status)) {
1026                 goto done;
1027         }
1028
1029         if (r.count == 0)
1030                 goto done;
1031
1032         if (!((*set = (LUID_ATTR *)talloc(mem_ctx, sizeof(LUID_ATTR) * r.count)))) {
1033                 DEBUG(0, ("(cli_lsa_enum_privsaccount): out of memory\n"));
1034                 result = NT_STATUS_UNSUCCESSFUL;
1035                 goto done;
1036         }
1037
1038         for (i=0; i<r.count; i++) {
1039                 (*set)[i].luid.low = r.set->set[i].luid.low;
1040                 (*set)[i].luid.high = r.set->set[i].luid.high;
1041                 (*set)[i].attr = r.set->set[i].attr;
1042         }
1043
1044         *count=r.count;
1045  done:
1046         prs_mem_free(&qbuf);
1047         prs_mem_free(&rbuf);
1048
1049         return result;
1050 }
1051
1052 /** Get a privilege value given its name */
1053
1054 NTSTATUS cli_lsa_lookupprivvalue(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1055                                  POLICY_HND *pol, const char *name, LUID *luid)
1056 {
1057         prs_struct qbuf, rbuf;
1058         LSA_Q_LOOKUPPRIVVALUE q;
1059         LSA_R_LOOKUPPRIVVALUE r;
1060         NTSTATUS result;
1061
1062         ZERO_STRUCT(q);
1063         ZERO_STRUCT(r);
1064
1065         /* Initialise parse structures */
1066
1067         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1068         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1069
1070         /* Marshall data and send request */
1071
1072         init_lsa_q_lookupprivvalue(&q, pol, name);
1073
1074         if (!lsa_io_q_lookupprivvalue("", &q, &qbuf, 0) ||
1075             !rpc_api_pipe_req(cli, LSA_LOOKUPPRIVVALUE, &qbuf, &rbuf)) {
1076                 result = NT_STATUS_UNSUCCESSFUL;
1077                 goto done;
1078         }
1079
1080         /* Unmarshall response */
1081
1082         if (!lsa_io_r_lookupprivvalue("", &r, &rbuf, 0)) {
1083                 result = NT_STATUS_UNSUCCESSFUL;
1084                 goto done;
1085         }
1086
1087         if (!NT_STATUS_IS_OK(result = r.status)) {
1088                 goto done;
1089         }
1090
1091         /* Return output parameters */
1092
1093         (*luid).low=r.luid.low;
1094         (*luid).high=r.luid.high;
1095
1096  done:
1097         prs_mem_free(&qbuf);
1098         prs_mem_free(&rbuf);
1099
1100         return result;
1101 }
1102
1103 /** Query LSA security object */
1104
1105 NTSTATUS cli_lsa_query_secobj(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1106                               POLICY_HND *pol, uint32 sec_info, 
1107                               SEC_DESC_BUF **psdb)
1108 {
1109         prs_struct qbuf, rbuf;
1110         LSA_Q_QUERY_SEC_OBJ q;
1111         LSA_R_QUERY_SEC_OBJ r;
1112         NTSTATUS result;
1113
1114         ZERO_STRUCT(q);
1115         ZERO_STRUCT(r);
1116
1117         /* Initialise parse structures */
1118
1119         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1120         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1121
1122         /* Marshall data and send request */
1123
1124         init_q_query_sec_obj(&q, pol, sec_info);
1125
1126         if (!lsa_io_q_query_sec_obj("", &q, &qbuf, 0) ||
1127             !rpc_api_pipe_req(cli, LSA_QUERYSECOBJ, &qbuf, &rbuf)) {
1128                 result = NT_STATUS_UNSUCCESSFUL;
1129                 goto done;
1130         }
1131
1132         /* Unmarshall response */
1133
1134         if (!lsa_io_r_query_sec_obj("", &r, &rbuf, 0)) {
1135                 result = NT_STATUS_UNSUCCESSFUL;
1136                 goto done;
1137         }
1138
1139         if (!NT_STATUS_IS_OK(result = r.status)) {
1140                 goto done;
1141         }
1142
1143         /* Return output parameters */
1144
1145         if (psdb)
1146                 *psdb = r.buf;
1147
1148  done:
1149         prs_mem_free(&qbuf);
1150         prs_mem_free(&rbuf);
1151
1152         return result;
1153 }
1154
1155
1156 /* Enumerate account rights This is similar to enum_privileges but
1157    takes a SID directly, avoiding the open_account call.
1158 */
1159
1160 NTSTATUS cli_lsa_enum_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1161                                      POLICY_HND *pol, DOM_SID sid,
1162                                      uint32 *count, char ***privs_name)
1163 {
1164         prs_struct qbuf, rbuf;
1165         LSA_Q_ENUM_ACCT_RIGHTS q;
1166         LSA_R_ENUM_ACCT_RIGHTS r;
1167         NTSTATUS result;
1168         int i;
1169
1170         ZERO_STRUCT(q);
1171         ZERO_STRUCT(r);
1172
1173         /* Initialise parse structures */
1174
1175         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1176         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1177
1178         /* Marshall data and send request */
1179         init_q_enum_acct_rights(&q, pol, 2, &sid);
1180
1181         if (!lsa_io_q_enum_acct_rights("", &q, &qbuf, 0) ||
1182             !rpc_api_pipe_req(cli, LSA_ENUMACCTRIGHTS, &qbuf, &rbuf)) {
1183                 result = NT_STATUS_UNSUCCESSFUL;
1184                 goto done;
1185         }
1186
1187         if (!lsa_io_r_enum_acct_rights("", &r, &rbuf, 0)) {
1188                 result = NT_STATUS_UNSUCCESSFUL;
1189                 goto done;
1190         }
1191
1192         if (!NT_STATUS_IS_OK(result = r.status)) {
1193                 goto done;
1194         }
1195
1196         *count = r.count;
1197         if (! *count) {
1198                 goto done;
1199         }
1200
1201         *privs_name = (char **)talloc(mem_ctx, (*count) * sizeof(char **));
1202         for (i=0;i<*count;i++) {
1203                 pull_ucs2_talloc(mem_ctx, &(*privs_name)[i], r.rights.strings[i].string.buffer);
1204         }
1205
1206 done:
1207
1208         return result;
1209 }
1210
1211
1212
1213 /* add account rights to an account. */
1214
1215 NTSTATUS cli_lsa_add_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1216                                     POLICY_HND *pol, DOM_SID sid,
1217                                     uint32 count, const char **privs_name)
1218 {
1219         prs_struct qbuf, rbuf;
1220         LSA_Q_ADD_ACCT_RIGHTS q;
1221         LSA_R_ADD_ACCT_RIGHTS r;
1222         NTSTATUS result;
1223
1224         ZERO_STRUCT(q);
1225
1226         /* Initialise parse structures */
1227         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1228         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1229
1230         /* Marshall data and send request */
1231         init_q_add_acct_rights(&q, pol, &sid, count, privs_name);
1232
1233         if (!lsa_io_q_add_acct_rights("", &q, &qbuf, 0) ||
1234             !rpc_api_pipe_req(cli, LSA_ADDACCTRIGHTS, &qbuf, &rbuf)) {
1235                 result = NT_STATUS_UNSUCCESSFUL;
1236                 goto done;
1237         }
1238
1239         /* Unmarshall response */
1240
1241         if (!lsa_io_r_add_acct_rights("", &r, &rbuf, 0)) {
1242                 result = NT_STATUS_UNSUCCESSFUL;
1243                 goto done;
1244         }
1245
1246         if (!NT_STATUS_IS_OK(result = r.status)) {
1247                 goto done;
1248         }
1249 done:
1250
1251         return result;
1252 }
1253
1254
1255 /* remove account rights for an account. */
1256
1257 NTSTATUS cli_lsa_remove_account_rights(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1258                                        POLICY_HND *pol, DOM_SID sid, BOOL removeall,
1259                                        uint32 count, const char **privs_name)
1260 {
1261         prs_struct qbuf, rbuf;
1262         LSA_Q_REMOVE_ACCT_RIGHTS q;
1263         LSA_R_REMOVE_ACCT_RIGHTS r;
1264         NTSTATUS result;
1265
1266         ZERO_STRUCT(q);
1267
1268         /* Initialise parse structures */
1269         prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
1270         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
1271
1272         /* Marshall data and send request */
1273         init_q_remove_acct_rights(&q, pol, &sid, removeall?1:0, count, privs_name);
1274
1275         if (!lsa_io_q_remove_acct_rights("", &q, &qbuf, 0) ||
1276             !rpc_api_pipe_req(cli, LSA_REMOVEACCTRIGHTS, &qbuf, &rbuf)) {
1277                 result = NT_STATUS_UNSUCCESSFUL;
1278                 goto done;
1279         }
1280
1281         /* Unmarshall response */
1282
1283         if (!lsa_io_r_remove_acct_rights("", &r, &rbuf, 0)) {
1284                 result = NT_STATUS_UNSUCCESSFUL;
1285                 goto done;
1286         }
1287
1288         if (!NT_STATUS_IS_OK(result = r.status)) {
1289                 goto done;
1290         }
1291 done:
1292
1293         return result;
1294 }
1295
1296
1297 #if 0
1298
1299 /** An example of how to use the routines in this file.  Fetch a DOMAIN
1300     sid. Does complete cli setup / teardown anonymously. */
1301
1302 BOOL fetch_domain_sid( char *domain, char *remote_machine, DOM_SID *psid)
1303 {
1304         extern pstring global_myname;
1305         struct cli_state cli;
1306         NTSTATUS result;
1307         POLICY_HND lsa_pol;
1308         BOOL ret = False;
1309  
1310         ZERO_STRUCT(cli);
1311         if(cli_initialise(&cli) == False) {
1312                 DEBUG(0,("fetch_domain_sid: unable to initialize client connection.\n"));
1313                 return False;
1314         }
1315  
1316         if(!resolve_name( remote_machine, &cli.dest_ip, 0x20)) {
1317                 DEBUG(0,("fetch_domain_sid: Can't resolve address for %s\n", remote_machine));
1318                 goto done;
1319         }
1320  
1321         if (!cli_connect(&cli, remote_machine, &cli.dest_ip)) {
1322                 DEBUG(0,("fetch_domain_sid: unable to connect to SMB server on \
1323 machine %s. Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1324                 goto done;
1325         }
1326
1327         if (!attempt_netbios_session_request(&cli, global_myname, remote_machine, &cli.dest_ip)) {
1328                 DEBUG(0,("fetch_domain_sid: machine %s rejected the NetBIOS session request.\n", 
1329                         remote_machine));
1330                 goto done;
1331         }
1332  
1333         cli.protocol = PROTOCOL_NT1;
1334  
1335         if (!cli_negprot(&cli)) {
1336                 DEBUG(0,("fetch_domain_sid: machine %s rejected the negotiate protocol. \
1337 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1338                 goto done;
1339         }
1340  
1341         if (cli.protocol != PROTOCOL_NT1) {
1342                 DEBUG(0,("fetch_domain_sid: machine %s didn't negotiate NT protocol.\n",
1343                         remote_machine));
1344                 goto done;
1345         }
1346  
1347         /*
1348          * Do an anonymous session setup.
1349          */
1350  
1351         if (!cli_session_setup(&cli, "", "", 0, "", 0, "")) {
1352                 DEBUG(0,("fetch_domain_sid: machine %s rejected the session setup. \
1353 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1354                 goto done;
1355         }
1356  
1357         if (!(cli.sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
1358                 DEBUG(0,("fetch_domain_sid: machine %s isn't in user level security mode\n",
1359                         remote_machine));
1360                 goto done;
1361         }
1362
1363         if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
1364                 DEBUG(0,("fetch_domain_sid: machine %s rejected the tconX on the IPC$ share. \
1365 Error was : %s.\n", remote_machine, cli_errstr(&cli) ));
1366                 goto done;
1367         }
1368
1369         /* Fetch domain sid */
1370  
1371         if (!cli_nt_session_open(&cli, PI_LSARPC)) {
1372                 DEBUG(0, ("fetch_domain_sid: Error connecting to SAM pipe\n"));
1373                 goto done;
1374         }
1375  
1376         result = cli_lsa_open_policy(&cli, cli.mem_ctx, True, SEC_RIGHTS_QUERY_VALUE, &lsa_pol);
1377         if (!NT_STATUS_IS_OK(result)) {
1378                 DEBUG(0, ("fetch_domain_sid: Error opening lsa policy handle. %s\n",
1379                         nt_errstr(result) ));
1380                 goto done;
1381         }
1382  
1383         result = cli_lsa_query_info_policy(&cli, cli.mem_ctx, &lsa_pol, 5, domain, psid);
1384         if (!NT_STATUS_IS_OK(result)) {
1385                 DEBUG(0, ("fetch_domain_sid: Error querying lsa policy handle. %s\n",
1386                         nt_errstr(result) ));
1387                 goto done;
1388         }
1389  
1390         ret = True;
1391
1392   done:
1393
1394         cli_shutdown(&cli);
1395         return ret;
1396 }
1397
1398 #endif
1399
1400 /** @} **/