Move policy_handle_is_valid out of parse_lsa.c
[gd/samba/.git] / source / rpc_parse / parse_misc.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) Gerald (Jerry) Carter             2005
8  *  
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 3 of the License, or
12  *  (at your option) any later version.
13  *  
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *  
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_PARSE
27
28 /*******************************************************************
29  Reads or writes a UTIME type.
30 ********************************************************************/
31
32 static bool smb_io_utime(const char *desc, UTIME *t, prs_struct *ps, int depth)
33 {
34         if (t == NULL)
35                 return False;
36
37         prs_debug(ps, depth, desc, "smb_io_utime");
38         depth++;
39
40         if(!prs_align(ps))
41                 return False;
42         
43         if(!prs_uint32 ("time", ps, depth, &t->time))
44                 return False;
45
46         return True;
47 }
48
49 /*******************************************************************
50  Reads or writes an NTTIME structure.
51 ********************************************************************/
52
53 bool smb_io_time(const char *desc, NTTIME *nttime, prs_struct *ps, int depth)
54 {
55         uint32 low, high;
56         if (nttime == NULL)
57                 return False;
58
59         prs_debug(ps, depth, desc, "smb_io_time");
60         depth++;
61
62         if(!prs_align(ps))
63                 return False;
64         
65         if (MARSHALLING(ps)) {
66                 low = *nttime & 0xFFFFFFFF;
67                 high = *nttime >> 32;
68         }
69         
70         if(!prs_uint32("low ", ps, depth, &low)) /* low part */
71                 return False;
72         if(!prs_uint32("high", ps, depth, &high)) /* high part */
73                 return False;
74
75         if (UNMARSHALLING(ps)) {
76                 *nttime = (((uint64_t)high << 32) + low);
77         }
78
79         return True;
80 }
81
82 /*******************************************************************
83  Reads or writes an NTTIME structure.
84 ********************************************************************/
85
86 bool smb_io_nttime(const char *desc, prs_struct *ps, int depth, NTTIME *nttime)
87 {
88         return smb_io_time( desc, nttime, ps, depth );
89 }
90
91 /*******************************************************************
92  Gets an enumeration handle from an ENUM_HND structure.
93 ********************************************************************/
94
95 uint32 get_enum_hnd(ENUM_HND *enh)
96 {
97         return (enh && enh->ptr_hnd != 0) ? enh->handle : 0;
98 }
99
100 /*******************************************************************
101  Inits an ENUM_HND structure.
102 ********************************************************************/
103
104 void init_enum_hnd(ENUM_HND *enh, uint32 hnd)
105 {
106         DEBUG(5,("smb_io_enum_hnd\n"));
107
108         enh->ptr_hnd = (hnd != 0) ? 1 : 0;
109         enh->handle = hnd;
110 }
111
112 /*******************************************************************
113  Reads or writes an ENUM_HND structure.
114 ********************************************************************/
115
116 bool smb_io_enum_hnd(const char *desc, ENUM_HND *hnd, prs_struct *ps, int depth)
117 {
118         if (hnd == NULL)
119                 return False;
120
121         prs_debug(ps, depth, desc, "smb_io_enum_hnd");
122         depth++;
123
124         if(!prs_align(ps))
125                 return False;
126         
127         if(!prs_uint32("ptr_hnd", ps, depth, &hnd->ptr_hnd)) /* pointer */
128                 return False;
129
130         if (hnd->ptr_hnd != 0) {
131                 if(!prs_uint32("handle ", ps, depth, &hnd->handle )) /* enum handle */
132                         return False;
133         }
134
135         return True;
136 }
137
138 /*******************************************************************
139  Reads or writes a DOM_SID structure.
140 ********************************************************************/
141
142 bool smb_io_dom_sid(const char *desc, DOM_SID *sid, prs_struct *ps, int depth)
143 {
144         int i;
145
146         if (sid == NULL)
147                 return False;
148
149         prs_debug(ps, depth, desc, "smb_io_dom_sid");
150         depth++;
151
152         if(!prs_uint8 ("sid_rev_num", ps, depth, &sid->sid_rev_num))
153                 return False;
154
155         if(!prs_uint8 ("num_auths  ", ps, depth, &sid->num_auths))
156                 return False;
157
158         for (i = 0; i < 6; i++)
159         {
160                 fstring tmp;
161                 slprintf(tmp, sizeof(tmp) - 1, "id_auth[%d] ", i);
162                 if(!prs_uint8 (tmp, ps, depth, &sid->id_auth[i]))
163                         return False;
164         }
165
166         /* oops! XXXX should really issue a warning here... */
167         if (sid->num_auths > MAXSUBAUTHS)
168                 sid->num_auths = MAXSUBAUTHS;
169
170         if(!prs_uint32s(False, "sub_auths ", ps, depth, sid->sub_auths, sid->num_auths))
171                 return False;
172
173         return True;
174 }
175
176 /*******************************************************************
177  Inits a DOM_SID2 structure.
178 ********************************************************************/
179
180 void init_dom_sid2(DOM_SID2 *sid2, const DOM_SID *sid)
181 {
182         sid2->sid = *sid;
183         sid2->num_auths = sid2->sid.num_auths;
184 }
185
186 /*******************************************************************
187  Reads or writes a DOM_SID2 structure.
188 ********************************************************************/
189
190 bool smb_io_dom_sid2_p(const char *desc, prs_struct *ps, int depth, DOM_SID2 **sid2)
191 {
192         uint32 data_p;
193
194         /* caputure the pointer value to stream */
195
196         data_p = *sid2 ? 0xf000baaa : 0;
197
198         if ( !prs_uint32("dom_sid2_p", ps, depth, &data_p ))
199                 return False;
200
201         /* we're done if there is no data */
202
203         if ( !data_p )
204                 return True;
205
206         if (UNMARSHALLING(ps)) {
207                 if ( !(*sid2 = PRS_ALLOC_MEM(ps, DOM_SID2, 1)) )
208                         return False;
209         }
210
211         return True;
212 }
213 /*******************************************************************
214  Reads or writes a DOM_SID2 structure.
215 ********************************************************************/
216
217 bool smb_io_dom_sid2(const char *desc, DOM_SID2 *sid, prs_struct *ps, int depth)
218 {
219         if (sid == NULL)
220                 return False;
221
222         prs_debug(ps, depth, desc, "smb_io_dom_sid2");
223         depth++;
224
225         if(!prs_align(ps))
226                 return False;
227         
228         if(!prs_uint32("num_auths", ps, depth, &sid->num_auths))
229                 return False;
230
231         if(!smb_io_dom_sid("sid", &sid->sid, ps, depth))
232                 return False;
233
234         return True;
235 }
236
237 /*******************************************************************
238  Reads or writes a struct GUID
239 ********************************************************************/
240
241 bool smb_io_uuid(const char *desc, struct GUID *uuid, 
242                  prs_struct *ps, int depth)
243 {
244         if (uuid == NULL)
245                 return False;
246
247         prs_debug(ps, depth, desc, "smb_io_uuid");
248         depth++;
249
250         if(!prs_uint32 ("data   ", ps, depth, &uuid->time_low))
251                 return False;
252         if(!prs_uint16 ("data   ", ps, depth, &uuid->time_mid))
253                 return False;
254         if(!prs_uint16 ("data   ", ps, depth, &uuid->time_hi_and_version))
255                 return False;
256
257         if(!prs_uint8s (False, "data   ", ps, depth, uuid->clock_seq, sizeof(uuid->clock_seq)))
258                 return False;
259         if(!prs_uint8s (False, "data   ", ps, depth, uuid->node, sizeof(uuid->node)))
260                 return False;
261
262         return True;
263 }
264
265 /*******************************************************************
266 creates a STRHDR structure.
267 ********************************************************************/
268
269 void init_str_hdr(STRHDR *hdr, int max_len, int len, uint32 buffer)
270 {
271         hdr->str_max_len = max_len;
272         hdr->str_str_len = len;
273         hdr->buffer      = buffer;
274 }
275
276 /*******************************************************************
277  Reads or writes a STRHDR structure.
278 ********************************************************************/
279
280 bool smb_io_strhdr(const char *desc,  STRHDR *hdr, prs_struct *ps, int depth)
281 {
282         if (hdr == NULL)
283                 return False;
284
285         prs_debug(ps, depth, desc, "smb_io_strhdr");
286         depth++;
287
288         prs_align(ps);
289         
290         if(!prs_uint16("str_str_len", ps, depth, &hdr->str_str_len))
291                 return False;
292         if(!prs_uint16("str_max_len", ps, depth, &hdr->str_max_len))
293                 return False;
294         if(!prs_uint32("buffer     ", ps, depth, &hdr->buffer))
295                 return False;
296
297         return True;
298 }
299
300 /*******************************************************************
301  Inits a UNIHDR structure.
302 ********************************************************************/
303
304 void init_uni_hdr(UNIHDR *hdr, UNISTR2 *str2)
305 {
306         hdr->uni_str_len = 2 * (str2->uni_str_len);
307         hdr->uni_max_len = 2 * (str2->uni_max_len);
308         hdr->buffer = (str2->uni_str_len != 0) ? 1 : 0;
309 }
310
311 /*******************************************************************
312  Reads or writes a UNIHDR structure.
313 ********************************************************************/
314
315 bool smb_io_unihdr(const char *desc, UNIHDR *hdr, prs_struct *ps, int depth)
316 {
317         if (hdr == NULL)
318                 return False;
319
320         prs_debug(ps, depth, desc, "smb_io_unihdr");
321         depth++;
322
323         if(!prs_align(ps))
324                 return False;
325         
326         if(!prs_uint16("uni_str_len", ps, depth, &hdr->uni_str_len))
327                 return False;
328         if(!prs_uint16("uni_max_len", ps, depth, &hdr->uni_max_len))
329                 return False;
330         if(!prs_uint32("buffer     ", ps, depth, &hdr->buffer))
331                 return False;
332
333         return True;
334 }
335
336 /*******************************************************************
337  Inits a BUFHDR structure.
338 ********************************************************************/
339
340 void init_buf_hdr(BUFHDR *hdr, int max_len, int len)
341 {
342         hdr->buf_max_len = max_len;
343         hdr->buf_len     = len;
344 }
345
346 /*******************************************************************
347  prs_uint16 wrapper. Call this and it sets up a pointer to where the
348  uint16 should be stored, or gets the size if reading.
349  ********************************************************************/
350
351 bool smb_io_hdrbuf_pre(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth, uint32 *offset)
352 {
353         (*offset) = prs_offset(ps);
354         if (ps->io) {
355
356                 /* reading. */
357
358                 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
359                         return False;
360
361         } else {
362
363                 /* writing. */
364
365                 if(!prs_set_offset(ps, prs_offset(ps) + (sizeof(uint32) * 2)))
366                         return False;
367         }
368
369         return True;
370 }
371
372 /*******************************************************************
373  smb_io_hdrbuf wrapper. Call this and it retrospectively stores the size.
374  Does nothing on reading, as that is already handled by ...._pre()
375  ********************************************************************/
376
377 bool smb_io_hdrbuf_post(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth, 
378                                 uint32 ptr_hdrbuf, uint32 max_len, uint32 len)
379 {
380         if (!ps->io) {
381                 /* writing: go back and do a retrospective job.  i hate this */
382
383                 uint32 old_offset = prs_offset(ps);
384
385                 init_buf_hdr(hdr, max_len, len);
386                 if(!prs_set_offset(ps, ptr_hdrbuf))
387                         return False;
388                 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
389                         return False;
390
391                 if(!prs_set_offset(ps, old_offset))
392                         return False;
393         }
394
395         return True;
396 }
397
398 /*******************************************************************
399  Reads or writes a BUFHDR structure.
400 ********************************************************************/
401
402 bool smb_io_hdrbuf(const char *desc, BUFHDR *hdr, prs_struct *ps, int depth)
403 {
404         if (hdr == NULL)
405                 return False;
406
407         prs_debug(ps, depth, desc, "smb_io_hdrbuf");
408         depth++;
409
410         if(!prs_align(ps))
411                 return False;
412         
413         if(!prs_uint32("buf_max_len", ps, depth, &hdr->buf_max_len))
414                 return False;
415         if(!prs_uint32("buf_len    ", ps, depth, &hdr->buf_len))
416                 return False;
417
418         return True;
419 }
420
421 /*******************************************************************
422  Inits a UNISTR structure.
423 ********************************************************************/
424
425 void init_unistr(UNISTR *str, const char *buf)
426 {
427         size_t len;
428
429         if (buf == NULL) {
430                 str->buffer = NULL;
431                 return;
432         }
433
434         len = rpcstr_push_talloc(talloc_tos(), &str->buffer, buf);
435         if (len == (size_t)-1) {
436                 str->buffer = NULL;
437         }
438 }
439
440 /*******************************************************************
441 reads or writes a UNISTR structure.
442 XXXX NOTE: UNISTR structures NEED to be null-terminated.
443 ********************************************************************/
444
445 bool smb_io_unistr(const char *desc, UNISTR *uni, prs_struct *ps, int depth)
446 {
447         if (uni == NULL)
448                 return False;
449
450         prs_debug(ps, depth, desc, "smb_io_unistr");
451         depth++;
452
453         if(!prs_unistr("unistr", ps, depth, uni))
454                 return False;
455
456         return True;
457 }
458
459 /*******************************************************************
460  Allocate the RPC_DATA_BLOB memory.
461 ********************************************************************/
462
463 static void create_rpc_blob(RPC_DATA_BLOB *str, size_t len)
464 {
465         if (len) {
466                 str->buffer = (uint8 *)TALLOC_ZERO(talloc_tos(), len);
467                 if (str->buffer == NULL)
468                         smb_panic("create_rpc_blob: talloc fail");
469                 str->buf_len = len;
470         } else {
471                 str->buffer = NULL;
472                 str->buf_len = 0;
473         }
474 }
475
476 /*******************************************************************
477  Inits a RPC_DATA_BLOB structure from a uint32
478 ********************************************************************/
479
480 void init_rpc_blob_uint32(RPC_DATA_BLOB *str, uint32 val)
481 {
482         ZERO_STRUCTP(str);
483
484         /* set up string lengths. */
485         create_rpc_blob(str, sizeof(uint32));
486         SIVAL(str->buffer, 0, val);
487 }
488
489 /*******************************************************************
490  Inits a RPC_DATA_BLOB structure.
491 ********************************************************************/
492
493 void init_rpc_blob_str(RPC_DATA_BLOB *str, const char *buf, int len)
494 {
495         ZERO_STRUCTP(str);
496
497         /* set up string lengths. */
498         if (len) {
499                 create_rpc_blob(str, len*2);
500                 rpcstr_push(str->buffer, buf, (size_t)str->buf_len, STR_TERMINATE);
501         }
502 }
503
504 /*******************************************************************
505  Inits a RPC_DATA_BLOB structure from a hex string.
506 ********************************************************************/
507
508 void init_rpc_blob_hex(RPC_DATA_BLOB *str, const char *buf)
509 {
510         ZERO_STRUCTP(str);
511         if (buf && *buf) {
512                 size_t len = strlen(buf);
513                 create_rpc_blob(str, len);
514                 str->buf_len = strhex_to_str((char *)str->buffer, str->buf_len,
515                                 buf, len);
516         }
517 }
518
519 /*******************************************************************
520  Inits a RPC_DATA_BLOB structure.
521 ********************************************************************/
522
523 void init_rpc_blob_bytes(RPC_DATA_BLOB *str, uint8 *buf, size_t len)
524 {
525         ZERO_STRUCTP(str);
526
527         /* max buffer size (allocated size) */
528         if (buf != NULL && len) {
529                 create_rpc_blob(str, len);
530                 memcpy(str->buffer, buf, len);
531         }
532         str->buf_len = len;
533 }
534
535 /*******************************************************************
536 reads or writes a BUFFER5 structure.
537 the buf_len member tells you how large the buffer is.
538 ********************************************************************/
539 bool smb_io_buffer5(const char *desc, BUFFER5 *buf5, prs_struct *ps, int depth)
540 {
541         prs_debug(ps, depth, desc, "smb_io_buffer5");
542         depth++;
543
544         if (buf5 == NULL) return False;
545
546         if(!prs_align(ps))
547                 return False;
548         if(!prs_uint32("buf_len", ps, depth, &buf5->buf_len))
549                 return False;
550
551         if(buf5->buf_len) {
552                 if(!prs_buffer5(True, "buffer" , ps, depth, buf5))
553                         return False;
554         }
555
556         return True;
557 }
558
559 /*******************************************************************
560  Inits a REGVAL_BUFFER structure.
561 ********************************************************************/
562
563 void init_regval_buffer(REGVAL_BUFFER *str, const uint8 *buf, size_t len)
564 {
565         ZERO_STRUCTP(str);
566
567         /* max buffer size (allocated size) */
568         str->buf_max_len = len;
569         str->offset = 0;
570         str->buf_len = buf != NULL ? len : 0;
571
572         if (buf != NULL) {
573                 SMB_ASSERT(str->buf_max_len >= str->buf_len);
574                 str->buffer = (uint16 *)TALLOC_ZERO(talloc_tos(),
575                                                     str->buf_max_len);
576                 if (str->buffer == NULL)
577                         smb_panic("init_regval_buffer: talloc fail");
578                 memcpy(str->buffer, buf, str->buf_len);
579         }
580 }
581
582 /*******************************************************************
583  Reads or writes a REGVAL_BUFFER structure.
584    the uni_max_len member tells you how large the buffer is.
585    the uni_str_len member tells you how much of the buffer is really used.
586 ********************************************************************/
587
588 bool smb_io_regval_buffer(const char *desc, prs_struct *ps, int depth, REGVAL_BUFFER *buf2)
589 {
590
591         prs_debug(ps, depth, desc, "smb_io_regval_buffer");
592         depth++;
593
594         if(!prs_align(ps))
595                 return False;
596                 
597         if(!prs_uint32("buf_max_len", ps, depth, &buf2->buf_max_len))
598                 return False;
599         if(!prs_uint32("offset     ", ps, depth, &buf2->offset))
600                 return False;
601         if(!prs_uint32("buf_len    ", ps, depth, &buf2->buf_len))
602                 return False;
603
604         /* buffer advanced by indicated length of string
605            NOT by searching for null-termination */
606
607         if(!prs_regval_buffer(True, "buffer     ", ps, depth, buf2))
608                 return False;
609
610         return True;
611 }
612
613 /*******************************************************************
614 creates a UNISTR2 structure: sets up the buffer, too
615 ********************************************************************/
616
617 void init_buf_unistr2(UNISTR2 *str, uint32 *ptr, const char *buf)
618 {
619         if (buf != NULL) {
620                 *ptr = 1;
621                 init_unistr2(str, buf, UNI_STR_TERMINATE);
622         } else {
623                 *ptr = 0;
624                 init_unistr2(str, NULL, UNI_FLAGS_NONE);
625
626         }
627 }
628
629 /*******************************************************************
630  Copies a UNISTR2 structure.
631 ********************************************************************/
632
633 void copy_unistr2(UNISTR2 *str, const UNISTR2 *from)
634 {
635         if (from->buffer == NULL) {
636                 ZERO_STRUCTP(str);
637                 return;
638         }
639
640         SMB_ASSERT(from->uni_max_len >= from->uni_str_len);
641
642         str->uni_max_len = from->uni_max_len;
643         str->offset      = from->offset;
644         str->uni_str_len = from->uni_str_len;
645
646         /* the string buffer is allocated to the maximum size
647            (the the length of the source string) to prevent
648            reallocation of memory. */
649         if (str->buffer == NULL) {
650                 if (str->uni_max_len) {
651                         str->buffer = (uint16 *)TALLOC_ZERO_ARRAY(talloc_tos(), uint16, str->uni_max_len);
652                         if ((str->buffer == NULL)) {
653                                 smb_panic("copy_unistr2: talloc fail");
654                                 return;
655                         }
656                         /* copy the string */
657                         memcpy(str->buffer, from->buffer, str->uni_max_len*sizeof(uint16));
658                 } else {
659                         str->buffer = NULL;
660                 }
661         }
662 }
663
664 /*******************************************************************
665  Creates a STRING2 structure.
666 ********************************************************************/
667
668 void init_string2(STRING2 *str, const char *buf, size_t max_len, size_t str_len)
669 {
670         /* set up string lengths. */
671         SMB_ASSERT(max_len >= str_len);
672
673         /* Ensure buf is valid if str_len was set. Coverity check. */
674         if (str_len && !buf) {
675                 return;
676         }
677
678         str->str_max_len = max_len;
679         str->offset = 0;
680         str->str_str_len = str_len;
681
682         /* store the string */
683         if(str_len != 0) {
684                 str->buffer = (uint8 *)TALLOC_ZERO(talloc_tos(),
685                                                    str->str_max_len);
686                 if (str->buffer == NULL)
687                         smb_panic("init_string2: malloc fail");
688                 memcpy(str->buffer, buf, str_len);
689         }
690 }
691
692 /*******************************************************************
693  Reads or writes a STRING2 structure.
694  XXXX NOTE: STRING2 structures need NOT be null-terminated.
695    the str_str_len member tells you how long the string is;
696    the str_max_len member tells you how large the buffer is.
697 ********************************************************************/
698
699 bool smb_io_string2(const char *desc, STRING2 *str2, uint32 buffer, prs_struct *ps, int depth)
700 {
701         if (str2 == NULL)
702                 return False;
703
704         if (buffer) {
705
706                 prs_debug(ps, depth, desc, "smb_io_string2");
707                 depth++;
708
709                 if(!prs_align(ps))
710                         return False;
711                 
712                 if(!prs_uint32("str_max_len", ps, depth, &str2->str_max_len))
713                         return False;
714                 if(!prs_uint32("offset     ", ps, depth, &str2->offset))
715                         return False;
716                 if(!prs_uint32("str_str_len", ps, depth, &str2->str_str_len))
717                         return False;
718
719                 /* buffer advanced by indicated length of string
720                    NOT by searching for null-termination */
721                 if(!prs_string2(True, "buffer     ", ps, depth, str2))
722                         return False;
723
724         } else {
725
726                 prs_debug(ps, depth, desc, "smb_io_string2 - NULL");
727                 depth++;
728                 memset((char *)str2, '\0', sizeof(*str2));
729
730         }
731
732         return True;
733 }
734
735 /*******************************************************************
736  Inits a UNISTR2 structure.
737 ********************************************************************/
738
739 void init_unistr2(UNISTR2 *str, const char *buf, enum unistr2_term_codes flags)
740 {
741         size_t len = 0;
742         uint32 num_chars = 0;
743
744         if (buf) {
745                 /* We always null terminate the copy. */
746                 len = strlen(buf) + 1;
747                 if ( flags == UNI_STR_DBLTERMINATE )
748                         len++;
749         }
750
751         if (buf == NULL || len == 0) {
752                 /* no buffer -- nothing to do */
753                 str->uni_max_len = 0;
754                 str->offset = 0;
755                 str->uni_str_len = 0;
756
757                 return;
758         }
759         
760
761         str->buffer = TALLOC_ZERO_ARRAY(talloc_tos(), uint16, len);
762         if (str->buffer == NULL) {
763                 smb_panic("init_unistr2: malloc fail");
764                 return;
765         }
766
767         /* Ensure len is the length in *bytes* */
768         len *= sizeof(uint16);
769
770         /*
771          * The UNISTR2 must be initialized !!!
772          * jfm, 7/7/2001.
773          */
774         if (buf) {
775                 rpcstr_push((char *)str->buffer, buf, len, STR_TERMINATE);
776                 num_chars = strlen_w(str->buffer);
777                 if (flags == UNI_STR_TERMINATE || flags == UNI_MAXLEN_TERMINATE) {
778                         num_chars++;
779                 }
780                 if ( flags == UNI_STR_DBLTERMINATE )
781                         num_chars += 2;
782         }
783
784         str->uni_max_len = num_chars;
785         str->offset = 0;
786         str->uni_str_len = num_chars;
787         if ( num_chars && ((flags == UNI_MAXLEN_TERMINATE) || (flags == UNI_BROKEN_NON_NULL)) )
788                 str->uni_max_len++;
789 }
790
791 /*******************************************************************
792  Inits a UNISTR4 structure.
793 ********************************************************************/
794
795 void init_unistr4(UNISTR4 *uni4, const char *buf, enum unistr2_term_codes flags)
796 {
797         uni4->string = TALLOC_P( talloc_tos(), UNISTR2 );
798         if (!uni4->string) {
799                 smb_panic("init_unistr4: talloc fail");
800                 return;
801         }
802         init_unistr2( uni4->string, buf, flags );
803
804         uni4->length = 2 * (uni4->string->uni_str_len);
805         uni4->size   = 2 * (uni4->string->uni_max_len);
806 }
807
808 void init_unistr4_w( TALLOC_CTX *ctx, UNISTR4 *uni4, const smb_ucs2_t *buf )
809 {
810         uni4->string = TALLOC_P( ctx, UNISTR2 );
811         if (!uni4->string) {
812                 smb_panic("init_unistr4_w: talloc fail");
813                 return;
814         }
815         init_unistr2_w( ctx, uni4->string, buf );
816
817         uni4->length = 2 * (uni4->string->uni_str_len);
818         uni4->size   = 2 * (uni4->string->uni_max_len);
819 }
820
821 /** 
822  *  Inits a UNISTR2 structure.
823  *  @param  ctx talloc context to allocate string on
824  *  @param  str pointer to string to create
825  *  @param  buf UCS2 null-terminated buffer to init from
826 */
827
828 void init_unistr2_w(TALLOC_CTX *ctx, UNISTR2 *str, const smb_ucs2_t *buf)
829 {
830         uint32 len = buf ? strlen_w(buf) : 0;
831
832         ZERO_STRUCTP(str);
833
834         /* set up string lengths. */
835         str->uni_max_len = len;
836         str->offset = 0;
837         str->uni_str_len = len;
838
839         if (len + 1) {
840                 str->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, len + 1);
841                 if (str->buffer == NULL) {
842                         smb_panic("init_unistr2_w: talloc fail");
843                         return;
844                 }
845         } else {
846                 str->buffer = NULL;
847         }
848         
849         /*
850          * don't move this test above ! The UNISTR2 must be initialized !!!
851          * jfm, 7/7/2001.
852          */
853         if (buf==NULL)
854                 return;
855         
856         /* Yes, this is a strncpy( foo, bar, strlen(bar)) - but as
857            long as the buffer above is talloc()ed correctly then this
858            is the correct thing to do */
859         if (len+1) {
860                 strncpy_w(str->buffer, buf, len + 1);
861         }
862 }
863
864 /*******************************************************************
865  Inits a UNISTR2 structure from a UNISTR
866 ********************************************************************/
867
868 void init_unistr2_from_unistr(TALLOC_CTX *ctx, UNISTR2 *to, const UNISTR *from)
869 {
870         uint32 i;
871
872         /* the destination UNISTR2 should never be NULL.
873            if it is it is a programming error */
874
875         /* if the source UNISTR is NULL, then zero out
876            the destination string and return */
877         ZERO_STRUCTP (to);
878         if ((from == NULL) || (from->buffer == NULL))
879                 return;
880
881         /* get the length; UNISTR must be NULL terminated */
882         i = 0;
883         while ((from->buffer)[i]!='\0')
884                 i++;
885         i++;    /* one more to catch the terminating NULL */
886                 /* is this necessary -- jerry?  I need to think */
887
888         /* set up string lengths; uni_max_len is set to i+1
889            because we need to account for the final NULL termination */
890         to->uni_max_len = i;
891         to->offset = 0;
892         to->uni_str_len = i;
893
894         /* allocate the space and copy the string buffer */
895         if (i) {
896                 to->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, i);
897                 if (to->buffer == NULL)
898                         smb_panic("init_unistr2_from_unistr: talloc fail");
899                 memcpy(to->buffer, from->buffer, i*sizeof(uint16));
900         } else {
901                 to->buffer = NULL;
902         }
903         return;
904 }
905
906 /*******************************************************************
907   Inits a UNISTR2 structure from a DATA_BLOB.
908   The length of the data_blob must count the bytes of the buffer.
909   Copies the blob data.
910 ********************************************************************/
911
912 void init_unistr2_from_datablob(UNISTR2 *str, DATA_BLOB *blob) 
913 {
914         /* Allocs the unistring */
915         init_unistr2(str, NULL, UNI_FLAGS_NONE);
916         
917         /* Sets the values */
918         str->uni_str_len = blob->length / sizeof(uint16);
919         str->uni_max_len = str->uni_str_len;
920         str->offset = 0;
921         if (blob->length) {
922                 str->buffer = (uint16 *) memdup(blob->data, blob->length);
923         } else {
924                 str->buffer = NULL;
925         }
926         if ((str->buffer == NULL) && (blob->length > 0)) {
927                 smb_panic("init_unistr2_from_datablob: malloc fail");
928         }
929 }
930
931 /*******************************************************************
932  UNISTR2* are a little different in that the pointer and the UNISTR2
933  are not necessarily read/written back to back.  So we break it up 
934  into 2 separate functions.
935  See SPOOL_USER_1 in include/rpc_spoolss.h for an example.
936 ********************************************************************/
937
938 bool prs_io_unistr2_p(const char *desc, prs_struct *ps, int depth, UNISTR2 **uni2)
939 {
940         uint32 data_p;
941
942         /* caputure the pointer value to stream */
943
944         data_p = *uni2 ? 0xf000baaa : 0;
945
946         if ( !prs_uint32("ptr", ps, depth, &data_p ))
947                 return False;
948
949         /* we're done if there is no data */
950
951         if ( !data_p )
952                 return True;
953
954         if (UNMARSHALLING(ps)) {
955                 if ( !(*uni2 = PRS_ALLOC_MEM(ps, UNISTR2, 1)) )
956                         return False;
957         }
958
959         return True;
960 }
961
962 /*******************************************************************
963  now read/write the actual UNISTR2.  Memory for the UNISTR2 (but
964  not UNISTR2.buffer) has been allocated previously by prs_unistr2_p()
965 ********************************************************************/
966
967 bool prs_io_unistr2(const char *desc, prs_struct *ps, int depth, UNISTR2 *uni2 )
968 {
969         /* just return true if there is no pointer to deal with.
970            the memory must have been previously allocated on unmarshalling
971            by prs_unistr2_p() */
972
973         if ( !uni2 )
974                 return True;
975
976         /* just pass off to smb_io_unstr2() passing the uni2 address as 
977            the pointer (like you would expect) */
978
979         return smb_io_unistr2( desc, uni2, uni2 ? 1 : 0, ps, depth );
980 }
981
982 /*******************************************************************
983  Reads or writes a UNISTR2 structure.
984  XXXX NOTE: UNISTR2 structures need NOT be null-terminated.
985    the uni_str_len member tells you how long the string is;
986    the uni_max_len member tells you how large the buffer is.
987 ********************************************************************/
988
989 bool smb_io_unistr2(const char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
990 {
991         if (uni2 == NULL)
992                 return False;
993
994         if (buffer) {
995
996                 prs_debug(ps, depth, desc, "smb_io_unistr2");
997                 depth++;
998
999                 if(!prs_align(ps))
1000                         return False;
1001                 
1002                 if(!prs_uint32("uni_max_len", ps, depth, &uni2->uni_max_len))
1003                         return False;
1004                 if(!prs_uint32("offset     ", ps, depth, &uni2->offset))
1005                         return False;
1006                 if(!prs_uint32("uni_str_len", ps, depth, &uni2->uni_str_len))
1007                         return False;
1008
1009                 /* buffer advanced by indicated length of string
1010                    NOT by searching for null-termination */
1011                 if(!prs_unistr2(True, "buffer     ", ps, depth, uni2))
1012                         return False;
1013
1014         } else {
1015
1016                 prs_debug(ps, depth, desc, "smb_io_unistr2 - NULL");
1017                 depth++;
1018                 memset((char *)uni2, '\0', sizeof(*uni2));
1019
1020         }
1021
1022         return True;
1023 }
1024
1025 /*******************************************************************
1026  now read/write UNISTR4
1027 ********************************************************************/
1028
1029 bool prs_unistr4(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
1030 {
1031         void *ptr;
1032         prs_debug(ps, depth, desc, "prs_unistr4");
1033         depth++;
1034
1035         if ( !prs_uint16("length", ps, depth, &uni4->length ))
1036                 return False;
1037         if ( !prs_uint16("size", ps, depth, &uni4->size ))
1038                 return False;
1039                 
1040         ptr = uni4->string;
1041
1042         if ( !prs_pointer( desc, ps, depth, &ptr, sizeof(UNISTR2), (PRS_POINTER_CAST)prs_io_unistr2 ) )
1043                 return False;
1044
1045         uni4->string = (UNISTR2 *)ptr;
1046         
1047         return True;
1048 }
1049
1050 /*******************************************************************
1051  now read/write UNISTR4 header
1052 ********************************************************************/
1053
1054 bool prs_unistr4_hdr(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
1055 {
1056         prs_debug(ps, depth, desc, "prs_unistr4_hdr");
1057         depth++;
1058
1059         if ( !prs_uint16("length", ps, depth, &uni4->length) )
1060                 return False;
1061         if ( !prs_uint16("size", ps, depth, &uni4->size) )
1062                 return False;
1063         if ( !prs_io_unistr2_p(desc, ps, depth, &uni4->string) )
1064                 return False;
1065                 
1066         return True;
1067 }
1068
1069 /*******************************************************************
1070  now read/write UNISTR4 string
1071 ********************************************************************/
1072
1073 bool prs_unistr4_str(const char *desc, prs_struct *ps, int depth, UNISTR4 *uni4)
1074 {
1075         prs_debug(ps, depth, desc, "prs_unistr4_str");
1076         depth++;
1077
1078         if ( !prs_io_unistr2(desc, ps, depth, uni4->string) )
1079                 return False;
1080                 
1081         return True;
1082 }
1083
1084 /*******************************************************************
1085  Reads or writes a UNISTR4_ARRAY structure.
1086 ********************************************************************/
1087
1088 bool prs_unistr4_array(const char *desc, prs_struct *ps, int depth, UNISTR4_ARRAY *array )
1089 {
1090         unsigned int i;
1091
1092         prs_debug(ps, depth, desc, "prs_unistr4_array");
1093         depth++;
1094
1095         if(!prs_uint32("count", ps, depth, &array->count))
1096                 return False;
1097
1098         if (UNMARSHALLING(ps)) {
1099                 if (array->count) {
1100                         if ( !(array->strings = TALLOC_ZERO_ARRAY( talloc_tos(), UNISTR4, array->count)) )
1101                                 return False;
1102                 } else {
1103                         array->strings = NULL;
1104                 }
1105         }
1106         
1107         /* write the headers and then the actual string buffer */
1108         
1109         for ( i=0; i<array->count; i++ ) {
1110                 if ( !prs_unistr4_hdr( "string", ps, depth, &array->strings[i]) )
1111                         return False;
1112         }
1113
1114         for (i=0;i<array->count;i++) {
1115                 if ( !prs_unistr4_str("string", ps, depth, &array->strings[i]) ) 
1116                         return False;
1117         }
1118         
1119         return True;
1120 }
1121
1122 /********************************************************************
1123   initialise a UNISTR_ARRAY from a char**
1124 ********************************************************************/
1125
1126 bool init_unistr4_array( UNISTR4_ARRAY *array, uint32 count, const char **strings )
1127 {
1128         unsigned int i;
1129
1130         array->count = count;
1131
1132         /* allocate memory for the array of UNISTR4 objects */
1133
1134         if (array->count) {
1135                 if ( !(array->strings = TALLOC_ZERO_ARRAY(talloc_tos(), UNISTR4, count )) )
1136                         return False;
1137         } else {
1138                 array->strings = NULL;
1139         }
1140
1141         for ( i=0; i<count; i++ ) 
1142                 init_unistr4( &array->strings[i], strings[i], UNI_STR_TERMINATE );
1143
1144         return True;
1145 }
1146
1147 bool smb_io_lockout_string_hdr(const char *desc, HDR_LOCKOUT_STRING *hdr_account_lockout, prs_struct *ps, int depth)
1148 {
1149         prs_debug(ps, depth, desc, "smb_io_lockout_string_hdr");
1150         depth++;
1151
1152         if(!prs_align(ps))
1153                 return False;
1154
1155         if(!prs_uint16("size", ps, depth, &hdr_account_lockout->size))
1156                 return False;
1157         if(!prs_uint16("length", ps, depth, &hdr_account_lockout->length))
1158                 return False;
1159         if(!prs_uint32("buffer", ps, depth, &hdr_account_lockout->buffer))
1160                 return False;
1161
1162         return True;
1163 }
1164
1165 bool smb_io_account_lockout_str(const char *desc, LOCKOUT_STRING *account_lockout, uint32 buffer, prs_struct *ps, int depth)
1166 {
1167         prs_debug(ps, depth, desc, "smb_io_account_lockout_string");
1168         depth++;
1169
1170         if(!prs_uint32("array_size", ps, depth, &account_lockout->array_size))
1171                 return False;
1172
1173         if(!prs_uint32("offset", ps, depth, &account_lockout->offset))
1174                 return False;
1175         if(!prs_uint32("length", ps, depth, &account_lockout->length))
1176                 return False;
1177
1178         if (!prs_uint64("lockout_duration", ps, depth, &account_lockout->lockout_duration))
1179                 return False;
1180         if (!prs_uint64("reset_count", ps, depth, &account_lockout->reset_count))
1181                 return False;
1182         if (!prs_uint32("bad_attempt_lockout", ps, depth, &account_lockout->bad_attempt_lockout))
1183                 return False;
1184         if (!prs_uint32("dummy", ps, depth, &account_lockout->dummy))
1185                 return False;
1186 #if 0
1187         if(!prs_uint16s (False, "bindata", ps, depth, &account_lockout->bindata, length))
1188                 return False;
1189 #endif
1190
1191         return True;
1192 }
1193
1194 /*******************************************************************
1195  Inits a DOM_RID structure.
1196 ********************************************************************/
1197
1198 void init_dom_rid(DOM_RID *prid, uint32 rid, uint16 type, uint32 idx)
1199 {
1200         prid->type    = type;
1201         prid->rid     = rid;
1202         prid->rid_idx = idx;
1203 }
1204
1205 /*******************************************************************
1206  Reads or writes a DOM_RID structure.
1207 ********************************************************************/
1208
1209 bool smb_io_dom_rid(const char *desc, DOM_RID *rid, prs_struct *ps, int depth)
1210 {
1211         if (rid == NULL)
1212                 return False;
1213
1214         prs_debug(ps, depth, desc, "smb_io_dom_rid");
1215         depth++;
1216
1217         if(!prs_align(ps))
1218                 return False;
1219    
1220         if(!prs_uint16("type   ", ps, depth, &rid->type))
1221                 return False;
1222         if(!prs_align(ps))
1223                 return False;
1224         if(!prs_uint32("rid    ", ps, depth, &rid->rid))
1225                 return False;
1226         if(!prs_uint32("rid_idx", ps, depth, &rid->rid_idx))
1227                 return False;
1228
1229         return True;
1230 }
1231
1232 /*******************************************************************
1233  Reads or writes a DOM_RID2 structure.
1234 ********************************************************************/
1235
1236 bool smb_io_dom_rid2(const char *desc, DOM_RID2 *rid, prs_struct *ps, int depth)
1237 {
1238         if (rid == NULL)
1239                 return False;
1240
1241         prs_debug(ps, depth, desc, "smb_io_dom_rid2");
1242         depth++;
1243
1244         if(!prs_align(ps))
1245                 return False;
1246    
1247         if(!prs_uint16("type   ", ps, depth, &rid->type))
1248                 return False;
1249         if(!prs_align(ps))
1250                 return False;
1251         if(!prs_uint32("rid    ", ps, depth, &rid->rid))
1252                 return False;
1253         if(!prs_uint32("rid_idx", ps, depth, &rid->rid_idx))
1254                 return False;
1255         if(!prs_uint32("unknown", ps, depth, &rid->unknown))
1256                 return False;
1257
1258         return True;
1259 }
1260
1261
1262 /*******************************************************************
1263 creates a DOM_RID3 structure.
1264 ********************************************************************/
1265
1266 void init_dom_rid3(DOM_RID3 *rid3, uint32 rid, uint8 type)
1267 {
1268     rid3->rid      = rid;
1269     rid3->type1    = type;
1270     rid3->ptr_type = 0x1; /* non-zero, basically. */
1271     rid3->type2    = 0x1;
1272     rid3->unk      = type;
1273 }
1274
1275 /*******************************************************************
1276 reads or writes a DOM_RID3 structure.
1277 ********************************************************************/
1278
1279 bool smb_io_dom_rid3(const char *desc, DOM_RID3 *rid3, prs_struct *ps, int depth)
1280 {
1281         if (rid3 == NULL)
1282                 return False;
1283
1284         prs_debug(ps, depth, desc, "smb_io_dom_rid3");
1285         depth++;
1286
1287         if(!prs_align(ps))
1288                 return False;
1289
1290         if(!prs_uint32("rid     ", ps, depth, &rid3->rid))
1291                 return False;
1292         if(!prs_uint32("type1   ", ps, depth, &rid3->type1))
1293                 return False;
1294         if(!prs_uint32("ptr_type", ps, depth, &rid3->ptr_type))
1295                 return False;
1296         if(!prs_uint32("type2   ", ps, depth, &rid3->type2))
1297                 return False;
1298         if(!prs_uint32("unk     ", ps, depth, &rid3->unk))
1299                 return False;
1300
1301         return True;
1302 }
1303
1304 /*******************************************************************
1305  Inits a DOM_RID4 structure.
1306 ********************************************************************/
1307
1308 void init_dom_rid4(DOM_RID4 *rid4, uint16 unknown, uint16 attr, uint32 rid)
1309 {
1310     rid4->unknown = unknown;
1311     rid4->attr    = attr;
1312     rid4->rid     = rid;
1313 }
1314
1315 /*******************************************************************
1316  Inits a DOM_CLNT_SRV structure.
1317 ********************************************************************/
1318
1319 void init_clnt_srv(DOM_CLNT_SRV *logcln, const char *logon_srv,
1320                    const char *comp_name)
1321 {
1322         DEBUG(5,("init_clnt_srv: %d\n", __LINE__));
1323
1324         if (logon_srv != NULL) {
1325                 logcln->undoc_buffer = 1;
1326                 init_unistr2(&logcln->uni_logon_srv, logon_srv, UNI_STR_TERMINATE);
1327         } else {
1328                 logcln->undoc_buffer = 0;
1329         }
1330
1331         if (comp_name != NULL) {
1332                 logcln->undoc_buffer2 = 1;
1333                 init_unistr2(&logcln->uni_comp_name, comp_name, UNI_STR_TERMINATE);
1334         } else {
1335                 logcln->undoc_buffer2 = 0;
1336         }
1337 }
1338
1339 /*******************************************************************
1340  Inits or writes a DOM_CLNT_SRV structure.
1341 ********************************************************************/
1342
1343 bool smb_io_clnt_srv(const char *desc, DOM_CLNT_SRV *logcln, prs_struct *ps, int depth)
1344 {
1345         if (logcln == NULL)
1346                 return False;
1347
1348         prs_debug(ps, depth, desc, "smb_io_clnt_srv");
1349         depth++;
1350
1351         if(!prs_align(ps))
1352                 return False;
1353         
1354         if(!prs_uint32("undoc_buffer ", ps, depth, &logcln->undoc_buffer))
1355                 return False;
1356
1357         if (logcln->undoc_buffer != 0) {
1358                 if(!smb_io_unistr2("unistr2", &logcln->uni_logon_srv, logcln->undoc_buffer, ps, depth))
1359                         return False;
1360         }
1361
1362         if(!prs_align(ps))
1363                 return False;
1364
1365         if(!prs_uint32("undoc_buffer2", ps, depth, &logcln->undoc_buffer2))
1366                 return False;
1367
1368         if (logcln->undoc_buffer2 != 0) {
1369                 if(!smb_io_unistr2("unistr2", &logcln->uni_comp_name, logcln->undoc_buffer2, ps, depth))
1370                         return False;
1371         }
1372
1373         return True;
1374 }
1375
1376 /*******************************************************************
1377  Inits a DOM_LOG_INFO structure.
1378 ********************************************************************/
1379
1380 void init_log_info(DOM_LOG_INFO *loginfo, const char *logon_srv, const char *acct_name,
1381                 uint16 sec_chan, const char *comp_name)
1382 {
1383         DEBUG(5,("make_log_info %d\n", __LINE__));
1384
1385         loginfo->undoc_buffer = 1;
1386
1387         init_unistr2(&loginfo->uni_logon_srv, logon_srv, UNI_STR_TERMINATE);
1388         init_unistr2(&loginfo->uni_acct_name, acct_name, UNI_STR_TERMINATE);
1389
1390         loginfo->sec_chan = sec_chan;
1391
1392         init_unistr2(&loginfo->uni_comp_name, comp_name, UNI_STR_TERMINATE);
1393 }
1394
1395 /*******************************************************************
1396  Reads or writes a DOM_LOG_INFO structure.
1397 ********************************************************************/
1398
1399 bool smb_io_log_info(const char *desc, DOM_LOG_INFO *loginfo, prs_struct *ps, int depth)
1400 {
1401         if (loginfo == NULL)
1402                 return False;
1403
1404         prs_debug(ps, depth, desc, "smb_io_log_info");
1405         depth++;
1406
1407         if(!prs_align(ps))
1408                 return False;
1409         
1410         if(!prs_uint32("undoc_buffer", ps, depth, &loginfo->undoc_buffer))
1411                 return False;
1412
1413         if(!smb_io_unistr2("unistr2", &loginfo->uni_logon_srv, True, ps, depth))
1414                 return False;
1415         if(!smb_io_unistr2("unistr2", &loginfo->uni_acct_name, True, ps, depth))
1416                 return False;
1417
1418         if(!prs_uint16("sec_chan", ps, depth, &loginfo->sec_chan))
1419                 return False;
1420
1421         if(!smb_io_unistr2("unistr2", &loginfo->uni_comp_name, True, ps, depth))
1422                 return False;
1423
1424         return True;
1425 }
1426
1427 /*******************************************************************
1428  Reads or writes a DOM_CHAL structure.
1429 ********************************************************************/
1430
1431 bool smb_io_chal(const char *desc, DOM_CHAL *chal, prs_struct *ps, int depth)
1432 {
1433         if (chal == NULL)
1434                 return False;
1435
1436         prs_debug(ps, depth, desc, "smb_io_chal");
1437         depth++;
1438         
1439         if(!prs_uint8s (False, "data", ps, depth, chal->data, 8))
1440                 return False;
1441
1442         return True;
1443 }
1444
1445 /*******************************************************************
1446  Reads or writes a DOM_CRED structure.
1447 ********************************************************************/
1448
1449 bool smb_io_cred(const char *desc,  DOM_CRED *cred, prs_struct *ps, int depth)
1450 {
1451         if (cred == NULL)
1452                 return False;
1453
1454         prs_debug(ps, depth, desc, "smb_io_cred");
1455         depth++;
1456
1457         if(!prs_align(ps))
1458                 return False;
1459
1460         if(!smb_io_chal ("", &cred->challenge, ps, depth))
1461                 return False;
1462
1463         if(!smb_io_utime("", &cred->timestamp, ps, depth))
1464                 return False;
1465
1466         return True;
1467 }
1468
1469 /*******************************************************************
1470  Inits a DOM_CLNT_INFO2 structure.
1471 ********************************************************************/
1472
1473 void init_clnt_info2(DOM_CLNT_INFO2 *clnt,
1474                                 const char *logon_srv, const char *comp_name,
1475                                 const DOM_CRED *clnt_cred)
1476 {
1477         DEBUG(5,("make_clnt_info: %d\n", __LINE__));
1478
1479         init_clnt_srv(&clnt->login, logon_srv, comp_name);
1480
1481         if (clnt_cred != NULL) {
1482                 clnt->ptr_cred = 1;
1483                 memcpy(&clnt->cred, clnt_cred, sizeof(clnt->cred));
1484         } else {
1485                 clnt->ptr_cred = 0;
1486         }
1487 }
1488
1489 /*******************************************************************
1490  Reads or writes a DOM_CLNT_INFO2 structure.
1491 ********************************************************************/
1492
1493 bool smb_io_clnt_info2(const char *desc, DOM_CLNT_INFO2 *clnt, prs_struct *ps, int depth)
1494 {
1495         if (clnt == NULL)
1496                 return False;
1497
1498         prs_debug(ps, depth, desc, "smb_io_clnt_info2");
1499         depth++;
1500
1501         if(!prs_align(ps))
1502                 return False;
1503         
1504         if(!smb_io_clnt_srv("", &clnt->login, ps, depth))
1505                 return False;
1506
1507         if(!prs_align(ps))
1508                 return False;
1509         
1510         if(!prs_uint32("ptr_cred", ps, depth, &clnt->ptr_cred))
1511                 return False;
1512         if(!smb_io_cred("", &clnt->cred, ps, depth))
1513                 return False;
1514
1515         return True;
1516 }
1517
1518 /*******************************************************************
1519  Inits a DOM_CLNT_INFO structure.
1520 ********************************************************************/
1521
1522 void init_clnt_info(DOM_CLNT_INFO *clnt,
1523                 const char *logon_srv, const char *acct_name,
1524                 uint16 sec_chan, const char *comp_name,
1525                 const DOM_CRED *cred)
1526 {
1527         DEBUG(5,("make_clnt_info\n"));
1528
1529         init_log_info(&clnt->login, logon_srv, acct_name, sec_chan, comp_name);
1530         memcpy(&clnt->cred, cred, sizeof(clnt->cred));
1531 }
1532
1533 /*******************************************************************
1534  Reads or writes a DOM_CLNT_INFO structure.
1535 ********************************************************************/
1536
1537 bool smb_io_clnt_info(const char *desc,  DOM_CLNT_INFO *clnt, prs_struct *ps, int depth)
1538 {
1539         if (clnt == NULL)
1540                 return False;
1541
1542         prs_debug(ps, depth, desc, "smb_io_clnt_info");
1543         depth++;
1544
1545         if(!prs_align(ps))
1546                 return False;
1547         
1548         if(!smb_io_log_info("", &clnt->login, ps, depth))
1549                 return False;
1550         if(!smb_io_cred("", &clnt->cred, ps, depth))
1551                 return False;
1552
1553         return True;
1554 }
1555
1556 /*******************************************************************
1557  Inits a DOM_LOGON_ID structure.
1558 ********************************************************************/
1559
1560 void init_logon_id(DOM_LOGON_ID *logonid, uint32 log_id_low, uint32 log_id_high)
1561 {
1562         DEBUG(5,("make_logon_id: %d\n", __LINE__));
1563
1564         logonid->low  = log_id_low;
1565         logonid->high = log_id_high;
1566 }
1567
1568 /*******************************************************************
1569  Reads or writes a DOM_LOGON_ID structure.
1570 ********************************************************************/
1571
1572 bool smb_io_logon_id(const char *desc, DOM_LOGON_ID *logonid, prs_struct *ps, int depth)
1573 {
1574         if (logonid == NULL)
1575                 return False;
1576
1577         prs_debug(ps, depth, desc, "smb_io_logon_id");
1578         depth++;
1579
1580         if(!prs_align(ps))
1581                 return False;
1582         
1583         if(!prs_uint32("low ", ps, depth, &logonid->low ))
1584                 return False;
1585         if(!prs_uint32("high", ps, depth, &logonid->high))
1586                 return False;
1587
1588         return True;
1589 }
1590
1591 /*******************************************************************
1592  Inits an OWF_INFO structure.
1593 ********************************************************************/
1594
1595 void init_owf_info(OWF_INFO *hash, const uint8 data[16])
1596 {
1597         DEBUG(5,("init_owf_info: %d\n", __LINE__));
1598         
1599         if (data != NULL)
1600                 memcpy(hash->data, data, sizeof(hash->data));
1601         else
1602                 memset((char *)hash->data, '\0', sizeof(hash->data));
1603 }
1604
1605 /*******************************************************************
1606  Reads or writes an OWF_INFO structure.
1607 ********************************************************************/
1608
1609 bool smb_io_owf_info(const char *desc, OWF_INFO *hash, prs_struct *ps, int depth)
1610 {
1611         if (hash == NULL)
1612                 return False;
1613
1614         prs_debug(ps, depth, desc, "smb_io_owf_info");
1615         depth++;
1616
1617         if(!prs_align(ps))
1618                 return False;
1619         
1620         if(!prs_uint8s (False, "data", ps, depth, hash->data, 16))
1621                 return False;
1622
1623         return True;
1624 }
1625
1626 /*******************************************************************
1627  Reads or writes a DOM_GID structure.
1628 ********************************************************************/
1629
1630 bool smb_io_gid(const char *desc,  DOM_GID *gid, prs_struct *ps, int depth)
1631 {
1632         if (gid == NULL)
1633                 return False;
1634
1635         prs_debug(ps, depth, desc, "smb_io_gid");
1636         depth++;
1637
1638         if(!prs_align(ps))
1639                 return False;
1640         
1641         if(!prs_uint32("g_rid", ps, depth, &gid->g_rid))
1642                 return False;
1643         if(!prs_uint32("attr ", ps, depth, &gid->attr))
1644                 return False;
1645
1646         return True;
1647 }
1648
1649 /*******************************************************************
1650  Reads or writes an POLICY_HND structure.
1651 ********************************************************************/
1652
1653 bool smb_io_pol_hnd(const char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
1654 {
1655         if (pol == NULL)
1656                 return False;
1657
1658         prs_debug(ps, depth, desc, "smb_io_pol_hnd");
1659         depth++;
1660
1661         if(!prs_align(ps))
1662                 return False;
1663
1664         if(UNMARSHALLING(ps))
1665                 ZERO_STRUCTP(pol);
1666         
1667         if (!prs_uint32("handle_type", ps, depth, &pol->handle_type))
1668                 return False;
1669         if (!smb_io_uuid("uuid", (struct GUID*)&pol->uuid, ps, depth))
1670                 return False;
1671
1672         return True;
1673 }
1674
1675 /*******************************************************************
1676  Create a UNISTR3.
1677 ********************************************************************/
1678
1679 void init_unistr3(UNISTR3 *str, const char *buf)
1680 {
1681         if (buf == NULL) {
1682                 str->uni_str_len=0;
1683                 str->str.buffer = NULL;
1684                 return;
1685         }
1686
1687         str->uni_str_len = strlen(buf) + 1;
1688
1689         if (str->uni_str_len) {
1690                 str->str.buffer = TALLOC_ZERO_ARRAY(talloc_tos(), uint16, str->uni_str_len);
1691                 if (str->str.buffer == NULL)
1692                         smb_panic("init_unistr3: malloc fail");
1693
1694                 rpcstr_push((char *)str->str.buffer, buf, str->uni_str_len * sizeof(uint16), STR_TERMINATE);
1695         } else {
1696                 str->str.buffer = NULL;
1697         }
1698 }
1699
1700 /*******************************************************************
1701  Reads or writes a UNISTR3 structure.
1702 ********************************************************************/
1703
1704 bool smb_io_unistr3(const char *desc, UNISTR3 *name, prs_struct *ps, int depth)
1705 {
1706         if (name == NULL)
1707                 return False;
1708
1709         prs_debug(ps, depth, desc, "smb_io_unistr3");
1710         depth++;
1711
1712         if(!prs_align(ps))
1713                 return False;
1714         
1715         if(!prs_uint32("uni_str_len", ps, depth, &name->uni_str_len))
1716                 return False;
1717                 
1718         /* we're done if there is no string */
1719         
1720         if ( name->uni_str_len == 0 )
1721                 return True;
1722
1723         /* don't know if len is specified by uni_str_len member... */
1724         /* assume unicode string is unicode-null-terminated, instead */
1725
1726         if(!prs_unistr3(True, "unistr", name, ps, depth))
1727                 return False;
1728
1729         return True;
1730 }
1731
1732 /*******************************************************************
1733  Stream a uint64_struct
1734  ********************************************************************/
1735 bool prs_uint64(const char *name, prs_struct *ps, int depth, uint64 *data64)
1736 {
1737         if (UNMARSHALLING(ps)) {
1738                 uint32 high, low;
1739
1740                 if (!prs_uint32(name, ps, depth+1, &low))
1741                         return False;
1742
1743                 if (!prs_uint32(name, ps, depth+1, &high))
1744                         return False;
1745
1746                 *data64 = ((uint64_t)high << 32) + low;
1747
1748                 return True;
1749         } else {
1750                 uint32 high = (*data64) >> 32, low = (*data64) & 0xFFFFFFFF;
1751                 return prs_uint32(name, ps, depth+1, &low) && 
1752                            prs_uint32(name, ps, depth+1, &high);
1753         }
1754 }
1755
1756 /*******************************************************************
1757 reads or writes a BUFHDR2 structure.
1758 ********************************************************************/
1759 bool smb_io_bufhdr2(const char *desc, BUFHDR2 *hdr, prs_struct *ps, int depth)
1760 {
1761         prs_debug(ps, depth, desc, "smb_io_bufhdr2");
1762         depth++;
1763
1764         prs_align(ps);
1765         prs_uint32("info_level", ps, depth, &(hdr->info_level));
1766         prs_uint32("length    ", ps, depth, &(hdr->length    ));
1767         prs_uint32("buffer    ", ps, depth, &(hdr->buffer    ));
1768
1769         return True;
1770 }
1771
1772 /*******************************************************************
1773 reads or writes a BUFHDR4 structure.
1774 ********************************************************************/
1775 bool smb_io_bufhdr4(const char *desc, BUFHDR4 *hdr, prs_struct *ps, int depth)
1776 {
1777         prs_debug(ps, depth, desc, "smb_io_bufhdr4");
1778         depth++;
1779
1780         prs_align(ps);
1781         prs_uint32("size", ps, depth, &hdr->size);
1782         prs_uint32("buffer", ps, depth, &hdr->buffer);
1783
1784         return True;
1785 }
1786
1787 /*******************************************************************
1788 reads or writes a RPC_DATA_BLOB structure.
1789 ********************************************************************/
1790
1791 bool smb_io_rpc_blob(const char *desc, RPC_DATA_BLOB *blob, prs_struct *ps, int depth)
1792 {
1793         prs_debug(ps, depth, desc, "smb_io_rpc_blob");
1794         depth++;
1795
1796         prs_align(ps);
1797         if ( !prs_uint32("buf_len", ps, depth, &blob->buf_len) )
1798                 return False;
1799
1800         if ( blob->buf_len == 0 )
1801                 return True;
1802
1803         if (UNMARSHALLING(ps)) {
1804                 blob->buffer = PRS_ALLOC_MEM(ps, uint8, blob->buf_len);
1805                 if (!blob->buffer) {
1806                         return False;
1807                 }
1808         }
1809
1810         if ( !prs_uint8s(True, "buffer", ps, depth, blob->buffer, blob->buf_len) )
1811                 return False;
1812
1813         return True;
1814 }
1815
1816 /*******************************************************************
1817 creates a UNIHDR structure.
1818 ********************************************************************/
1819
1820 bool make_uni_hdr(UNIHDR *hdr, int len)
1821 {
1822         if (hdr == NULL)
1823         {
1824                 return False;
1825         }
1826         hdr->uni_str_len = 2 * len;
1827         hdr->uni_max_len = 2 * len;
1828         hdr->buffer      = len != 0 ? 1 : 0;
1829
1830         return True;
1831 }
1832
1833 /*******************************************************************
1834 creates a BUFHDR2 structure.
1835 ********************************************************************/
1836 bool make_bufhdr2(BUFHDR2 *hdr, uint32 info_level, uint32 length, uint32 buffer)
1837 {
1838         hdr->info_level = info_level;
1839         hdr->length     = length;
1840         hdr->buffer     = buffer;
1841
1842         return True;
1843 }
1844
1845 /*******************************************************************
1846 return the length of a UNISTR string.
1847 ********************************************************************/  
1848
1849 uint32 str_len_uni(UNISTR *source)
1850 {
1851         uint32 i=0;
1852
1853         if (!source->buffer)
1854                 return 0;
1855
1856         while (source->buffer[i])
1857                 i++;
1858
1859         return i;
1860 }
1861
1862 /*******************************************************************
1863  Verifies policy handle
1864 ********************************************************************/
1865
1866 bool policy_handle_is_valid(const POLICY_HND *hnd)
1867 {
1868         POLICY_HND zero_pol;
1869
1870         ZERO_STRUCT(zero_pol);
1871         return ((memcmp(&zero_pol, hnd, sizeof(POLICY_HND)) == 0) ? false : true );
1872 }