r6228: remove BUFHDR2 and clean up LsaEnumTrustedDomains()
[ira/wip.git] / source3 / rpc_parse / parse_lsa.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Andrew Tridgell              1992-1997,
5  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6  *  Copyright (C) Paul Ashton                       1997,
7  *  Copyright (C) Andrew Bartlett                   2002,
8  *  Copyright (C) Jim McDonough <jmcd@us.ibm.com>   2002.
9  *  Copyright (C) Gerald )Jerry) Carter             2005
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 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_RPC_PARSE
30
31 static BOOL lsa_io_trans_names(const char *desc, LSA_TRANS_NAME_ENUM *trn, prs_struct *ps, int depth);
32
33 /*******************************************************************
34  Inits a LSA_TRANS_NAME structure.
35 ********************************************************************/
36
37 void init_lsa_trans_name(LSA_TRANS_NAME *trn, UNISTR2 *uni_name,
38                          uint16 sid_name_use, const char *name, uint32 idx)
39 {
40         trn->sid_name_use = sid_name_use;
41         init_unistr2(uni_name, name, UNI_FLAGS_NONE);
42         init_uni_hdr(&trn->hdr_name, uni_name);
43         trn->domain_idx = idx;
44 }
45
46 /*******************************************************************
47  Reads or writes a LSA_TRANS_NAME structure.
48 ********************************************************************/
49
50 static BOOL lsa_io_trans_name(const char *desc, LSA_TRANS_NAME *trn, prs_struct *ps, 
51                               int depth)
52 {
53         prs_debug(ps, depth, desc, "lsa_io_trans_name");
54         depth++;
55
56         if(!prs_align(ps))
57                 return False;
58         
59         if(!prs_uint16("sid_name_use", ps, depth, &trn->sid_name_use))
60                 return False;
61         if(!prs_align(ps))
62                 return False;
63         
64         if(!smb_io_unihdr ("hdr_name", &trn->hdr_name, ps, depth))
65                 return False;
66         if(!prs_uint32("domain_idx  ", ps, depth, &trn->domain_idx))
67                 return False;
68
69         return True;
70 }
71
72 /*******************************************************************
73  Reads or writes a DOM_R_REF structure.
74 ********************************************************************/
75
76 static BOOL lsa_io_dom_r_ref(const char *desc, DOM_R_REF *r_r, prs_struct *ps, 
77                              int depth)
78 {
79         unsigned int i;
80
81         prs_debug(ps, depth, desc, "lsa_io_dom_r_ref");
82         depth++;
83
84         if(!prs_align(ps))
85                 return False;
86         
87         if(!prs_uint32("num_ref_doms_1", ps, depth, &r_r->num_ref_doms_1)) /* num referenced domains? */
88                 return False;
89         if(!prs_uint32("ptr_ref_dom   ", ps, depth, &r_r->ptr_ref_dom)) /* undocumented buffer pointer. */
90                 return False;
91         if(!prs_uint32("max_entries   ", ps, depth, &r_r->max_entries)) /* 32 - max number of entries */
92                 return False;
93
94         SMB_ASSERT_ARRAY(r_r->hdr_ref_dom, r_r->num_ref_doms_1);
95
96         if (r_r->ptr_ref_dom != 0) {
97
98                 if(!prs_uint32("num_ref_doms_2", ps, depth, &r_r->num_ref_doms_2)) /* 4 - num referenced domains? */
99                         return False;
100
101                 SMB_ASSERT_ARRAY(r_r->ref_dom, r_r->num_ref_doms_2);
102
103                 for (i = 0; i < r_r->num_ref_doms_1; i++) {
104                         fstring t;
105
106                         slprintf(t, sizeof(t) - 1, "dom_ref[%d] ", i);
107                         if(!smb_io_unihdr(t, &r_r->hdr_ref_dom[i].hdr_dom_name, ps, depth))
108                                 return False;
109
110                         slprintf(t, sizeof(t) - 1, "sid_ptr[%d] ", i);
111                         if(!prs_uint32(t, ps, depth, &r_r->hdr_ref_dom[i].ptr_dom_sid))
112                                 return False;
113                 }
114
115                 for (i = 0; i < r_r->num_ref_doms_2; i++) {
116                         fstring t;
117
118                         if (r_r->hdr_ref_dom[i].hdr_dom_name.buffer != 0) {
119                                 slprintf(t, sizeof(t) - 1, "dom_ref[%d] ", i);
120                                 if(!smb_io_unistr2(t, &r_r->ref_dom[i].uni_dom_name, True, ps, depth)) /* domain name unicode string */
121                                         return False;
122                                 if(!prs_align(ps))
123                                         return False;
124                         }
125
126                         if (r_r->hdr_ref_dom[i].ptr_dom_sid != 0) {
127                                 slprintf(t, sizeof(t) - 1, "sid_ptr[%d] ", i);
128                                 if(!smb_io_dom_sid2(t, &r_r->ref_dom[i].ref_dom, ps, depth)) /* referenced domain SIDs */
129                                         return False;
130                         }
131                 }
132         }
133
134         return True;
135 }
136
137 /*******************************************************************
138  Inits an LSA_SEC_QOS structure.
139 ********************************************************************/
140
141 void init_lsa_sec_qos(LSA_SEC_QOS *qos, uint16 imp_lev, uint8 ctxt, uint8 eff)
142 {
143         DEBUG(5, ("init_lsa_sec_qos\n"));
144
145         qos->len = 0x0c; /* length of quality of service block, in bytes */
146         qos->sec_imp_level = imp_lev;
147         qos->sec_ctxt_mode = ctxt;
148         qos->effective_only = eff;
149 }
150
151 /*******************************************************************
152  Reads or writes an LSA_SEC_QOS structure.
153 ********************************************************************/
154
155 static BOOL lsa_io_sec_qos(const char *desc,  LSA_SEC_QOS *qos, prs_struct *ps, 
156                            int depth)
157 {
158         uint32 start;
159
160         prs_debug(ps, depth, desc, "lsa_io_obj_qos");
161         depth++;
162
163         if(!prs_align(ps))
164                 return False;
165         
166         start = prs_offset(ps);
167
168         /* these pointers had _better_ be zero, because we don't know
169            what they point to!
170          */
171         if(!prs_uint32("len           ", ps, depth, &qos->len)) /* 0x18 - length (in bytes) inc. the length field. */
172                 return False;
173         if(!prs_uint16("sec_imp_level ", ps, depth, &qos->sec_imp_level ))
174                 return False;
175         if(!prs_uint8 ("sec_ctxt_mode ", ps, depth, &qos->sec_ctxt_mode ))
176                 return False;
177         if(!prs_uint8 ("effective_only", ps, depth, &qos->effective_only))
178                 return False;
179
180         if (qos->len != prs_offset(ps) - start) {
181                 DEBUG(3,("lsa_io_sec_qos: length %x does not match size %x\n",
182                          qos->len, prs_offset(ps) - start));
183         }
184
185         return True;
186 }
187
188 /*******************************************************************
189  Inits an LSA_OBJ_ATTR structure.
190 ********************************************************************/
191
192 static void init_lsa_obj_attr(LSA_OBJ_ATTR *attr, uint32 attributes, LSA_SEC_QOS *qos)
193 {
194         DEBUG(5, ("init_lsa_obj_attr\n"));
195
196         attr->len = 0x18; /* length of object attribute block, in bytes */
197         attr->ptr_root_dir = 0;
198         attr->ptr_obj_name = 0;
199         attr->attributes = attributes;
200         attr->ptr_sec_desc = 0;
201         
202         if (qos != NULL) {
203                 attr->ptr_sec_qos = 1;
204                 attr->sec_qos = qos;
205         } else {
206                 attr->ptr_sec_qos = 0;
207                 attr->sec_qos = NULL;
208         }
209 }
210
211 /*******************************************************************
212  Reads or writes an LSA_OBJ_ATTR structure.
213 ********************************************************************/
214
215 static BOOL lsa_io_obj_attr(const char *desc, LSA_OBJ_ATTR *attr, prs_struct *ps, 
216                             int depth)
217 {
218         prs_debug(ps, depth, desc, "lsa_io_obj_attr");
219         depth++;
220
221         if(!prs_align(ps))
222                 return False;
223         
224         /* these pointers had _better_ be zero, because we don't know
225            what they point to!
226          */
227         if(!prs_uint32("len         ", ps, depth, &attr->len)) /* 0x18 - length (in bytes) inc. the length field. */
228                 return False;
229         if(!prs_uint32("ptr_root_dir", ps, depth, &attr->ptr_root_dir)) /* 0 - root directory (pointer) */
230                 return False;
231         if(!prs_uint32("ptr_obj_name", ps, depth, &attr->ptr_obj_name)) /* 0 - object name (pointer) */
232                 return False;
233         if(!prs_uint32("attributes  ", ps, depth, &attr->attributes)) /* 0 - attributes (undocumented) */
234                 return False;
235         if(!prs_uint32("ptr_sec_desc", ps, depth, &attr->ptr_sec_desc)) /* 0 - security descriptior (pointer) */
236                 return False;
237         if(!prs_uint32("ptr_sec_qos ", ps, depth, &attr->ptr_sec_qos )) /* security quality of service (pointer) */
238                 return False;
239
240         if (attr->ptr_sec_qos != 0) {
241                 if (UNMARSHALLING(ps))
242                         if (!(attr->sec_qos = PRS_ALLOC_MEM(ps,LSA_SEC_QOS,1)))
243                                 return False;
244
245                 if(!lsa_io_sec_qos("sec_qos", attr->sec_qos, ps, depth))
246                         return False;
247         }
248
249         return True;
250 }
251
252
253 /*******************************************************************
254  Inits an LSA_Q_OPEN_POL structure.
255 ********************************************************************/
256
257 void init_q_open_pol(LSA_Q_OPEN_POL *r_q, uint16 system_name,
258                      uint32 attributes, uint32 desired_access,
259                      LSA_SEC_QOS *qos)
260 {
261         DEBUG(5, ("init_open_pol: attr:%d da:%d\n", attributes, 
262                   desired_access));
263
264         r_q->ptr = 1; /* undocumented pointer */
265
266         r_q->des_access = desired_access;
267
268         r_q->system_name = system_name;
269         init_lsa_obj_attr(&r_q->attr, attributes, qos);
270 }
271
272 /*******************************************************************
273  Reads or writes an LSA_Q_OPEN_POL structure.
274 ********************************************************************/
275
276 BOOL lsa_io_q_open_pol(const char *desc, LSA_Q_OPEN_POL *r_q, prs_struct *ps, 
277                        int depth)
278 {
279         prs_debug(ps, depth, desc, "lsa_io_q_open_pol");
280         depth++;
281
282         if(!prs_uint32("ptr       ", ps, depth, &r_q->ptr))
283                 return False;
284         if(!prs_uint16("system_name", ps, depth, &r_q->system_name))
285                 return False;
286         if(!prs_align( ps ))
287                 return False;
288
289         if(!lsa_io_obj_attr("", &r_q->attr, ps, depth))
290                 return False;
291
292         if(!prs_uint32("des_access", ps, depth, &r_q->des_access))
293                 return False;
294
295         return True;
296 }
297
298 /*******************************************************************
299  Reads or writes an LSA_R_OPEN_POL structure.
300 ********************************************************************/
301
302 BOOL lsa_io_r_open_pol(const char *desc, LSA_R_OPEN_POL *r_p, prs_struct *ps, 
303                        int depth)
304 {
305         prs_debug(ps, depth, desc, "lsa_io_r_open_pol");
306         depth++;
307
308         if(!smb_io_pol_hnd("", &r_p->pol, ps, depth))
309                 return False;
310
311         if(!prs_ntstatus("status", ps, depth, &r_p->status))
312                 return False;
313
314         return True;
315 }
316
317 /*******************************************************************
318  Inits an LSA_Q_OPEN_POL2 structure.
319 ********************************************************************/
320
321 void init_q_open_pol2(LSA_Q_OPEN_POL2 *r_q, const char *server_name,
322                         uint32 attributes, uint32 desired_access,
323                         LSA_SEC_QOS *qos)
324 {
325         DEBUG(5, ("init_q_open_pol2: attr:%d da:%d\n", attributes, 
326                   desired_access));
327
328         r_q->ptr = 1; /* undocumented pointer */
329
330         r_q->des_access = desired_access;
331
332         init_unistr2(&r_q->uni_server_name, server_name, UNI_STR_TERMINATE);
333
334         init_lsa_obj_attr(&r_q->attr, attributes, qos);
335 }
336
337 /*******************************************************************
338  Reads or writes an LSA_Q_OPEN_POL2 structure.
339 ********************************************************************/
340
341 BOOL lsa_io_q_open_pol2(const char *desc, LSA_Q_OPEN_POL2 *r_q, prs_struct *ps, 
342                         int depth)
343 {
344         prs_debug(ps, depth, desc, "lsa_io_q_open_pol2");
345         depth++;
346
347         if(!prs_uint32("ptr       ", ps, depth, &r_q->ptr))
348                 return False;
349
350         if(!smb_io_unistr2 ("", &r_q->uni_server_name, r_q->ptr, ps, depth))
351                 return False;
352         if(!lsa_io_obj_attr("", &r_q->attr, ps, depth))
353                 return False;
354
355         if(!prs_uint32("des_access", ps, depth, &r_q->des_access))
356                 return False;
357
358         return True;
359 }
360
361 /*******************************************************************
362  Reads or writes an LSA_R_OPEN_POL2 structure.
363 ********************************************************************/
364
365 BOOL lsa_io_r_open_pol2(const char *desc, LSA_R_OPEN_POL2 *r_p, prs_struct *ps, 
366                         int depth)
367 {
368         prs_debug(ps, depth, desc, "lsa_io_r_open_pol2");
369         depth++;
370
371         if(!smb_io_pol_hnd("", &r_p->pol, ps, depth))
372                 return False;
373
374         if(!prs_ntstatus("status", ps, depth, &r_p->status))
375                 return False;
376
377         return True;
378 }
379
380 /*******************************************************************
381 makes an LSA_Q_QUERY_SEC_OBJ structure.
382 ********************************************************************/
383
384 void init_q_query_sec_obj(LSA_Q_QUERY_SEC_OBJ *q_q, const POLICY_HND *hnd, 
385                           uint32 sec_info)
386 {
387         DEBUG(5, ("init_q_query_sec_obj\n"));
388
389         q_q->pol = *hnd;
390         q_q->sec_info = sec_info;
391
392         return;
393 }
394
395 /*******************************************************************
396  Reads or writes an LSA_Q_QUERY_SEC_OBJ structure.
397 ********************************************************************/
398
399 BOOL lsa_io_q_query_sec_obj(const char *desc, LSA_Q_QUERY_SEC_OBJ *q_q, 
400                             prs_struct *ps, int depth)
401 {
402         prs_debug(ps, depth, desc, "lsa_io_q_query_sec_obj");
403         depth++;
404
405         if (!smb_io_pol_hnd("", &q_q->pol, ps, depth))
406                 return False;
407
408         if (!prs_uint32("sec_info", ps, depth, &q_q->sec_info))
409                 return False;
410
411         return True;
412
413
414 /*******************************************************************
415  Reads or writes a LSA_R_QUERY_SEC_OBJ structure.
416 ********************************************************************/
417
418 BOOL lsa_io_r_query_sec_obj(const char *desc, LSA_R_QUERY_SEC_OBJ *r_u, 
419                             prs_struct *ps, int depth)
420 {
421         prs_debug(ps, depth, desc, "lsa_io_r_query_sec_obj");
422         depth++;
423
424         if (!prs_align(ps))
425                 return False;
426
427         if (!prs_uint32("ptr", ps, depth, &r_u->ptr))
428                 return False;
429
430         if (r_u->ptr != 0) {
431                 if (!sec_io_desc_buf("sec", &r_u->buf, ps, depth))
432                         return False;
433         }
434
435         if (!prs_ntstatus("status", ps, depth, &r_u->status))
436                 return False;
437
438         return True;
439 }
440
441 /*******************************************************************
442  Inits an LSA_Q_QUERY_INFO structure.
443 ********************************************************************/
444
445 void init_q_query(LSA_Q_QUERY_INFO *q_q, POLICY_HND *hnd, uint16 info_class)
446 {
447         DEBUG(5, ("init_q_query\n"));
448
449         memcpy(&q_q->pol, hnd, sizeof(q_q->pol));
450
451         q_q->info_class = info_class;
452 }
453
454 /*******************************************************************
455  Reads or writes an LSA_Q_QUERY_INFO structure.
456 ********************************************************************/
457
458 BOOL lsa_io_q_query(const char *desc, LSA_Q_QUERY_INFO *q_q, prs_struct *ps, 
459                     int depth)
460 {
461         prs_debug(ps, depth, desc, "lsa_io_q_query");
462         depth++;
463
464         if(!smb_io_pol_hnd("", &q_q->pol, ps, depth))
465                 return False;
466
467         if(!prs_uint16("info_class", ps, depth, &q_q->info_class))
468                 return False;
469
470         return True;
471 }
472
473 /*******************************************************************
474 makes an LSA_Q_ENUM_TRUST_DOM structure.
475 ********************************************************************/
476 BOOL init_q_enum_trust_dom(LSA_Q_ENUM_TRUST_DOM * q_e, POLICY_HND *pol,
477                            uint32 enum_context, uint32 preferred_len)
478 {
479         DEBUG(5, ("init_q_enum_trust_dom\n"));
480
481         q_e->pol = *pol;
482         q_e->enum_context = enum_context;
483         q_e->preferred_len = preferred_len;
484
485         return True;
486 }
487
488 /*******************************************************************
489  Reads or writes an LSA_Q_ENUM_TRUST_DOM structure.
490 ********************************************************************/
491
492 BOOL lsa_io_q_enum_trust_dom(const char *desc, LSA_Q_ENUM_TRUST_DOM *q_e, 
493                              prs_struct *ps, int depth)
494 {
495         prs_debug(ps, depth, desc, "lsa_io_q_enum_trust_dom");
496         depth++;
497
498         if(!smb_io_pol_hnd("", &q_e->pol, ps, depth))
499                 return False;
500
501         if(!prs_uint32("enum_context ", ps, depth, &q_e->enum_context))
502                 return False;
503         if(!prs_uint32("preferred_len", ps, depth, &q_e->preferred_len))
504                 return False;
505
506         return True;
507 }
508
509 /*******************************************************************
510  Inits an LSA_R_ENUM_TRUST_DOM structure.
511 ********************************************************************/
512
513 void init_r_enum_trust_dom(TALLOC_CTX *ctx, LSA_R_ENUM_TRUST_DOM *r_e, uint32 enum_context,
514                            uint32 req_num_domains, uint32 num_domains, TRUSTDOM **td)
515 {
516         unsigned int i;
517
518         DEBUG(5, ("init_r_enum_trust_dom\n"));
519         
520         r_e->enum_context  = enum_context;
521         r_e->count         = num_domains;
522                         
523         if ( num_domains != 0 ) {
524         
525                 /* allocate container memory */
526                 
527                 r_e->domlist = TALLOC_P( ctx, DOMAIN_LIST );
528                 r_e->domlist->domains = TALLOC_ARRAY( ctx, DOMAIN_INFO, r_e->count );
529                 
530                 if ( !r_e->domlist || !r_e->domlist->domains ) {
531                         r_e->status = NT_STATUS_NO_MEMORY;
532                         return;
533                 }
534                 
535                 r_e->domlist->count = r_e->count;
536                 
537                 /* initialize the list of domains and their sid */
538                 
539                 for (i = 0; i < num_domains; i++) {     
540                         if ( !(r_e->domlist->domains[i].sid = TALLOC_P(ctx, DOM_SID2)) ) {
541                                 r_e->status = NT_STATUS_NO_MEMORY;
542                                 return;
543                         }
544                                 
545                         init_dom_sid2(r_e->domlist->domains[i].sid, &(td[i])->sid);
546                         init_unistr4_w(ctx, &r_e->domlist->domains[i].name, (td[i])->name);     
547                 }
548         }
549
550 }
551
552 /*******************************************************************
553 ********************************************************************/
554
555 BOOL lsa_io_domain_list( const char *desc, prs_struct *ps, int depth, DOMAIN_LIST *domlist )
556 {
557         int i;
558         
559         prs_debug(ps, depth, desc, "lsa_io_domain_list");
560         depth++;
561
562         if(!prs_uint32("count", ps, depth, &domlist->count))
563                 return False;
564
565         if ( domlist->count == 0 )
566                 return True;
567                 
568         if ( UNMARSHALLING(ps) ) {
569                 if ( !(domlist->domains = PRS_ALLOC_MEM( ps, DOMAIN_INFO, domlist->count )) )
570                         return False;
571         }
572         
573         /* headers */
574         
575         for ( i=0; i<domlist->count; i++ ) {
576                 if ( !prs_unistr4_hdr("name_header", ps, depth, &domlist->domains[i].name) )
577                         return False;
578                 if ( !smb_io_dom_sid2_p("sid_header", ps, depth, &domlist->domains[i].sid) )
579                         return False;
580         }
581
582         /* data */
583         
584         for ( i=0; i<domlist->count; i++ ) {
585                 if ( !prs_unistr4_str("name", ps, depth, &domlist->domains[i].name) )
586                         return False;
587                 if( !smb_io_dom_sid2("sid", domlist->domains[i].sid, ps, depth) )
588                         return False;
589         }
590         
591         return True;
592 }
593
594 /*******************************************************************
595  Reads or writes an LSA_R_ENUM_TRUST_DOM structure.
596 ********************************************************************/
597
598 BOOL lsa_io_r_enum_trust_dom(const char *desc, LSA_R_ENUM_TRUST_DOM *r_e, 
599                              prs_struct *ps, int depth)
600 {
601         prs_debug(ps, depth, desc, "lsa_io_r_enum_trust_dom");
602         depth++;
603
604         if(!prs_uint32("enum_context", ps, depth, &r_e->enum_context))
605                 return False;
606
607         if(!prs_uint32("count", ps, depth, &r_e->count))
608                 return False;
609
610         if ( !prs_pointer("trusted_domains", ps, depth, (void**)&r_e->domlist, sizeof(DOMAIN_LIST), (PRS_POINTER_CAST)lsa_io_domain_list))
611                 return False;
612                 
613         if(!prs_ntstatus("status", ps, depth, &r_e->status))
614                 return False;
615
616         return True;
617 }
618
619 /*******************************************************************
620 reads or writes a dom query structure.
621 ********************************************************************/
622
623 static BOOL lsa_io_dom_query(const char *desc, DOM_QUERY *d_q, prs_struct *ps, int depth)
624 {
625         if (d_q == NULL)
626                 return False;
627
628         prs_debug(ps, depth, desc, "lsa_io_dom_query");
629         depth++;
630
631         if(!prs_align(ps))
632                 return False;
633
634         if(!prs_uint16("uni_dom_max_len", ps, depth, &d_q->uni_dom_max_len)) /* domain name string length * 2 */
635                 return False;
636         if(!prs_uint16("uni_dom_str_len", ps, depth, &d_q->uni_dom_str_len)) /* domain name string length * 2 */
637                 return False;
638
639         if(!prs_uint32("buffer_dom_name", ps, depth, &d_q->buffer_dom_name)) /* undocumented domain name string buffer pointer */
640                 return False;
641         if(!prs_uint32("buffer_dom_sid ", ps, depth, &d_q->buffer_dom_sid)) /* undocumented domain SID string buffer pointer */
642                 return False;
643
644         if(!smb_io_unistr2("unistr2", &d_q->uni_domain_name, d_q->buffer_dom_name, ps, depth)) /* domain name (unicode string) */
645                 return False;
646
647         if(!prs_align(ps))
648                 return False;
649
650         if (d_q->buffer_dom_sid != 0) {
651                 if(!smb_io_dom_sid2("", &d_q->dom_sid, ps, depth)) /* domain SID */
652                         return False;
653         } else {
654                 memset((char *)&d_q->dom_sid, '\0', sizeof(d_q->dom_sid));
655         }
656
657         return True;
658 }
659
660 /*******************************************************************
661 reads or writes a structure.
662 ********************************************************************/
663
664 static BOOL lsa_io_dom_query_2(const char *desc, DOM_QUERY_2 *d_q, prs_struct *ps, int depth)
665 {
666         uint32 ptr = 1;
667
668         if (d_q == NULL)
669                 return False;
670
671         prs_debug(ps, depth, desc, "lsa_io_dom_query_2");
672         depth++;
673
674         if (!prs_align(ps))
675                 return False;
676
677         if (!prs_uint32("auditing_enabled", ps, depth, &d_q->auditing_enabled))
678                 return False;
679         if (!prs_uint32("ptr   ", ps, depth, &ptr))
680                 return False;
681         if (!prs_uint32("count1", ps, depth, &d_q->count1))
682                 return False;
683         if (!prs_uint32("count2", ps, depth, &d_q->count2))
684                 return False;
685
686         if (UNMARSHALLING(ps)) {
687                 d_q->auditsettings = TALLOC_ZERO_ARRAY(ps->mem_ctx, uint32, d_q->count2);
688         }
689
690         if (d_q->auditsettings == NULL) {
691                 DEBUG(1, ("lsa_io_dom_query_2: NULL auditsettings!\n"));
692                 return False;
693         }
694
695         if (!prs_uint32s(False, "auditsettings", ps, depth, d_q->auditsettings, d_q->count2))
696                 return False;
697
698     return True;
699 }
700
701 /*******************************************************************
702  Reads or writes a dom query structure.
703 ********************************************************************/
704
705 static BOOL lsa_io_dom_query_3(const char *desc, DOM_QUERY_3 *d_q, prs_struct *ps, int depth)
706 {
707         return lsa_io_dom_query("", d_q, ps, depth);
708 }
709
710 /*******************************************************************
711  Reads or writes a dom query structure.
712 ********************************************************************/
713
714 static BOOL lsa_io_dom_query_5(const char *desc, DOM_QUERY_5 *d_q, prs_struct *ps, int depth)
715 {
716         return lsa_io_dom_query("", d_q, ps, depth);
717 }
718
719 /*******************************************************************
720  Reads or writes a dom query structure.
721 ********************************************************************/
722
723 static BOOL lsa_io_dom_query_6(const char *desc, DOM_QUERY_6 *d_q, prs_struct *ps, int depth)
724 {
725         if (d_q == NULL)
726                 return False;
727
728         prs_debug(ps, depth, desc, "lsa_io_dom_query_6");
729         depth++;
730
731         if (!prs_uint16("server_role", ps, depth, &d_q->server_role))
732                 return False;
733
734         return True;
735 }
736
737 /*******************************************************************
738  Reads or writes an LSA_R_QUERY_INFO structure.
739 ********************************************************************/
740
741 BOOL lsa_io_r_query(const char *desc, LSA_R_QUERY_INFO *r_q, prs_struct *ps,
742                     int depth)
743 {
744         prs_debug(ps, depth, desc, "lsa_io_r_query");
745         depth++;
746
747         if(!prs_uint32("undoc_buffer", ps, depth, &r_q->undoc_buffer))
748                 return False;
749
750         if (r_q->undoc_buffer != 0) {
751                 if(!prs_uint16("info_class", ps, depth, &r_q->info_class))
752                         return False;
753
754                 if(!prs_align(ps))
755                         return False;
756
757                 switch (r_q->info_class) {
758                 case 2:
759                         if(!lsa_io_dom_query_2("", &r_q->dom.id2, ps, depth))
760                                 return False;
761                         break;
762                 case 3:
763                         if(!lsa_io_dom_query_3("", &r_q->dom.id3, ps, depth))
764                                 return False;
765                         break;
766                 case 5:
767                         if(!lsa_io_dom_query_5("", &r_q->dom.id5, ps, depth))
768                                 return False;
769                         break;
770                 case 6:
771                         if(!lsa_io_dom_query_6("", &r_q->dom.id6, ps, depth))
772                                 return False;
773                         break;
774                 default:
775                         /* PANIC! */
776                         break;
777                 }
778         }
779
780         if(!prs_align(ps))
781                 return False;
782
783         if(!prs_ntstatus("status", ps, depth, &r_q->status))
784                 return False;
785
786         return True;
787 }
788
789 /*******************************************************************
790  Inits a LSA_SID_ENUM structure.
791 ********************************************************************/
792
793 static void init_lsa_sid_enum(TALLOC_CTX *mem_ctx, LSA_SID_ENUM *sen, 
794                        int num_entries, const DOM_SID *sids)
795 {
796         int i;
797
798         DEBUG(5, ("init_lsa_sid_enum\n"));
799
800         sen->num_entries  = num_entries;
801         sen->ptr_sid_enum = (num_entries != 0);
802         sen->num_entries2 = num_entries;
803
804         /* Allocate memory for sids and sid pointers */
805
806         if (num_entries == 0) return;
807
808         if ((sen->ptr_sid = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_entries )) == NULL) {
809                 DEBUG(3, ("init_lsa_sid_enum(): out of memory for ptr_sid\n"));
810                 return;
811         }
812
813         if ((sen->sid = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID2, num_entries)) == NULL) {
814                 DEBUG(3, ("init_lsa_sid_enum(): out of memory for sids\n"));
815                 return;
816         }
817
818         /* Copy across SIDs and SID pointers */
819
820         for (i = 0; i < num_entries; i++) {
821                 sen->ptr_sid[i] = 1;
822                 init_dom_sid2(&sen->sid[i], &sids[i]);
823         }
824 }
825
826 /*******************************************************************
827  Reads or writes a LSA_SID_ENUM structure.
828 ********************************************************************/
829
830 static BOOL lsa_io_sid_enum(const char *desc, LSA_SID_ENUM *sen, prs_struct *ps, 
831                             int depth)
832 {
833         unsigned int i;
834
835         prs_debug(ps, depth, desc, "lsa_io_sid_enum");
836         depth++;
837
838         if(!prs_align(ps))
839                 return False;
840         
841         if(!prs_uint32("num_entries ", ps, depth, &sen->num_entries))
842                 return False;
843         if(!prs_uint32("ptr_sid_enum", ps, depth, &sen->ptr_sid_enum))
844                 return False;
845
846         /*
847            if the ptr is NULL, leave here. checked from a real w2k trace.
848            JFM, 11/23/2001
849          */
850         
851         if (sen->ptr_sid_enum==0)
852                 return True;
853
854         if(!prs_uint32("num_entries2", ps, depth, &sen->num_entries2))
855                 return False;
856
857         /* Mallocate memory if we're unpacking from the wire */
858
859         if (UNMARSHALLING(ps)) {
860                 if ((sen->ptr_sid = PRS_ALLOC_MEM( ps, uint32, sen->num_entries)) == NULL) {
861                         DEBUG(3, ("init_lsa_sid_enum(): out of memory for "
862                                   "ptr_sid\n"));
863                         return False;
864                 }
865
866                 if ((sen->sid = PRS_ALLOC_MEM( ps, DOM_SID2, sen->num_entries)) == NULL) {
867                         DEBUG(3, ("init_lsa_sid_enum(): out of memory for "
868                                   "sids\n"));
869                         return False;
870                 }
871         }
872
873         for (i = 0; i < sen->num_entries; i++) {        
874                 fstring temp;
875
876                 slprintf(temp, sizeof(temp) - 1, "ptr_sid[%d]", i);
877                 if(!prs_uint32(temp, ps, depth, &sen->ptr_sid[i])) {
878                         return False;
879                 }
880         }
881
882         for (i = 0; i < sen->num_entries; i++) {
883                 fstring temp;
884
885                 slprintf(temp, sizeof(temp) - 1, "sid[%d]", i);
886                 if(!smb_io_dom_sid2(temp, &sen->sid[i], ps, depth)) {
887                         return False;
888                 }
889         }
890
891         return True;
892 }
893
894 /*******************************************************************
895  Inits an LSA_R_ENUM_TRUST_DOM structure.
896 ********************************************************************/
897
898 void init_q_lookup_sids(TALLOC_CTX *mem_ctx, LSA_Q_LOOKUP_SIDS *q_l, 
899                         POLICY_HND *hnd, int num_sids, const DOM_SID *sids,
900                         uint16 level)
901 {
902         DEBUG(5, ("init_q_lookup_sids\n"));
903
904         ZERO_STRUCTP(q_l);
905
906         memcpy(&q_l->pol, hnd, sizeof(q_l->pol));
907         init_lsa_sid_enum(mem_ctx, &q_l->sids, num_sids, sids);
908         
909         q_l->level = level;
910 }
911
912 /*******************************************************************
913  Reads or writes a LSA_Q_LOOKUP_SIDS structure.
914 ********************************************************************/
915
916 BOOL lsa_io_q_lookup_sids(const char *desc, LSA_Q_LOOKUP_SIDS *q_s, prs_struct *ps,
917                           int depth)
918 {
919         prs_debug(ps, depth, desc, "lsa_io_q_lookup_sids");
920         depth++;
921
922         if(!prs_align(ps))
923                 return False;
924         
925         if(!smb_io_pol_hnd("pol_hnd", &q_s->pol, ps, depth)) /* policy handle */
926                 return False;
927         if(!lsa_io_sid_enum("sids   ", &q_s->sids, ps, depth)) /* sids to be looked up */
928                 return False;
929         if(!lsa_io_trans_names("names  ", &q_s->names, ps, depth)) /* translated names */
930                 return False;
931
932         if(!prs_uint16("level", ps, depth, &q_s->level)) /* lookup level */
933                 return False;
934         if(!prs_align(ps))
935                 return False;
936
937         if(!prs_uint32("mapped_count", ps, depth, &q_s->mapped_count))
938                 return False;
939
940         return True;
941 }
942
943 /*******************************************************************
944  Reads or writes a structure.
945 ********************************************************************/
946
947 static BOOL lsa_io_trans_names(const char *desc, LSA_TRANS_NAME_ENUM *trn,
948                 prs_struct *ps, int depth)
949 {
950         unsigned int i;
951
952         prs_debug(ps, depth, desc, "lsa_io_trans_names");
953         depth++;
954
955         if(!prs_align(ps))
956                 return False;
957    
958         if(!prs_uint32("num_entries    ", ps, depth, &trn->num_entries))
959                 return False;
960         if(!prs_uint32("ptr_trans_names", ps, depth, &trn->ptr_trans_names))
961                 return False;
962
963         if (trn->ptr_trans_names != 0) {
964                 if(!prs_uint32("num_entries2   ", ps, depth, 
965                                &trn->num_entries2))
966                         return False;
967
968                 if (UNMARSHALLING(ps)) {
969                         if ((trn->name = PRS_ALLOC_MEM(ps, LSA_TRANS_NAME, trn->num_entries)) == NULL) {
970                                 return False;
971                         }
972
973                         if ((trn->uni_name = PRS_ALLOC_MEM(ps, UNISTR2, trn->num_entries)) == NULL) {
974                                 return False;
975                         }
976                 }
977
978                 for (i = 0; i < trn->num_entries2; i++) {
979                         fstring t;
980                         slprintf(t, sizeof(t) - 1, "name[%d] ", i);
981
982                         if(!lsa_io_trans_name(t, &trn->name[i], ps, depth)) /* translated name */
983                                 return False;
984                 }
985
986                 for (i = 0; i < trn->num_entries2; i++) {
987                         fstring t;
988                         slprintf(t, sizeof(t) - 1, "name[%d] ", i);
989
990                         if(!smb_io_unistr2(t, &trn->uni_name[i], trn->name[i].hdr_name.buffer, ps, depth))
991                                 return False;
992                         if(!prs_align(ps))
993                                 return False;
994                 }
995         }
996
997         return True;
998 }
999
1000 /*******************************************************************
1001  Reads or writes a structure.
1002 ********************************************************************/
1003
1004 BOOL lsa_io_r_lookup_sids(const char *desc, LSA_R_LOOKUP_SIDS *r_s, 
1005                           prs_struct *ps, int depth)
1006 {
1007         prs_debug(ps, depth, desc, "lsa_io_r_lookup_sids");
1008         depth++;
1009
1010         if(!prs_align(ps))
1011                 return False;
1012         
1013         if(!prs_uint32("ptr_dom_ref", ps, depth, &r_s->ptr_dom_ref))
1014                 return False;
1015
1016         if (r_s->ptr_dom_ref != 0)
1017                 if(!lsa_io_dom_r_ref ("dom_ref", r_s->dom_ref, ps, depth)) /* domain reference info */
1018                         return False;
1019
1020         if(!lsa_io_trans_names("names  ", r_s->names, ps, depth)) /* translated names */
1021                 return False;
1022
1023         if(!prs_align(ps))
1024                 return False;
1025
1026         if(!prs_uint32("mapped_count", ps, depth, &r_s->mapped_count))
1027                 return False;
1028
1029         if(!prs_ntstatus("status      ", ps, depth, &r_s->status))
1030                 return False;
1031
1032         return True;
1033 }
1034
1035 /*******************************************************************
1036 makes a structure.
1037 ********************************************************************/
1038
1039 void init_q_lookup_names(TALLOC_CTX *mem_ctx, LSA_Q_LOOKUP_NAMES *q_l, 
1040                          POLICY_HND *hnd, int num_names, const char **names)
1041 {
1042         unsigned int i;
1043
1044         DEBUG(5, ("init_q_lookup_names\n"));
1045
1046         ZERO_STRUCTP(q_l);
1047
1048         q_l->pol = *hnd;
1049         q_l->num_entries = num_names;
1050         q_l->num_entries2 = num_names;
1051         q_l->lookup_level = 1;
1052
1053         if ((q_l->uni_name = TALLOC_ZERO_ARRAY(mem_ctx, UNISTR2, num_names)) == NULL) {
1054                 DEBUG(3, ("init_q_lookup_names(): out of memory\n"));
1055                 return;
1056         }
1057
1058         if ((q_l->hdr_name = TALLOC_ZERO_ARRAY(mem_ctx, UNIHDR, num_names)) == NULL) {
1059                 DEBUG(3, ("init_q_lookup_names(): out of memory\n"));
1060                 return;
1061         }
1062
1063         for (i = 0; i < num_names; i++) {
1064                 init_unistr2(&q_l->uni_name[i], names[i], UNI_FLAGS_NONE);
1065                 init_uni_hdr(&q_l->hdr_name[i], &q_l->uni_name[i]);
1066         }
1067 }
1068
1069 /*******************************************************************
1070 reads or writes a structure.
1071 ********************************************************************/
1072
1073 BOOL lsa_io_q_lookup_names(const char *desc, LSA_Q_LOOKUP_NAMES *q_r, 
1074                            prs_struct *ps, int depth)
1075 {
1076         unsigned int i;
1077
1078         prs_debug(ps, depth, desc, "lsa_io_q_lookup_names");
1079         depth++;
1080
1081         if(!prs_align(ps))
1082                 return False;
1083
1084         if(!smb_io_pol_hnd("", &q_r->pol, ps, depth)) /* policy handle */
1085                 return False;
1086
1087         if(!prs_align(ps))
1088                 return False;
1089         if(!prs_uint32("num_entries    ", ps, depth, &q_r->num_entries))
1090                 return False;
1091         if(!prs_uint32("num_entries2   ", ps, depth, &q_r->num_entries2))
1092                 return False;
1093
1094         if (UNMARSHALLING(ps)) {
1095                 if (q_r->num_entries) {
1096                         if ((q_r->hdr_name = PRS_ALLOC_MEM(ps, UNIHDR, q_r->num_entries)) == NULL)
1097                                 return False;
1098                         if ((q_r->uni_name = PRS_ALLOC_MEM(ps, UNISTR2, q_r->num_entries)) == NULL)
1099                                 return False;
1100                 }
1101         }
1102
1103         for (i = 0; i < q_r->num_entries; i++) {
1104                 if(!prs_align(ps))
1105                         return False;
1106                 if(!smb_io_unihdr("hdr_name", &q_r->hdr_name[i], ps, depth)) /* pointer names */
1107                         return False;
1108         }
1109
1110         for (i = 0; i < q_r->num_entries; i++) {
1111                 if(!prs_align(ps))
1112                         return False;
1113                 if(!smb_io_unistr2("dom_name", &q_r->uni_name[i], q_r->hdr_name[i].buffer, ps, depth)) /* names to be looked up */
1114                         return False;
1115         }
1116
1117         if(!prs_align(ps))
1118                 return False;
1119         if(!prs_uint32("num_trans_entries ", ps, depth, &q_r->num_trans_entries))
1120                 return False;
1121         if(!prs_uint32("ptr_trans_sids ", ps, depth, &q_r->ptr_trans_sids))
1122                 return False;
1123         if(!prs_uint32("lookup_level   ", ps, depth, &q_r->lookup_level))
1124                 return False;
1125         if(!prs_uint32("mapped_count   ", ps, depth, &q_r->mapped_count))
1126                 return False;
1127
1128         return True;
1129 }
1130
1131 /*******************************************************************
1132 reads or writes a structure.
1133 ********************************************************************/
1134
1135 BOOL lsa_io_r_lookup_names(const char *desc, LSA_R_LOOKUP_NAMES *r_r, 
1136                            prs_struct *ps, int depth)
1137 {
1138         unsigned int i;
1139
1140         prs_debug(ps, depth, desc, "lsa_io_r_lookup_names");
1141         depth++;
1142
1143         if(!prs_align(ps))
1144                 return False;
1145
1146         if(!prs_uint32("ptr_dom_ref", ps, depth, &r_r->ptr_dom_ref))
1147                 return False;
1148
1149         if (r_r->ptr_dom_ref != 0)
1150                 if(!lsa_io_dom_r_ref("", r_r->dom_ref, ps, depth))
1151                         return False;
1152
1153         if(!prs_uint32("num_entries", ps, depth, &r_r->num_entries))
1154                 return False;
1155         if(!prs_uint32("ptr_entries", ps, depth, &r_r->ptr_entries))
1156                 return False;
1157
1158         if (r_r->ptr_entries != 0) {
1159                 if(!prs_uint32("num_entries2", ps, depth, &r_r->num_entries2))
1160                         return False;
1161
1162                 if (r_r->num_entries2 != r_r->num_entries) {
1163                         /* RPC fault */
1164                         return False;
1165                 }
1166
1167                 if (UNMARSHALLING(ps)) {
1168                         if ((r_r->dom_rid = PRS_ALLOC_MEM(ps, DOM_RID2, r_r->num_entries2))
1169                             == NULL) {
1170                                 DEBUG(3, ("lsa_io_r_lookup_names(): out of memory\n"));
1171                                 return False;
1172                         }
1173                 }
1174
1175                 for (i = 0; i < r_r->num_entries2; i++)
1176                         if(!smb_io_dom_rid2("", &r_r->dom_rid[i], ps, depth)) /* domain RIDs being looked up */
1177                                 return False;
1178         }
1179
1180         if(!prs_uint32("mapped_count", ps, depth, &r_r->mapped_count))
1181                 return False;
1182
1183         if(!prs_ntstatus("status      ", ps, depth, &r_r->status))
1184                 return False;
1185
1186         return True;
1187 }
1188
1189
1190 /*******************************************************************
1191  Inits an LSA_Q_CLOSE structure.
1192 ********************************************************************/
1193
1194 void init_lsa_q_close(LSA_Q_CLOSE *q_c, POLICY_HND *hnd)
1195 {
1196         DEBUG(5, ("init_lsa_q_close\n"));
1197
1198         memcpy(&q_c->pol, hnd, sizeof(q_c->pol));
1199 }
1200
1201 /*******************************************************************
1202  Reads or writes an LSA_Q_CLOSE structure.
1203 ********************************************************************/
1204
1205 BOOL lsa_io_q_close(const char *desc, LSA_Q_CLOSE *q_c, prs_struct *ps, int depth)
1206 {
1207         prs_debug(ps, depth, desc, "lsa_io_q_close");
1208         depth++;
1209
1210         if(!smb_io_pol_hnd("", &q_c->pol, ps, depth))
1211                 return False;
1212
1213         return True;
1214 }
1215
1216 /*******************************************************************
1217  Reads or writes an LSA_R_CLOSE structure.
1218 ********************************************************************/
1219
1220 BOOL lsa_io_r_close(const char *desc,  LSA_R_CLOSE *r_c, prs_struct *ps, int depth)
1221 {
1222         prs_debug(ps, depth, desc, "lsa_io_r_close");
1223         depth++;
1224
1225         if(!smb_io_pol_hnd("", &r_c->pol, ps, depth))
1226                 return False;
1227
1228         if(!prs_ntstatus("status", ps, depth, &r_c->status))
1229                 return False;
1230
1231         return True;
1232 }
1233
1234 /*******************************************************************
1235  Reads or writes an LSA_Q_OPEN_SECRET structure.
1236 ********************************************************************/
1237
1238 BOOL lsa_io_q_open_secret(const char *desc, LSA_Q_OPEN_SECRET *q_c, prs_struct *ps, int depth)
1239 {
1240         prs_debug(ps, depth, desc, "lsa_io_q_open_secret");
1241         depth++;
1242
1243         /* Don't bother to read or write at present... */
1244         return True;
1245 }
1246
1247 /*******************************************************************
1248  Reads or writes an LSA_R_OPEN_SECRET structure.
1249 ********************************************************************/
1250
1251 BOOL lsa_io_r_open_secret(const char *desc, LSA_R_OPEN_SECRET *r_c, prs_struct *ps, int depth)
1252 {
1253         prs_debug(ps, depth, desc, "lsa_io_r_open_secret");
1254         depth++;
1255
1256         if(!prs_align(ps))
1257                 return False;
1258    
1259         if(!prs_uint32("dummy1", ps, depth, &r_c->dummy1))
1260                 return False;
1261         if(!prs_uint32("dummy2", ps, depth, &r_c->dummy2))
1262                 return False;
1263         if(!prs_uint32("dummy3", ps, depth, &r_c->dummy3))
1264                 return False;
1265         if(!prs_uint32("dummy4", ps, depth, &r_c->dummy4))
1266                 return False;
1267         if(!prs_ntstatus("status", ps, depth, &r_c->status))
1268                 return False;
1269
1270         return True;
1271 }
1272
1273 /*******************************************************************
1274  Inits an LSA_Q_ENUM_PRIVS structure.
1275 ********************************************************************/
1276
1277 void init_q_enum_privs(LSA_Q_ENUM_PRIVS *q_q, POLICY_HND *hnd, uint32 enum_context, uint32 pref_max_length)
1278 {
1279         DEBUG(5, ("init_q_enum_privs\n"));
1280
1281         memcpy(&q_q->pol, hnd, sizeof(q_q->pol));
1282
1283         q_q->enum_context = enum_context;
1284         q_q->pref_max_length = pref_max_length;
1285 }
1286
1287 /*******************************************************************
1288 reads or writes a structure.
1289 ********************************************************************/
1290 BOOL lsa_io_q_enum_privs(const char *desc, LSA_Q_ENUM_PRIVS *q_q, prs_struct *ps, int depth)
1291 {
1292         if (q_q == NULL)
1293                 return False;
1294
1295         prs_debug(ps, depth, desc, "lsa_io_q_enum_privs");
1296         depth++;
1297
1298         if (!smb_io_pol_hnd("", &q_q->pol, ps, depth))
1299                 return False;
1300
1301         if(!prs_uint32("enum_context   ", ps, depth, &q_q->enum_context))
1302                 return False;
1303         if(!prs_uint32("pref_max_length", ps, depth, &q_q->pref_max_length))
1304                 return False;
1305
1306         return True;
1307 }
1308
1309 /*******************************************************************
1310 reads or writes a structure.
1311 ********************************************************************/
1312 static BOOL lsa_io_priv_entries(const char *desc, LSA_PRIV_ENTRY *entries, uint32 count, prs_struct *ps, int depth)
1313 {
1314         uint32 i;
1315
1316         if (entries == NULL)
1317                 return False;
1318
1319         prs_debug(ps, depth, desc, "lsa_io_priv_entries");
1320         depth++;
1321
1322         if(!prs_align(ps))
1323                 return False;
1324
1325         for (i = 0; i < count; i++) {
1326                 if (!smb_io_unihdr("", &entries[i].hdr_name, ps, depth))
1327                         return False;
1328                 if(!prs_uint32("luid_low ", ps, depth, &entries[i].luid_low))
1329                         return False;
1330                 if(!prs_uint32("luid_high", ps, depth, &entries[i].luid_high))
1331                         return False;
1332         }
1333
1334         for (i = 0; i < count; i++)
1335                 if (!smb_io_unistr2("", &entries[i].name, entries[i].hdr_name.buffer, ps, depth))
1336                         return False;
1337
1338         return True;
1339 }
1340
1341 /*******************************************************************
1342  Inits an LSA_R_ENUM_PRIVS structure.
1343 ********************************************************************/
1344
1345 void init_lsa_r_enum_privs(LSA_R_ENUM_PRIVS *r_u, uint32 enum_context,
1346                           uint32 count, LSA_PRIV_ENTRY *entries)
1347 {
1348         DEBUG(5, ("init_lsa_r_enum_privs\n"));
1349
1350         r_u->enum_context=enum_context;
1351         r_u->count=count;
1352         
1353         if (entries!=NULL) {
1354                 r_u->ptr=1;
1355                 r_u->count1=count;
1356                 r_u->privs=entries;
1357         } else {
1358                 r_u->ptr=0;
1359                 r_u->count1=0;
1360                 r_u->privs=NULL;
1361         }               
1362 }
1363
1364 /*******************************************************************
1365 reads or writes a structure.
1366 ********************************************************************/
1367 BOOL lsa_io_r_enum_privs(const char *desc, LSA_R_ENUM_PRIVS *r_q, prs_struct *ps, int depth)
1368 {
1369         if (r_q == NULL)
1370                 return False;
1371
1372         prs_debug(ps, depth, desc, "lsa_io_r_enum_privs");
1373         depth++;
1374
1375         if(!prs_align(ps))
1376                 return False;
1377
1378         if(!prs_uint32("enum_context", ps, depth, &r_q->enum_context))
1379                 return False;
1380         if(!prs_uint32("count", ps, depth, &r_q->count))
1381                 return False;
1382         if(!prs_uint32("ptr", ps, depth, &r_q->ptr))
1383                 return False;
1384
1385         if (r_q->ptr) {
1386                 if(!prs_uint32("count1", ps, depth, &r_q->count1))
1387                         return False;
1388
1389                 if (UNMARSHALLING(ps))
1390                         if (!(r_q->privs = PRS_ALLOC_MEM(ps, LSA_PRIV_ENTRY, r_q->count1)))
1391                                 return False;
1392
1393                 if (!lsa_io_priv_entries("", r_q->privs, r_q->count1, ps, depth))
1394                         return False;
1395         }
1396
1397         if(!prs_align(ps))
1398                 return False;
1399
1400         if(!prs_ntstatus("status", ps, depth, &r_q->status))
1401                 return False;
1402
1403         return True;
1404 }
1405
1406 void init_lsa_priv_get_dispname(LSA_Q_PRIV_GET_DISPNAME *trn, POLICY_HND *hnd, const char *name, uint16 lang_id, uint16 lang_id_sys)
1407 {
1408         memcpy(&trn->pol, hnd, sizeof(trn->pol));
1409
1410         init_unistr2(&trn->name, name, UNI_FLAGS_NONE);
1411         init_uni_hdr(&trn->hdr_name, &trn->name);
1412         trn->lang_id = lang_id;
1413         trn->lang_id_sys = lang_id_sys;
1414 }
1415
1416 /*******************************************************************
1417 reads or writes a structure.
1418 ********************************************************************/
1419 BOOL lsa_io_q_priv_get_dispname(const char *desc, LSA_Q_PRIV_GET_DISPNAME *q_q, prs_struct *ps, int depth)
1420 {
1421         if (q_q == NULL)
1422                 return False;
1423
1424         prs_debug(ps, depth, desc, "lsa_io_q_priv_get_dispname");
1425         depth++;
1426
1427         if(!prs_align(ps))
1428                 return False;
1429
1430         if (!smb_io_pol_hnd("", &q_q->pol, ps, depth))
1431                 return False;
1432
1433         if (!smb_io_unihdr("hdr_name", &q_q->hdr_name, ps, depth))
1434                 return False;
1435
1436         if (!smb_io_unistr2("name", &q_q->name, q_q->hdr_name.buffer, ps, depth))
1437                 return False;
1438
1439         if(!prs_uint16("lang_id    ", ps, depth, &q_q->lang_id))
1440                 return False;
1441         if(!prs_uint16("lang_id_sys", ps, depth, &q_q->lang_id_sys))
1442                 return False;
1443
1444         return True;
1445 }
1446
1447 /*******************************************************************
1448 reads or writes a structure.
1449 ********************************************************************/
1450 BOOL lsa_io_r_priv_get_dispname(const char *desc, LSA_R_PRIV_GET_DISPNAME *r_q, prs_struct *ps, int depth)
1451 {
1452         if (r_q == NULL)
1453                 return False;
1454
1455         prs_debug(ps, depth, desc, "lsa_io_r_priv_get_dispname");
1456         depth++;
1457
1458         if (!prs_align(ps))
1459                 return False;
1460
1461         if (!prs_uint32("ptr_info", ps, depth, &r_q->ptr_info))
1462                 return False;
1463
1464         if (r_q->ptr_info){
1465                 if (!smb_io_unihdr("hdr_name", &r_q->hdr_desc, ps, depth))
1466                         return False;
1467
1468                 if (!smb_io_unistr2("desc", &r_q->desc, r_q->hdr_desc.buffer, ps, depth))
1469                         return False;
1470         }
1471 /*
1472         if(!prs_align(ps))
1473                 return False;
1474 */
1475         if(!prs_uint16("lang_id", ps, depth, &r_q->lang_id))
1476                 return False;
1477
1478         if(!prs_align(ps))
1479                 return False;
1480         if(!prs_ntstatus("status", ps, depth, &r_q->status))
1481                 return False;
1482
1483         return True;
1484 }
1485
1486 /*
1487   initialise a LSA_Q_ENUM_ACCOUNTS structure
1488 */
1489 void init_lsa_q_enum_accounts(LSA_Q_ENUM_ACCOUNTS *trn, POLICY_HND *hnd, uint32 enum_context, uint32 pref_max_length)
1490 {
1491         memcpy(&trn->pol, hnd, sizeof(trn->pol));
1492
1493         trn->enum_context = enum_context;
1494         trn->pref_max_length = pref_max_length;
1495 }
1496
1497 /*******************************************************************
1498 reads or writes a structure.
1499 ********************************************************************/
1500 BOOL lsa_io_q_enum_accounts(const char *desc, LSA_Q_ENUM_ACCOUNTS *q_q, prs_struct *ps, int depth)
1501 {
1502         if (q_q == NULL)
1503                 return False;
1504
1505         prs_debug(ps, depth, desc, "lsa_io_q_enum_accounts");
1506         depth++;
1507
1508         if (!smb_io_pol_hnd("", &q_q->pol, ps, depth))
1509                 return False;
1510
1511         if(!prs_uint32("enum_context   ", ps, depth, &q_q->enum_context))
1512                 return False;
1513         if(!prs_uint32("pref_max_length", ps, depth, &q_q->pref_max_length))
1514                 return False;
1515
1516         return True;
1517 }
1518
1519
1520 /*******************************************************************
1521  Inits an LSA_R_ENUM_PRIVS structure.
1522 ********************************************************************/
1523
1524 void init_lsa_r_enum_accounts(LSA_R_ENUM_ACCOUNTS *r_u, uint32 enum_context)
1525 {
1526         DEBUG(5, ("init_lsa_r_enum_accounts\n"));
1527
1528         r_u->enum_context=enum_context;
1529         if (r_u->enum_context!=0) {
1530                 r_u->sids.num_entries=enum_context;
1531                 r_u->sids.ptr_sid_enum=1;
1532                 r_u->sids.num_entries2=enum_context;
1533         } else {
1534                 r_u->sids.num_entries=0;
1535                 r_u->sids.ptr_sid_enum=0;
1536                 r_u->sids.num_entries2=0;
1537         }
1538 }
1539
1540 /*******************************************************************
1541 reads or writes a structure.
1542 ********************************************************************/
1543 BOOL lsa_io_r_enum_accounts(const char *desc, LSA_R_ENUM_ACCOUNTS *r_q, prs_struct *ps, int depth)
1544 {
1545         if (r_q == NULL)
1546                 return False;
1547
1548         prs_debug(ps, depth, desc, "lsa_io_r_enum_accounts");
1549         depth++;
1550
1551         if (!prs_align(ps))
1552                 return False;
1553
1554         if(!prs_uint32("enum_context", ps, depth, &r_q->enum_context))
1555                 return False;
1556
1557         if (!lsa_io_sid_enum("sids", &r_q->sids, ps, depth))
1558                 return False;
1559
1560         if (!prs_align(ps))
1561                 return False;
1562
1563         if(!prs_ntstatus("status", ps, depth, &r_q->status))
1564                 return False;
1565
1566         return True;
1567 }
1568
1569
1570 /*******************************************************************
1571  Reads or writes an LSA_Q_UNK_GET_CONNUSER structure.
1572 ********************************************************************/
1573
1574 BOOL lsa_io_q_unk_get_connuser(const char *desc, LSA_Q_UNK_GET_CONNUSER *q_c, prs_struct *ps, int depth)
1575 {
1576         prs_debug(ps, depth, desc, "lsa_io_q_unk_get_connuser");
1577         depth++;
1578
1579         if(!prs_align(ps))
1580                 return False;
1581    
1582         if(!prs_uint32("ptr_srvname", ps, depth, &q_c->ptr_srvname))
1583                 return False;
1584
1585         if(!smb_io_unistr2("uni2_srvname", &q_c->uni2_srvname, q_c->ptr_srvname, ps, depth)) /* server name to be looked up */
1586                 return False;
1587
1588         if (!prs_align(ps))
1589           return False;
1590
1591         if(!prs_uint32("unk1", ps, depth, &q_c->unk1))
1592                 return False;
1593         if(!prs_uint32("unk2", ps, depth, &q_c->unk2))
1594                 return False;
1595         if(!prs_uint32("unk3", ps, depth, &q_c->unk3))
1596                 return False;
1597
1598         /* Don't bother to read or write at present... */
1599         return True;
1600 }
1601
1602 /*******************************************************************
1603  Reads or writes an LSA_R_UNK_GET_CONNUSER structure.
1604 ********************************************************************/
1605
1606 BOOL lsa_io_r_unk_get_connuser(const char *desc, LSA_R_UNK_GET_CONNUSER *r_c, prs_struct *ps, int depth)
1607 {
1608         prs_debug(ps, depth, desc, "lsa_io_r_unk_get_connuser");
1609         depth++;
1610
1611         if(!prs_align(ps))
1612                 return False;
1613    
1614         if(!prs_uint32("ptr_user_name", ps, depth, &r_c->ptr_user_name))
1615                 return False;
1616         if(!smb_io_unihdr("hdr_user_name", &r_c->hdr_user_name, ps, depth))
1617                 return False;
1618         if(!smb_io_unistr2("uni2_user_name", &r_c->uni2_user_name, r_c->ptr_user_name, ps, depth))
1619                 return False;
1620
1621         if (!prs_align(ps))
1622           return False;
1623         
1624         if(!prs_uint32("unk1", ps, depth, &r_c->unk1))
1625                 return False;
1626
1627         if(!prs_uint32("ptr_dom_name", ps, depth, &r_c->ptr_dom_name))
1628                 return False;
1629         if(!smb_io_unihdr("hdr_dom_name", &r_c->hdr_dom_name, ps, depth))
1630                 return False;
1631         if(!smb_io_unistr2("uni2_dom_name", &r_c->uni2_dom_name, r_c->ptr_dom_name, ps, depth))
1632                 return False;
1633
1634         if (!prs_align(ps))
1635           return False;
1636         
1637         if(!prs_ntstatus("status", ps, depth, &r_c->status))
1638                 return False;
1639
1640         return True;
1641 }
1642
1643 void init_lsa_q_create_account(LSA_Q_CREATEACCOUNT *trn, POLICY_HND *hnd, DOM_SID *sid, uint32 desired_access)
1644 {
1645         memcpy(&trn->pol, hnd, sizeof(trn->pol));
1646
1647         init_dom_sid2(&trn->sid, sid);
1648         trn->access = desired_access;
1649 }
1650
1651
1652 /*******************************************************************
1653  Reads or writes an LSA_Q_CREATEACCOUNT structure.
1654 ********************************************************************/
1655
1656 BOOL lsa_io_q_create_account(const char *desc, LSA_Q_CREATEACCOUNT *r_c, prs_struct *ps, int depth)
1657 {
1658         prs_debug(ps, depth, desc, "lsa_io_q_create_account");
1659         depth++;
1660
1661         if(!prs_align(ps))
1662                 return False;
1663  
1664         if(!smb_io_pol_hnd("pol", &r_c->pol, ps, depth))
1665                 return False;
1666
1667         if(!smb_io_dom_sid2("sid", &r_c->sid, ps, depth)) /* domain SID */
1668                 return False;
1669
1670         if(!prs_uint32("access", ps, depth, &r_c->access))
1671                 return False;
1672   
1673         return True;
1674 }
1675
1676 /*******************************************************************
1677  Reads or writes an LSA_R_CREATEACCOUNT structure.
1678 ********************************************************************/
1679
1680 BOOL lsa_io_r_create_account(const char *desc, LSA_R_CREATEACCOUNT  *r_c, prs_struct *ps, int depth)
1681 {
1682         prs_debug(ps, depth, desc, "lsa_io_r_open_account");
1683         depth++;
1684
1685         if(!prs_align(ps))
1686                 return False;
1687  
1688         if(!smb_io_pol_hnd("pol", &r_c->pol, ps, depth))
1689                 return False;
1690
1691         if(!prs_ntstatus("status", ps, depth, &r_c->status))
1692                 return False;
1693
1694         return True;
1695 }
1696
1697
1698 void init_lsa_q_open_account(LSA_Q_OPENACCOUNT *trn, POLICY_HND *hnd, DOM_SID *sid, uint32 desired_access)
1699 {
1700         memcpy(&trn->pol, hnd, sizeof(trn->pol));
1701
1702         init_dom_sid2(&trn->sid, sid);
1703         trn->access = desired_access;
1704 }
1705
1706 /*******************************************************************
1707  Reads or writes an LSA_Q_OPENACCOUNT structure.
1708 ********************************************************************/
1709
1710 BOOL lsa_io_q_open_account(const char *desc, LSA_Q_OPENACCOUNT *r_c, prs_struct *ps, int depth)
1711 {
1712         prs_debug(ps, depth, desc, "lsa_io_q_open_account");
1713         depth++;
1714
1715         if(!prs_align(ps))
1716                 return False;
1717  
1718         if(!smb_io_pol_hnd("pol", &r_c->pol, ps, depth))
1719                 return False;
1720
1721         if(!smb_io_dom_sid2("sid", &r_c->sid, ps, depth)) /* domain SID */
1722                 return False;
1723
1724         if(!prs_uint32("access", ps, depth, &r_c->access))
1725                 return False;
1726   
1727         return True;
1728 }
1729
1730 /*******************************************************************
1731  Reads or writes an LSA_R_OPENACCOUNT structure.
1732 ********************************************************************/
1733
1734 BOOL lsa_io_r_open_account(const char *desc, LSA_R_OPENACCOUNT  *r_c, prs_struct *ps, int depth)
1735 {
1736         prs_debug(ps, depth, desc, "lsa_io_r_open_account");
1737         depth++;
1738
1739         if(!prs_align(ps))
1740                 return False;
1741  
1742         if(!smb_io_pol_hnd("pol", &r_c->pol, ps, depth))
1743                 return False;
1744
1745         if(!prs_ntstatus("status", ps, depth, &r_c->status))
1746                 return False;
1747
1748         return True;
1749 }
1750
1751
1752 void init_lsa_q_enum_privsaccount(LSA_Q_ENUMPRIVSACCOUNT *trn, POLICY_HND *hnd)
1753 {
1754         memcpy(&trn->pol, hnd, sizeof(trn->pol));
1755
1756 }
1757
1758 /*******************************************************************
1759  Reads or writes an LSA_Q_ENUMPRIVSACCOUNT structure.
1760 ********************************************************************/
1761
1762 BOOL lsa_io_q_enum_privsaccount(const char *desc, LSA_Q_ENUMPRIVSACCOUNT *r_c, prs_struct *ps, int depth)
1763 {
1764         prs_debug(ps, depth, desc, "lsa_io_q_enum_privsaccount");
1765         depth++;
1766
1767         if(!prs_align(ps))
1768                 return False;
1769  
1770         if(!smb_io_pol_hnd("pol", &r_c->pol, ps, depth))
1771                 return False;
1772
1773         return True;
1774 }
1775
1776 /*******************************************************************
1777  Reads or writes an LUID structure.
1778 ********************************************************************/
1779
1780 static BOOL lsa_io_luid(const char *desc, LUID *r_c, prs_struct *ps, int depth)
1781 {
1782         prs_debug(ps, depth, desc, "lsa_io_luid");
1783         depth++;
1784
1785         if(!prs_align(ps))
1786                 return False;
1787  
1788         if(!prs_uint32("low", ps, depth, &r_c->low))
1789                 return False;
1790
1791         if(!prs_uint32("high", ps, depth, &r_c->high))
1792                 return False;
1793
1794         return True;
1795 }
1796
1797 /*******************************************************************
1798  Reads or writes an LUID_ATTR structure.
1799 ********************************************************************/
1800
1801 static BOOL lsa_io_luid_attr(const char *desc, LUID_ATTR *r_c, prs_struct *ps, int depth)
1802 {
1803         prs_debug(ps, depth, desc, "lsa_io_luid_attr");
1804         depth++;
1805
1806         if(!prs_align(ps))
1807                 return False;
1808  
1809         if (!lsa_io_luid(desc, &r_c->luid, ps, depth))
1810                 return False;
1811
1812         if(!prs_uint32("attr", ps, depth, &r_c->attr))
1813                 return False;
1814
1815         return True;
1816 }
1817
1818 /*******************************************************************
1819  Reads or writes an PRIVILEGE_SET structure.
1820 ********************************************************************/
1821
1822 static BOOL lsa_io_privilege_set(const char *desc, PRIVILEGE_SET *r_c, prs_struct *ps, int depth)
1823 {
1824         uint32 i;
1825
1826         prs_debug(ps, depth, desc, "lsa_io_privilege_set");
1827         depth++;
1828
1829         if(!prs_align(ps))
1830                 return False;
1831  
1832         if(!prs_uint32("count", ps, depth, &r_c->count))
1833                 return False;
1834         if(!prs_uint32("control", ps, depth, &r_c->control))
1835                 return False;
1836
1837         for (i=0; i<r_c->count; i++) {
1838                 if (!lsa_io_luid_attr(desc, &r_c->set[i], ps, depth))
1839                         return False;
1840         }
1841         
1842         return True;
1843 }
1844
1845 NTSTATUS init_lsa_r_enum_privsaccount(TALLOC_CTX *mem_ctx, LSA_R_ENUMPRIVSACCOUNT *r_u, LUID_ATTR *set, uint32 count, uint32 control)
1846 {
1847         NTSTATUS ret = NT_STATUS_OK;
1848
1849         r_u->ptr = 1;
1850         r_u->count = count;
1851
1852         if ( !NT_STATUS_IS_OK(ret = privilege_set_init_by_ctx(mem_ctx, &(r_u->set))) )
1853                 return ret;
1854         
1855         r_u->set.count = count;
1856         
1857         if (!NT_STATUS_IS_OK(ret = dup_luid_attr(r_u->set.mem_ctx, &(r_u->set.set), set, count)))
1858                 return ret;
1859
1860         DEBUG(10,("init_lsa_r_enum_privsaccount: %d privileges\n", r_u->count));
1861
1862         return ret;
1863 }
1864
1865 /*******************************************************************
1866  Reads or writes an LSA_R_ENUMPRIVSACCOUNT structure.
1867 ********************************************************************/
1868
1869 BOOL lsa_io_r_enum_privsaccount(const char *desc, LSA_R_ENUMPRIVSACCOUNT *r_c, prs_struct *ps, int depth)
1870 {
1871         prs_debug(ps, depth, desc, "lsa_io_r_enum_privsaccount");
1872         depth++;
1873
1874         if(!prs_align(ps))
1875                 return False;
1876  
1877         if(!prs_uint32("ptr", ps, depth, &r_c->ptr))
1878                 return False;
1879
1880         if (r_c->ptr!=0) {
1881                 if(!prs_uint32("count", ps, depth, &r_c->count))
1882                         return False;
1883
1884                 /* malloc memory if unmarshalling here */
1885
1886                 if (UNMARSHALLING(ps) && r_c->count != 0) {
1887                         if (!NT_STATUS_IS_OK(privilege_set_init_by_ctx(ps->mem_ctx, &(r_c->set))))
1888                                 return False;
1889
1890                         if (!(r_c->set.set = PRS_ALLOC_MEM(ps,LUID_ATTR,r_c->count)))
1891                                 return False;
1892
1893                 }
1894                 
1895                 if(!lsa_io_privilege_set(desc, &r_c->set, ps, depth))
1896                         return False;
1897         }
1898
1899         if(!prs_ntstatus("status", ps, depth, &r_c->status))
1900                 return False;
1901
1902         return True;
1903 }
1904
1905
1906
1907 /*******************************************************************
1908  Reads or writes an  LSA_Q_GETSYSTEMACCOUNTstructure.
1909 ********************************************************************/
1910
1911 BOOL lsa_io_q_getsystemaccount(const char *desc, LSA_Q_GETSYSTEMACCOUNT  *r_c, prs_struct *ps, int depth)
1912 {
1913         prs_debug(ps, depth, desc, "lsa_io_q_getsystemaccount");
1914         depth++;
1915
1916         if(!prs_align(ps))
1917                 return False;
1918  
1919         if(!smb_io_pol_hnd("pol", &r_c->pol, ps, depth))
1920                 return False;
1921
1922         return True;
1923 }
1924
1925 /*******************************************************************
1926  Reads or writes an  LSA_R_GETSYSTEMACCOUNTstructure.
1927 ********************************************************************/
1928
1929 BOOL lsa_io_r_getsystemaccount(const char *desc, LSA_R_GETSYSTEMACCOUNT  *r_c, prs_struct *ps, int depth)
1930 {
1931         prs_debug(ps, depth, desc, "lsa_io_r_getsystemaccount");
1932         depth++;
1933
1934         if(!prs_align(ps))
1935                 return False;
1936  
1937         if(!prs_uint32("access", ps, depth, &r_c->access))
1938                 return False;
1939
1940         if(!prs_ntstatus("status", ps, depth, &r_c->status))
1941                 return False;
1942
1943         return True;
1944 }
1945
1946
1947 /*******************************************************************
1948  Reads or writes an LSA_Q_SETSYSTEMACCOUNT structure.
1949 ********************************************************************/
1950
1951 BOOL lsa_io_q_setsystemaccount(const char *desc, LSA_Q_SETSYSTEMACCOUNT  *r_c, prs_struct *ps, int depth)
1952 {
1953         prs_debug(ps, depth, desc, "lsa_io_q_setsystemaccount");
1954         depth++;
1955
1956         if(!prs_align(ps))
1957                 return False;
1958  
1959         if(!smb_io_pol_hnd("pol", &r_c->pol, ps, depth))
1960                 return False;
1961
1962         if(!prs_uint32("access", ps, depth, &r_c->access))
1963                 return False;
1964
1965         return True;
1966 }
1967
1968 /*******************************************************************
1969  Reads or writes an LSA_R_SETSYSTEMACCOUNT structure.
1970 ********************************************************************/
1971
1972 BOOL lsa_io_r_setsystemaccount(const char *desc, LSA_R_SETSYSTEMACCOUNT  *r_c, prs_struct *ps, int depth)
1973 {
1974         prs_debug(ps, depth, desc, "lsa_io_r_setsystemaccount");
1975         depth++;
1976
1977         if(!prs_align(ps))
1978                 return False;
1979  
1980         if(!prs_ntstatus("status", ps, depth, &r_c->status))
1981                 return False;
1982
1983         return True;
1984 }
1985
1986
1987 static void init_lsa_string( LSA_STRING *uni, const char *string )
1988 {
1989         init_unistr2(&uni->unistring, string, UNI_FLAGS_NONE);
1990         init_uni_hdr(&uni->hdr, &uni->unistring);
1991 }
1992
1993 void init_lsa_q_lookup_priv_value(LSA_Q_LOOKUP_PRIV_VALUE *q_u, POLICY_HND *hnd, const char *name)
1994 {
1995         memcpy(&q_u->pol, hnd, sizeof(q_u->pol));
1996         init_lsa_string( &q_u->privname, name );
1997 }
1998
1999 BOOL smb_io_lsa_string( const char *desc, LSA_STRING *string, prs_struct *ps, int depth )
2000 {
2001         prs_debug(ps, depth, desc, "smb_io_lsa_string");
2002         depth++;
2003
2004         if(!smb_io_unihdr ("hdr", &string->hdr, ps, depth))
2005                 return False;
2006         if(!smb_io_unistr2("unistring", &string->unistring, string->hdr.buffer, ps, depth))
2007                 return False;
2008         
2009         return True;
2010 }
2011
2012 /*******************************************************************
2013  Reads or writes an LSA_Q_LOOKUP_PRIV_VALUE  structure.
2014 ********************************************************************/
2015
2016 BOOL lsa_io_q_lookup_priv_value(const char *desc, LSA_Q_LOOKUP_PRIV_VALUE  *r_c, prs_struct *ps, int depth)
2017 {
2018         prs_debug(ps, depth, desc, "lsa_io_q_lookup_priv_value");
2019         depth++;
2020
2021         if(!prs_align(ps))
2022                 return False;
2023  
2024         if(!smb_io_pol_hnd("pol", &r_c->pol, ps, depth))
2025                 return False;
2026         if(!smb_io_lsa_string("privname", &r_c->privname, ps, depth))
2027                 return False;
2028
2029         return True;
2030 }
2031
2032 /*******************************************************************
2033  Reads or writes an  LSA_R_LOOKUP_PRIV_VALUE structure.
2034 ********************************************************************/
2035
2036 BOOL lsa_io_r_lookup_priv_value(const char *desc, LSA_R_LOOKUP_PRIV_VALUE  *r_c, prs_struct *ps, int depth)
2037 {
2038         prs_debug(ps, depth, desc, "lsa_io_r_lookup_priv_value");
2039         depth++;
2040
2041         if(!prs_align(ps))
2042                 return False;
2043                 
2044         if(!lsa_io_luid("luid", &r_c->luid, ps, depth))
2045                 return False;
2046  
2047         if(!prs_ntstatus("status", ps, depth, &r_c->status))
2048                 return False;
2049
2050         return True;
2051 }
2052
2053
2054 /*******************************************************************
2055  Reads or writes an LSA_Q_ADDPRIVS structure.
2056 ********************************************************************/
2057
2058 BOOL lsa_io_q_addprivs(const char *desc, LSA_Q_ADDPRIVS *r_c, prs_struct *ps, int depth)
2059 {
2060         prs_debug(ps, depth, desc, "lsa_io_q_addprivs");
2061         depth++;
2062
2063         if(!prs_align(ps))
2064                 return False;
2065  
2066         if(!smb_io_pol_hnd("pol", &r_c->pol, ps, depth))
2067                 return False;
2068         
2069         if(!prs_uint32("count", ps, depth, &r_c->count))
2070                 return False;
2071
2072         if (UNMARSHALLING(ps) && r_c->count!=0) {
2073                 if (!NT_STATUS_IS_OK(privilege_set_init_by_ctx(ps->mem_ctx, &(r_c->set))))
2074                         return False;
2075                 
2076                 if (!(r_c->set.set = PRS_ALLOC_MEM(ps, LUID_ATTR, r_c->count)))
2077                         return False;
2078         }
2079         
2080         if(!lsa_io_privilege_set(desc, &r_c->set, ps, depth))
2081                 return False;
2082         
2083         return True;
2084 }
2085
2086 /*******************************************************************
2087  Reads or writes an LSA_R_ADDPRIVS structure.
2088 ********************************************************************/
2089
2090 BOOL lsa_io_r_addprivs(const char *desc, LSA_R_ADDPRIVS *r_c, prs_struct *ps, int depth)
2091 {
2092         prs_debug(ps, depth, desc, "lsa_io_r_addprivs");
2093         depth++;
2094
2095         if(!prs_align(ps))
2096                 return False;
2097  
2098         if(!prs_ntstatus("status", ps, depth, &r_c->status))
2099                 return False;
2100
2101         return True;
2102 }
2103
2104 /*******************************************************************
2105  Reads or writes an LSA_Q_REMOVEPRIVS structure.
2106 ********************************************************************/
2107
2108 BOOL lsa_io_q_removeprivs(const char *desc, LSA_Q_REMOVEPRIVS *r_c, prs_struct *ps, int depth)
2109 {
2110         prs_debug(ps, depth, desc, "lsa_io_q_removeprivs");
2111         depth++;
2112
2113         if(!prs_align(ps))
2114                 return False;
2115  
2116         if(!smb_io_pol_hnd("pol", &r_c->pol, ps, depth))
2117                 return False;
2118         
2119         if(!prs_uint32("allrights", ps, depth, &r_c->allrights))
2120                 return False;
2121
2122         if(!prs_uint32("ptr", ps, depth, &r_c->ptr))
2123                 return False;
2124
2125         /* 
2126          * JFM: I'm not sure at all if the count is inside the ptr
2127          * never seen one with ptr=0
2128          */
2129
2130         if (r_c->ptr!=0) {
2131                 if(!prs_uint32("count", ps, depth, &r_c->count))
2132                         return False;
2133
2134                 if (UNMARSHALLING(ps) && r_c->count!=0) {
2135                         if (!NT_STATUS_IS_OK(privilege_set_init_by_ctx(ps->mem_ctx, &(r_c->set))))
2136                                 return False;
2137
2138                         if (!(r_c->set.set = PRS_ALLOC_MEM(ps, LUID_ATTR, r_c->count)))
2139                                 return False;
2140                 }
2141
2142                 if(!lsa_io_privilege_set(desc, &r_c->set, ps, depth))
2143                         return False;
2144         }
2145
2146         return True;
2147 }
2148
2149 /*******************************************************************
2150  Reads or writes an LSA_R_REMOVEPRIVS structure.
2151 ********************************************************************/
2152
2153 BOOL lsa_io_r_removeprivs(const char *desc, LSA_R_REMOVEPRIVS *r_c, prs_struct *ps, int depth)
2154 {
2155         prs_debug(ps, depth, desc, "lsa_io_r_removeprivs");
2156         depth++;
2157
2158         if(!prs_align(ps))
2159                 return False;
2160  
2161         if(!prs_ntstatus("status", ps, depth, &r_c->status))
2162                 return False;
2163
2164         return True;
2165 }
2166
2167 BOOL policy_handle_is_valid(const POLICY_HND *hnd)
2168 {
2169         POLICY_HND zero_pol;
2170
2171         ZERO_STRUCT(zero_pol);
2172         return ((memcmp(&zero_pol, hnd, sizeof(POLICY_HND)) == 0) ? False : True );
2173 }
2174
2175 /*******************************************************************
2176  Reads or writes an LSA_DNS_DOM_INFO structure.
2177 ********************************************************************/
2178
2179 BOOL lsa_io_dns_dom_info(const char *desc, LSA_DNS_DOM_INFO *info,
2180                          prs_struct *ps, int depth)
2181 {
2182         prs_debug(ps, depth, desc, "lsa_io_dns_dom_info");
2183         depth++;
2184
2185         if(!prs_align(ps))
2186                 return False;
2187         if(!smb_io_unihdr("nb_name", &info->hdr_nb_dom_name, ps, depth))
2188                 return False;
2189         if(!smb_io_unihdr("dns_name", &info->hdr_dns_dom_name, ps, depth))
2190                 return False;
2191         if(!smb_io_unihdr("forest", &info->hdr_forest_name, ps, depth))
2192                 return False;
2193
2194         if(!prs_align(ps))
2195                 return False;
2196         if ( !smb_io_uuid("dom_guid", &info->dom_guid, ps, depth) )
2197                 return False;
2198
2199         if(!prs_align(ps))
2200                 return False;
2201         if(!prs_uint32("dom_sid", ps, depth, &info->ptr_dom_sid))
2202                 return False;
2203
2204         if(!smb_io_unistr2("nb_name", &info->uni_nb_dom_name,
2205                            info->hdr_nb_dom_name.buffer, ps, depth))
2206                 return False;
2207         if(!smb_io_unistr2("dns_name", &info->uni_dns_dom_name, 
2208                            info->hdr_dns_dom_name.buffer, ps, depth))
2209                 return False;
2210         if(!smb_io_unistr2("forest", &info->uni_forest_name, 
2211                            info->hdr_forest_name.buffer, ps, depth))
2212                 return False;
2213
2214         if(!smb_io_dom_sid2("dom_sid", &info->dom_sid, ps, depth))
2215                 return False;
2216
2217         return True;
2218         
2219 }
2220
2221 /*******************************************************************
2222  Inits an LSA_Q_QUERY_INFO2 structure.
2223 ********************************************************************/
2224
2225 void init_q_query2(LSA_Q_QUERY_INFO2 *q_q, POLICY_HND *hnd, uint16 info_class)
2226 {
2227         DEBUG(5, ("init_q_query2\n"));
2228
2229         memcpy(&q_q->pol, hnd, sizeof(q_q->pol));
2230
2231         q_q->info_class = info_class;
2232 }
2233
2234 /*******************************************************************
2235  Reads or writes an LSA_Q_QUERY_DNSDOMINFO structure.
2236 ********************************************************************/
2237
2238 BOOL lsa_io_q_query_info2(const char *desc, LSA_Q_QUERY_INFO2 *q_c,
2239                           prs_struct *ps, int depth)
2240 {
2241         prs_debug(ps, depth, desc, "lsa_io_q_query_info2");
2242         depth++;
2243
2244         if(!prs_align(ps))
2245                 return False;
2246  
2247         if(!smb_io_pol_hnd("pol", &q_c->pol, ps, depth))
2248                 return False;
2249         
2250         if(!prs_uint16("info_class", ps, depth, &q_c->info_class))
2251                 return False;
2252
2253         return True;
2254 }
2255
2256 /*******************************************************************
2257  Reads or writes an LSA_R_QUERY_DNSDOMINFO structure.
2258 ********************************************************************/
2259
2260 BOOL lsa_io_r_query_info2(const char *desc, LSA_R_QUERY_INFO2 *r_c,
2261                           prs_struct *ps, int depth)
2262 {
2263         prs_debug(ps, depth, desc, "lsa_io_r_query_info2");
2264         depth++;
2265
2266         if(!prs_align(ps))
2267                 return False;
2268
2269         if(!prs_uint32("ptr", ps, depth, &r_c->ptr))
2270                 return False;
2271         if(!prs_uint16("info_class", ps, depth, &r_c->info_class))
2272                 return False;
2273         switch(r_c->info_class) {
2274         case 0x000c:
2275                 if (!lsa_io_dns_dom_info("info12", &r_c->info.dns_dom_info,
2276                                          ps, depth))
2277                         return False;
2278                 break;
2279         default:
2280                 DEBUG(0,("lsa_io_r_query_info2: unknown info class %d\n",
2281                          r_c->info_class));
2282                 return False;
2283         }
2284
2285         if(!prs_align(ps))
2286                 return False;
2287         if(!prs_ntstatus("status", ps, depth, &r_c->status))
2288                 return False;
2289
2290         return True;
2291 }
2292
2293 /*******************************************************************
2294  Inits an LSA_Q_ENUM_ACCT_RIGHTS structure.
2295 ********************************************************************/
2296 void init_q_enum_acct_rights(LSA_Q_ENUM_ACCT_RIGHTS *q_q, 
2297                              POLICY_HND *hnd, 
2298                              uint32 count, 
2299                              DOM_SID *sid)
2300 {
2301         DEBUG(5, ("init_q_enum_acct_rights\n"));
2302
2303         q_q->pol = *hnd;
2304         init_dom_sid2(&q_q->sid, sid);
2305 }
2306
2307 /*******************************************************************
2308 ********************************************************************/
2309 NTSTATUS init_r_enum_acct_rights( LSA_R_ENUM_ACCT_RIGHTS *r_u, PRIVILEGE_SET *privileges )
2310 {
2311         uint32 i;
2312         char *privname;
2313         const char **privname_array = NULL;
2314         int num_priv = 0;
2315
2316         for ( i=0; i<privileges->count; i++ ) {
2317                 privname = luid_to_privilege_name( &privileges->set[i].luid );
2318                 if ( privname ) {
2319                         if ( !add_string_to_array( get_talloc_ctx(), privname, &privname_array, &num_priv ) ) 
2320                                 return NT_STATUS_NO_MEMORY;
2321                 }
2322         }
2323
2324         if ( num_priv ) {
2325                 r_u->rights = TALLOC_P( get_talloc_ctx(), UNISTR4_ARRAY );
2326
2327                 if ( !init_unistr4_array( r_u->rights, num_priv, privname_array ) ) 
2328                         return NT_STATUS_NO_MEMORY;
2329
2330                 r_u->count = num_priv;
2331         }
2332
2333         return NT_STATUS_OK;
2334 }
2335
2336 /*******************************************************************
2337 reads or writes a LSA_Q_ENUM_ACCT_RIGHTS structure.
2338 ********************************************************************/
2339 BOOL lsa_io_q_enum_acct_rights(const char *desc, LSA_Q_ENUM_ACCT_RIGHTS *q_q, prs_struct *ps, int depth)
2340 {
2341         
2342         if (q_q == NULL)
2343                 return False;
2344
2345         prs_debug(ps, depth, desc, "lsa_io_q_enum_acct_rights");
2346         depth++;
2347
2348         if (!smb_io_pol_hnd("", &q_q->pol, ps, depth))
2349                 return False;
2350
2351         if(!smb_io_dom_sid2("sid", &q_q->sid, ps, depth))
2352                 return False;
2353
2354         return True;
2355 }
2356
2357
2358 /*******************************************************************
2359 reads or writes a LSA_R_ENUM_ACCT_RIGHTS structure.
2360 ********************************************************************/
2361 BOOL lsa_io_r_enum_acct_rights(const char *desc, LSA_R_ENUM_ACCT_RIGHTS *r_c, prs_struct *ps, int depth)
2362 {
2363         prs_debug(ps, depth, desc, "lsa_io_r_enum_acct_rights");
2364         depth++;
2365
2366         if(!prs_uint32("count   ", ps, depth, &r_c->count))
2367                 return False;
2368
2369         if ( !prs_pointer("rights", ps, depth, (void**)&r_c->rights, sizeof(UNISTR4_ARRAY), (PRS_POINTER_CAST)prs_unistr4_array) )
2370                 return False;
2371
2372         if(!prs_align(ps))
2373                 return False;
2374
2375         if(!prs_ntstatus("status", ps, depth, &r_c->status))
2376                 return False;
2377
2378         return True;
2379 }
2380
2381
2382 /*******************************************************************
2383  Inits an LSA_Q_ADD_ACCT_RIGHTS structure.
2384 ********************************************************************/
2385 void init_q_add_acct_rights( LSA_Q_ADD_ACCT_RIGHTS *q_q, POLICY_HND *hnd, 
2386                              DOM_SID *sid, uint32 count, const char **rights )
2387 {
2388         DEBUG(5, ("init_q_add_acct_rights\n"));
2389
2390         q_q->pol = *hnd;
2391         init_dom_sid2(&q_q->sid, sid);
2392         
2393         q_q->rights = TALLOC_P( get_talloc_ctx(), UNISTR4_ARRAY );
2394         init_unistr4_array( q_q->rights, count, rights );
2395         
2396         q_q->count = count;
2397 }
2398
2399
2400 /*******************************************************************
2401 reads or writes a LSA_Q_ADD_ACCT_RIGHTS structure.
2402 ********************************************************************/
2403 BOOL lsa_io_q_add_acct_rights(const char *desc, LSA_Q_ADD_ACCT_RIGHTS *q_q, prs_struct *ps, int depth)
2404 {
2405         prs_debug(ps, depth, desc, "lsa_io_q_add_acct_rights");
2406         depth++;
2407
2408         if (!smb_io_pol_hnd("", &q_q->pol, ps, depth))
2409                 return False;
2410
2411         if(!smb_io_dom_sid2("sid", &q_q->sid, ps, depth))
2412                 return False;
2413
2414         if(!prs_uint32("count", ps, depth, &q_q->count))
2415                 return False;
2416
2417         if ( !prs_pointer("rights", ps, depth, (void**)&q_q->rights, sizeof(UNISTR4_ARRAY), (PRS_POINTER_CAST)prs_unistr4_array) )
2418                 return False;
2419
2420         return True;
2421 }
2422
2423 /*******************************************************************
2424 reads or writes a LSA_R_ENUM_ACCT_RIGHTS structure.
2425 ********************************************************************/
2426 BOOL lsa_io_r_add_acct_rights(const char *desc, LSA_R_ADD_ACCT_RIGHTS *r_c, prs_struct *ps, int depth)
2427 {
2428         prs_debug(ps, depth, desc, "lsa_io_r_add_acct_rights");
2429         depth++;
2430
2431         if(!prs_ntstatus("status", ps, depth, &r_c->status))
2432                 return False;
2433
2434         return True;
2435 }
2436
2437
2438 /*******************************************************************
2439  Inits an LSA_Q_REMOVE_ACCT_RIGHTS structure.
2440 ********************************************************************/
2441 void init_q_remove_acct_rights(LSA_Q_REMOVE_ACCT_RIGHTS *q_q, 
2442                                POLICY_HND *hnd, 
2443                                DOM_SID *sid,
2444                                uint32 removeall,
2445                                uint32 count, 
2446                                const char **rights)
2447 {
2448         DEBUG(5, ("init_q_remove_acct_rights\n"));
2449
2450         q_q->pol = *hnd;
2451
2452         init_dom_sid2(&q_q->sid, sid);
2453
2454         q_q->removeall = removeall;
2455         q_q->count = count;
2456
2457         q_q->rights = TALLOC_P( get_talloc_ctx(), UNISTR4_ARRAY );
2458         init_unistr4_array( q_q->rights, count, rights );
2459 }
2460
2461
2462 /*******************************************************************
2463 reads or writes a LSA_Q_REMOVE_ACCT_RIGHTS structure.
2464 ********************************************************************/
2465 BOOL lsa_io_q_remove_acct_rights(const char *desc, LSA_Q_REMOVE_ACCT_RIGHTS *q_q, prs_struct *ps, int depth)
2466 {
2467         prs_debug(ps, depth, desc, "lsa_io_q_remove_acct_rights");
2468         depth++;
2469
2470         if (!smb_io_pol_hnd("", &q_q->pol, ps, depth))
2471                 return False;
2472
2473         if(!smb_io_dom_sid2("sid", &q_q->sid, ps, depth))
2474                 return False;
2475
2476         if(!prs_uint32("removeall", ps, depth, &q_q->removeall))
2477                 return False;
2478
2479         if(!prs_uint32("count", ps, depth, &q_q->count))
2480                 return False;
2481
2482         if ( !prs_pointer("rights", ps, depth, (void**)&q_q->rights, sizeof(UNISTR4_ARRAY), (PRS_POINTER_CAST)prs_unistr4_array) )
2483                 return False;
2484
2485         return True;
2486 }
2487
2488 /*******************************************************************
2489 reads or writes a LSA_R_ENUM_ACCT_RIGHTS structure.
2490 ********************************************************************/
2491 BOOL lsa_io_r_remove_acct_rights(const char *desc, LSA_R_REMOVE_ACCT_RIGHTS *r_c, prs_struct *ps, int depth)
2492 {
2493         prs_debug(ps, depth, desc, "lsa_io_r_remove_acct_rights");
2494         depth++;
2495
2496         if(!prs_ntstatus("status", ps, depth, &r_c->status))
2497                 return False;
2498
2499         return True;
2500 }