s3-spoolss: move SYSTEMTIME parsing to a more generic place, as suggested.
[Samba.git] / source3 / rpc_parse / parse_misc.c
blobe948afde87b3c858f43c77b7378dea6e7fce16ee
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1997,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6 * Copyright (C) Paul Ashton 1997.
7 * Copyright (C) Gerald (Jerry) Carter 2005
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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_PARSE
28 /*******************************************************************
29 Reads or writes an NTTIME structure.
30 ********************************************************************/
32 bool smb_io_time(const char *desc, NTTIME *nttime, prs_struct *ps, int depth)
34 uint32 low, high;
35 if (nttime == NULL)
36 return False;
38 prs_debug(ps, depth, desc, "smb_io_time");
39 depth++;
41 if(!prs_align(ps))
42 return False;
44 if (MARSHALLING(ps)) {
45 low = *nttime & 0xFFFFFFFF;
46 high = *nttime >> 32;
49 if(!prs_uint32("low ", ps, depth, &low)) /* low part */
50 return False;
51 if(!prs_uint32("high", ps, depth, &high)) /* high part */
52 return False;
54 if (UNMARSHALLING(ps)) {
55 *nttime = (((uint64_t)high << 32) + low);
58 return True;
61 /*******************************************************************
62 Reads or writes an NTTIME structure.
63 ********************************************************************/
65 bool smb_io_nttime(const char *desc, prs_struct *ps, int depth, NTTIME *nttime)
67 return smb_io_time( desc, nttime, ps, depth );
70 /*******************************************************************
71 ********************************************************************/
73 bool smb_io_system_time(const char *desc, prs_struct *ps, int depth, SYSTEMTIME *systime)
75 if(!prs_uint16("year", ps, depth, &systime->year))
76 return False;
77 if(!prs_uint16("month", ps, depth, &systime->month))
78 return False;
79 if(!prs_uint16("dayofweek", ps, depth, &systime->dayofweek))
80 return False;
81 if(!prs_uint16("day", ps, depth, &systime->day))
82 return False;
83 if(!prs_uint16("hour", ps, depth, &systime->hour))
84 return False;
85 if(!prs_uint16("minute", ps, depth, &systime->minute))
86 return False;
87 if(!prs_uint16("second", ps, depth, &systime->second))
88 return False;
89 if(!prs_uint16("milliseconds", ps, depth, &systime->milliseconds))
90 return False;
92 return True;
95 /*******************************************************************
96 ********************************************************************/
98 bool make_systemtime(SYSTEMTIME *systime, struct tm *unixtime)
100 systime->year=unixtime->tm_year+1900;
101 systime->month=unixtime->tm_mon+1;
102 systime->dayofweek=unixtime->tm_wday;
103 systime->day=unixtime->tm_mday;
104 systime->hour=unixtime->tm_hour;
105 systime->minute=unixtime->tm_min;
106 systime->second=unixtime->tm_sec;
107 systime->milliseconds=0;
109 return True;
112 /*******************************************************************
113 Reads or writes a DOM_SID structure.
114 ********************************************************************/
116 bool smb_io_dom_sid(const char *desc, DOM_SID *sid, prs_struct *ps, int depth)
118 int i;
120 if (sid == NULL)
121 return False;
123 prs_debug(ps, depth, desc, "smb_io_dom_sid");
124 depth++;
126 if(!prs_uint8 ("sid_rev_num", ps, depth, &sid->sid_rev_num))
127 return False;
129 if(!prs_uint8 ("num_auths ", ps, depth, (uint8 *)&sid->num_auths))
130 return False;
132 for (i = 0; i < 6; i++)
134 fstring tmp;
135 slprintf(tmp, sizeof(tmp) - 1, "id_auth[%d] ", i);
136 if(!prs_uint8 (tmp, ps, depth, &sid->id_auth[i]))
137 return False;
140 /* oops! XXXX should really issue a warning here... */
141 if (sid->num_auths > MAXSUBAUTHS)
142 sid->num_auths = MAXSUBAUTHS;
144 if(!prs_uint32s(False, "sub_auths ", ps, depth, sid->sub_auths, sid->num_auths))
145 return False;
147 return True;
150 /*******************************************************************
151 Reads or writes a struct GUID
152 ********************************************************************/
154 bool smb_io_uuid(const char *desc, struct GUID *uuid,
155 prs_struct *ps, int depth)
157 if (uuid == NULL)
158 return False;
160 prs_debug(ps, depth, desc, "smb_io_uuid");
161 depth++;
163 if(!prs_uint32 ("data ", ps, depth, &uuid->time_low))
164 return False;
165 if(!prs_uint16 ("data ", ps, depth, &uuid->time_mid))
166 return False;
167 if(!prs_uint16 ("data ", ps, depth, &uuid->time_hi_and_version))
168 return False;
170 if(!prs_uint8s (False, "data ", ps, depth, uuid->clock_seq, sizeof(uuid->clock_seq)))
171 return False;
172 if(!prs_uint8s (False, "data ", ps, depth, uuid->node, sizeof(uuid->node)))
173 return False;
175 return True;
178 /*******************************************************************
179 Inits a UNISTR structure.
180 ********************************************************************/
182 void init_unistr(UNISTR *str, const char *buf)
184 size_t len;
186 if (buf == NULL) {
187 str->buffer = NULL;
188 return;
191 len = rpcstr_push_talloc(talloc_tos(), &str->buffer, buf);
192 if (len == (size_t)-1) {
193 str->buffer = NULL;
197 /*******************************************************************
198 reads or writes a UNISTR structure.
199 XXXX NOTE: UNISTR structures NEED to be null-terminated.
200 ********************************************************************/
202 bool smb_io_unistr(const char *desc, UNISTR *uni, prs_struct *ps, int depth)
204 if (uni == NULL)
205 return False;
207 prs_debug(ps, depth, desc, "smb_io_unistr");
208 depth++;
210 if(!prs_unistr("unistr", ps, depth, uni))
211 return False;
213 return True;
216 /*******************************************************************
217 reads or writes a BUFFER5 structure.
218 the buf_len member tells you how large the buffer is.
219 ********************************************************************/
220 bool smb_io_buffer5(const char *desc, BUFFER5 *buf5, prs_struct *ps, int depth)
222 prs_debug(ps, depth, desc, "smb_io_buffer5");
223 depth++;
225 if (buf5 == NULL) return False;
227 if(!prs_align(ps))
228 return False;
229 if(!prs_uint32("buf_len", ps, depth, &buf5->buf_len))
230 return False;
232 if(buf5->buf_len) {
233 if(!prs_buffer5(True, "buffer" , ps, depth, buf5))
234 return False;
237 return True;
240 /*******************************************************************
241 creates a UNISTR2 structure: sets up the buffer, too
242 ********************************************************************/
244 void init_buf_unistr2(UNISTR2 *str, uint32 *ptr, const char *buf)
246 if (buf != NULL) {
247 *ptr = 1;
248 init_unistr2(str, buf, UNI_STR_TERMINATE);
249 } else {
250 *ptr = 0;
251 init_unistr2(str, NULL, UNI_FLAGS_NONE);
256 /*******************************************************************
257 Copies a UNISTR2 structure.
258 ********************************************************************/
260 void copy_unistr2(UNISTR2 *str, const UNISTR2 *from)
262 if (from->buffer == NULL) {
263 ZERO_STRUCTP(str);
264 return;
267 SMB_ASSERT(from->uni_max_len >= from->uni_str_len);
269 str->uni_max_len = from->uni_max_len;
270 str->offset = from->offset;
271 str->uni_str_len = from->uni_str_len;
273 /* the string buffer is allocated to the maximum size
274 (the the length of the source string) to prevent
275 reallocation of memory. */
276 if (str->buffer == NULL) {
277 if (str->uni_max_len) {
278 str->buffer = (uint16 *)TALLOC_ZERO_ARRAY(talloc_tos(), uint16, str->uni_max_len);
279 if ((str->buffer == NULL)) {
280 smb_panic("copy_unistr2: talloc fail");
281 return;
283 /* copy the string */
284 memcpy(str->buffer, from->buffer, str->uni_max_len*sizeof(uint16));
285 } else {
286 str->buffer = NULL;
291 /*******************************************************************
292 Inits a UNISTR2 structure.
293 ********************************************************************/
295 void init_unistr2(UNISTR2 *str, const char *buf, enum unistr2_term_codes flags)
297 size_t len = 0;
298 uint32 num_chars = 0;
300 if (buf) {
301 /* We always null terminate the copy. */
302 len = strlen(buf) + 1;
303 if ( flags == UNI_STR_DBLTERMINATE )
304 len++;
307 if (buf == NULL || len == 0) {
308 /* no buffer -- nothing to do */
309 str->uni_max_len = 0;
310 str->offset = 0;
311 str->uni_str_len = 0;
313 return;
317 str->buffer = TALLOC_ZERO_ARRAY(talloc_tos(), uint16, len);
318 if (str->buffer == NULL) {
319 smb_panic("init_unistr2: malloc fail");
320 return;
323 /* Ensure len is the length in *bytes* */
324 len *= sizeof(uint16);
327 * The UNISTR2 must be initialized !!!
328 * jfm, 7/7/2001.
330 if (buf) {
331 rpcstr_push((char *)str->buffer, buf, len, STR_TERMINATE);
332 num_chars = strlen_w(str->buffer);
333 if (flags == UNI_STR_TERMINATE || flags == UNI_MAXLEN_TERMINATE) {
334 num_chars++;
336 if ( flags == UNI_STR_DBLTERMINATE )
337 num_chars += 2;
340 str->uni_max_len = num_chars;
341 str->offset = 0;
342 str->uni_str_len = num_chars;
343 if ( num_chars && ((flags == UNI_MAXLEN_TERMINATE) || (flags == UNI_BROKEN_NON_NULL)) )
344 str->uni_max_len++;
347 /**
348 * Inits a UNISTR2 structure.
349 * @param ctx talloc context to allocate string on
350 * @param str pointer to string to create
351 * @param buf UCS2 null-terminated buffer to init from
354 void init_unistr2_w(TALLOC_CTX *ctx, UNISTR2 *str, const smb_ucs2_t *buf)
356 uint32 len = buf ? strlen_w(buf) : 0;
358 ZERO_STRUCTP(str);
360 /* set up string lengths. */
361 str->uni_max_len = len;
362 str->offset = 0;
363 str->uni_str_len = len;
365 if (len + 1) {
366 str->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, len + 1);
367 if (str->buffer == NULL) {
368 smb_panic("init_unistr2_w: talloc fail");
369 return;
371 } else {
372 str->buffer = NULL;
376 * don't move this test above ! The UNISTR2 must be initialized !!!
377 * jfm, 7/7/2001.
379 if (buf==NULL)
380 return;
382 /* Yes, this is a strncpy( foo, bar, strlen(bar)) - but as
383 long as the buffer above is talloc()ed correctly then this
384 is the correct thing to do */
385 if (len+1) {
386 strncpy_w(str->buffer, buf, len + 1);
390 /*******************************************************************
391 Inits a UNISTR2 structure from a UNISTR
392 ********************************************************************/
394 void init_unistr2_from_unistr(TALLOC_CTX *ctx, UNISTR2 *to, const UNISTR *from)
396 uint32 i;
398 /* the destination UNISTR2 should never be NULL.
399 if it is it is a programming error */
401 /* if the source UNISTR is NULL, then zero out
402 the destination string and return */
403 ZERO_STRUCTP (to);
404 if ((from == NULL) || (from->buffer == NULL))
405 return;
407 /* get the length; UNISTR must be NULL terminated */
408 i = 0;
409 while ((from->buffer)[i]!='\0')
410 i++;
411 i++; /* one more to catch the terminating NULL */
412 /* is this necessary -- jerry? I need to think */
414 /* set up string lengths; uni_max_len is set to i+1
415 because we need to account for the final NULL termination */
416 to->uni_max_len = i;
417 to->offset = 0;
418 to->uni_str_len = i;
420 /* allocate the space and copy the string buffer */
421 if (i) {
422 to->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, i);
423 if (to->buffer == NULL)
424 smb_panic("init_unistr2_from_unistr: talloc fail");
425 memcpy(to->buffer, from->buffer, i*sizeof(uint16));
426 } else {
427 to->buffer = NULL;
429 return;
432 /*******************************************************************
433 Inits a UNISTR2 structure from a DATA_BLOB.
434 The length of the data_blob must count the bytes of the buffer.
435 Copies the blob data.
436 ********************************************************************/
438 void init_unistr2_from_datablob(UNISTR2 *str, DATA_BLOB *blob)
440 /* Allocs the unistring */
441 init_unistr2(str, NULL, UNI_FLAGS_NONE);
443 /* Sets the values */
444 str->uni_str_len = blob->length / sizeof(uint16);
445 str->uni_max_len = str->uni_str_len;
446 str->offset = 0;
447 if (blob->length) {
448 str->buffer = (uint16 *) memdup(blob->data, blob->length);
449 } else {
450 str->buffer = NULL;
452 if ((str->buffer == NULL) && (blob->length > 0)) {
453 smb_panic("init_unistr2_from_datablob: malloc fail");
457 /*******************************************************************
458 UNISTR2* are a little different in that the pointer and the UNISTR2
459 are not necessarily read/written back to back. So we break it up
460 into 2 separate functions.
461 See SPOOL_USER_1 in include/rpc_spoolss.h for an example.
462 ********************************************************************/
464 bool prs_io_unistr2_p(const char *desc, prs_struct *ps, int depth, UNISTR2 **uni2)
466 uint32 data_p;
468 /* caputure the pointer value to stream */
470 data_p = *uni2 ? 0xf000baaa : 0;
472 if ( !prs_uint32("ptr", ps, depth, &data_p ))
473 return False;
475 /* we're done if there is no data */
477 if ( !data_p )
478 return True;
480 if (UNMARSHALLING(ps)) {
481 if ( !(*uni2 = PRS_ALLOC_MEM(ps, UNISTR2, 1)) )
482 return False;
485 return True;
488 /*******************************************************************
489 now read/write the actual UNISTR2. Memory for the UNISTR2 (but
490 not UNISTR2.buffer) has been allocated previously by prs_unistr2_p()
491 ********************************************************************/
493 bool prs_io_unistr2(const char *desc, prs_struct *ps, int depth, UNISTR2 *uni2 )
495 /* just return true if there is no pointer to deal with.
496 the memory must have been previously allocated on unmarshalling
497 by prs_unistr2_p() */
499 if ( !uni2 )
500 return True;
502 /* just pass off to smb_io_unstr2() passing the uni2 address as
503 the pointer (like you would expect) */
505 return smb_io_unistr2( desc, uni2, uni2 ? 1 : 0, ps, depth );
508 /*******************************************************************
509 Reads or writes a UNISTR2 structure.
510 XXXX NOTE: UNISTR2 structures need NOT be null-terminated.
511 the uni_str_len member tells you how long the string is;
512 the uni_max_len member tells you how large the buffer is.
513 ********************************************************************/
515 bool smb_io_unistr2(const char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
517 if (uni2 == NULL)
518 return False;
520 if (buffer) {
522 prs_debug(ps, depth, desc, "smb_io_unistr2");
523 depth++;
525 if(!prs_align(ps))
526 return False;
528 if(!prs_uint32("uni_max_len", ps, depth, &uni2->uni_max_len))
529 return False;
530 if(!prs_uint32("offset ", ps, depth, &uni2->offset))
531 return False;
532 if(!prs_uint32("uni_str_len", ps, depth, &uni2->uni_str_len))
533 return False;
535 /* buffer advanced by indicated length of string
536 NOT by searching for null-termination */
537 if(!prs_unistr2(True, "buffer ", ps, depth, uni2))
538 return False;
540 } else {
542 prs_debug(ps, depth, desc, "smb_io_unistr2 - NULL");
543 depth++;
544 memset((char *)uni2, '\0', sizeof(*uni2));
548 return True;
551 /*******************************************************************
552 Reads or writes an POLICY_HND structure.
553 ********************************************************************/
555 bool smb_io_pol_hnd(const char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
557 if (pol == NULL)
558 return False;
560 prs_debug(ps, depth, desc, "smb_io_pol_hnd");
561 depth++;
563 if(!prs_align(ps))
564 return False;
566 if(UNMARSHALLING(ps))
567 ZERO_STRUCTP(pol);
569 if (!prs_uint32("handle_type", ps, depth, &pol->handle_type))
570 return False;
571 if (!smb_io_uuid("uuid", (struct GUID*)&pol->uuid, ps, depth))
572 return False;
574 return True;
577 /*******************************************************************
578 Create a UNISTR3.
579 ********************************************************************/
581 void init_unistr3(UNISTR3 *str, const char *buf)
583 if (buf == NULL) {
584 str->uni_str_len=0;
585 str->str.buffer = NULL;
586 return;
589 str->uni_str_len = strlen(buf) + 1;
591 if (str->uni_str_len) {
592 str->str.buffer = TALLOC_ZERO_ARRAY(talloc_tos(), uint16, str->uni_str_len);
593 if (str->str.buffer == NULL)
594 smb_panic("init_unistr3: malloc fail");
596 rpcstr_push((char *)str->str.buffer, buf, str->uni_str_len * sizeof(uint16), STR_TERMINATE);
597 } else {
598 str->str.buffer = NULL;
602 /*******************************************************************
603 Reads or writes a UNISTR3 structure.
604 ********************************************************************/
606 bool smb_io_unistr3(const char *desc, UNISTR3 *name, prs_struct *ps, int depth)
608 if (name == NULL)
609 return False;
611 prs_debug(ps, depth, desc, "smb_io_unistr3");
612 depth++;
614 if(!prs_align(ps))
615 return False;
617 if(!prs_uint32("uni_str_len", ps, depth, &name->uni_str_len))
618 return False;
620 /* we're done if there is no string */
622 if ( name->uni_str_len == 0 )
623 return True;
625 /* don't know if len is specified by uni_str_len member... */
626 /* assume unicode string is unicode-null-terminated, instead */
628 if(!prs_unistr3(True, "unistr", name, ps, depth))
629 return False;
631 return True;
634 /*******************************************************************
635 Stream a uint64_struct
636 ********************************************************************/
637 bool prs_uint64(const char *name, prs_struct *ps, int depth, uint64 *data64)
639 if (UNMARSHALLING(ps)) {
640 uint32 high, low;
642 if (!prs_uint32(name, ps, depth+1, &low))
643 return False;
645 if (!prs_uint32(name, ps, depth+1, &high))
646 return False;
648 *data64 = ((uint64_t)high << 32) + low;
650 return True;
651 } else {
652 uint32 high = (*data64) >> 32, low = (*data64) & 0xFFFFFFFF;
653 return prs_uint32(name, ps, depth+1, &low) &&
654 prs_uint32(name, ps, depth+1, &high);
658 /*******************************************************************
659 return the length of a UNISTR string.
660 ********************************************************************/
662 uint32 str_len_uni(UNISTR *source)
664 uint32 i=0;
666 if (!source->buffer)
667 return 0;
669 while (source->buffer[i])
670 i++;
672 return i;
675 /*******************************************************************
676 Verifies policy handle
677 ********************************************************************/
679 bool policy_handle_is_valid(const POLICY_HND *hnd)
681 POLICY_HND zero_pol;
683 ZERO_STRUCT(zero_pol);
684 return ((memcmp(&zero_pol, hnd, sizeof(POLICY_HND)) == 0) ? false : true );