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