2230c9147f9119f78eb8e685ec5d605ce941ec66
[samba.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 3 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, see <http://www.gnu.org/licenses/>.
23  */
24
25 #include "includes.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_RPC_PARSE
29
30 static bool lsa_io_trans_names(const char *desc, LSA_TRANS_NAME_ENUM *trn, prs_struct *ps, int depth);
31 static bool lsa_io_trans_names2(const char *desc, LSA_TRANS_NAME_ENUM2 *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  Inits a LSA_TRANS_NAME2 structure.
74 ********************************************************************/
75
76 void init_lsa_trans_name2(LSA_TRANS_NAME2 *trn, UNISTR2 *uni_name,
77                          uint16 sid_name_use, const char *name, uint32 idx)
78 {
79         trn->sid_name_use = sid_name_use;
80         init_unistr2(uni_name, name, UNI_FLAGS_NONE);
81         init_uni_hdr(&trn->hdr_name, uni_name);
82         trn->domain_idx = idx;
83         trn->unknown = 0;
84 }
85
86 /*******************************************************************
87  Reads or writes a LSA_TRANS_NAME2 structure.
88 ********************************************************************/
89
90 static bool lsa_io_trans_name2(const char *desc, LSA_TRANS_NAME2 *trn, prs_struct *ps, 
91                               int depth)
92 {
93         prs_debug(ps, depth, desc, "lsa_io_trans_name2");
94         depth++;
95
96         if(!prs_align(ps))
97                 return False;
98         
99         if(!prs_uint16("sid_name_use", ps, depth, &trn->sid_name_use))
100                 return False;
101         if(!prs_align(ps))
102                 return False;
103         
104         if(!smb_io_unihdr ("hdr_name", &trn->hdr_name, ps, depth))
105                 return False;
106         if(!prs_uint32("domain_idx  ", ps, depth, &trn->domain_idx))
107                 return False;
108         if(!prs_uint32("unknown  ", ps, depth, &trn->unknown))
109                 return False;
110
111         return True;
112 }
113
114 /*******************************************************************
115  Reads or writes a DOM_R_REF structure.
116 ********************************************************************/
117
118 static bool lsa_io_dom_r_ref(const char *desc, DOM_R_REF *dom, prs_struct *ps, int depth)
119 {
120         unsigned int i;
121
122         prs_debug(ps, depth, desc, "lsa_io_dom_r_ref");
123         depth++;
124
125         if(!prs_align(ps))
126                 return False;
127         
128         if(!prs_uint32("num_ref_doms_1", ps, depth, &dom->num_ref_doms_1)) /* num referenced domains? */
129                 return False;
130         if(!prs_uint32("ptr_ref_dom   ", ps, depth, &dom->ptr_ref_dom)) /* undocumented buffer pointer. */
131                 return False;
132         if(!prs_uint32("max_entries   ", ps, depth, &dom->max_entries)) /* 32 - max number of entries */
133                 return False;
134
135         SMB_ASSERT_ARRAY(dom->hdr_ref_dom, dom->num_ref_doms_1);
136
137         if (dom->ptr_ref_dom != 0) {
138
139                 if(!prs_uint32("num_ref_doms_2", ps, depth, &dom->num_ref_doms_2)) /* 4 - num referenced domains? */
140                         return False;
141
142                 SMB_ASSERT_ARRAY(dom->ref_dom, dom->num_ref_doms_2);
143
144                 for (i = 0; i < dom->num_ref_doms_1; i++) {
145                         fstring t;
146
147                         slprintf(t, sizeof(t) - 1, "dom_ref[%d] ", i);
148                         if(!smb_io_unihdr(t, &dom->hdr_ref_dom[i].hdr_dom_name, ps, depth))
149                                 return False;
150
151                         slprintf(t, sizeof(t) - 1, "sid_ptr[%d] ", i);
152                         if(!prs_uint32(t, ps, depth, &dom->hdr_ref_dom[i].ptr_dom_sid))
153                                 return False;
154                 }
155
156                 for (i = 0; i < dom->num_ref_doms_2; i++) {
157                         fstring t;
158
159                         if (dom->hdr_ref_dom[i].hdr_dom_name.buffer != 0) {
160                                 slprintf(t, sizeof(t) - 1, "dom_ref[%d] ", i);
161                                 if(!smb_io_unistr2(t, &dom->ref_dom[i].uni_dom_name, True, ps, depth)) /* domain name unicode string */
162                                         return False;
163                                 if(!prs_align(ps))
164                                         return False;
165                         }
166
167                         if (dom->hdr_ref_dom[i].ptr_dom_sid != 0) {
168                                 slprintf(t, sizeof(t) - 1, "sid_ptr[%d] ", i);
169                                 if(!smb_io_dom_sid2(t, &dom->ref_dom[i].ref_dom, ps, depth)) /* referenced domain SIDs */
170                                         return False;
171                         }
172                 }
173         }
174
175         return True;
176 }
177
178 /*******************************************************************
179  Inits a LSA_SID_ENUM structure.
180 ********************************************************************/
181
182 static void init_lsa_sid_enum(TALLOC_CTX *mem_ctx, LSA_SID_ENUM *sen, 
183                        int num_entries, const DOM_SID *sids)
184 {
185         int i;
186
187         DEBUG(5, ("init_lsa_sid_enum\n"));
188
189         sen->num_entries  = num_entries;
190         sen->ptr_sid_enum = (num_entries != 0);
191         sen->num_entries2 = num_entries;
192
193         /* Allocate memory for sids and sid pointers */
194
195         if (num_entries) {
196                 if ((sen->ptr_sid = TALLOC_ZERO_ARRAY(mem_ctx, uint32, num_entries )) == NULL) {
197                         DEBUG(3, ("init_lsa_sid_enum(): out of memory for ptr_sid\n"));
198                         return;
199                 }
200
201                 if ((sen->sid = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID2, num_entries)) == NULL) {
202                         DEBUG(3, ("init_lsa_sid_enum(): out of memory for sids\n"));
203                         return;
204                 }
205         }
206
207         /* Copy across SIDs and SID pointers */
208
209         for (i = 0; i < num_entries; i++) {
210                 sen->ptr_sid[i] = 1;
211                 init_dom_sid2(&sen->sid[i], &sids[i]);
212         }
213 }
214
215 /*******************************************************************
216  Reads or writes a LSA_SID_ENUM structure.
217 ********************************************************************/
218
219 static bool lsa_io_sid_enum(const char *desc, LSA_SID_ENUM *sen, prs_struct *ps, 
220                             int depth)
221 {
222         unsigned int i;
223
224         prs_debug(ps, depth, desc, "lsa_io_sid_enum");
225         depth++;
226
227         if(!prs_align(ps))
228                 return False;
229         
230         if(!prs_uint32("num_entries ", ps, depth, &sen->num_entries))
231                 return False;
232         if(!prs_uint32("ptr_sid_enum", ps, depth, &sen->ptr_sid_enum))
233                 return False;
234
235         /*
236            if the ptr is NULL, leave here. checked from a real w2k trace.
237            JFM, 11/23/2001
238          */
239         
240         if (sen->ptr_sid_enum==0)
241                 return True;
242
243         if(!prs_uint32("num_entries2", ps, depth, &sen->num_entries2))
244                 return False;
245
246         /* Mallocate memory if we're unpacking from the wire */
247
248         if (UNMARSHALLING(ps) && sen->num_entries) {
249                 if ((sen->ptr_sid = PRS_ALLOC_MEM( ps, uint32, sen->num_entries)) == NULL) {
250                         DEBUG(3, ("init_lsa_sid_enum(): out of memory for "
251                                   "ptr_sid\n"));
252                         return False;
253                 }
254
255                 if ((sen->sid = PRS_ALLOC_MEM( ps, DOM_SID2, sen->num_entries)) == NULL) {
256                         DEBUG(3, ("init_lsa_sid_enum(): out of memory for "
257                                   "sids\n"));
258                         return False;
259                 }
260         }
261
262         for (i = 0; i < sen->num_entries; i++) {        
263                 fstring temp;
264
265                 slprintf(temp, sizeof(temp) - 1, "ptr_sid[%d]", i);
266                 if(!prs_uint32(temp, ps, depth, &sen->ptr_sid[i])) {
267                         return False;
268                 }
269         }
270
271         for (i = 0; i < sen->num_entries; i++) {
272                 fstring temp;
273
274                 slprintf(temp, sizeof(temp) - 1, "sid[%d]", i);
275                 if(!smb_io_dom_sid2(temp, &sen->sid[i], ps, depth)) {
276                         return False;
277                 }
278         }
279
280         return True;
281 }
282
283 /*******************************************************************
284  Inits an LSA_R_ENUM_TRUST_DOM structure.
285 ********************************************************************/
286
287 void init_q_lookup_sids(TALLOC_CTX *mem_ctx, LSA_Q_LOOKUP_SIDS *q_l, 
288                         POLICY_HND *hnd, int num_sids, const DOM_SID *sids,
289                         uint16 level)
290 {
291         DEBUG(5, ("init_q_lookup_sids\n"));
292
293         ZERO_STRUCTP(q_l);
294
295         memcpy(&q_l->pol, hnd, sizeof(q_l->pol));
296         init_lsa_sid_enum(mem_ctx, &q_l->sids, num_sids, sids);
297         
298         q_l->level = level;
299 }
300
301 /*******************************************************************
302  Reads or writes a LSA_Q_LOOKUP_SIDS structure.
303 ********************************************************************/
304
305 bool lsa_io_q_lookup_sids(const char *desc, LSA_Q_LOOKUP_SIDS *q_s, prs_struct *ps,
306                           int depth)
307 {
308         prs_debug(ps, depth, desc, "lsa_io_q_lookup_sids");
309         depth++;
310
311         if(!prs_align(ps))
312                 return False;
313         
314         if(!smb_io_pol_hnd("pol_hnd", &q_s->pol, ps, depth)) /* policy handle */
315                 return False;
316         if(!lsa_io_sid_enum("sids   ", &q_s->sids, ps, depth)) /* sids to be looked up */
317                 return False;
318         if(!lsa_io_trans_names("names  ", &q_s->names, ps, depth)) /* translated names */
319                 return False;
320
321         if(!prs_uint16("level", ps, depth, &q_s->level)) /* lookup level */
322                 return False;
323         if(!prs_align(ps))
324                 return False;
325
326         if(!prs_uint32("mapped_count", ps, depth, &q_s->mapped_count))
327                 return False;
328
329         return True;
330 }
331
332 /*******************************************************************
333  Reads or writes a LSA_Q_LOOKUP_SIDS2 structure.
334 ********************************************************************/
335
336 bool lsa_io_q_lookup_sids2(const char *desc, LSA_Q_LOOKUP_SIDS2 *q_s, prs_struct *ps,
337                           int depth)
338 {
339         prs_debug(ps, depth, desc, "lsa_io_q_lookup_sids2");
340         depth++;
341
342         if(!prs_align(ps))
343                 return False;
344         
345         if(!smb_io_pol_hnd("pol_hnd", &q_s->pol, ps, depth)) /* policy handle */
346                 return False;
347         if(!lsa_io_sid_enum("sids   ", &q_s->sids, ps, depth)) /* sids to be looked up */
348                 return False;
349         if(!lsa_io_trans_names2("names  ", &q_s->names, ps, depth)) /* translated names */
350                 return False;
351
352         if(!prs_uint16("level", ps, depth, &q_s->level)) /* lookup level */
353                 return False;
354         if(!prs_align(ps))
355                 return False;
356
357         if(!prs_uint32("mapped_count", ps, depth, &q_s->mapped_count))
358                 return False;
359         if(!prs_uint32("unknown1", ps, depth, &q_s->unknown1))
360                 return False;
361         if(!prs_uint32("unknown2", ps, depth, &q_s->unknown2))
362                 return False;
363
364         return True;
365 }
366
367 /*******************************************************************
368  Reads or writes a LSA_Q_LOOKUP_SIDS3 structure.
369 ********************************************************************/
370
371 bool lsa_io_q_lookup_sids3(const char *desc, LSA_Q_LOOKUP_SIDS3 *q_s, prs_struct *ps,
372                           int depth)
373 {
374         prs_debug(ps, depth, desc, "lsa_io_q_lookup_sids3");
375         depth++;
376
377         if(!prs_align(ps))
378                 return False;
379         
380         if(!lsa_io_sid_enum("sids   ", &q_s->sids, ps, depth)) /* sids to be looked up */
381                 return False;
382         if(!lsa_io_trans_names2("names  ", &q_s->names, ps, depth)) /* translated names */
383                 return False;
384
385         if(!prs_uint16("level", ps, depth, &q_s->level)) /* lookup level */
386                 return False;
387         if(!prs_align(ps))
388                 return False;
389
390         if(!prs_uint32("mapped_count", ps, depth, &q_s->mapped_count))
391                 return False;
392         if(!prs_uint32("unknown1", ps, depth, &q_s->unknown1))
393                 return False;
394         if(!prs_uint32("unknown2", ps, depth, &q_s->unknown2))
395                 return False;
396
397         return True;
398 }
399
400
401 /*******************************************************************
402  Reads or writes a structure.
403 ********************************************************************/
404
405 static bool lsa_io_trans_names(const char *desc, LSA_TRANS_NAME_ENUM *trn,
406                 prs_struct *ps, int depth)
407 {
408         unsigned int i;
409
410         prs_debug(ps, depth, desc, "lsa_io_trans_names");
411         depth++;
412
413         if(!prs_align(ps))
414                 return False;
415    
416         if(!prs_uint32("num_entries    ", ps, depth, &trn->num_entries))
417                 return False;
418         if(!prs_uint32("ptr_trans_names", ps, depth, &trn->ptr_trans_names))
419                 return False;
420
421         if (trn->ptr_trans_names != 0) {
422                 if(!prs_uint32("num_entries2   ", ps, depth, 
423                                &trn->num_entries2))
424                         return False;
425
426                 if (trn->num_entries2 != trn->num_entries) {
427                         /* RPC fault */
428                         return False;
429                 }
430
431                 if (UNMARSHALLING(ps) && trn->num_entries2) {
432                         if ((trn->name = PRS_ALLOC_MEM(ps, LSA_TRANS_NAME, trn->num_entries2)) == NULL) {
433                                 return False;
434                         }
435
436                         if ((trn->uni_name = PRS_ALLOC_MEM(ps, UNISTR2, trn->num_entries2)) == NULL) {
437                                 return False;
438                         }
439                 }
440
441                 for (i = 0; i < trn->num_entries2; i++) {
442                         fstring t;
443                         slprintf(t, sizeof(t) - 1, "name[%d] ", i);
444
445                         if(!lsa_io_trans_name(t, &trn->name[i], ps, depth)) /* translated name */
446                                 return False;
447                 }
448
449                 for (i = 0; i < trn->num_entries2; i++) {
450                         fstring t;
451                         slprintf(t, sizeof(t) - 1, "name[%d] ", i);
452
453                         if(!smb_io_unistr2(t, &trn->uni_name[i], trn->name[i].hdr_name.buffer, ps, depth))
454                                 return False;
455                         if(!prs_align(ps))
456                                 return False;
457                 }
458         }
459
460         return True;
461 }
462
463 /*******************************************************************
464  Reads or writes a structure.
465 ********************************************************************/
466
467 static bool lsa_io_trans_names2(const char *desc, LSA_TRANS_NAME_ENUM2 *trn,
468                 prs_struct *ps, int depth)
469 {
470         unsigned int i;
471
472         prs_debug(ps, depth, desc, "lsa_io_trans_names2");
473         depth++;
474
475         if(!prs_align(ps))
476                 return False;
477    
478         if(!prs_uint32("num_entries    ", ps, depth, &trn->num_entries))
479                 return False;
480         if(!prs_uint32("ptr_trans_names", ps, depth, &trn->ptr_trans_names))
481                 return False;
482
483         if (trn->ptr_trans_names != 0) {
484                 if(!prs_uint32("num_entries2   ", ps, depth, 
485                                &trn->num_entries2))
486                         return False;
487
488                 if (trn->num_entries2 != trn->num_entries) {
489                         /* RPC fault */
490                         return False;
491                 }
492
493                 if (UNMARSHALLING(ps) && trn->num_entries2) {
494                         if ((trn->name = PRS_ALLOC_MEM(ps, LSA_TRANS_NAME2, trn->num_entries2)) == NULL) {
495                                 return False;
496                         }
497
498                         if ((trn->uni_name = PRS_ALLOC_MEM(ps, UNISTR2, trn->num_entries2)) == NULL) {
499                                 return False;
500                         }
501                 }
502
503                 for (i = 0; i < trn->num_entries2; i++) {
504                         fstring t;
505                         slprintf(t, sizeof(t) - 1, "name[%d] ", i);
506
507                         if(!lsa_io_trans_name2(t, &trn->name[i], ps, depth)) /* translated name */
508                                 return False;
509                 }
510
511                 for (i = 0; i < trn->num_entries2; i++) {
512                         fstring t;
513                         slprintf(t, sizeof(t) - 1, "name[%d] ", i);
514
515                         if(!smb_io_unistr2(t, &trn->uni_name[i], trn->name[i].hdr_name.buffer, ps, depth))
516                                 return False;
517                         if(!prs_align(ps))
518                                 return False;
519                 }
520         }
521
522         return True;
523 }
524
525
526 /*******************************************************************
527  Reads or writes a structure.
528 ********************************************************************/
529
530 bool lsa_io_r_lookup_sids(const char *desc, LSA_R_LOOKUP_SIDS *r_s, 
531                           prs_struct *ps, int depth)
532 {
533         prs_debug(ps, depth, desc, "lsa_io_r_lookup_sids");
534         depth++;
535
536         if(!prs_align(ps))
537                 return False;
538         
539         if(!prs_uint32("ptr_dom_ref", ps, depth, &r_s->ptr_dom_ref))
540                 return False;
541
542         if (r_s->ptr_dom_ref != 0)
543                 if(!lsa_io_dom_r_ref ("dom_ref", r_s->dom_ref, ps, depth)) /* domain reference info */
544                         return False;
545
546         if(!lsa_io_trans_names("names  ", &r_s->names, ps, depth)) /* translated names */
547                 return False;
548
549         if(!prs_align(ps))
550                 return False;
551
552         if(!prs_uint32("mapped_count", ps, depth, &r_s->mapped_count))
553                 return False;
554
555         if(!prs_ntstatus("status      ", ps, depth, &r_s->status))
556                 return False;
557
558         return True;
559 }
560
561 /*******************************************************************
562  Reads or writes a structure.
563 ********************************************************************/
564
565 bool lsa_io_r_lookup_sids2(const char *desc, LSA_R_LOOKUP_SIDS2 *r_s, 
566                           prs_struct *ps, int depth)
567 {
568         prs_debug(ps, depth, desc, "lsa_io_r_lookup_sids2");
569         depth++;
570
571         if(!prs_align(ps))
572                 return False;
573         
574         if(!prs_uint32("ptr_dom_ref", ps, depth, &r_s->ptr_dom_ref))
575                 return False;
576
577         if (r_s->ptr_dom_ref != 0)
578                 if(!lsa_io_dom_r_ref ("dom_ref", r_s->dom_ref, ps, depth)) /* domain reference info */
579                         return False;
580
581         if(!lsa_io_trans_names2("names  ", &r_s->names, ps, depth)) /* translated names */
582                 return False;
583
584         if(!prs_align(ps))
585                 return False;
586
587         if(!prs_uint32("mapped_count", ps, depth, &r_s->mapped_count))
588                 return False;
589
590         if(!prs_ntstatus("status      ", ps, depth, &r_s->status))
591                 return False;
592
593         return True;
594 }
595
596
597 /*******************************************************************
598  Reads or writes a structure.
599 ********************************************************************/
600
601 bool lsa_io_r_lookup_sids3(const char *desc, LSA_R_LOOKUP_SIDS3 *r_s, 
602                           prs_struct *ps, int depth)
603 {
604         prs_debug(ps, depth, desc, "lsa_io_r_lookup_sids3");
605         depth++;
606
607         if(!prs_align(ps))
608                 return False;
609         
610         if(!prs_uint32("ptr_dom_ref", ps, depth, &r_s->ptr_dom_ref))
611                 return False;
612
613         if (r_s->ptr_dom_ref != 0)
614                 if(!lsa_io_dom_r_ref ("dom_ref", r_s->dom_ref, ps, depth)) /* domain reference info */
615                         return False;
616
617         if(!lsa_io_trans_names2("names  ", &r_s->names, ps, depth)) /* translated names */
618                 return False;
619
620         if(!prs_align(ps))
621                 return False;
622
623         if(!prs_uint32("mapped_count", ps, depth, &r_s->mapped_count))
624                 return False;
625
626         if(!prs_ntstatus("status      ", ps, depth, &r_s->status))
627                 return False;
628
629         return True;
630 }
631
632 /*******************************************************************
633 makes a structure.
634 ********************************************************************/
635
636 void init_q_lookup_names(TALLOC_CTX *mem_ctx, LSA_Q_LOOKUP_NAMES *q_l, 
637                          POLICY_HND *hnd, int num_names, const char **names, 
638                          int level)
639 {
640         unsigned int i;
641
642         DEBUG(5, ("init_q_lookup_names\n"));
643
644         ZERO_STRUCTP(q_l);
645
646         q_l->pol = *hnd;
647         q_l->num_entries = num_names;
648         q_l->num_entries2 = num_names;
649         q_l->lookup_level = level;
650
651         if (num_names) {
652                 if ((q_l->uni_name = TALLOC_ZERO_ARRAY(mem_ctx, UNISTR2, num_names)) == NULL) {
653                         DEBUG(3, ("init_q_lookup_names(): out of memory\n"));
654                         return;
655                 }
656
657                 if ((q_l->hdr_name = TALLOC_ZERO_ARRAY(mem_ctx, UNIHDR, num_names)) == NULL) {
658                         DEBUG(3, ("init_q_lookup_names(): out of memory\n"));
659                         return;
660                 }
661         } else {
662                 q_l->uni_name = NULL;
663                 q_l->hdr_name = NULL;
664         }
665
666         for (i = 0; i < num_names; i++) {
667                 init_unistr2(&q_l->uni_name[i], names[i], UNI_FLAGS_NONE);
668                 init_uni_hdr(&q_l->hdr_name[i], &q_l->uni_name[i]);
669         }
670 }
671
672 /*******************************************************************
673 reads or writes a structure.
674 ********************************************************************/
675
676 bool lsa_io_q_lookup_names(const char *desc, LSA_Q_LOOKUP_NAMES *q_r, 
677                            prs_struct *ps, int depth)
678 {
679         unsigned int i;
680
681         prs_debug(ps, depth, desc, "lsa_io_q_lookup_names");
682         depth++;
683
684         if(!prs_align(ps))
685                 return False;
686
687         if(!smb_io_pol_hnd("", &q_r->pol, ps, depth)) /* policy handle */
688                 return False;
689
690         if(!prs_align(ps))
691                 return False;
692         if(!prs_uint32("num_entries    ", ps, depth, &q_r->num_entries))
693                 return False;
694         if(!prs_uint32("num_entries2   ", ps, depth, &q_r->num_entries2))
695                 return False;
696
697         if (UNMARSHALLING(ps)) {
698                 if (q_r->num_entries) {
699                         if ((q_r->hdr_name = PRS_ALLOC_MEM(ps, UNIHDR, q_r->num_entries)) == NULL)
700                                 return False;
701                         if ((q_r->uni_name = PRS_ALLOC_MEM(ps, UNISTR2, q_r->num_entries)) == NULL)
702                                 return False;
703                 }
704         }
705
706         for (i = 0; i < q_r->num_entries; i++) {
707                 if(!prs_align(ps))
708                         return False;
709                 if(!smb_io_unihdr("hdr_name", &q_r->hdr_name[i], ps, depth)) /* pointer names */
710                         return False;
711         }
712
713         for (i = 0; i < q_r->num_entries; i++) {
714                 if(!prs_align(ps))
715                         return False;
716                 if(!smb_io_unistr2("dom_name", &q_r->uni_name[i], q_r->hdr_name[i].buffer, ps, depth)) /* names to be looked up */
717                         return False;
718         }
719
720         if(!prs_align(ps))
721                 return False;
722         if(!prs_uint32("num_trans_entries ", ps, depth, &q_r->num_trans_entries))
723                 return False;
724         if(!prs_uint32("ptr_trans_sids ", ps, depth, &q_r->ptr_trans_sids))
725                 return False;
726         if(!prs_uint16("lookup_level   ", ps, depth, &q_r->lookup_level))
727                 return False;
728         if(!prs_align(ps))
729                 return False;
730         if(!prs_uint32("mapped_count   ", ps, depth, &q_r->mapped_count))
731                 return False;
732
733         return True;
734 }
735
736 /*******************************************************************
737 reads or writes a structure.
738 ********************************************************************/
739
740 bool lsa_io_r_lookup_names(const char *desc, LSA_R_LOOKUP_NAMES *out, prs_struct *ps, int depth)
741 {
742         unsigned int i;
743
744         prs_debug(ps, depth, desc, "lsa_io_r_lookup_names");
745         depth++;
746
747         if(!prs_align(ps))
748                 return False;
749
750         if(!prs_uint32("ptr_dom_ref", ps, depth, &out->ptr_dom_ref))
751                 return False;
752
753         if (out->ptr_dom_ref != 0)
754                 if(!lsa_io_dom_r_ref("", out->dom_ref, ps, depth))
755                         return False;
756
757         if(!prs_uint32("num_entries", ps, depth, &out->num_entries))
758                 return False;
759         if(!prs_uint32("ptr_entries", ps, depth, &out->ptr_entries))
760                 return False;
761
762         if (out->ptr_entries != 0) {
763                 if(!prs_uint32("num_entries2", ps, depth, &out->num_entries2))
764                         return False;
765
766                 if (out->num_entries2 != out->num_entries) {
767                         /* RPC fault */
768                         return False;
769                 }
770
771                 if (UNMARSHALLING(ps) && out->num_entries2) {
772                         if ((out->dom_rid = PRS_ALLOC_MEM(ps, DOM_RID, out->num_entries2))
773                             == NULL) {
774                                 DEBUG(3, ("lsa_io_r_lookup_names(): out of memory\n"));
775                                 return False;
776                         }
777                 }
778
779                 for (i = 0; i < out->num_entries2; i++)
780                         if(!smb_io_dom_rid("", &out->dom_rid[i], ps, depth)) /* domain RIDs being looked up */
781                                 return False;
782         }
783
784         if(!prs_uint32("mapped_count", ps, depth, &out->mapped_count))
785                 return False;
786
787         if(!prs_ntstatus("status      ", ps, depth, &out->status))
788                 return False;
789
790         return True;
791 }
792
793 /*******************************************************************
794 reads or writes a structure.
795 ********************************************************************/
796
797 bool lsa_io_q_lookup_names2(const char *desc, LSA_Q_LOOKUP_NAMES2 *q_r, 
798                            prs_struct *ps, int depth)
799 {
800         unsigned int i;
801
802         prs_debug(ps, depth, desc, "lsa_io_q_lookup_names2");
803         depth++;
804
805         if(!prs_align(ps))
806                 return False;
807
808         if(!smb_io_pol_hnd("", &q_r->pol, ps, depth)) /* policy handle */
809                 return False;
810
811         if(!prs_align(ps))
812                 return False;
813         if(!prs_uint32("num_entries    ", ps, depth, &q_r->num_entries))
814                 return False;
815         if(!prs_uint32("num_entries2   ", ps, depth, &q_r->num_entries2))
816                 return False;
817
818         if (UNMARSHALLING(ps)) {
819                 if (q_r->num_entries) {
820                         if ((q_r->hdr_name = PRS_ALLOC_MEM(ps, UNIHDR, q_r->num_entries)) == NULL)
821                                 return False;
822                         if ((q_r->uni_name = PRS_ALLOC_MEM(ps, UNISTR2, q_r->num_entries)) == NULL)
823                                 return False;
824                 }
825         }
826
827         for (i = 0; i < q_r->num_entries; i++) {
828                 if(!prs_align(ps))
829                         return False;
830                 if(!smb_io_unihdr("hdr_name", &q_r->hdr_name[i], ps, depth)) /* pointer names */
831                         return False;
832         }
833
834         for (i = 0; i < q_r->num_entries; i++) {
835                 if(!prs_align(ps))
836                         return False;
837                 if(!smb_io_unistr2("dom_name", &q_r->uni_name[i], q_r->hdr_name[i].buffer, ps, depth)) /* names to be looked up */
838                         return False;
839         }
840
841         if(!prs_align(ps))
842                 return False;
843         if(!prs_uint32("num_trans_entries ", ps, depth, &q_r->num_trans_entries))
844                 return False;
845         if(!prs_uint32("ptr_trans_sids ", ps, depth, &q_r->ptr_trans_sids))
846                 return False;
847         if(!prs_uint16("lookup_level   ", ps, depth, &q_r->lookup_level))
848                 return False;
849         if(!prs_align(ps))
850                 return False;
851         if(!prs_uint32("mapped_count   ", ps, depth, &q_r->mapped_count))
852                 return False;
853         if(!prs_uint32("unknown1   ", ps, depth, &q_r->unknown1))
854                 return False;
855         if(!prs_uint32("unknown2   ", ps, depth, &q_r->unknown2))
856                 return False;
857
858         return True;
859 }
860
861 /*******************************************************************
862 reads or writes a structure.
863 ********************************************************************/
864
865 bool lsa_io_r_lookup_names2(const char *desc, LSA_R_LOOKUP_NAMES2 *out, prs_struct *ps, int depth)
866 {
867         unsigned int i;
868
869         prs_debug(ps, depth, desc, "lsa_io_r_lookup_names2");
870         depth++;
871
872         if(!prs_align(ps))
873                 return False;
874
875         if(!prs_uint32("ptr_dom_ref", ps, depth, &out->ptr_dom_ref))
876                 return False;
877
878         if (out->ptr_dom_ref != 0)
879                 if(!lsa_io_dom_r_ref("", out->dom_ref, ps, depth))
880                         return False;
881
882         if(!prs_uint32("num_entries", ps, depth, &out->num_entries))
883                 return False;
884         if(!prs_uint32("ptr_entries", ps, depth, &out->ptr_entries))
885                 return False;
886
887         if (out->ptr_entries != 0) {
888                 if(!prs_uint32("num_entries2", ps, depth, &out->num_entries2))
889                         return False;
890
891                 if (out->num_entries2 != out->num_entries) {
892                         /* RPC fault */
893                         return False;
894                 }
895
896                 if (UNMARSHALLING(ps) && out->num_entries2) {
897                         if ((out->dom_rid = PRS_ALLOC_MEM(ps, DOM_RID2, out->num_entries2))
898                             == NULL) {
899                                 DEBUG(3, ("lsa_io_r_lookup_names2(): out of memory\n"));
900                                 return False;
901                         }
902                 }
903
904                 for (i = 0; i < out->num_entries2; i++)
905                         if(!smb_io_dom_rid2("", &out->dom_rid[i], ps, depth)) /* domain RIDs being looked up */
906                                 return False;
907         }
908
909         if(!prs_uint32("mapped_count", ps, depth, &out->mapped_count))
910                 return False;
911
912         if(!prs_ntstatus("status      ", ps, depth, &out->status))
913                 return False;
914
915         return True;
916 }
917
918 /*******************************************************************
919  Internal lsa data type io.
920  Following pass must read DOM_SID2 types.
921 ********************************************************************/
922
923 bool smb_io_lsa_translated_sids3(const char *desc, LSA_TRANSLATED_SID3 *q_r, 
924                            prs_struct *ps, int depth)
925 {
926         prs_debug(ps, depth, desc, "smb_io_lsa_translated_sids3");
927         depth++;
928
929         if(!prs_align(ps))
930                 return False;
931         if(!prs_uint8 ("sid_type ", ps, depth, &q_r->sid_type ))
932                 return False;
933         if(!prs_align(ps))
934                 return False;
935         /* Second pass will read/write these. */
936         if (!smb_io_dom_sid2_p("sid_header", ps, depth, &q_r->sid2))
937                 return False;
938         if(!prs_uint32("sid_idx ", ps, depth, &q_r->sid_idx ))
939                 return False;
940         if(!prs_uint32("unknown ", ps, depth, &q_r->unknown ))
941                 return False;
942         
943         return True;
944 }
945
946 /*******************************************************************
947  Identical to lsa_io_q_lookup_names2.
948 ********************************************************************/
949
950 bool lsa_io_q_lookup_names3(const char *desc, LSA_Q_LOOKUP_NAMES3 *q_r, 
951                            prs_struct *ps, int depth)
952 {
953         unsigned int i;
954
955         prs_debug(ps, depth, desc, "lsa_io_q_lookup_names3");
956         depth++;
957
958         if(!prs_align(ps))
959                 return False;
960
961         if(!smb_io_pol_hnd("", &q_r->pol, ps, depth)) /* policy handle */
962                 return False;
963
964         if(!prs_align(ps))
965                 return False;
966         if(!prs_uint32("num_entries    ", ps, depth, &q_r->num_entries))
967                 return False;
968         if(!prs_uint32("num_entries2   ", ps, depth, &q_r->num_entries2))
969                 return False;
970
971         if (UNMARSHALLING(ps)) {
972                 if (q_r->num_entries) {
973                         if ((q_r->hdr_name = PRS_ALLOC_MEM(ps, UNIHDR, q_r->num_entries)) == NULL)
974                                 return False;
975                         if ((q_r->uni_name = PRS_ALLOC_MEM(ps, UNISTR2, q_r->num_entries)) == NULL)
976                                 return False;
977                 }
978         }
979
980         for (i = 0; i < q_r->num_entries; i++) {
981                 if(!prs_align(ps))
982                         return False;
983                 if(!smb_io_unihdr("hdr_name", &q_r->hdr_name[i], ps, depth)) /* pointer names */
984                         return False;
985         }
986
987         for (i = 0; i < q_r->num_entries; i++) {
988                 if(!prs_align(ps))
989                         return False;
990                 if(!smb_io_unistr2("dom_name", &q_r->uni_name[i], q_r->hdr_name[i].buffer, ps, depth)) /* names to be looked up */
991                         return False;
992         }
993
994         if(!prs_align(ps))
995                 return False;
996         if(!prs_uint32("num_trans_entries ", ps, depth, &q_r->num_trans_entries))
997                 return False;
998         if(!prs_uint32("ptr_trans_sids ", ps, depth, &q_r->ptr_trans_sids))
999                 return False;
1000         if(!prs_uint16("lookup_level   ", ps, depth, &q_r->lookup_level))
1001                 return False;
1002         if(!prs_align(ps))
1003                 return False;
1004         if(!prs_uint32("mapped_count   ", ps, depth, &q_r->mapped_count))
1005                 return False;
1006         if(!prs_uint32("unknown1   ", ps, depth, &q_r->unknown1))
1007                 return False;
1008         if(!prs_uint32("unknown2   ", ps, depth, &q_r->unknown2))
1009                 return False;
1010
1011         return True;
1012 }
1013
1014 /*******************************************************************
1015 reads or writes a structure.
1016 ********************************************************************/
1017
1018 bool lsa_io_r_lookup_names3(const char *desc, LSA_R_LOOKUP_NAMES3 *out, prs_struct *ps, int depth)
1019 {
1020         unsigned int i;
1021
1022         prs_debug(ps, depth, desc, "lsa_io_r_lookup_names3");
1023         depth++;
1024
1025         if(!prs_align(ps))
1026                 return False;
1027
1028         if(!prs_uint32("ptr_dom_ref", ps, depth, &out->ptr_dom_ref))
1029                 return False;
1030
1031         if (out->ptr_dom_ref != 0)
1032                 if(!lsa_io_dom_r_ref("", out->dom_ref, ps, depth))
1033                         return False;
1034
1035         if(!prs_uint32("num_entries", ps, depth, &out->num_entries))
1036                 return False;
1037         if(!prs_uint32("ptr_entries", ps, depth, &out->ptr_entries))
1038                 return False;
1039
1040         if (out->ptr_entries != 0) {
1041                 if(!prs_uint32("num_entries2", ps, depth, &out->num_entries2))
1042                         return False;
1043
1044                 if (out->num_entries2 != out->num_entries) {
1045                         /* RPC fault */
1046                         return False;
1047                 }
1048
1049                 if (UNMARSHALLING(ps) && out->num_entries2) {
1050                         if ((out->trans_sids = PRS_ALLOC_MEM(ps, LSA_TRANSLATED_SID3, out->num_entries2))
1051                             == NULL) {
1052                                 DEBUG(3, ("lsa_io_r_lookup_names3(): out of memory\n"));
1053                                 return False;
1054                         }
1055                 }
1056
1057                 for (i = 0; i < out->num_entries2; i++) {
1058                         if(!smb_io_lsa_translated_sids3("", &out->trans_sids[i], ps, depth)) {
1059                                 return False;
1060                         }
1061                 }
1062                 /* Now process the DOM_SID2 entries. */
1063                 for (i = 0; i < out->num_entries2; i++) {
1064                         if (out->trans_sids[i].sid2) {
1065                                 if( !smb_io_dom_sid2("sid2", out->trans_sids[i].sid2, ps, depth) ) {
1066                                         return False;
1067                                 }
1068                         }
1069                 }
1070         }
1071
1072         if(!prs_uint32("mapped_count", ps, depth, &out->mapped_count))
1073                 return False;
1074
1075         if(!prs_ntstatus("status      ", ps, depth, &out->status))
1076                 return False;
1077
1078         return True;
1079 }
1080
1081 /*******************************************************************
1082 ********************************************************************/
1083
1084 bool lsa_io_q_lookup_names4(const char *desc, LSA_Q_LOOKUP_NAMES4 *q_r, 
1085                            prs_struct *ps, int depth)
1086 {
1087         unsigned int i;
1088
1089         prs_debug(ps, depth, desc, "lsa_io_q_lookup_names4");
1090         depth++;
1091
1092         if(!prs_align(ps))
1093                 return False;
1094
1095         if(!prs_uint32("num_entries    ", ps, depth, &q_r->num_entries))
1096                 return False;
1097         if(!prs_uint32("num_entries2   ", ps, depth, &q_r->num_entries2))
1098                 return False;
1099
1100         if (UNMARSHALLING(ps)) {
1101                 if (q_r->num_entries) {
1102                         if ((q_r->hdr_name = PRS_ALLOC_MEM(ps, UNIHDR, q_r->num_entries)) == NULL)
1103                                 return False;
1104                         if ((q_r->uni_name = PRS_ALLOC_MEM(ps, UNISTR2, q_r->num_entries)) == NULL)
1105                                 return False;
1106                 }
1107         }
1108
1109         for (i = 0; i < q_r->num_entries; i++) {
1110                 if(!prs_align(ps))
1111                         return False;
1112                 if(!smb_io_unihdr("hdr_name", &q_r->hdr_name[i], ps, depth)) /* pointer names */
1113                         return False;
1114         }
1115
1116         for (i = 0; i < q_r->num_entries; i++) {
1117                 if(!prs_align(ps))
1118                         return False;
1119                 if(!smb_io_unistr2("dom_name", &q_r->uni_name[i], q_r->hdr_name[i].buffer, ps, depth)) /* names to be looked up */
1120                         return False;
1121         }
1122
1123         if(!prs_align(ps))
1124                 return False;
1125         if(!prs_uint32("num_trans_entries ", ps, depth, &q_r->num_trans_entries))
1126                 return False;
1127         if(!prs_uint32("ptr_trans_sids ", ps, depth, &q_r->ptr_trans_sids))
1128                 return False;
1129         if(!prs_uint16("lookup_level   ", ps, depth, &q_r->lookup_level))
1130                 return False;
1131         if(!prs_align(ps))
1132                 return False;
1133         if(!prs_uint32("mapped_count   ", ps, depth, &q_r->mapped_count))
1134                 return False;
1135         if(!prs_uint32("unknown1   ", ps, depth, &q_r->unknown1))
1136                 return False;
1137         if(!prs_uint32("unknown2   ", ps, depth, &q_r->unknown2))
1138                 return False;
1139
1140         return True;
1141 }
1142
1143 /*******************************************************************
1144  Identical to lsa_io_r_lookup_names3.
1145 ********************************************************************/
1146
1147 bool lsa_io_r_lookup_names4(const char *desc, LSA_R_LOOKUP_NAMES4 *out, prs_struct *ps, int depth)
1148 {
1149         unsigned int i;
1150
1151         prs_debug(ps, depth, desc, "lsa_io_r_lookup_names4");
1152         depth++;
1153
1154         if(!prs_align(ps))
1155                 return False;
1156
1157         if(!prs_uint32("ptr_dom_ref", ps, depth, &out->ptr_dom_ref))
1158                 return False;
1159
1160         if (out->ptr_dom_ref != 0)
1161                 if(!lsa_io_dom_r_ref("", out->dom_ref, ps, depth))
1162                         return False;
1163
1164         if(!prs_uint32("num_entries", ps, depth, &out->num_entries))
1165                 return False;
1166         if(!prs_uint32("ptr_entries", ps, depth, &out->ptr_entries))
1167                 return False;
1168
1169         if (out->ptr_entries != 0) {
1170                 if(!prs_uint32("num_entries2", ps, depth, &out->num_entries2))
1171                         return False;
1172
1173                 if (out->num_entries2 != out->num_entries) {
1174                         /* RPC fault */
1175                         return False;
1176                 }
1177
1178                 if (UNMARSHALLING(ps) && out->num_entries2) {
1179                         if ((out->trans_sids = PRS_ALLOC_MEM(ps, LSA_TRANSLATED_SID3, out->num_entries2))
1180                             == NULL) {
1181                                 DEBUG(3, ("lsa_io_r_lookup_names4(): out of memory\n"));
1182                                 return False;
1183                         }
1184                 }
1185
1186                 for (i = 0; i < out->num_entries2; i++) {
1187                         if(!smb_io_lsa_translated_sids3("", &out->trans_sids[i], ps, depth)) {
1188                                 return False;
1189                         }
1190                 }
1191                 /* Now process the DOM_SID2 entries. */
1192                 for (i = 0; i < out->num_entries2; i++) {
1193                         if (out->trans_sids[i].sid2) {
1194                                 if( !smb_io_dom_sid2("sid2", out->trans_sids[i].sid2, ps, depth) ) {
1195                                         return False;
1196                                 }
1197                         }
1198                 }
1199         }
1200
1201         if(!prs_uint32("mapped_count", ps, depth, &out->mapped_count))
1202                 return False;
1203
1204         if(!prs_ntstatus("status      ", ps, depth, &out->status))
1205                 return False;
1206
1207         return True;
1208 }
1209
1210 /*******************************************************************
1211  Reads or writes an LUID structure.
1212 ********************************************************************/
1213
1214 static bool lsa_io_luid(const char *desc, LUID *out, prs_struct *ps, int depth)
1215 {
1216         prs_debug(ps, depth, desc, "lsa_io_luid");
1217         depth++;
1218
1219         if(!prs_align(ps))
1220                 return False;
1221  
1222         if(!prs_uint32("low", ps, depth, &out->low))
1223                 return False;
1224
1225         if(!prs_uint32("high", ps, depth, &out->high))
1226                 return False;
1227
1228         return True;
1229 }
1230
1231 /*******************************************************************
1232  Reads or writes an LUID_ATTR structure.
1233 ********************************************************************/
1234
1235 static bool lsa_io_luid_attr(const char *desc, LUID_ATTR *out, prs_struct *ps, int depth)
1236 {
1237         prs_debug(ps, depth, desc, "lsa_io_luid_attr");
1238         depth++;
1239
1240         if(!prs_align(ps))
1241                 return False;
1242  
1243         if (!lsa_io_luid(desc, &out->luid, ps, depth))
1244                 return False;
1245
1246         if(!prs_uint32("attr", ps, depth, &out->attr))
1247                 return False;
1248
1249         return True;
1250 }
1251
1252 /*******************************************************************
1253  Reads or writes an PRIVILEGE_SET structure.
1254 ********************************************************************/
1255
1256 static bool lsa_io_privilege_set(const char *desc, PRIVILEGE_SET *out, prs_struct *ps, int depth)
1257 {
1258         uint32 i, dummy;
1259
1260         prs_debug(ps, depth, desc, "lsa_io_privilege_set");
1261         depth++;
1262
1263         if(!prs_align(ps))
1264                 return False;
1265  
1266         if(!prs_uint32("count", ps, depth, &dummy))
1267                 return False;
1268         if(!prs_uint32("control", ps, depth, &out->control))
1269                 return False;
1270
1271         for (i=0; i<out->count; i++) {
1272                 if (!lsa_io_luid_attr(desc, &out->set[i], ps, depth))
1273                         return False;
1274         }
1275         
1276         return True;
1277 }
1278
1279 void init_lsa_string( LSA_STRING *uni, const char *string )
1280 {
1281         init_unistr2(&uni->unistring, string, UNI_FLAGS_NONE);
1282         init_uni_hdr(&uni->hdr, &uni->unistring);
1283 }
1284
1285 void init_lsa_q_lookup_priv_value(LSA_Q_LOOKUP_PRIV_VALUE *q_u, POLICY_HND *hnd, const char *name)
1286 {
1287         memcpy(&q_u->pol, hnd, sizeof(q_u->pol));
1288         init_lsa_string( &q_u->privname, name );
1289 }
1290
1291 bool smb_io_lsa_string( const char *desc, LSA_STRING *string, prs_struct *ps, int depth )
1292 {
1293         prs_debug(ps, depth, desc, "smb_io_lsa_string");
1294         depth++;
1295
1296         if(!smb_io_unihdr ("hdr", &string->hdr, ps, depth))
1297                 return False;
1298         if(!smb_io_unistr2("unistring", &string->unistring, string->hdr.buffer, ps, depth))
1299                 return False;
1300         
1301         return True;
1302 }
1303
1304 /*******************************************************************
1305  Reads or writes an LSA_Q_LOOKUP_PRIV_VALUE  structure.
1306 ********************************************************************/
1307
1308 bool lsa_io_q_lookup_priv_value(const char *desc, LSA_Q_LOOKUP_PRIV_VALUE  *out, prs_struct *ps, int depth)
1309 {
1310         prs_debug(ps, depth, desc, "lsa_io_q_lookup_priv_value");
1311         depth++;
1312
1313         if(!prs_align(ps))
1314                 return False;
1315  
1316         if(!smb_io_pol_hnd("pol", &out->pol, ps, depth))
1317                 return False;
1318         if(!smb_io_lsa_string("privname", &out->privname, ps, depth))
1319                 return False;
1320
1321         return True;
1322 }
1323
1324 /*******************************************************************
1325  Reads or writes an  LSA_R_LOOKUP_PRIV_VALUE structure.
1326 ********************************************************************/
1327
1328 bool lsa_io_r_lookup_priv_value(const char *desc, LSA_R_LOOKUP_PRIV_VALUE  *out, prs_struct *ps, int depth)
1329 {
1330         prs_debug(ps, depth, desc, "lsa_io_r_lookup_priv_value");
1331         depth++;
1332
1333         if(!prs_align(ps))
1334                 return False;
1335                 
1336         if(!lsa_io_luid("luid", &out->luid, ps, depth))
1337                 return False;
1338  
1339         if(!prs_ntstatus("status", ps, depth, &out->status))
1340                 return False;
1341
1342         return True;
1343 }
1344
1345
1346 /*******************************************************************
1347  Reads or writes an LSA_Q_ADDPRIVS structure.
1348 ********************************************************************/
1349
1350 bool lsa_io_q_addprivs(const char *desc, LSA_Q_ADDPRIVS *out, prs_struct *ps, int depth)
1351 {
1352         prs_debug(ps, depth, desc, "lsa_io_q_addprivs");
1353         depth++;
1354
1355         if(!prs_align(ps))
1356                 return False;
1357  
1358         if(!smb_io_pol_hnd("pol", &out->pol, ps, depth))
1359                 return False;
1360         
1361         if(!prs_uint32("count", ps, depth, &out->count))
1362                 return False;
1363
1364         if (UNMARSHALLING(ps) && out->count!=0) {
1365                 if (!NT_STATUS_IS_OK(privilege_set_init_by_ctx(ps->mem_ctx, &(out->set))))
1366                         return False;
1367                 
1368                 if (!(out->set.set = PRS_ALLOC_MEM(ps, LUID_ATTR, out->count)))
1369                         return False;
1370         }
1371         
1372         if(!lsa_io_privilege_set(desc, &out->set, ps, depth))
1373                 return False;
1374         
1375         return True;
1376 }
1377
1378 /*******************************************************************
1379  Reads or writes an LSA_R_ADDPRIVS structure.
1380 ********************************************************************/
1381
1382 bool lsa_io_r_addprivs(const char *desc, LSA_R_ADDPRIVS *out, prs_struct *ps, int depth)
1383 {
1384         prs_debug(ps, depth, desc, "lsa_io_r_addprivs");
1385         depth++;
1386
1387         if(!prs_align(ps))
1388                 return False;
1389  
1390         if(!prs_ntstatus("status", ps, depth, &out->status))
1391                 return False;
1392
1393         return True;
1394 }
1395
1396 /*******************************************************************
1397  Reads or writes an LSA_Q_REMOVEPRIVS structure.
1398 ********************************************************************/
1399
1400 bool lsa_io_q_removeprivs(const char *desc, LSA_Q_REMOVEPRIVS *out, prs_struct *ps, int depth)
1401 {
1402         prs_debug(ps, depth, desc, "lsa_io_q_removeprivs");
1403         depth++;
1404
1405         if(!prs_align(ps))
1406                 return False;
1407  
1408         if(!smb_io_pol_hnd("pol", &out->pol, ps, depth))
1409                 return False;
1410         
1411         if(!prs_uint32("allrights", ps, depth, &out->allrights))
1412                 return False;
1413
1414         if(!prs_uint32("ptr", ps, depth, &out->ptr))
1415                 return False;
1416
1417         /* 
1418          * JFM: I'm not sure at all if the count is inside the ptr
1419          * never seen one with ptr=0
1420          */
1421
1422         if (out->ptr!=0) {
1423                 if(!prs_uint32("count", ps, depth, &out->count))
1424                         return False;
1425
1426                 if (UNMARSHALLING(ps) && out->count!=0) {
1427                         if (!NT_STATUS_IS_OK(privilege_set_init_by_ctx(ps->mem_ctx, &(out->set))))
1428                                 return False;
1429
1430                         if (!(out->set.set = PRS_ALLOC_MEM(ps, LUID_ATTR, out->count)))
1431                                 return False;
1432                 }
1433
1434                 if(!lsa_io_privilege_set(desc, &out->set, ps, depth))
1435                         return False;
1436         }
1437
1438         return True;
1439 }
1440
1441 /*******************************************************************
1442  Reads or writes an LSA_R_REMOVEPRIVS structure.
1443 ********************************************************************/
1444
1445 bool lsa_io_r_removeprivs(const char *desc, LSA_R_REMOVEPRIVS *out, prs_struct *ps, int depth)
1446 {
1447         prs_debug(ps, depth, desc, "lsa_io_r_removeprivs");
1448         depth++;
1449
1450         if(!prs_align(ps))
1451                 return False;
1452  
1453         if(!prs_ntstatus("status", ps, depth, &out->status))
1454                 return False;
1455
1456         return True;
1457 }
1458
1459 bool policy_handle_is_valid(const POLICY_HND *hnd)
1460 {
1461         POLICY_HND zero_pol;
1462
1463         ZERO_STRUCT(zero_pol);
1464         return ((memcmp(&zero_pol, hnd, sizeof(POLICY_HND)) == 0) ? False : True );
1465 }
1466
1467 /*******************************************************************
1468  Inits an LSA_Q_ENUM_ACCT_RIGHTS structure.
1469 ********************************************************************/
1470 void init_q_enum_acct_rights(LSA_Q_ENUM_ACCT_RIGHTS *in, 
1471                              POLICY_HND *hnd, 
1472                              uint32 count, 
1473                              DOM_SID *sid)
1474 {
1475         DEBUG(5, ("init_q_enum_acct_rights\n"));
1476
1477         in->pol = *hnd;
1478         init_dom_sid2(&in->sid, sid);
1479 }
1480
1481 /*******************************************************************
1482 ********************************************************************/
1483 NTSTATUS init_r_enum_acct_rights( LSA_R_ENUM_ACCT_RIGHTS *out, PRIVILEGE_SET *privileges )
1484 {
1485         uint32 i;
1486         const char *privname;
1487         const char **privname_array = NULL;
1488         int num_priv = 0;
1489
1490         for ( i=0; i<privileges->count; i++ ) {
1491                 privname = luid_to_privilege_name( &privileges->set[i].luid );
1492                 if ( privname ) {
1493                         if ( !add_string_to_array( talloc_tos(), privname, &privname_array, &num_priv ) )
1494                                 return NT_STATUS_NO_MEMORY;
1495                 }
1496         }
1497
1498         if ( num_priv ) {
1499                 out->rights = TALLOC_P( talloc_tos(), UNISTR4_ARRAY );
1500                 if (!out->rights) {
1501                         return NT_STATUS_NO_MEMORY;
1502                 }
1503
1504                 if ( !init_unistr4_array( out->rights, num_priv, privname_array ) ) 
1505                         return NT_STATUS_NO_MEMORY;
1506
1507                 out->count = num_priv;
1508         }
1509
1510         return NT_STATUS_OK;
1511 }
1512
1513 /*******************************************************************
1514 reads or writes a LSA_Q_ENUM_ACCT_RIGHTS structure.
1515 ********************************************************************/
1516 bool lsa_io_q_enum_acct_rights(const char *desc, LSA_Q_ENUM_ACCT_RIGHTS *in, prs_struct *ps, int depth)
1517 {
1518         
1519         if (in == NULL)
1520                 return False;
1521
1522         prs_debug(ps, depth, desc, "lsa_io_q_enum_acct_rights");
1523         depth++;
1524
1525         if (!smb_io_pol_hnd("", &in->pol, ps, depth))
1526                 return False;
1527
1528         if(!smb_io_dom_sid2("sid", &in->sid, ps, depth))
1529                 return False;
1530
1531         return True;
1532 }
1533
1534
1535 /*******************************************************************
1536 reads or writes a LSA_R_ENUM_ACCT_RIGHTS structure.
1537 ********************************************************************/
1538 bool lsa_io_r_enum_acct_rights(const char *desc, LSA_R_ENUM_ACCT_RIGHTS *out, prs_struct *ps, int depth)
1539 {
1540         prs_debug(ps, depth, desc, "lsa_io_r_enum_acct_rights");
1541         depth++;
1542
1543         if(!prs_uint32("count   ", ps, depth, &out->count))
1544                 return False;
1545
1546         if ( !prs_pointer("rights", ps, depth, (void*)&out->rights, sizeof(UNISTR4_ARRAY), (PRS_POINTER_CAST)prs_unistr4_array) )
1547                 return False;
1548
1549         if(!prs_align(ps))
1550                 return False;
1551
1552         if(!prs_ntstatus("status", ps, depth, &out->status))
1553                 return False;
1554
1555         return True;
1556 }
1557
1558
1559 /*******************************************************************
1560  Inits an LSA_Q_ADD_ACCT_RIGHTS structure.
1561 ********************************************************************/
1562 void init_q_add_acct_rights( LSA_Q_ADD_ACCT_RIGHTS *in, POLICY_HND *hnd, 
1563                              DOM_SID *sid, uint32 count, const char **rights )
1564 {
1565         DEBUG(5, ("init_q_add_acct_rights\n"));
1566
1567         in->pol = *hnd;
1568         init_dom_sid2(&in->sid, sid);
1569         
1570         in->rights = TALLOC_P( talloc_tos(), UNISTR4_ARRAY );
1571         if (!in->rights) {
1572                 smb_panic("init_q_add_acct_rights: talloc fail\n");
1573                 return;
1574         }
1575         init_unistr4_array( in->rights, count, rights );
1576         
1577         in->count = count;
1578 }
1579
1580
1581 /*******************************************************************
1582 reads or writes a LSA_Q_ADD_ACCT_RIGHTS structure.
1583 ********************************************************************/
1584 bool lsa_io_q_add_acct_rights(const char *desc, LSA_Q_ADD_ACCT_RIGHTS *in, prs_struct *ps, int depth)
1585 {
1586         prs_debug(ps, depth, desc, "lsa_io_q_add_acct_rights");
1587         depth++;
1588
1589         if (!smb_io_pol_hnd("", &in->pol, ps, depth))
1590                 return False;
1591
1592         if(!smb_io_dom_sid2("sid", &in->sid, ps, depth))
1593                 return False;
1594
1595         if(!prs_uint32("count", ps, depth, &in->count))
1596                 return False;
1597
1598         if ( !prs_pointer("rights", ps, depth, (void*)&in->rights, sizeof(UNISTR4_ARRAY), (PRS_POINTER_CAST)prs_unistr4_array) )
1599                 return False;
1600
1601         return True;
1602 }
1603
1604 /*******************************************************************
1605 reads or writes a LSA_R_ENUM_ACCT_RIGHTS structure.
1606 ********************************************************************/
1607 bool lsa_io_r_add_acct_rights(const char *desc, LSA_R_ADD_ACCT_RIGHTS *out, prs_struct *ps, int depth)
1608 {
1609         prs_debug(ps, depth, desc, "lsa_io_r_add_acct_rights");
1610         depth++;
1611
1612         if(!prs_ntstatus("status", ps, depth, &out->status))
1613                 return False;
1614
1615         return True;
1616 }
1617
1618 /*******************************************************************
1619  Inits an LSA_Q_REMOVE_ACCT_RIGHTS structure.
1620 ********************************************************************/
1621
1622 void init_q_remove_acct_rights(LSA_Q_REMOVE_ACCT_RIGHTS *in, 
1623                                POLICY_HND *hnd, 
1624                                DOM_SID *sid,
1625                                uint32 removeall,
1626                                uint32 count, 
1627                                const char **rights)
1628 {
1629         DEBUG(5, ("init_q_remove_acct_rights\n"));
1630
1631         in->pol = *hnd;
1632
1633         init_dom_sid2(&in->sid, sid);
1634
1635         in->removeall = removeall;
1636         in->count = count;
1637
1638         in->rights = TALLOC_P( talloc_tos(), UNISTR4_ARRAY );
1639         if (!in->rights) {
1640                 smb_panic("init_q_remove_acct_rights: talloc fail\n");
1641                 return;
1642         }
1643         init_unistr4_array( in->rights, count, rights );
1644 }
1645
1646 /*******************************************************************
1647 reads or writes a LSA_Q_REMOVE_ACCT_RIGHTS structure.
1648 ********************************************************************/
1649
1650 bool lsa_io_q_remove_acct_rights(const char *desc, LSA_Q_REMOVE_ACCT_RIGHTS *in, prs_struct *ps, int depth)
1651 {
1652         prs_debug(ps, depth, desc, "lsa_io_q_remove_acct_rights");
1653         depth++;
1654
1655         if (!smb_io_pol_hnd("", &in->pol, ps, depth))
1656                 return False;
1657
1658         if(!smb_io_dom_sid2("sid", &in->sid, ps, depth))
1659                 return False;
1660
1661         if(!prs_uint32("removeall", ps, depth, &in->removeall))
1662                 return False;
1663
1664         if(!prs_uint32("count", ps, depth, &in->count))
1665                 return False;
1666
1667         if ( !prs_pointer("rights", ps, depth, (void*)&in->rights, sizeof(UNISTR4_ARRAY), (PRS_POINTER_CAST)prs_unistr4_array) )
1668                 return False;
1669
1670         return True;
1671 }
1672
1673 /*******************************************************************
1674 reads or writes a LSA_R_ENUM_ACCT_RIGHTS structure.
1675 ********************************************************************/
1676 bool lsa_io_r_remove_acct_rights(const char *desc, LSA_R_REMOVE_ACCT_RIGHTS *out, prs_struct *ps, int depth)
1677 {
1678         prs_debug(ps, depth, desc, "lsa_io_r_remove_acct_rights");
1679         depth++;
1680
1681         if(!prs_ntstatus("status", ps, depth, &out->status))
1682                 return False;
1683
1684         return True;
1685 }