merge from 2.2 and regenerate
[Samba.git] / source / rpc_parse / parse_misc.c
blobd2e2e3cf2645da1f96d9f62b35caf63cea54218e
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.
24 #include "includes.h"
26 /****************************************************************************
27 A temporary TALLOC context for things like unistrs, that is valid for
28 the life of a complete RPC call.
29 ****************************************************************************/
31 static TALLOC_CTX *current_rpc_talloc = NULL;
33 TALLOC_CTX *get_current_rpc_talloc(void)
35 return current_rpc_talloc;
38 void set_current_rpc_talloc( TALLOC_CTX *ctx)
40 current_rpc_talloc = ctx;
43 static TALLOC_CTX *main_loop_talloc = NULL;
45 /*******************************************************************
46 free up temporary memory - called from the main loop
47 ********************************************************************/
49 void main_loop_talloc_free(void)
51 if (!main_loop_talloc)
52 return;
53 talloc_destroy(main_loop_talloc);
54 main_loop_talloc = NULL;
57 /*******************************************************************
58 Get a talloc context that is freed in the main loop...
59 ********************************************************************/
61 TALLOC_CTX *main_loop_talloc_get(void)
63 if (!main_loop_talloc) {
64 main_loop_talloc = talloc_init_named("main loop talloc (mainly parse_misc)");
65 if (!main_loop_talloc)
66 smb_panic("main_loop_talloc: malloc fail\n");
69 return main_loop_talloc;
72 /*******************************************************************
73 Try and get a talloc context. Get the rpc one if possible, else
74 get the main loop one. The main loop one is more dangerous as it
75 goes away between packets, the rpc one will stay around for as long
76 as a current RPC lasts.
77 ********************************************************************/
79 TALLOC_CTX *get_talloc_ctx(void)
81 TALLOC_CTX *tc = get_current_rpc_talloc();
83 if (tc)
84 return tc;
85 return main_loop_talloc_get();
88 /*******************************************************************
89 Reads or writes a UTIME type.
90 ********************************************************************/
92 static BOOL smb_io_utime(char *desc, UTIME *t, prs_struct *ps, int depth)
94 if (t == NULL)
95 return False;
97 prs_debug(ps, depth, desc, "smb_io_utime");
98 depth++;
100 if(!prs_align(ps))
101 return False;
103 if(!prs_uint32 ("time", ps, depth, &t->time))
104 return False;
106 return True;
109 /*******************************************************************
110 Reads or writes an NTTIME structure.
111 ********************************************************************/
113 BOOL smb_io_time(char *desc, NTTIME *nttime, prs_struct *ps, int depth)
115 if (nttime == NULL)
116 return False;
118 prs_debug(ps, depth, desc, "smb_io_time");
119 depth++;
121 if(!prs_align(ps))
122 return False;
124 if(!prs_uint32("low ", ps, depth, &nttime->low)) /* low part */
125 return False;
126 if(!prs_uint32("high", ps, depth, &nttime->high)) /* high part */
127 return False;
129 return True;
132 /*******************************************************************
133 Reads or writes a LOOKUP_LEVEL structure.
134 ********************************************************************/
136 BOOL smb_io_lookup_level(char *desc, LOOKUP_LEVEL *level, prs_struct *ps, int depth)
138 if (level == NULL)
139 return False;
141 prs_debug(ps, depth, desc, "smb_io_lookup_level");
142 depth++;
144 if(!prs_align(ps))
145 return False;
146 if(!prs_uint16("value", ps, depth, &level->value))
147 return False;
148 if(!prs_align(ps))
149 return False;
151 return True;
154 /*******************************************************************
155 Gets an enumeration handle from an ENUM_HND structure.
156 ********************************************************************/
158 uint32 get_enum_hnd(ENUM_HND *enh)
160 return (enh && enh->ptr_hnd != 0) ? enh->handle : 0;
163 /*******************************************************************
164 Inits an ENUM_HND structure.
165 ********************************************************************/
167 void init_enum_hnd(ENUM_HND *enh, uint32 hnd)
169 DEBUG(5,("smb_io_enum_hnd\n"));
171 enh->ptr_hnd = (hnd != 0) ? 1 : 0;
172 enh->handle = hnd;
175 /*******************************************************************
176 Reads or writes an ENUM_HND structure.
177 ********************************************************************/
179 BOOL smb_io_enum_hnd(char *desc, ENUM_HND *hnd, prs_struct *ps, int depth)
181 if (hnd == NULL)
182 return False;
184 prs_debug(ps, depth, desc, "smb_io_enum_hnd");
185 depth++;
187 if(!prs_align(ps))
188 return False;
190 if(!prs_uint32("ptr_hnd", ps, depth, &hnd->ptr_hnd)) /* pointer */
191 return False;
193 if (hnd->ptr_hnd != 0) {
194 if(!prs_uint32("handle ", ps, depth, &hnd->handle )) /* enum handle */
195 return False;
198 return True;
201 /*******************************************************************
202 Reads or writes a DOM_SID structure.
203 ********************************************************************/
205 BOOL smb_io_dom_sid(char *desc, DOM_SID *sid, prs_struct *ps, int depth)
207 int i;
209 if (sid == NULL)
210 return False;
212 prs_debug(ps, depth, desc, "smb_io_dom_sid");
213 depth++;
215 if(!prs_align(ps))
216 return False;
218 if(!prs_uint8 ("sid_rev_num", ps, depth, &sid->sid_rev_num))
219 return False;
220 if(!prs_uint8 ("num_auths ", ps, depth, &sid->num_auths))
221 return False;
223 for (i = 0; i < 6; i++)
225 fstring tmp;
226 slprintf(tmp, sizeof(tmp) - 1, "id_auth[%d] ", i);
227 if(!prs_uint8 (tmp, ps, depth, &sid->id_auth[i]))
228 return False;
231 /* oops! XXXX should really issue a warning here... */
232 if (sid->num_auths > MAXSUBAUTHS)
233 sid->num_auths = MAXSUBAUTHS;
235 if(!prs_uint32s(False, "sub_auths ", ps, depth, sid->sub_auths, sid->num_auths))
236 return False;
238 return True;
241 /*******************************************************************
242 Inits a DOM_SID structure.
244 BIG NOTE: this function only does SIDS where the identauth is not >= 2^32
245 identauth >= 2^32 can be detected because it will be specified in hex
246 ********************************************************************/
248 void init_dom_sid(DOM_SID *sid, char *str_sid)
250 pstring domsid;
251 int identauth;
252 char *p;
254 if (str_sid == NULL)
256 DEBUG(4,("netlogon domain SID: none\n"));
257 sid->sid_rev_num = 0;
258 sid->num_auths = 0;
259 return;
262 pstrcpy(domsid, str_sid);
264 DEBUG(4,("init_dom_sid %d SID: %s\n", __LINE__, domsid));
266 /* assume, but should check, that domsid starts "S-" */
267 p = strtok(domsid+2,"-");
268 sid->sid_rev_num = atoi(p);
270 /* identauth in decimal should be < 2^32 */
271 /* identauth in hex should be >= 2^32 */
272 identauth = atoi(strtok(0,"-"));
274 DEBUG(4,("netlogon rev %d\n", sid->sid_rev_num));
275 DEBUG(4,("netlogon %s ia %d\n", p, identauth));
277 sid->id_auth[0] = 0;
278 sid->id_auth[1] = 0;
279 sid->id_auth[2] = (identauth & 0xff000000) >> 24;
280 sid->id_auth[3] = (identauth & 0x00ff0000) >> 16;
281 sid->id_auth[4] = (identauth & 0x0000ff00) >> 8;
282 sid->id_auth[5] = (identauth & 0x000000ff);
284 sid->num_auths = 0;
286 while ((p = strtok(0, "-")) != NULL && sid->num_auths < MAXSUBAUTHS)
287 sid->sub_auths[sid->num_auths++] = atoi(p);
289 DEBUG(4,("init_dom_sid: %d SID: %s\n", __LINE__, domsid));
292 /*******************************************************************
293 Inits a DOM_SID2 structure.
294 ********************************************************************/
296 void init_dom_sid2(DOM_SID2 *sid2, const DOM_SID *sid)
298 sid2->sid = *sid;
299 sid2->num_auths = sid2->sid.num_auths;
302 /*******************************************************************
303 Reads or writes a DOM_SID2 structure.
304 ********************************************************************/
306 BOOL smb_io_dom_sid2(char *desc, DOM_SID2 *sid, prs_struct *ps, int depth)
308 if (sid == NULL)
309 return False;
311 prs_debug(ps, depth, desc, "smb_io_dom_sid2");
312 depth++;
314 if(!prs_align(ps))
315 return False;
317 if(!prs_uint32("num_auths", ps, depth, &sid->num_auths))
318 return False;
320 if(!smb_io_dom_sid("sid", &sid->sid, ps, depth))
321 return False;
323 return True;
326 /*******************************************************************
327 creates a STRHDR structure.
328 ********************************************************************/
330 void init_str_hdr(STRHDR *hdr, int max_len, int len, uint32 buffer)
332 hdr->str_max_len = max_len;
333 hdr->str_str_len = len;
334 hdr->buffer = buffer;
337 /*******************************************************************
338 Reads or writes a STRHDR structure.
339 ********************************************************************/
341 BOOL smb_io_strhdr(char *desc, STRHDR *hdr, prs_struct *ps, int depth)
343 if (hdr == NULL)
344 return False;
346 prs_debug(ps, depth, desc, "smb_io_strhdr");
347 depth++;
349 prs_align(ps);
351 if(!prs_uint16("str_str_len", ps, depth, &hdr->str_str_len))
352 return False;
353 if(!prs_uint16("str_max_len", ps, depth, &hdr->str_max_len))
354 return False;
355 if(!prs_uint32("buffer ", ps, depth, &hdr->buffer))
356 return False;
358 return True;
361 /*******************************************************************
362 Inits a UNIHDR structure.
363 ********************************************************************/
365 void init_uni_hdr(UNIHDR *hdr, int len)
367 hdr->uni_str_len = 2 * len;
368 hdr->uni_max_len = 2 * len;
369 hdr->buffer = len != 0 ? 1 : 0;
372 /*******************************************************************
373 Reads or writes a UNIHDR structure.
374 ********************************************************************/
376 BOOL smb_io_unihdr(char *desc, UNIHDR *hdr, prs_struct *ps, int depth)
378 if (hdr == NULL)
379 return False;
381 prs_debug(ps, depth, desc, "smb_io_unihdr");
382 depth++;
384 if(!prs_align(ps))
385 return False;
387 if(!prs_uint16("uni_str_len", ps, depth, &hdr->uni_str_len))
388 return False;
389 if(!prs_uint16("uni_max_len", ps, depth, &hdr->uni_max_len))
390 return False;
391 if(!prs_uint32("buffer ", ps, depth, &hdr->buffer))
392 return False;
394 return True;
397 /*******************************************************************
398 Inits a BUFHDR structure.
399 ********************************************************************/
401 void init_buf_hdr(BUFHDR *hdr, int max_len, int len)
403 hdr->buf_max_len = max_len;
404 hdr->buf_len = len;
407 /*******************************************************************
408 prs_uint16 wrapper. Call this and it sets up a pointer to where the
409 uint16 should be stored, or gets the size if reading.
410 ********************************************************************/
412 BOOL smb_io_hdrbuf_pre(char *desc, BUFHDR *hdr, prs_struct *ps, int depth, uint32 *offset)
414 (*offset) = prs_offset(ps);
415 if (ps->io) {
417 /* reading. */
419 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
420 return False;
422 } else {
424 /* writing. */
426 if(!prs_set_offset(ps, prs_offset(ps) + (sizeof(uint32) * 2)))
427 return False;
430 return True;
433 /*******************************************************************
434 smb_io_hdrbuf wrapper. Call this and it retrospectively stores the size.
435 Does nothing on reading, as that is already handled by ...._pre()
436 ********************************************************************/
438 BOOL smb_io_hdrbuf_post(char *desc, BUFHDR *hdr, prs_struct *ps, int depth,
439 uint32 ptr_hdrbuf, uint32 max_len, uint32 len)
441 if (!ps->io) {
442 /* writing: go back and do a retrospective job. i hate this */
444 uint32 old_offset = prs_offset(ps);
446 init_buf_hdr(hdr, max_len, len);
447 if(!prs_set_offset(ps, ptr_hdrbuf))
448 return False;
449 if(!smb_io_hdrbuf(desc, hdr, ps, depth))
450 return False;
452 if(!prs_set_offset(ps, old_offset))
453 return False;
456 return True;
459 /*******************************************************************
460 Reads or writes a BUFHDR structure.
461 ********************************************************************/
463 BOOL smb_io_hdrbuf(char *desc, BUFHDR *hdr, prs_struct *ps, int depth)
465 if (hdr == NULL)
466 return False;
468 prs_debug(ps, depth, desc, "smb_io_hdrbuf");
469 depth++;
471 if(!prs_align(ps))
472 return False;
474 if(!prs_uint32("buf_max_len", ps, depth, &hdr->buf_max_len))
475 return False;
476 if(!prs_uint32("buf_len ", ps, depth, &hdr->buf_len))
477 return False;
479 return True;
482 /*******************************************************************
483 creates a UNIHDR2 structure.
484 ********************************************************************/
486 void init_uni_hdr2(UNIHDR2 *hdr, int len)
488 init_uni_hdr(&hdr->unihdr, len);
489 hdr->buffer = (len > 0) ? 1 : 0;
492 /*******************************************************************
493 Reads or writes a UNIHDR2 structure.
494 ********************************************************************/
496 BOOL smb_io_unihdr2(char *desc, UNIHDR2 *hdr2, prs_struct *ps, int depth)
498 if (hdr2 == NULL)
499 return False;
501 prs_debug(ps, depth, desc, "smb_io_unihdr2");
502 depth++;
504 if(!prs_align(ps))
505 return False;
507 if(!smb_io_unihdr("hdr", &hdr2->unihdr, ps, depth))
508 return False;
509 if(!prs_uint32("buffer", ps, depth, &hdr2->buffer))
510 return False;
512 return True;
515 /*******************************************************************
516 Inits a UNISTR structure.
517 ********************************************************************/
519 void init_unistr(UNISTR *str, const char *buf)
521 size_t len;
523 if (buf == NULL) {
524 str->buffer = NULL;
525 return;
529 len = strlen(buf) + 1;
531 if (len < MAX_UNISTRLEN)
532 len = MAX_UNISTRLEN;
533 len *= sizeof(uint16);
535 str->buffer = (uint16 *)talloc_zero(get_talloc_ctx(), len);
536 if (str->buffer == NULL)
537 smb_panic("init_unistr: malloc fail\n");
539 rpcstr_push(str->buffer, buf, len, STR_TERMINATE);
542 /*******************************************************************
543 reads or writes a UNISTR structure.
544 XXXX NOTE: UNISTR structures NEED to be null-terminated.
545 ********************************************************************/
547 BOOL smb_io_unistr(char *desc, UNISTR *uni, prs_struct *ps, int depth)
549 if (uni == NULL)
550 return False;
552 prs_debug(ps, depth, desc, "smb_io_unistr");
553 depth++;
555 if(!prs_align(ps))
556 return False;
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 rpcstr_push(str->buffer, buf, str->buf_max_len, STR_TERMINATE);
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 rpcstr_push((char *)str->buffer, buf, len, STR_TERMINATE);
920 /*******************************************************************
921 Inits a UNISTR2 structure from a UNISTR
922 ********************************************************************/
923 void init_unistr2_from_unistr (UNISTR2 *to, UNISTR *from)
926 uint32 i;
928 /* the destination UNISTR2 should never be NULL.
929 if it is it is a programming error */
931 /* if the source UNISTR is NULL, then zero out
932 the destination string and return */
933 ZERO_STRUCTP (to);
934 if ((from == NULL) || (from->buffer == NULL))
935 return;
937 /* get the length; UNISTR must be NULL terminated */
938 i = 0;
939 while ((from->buffer)[i]!='\0')
940 i++;
941 i++; /* one more to catch the terminating NULL */
942 /* is this necessary -- jerry? I need to think */
944 /* set up string lengths; uni_max_len is set to i+1
945 because we need to account for the final NULL termination */
946 to->uni_max_len = i;
947 to->undoc = 0;
948 to->uni_str_len = i;
950 /* allocate the space and copy the string buffer */
951 to->buffer = (uint16 *)talloc_zero(get_talloc_ctx(), sizeof(uint16)*(to->uni_str_len));
952 if (to->buffer == NULL)
953 smb_panic("init_unistr2_from_unistr: malloc fail\n");
954 memcpy(to->buffer, from->buffer, to->uni_max_len*sizeof(uint16));
956 return;
960 /*******************************************************************
961 Reads or writes a UNISTR2 structure.
962 XXXX NOTE: UNISTR2 structures need NOT be null-terminated.
963 the uni_str_len member tells you how long the string is;
964 the uni_max_len member tells you how large the buffer is.
965 ********************************************************************/
967 BOOL smb_io_unistr2(char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
969 if (uni2 == NULL)
970 return False;
972 if (buffer) {
974 prs_debug(ps, depth, desc, "smb_io_unistr2");
975 depth++;
977 if(!prs_align(ps))
978 return False;
980 if(!prs_uint32("uni_max_len", ps, depth, &uni2->uni_max_len))
981 return False;
982 if(!prs_uint32("undoc ", ps, depth, &uni2->undoc))
983 return False;
984 if(!prs_uint32("uni_str_len", ps, depth, &uni2->uni_str_len))
985 return False;
987 /* buffer advanced by indicated length of string
988 NOT by searching for null-termination */
989 if(!prs_unistr2(True, "buffer ", ps, depth, uni2))
990 return False;
992 } else {
994 prs_debug(ps, depth, desc, "smb_io_unistr2 - NULL");
995 depth++;
996 memset((char *)uni2, '\0', sizeof(*uni2));
1000 return True;
1003 /*******************************************************************
1004 Inits a DOM_RID2 structure.
1005 ********************************************************************/
1007 void init_dom_rid2(DOM_RID2 *rid2, uint32 rid, uint8 type, uint32 idx)
1009 rid2->type = type;
1010 rid2->rid = rid;
1011 rid2->rid_idx = idx;
1014 /*******************************************************************
1015 Reads or writes a DOM_RID2 structure.
1016 ********************************************************************/
1018 BOOL smb_io_dom_rid2(char *desc, DOM_RID2 *rid2, prs_struct *ps, int depth)
1020 if (rid2 == NULL)
1021 return False;
1023 prs_debug(ps, depth, desc, "smb_io_dom_rid2");
1024 depth++;
1026 if(!prs_align(ps))
1027 return False;
1029 if(!prs_uint8("type ", ps, depth, &rid2->type))
1030 return False;
1031 if(!prs_align(ps))
1032 return False;
1033 if(!prs_uint32("rid ", ps, depth, &rid2->rid))
1034 return False;
1035 if(!prs_uint32("rid_idx", ps, depth, &rid2->rid_idx))
1036 return False;
1038 return True;
1041 /*******************************************************************
1042 creates a DOM_RID3 structure.
1043 ********************************************************************/
1045 void init_dom_rid3(DOM_RID3 *rid3, uint32 rid, uint8 type)
1047 rid3->rid = rid;
1048 rid3->type1 = type;
1049 rid3->ptr_type = 0x1; /* non-zero, basically. */
1050 rid3->type2 = 0x1;
1051 rid3->unk = type;
1054 /*******************************************************************
1055 reads or writes a DOM_RID3 structure.
1056 ********************************************************************/
1058 BOOL smb_io_dom_rid3(char *desc, DOM_RID3 *rid3, prs_struct *ps, int depth)
1060 if (rid3 == NULL)
1061 return False;
1063 prs_debug(ps, depth, desc, "smb_io_dom_rid3");
1064 depth++;
1066 if(!prs_align(ps))
1067 return False;
1069 if(!prs_uint32("rid ", ps, depth, &rid3->rid))
1070 return False;
1071 if(!prs_uint32("type1 ", ps, depth, &rid3->type1))
1072 return False;
1073 if(!prs_uint32("ptr_type", ps, depth, &rid3->ptr_type))
1074 return False;
1075 if(!prs_uint32("type2 ", ps, depth, &rid3->type2))
1076 return False;
1077 if(!prs_uint32("unk ", ps, depth, &rid3->unk))
1078 return False;
1080 return True;
1083 /*******************************************************************
1084 Inits a DOM_RID4 structure.
1085 ********************************************************************/
1087 void init_dom_rid4(DOM_RID4 *rid4, uint16 unknown, uint16 attr, uint32 rid)
1089 rid4->unknown = unknown;
1090 rid4->attr = attr;
1091 rid4->rid = rid;
1094 /*******************************************************************
1095 Inits a DOM_CLNT_SRV structure.
1096 ********************************************************************/
1098 static void init_clnt_srv(DOM_CLNT_SRV *log, char *logon_srv, char *comp_name)
1100 DEBUG(5,("init_clnt_srv: %d\n", __LINE__));
1102 if (logon_srv != NULL) {
1103 log->undoc_buffer = 1;
1104 init_unistr2(&log->uni_logon_srv, logon_srv, strlen(logon_srv)+1);
1105 } else {
1106 log->undoc_buffer = 0;
1109 if (comp_name != NULL) {
1110 log->undoc_buffer2 = 1;
1111 init_unistr2(&log->uni_comp_name, comp_name, strlen(comp_name)+1);
1112 } else {
1113 log->undoc_buffer2 = 0;
1117 /*******************************************************************
1118 Inits or writes a DOM_CLNT_SRV structure.
1119 ********************************************************************/
1121 static BOOL smb_io_clnt_srv(char *desc, DOM_CLNT_SRV *log, prs_struct *ps, int depth)
1123 if (log == NULL)
1124 return False;
1126 prs_debug(ps, depth, desc, "smb_io_clnt_srv");
1127 depth++;
1129 if(!prs_align(ps))
1130 return False;
1132 if(!prs_uint32("undoc_buffer ", ps, depth, &log->undoc_buffer))
1133 return False;
1135 if (log->undoc_buffer != 0) {
1136 if(!smb_io_unistr2("unistr2", &log->uni_logon_srv, log->undoc_buffer, ps, depth))
1137 return False;
1140 if(!prs_align(ps))
1141 return False;
1143 if(!prs_uint32("undoc_buffer2", ps, depth, &log->undoc_buffer2))
1144 return False;
1146 if (log->undoc_buffer2 != 0) {
1147 if(!smb_io_unistr2("unistr2", &log->uni_comp_name, log->undoc_buffer2, ps, depth))
1148 return False;
1151 return True;
1154 /*******************************************************************
1155 Inits a DOM_LOG_INFO structure.
1156 ********************************************************************/
1158 void init_log_info(DOM_LOG_INFO *log, char *logon_srv, char *acct_name,
1159 uint16 sec_chan, char *comp_name)
1161 DEBUG(5,("make_log_info %d\n", __LINE__));
1163 log->undoc_buffer = 1;
1165 init_unistr2(&log->uni_logon_srv, logon_srv, strlen(logon_srv)+1);
1166 init_unistr2(&log->uni_acct_name, acct_name, strlen(acct_name)+1);
1168 log->sec_chan = sec_chan;
1170 init_unistr2(&log->uni_comp_name, comp_name, strlen(comp_name)+1);
1173 /*******************************************************************
1174 Reads or writes a DOM_LOG_INFO structure.
1175 ********************************************************************/
1177 BOOL smb_io_log_info(char *desc, DOM_LOG_INFO *log, prs_struct *ps, int depth)
1179 if (log == NULL)
1180 return False;
1182 prs_debug(ps, depth, desc, "smb_io_log_info");
1183 depth++;
1185 if(!prs_align(ps))
1186 return False;
1188 if(!prs_uint32("undoc_buffer", ps, depth, &log->undoc_buffer))
1189 return False;
1191 if(!smb_io_unistr2("unistr2", &log->uni_logon_srv, True, ps, depth))
1192 return False;
1193 if(!smb_io_unistr2("unistr2", &log->uni_acct_name, True, ps, depth))
1194 return False;
1196 if(!prs_uint16("sec_chan", ps, depth, &log->sec_chan))
1197 return False;
1199 if(!smb_io_unistr2("unistr2", &log->uni_comp_name, True, ps, depth))
1200 return False;
1202 return True;
1205 /*******************************************************************
1206 Reads or writes a DOM_CHAL structure.
1207 ********************************************************************/
1209 BOOL smb_io_chal(char *desc, DOM_CHAL *chal, prs_struct *ps, int depth)
1211 if (chal == NULL)
1212 return False;
1214 prs_debug(ps, depth, desc, "smb_io_chal");
1215 depth++;
1217 if(!prs_align(ps))
1218 return False;
1220 if(!prs_uint8s (False, "data", ps, depth, chal->data, 8))
1221 return False;
1223 return True;
1226 /*******************************************************************
1227 Reads or writes a DOM_CRED structure.
1228 ********************************************************************/
1230 BOOL smb_io_cred(char *desc, DOM_CRED *cred, prs_struct *ps, int depth)
1232 if (cred == NULL)
1233 return False;
1235 prs_debug(ps, depth, desc, "smb_io_cred");
1236 depth++;
1238 if(!prs_align(ps))
1239 return False;
1241 if(!smb_io_chal ("", &cred->challenge, ps, depth))
1242 return False;
1244 if(!smb_io_utime("", &cred->timestamp, ps, depth))
1245 return False;
1247 return True;
1250 /*******************************************************************
1251 Inits a DOM_CLNT_INFO2 structure.
1252 ********************************************************************/
1254 void init_clnt_info2(DOM_CLNT_INFO2 *clnt,
1255 char *logon_srv, char *comp_name,
1256 DOM_CRED *clnt_cred)
1258 DEBUG(5,("make_clnt_info: %d\n", __LINE__));
1260 init_clnt_srv(&(clnt->login), logon_srv, comp_name);
1262 if (clnt_cred != NULL) {
1263 clnt->ptr_cred = 1;
1264 memcpy(&(clnt->cred), clnt_cred, sizeof(clnt->cred));
1265 } else {
1266 clnt->ptr_cred = 0;
1270 /*******************************************************************
1271 Reads or writes a DOM_CLNT_INFO2 structure.
1272 ********************************************************************/
1274 BOOL smb_io_clnt_info2(char *desc, DOM_CLNT_INFO2 *clnt, prs_struct *ps, int depth)
1276 if (clnt == NULL)
1277 return False;
1279 prs_debug(ps, depth, desc, "smb_io_clnt_info2");
1280 depth++;
1282 if(!prs_align(ps))
1283 return False;
1285 if(!smb_io_clnt_srv("", &clnt->login, ps, depth))
1286 return False;
1288 if(!prs_align(ps))
1289 return False;
1291 if(!prs_uint32("ptr_cred", ps, depth, &clnt->ptr_cred))
1292 return False;
1293 if(!smb_io_cred("", &clnt->cred, ps, depth))
1294 return False;
1296 return True;
1299 /*******************************************************************
1300 Inits a DOM_CLNT_INFO structure.
1301 ********************************************************************/
1303 void init_clnt_info(DOM_CLNT_INFO *clnt,
1304 char *logon_srv, char *acct_name,
1305 uint16 sec_chan, char *comp_name,
1306 DOM_CRED *cred)
1308 DEBUG(5,("make_clnt_info\n"));
1310 init_log_info(&clnt->login, logon_srv, acct_name, sec_chan, comp_name);
1311 memcpy(&clnt->cred, cred, sizeof(clnt->cred));
1314 /*******************************************************************
1315 Reads or writes a DOM_CLNT_INFO structure.
1316 ********************************************************************/
1318 BOOL smb_io_clnt_info(char *desc, DOM_CLNT_INFO *clnt, prs_struct *ps, int depth)
1320 if (clnt == NULL)
1321 return False;
1323 prs_debug(ps, depth, desc, "smb_io_clnt_info");
1324 depth++;
1326 if(!prs_align(ps))
1327 return False;
1329 if(!smb_io_log_info("", &clnt->login, ps, depth))
1330 return False;
1331 if(!smb_io_cred("", &clnt->cred, ps, depth))
1332 return False;
1334 return True;
1337 /*******************************************************************
1338 Inits a DOM_LOGON_ID structure.
1339 ********************************************************************/
1341 void init_logon_id(DOM_LOGON_ID *log, uint32 log_id_low, uint32 log_id_high)
1343 DEBUG(5,("make_logon_id: %d\n", __LINE__));
1345 log->low = log_id_low;
1346 log->high = log_id_high;
1349 /*******************************************************************
1350 Reads or writes a DOM_LOGON_ID structure.
1351 ********************************************************************/
1353 BOOL smb_io_logon_id(char *desc, DOM_LOGON_ID *log, prs_struct *ps, int depth)
1355 if (log == NULL)
1356 return False;
1358 prs_debug(ps, depth, desc, "smb_io_logon_id");
1359 depth++;
1361 if(!prs_align(ps))
1362 return False;
1364 if(!prs_uint32("low ", ps, depth, &log->low ))
1365 return False;
1366 if(!prs_uint32("high", ps, depth, &log->high))
1367 return False;
1369 return True;
1372 /*******************************************************************
1373 Inits an OWF_INFO structure.
1374 ********************************************************************/
1376 void init_owf_info(OWF_INFO *hash, uint8 data[16])
1378 DEBUG(5,("init_owf_info: %d\n", __LINE__));
1380 if (data != NULL)
1381 memcpy(hash->data, data, sizeof(hash->data));
1382 else
1383 memset((char *)hash->data, '\0', sizeof(hash->data));
1386 /*******************************************************************
1387 Reads or writes an OWF_INFO structure.
1388 ********************************************************************/
1390 BOOL smb_io_owf_info(char *desc, OWF_INFO *hash, prs_struct *ps, int depth)
1392 if (hash == NULL)
1393 return False;
1395 prs_debug(ps, depth, desc, "smb_io_owf_info");
1396 depth++;
1398 if(!prs_align(ps))
1399 return False;
1401 if(!prs_uint8s (False, "data", ps, depth, hash->data, 16))
1402 return False;
1404 return True;
1407 /*******************************************************************
1408 Reads or writes a DOM_GID structure.
1409 ********************************************************************/
1411 BOOL smb_io_gid(char *desc, DOM_GID *gid, prs_struct *ps, int depth)
1413 if (gid == NULL)
1414 return False;
1416 prs_debug(ps, depth, desc, "smb_io_gid");
1417 depth++;
1419 if(!prs_align(ps))
1420 return False;
1422 if(!prs_uint32("g_rid", ps, depth, &gid->g_rid))
1423 return False;
1424 if(!prs_uint32("attr ", ps, depth, &gid->attr))
1425 return False;
1427 return True;
1430 /*******************************************************************
1431 Reads or writes an POLICY_HND structure.
1432 ********************************************************************/
1434 BOOL smb_io_pol_hnd(char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
1436 if (pol == NULL)
1437 return False;
1439 prs_debug(ps, depth, desc, "smb_io_pol_hnd");
1440 depth++;
1442 if(!prs_align(ps))
1443 return False;
1445 if(UNMARSHALLING(ps))
1446 ZERO_STRUCTP(pol);
1448 if (!prs_uint32("data1", ps, depth, &pol->data1))
1449 return False;
1450 if (!prs_uint32("data2", ps, depth, &pol->data2))
1451 return False;
1452 if (!prs_uint16("data3", ps, depth, &pol->data3))
1453 return False;
1454 if (!prs_uint16("data4", ps, depth, &pol->data4))
1455 return False;
1456 if(!prs_uint8s (False, "data5", ps, depth, pol->data5, sizeof(pol->data5)))
1457 return False;
1459 return True;
1462 /*******************************************************************
1463 Create a UNISTR3.
1464 ********************************************************************/
1466 void init_unistr3(UNISTR3 *str, const char *buf)
1468 size_t len;
1470 if (buf == NULL) {
1471 str->uni_str_len=0;
1472 str->str.buffer = NULL;
1473 return;
1476 len = strlen(buf) + 1;
1478 str->uni_str_len=len;
1480 if (len < MAX_UNISTRLEN)
1481 len = MAX_UNISTRLEN;
1483 len *= sizeof(uint16);
1485 str->str.buffer = (uint16 *)talloc_zero(get_talloc_ctx(), len);
1486 if (str->str.buffer == NULL)
1487 smb_panic("init_unistr3: malloc fail\n");
1489 rpcstr_push((char *)str->str.buffer, buf, len, STR_TERMINATE);
1492 /*******************************************************************
1493 Reads or writes a UNISTR3 structure.
1494 ********************************************************************/
1496 BOOL smb_io_unistr3(char *desc, UNISTR3 *name, prs_struct *ps, int depth)
1498 if (name == NULL)
1499 return False;
1501 prs_debug(ps, depth, desc, "smb_io_unistr3");
1502 depth++;
1504 if(!prs_align(ps))
1505 return False;
1507 if(!prs_uint32("uni_str_len", ps, depth, &name->uni_str_len))
1508 return False;
1510 /* don't know if len is specified by uni_str_len member... */
1511 /* assume unicode string is unicode-null-terminated, instead */
1513 if(!prs_unistr3(True, "unistr", name, ps, depth))
1514 return False;
1516 return True;
1520 /*******************************************************************
1521 Stream a uint64_struct
1522 ********************************************************************/
1523 BOOL prs_uint64(char *name, prs_struct *ps, int depth, UINT64_S *data64)
1525 return prs_uint32(name, ps, depth+1, &data64->low) &&
1526 prs_uint32(name, ps, depth+1, &data64->high);
1529 /*******************************************************************
1530 reads or writes a BUFHDR2 structure.
1531 ********************************************************************/
1532 BOOL smb_io_bufhdr2(char *desc, BUFHDR2 *hdr, prs_struct *ps, int depth)
1534 prs_debug(ps, depth, desc, "smb_io_bufhdr2");
1535 depth++;
1537 prs_align(ps);
1538 prs_uint32("info_level", ps, depth, &(hdr->info_level));
1539 prs_uint32("length ", ps, depth, &(hdr->length ));
1540 prs_uint32("buffer ", ps, depth, &(hdr->buffer ));
1542 return True;
1545 /*******************************************************************
1546 reads or writes a BUFFER4 structure.
1547 ********************************************************************/
1548 BOOL smb_io_buffer4(char *desc, BUFFER4 *buf4, uint32 buffer, prs_struct *ps, int depth)
1550 prs_debug(ps, depth, desc, "smb_io_buffer4");
1551 depth++;
1553 prs_align(ps);
1554 prs_uint32("buf_len", ps, depth, &(buf4->buf_len));
1556 if (buf4->buf_len > MAX_BUFFERLEN)
1558 buf4->buf_len = MAX_BUFFERLEN;
1561 prs_uint8s(True, "buffer", ps, depth, buf4->buffer, buf4->buf_len);
1563 return True;
1566 /*******************************************************************
1567 creates a UNIHDR structure.
1568 ********************************************************************/
1570 BOOL make_uni_hdr(UNIHDR *hdr, int len)
1572 if (hdr == NULL)
1574 return False;
1576 hdr->uni_str_len = 2 * len;
1577 hdr->uni_max_len = 2 * len;
1578 hdr->buffer = len != 0 ? 1 : 0;
1580 return True;
1583 /*******************************************************************
1584 creates a BUFHDR2 structure.
1585 ********************************************************************/
1586 BOOL make_bufhdr2(BUFHDR2 *hdr, uint32 info_level, uint32 length, uint32 buffer)
1588 hdr->info_level = info_level;
1589 hdr->length = length;
1590 hdr->buffer = buffer;
1592 return True;