sync with HEAD
[Samba.git] / source / rpc_parse / parse_misc.c
blob466653af52c703c614a5518593f05eeb58debd75
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 /****************************************************************************
28 A temporary TALLOC context for things like unistrs, that is valid for
29 the life of a complete RPC call.
30 ****************************************************************************/
32 static TALLOC_CTX *current_rpc_talloc = NULL;
34 TALLOC_CTX *get_current_rpc_talloc(void)
36 return current_rpc_talloc;
39 void set_current_rpc_talloc( TALLOC_CTX *ctx)
41 current_rpc_talloc = ctx;
44 static TALLOC_CTX *main_loop_talloc = NULL;
46 /*******************************************************************
47 free up temporary memory - called from the main loop
48 ********************************************************************/
50 void main_loop_talloc_free(void)
52 if (!main_loop_talloc)
53 return;
54 talloc_destroy(main_loop_talloc);
55 main_loop_talloc = NULL;
58 /*******************************************************************
59 Get a talloc context that is freed in the main loop...
60 ********************************************************************/
62 TALLOC_CTX *main_loop_talloc_get(void)
64 if (!main_loop_talloc) {
65 main_loop_talloc = talloc_init();
66 if (!main_loop_talloc)
67 smb_panic("main_loop_talloc: malloc fail\n");
70 return main_loop_talloc;
73 /*******************************************************************
74 Try and get a talloc context. Get the rpc one if possible, else
75 get the main loop one. The main loop one is more dangerous as it
76 goes away between packets, the rpc one will stay around for as long
77 as a current RPC lasts.
78 ********************************************************************/
80 TALLOC_CTX *get_talloc_ctx(void)
82 TALLOC_CTX *tc = get_current_rpc_talloc();
84 if (tc)
85 return tc;
86 return main_loop_talloc_get();
89 /*******************************************************************
90 Reads or writes a UTIME type.
91 ********************************************************************/
93 static BOOL smb_io_utime(char *desc, UTIME *t, prs_struct *ps, int depth)
95 if (t == NULL)
96 return False;
98 prs_debug(ps, depth, desc, "smb_io_utime");
99 depth++;
101 if(!prs_align(ps))
102 return False;
104 if(!prs_uint32 ("time", ps, depth, &t->time))
105 return False;
107 return True;
110 /*******************************************************************
111 Reads or writes an NTTIME structure.
112 ********************************************************************/
114 BOOL smb_io_time(char *desc, NTTIME *nttime, prs_struct *ps, int depth)
116 if (nttime == NULL)
117 return False;
119 prs_debug(ps, depth, desc, "smb_io_time");
120 depth++;
122 if(!prs_align(ps))
123 return False;
125 if(!prs_uint32("low ", ps, depth, &nttime->low)) /* low part */
126 return False;
127 if(!prs_uint32("high", ps, depth, &nttime->high)) /* high part */
128 return False;
130 return True;
133 /*******************************************************************
134 Reads or writes a LOOKUP_LEVEL structure.
135 ********************************************************************/
137 BOOL smb_io_lookup_level(char *desc, LOOKUP_LEVEL *level, prs_struct *ps, int depth)
139 if (level == NULL)
140 return False;
142 prs_debug(ps, depth, desc, "smb_io_lookup_level");
143 depth++;
145 if(!prs_align(ps))
146 return False;
147 if(!prs_uint16("value", ps, depth, &level->value))
148 return False;
149 if(!prs_align(ps))
150 return False;
152 return True;
155 /*******************************************************************
156 Gets an enumeration handle from an ENUM_HND structure.
157 ********************************************************************/
159 uint32 get_enum_hnd(ENUM_HND *enh)
161 return (enh && enh->ptr_hnd != 0) ? enh->handle : 0;
164 /*******************************************************************
165 Inits an ENUM_HND structure.
166 ********************************************************************/
168 void init_enum_hnd(ENUM_HND *enh, uint32 hnd)
170 DEBUG(5,("smb_io_enum_hnd\n"));
172 enh->ptr_hnd = (hnd != 0) ? 1 : 0;
173 enh->handle = hnd;
176 /*******************************************************************
177 Reads or writes an ENUM_HND structure.
178 ********************************************************************/
180 BOOL smb_io_enum_hnd(char *desc, ENUM_HND *hnd, prs_struct *ps, int depth)
182 if (hnd == NULL)
183 return False;
185 prs_debug(ps, depth, desc, "smb_io_enum_hnd");
186 depth++;
188 if(!prs_align(ps))
189 return False;
191 if(!prs_uint32("ptr_hnd", ps, depth, &hnd->ptr_hnd)) /* pointer */
192 return False;
194 if (hnd->ptr_hnd != 0) {
195 if(!prs_uint32("handle ", ps, depth, &hnd->handle )) /* enum handle */
196 return False;
199 return True;
202 /*******************************************************************
203 Reads or writes a DOM_SID structure.
204 ********************************************************************/
206 BOOL smb_io_dom_sid(char *desc, DOM_SID *sid, prs_struct *ps, int depth)
208 int i;
210 if (sid == NULL)
211 return False;
213 prs_debug(ps, depth, desc, "smb_io_dom_sid");
214 depth++;
216 if(!prs_align(ps))
217 return False;
219 if(!prs_uint8 ("sid_rev_num", ps, depth, &sid->sid_rev_num))
220 return False;
221 if(!prs_uint8 ("num_auths ", ps, depth, &sid->num_auths))
222 return False;
224 for (i = 0; i < 6; i++)
226 fstring tmp;
227 slprintf(tmp, sizeof(tmp) - 1, "id_auth[%d] ", i);
228 if(!prs_uint8 (tmp, ps, depth, &sid->id_auth[i]))
229 return False;
232 /* oops! XXXX should really issue a warning here... */
233 if (sid->num_auths > MAXSUBAUTHS)
234 sid->num_auths = MAXSUBAUTHS;
236 if(!prs_uint32s(False, "sub_auths ", ps, depth, sid->sub_auths, sid->num_auths))
237 return False;
239 return True;
242 /*******************************************************************
243 Inits a DOM_SID structure.
245 BIG NOTE: this function only does SIDS where the identauth is not >= 2^32
246 identauth >= 2^32 can be detected because it will be specified in hex
247 ********************************************************************/
249 void init_dom_sid(DOM_SID *sid, char *str_sid)
251 pstring domsid;
252 int identauth;
253 char *p;
255 if (str_sid == NULL)
257 DEBUG(4,("netlogon domain SID: none\n"));
258 sid->sid_rev_num = 0;
259 sid->num_auths = 0;
260 return;
263 pstrcpy(domsid, str_sid);
265 DEBUG(4,("init_dom_sid %d SID: %s\n", __LINE__, domsid));
267 /* assume, but should check, that domsid starts "S-" */
268 p = strtok(domsid+2,"-");
269 sid->sid_rev_num = atoi(p);
271 /* identauth in decimal should be < 2^32 */
272 /* identauth in hex should be >= 2^32 */
273 identauth = atoi(strtok(0,"-"));
275 DEBUG(4,("netlogon rev %d\n", sid->sid_rev_num));
276 DEBUG(4,("netlogon %s ia %d\n", p, identauth));
278 sid->id_auth[0] = 0;
279 sid->id_auth[1] = 0;
280 sid->id_auth[2] = (identauth & 0xff000000) >> 24;
281 sid->id_auth[3] = (identauth & 0x00ff0000) >> 16;
282 sid->id_auth[4] = (identauth & 0x0000ff00) >> 8;
283 sid->id_auth[5] = (identauth & 0x000000ff);
285 sid->num_auths = 0;
287 while ((p = strtok(0, "-")) != NULL && sid->num_auths < MAXSUBAUTHS)
288 sid->sub_auths[sid->num_auths++] = atoi(p);
290 DEBUG(4,("init_dom_sid: %d SID: %s\n", __LINE__, domsid));
293 /*******************************************************************
294 Inits a DOM_SID2 structure.
295 ********************************************************************/
297 void init_dom_sid2(DOM_SID2 *sid2, const DOM_SID *sid)
299 sid2->sid = *sid;
300 sid2->num_auths = sid2->sid.num_auths;
303 /*******************************************************************
304 Reads or writes a DOM_SID2 structure.
305 ********************************************************************/
307 BOOL smb_io_dom_sid2(char *desc, DOM_SID2 *sid, prs_struct *ps, int depth)
309 if (sid == NULL)
310 return False;
312 prs_debug(ps, depth, desc, "smb_io_dom_sid2");
313 depth++;
315 if(!prs_align(ps))
316 return False;
318 if(!prs_uint32("num_auths", ps, depth, &sid->num_auths))
319 return False;
321 if(!smb_io_dom_sid("sid", &sid->sid, ps, depth))
322 return False;
324 return True;
327 /*******************************************************************
328 creates a STRHDR structure.
329 ********************************************************************/
331 void init_str_hdr(STRHDR *hdr, int max_len, int len, uint32 buffer)
333 hdr->str_max_len = max_len;
334 hdr->str_str_len = len;
335 hdr->buffer = buffer;
338 /*******************************************************************
339 Reads or writes a STRHDR structure.
340 ********************************************************************/
342 BOOL smb_io_strhdr(char *desc, STRHDR *hdr, prs_struct *ps, int depth)
344 if (hdr == NULL)
345 return False;
347 prs_debug(ps, depth, desc, "smb_io_strhdr");
348 depth++;
350 prs_align(ps);
352 if(!prs_uint16("str_str_len", ps, depth, &hdr->str_str_len))
353 return False;
354 if(!prs_uint16("str_max_len", ps, depth, &hdr->str_max_len))
355 return False;
356 if(!prs_uint32("buffer ", ps, depth, &hdr->buffer))
357 return False;
359 return True;
362 /*******************************************************************
363 Inits a UNIHDR structure.
364 ********************************************************************/
366 void init_uni_hdr(UNIHDR *hdr, int len)
368 hdr->uni_str_len = 2 * len;
369 hdr->uni_max_len = 2 * len;
370 hdr->buffer = len != 0 ? 1 : 0;
373 /*******************************************************************
374 Reads or writes a UNIHDR structure.
375 ********************************************************************/
377 BOOL smb_io_unihdr(char *desc, UNIHDR *hdr, prs_struct *ps, int depth)
379 if (hdr == NULL)
380 return False;
382 prs_debug(ps, depth, desc, "smb_io_unihdr");
383 depth++;
385 if(!prs_align(ps))
386 return False;
388 if(!prs_uint16("uni_str_len", ps, depth, &hdr->uni_str_len))
389 return False;
390 if(!prs_uint16("uni_max_len", ps, depth, &hdr->uni_max_len))
391 return False;
392 if(!prs_uint32("buffer ", ps, depth, &hdr->buffer))
393 return False;
395 return True;
398 /*******************************************************************
399 Inits a BUFHDR structure.
400 ********************************************************************/
402 void init_buf_hdr(BUFHDR *hdr, int max_len, int len)
404 hdr->buf_max_len = max_len;
405 hdr->buf_len = len;
408 /*******************************************************************
409 prs_uint16 wrapper. Call this and it sets up a pointer to where the
410 uint16 should be stored, or gets the size if reading.
411 ********************************************************************/
413 BOOL smb_io_hdrbuf_pre(char *desc, BUFHDR *hdr, prs_struct *ps, int depth, uint32 *offset)
415 (*offset) = prs_offset(ps);
416 if (ps->io) {
418 /* reading. */
420 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
421 return False;
423 } else {
425 /* writing. */
427 if(!prs_set_offset(ps, prs_offset(ps) + (sizeof(uint32) * 2)))
428 return False;
431 return True;
434 /*******************************************************************
435 smb_io_hdrbuf wrapper. Call this and it retrospectively stores the size.
436 Does nothing on reading, as that is already handled by ...._pre()
437 ********************************************************************/
439 BOOL smb_io_hdrbuf_post(char *desc, BUFHDR *hdr, prs_struct *ps, int depth,
440 uint32 ptr_hdrbuf, uint32 max_len, uint32 len)
442 if (!ps->io) {
443 /* writing: go back and do a retrospective job. i hate this */
445 uint32 old_offset = prs_offset(ps);
447 init_buf_hdr(hdr, max_len, len);
448 if(!prs_set_offset(ps, ptr_hdrbuf))
449 return False;
450 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
451 return False;
453 if(!prs_set_offset(ps, old_offset))
454 return False;
457 return True;
460 /*******************************************************************
461 Reads or writes a BUFHDR structure.
462 ********************************************************************/
464 BOOL smb_io_hdrbuf(char *desc, BUFHDR *hdr, prs_struct *ps, int depth)
466 if (hdr == NULL)
467 return False;
469 prs_debug(ps, depth, desc, "smb_io_hdrbuf");
470 depth++;
472 if(!prs_align(ps))
473 return False;
475 if(!prs_uint32("buf_max_len", ps, depth, &hdr->buf_max_len))
476 return False;
477 if(!prs_uint32("buf_len ", ps, depth, &hdr->buf_len))
478 return False;
480 return True;
483 /*******************************************************************
484 creates a UNIHDR2 structure.
485 ********************************************************************/
487 void init_uni_hdr2(UNIHDR2 *hdr, int len)
489 init_uni_hdr(&hdr->unihdr, len);
490 hdr->buffer = (len > 0) ? 1 : 0;
493 /*******************************************************************
494 Reads or writes a UNIHDR2 structure.
495 ********************************************************************/
497 BOOL smb_io_unihdr2(char *desc, UNIHDR2 *hdr2, prs_struct *ps, int depth)
499 if (hdr2 == NULL)
500 return False;
502 prs_debug(ps, depth, desc, "smb_io_unihdr2");
503 depth++;
505 if(!prs_align(ps))
506 return False;
508 if(!smb_io_unihdr("hdr", &hdr2->unihdr, ps, depth))
509 return False;
510 if(!prs_uint32("buffer", ps, depth, &hdr2->buffer))
511 return False;
513 return True;
516 /*******************************************************************
517 Inits a UNISTR structure.
518 ********************************************************************/
520 void init_unistr(UNISTR *str, const char *buf)
522 size_t len;
524 if (buf == NULL) {
525 str->buffer = NULL;
526 return;
530 len = strlen(buf) + 1;
532 if (len < MAX_UNISTRLEN)
533 len = MAX_UNISTRLEN;
534 len *= sizeof(uint16);
536 str->buffer = (uint16 *)talloc_zero(get_talloc_ctx(), len);
537 if (str->buffer == NULL)
538 smb_panic("init_unistr: malloc fail\n");
540 /* store the string (null-terminated copy) */
541 dos_struni2((char *)str->buffer, buf, len);
544 /*******************************************************************
545 reads or writes a UNISTR structure.
546 XXXX NOTE: UNISTR structures NEED to be null-terminated.
547 ********************************************************************/
549 BOOL smb_io_unistr(char *desc, UNISTR *uni, prs_struct *ps, int depth)
551 if (uni == NULL)
552 return False;
554 prs_debug(ps, depth, desc, "smb_io_unistr");
555 depth++;
557 if(!prs_unistr("unistr", ps, depth, uni))
558 return False;
560 return True;
563 /*******************************************************************
564 Allocate the BUFFER3 memory.
565 ********************************************************************/
567 static void create_buffer3(BUFFER3 *str, size_t len)
569 if (len < MAX_BUFFERLEN)
570 len = MAX_BUFFERLEN;
572 str->buffer = talloc_zero(get_talloc_ctx(), len);
573 if (str->buffer == NULL)
574 smb_panic("create_buffer3: talloc fail\n");
578 /*******************************************************************
579 Inits a BUFFER3 structure from a uint32
580 ********************************************************************/
582 void init_buffer3_uint32(BUFFER3 *str, uint32 val)
584 ZERO_STRUCTP(str);
586 /* set up string lengths. */
587 str->buf_max_len = sizeof(uint32);
588 str->buf_len = sizeof(uint32);
590 create_buffer3(str, sizeof(uint32));
591 SIVAL(str->buffer, 0, val);
594 /*******************************************************************
595 Inits a BUFFER3 structure.
596 ********************************************************************/
598 void init_buffer3_str(BUFFER3 *str, char *buf, int len)
600 ZERO_STRUCTP(str);
602 /* set up string lengths. */
603 str->buf_max_len = len * 2;
604 str->buf_len = len * 2;
606 create_buffer3(str, str->buf_max_len);
608 /* store the string (null-terminated 8 bit chars into 16 bit chars) */
609 dos_struni2((char *)str->buffer, buf, str->buf_max_len);
612 /*******************************************************************
613 Inits a BUFFER3 structure from a hex string.
614 ********************************************************************/
616 void init_buffer3_hex(BUFFER3 *str, char *buf)
618 ZERO_STRUCTP(str);
619 create_buffer3(str, strlen(buf));
620 str->buf_max_len = str->buf_len = strhex_to_str((char *)str->buffer, sizeof(str->buffer), buf);
623 /*******************************************************************
624 Inits a BUFFER3 structure.
625 ********************************************************************/
627 void init_buffer3_bytes(BUFFER3 *str, uint8 *buf, int len)
629 ZERO_STRUCTP(str);
631 /* max buffer size (allocated size) */
632 str->buf_max_len = len;
633 if (buf != NULL) {
634 create_buffer3(str, len);
635 memcpy(str->buffer, buf, len);
637 str->buf_len = buf != NULL ? len : 0;
640 /*******************************************************************
641 Reads or writes a BUFFER3 structure.
642 the uni_max_len member tells you how large the buffer is.
643 the uni_str_len member tells you how much of the buffer is really used.
644 ********************************************************************/
646 BOOL smb_io_buffer3(char *desc, BUFFER3 *buf3, prs_struct *ps, int depth)
648 if (buf3 == NULL)
649 return False;
651 prs_debug(ps, depth, desc, "smb_io_buffer3");
652 depth++;
654 if(!prs_align(ps))
655 return False;
657 if(!prs_uint32("uni_max_len", ps, depth, &buf3->buf_max_len))
658 return False;
660 if (UNMARSHALLING(ps)) {
661 buf3->buffer = (unsigned char *)prs_alloc_mem(ps, buf3->buf_max_len);
662 if (buf3->buffer == NULL)
663 return False;
666 if(!prs_uint8s(True, "buffer ", ps, depth, buf3->buffer, buf3->buf_max_len))
667 return False;
669 if(!prs_uint32("buf_len ", ps, depth, &buf3->buf_len))
670 return False;
672 return True;
675 /*******************************************************************
676 reads or writes a BUFFER5 structure.
677 the buf_len member tells you how large the buffer is.
678 ********************************************************************/
679 BOOL smb_io_buffer5(char *desc, BUFFER5 *buf5, prs_struct *ps, int depth)
681 prs_debug(ps, depth, desc, "smb_io_buffer5");
682 depth++;
684 if (buf5 == NULL) return False;
686 if(!prs_align(ps))
687 return False;
688 if(!prs_uint32("buf_len", ps, depth, &buf5->buf_len))
689 return False;
692 if(!prs_buffer5(True, "buffer" , ps, depth, buf5))
693 return False;
695 return True;
698 /*******************************************************************
699 Inits a BUFFER2 structure.
700 ********************************************************************/
702 void init_buffer2(BUFFER2 *str, uint8 *buf, int len)
704 ZERO_STRUCTP(str);
706 /* max buffer size (allocated size) */
707 str->buf_max_len = len;
708 str->undoc = 0;
709 str->buf_len = buf != NULL ? len : 0;
711 if (buf != NULL) {
712 if (len < MAX_BUFFERLEN)
713 len = MAX_BUFFERLEN;
714 str->buffer = talloc_zero(get_talloc_ctx(), len);
715 if (str->buffer == NULL)
716 smb_panic("init_buffer2: talloc fail\n");
717 memcpy(str->buffer, buf, MIN(str->buf_len, len));
721 /*******************************************************************
722 Reads or writes a BUFFER2 structure.
723 the uni_max_len member tells you how large the buffer is.
724 the uni_str_len member tells you how much of the buffer is really used.
725 ********************************************************************/
727 BOOL smb_io_buffer2(char *desc, BUFFER2 *buf2, uint32 buffer, prs_struct *ps, int depth)
729 if (buf2 == NULL)
730 return False;
732 if (buffer) {
734 prs_debug(ps, depth, desc, "smb_io_buffer2");
735 depth++;
737 if(!prs_align(ps))
738 return False;
740 if(!prs_uint32("uni_max_len", ps, depth, &buf2->buf_max_len))
741 return False;
742 if(!prs_uint32("undoc ", ps, depth, &buf2->undoc))
743 return False;
744 if(!prs_uint32("buf_len ", ps, depth, &buf2->buf_len))
745 return False;
747 /* buffer advanced by indicated length of string
748 NOT by searching for null-termination */
750 if(!prs_buffer2(True, "buffer ", ps, depth, buf2))
751 return False;
753 } else {
755 prs_debug(ps, depth, desc, "smb_io_buffer2 - NULL");
756 depth++;
757 memset((char *)buf2, '\0', sizeof(*buf2));
760 return True;
763 /*******************************************************************
764 creates a UNISTR2 structure: sets up the buffer, too
765 ********************************************************************/
767 void init_buf_unistr2(UNISTR2 *str, uint32 *ptr, const char *buf)
769 if (buf != NULL) {
771 *ptr = 1;
772 init_unistr2(str, buf, strlen(buf)+1);
774 } else {
776 *ptr = 0;
777 init_unistr2(str, "", 0);
782 /*******************************************************************
783 Copies a UNISTR2 structure.
784 ********************************************************************/
786 void copy_unistr2(UNISTR2 *str, const UNISTR2 *from)
789 /* set up string lengths. add one if string is not null-terminated */
790 str->uni_max_len = from->uni_max_len;
791 str->undoc = from->undoc;
792 str->uni_str_len = from->uni_str_len;
794 if (from->buffer == NULL)
795 return;
797 /* the string buffer is allocated to the maximum size
798 (the the length of the source string) to prevent
799 reallocation of memory. */
800 if (str->buffer == NULL) {
801 size_t len = from->uni_max_len * sizeof(uint16);
803 if (len < MAX_UNISTRLEN)
804 len = MAX_UNISTRLEN;
805 len *= sizeof(uint16);
807 str->buffer = (uint16 *)talloc_zero(get_talloc_ctx(), len);
808 if ((str->buffer == NULL) && (len > 0 ))
810 smb_panic("copy_unistr2: talloc fail\n");
811 return;
815 /* copy the string */
816 memcpy(str->buffer, from->buffer, from->uni_max_len*sizeof(uint16));
819 /*******************************************************************
820 Creates a STRING2 structure.
821 ********************************************************************/
823 void init_string2(STRING2 *str, const char *buf, int max_len, int str_len)
825 int alloc_len = 0;
827 /* set up string lengths. */
828 str->str_max_len = max_len;
829 str->undoc = 0;
830 str->str_str_len = str_len;
832 /* store the string */
833 if(str_len != 0) {
834 if (str_len < MAX_STRINGLEN)
835 alloc_len = MAX_STRINGLEN;
836 str->buffer = talloc_zero(get_talloc_ctx(), alloc_len);
837 if (str->buffer == NULL)
838 smb_panic("init_string2: malloc fail\n");
839 memcpy(str->buffer, buf, str_len);
843 /*******************************************************************
844 Reads or writes a STRING2 structure.
845 XXXX NOTE: STRING2 structures need NOT be null-terminated.
846 the str_str_len member tells you how long the string is;
847 the str_max_len member tells you how large the buffer is.
848 ********************************************************************/
850 BOOL smb_io_string2(char *desc, STRING2 *str2, uint32 buffer, prs_struct *ps, int depth)
852 if (str2 == NULL)
853 return False;
855 if (buffer) {
857 prs_debug(ps, depth, desc, "smb_io_string2");
858 depth++;
860 if(!prs_align(ps))
861 return False;
863 if(!prs_uint32("str_max_len", ps, depth, &str2->str_max_len))
864 return False;
865 if(!prs_uint32("undoc ", ps, depth, &str2->undoc))
866 return False;
867 if(!prs_uint32("str_str_len", ps, depth, &str2->str_str_len))
868 return False;
870 /* buffer advanced by indicated length of string
871 NOT by searching for null-termination */
872 if(!prs_string2(True, "buffer ", ps, depth, str2))
873 return False;
875 } else {
877 prs_debug(ps, depth, desc, "smb_io_string2 - NULL");
878 depth++;
879 memset((char *)str2, '\0', sizeof(*str2));
883 return True;
886 /*******************************************************************
887 Inits a UNISTR2 structure.
888 ********************************************************************/
890 void init_unistr2(UNISTR2 *str, const char *buf, size_t len)
892 ZERO_STRUCTP(str);
894 /* set up string lengths. */
895 str->uni_max_len = (uint32)len;
896 str->undoc = 0;
897 str->uni_str_len = (uint32)len;
899 if (len < MAX_UNISTRLEN)
900 len = MAX_UNISTRLEN;
901 len *= sizeof(uint16);
903 str->buffer = (uint16 *)talloc_zero(get_talloc_ctx(), len);
904 if ((str->buffer == NULL) && (len > 0))
906 smb_panic("init_unistr2: malloc fail\n");
907 return;
911 * don't move this test above ! The UNISTR2 must be initialized !!!
912 * jfm, 7/7/2001.
914 if (buf==NULL)
915 return;
917 /* store the string (null-terminated 8 bit chars into 16 bit chars) */
918 dos_struni2((char *)str->buffer, buf, len);
921 /*******************************************************************
922 Inits a UNISTR2 structure from a UNISTR
923 ********************************************************************/
924 void init_unistr2_from_unistr (UNISTR2 *to, UNISTR *from)
927 uint32 i;
929 /* the destination UNISTR2 should never be NULL.
930 if it is it is a programming error */
932 /* if the source UNISTR is NULL, then zero out
933 the destination string and return */
934 ZERO_STRUCTP (to);
935 if ((from == NULL) || (from->buffer == NULL))
936 return;
938 /* get the length; UNISTR must be NULL terminated */
939 i = 0;
940 while ((from->buffer)[i]!='\0')
941 i++;
942 i++; /* one more to catch the terminating NULL */
943 /* is this necessary -- jerry? I need to think */
945 /* set up string lengths; uni_max_len is set to i+1
946 because we need to account for the final NULL termination */
947 to->uni_max_len = i;
948 to->undoc = 0;
949 to->uni_str_len = i;
951 /* allocate the space and copy the string buffer */
952 to->buffer = (uint16 *)talloc_zero(get_talloc_ctx(), sizeof(uint16)*(to->uni_str_len));
953 if (to->buffer == NULL)
954 smb_panic("init_unistr2_from_unistr: malloc fail\n");
955 memcpy(to->buffer, from->buffer, to->uni_max_len*sizeof(uint16));
957 return;
961 /*******************************************************************
962 Reads or writes a UNISTR2 structure.
963 XXXX NOTE: UNISTR2 structures need NOT be null-terminated.
964 the uni_str_len member tells you how long the string is;
965 the uni_max_len member tells you how large the buffer is.
966 ********************************************************************/
968 BOOL smb_io_unistr2(char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
970 if (uni2 == NULL)
971 return False;
973 if (buffer) {
975 prs_debug(ps, depth, desc, "smb_io_unistr2");
976 depth++;
978 if(!prs_align(ps))
979 return False;
981 if(!prs_uint32("uni_max_len", ps, depth, &uni2->uni_max_len))
982 return False;
983 if(!prs_uint32("undoc ", ps, depth, &uni2->undoc))
984 return False;
985 if(!prs_uint32("uni_str_len", ps, depth, &uni2->uni_str_len))
986 return False;
988 /* buffer advanced by indicated length of string
989 NOT by searching for null-termination */
990 if(!prs_unistr2(True, "buffer ", ps, depth, uni2))
991 return False;
993 } else {
995 prs_debug(ps, depth, desc, "smb_io_unistr2 - NULL");
996 depth++;
997 memset((char *)uni2, '\0', sizeof(*uni2));
1001 return True;
1004 /*******************************************************************
1005 Inits a DOM_RID2 structure.
1006 ********************************************************************/
1008 void init_dom_rid2(DOM_RID2 *rid2, uint32 rid, uint8 type, uint32 idx)
1010 rid2->type = type;
1011 rid2->rid = rid;
1012 rid2->rid_idx = idx;
1015 /*******************************************************************
1016 Reads or writes a DOM_RID2 structure.
1017 ********************************************************************/
1019 BOOL smb_io_dom_rid2(char *desc, DOM_RID2 *rid2, prs_struct *ps, int depth)
1021 if (rid2 == NULL)
1022 return False;
1024 prs_debug(ps, depth, desc, "smb_io_dom_rid2");
1025 depth++;
1027 if(!prs_align(ps))
1028 return False;
1030 if(!prs_uint8("type ", ps, depth, &rid2->type))
1031 return False;
1032 if(!prs_align(ps))
1033 return False;
1034 if(!prs_uint32("rid ", ps, depth, &rid2->rid))
1035 return False;
1036 if(!prs_uint32("rid_idx", ps, depth, &rid2->rid_idx))
1037 return False;
1039 return True;
1042 /*******************************************************************
1043 creates a DOM_RID3 structure.
1044 ********************************************************************/
1046 void init_dom_rid3(DOM_RID3 *rid3, uint32 rid, uint8 type)
1048 rid3->rid = rid;
1049 rid3->type1 = type;
1050 rid3->ptr_type = 0x1; /* non-zero, basically. */
1051 rid3->type2 = 0x1;
1052 rid3->unk = type;
1055 /*******************************************************************
1056 reads or writes a DOM_RID3 structure.
1057 ********************************************************************/
1059 BOOL smb_io_dom_rid3(char *desc, DOM_RID3 *rid3, prs_struct *ps, int depth)
1061 if (rid3 == NULL)
1062 return False;
1064 prs_debug(ps, depth, desc, "smb_io_dom_rid3");
1065 depth++;
1067 if(!prs_align(ps))
1068 return False;
1070 if(!prs_uint32("rid ", ps, depth, &rid3->rid))
1071 return False;
1072 if(!prs_uint32("type1 ", ps, depth, &rid3->type1))
1073 return False;
1074 if(!prs_uint32("ptr_type", ps, depth, &rid3->ptr_type))
1075 return False;
1076 if(!prs_uint32("type2 ", ps, depth, &rid3->type2))
1077 return False;
1078 if(!prs_uint32("unk ", ps, depth, &rid3->unk))
1079 return False;
1081 return True;
1084 /*******************************************************************
1085 Inits a DOM_RID4 structure.
1086 ********************************************************************/
1088 void init_dom_rid4(DOM_RID4 *rid4, uint16 unknown, uint16 attr, uint32 rid)
1090 rid4->unknown = unknown;
1091 rid4->attr = attr;
1092 rid4->rid = rid;
1095 /*******************************************************************
1096 Inits a DOM_CLNT_SRV structure.
1097 ********************************************************************/
1099 static void init_clnt_srv(DOM_CLNT_SRV *log, char *logon_srv, char *comp_name)
1101 DEBUG(5,("init_clnt_srv: %d\n", __LINE__));
1103 if (logon_srv != NULL) {
1104 log->undoc_buffer = 1;
1105 init_unistr2(&log->uni_logon_srv, logon_srv, strlen(logon_srv)+1);
1106 } else {
1107 log->undoc_buffer = 0;
1110 if (comp_name != NULL) {
1111 log->undoc_buffer2 = 1;
1112 init_unistr2(&log->uni_comp_name, comp_name, strlen(comp_name)+1);
1113 } else {
1114 log->undoc_buffer2 = 0;
1118 /*******************************************************************
1119 Inits or writes a DOM_CLNT_SRV structure.
1120 ********************************************************************/
1122 static BOOL smb_io_clnt_srv(char *desc, DOM_CLNT_SRV *log, prs_struct *ps, int depth)
1124 if (log == NULL)
1125 return False;
1127 prs_debug(ps, depth, desc, "smb_io_clnt_srv");
1128 depth++;
1130 if(!prs_align(ps))
1131 return False;
1133 if(!prs_uint32("undoc_buffer ", ps, depth, &log->undoc_buffer))
1134 return False;
1136 if (log->undoc_buffer != 0) {
1137 if(!smb_io_unistr2("unistr2", &log->uni_logon_srv, log->undoc_buffer, ps, depth))
1138 return False;
1141 if(!prs_align(ps))
1142 return False;
1144 if(!prs_uint32("undoc_buffer2", ps, depth, &log->undoc_buffer2))
1145 return False;
1147 if (log->undoc_buffer2 != 0) {
1148 if(!smb_io_unistr2("unistr2", &log->uni_comp_name, log->undoc_buffer2, ps, depth))
1149 return False;
1152 return True;
1155 /*******************************************************************
1156 Inits a DOM_LOG_INFO structure.
1157 ********************************************************************/
1159 void init_log_info(DOM_LOG_INFO *log, char *logon_srv, char *acct_name,
1160 uint16 sec_chan, char *comp_name)
1162 DEBUG(5,("make_log_info %d\n", __LINE__));
1164 log->undoc_buffer = 1;
1166 init_unistr2(&log->uni_logon_srv, logon_srv, strlen(logon_srv)+1);
1167 init_unistr2(&log->uni_acct_name, acct_name, strlen(acct_name)+1);
1169 log->sec_chan = sec_chan;
1171 init_unistr2(&log->uni_comp_name, comp_name, strlen(comp_name)+1);
1174 /*******************************************************************
1175 Reads or writes a DOM_LOG_INFO structure.
1176 ********************************************************************/
1178 BOOL smb_io_log_info(char *desc, DOM_LOG_INFO *log, prs_struct *ps, int depth)
1180 if (log == NULL)
1181 return False;
1183 prs_debug(ps, depth, desc, "smb_io_log_info");
1184 depth++;
1186 if(!prs_align(ps))
1187 return False;
1189 if(!prs_uint32("undoc_buffer", ps, depth, &log->undoc_buffer))
1190 return False;
1192 if(!smb_io_unistr2("unistr2", &log->uni_logon_srv, True, ps, depth))
1193 return False;
1194 if(!smb_io_unistr2("unistr2", &log->uni_acct_name, True, ps, depth))
1195 return False;
1197 if(!prs_uint16("sec_chan", ps, depth, &log->sec_chan))
1198 return False;
1200 if(!smb_io_unistr2("unistr2", &log->uni_comp_name, True, ps, depth))
1201 return False;
1203 return True;
1206 /*******************************************************************
1207 Reads or writes a DOM_CHAL structure.
1208 ********************************************************************/
1210 BOOL smb_io_chal(char *desc, DOM_CHAL *chal, prs_struct *ps, int depth)
1212 if (chal == NULL)
1213 return False;
1215 prs_debug(ps, depth, desc, "smb_io_chal");
1216 depth++;
1218 if(!prs_align(ps))
1219 return False;
1221 if(!prs_uint8s (False, "data", ps, depth, chal->data, 8))
1222 return False;
1224 return True;
1227 /*******************************************************************
1228 Reads or writes a DOM_CRED structure.
1229 ********************************************************************/
1231 BOOL smb_io_cred(char *desc, DOM_CRED *cred, prs_struct *ps, int depth)
1233 if (cred == NULL)
1234 return False;
1236 prs_debug(ps, depth, desc, "smb_io_cred");
1237 depth++;
1239 if(!prs_align(ps))
1240 return False;
1242 if(!smb_io_chal ("", &cred->challenge, ps, depth))
1243 return False;
1245 if(!smb_io_utime("", &cred->timestamp, ps, depth))
1246 return False;
1248 return True;
1251 /*******************************************************************
1252 Inits a DOM_CLNT_INFO2 structure.
1253 ********************************************************************/
1255 void init_clnt_info2(DOM_CLNT_INFO2 *clnt,
1256 char *logon_srv, char *comp_name,
1257 DOM_CRED *clnt_cred)
1259 DEBUG(5,("make_clnt_info: %d\n", __LINE__));
1261 init_clnt_srv(&(clnt->login), logon_srv, comp_name);
1263 if (clnt_cred != NULL) {
1264 clnt->ptr_cred = 1;
1265 memcpy(&(clnt->cred), clnt_cred, sizeof(clnt->cred));
1266 } else {
1267 clnt->ptr_cred = 0;
1271 /*******************************************************************
1272 Reads or writes a DOM_CLNT_INFO2 structure.
1273 ********************************************************************/
1275 BOOL smb_io_clnt_info2(char *desc, DOM_CLNT_INFO2 *clnt, prs_struct *ps, int depth)
1277 if (clnt == NULL)
1278 return False;
1280 prs_debug(ps, depth, desc, "smb_io_clnt_info2");
1281 depth++;
1283 if(!prs_align(ps))
1284 return False;
1286 if(!smb_io_clnt_srv("", &clnt->login, ps, depth))
1287 return False;
1289 if(!prs_align(ps))
1290 return False;
1292 if(!prs_uint32("ptr_cred", ps, depth, &clnt->ptr_cred))
1293 return False;
1294 if(!smb_io_cred("", &clnt->cred, ps, depth))
1295 return False;
1297 return True;
1300 /*******************************************************************
1301 Inits a DOM_CLNT_INFO structure.
1302 ********************************************************************/
1304 void init_clnt_info(DOM_CLNT_INFO *clnt,
1305 char *logon_srv, char *acct_name,
1306 uint16 sec_chan, char *comp_name,
1307 DOM_CRED *cred)
1309 DEBUG(5,("make_clnt_info\n"));
1311 init_log_info(&clnt->login, logon_srv, acct_name, sec_chan, comp_name);
1312 memcpy(&clnt->cred, cred, sizeof(clnt->cred));
1315 /*******************************************************************
1316 Reads or writes a DOM_CLNT_INFO structure.
1317 ********************************************************************/
1319 BOOL smb_io_clnt_info(char *desc, DOM_CLNT_INFO *clnt, prs_struct *ps, int depth)
1321 if (clnt == NULL)
1322 return False;
1324 prs_debug(ps, depth, desc, "smb_io_clnt_info");
1325 depth++;
1327 if(!prs_align(ps))
1328 return False;
1330 if(!smb_io_log_info("", &clnt->login, ps, depth))
1331 return False;
1332 if(!smb_io_cred("", &clnt->cred, ps, depth))
1333 return False;
1335 return True;
1338 /*******************************************************************
1339 Inits a DOM_LOGON_ID structure.
1340 ********************************************************************/
1342 void init_logon_id(DOM_LOGON_ID *log, uint32 log_id_low, uint32 log_id_high)
1344 DEBUG(5,("make_logon_id: %d\n", __LINE__));
1346 log->low = log_id_low;
1347 log->high = log_id_high;
1350 /*******************************************************************
1351 Reads or writes a DOM_LOGON_ID structure.
1352 ********************************************************************/
1354 BOOL smb_io_logon_id(char *desc, DOM_LOGON_ID *log, prs_struct *ps, int depth)
1356 if (log == NULL)
1357 return False;
1359 prs_debug(ps, depth, desc, "smb_io_logon_id");
1360 depth++;
1362 if(!prs_align(ps))
1363 return False;
1365 if(!prs_uint32("low ", ps, depth, &log->low ))
1366 return False;
1367 if(!prs_uint32("high", ps, depth, &log->high))
1368 return False;
1370 return True;
1373 /*******************************************************************
1374 Inits an OWF_INFO structure.
1375 ********************************************************************/
1377 void init_owf_info(OWF_INFO *hash, uint8 data[16])
1379 DEBUG(5,("init_owf_info: %d\n", __LINE__));
1381 if (data != NULL)
1382 memcpy(hash->data, data, sizeof(hash->data));
1383 else
1384 memset((char *)hash->data, '\0', sizeof(hash->data));
1387 /*******************************************************************
1388 Reads or writes an OWF_INFO structure.
1389 ********************************************************************/
1391 BOOL smb_io_owf_info(char *desc, OWF_INFO *hash, prs_struct *ps, int depth)
1393 if (hash == NULL)
1394 return False;
1396 prs_debug(ps, depth, desc, "smb_io_owf_info");
1397 depth++;
1399 if(!prs_align(ps))
1400 return False;
1402 if(!prs_uint8s (False, "data", ps, depth, hash->data, 16))
1403 return False;
1405 return True;
1408 /*******************************************************************
1409 Reads or writes a DOM_GID structure.
1410 ********************************************************************/
1412 BOOL smb_io_gid(char *desc, DOM_GID *gid, prs_struct *ps, int depth)
1414 if (gid == NULL)
1415 return False;
1417 prs_debug(ps, depth, desc, "smb_io_gid");
1418 depth++;
1420 if(!prs_align(ps))
1421 return False;
1423 if(!prs_uint32("g_rid", ps, depth, &gid->g_rid))
1424 return False;
1425 if(!prs_uint32("attr ", ps, depth, &gid->attr))
1426 return False;
1428 return True;
1431 /*******************************************************************
1432 Reads or writes an POLICY_HND structure.
1433 ********************************************************************/
1435 BOOL smb_io_pol_hnd(char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
1437 if (pol == NULL)
1438 return False;
1440 prs_debug(ps, depth, desc, "smb_io_pol_hnd");
1441 depth++;
1443 if(!prs_align(ps))
1444 return False;
1446 if(UNMARSHALLING(ps))
1447 ZERO_STRUCTP(pol);
1449 if (!prs_uint32("data1", ps, depth, &pol->data1))
1450 return False;
1451 if (!prs_uint32("data2", ps, depth, &pol->data2))
1452 return False;
1453 if (!prs_uint16("data3", ps, depth, &pol->data3))
1454 return False;
1455 if (!prs_uint16("data4", ps, depth, &pol->data4))
1456 return False;
1457 if(!prs_uint8s (False, "data5", ps, depth, pol->data5, sizeof(pol->data5)))
1458 return False;
1460 return True;
1463 /*******************************************************************
1464 Create a UNISTR3.
1465 ********************************************************************/
1467 void init_unistr3(UNISTR3 *str, const char *buf)
1469 size_t len;
1471 if (buf == NULL) {
1472 str->uni_str_len=0;
1473 str->str.buffer = NULL;
1474 return;
1477 len = strlen(buf) + 1;
1479 str->uni_str_len=len;
1481 if (len < MAX_UNISTRLEN)
1482 len = MAX_UNISTRLEN;
1484 len *= sizeof(uint16);
1486 str->str.buffer = (uint16 *)talloc_zero(get_talloc_ctx(), len);
1487 if (str->str.buffer == NULL)
1488 smb_panic("init_unistr3: malloc fail\n");
1490 /* store the string (null-terminated copy) */
1491 dos_struni2((char *)str->str.buffer, buf, len);
1494 /*******************************************************************
1495 Reads or writes a UNISTR3 structure.
1496 ********************************************************************/
1498 BOOL smb_io_unistr3(char *desc, UNISTR3 *name, prs_struct *ps, int depth)
1500 if (name == NULL)
1501 return False;
1503 prs_debug(ps, depth, desc, "smb_io_unistr3");
1504 depth++;
1506 if(!prs_align(ps))
1507 return False;
1509 if(!prs_uint32("uni_str_len", ps, depth, &name->uni_str_len))
1510 return False;
1512 /* don't know if len is specified by uni_str_len member... */
1513 /* assume unicode string is unicode-null-terminated, instead */
1515 if(!prs_unistr3(True, "unistr", name, ps, depth))
1516 return False;
1518 return True;
1522 /*******************************************************************
1523 Stream a uint64_struct
1524 ********************************************************************/
1525 BOOL prs_uint64(char *name, prs_struct *ps, int depth, UINT64_S *data64)
1527 return prs_uint32(name, ps, depth+1, &data64->low) &&
1528 prs_uint32(name, ps, depth+1, &data64->high);
1531 /*******************************************************************
1532 reads or writes a BUFHDR2 structure.
1533 ********************************************************************/
1534 BOOL smb_io_bufhdr2(char *desc, BUFHDR2 *hdr, prs_struct *ps, int depth)
1536 prs_debug(ps, depth, desc, "smb_io_bufhdr2");
1537 depth++;
1539 prs_align(ps);
1540 prs_uint32("info_level", ps, depth, &(hdr->info_level));
1541 prs_uint32("length ", ps, depth, &(hdr->length ));
1542 prs_uint32("buffer ", ps, depth, &(hdr->buffer ));
1544 return True;
1547 /*******************************************************************
1548 reads or writes a BUFFER4 structure.
1549 ********************************************************************/
1550 BOOL smb_io_buffer4(char *desc, BUFFER4 *buf4, uint32 buffer, prs_struct *ps, int depth)
1552 prs_debug(ps, depth, desc, "smb_io_buffer4");
1553 depth++;
1555 prs_align(ps);
1556 prs_uint32("buf_len", ps, depth, &(buf4->buf_len));
1558 if (buf4->buf_len > MAX_BUFFERLEN)
1560 buf4->buf_len = MAX_BUFFERLEN;
1563 prs_uint8s(True, "buffer", ps, depth, buf4->buffer, buf4->buf_len);
1565 return True;
1568 /*******************************************************************
1569 creates a UNIHDR structure.
1570 ********************************************************************/
1572 BOOL make_uni_hdr(UNIHDR *hdr, int len)
1574 if (hdr == NULL)
1576 return False;
1578 hdr->uni_str_len = 2 * len;
1579 hdr->uni_max_len = 2 * len;
1580 hdr->buffer = len != 0 ? 1 : 0;
1582 return True;
1585 /*******************************************************************
1586 creates a BUFHDR2 structure.
1587 ********************************************************************/
1588 BOOL make_bufhdr2(BUFHDR2 *hdr, uint32 info_level, uint32 length, uint32 buffer)
1590 hdr->info_level = info_level;
1591 hdr->length = length;
1592 hdr->buffer = buffer;
1594 return True;