bddd4212dc5b1e9695a36e03c34fbb40e066800a
[ira/wip.git] / source3 / rpc_parse / parse_misc.c
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.
13  *  
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *  
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_PARSE
27
28 /*******************************************************************
29  Reads or writes an NTTIME structure.
30 ********************************************************************/
31
32 bool smb_io_time(const char *desc, NTTIME *nttime, prs_struct *ps, int depth)
33 {
34         uint32 low, high;
35         if (nttime == NULL)
36                 return False;
37
38         prs_debug(ps, depth, desc, "smb_io_time");
39         depth++;
40
41         if(!prs_align(ps))
42                 return False;
43         
44         if (MARSHALLING(ps)) {
45                 low = *nttime & 0xFFFFFFFF;
46                 high = *nttime >> 32;
47         }
48         
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;
53
54         if (UNMARSHALLING(ps)) {
55                 *nttime = (((uint64_t)high << 32) + low);
56         }
57
58         return True;
59 }
60
61 /*******************************************************************
62  Reads or writes an NTTIME structure.
63 ********************************************************************/
64
65 bool smb_io_nttime(const char *desc, prs_struct *ps, int depth, NTTIME *nttime)
66 {
67         return smb_io_time( desc, nttime, ps, depth );
68 }
69
70 /*******************************************************************
71 ********************************************************************/
72
73 bool smb_io_system_time(const char *desc, prs_struct *ps, int depth, SYSTEMTIME *systime)
74 {
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;
91
92         return True;
93 }
94
95 /*******************************************************************
96 ********************************************************************/
97
98 bool make_systemtime(SYSTEMTIME *systime, struct tm *unixtime)
99 {
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;
108
109         return True;
110 }
111
112 /*******************************************************************
113  Reads or writes a DOM_SID structure.
114 ********************************************************************/
115
116 bool smb_io_dom_sid(const char *desc, DOM_SID *sid, prs_struct *ps, int depth)
117 {
118         int i;
119
120         if (sid == NULL)
121                 return False;
122
123         prs_debug(ps, depth, desc, "smb_io_dom_sid");
124         depth++;
125
126         if(!prs_uint8 ("sid_rev_num", ps, depth, &sid->sid_rev_num))
127                 return False;
128
129         if(!prs_uint8 ("num_auths  ", ps, depth, (uint8 *)&sid->num_auths))
130                 return False;
131
132         for (i = 0; i < 6; i++)
133         {
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;
138         }
139
140         /* oops! XXXX should really issue a warning here... */
141         if (sid->num_auths > MAXSUBAUTHS)
142                 sid->num_auths = MAXSUBAUTHS;
143
144         if(!prs_uint32s(False, "sub_auths ", ps, depth, sid->sub_auths, sid->num_auths))
145                 return False;
146
147         return True;
148 }
149
150 /*******************************************************************
151  Reads or writes a struct GUID
152 ********************************************************************/
153
154 bool smb_io_uuid(const char *desc, struct GUID *uuid, 
155                  prs_struct *ps, int depth)
156 {
157         if (uuid == NULL)
158                 return False;
159
160         prs_debug(ps, depth, desc, "smb_io_uuid");
161         depth++;
162
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;
169
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;
174
175         return True;
176 }
177
178 /*******************************************************************
179  Inits a UNISTR structure.
180 ********************************************************************/
181
182 void init_unistr(UNISTR *str, const char *buf)
183 {
184         size_t len;
185
186         if (buf == NULL) {
187                 str->buffer = NULL;
188                 return;
189         }
190
191         len = rpcstr_push_talloc(talloc_tos(), &str->buffer, buf);
192         if (len == (size_t)-1) {
193                 str->buffer = NULL;
194         }
195 }
196
197 /*******************************************************************
198 reads or writes a UNISTR structure.
199 XXXX NOTE: UNISTR structures NEED to be null-terminated.
200 ********************************************************************/
201
202 bool smb_io_unistr(const char *desc, UNISTR *uni, prs_struct *ps, int depth)
203 {
204         if (uni == NULL)
205                 return False;
206
207         prs_debug(ps, depth, desc, "smb_io_unistr");
208         depth++;
209
210         if(!prs_unistr("unistr", ps, depth, uni))
211                 return False;
212
213         return True;
214 }
215
216 /*******************************************************************
217 creates a UNISTR2 structure: sets up the buffer, too
218 ********************************************************************/
219
220 void init_buf_unistr2(UNISTR2 *str, uint32 *ptr, const char *buf)
221 {
222         if (buf != NULL) {
223                 *ptr = 1;
224                 init_unistr2(str, buf, UNI_STR_TERMINATE);
225         } else {
226                 *ptr = 0;
227                 init_unistr2(str, NULL, UNI_FLAGS_NONE);
228
229         }
230 }
231
232 /*******************************************************************
233  Copies a UNISTR2 structure.
234 ********************************************************************/
235
236 void copy_unistr2(UNISTR2 *str, const UNISTR2 *from)
237 {
238         if (from->buffer == NULL) {
239                 ZERO_STRUCTP(str);
240                 return;
241         }
242
243         SMB_ASSERT(from->uni_max_len >= from->uni_str_len);
244
245         str->uni_max_len = from->uni_max_len;
246         str->offset      = from->offset;
247         str->uni_str_len = from->uni_str_len;
248
249         /* the string buffer is allocated to the maximum size
250            (the the length of the source string) to prevent
251            reallocation of memory. */
252         if (str->buffer == NULL) {
253                 if (str->uni_max_len) {
254                         str->buffer = (uint16 *)TALLOC_ZERO_ARRAY(talloc_tos(), uint16, str->uni_max_len);
255                         if ((str->buffer == NULL)) {
256                                 smb_panic("copy_unistr2: talloc fail");
257                                 return;
258                         }
259                         /* copy the string */
260                         memcpy(str->buffer, from->buffer, str->uni_max_len*sizeof(uint16));
261                 } else {
262                         str->buffer = NULL;
263                 }
264         }
265 }
266
267 /*******************************************************************
268  Inits a UNISTR2 structure.
269 ********************************************************************/
270
271 void init_unistr2(UNISTR2 *str, const char *buf, enum unistr2_term_codes flags)
272 {
273         size_t len = 0;
274         uint32 num_chars = 0;
275
276         if (buf) {
277                 /* We always null terminate the copy. */
278                 len = strlen(buf) + 1;
279                 if ( flags == UNI_STR_DBLTERMINATE )
280                         len++;
281         }
282
283         if (buf == NULL || len == 0) {
284                 /* no buffer -- nothing to do */
285                 str->uni_max_len = 0;
286                 str->offset = 0;
287                 str->uni_str_len = 0;
288
289                 return;
290         }
291         
292
293         str->buffer = TALLOC_ZERO_ARRAY(talloc_tos(), uint16, len);
294         if (str->buffer == NULL) {
295                 smb_panic("init_unistr2: malloc fail");
296                 return;
297         }
298
299         /* Ensure len is the length in *bytes* */
300         len *= sizeof(uint16);
301
302         /*
303          * The UNISTR2 must be initialized !!!
304          * jfm, 7/7/2001.
305          */
306         if (buf) {
307                 rpcstr_push((char *)str->buffer, buf, len, STR_TERMINATE);
308                 num_chars = strlen_w(str->buffer);
309                 if (flags == UNI_STR_TERMINATE || flags == UNI_MAXLEN_TERMINATE) {
310                         num_chars++;
311                 }
312                 if ( flags == UNI_STR_DBLTERMINATE )
313                         num_chars += 2;
314         }
315
316         str->uni_max_len = num_chars;
317         str->offset = 0;
318         str->uni_str_len = num_chars;
319         if ( num_chars && ((flags == UNI_MAXLEN_TERMINATE) || (flags == UNI_BROKEN_NON_NULL)) )
320                 str->uni_max_len++;
321 }
322
323 /** 
324  *  Inits a UNISTR2 structure.
325  *  @param  ctx talloc context to allocate string on
326  *  @param  str pointer to string to create
327  *  @param  buf UCS2 null-terminated buffer to init from
328 */
329
330 void init_unistr2_w(TALLOC_CTX *ctx, UNISTR2 *str, const smb_ucs2_t *buf)
331 {
332         uint32 len = buf ? strlen_w(buf) : 0;
333
334         ZERO_STRUCTP(str);
335
336         /* set up string lengths. */
337         str->uni_max_len = len;
338         str->offset = 0;
339         str->uni_str_len = len;
340
341         if (len + 1) {
342                 str->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, len + 1);
343                 if (str->buffer == NULL) {
344                         smb_panic("init_unistr2_w: talloc fail");
345                         return;
346                 }
347         } else {
348                 str->buffer = NULL;
349         }
350         
351         /*
352          * don't move this test above ! The UNISTR2 must be initialized !!!
353          * jfm, 7/7/2001.
354          */
355         if (buf==NULL)
356                 return;
357         
358         /* Yes, this is a strncpy( foo, bar, strlen(bar)) - but as
359            long as the buffer above is talloc()ed correctly then this
360            is the correct thing to do */
361         if (len+1) {
362                 strncpy_w(str->buffer, buf, len + 1);
363         }
364 }
365
366 /*******************************************************************
367  Inits a UNISTR2 structure from a UNISTR
368 ********************************************************************/
369
370 void init_unistr2_from_unistr(TALLOC_CTX *ctx, UNISTR2 *to, const UNISTR *from)
371 {
372         uint32 i;
373
374         /* the destination UNISTR2 should never be NULL.
375            if it is it is a programming error */
376
377         /* if the source UNISTR is NULL, then zero out
378            the destination string and return */
379         ZERO_STRUCTP (to);
380         if ((from == NULL) || (from->buffer == NULL))
381                 return;
382
383         /* get the length; UNISTR must be NULL terminated */
384         i = 0;
385         while ((from->buffer)[i]!='\0')
386                 i++;
387         i++;    /* one more to catch the terminating NULL */
388                 /* is this necessary -- jerry?  I need to think */
389
390         /* set up string lengths; uni_max_len is set to i+1
391            because we need to account for the final NULL termination */
392         to->uni_max_len = i;
393         to->offset = 0;
394         to->uni_str_len = i;
395
396         /* allocate the space and copy the string buffer */
397         if (i) {
398                 to->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, i);
399                 if (to->buffer == NULL)
400                         smb_panic("init_unistr2_from_unistr: talloc fail");
401                 memcpy(to->buffer, from->buffer, i*sizeof(uint16));
402         } else {
403                 to->buffer = NULL;
404         }
405         return;
406 }
407
408 /*******************************************************************
409   Inits a UNISTR2 structure from a DATA_BLOB.
410   The length of the data_blob must count the bytes of the buffer.
411   Copies the blob data.
412 ********************************************************************/
413
414 void init_unistr2_from_datablob(UNISTR2 *str, DATA_BLOB *blob) 
415 {
416         /* Allocs the unistring */
417         init_unistr2(str, NULL, UNI_FLAGS_NONE);
418         
419         /* Sets the values */
420         str->uni_str_len = blob->length / sizeof(uint16);
421         str->uni_max_len = str->uni_str_len;
422         str->offset = 0;
423         if (blob->length) {
424                 str->buffer = (uint16 *) memdup(blob->data, blob->length);
425         } else {
426                 str->buffer = NULL;
427         }
428         if ((str->buffer == NULL) && (blob->length > 0)) {
429                 smb_panic("init_unistr2_from_datablob: malloc fail");
430         }
431 }
432
433 /*******************************************************************
434  UNISTR2* are a little different in that the pointer and the UNISTR2
435  are not necessarily read/written back to back.  So we break it up 
436  into 2 separate functions.
437  See SPOOL_USER_1 in include/rpc_spoolss.h for an example.
438 ********************************************************************/
439
440 bool prs_io_unistr2_p(const char *desc, prs_struct *ps, int depth, UNISTR2 **uni2)
441 {
442         uint32 data_p;
443
444         /* caputure the pointer value to stream */
445
446         data_p = *uni2 ? 0xf000baaa : 0;
447
448         if ( !prs_uint32("ptr", ps, depth, &data_p ))
449                 return False;
450
451         /* we're done if there is no data */
452
453         if ( !data_p )
454                 return True;
455
456         if (UNMARSHALLING(ps)) {
457                 if ( !(*uni2 = PRS_ALLOC_MEM(ps, UNISTR2, 1)) )
458                         return False;
459         }
460
461         return True;
462 }
463
464 /*******************************************************************
465  now read/write the actual UNISTR2.  Memory for the UNISTR2 (but
466  not UNISTR2.buffer) has been allocated previously by prs_unistr2_p()
467 ********************************************************************/
468
469 bool prs_io_unistr2(const char *desc, prs_struct *ps, int depth, UNISTR2 *uni2 )
470 {
471         /* just return true if there is no pointer to deal with.
472            the memory must have been previously allocated on unmarshalling
473            by prs_unistr2_p() */
474
475         if ( !uni2 )
476                 return True;
477
478         /* just pass off to smb_io_unstr2() passing the uni2 address as 
479            the pointer (like you would expect) */
480
481         return smb_io_unistr2( desc, uni2, uni2 ? 1 : 0, ps, depth );
482 }
483
484 /*******************************************************************
485  Reads or writes a UNISTR2 structure.
486  XXXX NOTE: UNISTR2 structures need NOT be null-terminated.
487    the uni_str_len member tells you how long the string is;
488    the uni_max_len member tells you how large the buffer is.
489 ********************************************************************/
490
491 bool smb_io_unistr2(const char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
492 {
493         if (uni2 == NULL)
494                 return False;
495
496         if (buffer) {
497
498                 prs_debug(ps, depth, desc, "smb_io_unistr2");
499                 depth++;
500
501                 if(!prs_align(ps))
502                         return False;
503                 
504                 if(!prs_uint32("uni_max_len", ps, depth, &uni2->uni_max_len))
505                         return False;
506                 if(!prs_uint32("offset     ", ps, depth, &uni2->offset))
507                         return False;
508                 if(!prs_uint32("uni_str_len", ps, depth, &uni2->uni_str_len))
509                         return False;
510
511                 /* buffer advanced by indicated length of string
512                    NOT by searching for null-termination */
513                 if(!prs_unistr2(True, "buffer     ", ps, depth, uni2))
514                         return False;
515
516         } else {
517
518                 prs_debug(ps, depth, desc, "smb_io_unistr2 - NULL");
519                 depth++;
520                 memset((char *)uni2, '\0', sizeof(*uni2));
521
522         }
523
524         return True;
525 }
526
527 /*******************************************************************
528  Reads or writes an POLICY_HND structure.
529 ********************************************************************/
530
531 bool smb_io_pol_hnd(const char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
532 {
533         if (pol == NULL)
534                 return False;
535
536         prs_debug(ps, depth, desc, "smb_io_pol_hnd");
537         depth++;
538
539         if(!prs_align(ps))
540                 return False;
541
542         if(UNMARSHALLING(ps))
543                 ZERO_STRUCTP(pol);
544         
545         if (!prs_uint32("handle_type", ps, depth, &pol->handle_type))
546                 return False;
547         if (!smb_io_uuid("uuid", (struct GUID*)&pol->uuid, ps, depth))
548                 return False;
549
550         return True;
551 }
552
553 /*******************************************************************
554 return the length of a UNISTR string.
555 ********************************************************************/  
556
557 uint32 str_len_uni(UNISTR *source)
558 {
559         uint32 i=0;
560
561         if (!source->buffer)
562                 return 0;
563
564         while (source->buffer[i])
565                 i++;
566
567         return i;
568 }
569
570 /*******************************************************************
571  Verifies policy handle
572 ********************************************************************/
573
574 bool policy_handle_is_valid(const POLICY_HND *hnd)
575 {
576         POLICY_HND zero_pol;
577
578         ZERO_STRUCT(zero_pol);
579         return ((memcmp(&zero_pol, hnd, sizeof(POLICY_HND)) == 0) ? false : true );
580 }