further abstraction involving client states. main client-side code
[kai/samba.git] / source3 / rpc_client / cli_lsarpc.c
1
2 /* 
3  *  Unix SMB/Netbios implementation.
4  *  Version 1.9.
5  *  RPC Pipe client / server routines
6  *  Copyright (C) Andrew Tridgell              1992-1997,
7  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
8  *  Copyright (C) Paul Ashton                       1997.
9  *  
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25
26 #ifdef SYSLOG
27 #undef SYSLOG
28 #endif
29
30 #include "includes.h"
31
32 extern int DEBUGLEVEL;
33
34 /****************************************************************************
35  obtain the sid from the PDC.  do some verification along the way...
36 ****************************************************************************/
37 BOOL get_domain_sids(const char *myname,
38                                 DOM_SID *sid3, DOM_SID *sid5, char *servers)
39 {
40         POLICY_HND pol;
41         fstring srv_name;
42         struct cli_connection *con = NULL;
43         BOOL res = True;
44         fstring dom3;
45         fstring dom5;
46         extern struct user_credentials *usr_creds;
47         struct user_credentials usr;
48         
49         usr_creds = &usr;
50         ZERO_STRUCT(usr);
51         pwd_set_nullpwd(&usr.pwd);
52
53         if (sid3 == NULL && sid5 == NULL)
54         {
55                 /* don't waste my time... */
56                 return False;
57         }
58
59         if (!cli_connection_init_list(servers, PIPE_LSARPC, &con))
60         {
61                 DEBUG(0,("get_domain_sids: unable to initialise client connection.\n"));
62                 return False;
63         }
64
65         /*
66          * Ok - we have an anonymous connection to the IPC$ share.
67          * Now start the NT Domain stuff :-).
68          */
69
70         fstrcpy(dom3, "");
71         fstrcpy(dom5, "");
72         if (sid3 != NULL)
73         {
74                 ZERO_STRUCTP(sid3);
75         }
76         if (sid5 != NULL)
77         {
78                 ZERO_STRUCTP(sid5);
79         }
80
81         fstrcpy(srv_name, "\\\\");
82         fstrcat(srv_name, myname);
83         strupper(srv_name);
84
85         /* lookup domain controller; receive a policy handle */
86         res = res ? lsa_open_policy(srv_name, &pol, False) : False;
87
88         if (sid3 != NULL)
89         {
90                 /* send client info query, level 3.  receive domain name and sid */
91                 res = res ? lsa_query_info_pol(&pol, 3, dom3, sid3) : False;
92         }
93
94         if (sid5 != NULL)
95         {
96                 /* send client info query, level 5.  receive domain name and sid */
97                 res = res ? lsa_query_info_pol(&pol, 5, dom5, sid5) : False;
98         }
99
100         /* close policy handle */
101         res = res ? lsa_close(&pol) : False;
102
103         /* close the session */
104         cli_connection_unlink(con);
105
106         if (res)
107         {
108                 pstring sid;
109                 DEBUG(2,("LSA Query Info Policy\n"));
110                 if (sid3 != NULL)
111                 {
112                         sid_to_string(sid, sid3);
113                         DEBUG(2,("Domain Member     - Domain: %s SID: %s\n", dom3, sid));
114                 }
115                 if (sid5 != NULL)
116                 {
117                         sid_to_string(sid, sid5);
118                         DEBUG(2,("Domain Controller - Domain: %s SID: %s\n", dom5, sid));
119                 }
120         }
121         else
122         {
123                 DEBUG(1,("lsa query info failed\n"));
124         }
125
126         return res;
127 }
128
129 /****************************************************************************
130  obtain a sid and domain name from a Domain Controller.  
131 ****************************************************************************/
132 BOOL get_trust_sid_and_domain(const char* myname, char *server,
133                                 DOM_SID *sid,
134                                 char *domain, size_t len)
135 {
136         POLICY_HND pol;
137         fstring srv_name;
138         struct cli_connection *con = NULL;
139         BOOL res = True;
140         BOOL res1 = True;
141         DOM_SID sid3;
142         DOM_SID sid5;
143         fstring dom3;
144         fstring dom5;
145
146         extern struct user_credentials *usr_creds;
147         struct user_credentials usr;
148         
149         usr_creds = &usr;
150         ZERO_STRUCT(usr);
151         pwd_set_nullpwd(&usr.pwd);
152
153         if (!cli_connection_init_list(server, PIPE_LSARPC, &con))
154         {
155                 DEBUG(0,("get_trust_sid: unable to initialise client connection.\n"));
156                 return False;
157         }
158
159         fstrcpy(dom3, "");
160         fstrcpy(dom5, "");
161         ZERO_STRUCT(sid3);
162         ZERO_STRUCT(sid5);
163
164         fstrcpy(srv_name, "\\\\");
165         fstrcat(srv_name, myname);
166         strupper(srv_name);
167
168         /* lookup domain controller; receive a policy handle */
169         res = res ? lsa_open_policy(srv_name, &pol, False) : False;
170
171         /* send client info query, level 3.  receive domain name and sid */
172         res1 = res ? lsa_query_info_pol(&pol, 3, dom3, &sid3) : False;
173
174         /* send client info query, level 5.  receive domain name and sid */
175         res1 = res1 ? lsa_query_info_pol(&pol, 5, dom5, &sid5) : False;
176
177         /* close policy handle */
178         res = res ? lsa_close(&pol) : False;
179
180         /* close the session */
181         cli_connection_unlink(con);
182
183         if (res1)
184         {
185                 pstring sid_str;
186                 DEBUG(2,("LSA Query Info Policy\n"));
187                 sid_to_string(sid_str, &sid3);
188                 DEBUG(2,("Domain Member     - Domain: %s SID: %s\n",
189                           dom3, sid_str));
190                 sid_to_string(sid_str, &sid5);
191                 DEBUG(2,("Domain Controller - Domain: %s SID: %s\n",
192                           dom5, sid_str));
193
194                 if (dom5[0] != 0 && sid_equal(&sid3, &sid5))
195                 {
196                         safe_strcpy(domain, dom5, len);
197                         sid_copy(sid, &sid5);
198                 }
199                 else
200                 {
201                         DEBUG(2,("Server %s is not a PDC\n", server));
202                         return False;
203                 }
204
205         }
206         else
207         {
208                 DEBUG(1,("lsa query info failed\n"));
209         }
210
211         return res1;
212 }
213
214 /****************************************************************************
215 do a LSA Open Policy
216 ****************************************************************************/
217 BOOL lsa_open_policy(const char *server_name, POLICY_HND *hnd,
218                         BOOL sec_qos)
219 {
220         prs_struct rbuf;
221         prs_struct buf; 
222         LSA_Q_OPEN_POL q_o;
223         LSA_SEC_QOS qos;
224         BOOL valid_pol = False;
225         struct cli_connection *con = NULL;
226
227         if (!cli_connection_init(server_name, PIPE_LSARPC, &con))
228         {
229                 return False;
230         }
231
232         if (hnd == NULL) return False;
233
234         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
235         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
236
237         /* create and send a MSRPC command with api LSA_OPENPOLICY */
238
239         DEBUG(4,("LSA Open Policy\n"));
240
241         /* store the parameters */
242         if (sec_qos)
243         {
244                 make_lsa_sec_qos(&qos, 2, 1, 0, 0x20000000);
245                 make_q_open_pol(&q_o, 0x5c, 0, 0x02000000, &qos);
246         }
247         else
248         {
249                 make_q_open_pol(&q_o, 0x5c, 0, 0x1, NULL);
250         }
251
252         /* turn parameters into data stream */
253         lsa_io_q_open_pol("", &q_o, &buf, 0);
254
255         /* send the data on \PIPE\ */
256         if (rpc_con_pipe_req(con, LSA_OPENPOLICY, &buf, &rbuf))
257         {
258                 LSA_R_OPEN_POL r_o;
259                 BOOL p;
260
261                 lsa_io_r_open_pol("", &r_o, &rbuf, 0);
262                 p = rbuf.offset != 0;
263
264                 if (p && r_o.status != 0)
265                 {
266                         /* report error code */
267                         DEBUG(0,("LSA_OPENPOLICY: %s\n", get_nt_error_msg(r_o.status)));
268                         p = False;
269                 }
270
271                 if (p)
272                 {
273                         /* ok, at last: we're happy. return the policy handle */
274                         memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
275                         
276                         valid_pol = register_policy_hnd(hnd) &&
277                                     set_policy_con(hnd, con, 
278                                                          cli_connection_unlink);
279                 }
280         }
281
282         prs_mem_free(&rbuf);
283         prs_mem_free(&buf );
284
285         return valid_pol;
286 }
287
288 /****************************************************************************
289 do a LSA Open Policy2
290 ****************************************************************************/
291 BOOL lsa_open_policy2( const char *server_name, POLICY_HND *hnd,
292                         BOOL sec_qos)
293 {
294         prs_struct rbuf;
295         prs_struct buf; 
296         LSA_Q_OPEN_POL2 q_o;
297         LSA_SEC_QOS qos;
298         BOOL valid_pol = False;
299
300         struct cli_connection *con = NULL;
301
302         if (!cli_connection_init(server_name, PIPE_LSARPC, &con))
303         {
304                 return False;
305         }
306
307         if (hnd == NULL) return False;
308
309         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
310         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
311
312         /* create and send a MSRPC command with api LSA_OPENPOLICY2 */
313
314         DEBUG(4,("LSA Open Policy2\n"));
315
316         /* store the parameters */
317         if (sec_qos)
318         {
319                 make_lsa_sec_qos(&qos, 2, 1, 0, 0x02000000);
320                 make_q_open_pol2(&q_o, server_name, 0, 0x02000000, &qos);
321         }
322         else
323         {
324                 make_q_open_pol2(&q_o, server_name, 0, 0x02000000, NULL);
325         }
326
327         /* turn parameters into data stream */
328         lsa_io_q_open_pol2("", &q_o, &buf, 0);
329
330         /* send the data on \PIPE\ */
331         if (rpc_hnd_pipe_req(hnd, LSA_OPENPOLICY2, &buf, &rbuf))
332         {
333                 LSA_R_OPEN_POL2 r_o;
334                 BOOL p;
335
336                 lsa_io_r_open_pol2("", &r_o, &rbuf, 0);
337                 p = rbuf.offset != 0;
338
339                 if (p && r_o.status != 0)
340                 {
341                         /* report error code */
342                         DEBUG(0,("LSA_OPENPOLICY2: %s\n", get_nt_error_msg(r_o.status)));
343                         p = False;
344                 }
345
346                 if (p)
347                 {
348                         /* ok, at last: we're happy. return the policy handle */
349                         memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
350                         valid_pol = register_policy_hnd(hnd) &&
351                                     set_policy_con(hnd, con, 
352                                                          cli_connection_unlink);
353                 }
354         }
355
356         prs_mem_free(&rbuf);
357         prs_mem_free(&buf );
358
359         return valid_pol;
360 }
361
362 /****************************************************************************
363 do a LSA Open Secret
364 ****************************************************************************/
365 BOOL lsa_open_secret( const POLICY_HND *hnd,
366                                 const char *secret_name,
367                                 uint32 des_access,
368                                 POLICY_HND *hnd_secret)
369 {
370         prs_struct rbuf;
371         prs_struct buf; 
372         LSA_Q_OPEN_SECRET q_o;
373         BOOL valid_pol = False;
374
375         if (hnd == NULL) return False;
376
377         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
378         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
379
380         /* create and send a MSRPC command with api LSA_OPENSECRET */
381
382         DEBUG(4,("LSA Open Secret\n"));
383
384         make_q_open_secret(&q_o, hnd, secret_name, des_access);
385
386         /* turn parameters into data stream */
387         lsa_io_q_open_secret("", &q_o, &buf, 0);
388
389         /* send the data on \PIPE\ */
390         if (rpc_hnd_pipe_req(hnd, LSA_OPENSECRET, &buf, &rbuf))
391         {
392                 LSA_R_OPEN_SECRET r_o;
393                 BOOL p;
394
395                 lsa_io_r_open_secret("", &r_o, &rbuf, 0);
396                 p = rbuf.offset != 0;
397
398                 if (p && r_o.status != 0)
399                 {
400                         /* report error code */
401                         DEBUG(0,("LSA_OPENSECRET: %s\n", get_nt_error_msg(r_o.status)));
402                         p = False;
403                 }
404
405                 if (p)
406                 {
407                         /* ok, at last: we're happy. return the policy handle */
408                         memcpy(hnd_secret, r_o.pol.data, sizeof(hnd_secret->data));
409                         valid_pol = True;
410                 }
411         }
412
413         prs_mem_free(&rbuf);
414         prs_mem_free(&buf );
415
416         return valid_pol;
417 }
418
419 /****************************************************************************
420 do a LSA Query Secret
421 ****************************************************************************/
422 BOOL lsa_query_secret(POLICY_HND *hnd, STRING2 *secret,
423                       NTTIME *last_update)
424 {
425         prs_struct rbuf;
426         prs_struct buf; 
427         LSA_Q_QUERY_SECRET q_q;
428         BOOL valid_info = False;
429
430         if (hnd == NULL) return False;
431
432         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
433         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
434
435         /* create and send a MSRPC command with api LSA_QUERYSECRET */
436
437         DEBUG(4,("LSA Query Secret\n"));
438
439         make_q_query_secret(&q_q, hnd);
440
441         /* turn parameters into data stream */
442         lsa_io_q_query_secret("", &q_q, &buf, 0);
443
444         /* send the data on \PIPE\ */
445         if (rpc_hnd_pipe_req(hnd, LSA_QUERYSECRET, &buf, &rbuf))
446         {
447                 LSA_R_QUERY_SECRET r_q;
448                 BOOL p;
449
450                 lsa_io_r_query_secret("", &r_q, &rbuf, 0);
451                 p = rbuf.offset != 0;
452
453                 if (p && r_q.status != 0)
454                 {
455                         /* report error code */
456                         DEBUG(0,("LSA_QUERYSECRET: %s\n", get_nt_error_msg(r_q.status)));
457                         p = False;
458                 }
459
460                 if (p && (r_q.info.ptr_value != 0) &&
461                     (r_q.info.value.ptr_secret != 0) &&
462                     (r_q.info.ptr_update != 0))
463                 {
464                         uchar sess_key[16];
465                         STRING2 enc_secret;
466                         memcpy(&enc_secret,  &(r_q.info.value.enc_secret), sizeof(STRING2));
467                         memcpy(last_update, &(r_q.info.last_update),      sizeof(NTTIME));
468                         if (!cli_get_usr_sesskey(hnd, sess_key))
469                         {
470                                 return False;
471                         }
472 #ifdef DEBUG_PASSWORD
473                         dump_data(100, sess_key, 16);
474 #endif
475                         valid_info = nt_decrypt_string2(secret, &enc_secret,
476                                      sess_key);
477                 }
478         }
479
480         prs_mem_free(&rbuf);
481         prs_mem_free(&buf );
482
483         return valid_info;
484 }
485
486
487 /****************************************************************************
488 do a LSA Lookup Names
489 ****************************************************************************/
490 BOOL lsa_lookup_names( POLICY_HND *hnd,
491                         int num_names,
492                         char **names,
493                         DOM_SID **sids,
494                         uint8 **types,
495                         int *num_sids)
496 {
497         prs_struct rbuf;
498         prs_struct buf; 
499         LSA_Q_LOOKUP_NAMES q_l;
500         BOOL valid_response = False;
501
502         if (hnd == NULL || num_sids == 0 || sids == NULL) return False;
503
504         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
505         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
506
507         /* create and send a MSRPC command with api LSA_LOOKUP_NAMES */
508
509         DEBUG(4,("LSA Lookup NAMEs\n"));
510
511         /* store the parameters */
512         make_q_lookup_names(&q_l, hnd, num_names, names);
513
514         /* turn parameters into data stream */
515         lsa_io_q_lookup_names("", &q_l, &buf, 0);
516
517         /* send the data on \PIPE\ */
518         if (rpc_hnd_pipe_req(hnd, LSA_LOOKUPNAMES, &buf, &rbuf))
519         {
520                 LSA_R_LOOKUP_NAMES r_l;
521                 DOM_R_REF ref;
522                 DOM_RID2 t_rids[MAX_LOOKUP_SIDS];
523                 BOOL p;
524
525                 ZERO_STRUCT(ref);
526                 ZERO_STRUCT(t_rids);
527
528                 r_l.dom_ref = &ref;
529                 r_l.dom_rid = t_rids;
530
531                 lsa_io_r_lookup_names("", &r_l, &rbuf, 0);
532                 p = rbuf.offset != 0;
533                 
534                 if (p && r_l.status != 0)
535                 {
536                         /* report error code */
537                         DEBUG(1,("LSA_LOOKUP_NAMES: %s\n", get_nt_error_msg(r_l.status)));
538                         p = False;
539                 }
540
541                 if (p)
542                 {
543                         if (r_l.ptr_dom_ref != 0 && r_l.ptr_entries != 0)
544                         {
545                                 valid_response = True;
546                         }
547                 }
548
549                 if (num_sids != NULL && valid_response)
550                 {
551                         (*num_sids) = r_l.num_entries;
552                 }
553                 if (valid_response)
554                 {
555                         uint32 i;
556                         for (i = 0; i < r_l.num_entries; i++)
557                         {
558                                 if (t_rids[i].rid_idx >= ref.num_ref_doms_1 &&
559                                     t_rids[i].rid_idx != 0xffffffff)
560                                 {
561                                         DEBUG(0,("LSA_LOOKUP_NAMES: domain index %d out of bounds\n",
562                                                   t_rids[i].rid_idx));
563                                         valid_response = False;
564                                         break;
565                                 }
566                         }
567                 }
568
569                 if (types != NULL && valid_response && r_l.num_entries != 0)
570                 {
571                         (*types) = (uint8*)malloc((*num_sids) * sizeof(uint8));
572                 }
573
574                 if (sids != NULL && valid_response && r_l.num_entries != 0)
575                 {
576                         (*sids) = (DOM_SID*)malloc((*num_sids) * sizeof(DOM_SID));
577                 }
578
579                 if (sids != NULL && (*sids) != NULL)
580                 {
581                         int i;
582                         /* take each name, construct a SID */
583                         for (i = 0; i < (*num_sids); i++)
584                         {
585                                 uint32 dom_idx = t_rids[i].rid_idx;
586                                 uint32 dom_rid = t_rids[i].rid;
587                                 DOM_SID *sid = &(*sids)[i];
588                                 if (dom_idx != 0xffffffff)
589                                 {
590                                         sid_copy(sid, &ref.ref_dom[dom_idx].ref_dom.sid);
591                                         if (dom_rid != 0xffffffff)
592                                         {
593                                                 sid_append_rid(sid, dom_rid);
594                                         }
595                                         if (types != NULL && (*types) != NULL)
596                                         {
597                                                 (*types)[i] = t_rids[i].type;
598                                         }
599                                 }
600                                 else
601                                 {
602                                         ZERO_STRUCTP(sid);
603                                         if (types != NULL && (*types) != NULL)
604                                         {
605                                                 (*types)[i] = SID_NAME_UNKNOWN;
606                                         }
607                                 }
608                         }
609                 }
610         }
611
612         prs_mem_free(&rbuf);
613         prs_mem_free(&buf );
614
615         return valid_response;
616 }
617
618 /****************************************************************************
619 do a LSA Lookup SIDs
620 ****************************************************************************/
621 BOOL lsa_lookup_sids(POLICY_HND *hnd,
622                         int num_sids,
623                         DOM_SID **sids,
624                         char ***names,
625                         uint8 **types,
626                         int *num_names)
627 {
628         prs_struct rbuf;
629         prs_struct buf; 
630         LSA_Q_LOOKUP_SIDS q_l;
631         BOOL valid_response = False;
632
633         ZERO_STRUCT(q_l);
634
635         if (hnd == NULL || num_sids == 0 || sids == NULL) return False;
636
637         if (num_names != NULL)
638         {
639                 *num_names = 0;
640         }
641         if (types != NULL)
642         {
643                 *types = NULL;
644         }
645         if (names != NULL)
646         {
647                 *names = NULL;
648         }
649
650         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
651         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
652
653         /* create and send a MSRPC command with api LSA_LOOKUP_SIDS */
654
655         DEBUG(4,("LSA Lookup SIDs\n"));
656
657         /* store the parameters */
658         make_q_lookup_sids(&q_l, hnd, num_sids, sids, 1);
659
660         /* turn parameters into data stream */
661         lsa_io_q_lookup_sids("", &q_l, &buf, 0);
662
663         /* send the data on \PIPE\ */
664         if (rpc_hnd_pipe_req(hnd, LSA_LOOKUPSIDS, &buf, &rbuf))
665         {
666                 LSA_R_LOOKUP_SIDS r_l;
667                 DOM_R_REF ref;
668                 LSA_TRANS_NAME_ENUM t_names;
669                 BOOL p;
670
671                 r_l.dom_ref = &ref;
672                 r_l.names   = &t_names;
673
674                 lsa_io_r_lookup_sids("", &r_l, &rbuf, 0);
675                 p = rbuf.offset != 0;
676                 
677                 if (p && r_l.status != 0 &&
678                          r_l.status != 0x107 &&
679                          r_l.status != (0xC0000000 | NT_STATUS_NONE_MAPPED))
680                 {
681                         /* report error code */
682                         DEBUG(1,("LSA_LOOKUP_SIDS: %s\n", get_nt_error_msg(r_l.status)));
683                         p = False;
684                 }
685
686                 if (p)
687                 {
688                         if (t_names.ptr_trans_names != 0 && r_l.ptr_dom_ref != 0)
689                         {
690                                 valid_response = True;
691                         }
692                 }
693
694                 if (num_names != NULL && valid_response)
695                 {
696                         (*num_names) = t_names.num_entries;
697                 }
698                 if (valid_response)
699                 {
700                         uint32 i;
701                         for (i = 0; i < t_names.num_entries; i++)
702                         {
703                                 if (t_names.name[i].domain_idx >= ref.num_ref_doms_1)
704                                 {
705                                         DEBUG(0,("LSA_LOOKUP_SIDS: domain index out of bounds\n"));
706                                         valid_response = False;
707                                         break;
708                                 }
709                         }
710                 }
711
712                 if (types != NULL && valid_response && (*num_names) != 0)
713                 {
714                         (*types) = (uint8*)malloc((*num_names) * sizeof(uint8));
715                 }
716
717                 if (names != NULL && valid_response && (*num_names) != 0)
718                 {
719                         (*names) = (char**)malloc((*num_names) * sizeof(char*));
720                 }
721
722                 if (names != NULL && (*names) != NULL)
723                 {
724                         int i;
725                         /* take each name, construct a \DOMAIN\name string */
726                         for (i = 0; i < (*num_names); i++)
727                         {
728                                 fstring name;
729                                 fstring dom_name;
730                                 fstring full_name;
731                                 uint32 dom_idx = t_names.name[i].domain_idx;
732
733                                 if (dom_idx != 0xffffffff)
734                                 {
735                                         unistr2_to_ascii(dom_name, &ref.ref_dom[dom_idx].uni_dom_name, sizeof(dom_name)-1);
736                                         unistr2_to_ascii(name, &t_names.uni_name[i], sizeof(name)-1);
737                                         
738                                         memset(full_name, 0, sizeof(full_name));
739
740                                         slprintf(full_name, sizeof(full_name)-1, "%s\\%s",
741                                                  dom_name, name);
742
743                                         (*names)[i] = strdup(full_name);
744                                         if (types != NULL && (*types) != NULL)
745                                         {
746                                                 (*types)[i] = t_names.name[i].sid_name_use;
747                                         }
748                                 }
749                                 else
750                                 {
751                                         (*names)[i] = NULL;
752                                         if (types != NULL && (*types) != NULL)
753                                         {
754                                                 (*types)[i] = SID_NAME_UNKNOWN;
755                                         }
756                                 }
757                         }
758                 }
759         }
760
761         prs_mem_free(&rbuf);
762         prs_mem_free(&buf );
763
764         return valid_response;
765 }
766
767 /****************************************************************************
768 do a LSA Query Info Policy
769 ****************************************************************************/
770 BOOL lsa_query_info_pol(POLICY_HND *hnd, uint16 info_class,
771                         fstring domain_name, DOM_SID *domain_sid)
772 {
773         prs_struct rbuf;
774         prs_struct buf; 
775         LSA_Q_QUERY_INFO q_q;
776         BOOL valid_response = False;
777
778         ZERO_STRUCTP(domain_sid);
779         domain_name[0] = 0;
780
781         if (hnd == NULL || domain_name == NULL || domain_sid == NULL) return False;
782
783         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
784         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
785
786         /* create and send a MSRPC command with api LSA_QUERYINFOPOLICY */
787
788         DEBUG(4,("LSA Query Info Policy\n"));
789
790         /* store the parameters */
791         make_q_query(&q_q, hnd, info_class);
792
793         /* turn parameters into data stream */
794         lsa_io_q_query("", &q_q, &buf, 0);
795
796         /* send the data on \PIPE\ */
797         if (rpc_hnd_pipe_req(hnd, LSA_QUERYINFOPOLICY, &buf, &rbuf))
798         {
799                 LSA_R_QUERY_INFO r_q;
800                 BOOL p;
801
802                 lsa_io_r_query("", &r_q, &rbuf, 0);
803                 p = rbuf.offset != 0;
804                 
805                 if (p && r_q.status != 0)
806                 {
807                         /* report error code */
808                         DEBUG(0,("LSA_QUERYINFOPOLICY: %s\n", get_nt_error_msg(r_q.status)));
809                         p = False;
810                 }
811
812                 if (p && r_q.info_class != q_q.info_class)
813                 {
814                         /* report different info classes */
815                         DEBUG(0,("LSA_QUERYINFOPOLICY: error info_class (q,r) differ - (%x,%x)\n",
816                                         q_q.info_class, r_q.info_class));
817                         p = False;
818                 }
819
820                 if (p)
821                 {
822                         fstring sid_str;
823                         /* ok, at last: we're happy. */
824                         switch (r_q.info_class)
825                         {
826                                 case 3:
827                                 {
828                                         if (r_q.dom.id3.buffer_dom_name != 0)
829                                         {
830                                                 unistr2_to_ascii(domain_name, &r_q.dom.id3.uni_domain_name, sizeof(fstring)-1);
831                                         }
832                                         if (r_q.dom.id3.buffer_dom_sid != 0)
833                                         {
834                                                 *domain_sid = r_q.dom.id3.dom_sid.sid;
835                                         }
836
837                                         valid_response = True;
838                                         break;
839                                 }
840                                 case 5:
841                                 {
842                                         if (r_q.dom.id5.buffer_dom_name != 0)
843                                         {
844                                                 unistr2_to_ascii(domain_name, &r_q.dom.id5.uni_domain_name, sizeof(fstring)-1);
845                                         }
846                                         if (r_q.dom.id5.buffer_dom_sid != 0)
847                                         {
848                                                 *domain_sid = r_q.dom.id5.dom_sid.sid;
849                                         }
850
851                                         valid_response = True;
852                                         break;
853                                 }
854                                 default:
855                                 {
856                                         DEBUG(3,("LSA_QUERYINFOPOLICY: unknown info class\n"));
857                                         domain_name[0] = 0;
858
859                                         break;
860                                 }
861                         }
862                 
863                         sid_to_string(sid_str, domain_sid);
864                         DEBUG(3,("LSA_QUERYINFOPOLICY (level %x): domain:%s  domain sid:%s\n",
865                                   r_q.info_class, domain_name, sid_str));
866                 }
867         }
868
869         prs_mem_free(&rbuf);
870         prs_mem_free(&buf );
871
872         return valid_response;
873 }
874
875 /****************************************************************************
876 do a LSA Enumerate Trusted Domain 
877 ****************************************************************************/
878 BOOL lsa_enum_trust_dom(POLICY_HND *hnd, uint32 *enum_ctx,
879                         uint32 *num_doms, char ***names,
880                         DOM_SID ***sids)
881 {
882         prs_struct rbuf;
883         prs_struct buf; 
884         LSA_Q_ENUM_TRUST_DOM q_q;
885         BOOL valid_response = False;
886
887         if (hnd == NULL || num_doms == NULL || names == NULL) return False;
888
889         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
890         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
891
892         /* create and send a MSRPC command with api LSA_ENUMTRUSTDOM */
893
894         DEBUG(4,("LSA Enum Trusted Domains\n"));
895
896         /* store the parameters */
897         make_q_enum_trust_dom(&q_q, hnd, *enum_ctx, 0xffffffff);
898
899         /* turn parameters into data stream */
900         lsa_io_q_enum_trust_dom("", &q_q, &buf, 0);
901
902         /* send the data on \PIPE\ */
903         if (rpc_hnd_pipe_req(hnd, LSA_ENUMTRUSTDOM, &buf, &rbuf))
904         {
905                 LSA_R_ENUM_TRUST_DOM r_q;
906                 BOOL p;
907
908                 lsa_io_r_enum_trust_dom("", &r_q, &rbuf, 0);
909                 p = rbuf.offset != 0;
910                 
911                 if (p && r_q.status != 0)
912                 {
913                         /* report error code */
914                         DEBUG(0,("LSA_ENUMTRUSTDOM: %s\n", get_nt_error_msg(r_q.status)));
915                         p = r_q.status == 0x8000001a;
916                 }
917
918                 if (p)
919                 {
920                         uint32 i;
921                         uint32 num_sids = 0;
922                         valid_response = True;
923
924                         for (i = 0; i < r_q.num_domains; i++)
925                         {
926                                 fstring tmp;
927                                 unistr2_to_ascii(tmp, &r_q.uni_domain_name[i],
928                                                  sizeof(tmp)-1);
929                                 add_chars_to_array(num_doms, names, tmp);
930                                 add_sid_to_array(&num_sids, sids,
931                                                  &r_q.domain_sid[i].sid);
932                         }
933
934                         if (r_q.status == 0x0)
935                         {
936                                 *enum_ctx = r_q.enum_context;
937                         }
938                         else
939                         {
940                                 *enum_ctx = 0;
941                         }
942                 }
943         }
944
945         prs_mem_free(&rbuf);
946         prs_mem_free(&buf );
947
948         return valid_response;
949 }
950
951 /****************************************************************************
952 do a LSA Close
953 ****************************************************************************/
954 BOOL lsa_close(POLICY_HND *hnd)
955 {
956         prs_struct rbuf;
957         prs_struct buf; 
958         LSA_Q_CLOSE q_c;
959         BOOL valid_close = False;
960
961         if (hnd == NULL) return False;
962
963         /* create and send a MSRPC command with api LSA_OPENPOLICY */
964
965         prs_init(&buf , 1024, 4, SAFETY_MARGIN, False);
966         prs_init(&rbuf, 0   , 4, SAFETY_MARGIN, True );
967
968         DEBUG(4,("LSA Close\n"));
969
970         /* store the parameters */
971         make_lsa_q_close(&q_c, hnd);
972
973         /* turn parameters into data stream */
974         lsa_io_q_close("", &q_c, &buf, 0);
975
976         /* send the data on \PIPE\ */
977         if (rpc_hnd_pipe_req(hnd, LSA_CLOSE, &buf, &rbuf))
978         {
979                 LSA_R_CLOSE r_c;
980                 BOOL p;
981
982                 lsa_io_r_close("", &r_c, &rbuf, 0);
983                 p = rbuf.offset != 0;
984
985                 if (p && r_c.status != 0)
986                 {
987                         /* report error code */
988                         DEBUG(0,("LSA_CLOSE: %s\n", get_nt_error_msg(r_c.status)));
989                         p = False;
990                 }
991
992                 if (p)
993                 {
994                         /* check that the returned policy handle is all zeros */
995                         uint32 i;
996                         valid_close = True;
997
998                         for (i = 0; i < sizeof(r_c.pol.data); i++)
999                         {
1000                                 if (r_c.pol.data[i] != 0)
1001                                 {
1002                                         valid_close = False;
1003                                         break;
1004                                 }
1005                         }       
1006                         if (!valid_close)
1007                         {
1008                                 DEBUG(0,("LSA_CLOSE: non-zero handle returned\n"));
1009                         }
1010                 }
1011         }
1012
1013         prs_mem_free(&rbuf);
1014         prs_mem_free(&buf );
1015
1016         close_policy_hnd(hnd);
1017
1018         return valid_close;
1019 }
1020