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