- fixed memcpy bug in copy_unistr2()
[Samba.git] / source / rpc_parse / parse_misc.c
blobcef2d3e3c2bceb5dc8753153aa0349c0956c0814
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.
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.
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.
25 #include "includes.h"
27 extern int DEBUGLEVEL;
29 static TALLOC_CTX *parse_misc_talloc = NULL;
31 /******************************************************************* a
32 free up temporary memory - called from the main loop
33 ********************************************************************/
35 void parse_talloc_free(void)
37 if (!parse_misc_talloc)
38 return;
39 talloc_destroy(parse_misc_talloc);
40 parse_misc_talloc = NULL;
43 /*******************************************************************
44 Reads or writes a UTIME type.
45 ********************************************************************/
47 static BOOL smb_io_utime(char *desc, UTIME *t, prs_struct *ps, int depth)
49 if (t == NULL)
50 return False;
52 prs_debug(ps, depth, desc, "smb_io_utime");
53 depth++;
55 if(!prs_align(ps))
56 return False;
58 if(!prs_uint32 ("time", ps, depth, &t->time))
59 return False;
61 return True;
64 /*******************************************************************
65 Reads or writes an NTTIME structure.
66 ********************************************************************/
68 BOOL smb_io_time(char *desc, NTTIME *nttime, prs_struct *ps, int depth)
70 if (nttime == NULL)
71 return False;
73 prs_debug(ps, depth, desc, "smb_io_time");
74 depth++;
76 if(!prs_align(ps))
77 return False;
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;
84 return True;
87 /*******************************************************************
88 Reads or writes a LOOKUP_LEVEL structure.
89 ********************************************************************/
91 BOOL smb_io_lookup_level(char *desc, LOOKUP_LEVEL *level, prs_struct *ps, int depth)
93 if (level == NULL)
94 return False;
96 prs_debug(ps, depth, desc, "smb_io_lookup_level");
97 depth++;
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;
106 return True;
109 /*******************************************************************
110 Gets an enumeration handle from an ENUM_HND structure.
111 ********************************************************************/
113 uint32 get_enum_hnd(ENUM_HND *enh)
115 return (enh && enh->ptr_hnd != 0) ? enh->handle : 0;
118 /*******************************************************************
119 Inits an ENUM_HND structure.
120 ********************************************************************/
122 void init_enum_hnd(ENUM_HND *enh, uint32 hnd)
124 DEBUG(5,("smb_io_enum_hnd\n"));
126 enh->ptr_hnd = (hnd != 0) ? 1 : 0;
127 enh->handle = hnd;
130 /*******************************************************************
131 Reads or writes an ENUM_HND structure.
132 ********************************************************************/
134 BOOL smb_io_enum_hnd(char *desc, ENUM_HND *hnd, prs_struct *ps, int depth)
136 if (hnd == NULL)
137 return False;
139 prs_debug(ps, depth, desc, "smb_io_enum_hnd");
140 depth++;
142 if(!prs_align(ps))
143 return False;
145 if(!prs_uint32("ptr_hnd", ps, depth, &hnd->ptr_hnd)) /* pointer */
146 return False;
148 if (hnd->ptr_hnd != 0) {
149 if(!prs_uint32("handle ", ps, depth, &hnd->handle )) /* enum handle */
150 return False;
153 return True;
156 /*******************************************************************
157 Reads or writes a DOM_SID structure.
158 ********************************************************************/
160 BOOL smb_io_dom_sid(char *desc, DOM_SID *sid, prs_struct *ps, int depth)
162 int i;
164 if (sid == NULL)
165 return False;
167 prs_debug(ps, depth, desc, "smb_io_dom_sid");
168 depth++;
170 if(!prs_align(ps))
171 return False;
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;
178 for (i = 0; i < 6; i++)
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;
186 /* oops! XXXX should really issue a warning here... */
187 if (sid->num_auths > MAXSUBAUTHS)
188 sid->num_auths = MAXSUBAUTHS;
190 if(!prs_uint32s(False, "sub_auths ", ps, depth, sid->sub_auths, sid->num_auths))
191 return False;
193 return True;
196 /*******************************************************************
197 Inits a DOM_SID structure.
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 ********************************************************************/
203 void init_dom_sid(DOM_SID *sid, char *str_sid)
205 pstring domsid;
206 int identauth;
207 char *p;
209 if (str_sid == NULL)
211 DEBUG(4,("netlogon domain SID: none\n"));
212 sid->sid_rev_num = 0;
213 sid->num_auths = 0;
214 return;
217 pstrcpy(domsid, str_sid);
219 DEBUG(4,("init_dom_sid %d SID: %s\n", __LINE__, domsid));
221 /* assume, but should check, that domsid starts "S-" */
222 p = strtok(domsid+2,"-");
223 sid->sid_rev_num = atoi(p);
225 /* identauth in decimal should be < 2^32 */
226 /* identauth in hex should be >= 2^32 */
227 identauth = atoi(strtok(0,"-"));
229 DEBUG(4,("netlogon rev %d\n", sid->sid_rev_num));
230 DEBUG(4,("netlogon %s ia %d\n", p, identauth));
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);
239 sid->num_auths = 0;
241 while ((p = strtok(0, "-")) != NULL && sid->num_auths < MAXSUBAUTHS)
242 sid->sub_auths[sid->num_auths++] = atoi(p);
244 DEBUG(4,("init_dom_sid: %d SID: %s\n", __LINE__, domsid));
247 /*******************************************************************
248 Inits a DOM_SID2 structure.
249 ********************************************************************/
251 void init_dom_sid2(DOM_SID2 *sid2, DOM_SID *sid)
253 sid2->sid = *sid;
254 sid2->num_auths = sid2->sid.num_auths;
257 /*******************************************************************
258 Reads or writes a DOM_SID2 structure.
259 ********************************************************************/
261 BOOL smb_io_dom_sid2(char *desc, DOM_SID2 *sid, prs_struct *ps, int depth)
263 if (sid == NULL)
264 return False;
266 prs_debug(ps, depth, desc, "smb_io_dom_sid2");
267 depth++;
269 if(!prs_align(ps))
270 return False;
272 if(!prs_uint32("num_auths", ps, depth, &sid->num_auths))
273 return False;
275 if(!smb_io_dom_sid("sid", &sid->sid, ps, depth))
276 return False;
278 return True;
281 /*******************************************************************
282 creates a STRHDR structure.
283 ********************************************************************/
285 void init_str_hdr(STRHDR *hdr, int max_len, int len, uint32 buffer)
287 hdr->str_max_len = max_len;
288 hdr->str_str_len = len;
289 hdr->buffer = buffer;
292 /*******************************************************************
293 Reads or writes a STRHDR structure.
294 ********************************************************************/
296 BOOL smb_io_strhdr(char *desc, STRHDR *hdr, prs_struct *ps, int depth)
298 if (hdr == NULL)
299 return False;
301 prs_debug(ps, depth, desc, "smb_io_strhdr");
302 depth++;
304 prs_align(ps);
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;
313 return True;
316 /*******************************************************************
317 Inits a UNIHDR structure.
318 ********************************************************************/
320 void init_uni_hdr(UNIHDR *hdr, int len)
322 hdr->uni_str_len = 2 * len;
323 hdr->uni_max_len = 2 * len;
324 hdr->buffer = len != 0 ? 1 : 0;
327 /*******************************************************************
328 Reads or writes a UNIHDR structure.
329 ********************************************************************/
331 BOOL smb_io_unihdr(char *desc, UNIHDR *hdr, prs_struct *ps, int depth)
333 if (hdr == NULL)
334 return False;
336 prs_debug(ps, depth, desc, "smb_io_unihdr");
337 depth++;
339 if(!prs_align(ps))
340 return False;
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;
349 return True;
352 /*******************************************************************
353 Inits a BUFHDR structure.
354 ********************************************************************/
356 void init_buf_hdr(BUFHDR *hdr, int max_len, int len)
358 hdr->buf_max_len = max_len;
359 hdr->buf_len = len;
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 ********************************************************************/
367 BOOL smb_io_hdrbuf_pre(char *desc, BUFHDR *hdr, prs_struct *ps, int depth, uint32 *offset)
369 (*offset) = prs_offset(ps);
370 if (ps->io) {
372 /* reading. */
374 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
375 return False;
377 } else {
379 /* writing. */
381 if(!prs_set_offset(ps, prs_offset(ps) + (sizeof(uint32) * 2)))
382 return False;
385 return True;
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 ********************************************************************/
393 BOOL smb_io_hdrbuf_post(char *desc, BUFHDR *hdr, prs_struct *ps, int depth,
394 uint32 ptr_hdrbuf, uint32 max_len, uint32 len)
396 if (!ps->io) {
397 /* writing: go back and do a retrospective job. i hate this */
399 uint32 old_offset = prs_offset(ps);
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;
407 if(!prs_set_offset(ps, old_offset))
408 return False;
411 return True;
414 /*******************************************************************
415 Reads or writes a BUFHDR structure.
416 ********************************************************************/
418 BOOL smb_io_hdrbuf(char *desc, BUFHDR *hdr, prs_struct *ps, int depth)
420 if (hdr == NULL)
421 return False;
423 prs_debug(ps, depth, desc, "smb_io_hdrbuf");
424 depth++;
426 if(!prs_align(ps))
427 return False;
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;
434 return True;
437 /*******************************************************************
438 creates a UNIHDR2 structure.
439 ********************************************************************/
441 void init_uni_hdr2(UNIHDR2 *hdr, int len)
443 init_uni_hdr(&hdr->unihdr, len);
444 hdr->buffer = (len > 0) ? 1 : 0;
447 /*******************************************************************
448 Reads or writes a UNIHDR2 structure.
449 ********************************************************************/
451 BOOL smb_io_unihdr2(char *desc, UNIHDR2 *hdr2, prs_struct *ps, int depth)
453 if (hdr2 == NULL)
454 return False;
456 prs_debug(ps, depth, desc, "smb_io_unihdr2");
457 depth++;
459 if(!prs_align(ps))
460 return False;
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;
467 return True;
470 /*******************************************************************
471 Inits a UNISTR structure.
472 ********************************************************************/
474 void init_unistr(UNISTR *str, const char *buf)
476 size_t len;
478 if (buf == NULL) {
479 str->buffer = NULL;
480 return;
484 len = strlen(buf) + 1;
486 if (!parse_misc_talloc)
487 parse_misc_talloc = talloc_init();
489 if (len < MAX_UNISTRLEN)
490 len = MAX_UNISTRLEN;
491 len *= sizeof(uint16);
493 str->buffer = (uint16 *)talloc(parse_misc_talloc, len);
494 if (str->buffer == NULL)
495 smb_panic("init_unistr: malloc fail\n");
497 /* store the string (null-terminated copy) */
498 dos_struni2((char *)str->buffer, buf, len);
501 /*******************************************************************
502 reads or writes a UNISTR structure.
503 XXXX NOTE: UNISTR structures NEED to be null-terminated.
504 ********************************************************************/
506 BOOL smb_io_unistr(char *desc, UNISTR *uni, prs_struct *ps, int depth)
508 if (uni == NULL)
509 return False;
511 prs_debug(ps, depth, desc, "smb_io_unistr");
512 depth++;
514 if(!prs_align(ps))
515 return False;
516 if(!prs_unistr("unistr", ps, depth, uni))
517 return False;
519 return True;
522 /*******************************************************************
523 Allocate the BUFFER3 memory.
524 ********************************************************************/
526 static void create_buffer3(BUFFER3 *str, size_t len)
528 if (!parse_misc_talloc)
529 parse_misc_talloc = talloc_init();
531 if (len < MAX_BUFFERLEN)
532 len = MAX_BUFFERLEN;
534 str->buffer = talloc(parse_misc_talloc, len);
535 if (str->buffer == NULL)
536 smb_panic("create_buffer3: malloc fail\n");
540 /*******************************************************************
541 Inits a BUFFER3 structure from a uint32
542 ********************************************************************/
544 void init_buffer3_uint32(BUFFER3 *str, uint32 val)
546 ZERO_STRUCTP(str);
548 /* set up string lengths. */
549 str->buf_max_len = sizeof(uint32);
550 str->buf_len = sizeof(uint32);
552 create_buffer3(str, sizeof(uint32));
553 SIVAL(str->buffer, 0, val);
556 /*******************************************************************
557 Inits a BUFFER3 structure.
558 ********************************************************************/
560 void init_buffer3_str(BUFFER3 *str, char *buf, int len)
562 ZERO_STRUCTP(str);
564 /* set up string lengths. */
565 str->buf_max_len = len * 2;
566 str->buf_len = len * 2;
568 create_buffer3(str, str->buf_max_len);
570 /* store the string (null-terminated 8 bit chars into 16 bit chars) */
571 dos_struni2((char *)str->buffer, buf, str->buf_max_len);
574 /*******************************************************************
575 Inits a BUFFER3 structure from a hex string.
576 ********************************************************************/
578 void init_buffer3_hex(BUFFER3 *str, char *buf)
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);
585 /*******************************************************************
586 Inits a BUFFER3 structure.
587 ********************************************************************/
589 void init_buffer3_bytes(BUFFER3 *str, uint8 *buf, int len)
591 ZERO_STRUCTP(str);
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);
599 str->buf_len = buf != NULL ? len : 0;
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 ********************************************************************/
608 BOOL smb_io_buffer3(char *desc, BUFFER3 *buf3, prs_struct *ps, int depth)
610 if (buf3 == NULL)
611 return False;
613 prs_debug(ps, depth, desc, "smb_io_buffer3");
614 depth++;
616 if(!prs_align(ps))
617 return False;
619 if(!prs_uint32("uni_max_len", ps, depth, &buf3->buf_max_len))
620 return False;
622 if (UNMARSHALLING(ps)) {
623 buf3->buffer = prs_alloc_mem(ps, buf3->buf_max_len);
624 if (buf3->buffer == NULL)
625 return False;
628 if(!prs_uint8s(True, "buffer ", ps, depth, buf3->buffer, buf3->buf_max_len))
629 return False;
631 if(!prs_uint32("buf_len ", ps, depth, &buf3->buf_len))
632 return False;
634 return True;
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)
643 prs_debug(ps, depth, desc, "smb_io_buffer5");
644 depth++;
646 if (buf5 == NULL) return False;
648 prs_align(ps);
649 prs_uint32("buf_len", ps, depth, &(buf5->buf_len));
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;
658 prs_uint16s(True, "buffer", ps, depth, buf5->buffer, buf5->buf_len);
660 return True;
663 /*******************************************************************
664 Inits a BUFFER2 structure.
665 ********************************************************************/
667 void init_buffer2(BUFFER2 *str, uint8 *buf, int len)
669 ZERO_STRUCTP(str);
671 /* max buffer size (allocated size) */
672 str->buf_max_len = len;
673 str->undoc = 0;
674 str->buf_len = buf != NULL ? len : 0;
676 if (buf != NULL) {
677 if (!parse_misc_talloc)
678 parse_misc_talloc = talloc_init();
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));
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 ********************************************************************/
695 BOOL smb_io_buffer2(char *desc, BUFFER2 *buf2, uint32 buffer, prs_struct *ps, int depth)
697 if (buf2 == NULL)
698 return False;
700 if (buffer) {
702 prs_debug(ps, depth, desc, "smb_io_buffer2");
703 depth++;
705 if(!prs_align(ps))
706 return False;
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;
715 /* buffer advanced by indicated length of string
716 NOT by searching for null-termination */
718 if(!prs_buffer2(True, "buffer ", ps, depth, buf2))
719 return False;
721 } else {
723 prs_debug(ps, depth, desc, "smb_io_buffer2 - NULL");
724 depth++;
725 memset((char *)buf2, '\0', sizeof(*buf2));
728 return True;
731 /*******************************************************************
732 creates a UNISTR2 structure: sets up the buffer, too
733 ********************************************************************/
735 void init_buf_unistr2(UNISTR2 *str, uint32 *ptr, const char *buf)
737 if (buf != NULL) {
739 *ptr = 1;
740 init_unistr2(str, buf, strlen(buf)+1);
742 } else {
744 *ptr = 0;
745 init_unistr2(str, "", 0);
750 /*******************************************************************
751 Copies a UNISTR2 structure.
752 ********************************************************************/
754 void copy_unistr2(UNISTR2 *str, UNISTR2 *from)
757 /* set up string lengths. add one if string is not null-terminated */
758 str->uni_max_len = from->uni_max_len;
759 str->undoc = from->undoc;
760 str->uni_str_len = from->uni_str_len;
762 if (from->buffer == NULL)
763 return;
765 /* the string buffer is allocated to the maximum size
766 (the the length of the source string) to prevent
767 reallocation of memory. */
768 if (str->buffer == NULL) {
769 size_t len = from->uni_max_len * sizeof(uint16);
771 if (!parse_misc_talloc)
772 parse_misc_talloc = talloc_init();
774 if (len < MAX_UNISTRLEN)
775 len = MAX_UNISTRLEN;
776 len *= sizeof(uint16);
778 str->buffer = (uint16 *)talloc(parse_misc_talloc, len);
779 if ((str->buffer == NULL) && (len > 0 ))
781 smb_panic("copy_unistr2: malloc fail\n");
782 return;
786 /* copy the string */
787 memcpy(str->buffer, from->buffer, from->uni_max_len*sizeof(uint16));
790 /*******************************************************************
791 Creates a STRING2 structure.
792 ********************************************************************/
794 void init_string2(STRING2 *str, char *buf, int len)
796 int alloc_len = 0;
798 /* set up string lengths. */
799 str->str_max_len = len;
800 str->undoc = 0;
801 str->str_str_len = len;
803 /* store the string */
804 if(len != 0) {
805 if (!parse_misc_talloc)
806 parse_misc_talloc = talloc_init();
808 if (len < MAX_STRINGLEN)
809 alloc_len = MAX_STRINGLEN;
810 str->buffer = talloc(parse_misc_talloc, alloc_len);
811 if (str->buffer == NULL)
812 smb_panic("init_string2: malloc fail\n");
813 memcpy(str->buffer, buf, len);
817 /*******************************************************************
818 Reads or writes a STRING2 structure.
819 XXXX NOTE: STRING2 structures need NOT be null-terminated.
820 the str_str_len member tells you how long the string is;
821 the str_max_len member tells you how large the buffer is.
822 ********************************************************************/
824 BOOL smb_io_string2(char *desc, STRING2 *str2, uint32 buffer, prs_struct *ps, int depth)
826 if (str2 == NULL)
827 return False;
829 if (buffer) {
831 prs_debug(ps, depth, desc, "smb_io_string2");
832 depth++;
834 if(!prs_align(ps))
835 return False;
837 if(!prs_uint32("str_max_len", ps, depth, &str2->str_max_len))
838 return False;
839 if(!prs_uint32("undoc ", ps, depth, &str2->undoc))
840 return False;
841 if(!prs_uint32("str_str_len", ps, depth, &str2->str_str_len))
842 return False;
844 /* buffer advanced by indicated length of string
845 NOT by searching for null-termination */
846 if(!prs_string2(True, "buffer ", ps, depth, str2))
847 return False;
849 } else {
851 prs_debug(ps, depth, desc, "smb_io_string2 - NULL");
852 depth++;
853 memset((char *)str2, '\0', sizeof(*str2));
857 return True;
860 /*******************************************************************
861 Inits a UNISTR2 structure.
862 ********************************************************************/
864 void init_unistr2(UNISTR2 *str, const char *buf, size_t len)
866 ZERO_STRUCTP(str);
868 /* set up string lengths. */
869 str->uni_max_len = (uint32)len;
870 str->undoc = 0;
871 str->uni_str_len = (uint32)len;
873 if (!parse_misc_talloc)
874 parse_misc_talloc = talloc_init();
876 if (len < MAX_UNISTRLEN)
877 len = MAX_UNISTRLEN;
878 len *= sizeof(uint16);
880 str->buffer = (uint16 *)talloc(parse_misc_talloc, len);
881 if ((str->buffer == NULL) && (len > 0))
883 smb_panic("init_unistr2: malloc fail\n");
884 return;
887 /* store the string (null-terminated 8 bit chars into 16 bit chars) */
888 dos_struni2((char *)str->buffer, buf, len);
891 /*******************************************************************
892 Inits a UNISTR2 structure from a UNISTR
893 ********************************************************************/
894 void init_unistr2_from_unistr (UNISTR2 *to, UNISTR *from)
897 uint32 i;
899 /* the destination UNISTR2 should never be NULL.
900 if it is it is a programming error */
902 /* if the source UNISTR is NULL, then zero out
903 the destination string and return */
904 ZERO_STRUCTP (to);
905 if ((from == NULL) || (from->buffer == NULL))
906 return;
908 /* get the length; UNISTR must be NULL terminated */
909 i = 0;
910 while ((from->buffer)[i]!='\0')
911 i++;
912 i++; /* one more to catch the terminating NULL */
914 /* set up string lengths; uni_max_len is set to i+1
915 because we need to account for the final NULL termination */
916 to->uni_max_len = i;
917 to->undoc = 0;
918 to->uni_str_len = i;
920 if (!parse_misc_talloc)
921 parse_misc_talloc = talloc_init();
923 /* allocate the space and copy the string buffer */
924 to->buffer = (uint16 *)talloc(parse_misc_talloc, sizeof(uint16)*(to->uni_str_len));
925 if (to->buffer == NULL)
926 smb_panic("init_unistr2_from_unistr: malloc fail\n");
927 memcpy(to->buffer, from->buffer, to->uni_max_len*sizeof(uint16));
929 return;
933 /*******************************************************************
934 Reads or writes a UNISTR2 structure.
935 XXXX NOTE: UNISTR2 structures need NOT be null-terminated.
936 the uni_str_len member tells you how long the string is;
937 the uni_max_len member tells you how large the buffer is.
938 ********************************************************************/
940 BOOL smb_io_unistr2(char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
942 if (uni2 == NULL)
943 return False;
945 if (buffer) {
947 prs_debug(ps, depth, desc, "smb_io_unistr2");
948 depth++;
950 if(!prs_align(ps))
951 return False;
953 if(!prs_uint32("uni_max_len", ps, depth, &uni2->uni_max_len))
954 return False;
955 if(!prs_uint32("undoc ", ps, depth, &uni2->undoc))
956 return False;
957 if(!prs_uint32("uni_str_len", ps, depth, &uni2->uni_str_len))
958 return False;
960 /* buffer advanced by indicated length of string
961 NOT by searching for null-termination */
962 if(!prs_unistr2(True, "buffer ", ps, depth, uni2))
963 return False;
965 } else {
967 prs_debug(ps, depth, desc, "smb_io_unistr2 - NULL");
968 depth++;
969 memset((char *)uni2, '\0', sizeof(*uni2));
973 return True;
976 /*******************************************************************
977 Inits a DOM_RID2 structure.
978 ********************************************************************/
980 void init_dom_rid2(DOM_RID2 *rid2, uint32 rid, uint8 type, uint32 idx)
982 rid2->type = type;
983 rid2->rid = rid;
984 rid2->rid_idx = idx;
987 /*******************************************************************
988 Reads or writes a DOM_RID2 structure.
989 ********************************************************************/
991 BOOL smb_io_dom_rid2(char *desc, DOM_RID2 *rid2, prs_struct *ps, int depth)
993 if (rid2 == NULL)
994 return False;
996 prs_debug(ps, depth, desc, "smb_io_dom_rid2");
997 depth++;
999 if(!prs_align(ps))
1000 return False;
1002 if(!prs_uint8("type ", ps, depth, &rid2->type))
1003 return False;
1004 if(!prs_align(ps))
1005 return False;
1006 if(!prs_uint32("rid ", ps, depth, &rid2->rid))
1007 return False;
1008 if(!prs_uint32("rid_idx", ps, depth, &rid2->rid_idx))
1009 return False;
1011 return True;
1014 /*******************************************************************
1015 creates a DOM_RID3 structure.
1016 ********************************************************************/
1018 void init_dom_rid3(DOM_RID3 *rid3, uint32 rid, uint8 type)
1020 rid3->rid = rid;
1021 rid3->type1 = type;
1022 rid3->ptr_type = 0x1; /* non-zero, basically. */
1023 rid3->type2 = 0x1;
1024 rid3->unk = type;
1027 /*******************************************************************
1028 reads or writes a DOM_RID3 structure.
1029 ********************************************************************/
1031 BOOL smb_io_dom_rid3(char *desc, DOM_RID3 *rid3, prs_struct *ps, int depth)
1033 if (rid3 == NULL)
1034 return False;
1036 prs_debug(ps, depth, desc, "smb_io_dom_rid3");
1037 depth++;
1039 if(!prs_align(ps))
1040 return False;
1042 if(!prs_uint32("rid ", ps, depth, &rid3->rid))
1043 return False;
1044 if(!prs_uint32("type1 ", ps, depth, &rid3->type1))
1045 return False;
1046 if(!prs_uint32("ptr_type", ps, depth, &rid3->ptr_type))
1047 return False;
1048 if(!prs_uint32("type2 ", ps, depth, &rid3->type2))
1049 return False;
1050 if(!prs_uint32("unk ", ps, depth, &rid3->unk))
1051 return False;
1053 return True;
1056 /*******************************************************************
1057 Inits a DOM_RID4 structure.
1058 ********************************************************************/
1060 void init_dom_rid4(DOM_RID4 *rid4, uint16 unknown, uint16 attr, uint32 rid)
1062 rid4->unknown = unknown;
1063 rid4->attr = attr;
1064 rid4->rid = rid;
1067 /*******************************************************************
1068 Inits a DOM_CLNT_SRV structure.
1069 ********************************************************************/
1071 static void init_clnt_srv(DOM_CLNT_SRV *log, char *logon_srv, char *comp_name)
1073 DEBUG(5,("init_clnt_srv: %d\n", __LINE__));
1075 if (logon_srv != NULL) {
1076 log->undoc_buffer = 1;
1077 init_unistr2(&log->uni_logon_srv, logon_srv, strlen(logon_srv)+1);
1078 } else {
1079 log->undoc_buffer = 0;
1082 if (comp_name != NULL) {
1083 log->undoc_buffer2 = 1;
1084 init_unistr2(&log->uni_comp_name, comp_name, strlen(comp_name)+1);
1085 } else {
1086 log->undoc_buffer2 = 0;
1090 /*******************************************************************
1091 Inits or writes a DOM_CLNT_SRV structure.
1092 ********************************************************************/
1094 static BOOL smb_io_clnt_srv(char *desc, DOM_CLNT_SRV *log, prs_struct *ps, int depth)
1096 if (log == NULL)
1097 return False;
1099 prs_debug(ps, depth, desc, "smb_io_clnt_srv");
1100 depth++;
1102 if(!prs_align(ps))
1103 return False;
1105 if(!prs_uint32("undoc_buffer ", ps, depth, &log->undoc_buffer))
1106 return False;
1108 if (log->undoc_buffer != 0) {
1109 if(!smb_io_unistr2("unistr2", &log->uni_logon_srv, log->undoc_buffer, ps, depth))
1110 return False;
1113 if(!prs_align(ps))
1114 return False;
1116 if(!prs_uint32("undoc_buffer2", ps, depth, &log->undoc_buffer2))
1117 return False;
1119 if (log->undoc_buffer2 != 0) {
1120 if(!smb_io_unistr2("unistr2", &log->uni_comp_name, log->undoc_buffer2, ps, depth))
1121 return False;
1124 return True;
1127 /*******************************************************************
1128 Inits a DOM_LOG_INFO structure.
1129 ********************************************************************/
1131 void init_log_info(DOM_LOG_INFO *log, char *logon_srv, char *acct_name,
1132 uint16 sec_chan, char *comp_name)
1134 DEBUG(5,("make_log_info %d\n", __LINE__));
1136 log->undoc_buffer = 1;
1138 init_unistr2(&log->uni_logon_srv, logon_srv, strlen(logon_srv)+1);
1139 init_unistr2(&log->uni_acct_name, acct_name, strlen(acct_name)+1);
1141 log->sec_chan = sec_chan;
1143 init_unistr2(&log->uni_comp_name, comp_name, strlen(comp_name)+1);
1146 /*******************************************************************
1147 Reads or writes a DOM_LOG_INFO structure.
1148 ********************************************************************/
1150 BOOL smb_io_log_info(char *desc, DOM_LOG_INFO *log, prs_struct *ps, int depth)
1152 if (log == NULL)
1153 return False;
1155 prs_debug(ps, depth, desc, "smb_io_log_info");
1156 depth++;
1158 if(!prs_align(ps))
1159 return False;
1161 if(!prs_uint32("undoc_buffer", ps, depth, &log->undoc_buffer))
1162 return False;
1164 if(!smb_io_unistr2("unistr2", &log->uni_logon_srv, True, ps, depth))
1165 return False;
1166 if(!smb_io_unistr2("unistr2", &log->uni_acct_name, True, ps, depth))
1167 return False;
1169 if(!prs_uint16("sec_chan", ps, depth, &log->sec_chan))
1170 return False;
1172 if(!smb_io_unistr2("unistr2", &log->uni_comp_name, True, ps, depth))
1173 return False;
1175 return True;
1178 /*******************************************************************
1179 Reads or writes a DOM_CHAL structure.
1180 ********************************************************************/
1182 BOOL smb_io_chal(char *desc, DOM_CHAL *chal, prs_struct *ps, int depth)
1184 if (chal == NULL)
1185 return False;
1187 prs_debug(ps, depth, desc, "smb_io_chal");
1188 depth++;
1190 if(!prs_align(ps))
1191 return False;
1193 if(!prs_uint8s (False, "data", ps, depth, chal->data, 8))
1194 return False;
1196 return True;
1199 /*******************************************************************
1200 Reads or writes a DOM_CRED structure.
1201 ********************************************************************/
1203 BOOL smb_io_cred(char *desc, DOM_CRED *cred, prs_struct *ps, int depth)
1205 if (cred == NULL)
1206 return False;
1208 prs_debug(ps, depth, desc, "smb_io_cred");
1209 depth++;
1211 if(!prs_align(ps))
1212 return False;
1214 if(!smb_io_chal ("", &cred->challenge, ps, depth))
1215 return False;
1216 if(!smb_io_utime("", &cred->timestamp, ps, depth))
1217 return False;
1219 return True;
1222 /*******************************************************************
1223 Inits a DOM_CLNT_INFO2 structure.
1224 ********************************************************************/
1226 void init_clnt_info2(DOM_CLNT_INFO2 *clnt,
1227 char *logon_srv, char *comp_name,
1228 DOM_CRED *clnt_cred)
1230 DEBUG(5,("make_clnt_info: %d\n", __LINE__));
1232 init_clnt_srv(&(clnt->login), logon_srv, comp_name);
1234 if (clnt_cred != NULL) {
1235 clnt->ptr_cred = 1;
1236 memcpy(&(clnt->cred), clnt_cred, sizeof(clnt->cred));
1237 } else {
1238 clnt->ptr_cred = 0;
1242 /*******************************************************************
1243 Reads or writes a DOM_CLNT_INFO2 structure.
1244 ********************************************************************/
1246 BOOL smb_io_clnt_info2(char *desc, DOM_CLNT_INFO2 *clnt, prs_struct *ps, int depth)
1248 if (clnt == NULL)
1249 return False;
1251 prs_debug(ps, depth, desc, "smb_io_clnt_info2");
1252 depth++;
1254 if(!prs_align(ps))
1255 return False;
1257 if(!smb_io_clnt_srv("", &clnt->login, ps, depth))
1258 return False;
1260 if(!prs_align(ps))
1261 return False;
1263 if(!prs_uint32("ptr_cred", ps, depth, &clnt->ptr_cred))
1264 return False;
1265 if(!smb_io_cred("", &clnt->cred, ps, depth))
1266 return False;
1268 return True;
1271 /*******************************************************************
1272 Inits a DOM_CLNT_INFO structure.
1273 ********************************************************************/
1275 void init_clnt_info(DOM_CLNT_INFO *clnt,
1276 char *logon_srv, char *acct_name,
1277 uint16 sec_chan, char *comp_name,
1278 DOM_CRED *cred)
1280 DEBUG(5,("make_clnt_info\n"));
1282 init_log_info(&clnt->login, logon_srv, acct_name, sec_chan, comp_name);
1283 memcpy(&clnt->cred, cred, sizeof(clnt->cred));
1286 /*******************************************************************
1287 Reads or writes a DOM_CLNT_INFO structure.
1288 ********************************************************************/
1290 BOOL smb_io_clnt_info(char *desc, DOM_CLNT_INFO *clnt, prs_struct *ps, int depth)
1292 if (clnt == NULL)
1293 return False;
1295 prs_debug(ps, depth, desc, "smb_io_clnt_info");
1296 depth++;
1298 if(!prs_align(ps))
1299 return False;
1301 if(!smb_io_log_info("", &clnt->login, ps, depth))
1302 return False;
1303 if(!smb_io_cred("", &clnt->cred, ps, depth))
1304 return False;
1306 return True;
1309 /*******************************************************************
1310 Inits a DOM_LOGON_ID structure.
1311 ********************************************************************/
1313 void init_logon_id(DOM_LOGON_ID *log, uint32 log_id_low, uint32 log_id_high)
1315 DEBUG(5,("make_logon_id: %d\n", __LINE__));
1317 log->low = log_id_low;
1318 log->high = log_id_high;
1321 /*******************************************************************
1322 Reads or writes a DOM_LOGON_ID structure.
1323 ********************************************************************/
1325 BOOL smb_io_logon_id(char *desc, DOM_LOGON_ID *log, prs_struct *ps, int depth)
1327 if (log == NULL)
1328 return False;
1330 prs_debug(ps, depth, desc, "smb_io_logon_id");
1331 depth++;
1333 if(!prs_align(ps))
1334 return False;
1336 if(!prs_uint32("low ", ps, depth, &log->low ))
1337 return False;
1338 if(!prs_uint32("high", ps, depth, &log->high))
1339 return False;
1341 return True;
1344 /*******************************************************************
1345 Inits an OWF_INFO structure.
1346 ********************************************************************/
1348 void init_owf_info(OWF_INFO *hash, uint8 data[16])
1350 DEBUG(5,("init_owf_info: %d\n", __LINE__));
1352 if (data != NULL)
1353 memcpy(hash->data, data, sizeof(hash->data));
1354 else
1355 memset((char *)hash->data, '\0', sizeof(hash->data));
1358 /*******************************************************************
1359 Reads or writes an OWF_INFO structure.
1360 ********************************************************************/
1362 BOOL smb_io_owf_info(char *desc, OWF_INFO *hash, prs_struct *ps, int depth)
1364 if (hash == NULL)
1365 return False;
1367 prs_debug(ps, depth, desc, "smb_io_owf_info");
1368 depth++;
1370 if(!prs_align(ps))
1371 return False;
1373 if(!prs_uint8s (False, "data", ps, depth, hash->data, 16))
1374 return False;
1376 return True;
1379 /*******************************************************************
1380 Reads or writes a DOM_GID structure.
1381 ********************************************************************/
1383 BOOL smb_io_gid(char *desc, DOM_GID *gid, prs_struct *ps, int depth)
1385 if (gid == NULL)
1386 return False;
1388 prs_debug(ps, depth, desc, "smb_io_gid");
1389 depth++;
1391 if(!prs_align(ps))
1392 return False;
1394 if(!prs_uint32("g_rid", ps, depth, &gid->g_rid))
1395 return False;
1396 if(!prs_uint32("attr ", ps, depth, &gid->attr))
1397 return False;
1399 return True;
1402 /*******************************************************************
1403 Reads or writes an POLICY_HND structure.
1404 ********************************************************************/
1406 BOOL smb_io_pol_hnd(char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
1408 if (pol == NULL)
1409 return False;
1411 prs_debug(ps, depth, desc, "smb_io_pol_hnd");
1412 depth++;
1414 if(!prs_align(ps))
1415 return False;
1417 if(!prs_uint8s (False, "data", ps, depth, pol->data, POL_HND_SIZE))
1418 return False;
1420 return True;
1423 /*******************************************************************
1424 Reads or writes a dom query structure.
1425 ********************************************************************/
1427 static BOOL smb_io_dom_query(char *desc, DOM_QUERY *d_q, prs_struct *ps, int depth)
1429 if (d_q == NULL)
1430 return False;
1432 prs_debug(ps, depth, desc, "smb_io_dom_query");
1433 depth++;
1435 if(!prs_align(ps))
1436 return False;
1438 if(!prs_uint16("uni_dom_max_len", ps, depth, &d_q->uni_dom_max_len)) /* domain name string length * 2 */
1439 return False;
1440 if(!prs_uint16("uni_dom_str_len", ps, depth, &d_q->uni_dom_str_len)) /* domain name string length * 2 */
1441 return False;
1443 if(!prs_uint32("buffer_dom_name", ps, depth, &d_q->buffer_dom_name)) /* undocumented domain name string buffer pointer */
1444 return False;
1445 if(!prs_uint32("buffer_dom_sid ", ps, depth, &d_q->buffer_dom_sid)) /* undocumented domain SID string buffer pointer */
1446 return False;
1448 if(!smb_io_unistr2("unistr2", &d_q->uni_domain_name, d_q->buffer_dom_name, ps, depth)) /* domain name (unicode string) */
1449 return False;
1451 if(!prs_align(ps))
1452 return False;
1454 if (d_q->buffer_dom_sid != 0) {
1455 if(!smb_io_dom_sid2("", &d_q->dom_sid, ps, depth)) /* domain SID */
1456 return False;
1457 } else {
1458 memset((char *)&d_q->dom_sid, '\0', sizeof(d_q->dom_sid));
1461 return True;
1464 /*******************************************************************
1465 Reads or writes a dom query structure.
1466 ********************************************************************/
1468 BOOL smb_io_dom_query_3(char *desc, DOM_QUERY_3 *d_q, prs_struct *ps, int depth)
1470 return smb_io_dom_query("", d_q, ps, depth);
1473 /*******************************************************************
1474 Reads or writes a dom query structure.
1475 ********************************************************************/
1477 BOOL smb_io_dom_query_5(char *desc, DOM_QUERY_3 *d_q, prs_struct *ps, int depth)
1479 return smb_io_dom_query("", d_q, ps, depth);
1483 /*******************************************************************
1484 Reads or writes a UNISTR3 structure.
1485 ********************************************************************/
1487 BOOL smb_io_unistr3(char *desc, UNISTR3 *name, prs_struct *ps, int depth)
1489 if (name == NULL)
1490 return False;
1492 prs_debug(ps, depth, desc, "smb_io_unistr3");
1493 depth++;
1495 if(!prs_align(ps))
1496 return False;
1498 if(!prs_uint32("uni_str_len", ps, depth, &name->uni_str_len))
1499 return False;
1501 /* don't know if len is specified by uni_str_len member... */
1502 /* assume unicode string is unicode-null-terminated, instead */
1504 if(!prs_unistr3(True, "unistr", name, ps, depth))
1505 return False;
1507 return True;
1511 /*******************************************************************
1512 Stream a uint64_struct
1513 ********************************************************************/
1514 BOOL prs_uint64(char *name, prs_struct *ps, int depth, UINT64_S *data64)
1516 return prs_uint32(name, ps, depth+1, &data64->low) &&
1517 prs_uint32(name, ps, depth+1, &data64->high);