Fixed the problem with UNISTR marshalling in a buffer5 struct.
[kai/samba-autobuild/.git] / source / rpc_parse / parse_misc.c
1 /* 
2  *  Unix SMB/Netbios implementation.
3  *  Version 1.9.
4  *  RPC Pipe client / server routines
5  *  Copyright (C) Andrew Tridgell              1992-1997,
6  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
7  *  Copyright (C) Paul Ashton                       1997.
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 2 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, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24
25 #include "includes.h"
26
27 extern int DEBUGLEVEL;
28
29 static TALLOC_CTX *parse_misc_talloc = NULL;
30
31 /******************************************************************* a
32 free up temporary memory - called from the main loop
33 ********************************************************************/
34
35 void parse_talloc_free(void)
36 {
37     if (!parse_misc_talloc)
38         return;
39     talloc_destroy(parse_misc_talloc);
40     parse_misc_talloc = NULL;
41 }
42
43 /*******************************************************************
44  Reads or writes a UTIME type.
45 ********************************************************************/
46
47 static BOOL smb_io_utime(char *desc, UTIME *t, prs_struct *ps, int depth)
48 {
49         if (t == NULL)
50                 return False;
51
52         prs_debug(ps, depth, desc, "smb_io_utime");
53         depth++;
54
55         if(!prs_align(ps))
56                 return False;
57         
58         if(!prs_uint32 ("time", ps, depth, &t->time))
59                 return False;
60
61         return True;
62 }
63
64 /*******************************************************************
65  Reads or writes an NTTIME structure.
66 ********************************************************************/
67
68 BOOL smb_io_time(char *desc, NTTIME *nttime, prs_struct *ps, int depth)
69 {
70         if (nttime == NULL)
71                 return False;
72
73         prs_debug(ps, depth, desc, "smb_io_time");
74         depth++;
75
76         if(!prs_align(ps))
77                 return False;
78         
79         if(!prs_uint32("low ", ps, depth, &nttime->low)) /* low part */
80                 return False;
81         if(!prs_uint32("high", ps, depth, &nttime->high)) /* high part */
82                 return False;
83
84         return True;
85 }
86
87 /*******************************************************************
88  Reads or writes a LOOKUP_LEVEL structure.
89 ********************************************************************/
90
91 BOOL smb_io_lookup_level(char *desc, LOOKUP_LEVEL *level, prs_struct *ps, int depth)
92 {
93         if (level == NULL)
94                 return False;
95
96         prs_debug(ps, depth, desc, "smb_io_lookup_level");
97         depth++;
98
99         if(!prs_align(ps))
100                 return False;
101         if(!prs_uint16("value", ps, depth, &level->value))
102                 return False;
103         if(!prs_align(ps))
104                 return False;
105
106         return True;
107 }
108
109 /*******************************************************************
110  Gets an enumeration handle from an ENUM_HND structure.
111 ********************************************************************/
112
113 uint32 get_enum_hnd(ENUM_HND *enh)
114 {
115         return (enh && enh->ptr_hnd != 0) ? enh->handle : 0;
116 }
117
118 /*******************************************************************
119  Inits an ENUM_HND structure.
120 ********************************************************************/
121
122 void init_enum_hnd(ENUM_HND *enh, uint32 hnd)
123 {
124         DEBUG(5,("smb_io_enum_hnd\n"));
125
126         enh->ptr_hnd = (hnd != 0) ? 1 : 0;
127         enh->handle = hnd;
128 }
129
130 /*******************************************************************
131  Reads or writes an ENUM_HND structure.
132 ********************************************************************/
133
134 BOOL smb_io_enum_hnd(char *desc, ENUM_HND *hnd, prs_struct *ps, int depth)
135 {
136         if (hnd == NULL)
137                 return False;
138
139         prs_debug(ps, depth, desc, "smb_io_enum_hnd");
140         depth++;
141
142         if(!prs_align(ps))
143                 return False;
144         
145         if(!prs_uint32("ptr_hnd", ps, depth, &hnd->ptr_hnd)) /* pointer */
146                 return False;
147
148         if (hnd->ptr_hnd != 0) {
149                 if(!prs_uint32("handle ", ps, depth, &hnd->handle )) /* enum handle */
150                         return False;
151         }
152
153         return True;
154 }
155
156 /*******************************************************************
157  Reads or writes a DOM_SID structure.
158 ********************************************************************/
159
160 BOOL smb_io_dom_sid(char *desc, DOM_SID *sid, prs_struct *ps, int depth)
161 {
162         int i;
163
164         if (sid == NULL)
165                 return False;
166
167         prs_debug(ps, depth, desc, "smb_io_dom_sid");
168         depth++;
169
170         if(!prs_align(ps))
171                 return False;
172         
173         if(!prs_uint8 ("sid_rev_num", ps, depth, &sid->sid_rev_num))
174                 return False;
175         if(!prs_uint8 ("num_auths  ", ps, depth, &sid->num_auths))
176                 return False;
177
178         for (i = 0; i < 6; i++)
179         {
180                 fstring tmp;
181                 slprintf(tmp, sizeof(tmp) - 1, "id_auth[%d] ", i);
182                 if(!prs_uint8 (tmp, ps, depth, &sid->id_auth[i]))
183                         return False;
184         }
185
186         /* oops! XXXX should really issue a warning here... */
187         if (sid->num_auths > MAXSUBAUTHS)
188                 sid->num_auths = MAXSUBAUTHS;
189
190         if(!prs_uint32s(False, "sub_auths ", ps, depth, sid->sub_auths, sid->num_auths))
191                 return False;
192
193         return True;
194 }
195
196 /*******************************************************************
197  Inits a DOM_SID structure.
198
199  BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 
200  identauth >= 2^32 can be detected because it will be specified in hex
201 ********************************************************************/
202
203 void init_dom_sid(DOM_SID *sid, char *str_sid)
204 {
205         pstring domsid;
206         int identauth;
207         char *p;
208
209         if (str_sid == NULL)
210         {
211                 DEBUG(4,("netlogon domain SID: none\n"));
212                 sid->sid_rev_num = 0;
213                 sid->num_auths = 0;
214                 return;
215         }
216                 
217         pstrcpy(domsid, str_sid);
218
219         DEBUG(4,("init_dom_sid %d SID:  %s\n", __LINE__, domsid));
220
221         /* assume, but should check, that domsid starts "S-" */
222         p = strtok(domsid+2,"-");
223         sid->sid_rev_num = atoi(p);
224
225         /* identauth in decimal should be <  2^32 */
226         /* identauth in hex     should be >= 2^32 */
227         identauth = atoi(strtok(0,"-"));
228
229         DEBUG(4,("netlogon rev %d\n", sid->sid_rev_num));
230         DEBUG(4,("netlogon %s ia %d\n", p, identauth));
231
232         sid->id_auth[0] = 0;
233         sid->id_auth[1] = 0;
234         sid->id_auth[2] = (identauth & 0xff000000) >> 24;
235         sid->id_auth[3] = (identauth & 0x00ff0000) >> 16;
236         sid->id_auth[4] = (identauth & 0x0000ff00) >> 8;
237         sid->id_auth[5] = (identauth & 0x000000ff);
238
239         sid->num_auths = 0;
240
241         while ((p = strtok(0, "-")) != NULL && sid->num_auths < MAXSUBAUTHS)
242                 sid->sub_auths[sid->num_auths++] = atoi(p);
243
244         DEBUG(4,("init_dom_sid: %d SID:  %s\n", __LINE__, domsid));
245 }
246
247 /*******************************************************************
248  Inits a DOM_SID2 structure.
249 ********************************************************************/
250
251 void init_dom_sid2(DOM_SID2 *sid2, DOM_SID *sid)
252 {
253         sid2->sid = *sid;
254         sid2->num_auths = sid2->sid.num_auths;
255 }
256
257 /*******************************************************************
258  Reads or writes a DOM_SID2 structure.
259 ********************************************************************/
260
261 BOOL smb_io_dom_sid2(char *desc, DOM_SID2 *sid, prs_struct *ps, int depth)
262 {
263         if (sid == NULL)
264                 return False;
265
266         prs_debug(ps, depth, desc, "smb_io_dom_sid2");
267         depth++;
268
269         if(!prs_align(ps))
270                 return False;
271         
272         if(!prs_uint32("num_auths", ps, depth, &sid->num_auths))
273                 return False;
274
275         if(!smb_io_dom_sid("sid", &sid->sid, ps, depth))
276                 return False;
277
278         return True;
279 }
280
281 /*******************************************************************
282 creates a STRHDR structure.
283 ********************************************************************/
284
285 void init_str_hdr(STRHDR *hdr, int max_len, int len, uint32 buffer)
286 {
287         hdr->str_max_len = max_len;
288         hdr->str_str_len = len;
289         hdr->buffer      = buffer;
290 }
291
292 /*******************************************************************
293  Reads or writes a STRHDR structure.
294 ********************************************************************/
295
296 BOOL smb_io_strhdr(char *desc,  STRHDR *hdr, prs_struct *ps, int depth)
297 {
298         if (hdr == NULL)
299                 return False;
300
301         prs_debug(ps, depth, desc, "smb_io_strhdr");
302         depth++;
303
304         prs_align(ps);
305         
306         if(!prs_uint16("str_str_len", ps, depth, &hdr->str_str_len))
307                 return False;
308         if(!prs_uint16("str_max_len", ps, depth, &hdr->str_max_len))
309                 return False;
310         if(!prs_uint32("buffer     ", ps, depth, &hdr->buffer))
311                 return False;
312
313         return True;
314 }
315
316 /*******************************************************************
317  Inits a UNIHDR structure.
318 ********************************************************************/
319
320 void init_uni_hdr(UNIHDR *hdr, int len)
321 {
322         hdr->uni_str_len = 2 * len;
323         hdr->uni_max_len = 2 * len;
324         hdr->buffer      = len != 0 ? 1 : 0;
325 }
326
327 /*******************************************************************
328  Reads or writes a UNIHDR structure.
329 ********************************************************************/
330
331 BOOL smb_io_unihdr(char *desc, UNIHDR *hdr, prs_struct *ps, int depth)
332 {
333         if (hdr == NULL)
334                 return False;
335
336         prs_debug(ps, depth, desc, "smb_io_unihdr");
337         depth++;
338
339         if(!prs_align(ps))
340                 return False;
341         
342         if(!prs_uint16("uni_str_len", ps, depth, &hdr->uni_str_len))
343                 return False;
344         if(!prs_uint16("uni_max_len", ps, depth, &hdr->uni_max_len))
345                 return False;
346         if(!prs_uint32("buffer     ", ps, depth, &hdr->buffer))
347                 return False;
348
349         return True;
350 }
351
352 /*******************************************************************
353  Inits a BUFHDR structure.
354 ********************************************************************/
355
356 void init_buf_hdr(BUFHDR *hdr, int max_len, int len)
357 {
358         hdr->buf_max_len = max_len;
359         hdr->buf_len     = len;
360 }
361
362 /*******************************************************************
363  prs_uint16 wrapper. Call this and it sets up a pointer to where the
364  uint16 should be stored, or gets the size if reading.
365  ********************************************************************/
366
367 BOOL smb_io_hdrbuf_pre(char *desc, BUFHDR *hdr, prs_struct *ps, int depth, uint32 *offset)
368 {
369         (*offset) = prs_offset(ps);
370         if (ps->io) {
371
372                 /* reading. */
373
374                 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
375                         return False;
376
377         } else {
378
379                 /* writing. */
380
381                 if(!prs_set_offset(ps, prs_offset(ps) + (sizeof(uint32) * 2)))
382                         return False;
383         }
384
385         return True;
386 }
387
388 /*******************************************************************
389  smb_io_hdrbuf wrapper. Call this and it retrospectively stores the size.
390  Does nothing on reading, as that is already handled by ...._pre()
391  ********************************************************************/
392
393 BOOL smb_io_hdrbuf_post(char *desc, BUFHDR *hdr, prs_struct *ps, int depth, 
394                                 uint32 ptr_hdrbuf, uint32 max_len, uint32 len)
395 {
396         if (!ps->io) {
397                 /* writing: go back and do a retrospective job.  i hate this */
398
399                 uint32 old_offset = prs_offset(ps);
400
401                 init_buf_hdr(hdr, max_len, len);
402                 if(!prs_set_offset(ps, ptr_hdrbuf))
403                         return False;
404                 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
405                         return False;
406
407                 if(!prs_set_offset(ps, old_offset))
408                         return False;
409         }
410
411         return True;
412 }
413
414 /*******************************************************************
415  Reads or writes a BUFHDR structure.
416 ********************************************************************/
417
418 BOOL smb_io_hdrbuf(char *desc, BUFHDR *hdr, prs_struct *ps, int depth)
419 {
420         if (hdr == NULL)
421                 return False;
422
423         prs_debug(ps, depth, desc, "smb_io_hdrbuf");
424         depth++;
425
426         if(!prs_align(ps))
427                 return False;
428         
429         if(!prs_uint32("buf_max_len", ps, depth, &hdr->buf_max_len))
430                 return False;
431         if(!prs_uint32("buf_len    ", ps, depth, &hdr->buf_len))
432                 return False;
433
434         return True;
435 }
436
437 /*******************************************************************
438 creates a UNIHDR2 structure.
439 ********************************************************************/
440
441 void init_uni_hdr2(UNIHDR2 *hdr, int len)
442 {
443         init_uni_hdr(&hdr->unihdr, len);
444         hdr->buffer = (len > 0) ? 1 : 0;
445 }
446
447 /*******************************************************************
448  Reads or writes a UNIHDR2 structure.
449 ********************************************************************/
450
451 BOOL smb_io_unihdr2(char *desc, UNIHDR2 *hdr2, prs_struct *ps, int depth)
452 {
453         if (hdr2 == NULL)
454                 return False;
455
456         prs_debug(ps, depth, desc, "smb_io_unihdr2");
457         depth++;
458
459         if(!prs_align(ps))
460                 return False;
461
462         if(!smb_io_unihdr("hdr", &hdr2->unihdr, ps, depth))
463                 return False;
464         if(!prs_uint32("buffer", ps, depth, &hdr2->buffer))
465                 return False;
466
467         return True;
468 }
469
470 /*******************************************************************
471  Inits a UNISTR structure.
472 ********************************************************************/
473
474 void init_unistr(UNISTR *str, const char *buf)
475 {
476         size_t len;
477
478         if (buf == NULL) {
479                 str->buffer = NULL;
480                 return;
481         }
482                 
483
484         len = strlen(buf) + 1;
485
486         if (!parse_misc_talloc)
487                 parse_misc_talloc = talloc_init();
488
489         if (len < MAX_UNISTRLEN)
490                 len = MAX_UNISTRLEN;
491         len *= sizeof(uint16);
492
493         str->buffer = (uint16 *)talloc(parse_misc_talloc, len);
494         if (str->buffer == NULL)
495                 smb_panic("init_unistr: malloc fail\n");
496
497         /* store the string (null-terminated copy) */
498         dos_struni2((char *)str->buffer, buf, len);
499 }
500
501 /*******************************************************************
502 reads or writes a UNISTR structure.
503 XXXX NOTE: UNISTR structures NEED to be null-terminated.
504 ********************************************************************/
505
506 BOOL smb_io_unistr(char *desc, UNISTR *uni, prs_struct *ps, int depth)
507 {
508         if (uni == NULL)
509                 return False;
510
511         prs_debug(ps, depth, desc, "smb_io_unistr");
512         depth++;
513
514         if(!prs_align(ps))
515                 return False;
516         if(!prs_unistr("unistr", ps, depth, uni))
517                 return False;
518
519         return True;
520 }
521
522 /*******************************************************************
523  Allocate the BUFFER3 memory.
524 ********************************************************************/
525
526 static void create_buffer3(BUFFER3 *str, size_t len)
527 {
528     if (!parse_misc_talloc)
529                 parse_misc_talloc = talloc_init();
530
531         if (len < MAX_BUFFERLEN)
532                 len = MAX_BUFFERLEN;
533
534     str->buffer = talloc(parse_misc_talloc, len);
535         if (str->buffer == NULL)
536                 smb_panic("create_buffer3: malloc fail\n");
537
538 }
539
540 /*******************************************************************
541  Inits a BUFFER3 structure from a uint32
542 ********************************************************************/
543
544 void init_buffer3_uint32(BUFFER3 *str, uint32 val)
545 {
546         ZERO_STRUCTP(str);
547
548         /* set up string lengths. */
549         str->buf_max_len = sizeof(uint32);
550         str->buf_len     = sizeof(uint32);
551
552         create_buffer3(str, sizeof(uint32));
553         SIVAL(str->buffer, 0, val);
554 }
555
556 /*******************************************************************
557  Inits a BUFFER3 structure.
558 ********************************************************************/
559
560 void init_buffer3_str(BUFFER3 *str, char *buf, int len)
561 {
562         ZERO_STRUCTP(str);
563
564         /* set up string lengths. */
565         str->buf_max_len = len * 2;
566         str->buf_len     = len * 2;
567
568         create_buffer3(str, str->buf_max_len);
569
570         /* store the string (null-terminated 8 bit chars into 16 bit chars) */
571         dos_struni2((char *)str->buffer, buf, str->buf_max_len);
572 }
573
574 /*******************************************************************
575  Inits a BUFFER3 structure from a hex string.
576 ********************************************************************/
577
578 void init_buffer3_hex(BUFFER3 *str, char *buf)
579 {
580         ZERO_STRUCTP(str);
581         create_buffer3(str, strlen(buf));
582         str->buf_max_len = str->buf_len = strhex_to_str((char *)str->buffer, sizeof(str->buffer), buf);
583 }
584
585 /*******************************************************************
586  Inits a BUFFER3 structure.
587 ********************************************************************/
588
589 void init_buffer3_bytes(BUFFER3 *str, uint8 *buf, int len)
590 {
591         ZERO_STRUCTP(str);
592
593         /* max buffer size (allocated size) */
594         str->buf_max_len = len;
595         if (buf != NULL) {
596                 create_buffer3(str, len);
597                 memcpy(str->buffer, buf, len);
598         }
599         str->buf_len = buf != NULL ? len : 0;
600 }
601
602 /*******************************************************************
603  Reads or writes a BUFFER3 structure.
604    the uni_max_len member tells you how large the buffer is.
605    the uni_str_len member tells you how much of the buffer is really used.
606 ********************************************************************/
607
608 BOOL smb_io_buffer3(char *desc, BUFFER3 *buf3, prs_struct *ps, int depth)
609 {
610         if (buf3 == NULL)
611                 return False;
612
613         prs_debug(ps, depth, desc, "smb_io_buffer3");
614         depth++;
615
616         if(!prs_align(ps))
617                 return False;
618         
619         if(!prs_uint32("uni_max_len", ps, depth, &buf3->buf_max_len))
620                 return False;
621
622         if (UNMARSHALLING(ps)) {
623                 buf3->buffer = prs_alloc_mem(ps, buf3->buf_max_len);
624                 if (buf3->buffer == NULL)
625                         return False;
626         }
627
628         if(!prs_uint8s(True, "buffer     ", ps, depth, buf3->buffer, buf3->buf_max_len))
629                 return False;
630
631         if(!prs_uint32("buf_len    ", ps, depth, &buf3->buf_len))
632                 return False;
633
634         return True;
635 }
636
637 /*******************************************************************
638 reads or writes a BUFFER5 structure.
639 the buf_len member tells you how large the buffer is.
640 ********************************************************************/
641 BOOL smb_io_buffer5(char *desc, BUFFER5 *buf5, prs_struct *ps, int depth)
642 {
643         prs_debug(ps, depth, desc, "smb_io_buffer5");
644         depth++;
645
646         if (buf5 == NULL) return False;
647
648         prs_align(ps);
649         prs_uint32("buf_len", ps, depth, &(buf5->buf_len));
650
651         /* reading: alloc the buffer first */
652         if ( UNMARSHALLING(ps) ) {
653                 buf5->buffer=(uint16 *)prs_alloc_mem(ps, sizeof(uint16)*buf5->buf_len );
654                 if (buf5->buffer == NULL)
655                         return False;
656         }
657         
658         prs_uint16s(True, "buffer", ps, depth, buf5->buffer, buf5->buf_len);
659
660         return True;
661 }
662
663 /*******************************************************************
664  Inits a BUFFER2 structure.
665 ********************************************************************/
666
667 void init_buffer2(BUFFER2 *str, uint8 *buf, int len)
668 {
669         ZERO_STRUCTP(str);
670
671         /* max buffer size (allocated size) */
672         str->buf_max_len = len;
673         str->undoc       = 0;
674         str->buf_len = buf != NULL ? len : 0;
675
676         if (buf != NULL) {
677                 if (!parse_misc_talloc)
678                         parse_misc_talloc = talloc_init();
679
680                 if (len < MAX_BUFFERLEN)
681                         len = MAX_BUFFERLEN;
682                 str->buffer = talloc(parse_misc_talloc, len);
683                 if (str->buffer == NULL)
684                         smb_panic("init_buffer2: malloc fail\n");
685                 memcpy(str->buffer, buf, MIN(str->buf_len, len));
686         }
687 }
688
689 /*******************************************************************
690  Reads or writes a BUFFER2 structure.
691    the uni_max_len member tells you how large the buffer is.
692    the uni_str_len member tells you how much of the buffer is really used.
693 ********************************************************************/
694
695 BOOL smb_io_buffer2(char *desc, BUFFER2 *buf2, uint32 buffer, prs_struct *ps, int depth)
696 {
697         if (buf2 == NULL)
698                 return False;
699
700         if (buffer) {
701
702                 prs_debug(ps, depth, desc, "smb_io_buffer2");
703                 depth++;
704
705                 if(!prs_align(ps))
706                         return False;
707                 
708                 if(!prs_uint32("uni_max_len", ps, depth, &buf2->buf_max_len))
709                         return False;
710                 if(!prs_uint32("undoc      ", ps, depth, &buf2->undoc))
711                         return False;
712                 if(!prs_uint32("buf_len    ", ps, depth, &buf2->buf_len))
713                         return False;
714
715                 /* buffer advanced by indicated length of string
716                    NOT by searching for null-termination */
717
718                 if(!prs_buffer2(True, "buffer     ", ps, depth, buf2))
719                         return False;
720
721         } else {
722
723                 prs_debug(ps, depth, desc, "smb_io_buffer2 - NULL");
724                 depth++;
725                 memset((char *)buf2, '\0', sizeof(*buf2));
726
727         }
728         return True;
729 }
730
731 /*******************************************************************
732 creates a UNISTR2 structure: sets up the buffer, too
733 ********************************************************************/
734
735 void init_buf_unistr2(UNISTR2 *str, uint32 *ptr, const char *buf)
736 {
737         if (buf != NULL) {
738
739                 *ptr = 1;
740                 init_unistr2(str, buf, strlen(buf)+1);
741
742         } else {
743
744                 *ptr = 0;
745                 init_unistr2(str, "", 0);
746
747         }
748 }
749
750 /*******************************************************************
751  Copies a UNISTR2 structure.
752 ********************************************************************/
753
754 void copy_unistr2(UNISTR2 *str, UNISTR2 *from)
755 {
756         /* set up string lengths. add one if string is not null-terminated */
757         str->uni_max_len = from->uni_max_len;
758         str->undoc       = from->undoc;
759         str->uni_str_len = from->uni_str_len;
760
761         if (str->buffer == NULL) {
762                 size_t len = from->uni_max_len * 2;
763
764         if (!parse_misc_talloc)
765                         parse_misc_talloc = talloc_init();
766
767                 if (len < MAX_UNISTRLEN)
768                         len = MAX_UNISTRLEN;
769                 len *= sizeof(uint16);
770
771                 str->buffer = (uint16 *)talloc(parse_misc_talloc, len);
772                 if (str->buffer == NULL)
773                         smb_panic("copy_unistr2: malloc fail\n");
774         }
775
776         /* copy the string */
777         memcpy(str->buffer, from->buffer, sizeof(from->buffer));
778 }
779
780 /*******************************************************************
781  Creates a STRING2 structure.
782 ********************************************************************/
783
784 void init_string2(STRING2 *str, char *buf, int len)
785 {
786         int alloc_len = 0;
787
788         /* set up string lengths. */
789         str->str_max_len = len;
790         str->undoc       = 0;
791         str->str_str_len = len;
792
793         /* store the string */
794         if(len != 0) {
795                 if (!parse_misc_talloc)
796                         parse_misc_talloc = talloc_init();
797
798                 if (len < MAX_STRINGLEN)
799                         alloc_len = MAX_STRINGLEN;
800                 str->buffer = talloc(parse_misc_talloc, alloc_len);
801                 if (str->buffer == NULL)
802                         smb_panic("init_string2: malloc fail\n");
803                 memcpy(str->buffer, buf, len);
804   }
805 }
806
807 /*******************************************************************
808  Reads or writes a STRING2 structure.
809  XXXX NOTE: STRING2 structures need NOT be null-terminated.
810    the str_str_len member tells you how long the string is;
811    the str_max_len member tells you how large the buffer is.
812 ********************************************************************/
813
814 BOOL smb_io_string2(char *desc, STRING2 *str2, uint32 buffer, prs_struct *ps, int depth)
815 {
816         if (str2 == NULL)
817                 return False;
818
819         if (buffer) {
820
821                 prs_debug(ps, depth, desc, "smb_io_string2");
822                 depth++;
823
824                 if(!prs_align(ps))
825                         return False;
826                 
827                 if(!prs_uint32("str_max_len", ps, depth, &str2->str_max_len))
828                         return False;
829                 if(!prs_uint32("undoc      ", ps, depth, &str2->undoc))
830                         return False;
831                 if(!prs_uint32("str_str_len", ps, depth, &str2->str_str_len))
832                         return False;
833
834                 /* buffer advanced by indicated length of string
835                    NOT by searching for null-termination */
836                 if(!prs_string2(True, "buffer     ", ps, depth, str2))
837                         return False;
838
839         } else {
840
841                 prs_debug(ps, depth, desc, "smb_io_string2 - NULL");
842                 depth++;
843                 memset((char *)str2, '\0', sizeof(*str2));
844
845         }
846
847         return True;
848 }
849
850 /*******************************************************************
851  Inits a UNISTR2 structure.
852 ********************************************************************/
853
854 void init_unistr2(UNISTR2 *str, const char *buf, size_t len)
855 {
856         ZERO_STRUCTP(str);
857
858         /* set up string lengths. */
859         str->uni_max_len = (uint32)len;
860         str->undoc       = 0;
861         str->uni_str_len = (uint32)len;
862
863         if (!parse_misc_talloc)
864                 parse_misc_talloc = talloc_init();
865
866         if (len < MAX_UNISTRLEN)
867                 len = MAX_UNISTRLEN;
868         len *= sizeof(uint16);
869
870         str->buffer = (uint16 *)talloc(parse_misc_talloc, len);
871         if (str->buffer == NULL)
872                 smb_panic("init_unistr2: malloc fail\n");
873
874         /* store the string (null-terminated 8 bit chars into 16 bit chars) */
875         dos_struni2((char *)str->buffer, buf, len);
876 }
877
878 /*******************************************************************
879  Inits a UNISTR2 structure from a UNISTR
880 ********************************************************************/
881 void init_unistr2_from_unistr (UNISTR2 *to, UNISTR *from)
882 {
883
884         uint32 i;
885
886         /* the destination UNISTR2 should never be NULL.
887            if it is it is a programming error */
888
889         /* if the source UNISTR is NULL, then zero out
890            the destination string and return */
891         ZERO_STRUCTP (to);
892         if ((from == NULL) || (from->buffer == NULL))
893                 return;
894
895         /* get the length; UNISTR must be NULL terminated */
896         i = 0;
897         while ((from->buffer)[i]!='\0')
898                 i++;
899
900         /* set up string lengths; uni_max_len is set to i+1
901            because we need to account for the final NULL termination */
902         to->uni_max_len = i+1;
903         to->undoc       = 0;
904         to->uni_str_len = i+1;
905
906         if (!parse_misc_talloc)
907                 parse_misc_talloc = talloc_init();
908         
909         /* allocate the space and copy the string buffer */
910         to->buffer = (uint16 *)talloc(parse_misc_talloc, sizeof(uint16)*(to->uni_str_len));
911         if (to->buffer == NULL)
912                 smb_panic("init_unistr2_from_unistr: malloc fail\n");
913         memcpy(to->buffer, from->buffer, to->uni_max_len*sizeof(uint16));
914                 
915         return;
916 }
917
918
919 /*******************************************************************
920  Reads or writes a UNISTR2 structure.
921  XXXX NOTE: UNISTR2 structures need NOT be null-terminated.
922    the uni_str_len member tells you how long the string is;
923    the uni_max_len member tells you how large the buffer is.
924 ********************************************************************/
925
926 BOOL smb_io_unistr2(char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
927 {
928         if (uni2 == NULL)
929                 return False;
930
931         if (buffer) {
932
933                 prs_debug(ps, depth, desc, "smb_io_unistr2");
934                 depth++;
935
936                 if(!prs_align(ps))
937                         return False;
938                 
939                 if(!prs_uint32("uni_max_len", ps, depth, &uni2->uni_max_len))
940                         return False;
941                 if(!prs_uint32("undoc      ", ps, depth, &uni2->undoc))
942                         return False;
943                 if(!prs_uint32("uni_str_len", ps, depth, &uni2->uni_str_len))
944                         return False;
945
946                 /* buffer advanced by indicated length of string
947                    NOT by searching for null-termination */
948                 if(!prs_unistr2(True, "buffer     ", ps, depth, uni2))
949                         return False;
950
951         } else {
952
953                 prs_debug(ps, depth, desc, "smb_io_unistr2 - NULL");
954                 depth++;
955                 memset((char *)uni2, '\0', sizeof(*uni2));
956
957         }
958
959         return True;
960 }
961
962 /*******************************************************************
963  Inits a DOM_RID2 structure.
964 ********************************************************************/
965
966 void init_dom_rid2(DOM_RID2 *rid2, uint32 rid, uint8 type, uint32 idx)
967 {
968         rid2->type    = type;
969         rid2->rid     = rid;
970         rid2->rid_idx = idx;
971 }
972
973 /*******************************************************************
974  Reads or writes a DOM_RID2 structure.
975 ********************************************************************/
976
977 BOOL smb_io_dom_rid2(char *desc, DOM_RID2 *rid2, prs_struct *ps, int depth)
978 {
979         if (rid2 == NULL)
980                 return False;
981
982         prs_debug(ps, depth, desc, "smb_io_dom_rid2");
983         depth++;
984
985         if(!prs_align(ps))
986                 return False;
987    
988         if(!prs_uint8("type   ", ps, depth, &rid2->type))
989                 return False;
990         if(!prs_align(ps))
991                 return False;
992         if(!prs_uint32("rid    ", ps, depth, &rid2->rid))
993                 return False;
994         if(!prs_uint32("rid_idx", ps, depth, &rid2->rid_idx))
995                 return False;
996
997         return True;
998 }
999
1000 /*******************************************************************
1001 creates a DOM_RID3 structure.
1002 ********************************************************************/
1003
1004 void init_dom_rid3(DOM_RID3 *rid3, uint32 rid, uint8 type)
1005 {
1006     rid3->rid      = rid;
1007     rid3->type1    = type;
1008     rid3->ptr_type = 0x1; /* non-zero, basically. */
1009     rid3->type2    = 0x1;
1010     rid3->unk      = type;
1011 }
1012
1013 /*******************************************************************
1014 reads or writes a DOM_RID3 structure.
1015 ********************************************************************/
1016
1017 BOOL smb_io_dom_rid3(char *desc, DOM_RID3 *rid3, prs_struct *ps, int depth)
1018 {
1019         if (rid3 == NULL)
1020                 return False;
1021
1022         prs_debug(ps, depth, desc, "smb_io_dom_rid3");
1023         depth++;
1024
1025         if(!prs_align(ps))
1026                 return False;
1027
1028         if(!prs_uint32("rid     ", ps, depth, &rid3->rid))
1029                 return False;
1030         if(!prs_uint32("type1   ", ps, depth, &rid3->type1))
1031                 return False;
1032         if(!prs_uint32("ptr_type", ps, depth, &rid3->ptr_type))
1033                 return False;
1034         if(!prs_uint32("type2   ", ps, depth, &rid3->type2))
1035                 return False;
1036         if(!prs_uint32("unk     ", ps, depth, &rid3->unk))
1037                 return False;
1038
1039         return True;
1040 }
1041
1042 /*******************************************************************
1043  Inits a DOM_RID4 structure.
1044 ********************************************************************/
1045
1046 void init_dom_rid4(DOM_RID4 *rid4, uint16 unknown, uint16 attr, uint32 rid)
1047 {
1048     rid4->unknown = unknown;
1049     rid4->attr    = attr;
1050     rid4->rid     = rid;
1051 }
1052
1053 /*******************************************************************
1054  Inits a DOM_CLNT_SRV structure.
1055 ********************************************************************/
1056
1057 static void init_clnt_srv(DOM_CLNT_SRV *log, char *logon_srv, char *comp_name)
1058 {
1059         DEBUG(5,("init_clnt_srv: %d\n", __LINE__));
1060
1061         if (logon_srv != NULL) {
1062                 log->undoc_buffer = 1;
1063                 init_unistr2(&log->uni_logon_srv, logon_srv, strlen(logon_srv)+1);
1064         } else {
1065                 log->undoc_buffer = 0;
1066         }
1067
1068         if (comp_name != NULL) {
1069                 log->undoc_buffer2 = 1;
1070                 init_unistr2(&log->uni_comp_name, comp_name, strlen(comp_name)+1);
1071         } else {
1072                 log->undoc_buffer2 = 0;
1073         }
1074 }
1075
1076 /*******************************************************************
1077  Inits or writes a DOM_CLNT_SRV structure.
1078 ********************************************************************/
1079
1080 static BOOL smb_io_clnt_srv(char *desc, DOM_CLNT_SRV *log, prs_struct *ps, int depth)
1081 {
1082         if (log == NULL)
1083                 return False;
1084
1085         prs_debug(ps, depth, desc, "smb_io_clnt_srv");
1086         depth++;
1087
1088         if(!prs_align(ps))
1089                 return False;
1090         
1091         if(!prs_uint32("undoc_buffer ", ps, depth, &log->undoc_buffer))
1092                 return False;
1093
1094         if (log->undoc_buffer != 0) {
1095                 if(!smb_io_unistr2("unistr2", &log->uni_logon_srv, log->undoc_buffer, ps, depth))
1096                         return False;
1097         }
1098
1099         if(!prs_align(ps))
1100                 return False;
1101
1102         if(!prs_uint32("undoc_buffer2", ps, depth, &log->undoc_buffer2))
1103                 return False;
1104
1105         if (log->undoc_buffer2 != 0) {
1106                 if(!smb_io_unistr2("unistr2", &log->uni_comp_name, log->undoc_buffer2, ps, depth))
1107                         return False;
1108         }
1109
1110         return True;
1111 }
1112
1113 /*******************************************************************
1114  Inits a DOM_LOG_INFO structure.
1115 ********************************************************************/
1116
1117 void init_log_info(DOM_LOG_INFO *log, char *logon_srv, char *acct_name,
1118                 uint16 sec_chan, char *comp_name)
1119 {
1120         DEBUG(5,("make_log_info %d\n", __LINE__));
1121
1122         log->undoc_buffer = 1;
1123
1124         init_unistr2(&log->uni_logon_srv, logon_srv, strlen(logon_srv)+1);
1125         init_unistr2(&log->uni_acct_name, acct_name, strlen(acct_name)+1);
1126
1127         log->sec_chan = sec_chan;
1128
1129         init_unistr2(&log->uni_comp_name, comp_name, strlen(comp_name)+1);
1130 }
1131
1132 /*******************************************************************
1133  Reads or writes a DOM_LOG_INFO structure.
1134 ********************************************************************/
1135
1136 BOOL smb_io_log_info(char *desc, DOM_LOG_INFO *log, prs_struct *ps, int depth)
1137 {
1138         if (log == NULL)
1139                 return False;
1140
1141         prs_debug(ps, depth, desc, "smb_io_log_info");
1142         depth++;
1143
1144         if(!prs_align(ps))
1145                 return False;
1146         
1147         if(!prs_uint32("undoc_buffer", ps, depth, &log->undoc_buffer))
1148                 return False;
1149
1150         if(!smb_io_unistr2("unistr2", &log->uni_logon_srv, True, ps, depth))
1151                 return False;
1152         if(!smb_io_unistr2("unistr2", &log->uni_acct_name, True, ps, depth))
1153                 return False;
1154
1155         if(!prs_uint16("sec_chan", ps, depth, &log->sec_chan))
1156                 return False;
1157
1158         if(!smb_io_unistr2("unistr2", &log->uni_comp_name, True, ps, depth))
1159                 return False;
1160
1161         return True;
1162 }
1163
1164 /*******************************************************************
1165  Reads or writes a DOM_CHAL structure.
1166 ********************************************************************/
1167
1168 BOOL smb_io_chal(char *desc, DOM_CHAL *chal, prs_struct *ps, int depth)
1169 {
1170         if (chal == NULL)
1171                 return False;
1172
1173         prs_debug(ps, depth, desc, "smb_io_chal");
1174         depth++;
1175
1176         if(!prs_align(ps))
1177                 return False;
1178         
1179         if(!prs_uint8s (False, "data", ps, depth, chal->data, 8))
1180                 return False;
1181
1182         return True;
1183 }
1184
1185 /*******************************************************************
1186  Reads or writes a DOM_CRED structure.
1187 ********************************************************************/
1188
1189 BOOL smb_io_cred(char *desc,  DOM_CRED *cred, prs_struct *ps, int depth)
1190 {
1191         if (cred == NULL)
1192                 return False;
1193
1194         prs_debug(ps, depth, desc, "smb_io_cred");
1195         depth++;
1196
1197         if(!prs_align(ps))
1198                 return False;
1199
1200         if(!smb_io_chal ("", &cred->challenge, ps, depth))
1201                 return False;
1202         if(!smb_io_utime("", &cred->timestamp, ps, depth))
1203                 return False;
1204
1205         return True;
1206 }
1207
1208 /*******************************************************************
1209  Inits a DOM_CLNT_INFO2 structure.
1210 ********************************************************************/
1211
1212 void init_clnt_info2(DOM_CLNT_INFO2 *clnt,
1213                                 char *logon_srv, char *comp_name,
1214                                 DOM_CRED *clnt_cred)
1215 {
1216         DEBUG(5,("make_clnt_info: %d\n", __LINE__));
1217
1218         init_clnt_srv(&(clnt->login), logon_srv, comp_name);
1219
1220         if (clnt_cred != NULL) {
1221                 clnt->ptr_cred = 1;
1222                 memcpy(&(clnt->cred), clnt_cred, sizeof(clnt->cred));
1223         } else {
1224                 clnt->ptr_cred = 0;
1225         }
1226 }
1227
1228 /*******************************************************************
1229  Reads or writes a DOM_CLNT_INFO2 structure.
1230 ********************************************************************/
1231
1232 BOOL smb_io_clnt_info2(char *desc, DOM_CLNT_INFO2 *clnt, prs_struct *ps, int depth)
1233 {
1234         if (clnt == NULL)
1235                 return False;
1236
1237         prs_debug(ps, depth, desc, "smb_io_clnt_info2");
1238         depth++;
1239
1240         if(!prs_align(ps))
1241                 return False;
1242         
1243         if(!smb_io_clnt_srv("", &clnt->login, ps, depth))
1244                 return False;
1245
1246         if(!prs_align(ps))
1247                 return False;
1248         
1249         if(!prs_uint32("ptr_cred", ps, depth, &clnt->ptr_cred))
1250                 return False;
1251         if(!smb_io_cred("", &clnt->cred, ps, depth))
1252                 return False;
1253
1254         return True;
1255 }
1256
1257 /*******************************************************************
1258  Inits a DOM_CLNT_INFO structure.
1259 ********************************************************************/
1260
1261 void init_clnt_info(DOM_CLNT_INFO *clnt,
1262                 char *logon_srv, char *acct_name,
1263                 uint16 sec_chan, char *comp_name,
1264                                 DOM_CRED *cred)
1265 {
1266         DEBUG(5,("make_clnt_info\n"));
1267
1268         init_log_info(&clnt->login, logon_srv, acct_name, sec_chan, comp_name);
1269         memcpy(&clnt->cred, cred, sizeof(clnt->cred));
1270 }
1271
1272 /*******************************************************************
1273  Reads or writes a DOM_CLNT_INFO structure.
1274 ********************************************************************/
1275
1276 BOOL smb_io_clnt_info(char *desc,  DOM_CLNT_INFO *clnt, prs_struct *ps, int depth)
1277 {
1278         if (clnt == NULL)
1279                 return False;
1280
1281         prs_debug(ps, depth, desc, "smb_io_clnt_info");
1282         depth++;
1283
1284         if(!prs_align(ps))
1285                 return False;
1286         
1287         if(!smb_io_log_info("", &clnt->login, ps, depth))
1288                 return False;
1289         if(!smb_io_cred("", &clnt->cred, ps, depth))
1290                 return False;
1291
1292         return True;
1293 }
1294
1295 /*******************************************************************
1296  Inits a DOM_LOGON_ID structure.
1297 ********************************************************************/
1298
1299 void init_logon_id(DOM_LOGON_ID *log, uint32 log_id_low, uint32 log_id_high)
1300 {
1301         DEBUG(5,("make_logon_id: %d\n", __LINE__));
1302
1303         log->low  = log_id_low;
1304         log->high = log_id_high;
1305 }
1306
1307 /*******************************************************************
1308  Reads or writes a DOM_LOGON_ID structure.
1309 ********************************************************************/
1310
1311 BOOL smb_io_logon_id(char *desc, DOM_LOGON_ID *log, prs_struct *ps, int depth)
1312 {
1313         if (log == NULL)
1314                 return False;
1315
1316         prs_debug(ps, depth, desc, "smb_io_logon_id");
1317         depth++;
1318
1319         if(!prs_align(ps))
1320                 return False;
1321         
1322         if(!prs_uint32("low ", ps, depth, &log->low ))
1323                 return False;
1324         if(!prs_uint32("high", ps, depth, &log->high))
1325                 return False;
1326
1327         return True;
1328 }
1329
1330 /*******************************************************************
1331  Inits an OWF_INFO structure.
1332 ********************************************************************/
1333
1334 void init_owf_info(OWF_INFO *hash, uint8 data[16])
1335 {
1336         DEBUG(5,("init_owf_info: %d\n", __LINE__));
1337         
1338         if (data != NULL)
1339                 memcpy(hash->data, data, sizeof(hash->data));
1340         else
1341                 memset((char *)hash->data, '\0', sizeof(hash->data));
1342 }
1343
1344 /*******************************************************************
1345  Reads or writes an OWF_INFO structure.
1346 ********************************************************************/
1347
1348 BOOL smb_io_owf_info(char *desc, OWF_INFO *hash, prs_struct *ps, int depth)
1349 {
1350         if (hash == NULL)
1351                 return False;
1352
1353         prs_debug(ps, depth, desc, "smb_io_owf_info");
1354         depth++;
1355
1356         if(!prs_align(ps))
1357                 return False;
1358         
1359         if(!prs_uint8s (False, "data", ps, depth, hash->data, 16))
1360                 return False;
1361
1362         return True;
1363 }
1364
1365 /*******************************************************************
1366  Reads or writes a DOM_GID structure.
1367 ********************************************************************/
1368
1369 BOOL smb_io_gid(char *desc,  DOM_GID *gid, prs_struct *ps, int depth)
1370 {
1371         if (gid == NULL)
1372                 return False;
1373
1374         prs_debug(ps, depth, desc, "smb_io_gid");
1375         depth++;
1376
1377         if(!prs_align(ps))
1378                 return False;
1379         
1380         if(!prs_uint32("g_rid", ps, depth, &gid->g_rid))
1381                 return False;
1382         if(!prs_uint32("attr ", ps, depth, &gid->attr))
1383                 return False;
1384
1385         return True;
1386 }
1387
1388 /*******************************************************************
1389  Reads or writes an POLICY_HND structure.
1390 ********************************************************************/
1391
1392 BOOL smb_io_pol_hnd(char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
1393 {
1394         if (pol == NULL)
1395                 return False;
1396
1397         prs_debug(ps, depth, desc, "smb_io_pol_hnd");
1398         depth++;
1399
1400         if(!prs_align(ps))
1401                 return False;
1402         
1403         if(!prs_uint8s (False, "data", ps, depth, pol->data, POL_HND_SIZE))
1404                 return False;
1405
1406         return True;
1407 }
1408
1409 /*******************************************************************
1410  Reads or writes a dom query structure.
1411 ********************************************************************/
1412
1413 static BOOL smb_io_dom_query(char *desc, DOM_QUERY *d_q, prs_struct *ps, int depth)
1414 {
1415         if (d_q == NULL)
1416                 return False;
1417
1418         prs_debug(ps, depth, desc, "smb_io_dom_query");
1419         depth++;
1420
1421         if(!prs_align(ps))
1422                 return False;
1423         
1424         if(!prs_uint16("uni_dom_max_len", ps, depth, &d_q->uni_dom_max_len)) /* domain name string length * 2 */
1425                 return False;
1426         if(!prs_uint16("uni_dom_str_len", ps, depth, &d_q->uni_dom_str_len)) /* domain name string length * 2 */
1427                 return False;
1428
1429         if(!prs_uint32("buffer_dom_name", ps, depth, &d_q->buffer_dom_name)) /* undocumented domain name string buffer pointer */
1430                 return False;
1431         if(!prs_uint32("buffer_dom_sid ", ps, depth, &d_q->buffer_dom_sid)) /* undocumented domain SID string buffer pointer */
1432                 return False;
1433
1434         if(!smb_io_unistr2("unistr2", &d_q->uni_domain_name, d_q->buffer_dom_name, ps, depth)) /* domain name (unicode string) */
1435                 return False;
1436
1437         if(!prs_align(ps))
1438                 return False;
1439         
1440         if (d_q->buffer_dom_sid != 0) {
1441                 if(!smb_io_dom_sid2("", &d_q->dom_sid, ps, depth)) /* domain SID */
1442                         return False;
1443         } else {
1444                 memset((char *)&d_q->dom_sid, '\0', sizeof(d_q->dom_sid));
1445         }
1446
1447         return True;
1448 }
1449
1450 /*******************************************************************
1451  Reads or writes a dom query structure.
1452 ********************************************************************/
1453
1454 BOOL smb_io_dom_query_3(char *desc, DOM_QUERY_3 *d_q, prs_struct *ps, int depth)
1455 {
1456         return smb_io_dom_query("", d_q, ps, depth);
1457 }
1458
1459 /*******************************************************************
1460  Reads or writes a dom query structure.
1461 ********************************************************************/
1462
1463 BOOL smb_io_dom_query_5(char *desc, DOM_QUERY_3 *d_q, prs_struct *ps, int depth)
1464 {
1465         return smb_io_dom_query("", d_q, ps, depth);
1466 }
1467
1468
1469 /*******************************************************************
1470  Reads or writes a UNISTR3 structure.
1471 ********************************************************************/
1472
1473 BOOL smb_io_unistr3(char *desc, UNISTR3 *name, prs_struct *ps, int depth)
1474 {
1475         if (name == NULL)
1476                 return False;
1477
1478         prs_debug(ps, depth, desc, "smb_io_unistr3");
1479         depth++;
1480
1481         if(!prs_align(ps))
1482                 return False;
1483         
1484         if(!prs_uint32("uni_str_len", ps, depth, &name->uni_str_len))
1485                 return False;
1486
1487         /* don't know if len is specified by uni_str_len member... */
1488         /* assume unicode string is unicode-null-terminated, instead */
1489
1490         if(!prs_unistr3(True, "unistr", name, ps, depth))
1491                 return False;
1492
1493         return True;
1494 }
1495
1496
1497 /*******************************************************************
1498  Stream a uint64_struct
1499  ********************************************************************/
1500 BOOL prs_uint64(char *name, prs_struct *ps, int depth, UINT64_S *data64)
1501 {
1502         return prs_uint32(name, ps, depth+1, &data64->low) &&
1503                 prs_uint32(name, ps, depth+1, &data64->high);
1504 }
1505
1506