Merge branch 'nodiscard' of /home/jelmer/samba4
[kai/samba.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  Reads or writes a DOM_SID structure.
72 ********************************************************************/
73
74 bool smb_io_dom_sid(const char *desc, DOM_SID *sid, prs_struct *ps, int depth)
75 {
76         int i;
77
78         if (sid == NULL)
79                 return False;
80
81         prs_debug(ps, depth, desc, "smb_io_dom_sid");
82         depth++;
83
84         if(!prs_uint8 ("sid_rev_num", ps, depth, &sid->sid_rev_num))
85                 return False;
86
87         if(!prs_uint8 ("num_auths  ", ps, depth, (uint8 *)&sid->num_auths))
88                 return False;
89
90         for (i = 0; i < 6; i++)
91         {
92                 fstring tmp;
93                 slprintf(tmp, sizeof(tmp) - 1, "id_auth[%d] ", i);
94                 if(!prs_uint8 (tmp, ps, depth, &sid->id_auth[i]))
95                         return False;
96         }
97
98         /* oops! XXXX should really issue a warning here... */
99         if (sid->num_auths > MAXSUBAUTHS)
100                 sid->num_auths = MAXSUBAUTHS;
101
102         if(!prs_uint32s(False, "sub_auths ", ps, depth, sid->sub_auths, sid->num_auths))
103                 return False;
104
105         return True;
106 }
107
108 /*******************************************************************
109  Reads or writes a struct GUID
110 ********************************************************************/
111
112 bool smb_io_uuid(const char *desc, struct GUID *uuid, 
113                  prs_struct *ps, int depth)
114 {
115         if (uuid == NULL)
116                 return False;
117
118         prs_debug(ps, depth, desc, "smb_io_uuid");
119         depth++;
120
121         if(!prs_uint32 ("data   ", ps, depth, &uuid->time_low))
122                 return False;
123         if(!prs_uint16 ("data   ", ps, depth, &uuid->time_mid))
124                 return False;
125         if(!prs_uint16 ("data   ", ps, depth, &uuid->time_hi_and_version))
126                 return False;
127
128         if(!prs_uint8s (False, "data   ", ps, depth, uuid->clock_seq, sizeof(uuid->clock_seq)))
129                 return False;
130         if(!prs_uint8s (False, "data   ", ps, depth, uuid->node, sizeof(uuid->node)))
131                 return False;
132
133         return True;
134 }
135
136 /*******************************************************************
137  Inits a UNISTR structure.
138 ********************************************************************/
139
140 void init_unistr(UNISTR *str, const char *buf)
141 {
142         size_t len;
143
144         if (buf == NULL) {
145                 str->buffer = NULL;
146                 return;
147         }
148
149         len = rpcstr_push_talloc(talloc_tos(), &str->buffer, buf);
150         if (len == (size_t)-1) {
151                 str->buffer = NULL;
152         }
153 }
154
155 /*******************************************************************
156 reads or writes a UNISTR structure.
157 XXXX NOTE: UNISTR structures NEED to be null-terminated.
158 ********************************************************************/
159
160 bool smb_io_unistr(const char *desc, UNISTR *uni, prs_struct *ps, int depth)
161 {
162         if (uni == NULL)
163                 return False;
164
165         prs_debug(ps, depth, desc, "smb_io_unistr");
166         depth++;
167
168         if(!prs_unistr("unistr", ps, depth, uni))
169                 return False;
170
171         return True;
172 }
173
174 /*******************************************************************
175 reads or writes a BUFFER5 structure.
176 the buf_len member tells you how large the buffer is.
177 ********************************************************************/
178 bool smb_io_buffer5(const char *desc, BUFFER5 *buf5, prs_struct *ps, int depth)
179 {
180         prs_debug(ps, depth, desc, "smb_io_buffer5");
181         depth++;
182
183         if (buf5 == NULL) return False;
184
185         if(!prs_align(ps))
186                 return False;
187         if(!prs_uint32("buf_len", ps, depth, &buf5->buf_len))
188                 return False;
189
190         if(buf5->buf_len) {
191                 if(!prs_buffer5(True, "buffer" , ps, depth, buf5))
192                         return False;
193         }
194
195         return True;
196 }
197
198 /*******************************************************************
199 creates a UNISTR2 structure: sets up the buffer, too
200 ********************************************************************/
201
202 void init_buf_unistr2(UNISTR2 *str, uint32 *ptr, const char *buf)
203 {
204         if (buf != NULL) {
205                 *ptr = 1;
206                 init_unistr2(str, buf, UNI_STR_TERMINATE);
207         } else {
208                 *ptr = 0;
209                 init_unistr2(str, NULL, UNI_FLAGS_NONE);
210
211         }
212 }
213
214 /*******************************************************************
215  Copies a UNISTR2 structure.
216 ********************************************************************/
217
218 void copy_unistr2(UNISTR2 *str, const UNISTR2 *from)
219 {
220         if (from->buffer == NULL) {
221                 ZERO_STRUCTP(str);
222                 return;
223         }
224
225         SMB_ASSERT(from->uni_max_len >= from->uni_str_len);
226
227         str->uni_max_len = from->uni_max_len;
228         str->offset      = from->offset;
229         str->uni_str_len = from->uni_str_len;
230
231         /* the string buffer is allocated to the maximum size
232            (the the length of the source string) to prevent
233            reallocation of memory. */
234         if (str->buffer == NULL) {
235                 if (str->uni_max_len) {
236                         str->buffer = (uint16 *)TALLOC_ZERO_ARRAY(talloc_tos(), uint16, str->uni_max_len);
237                         if ((str->buffer == NULL)) {
238                                 smb_panic("copy_unistr2: talloc fail");
239                                 return;
240                         }
241                         /* copy the string */
242                         memcpy(str->buffer, from->buffer, str->uni_max_len*sizeof(uint16));
243                 } else {
244                         str->buffer = NULL;
245                 }
246         }
247 }
248
249 /*******************************************************************
250  Inits a UNISTR2 structure.
251 ********************************************************************/
252
253 void init_unistr2(UNISTR2 *str, const char *buf, enum unistr2_term_codes flags)
254 {
255         size_t len = 0;
256         uint32 num_chars = 0;
257
258         if (buf) {
259                 /* We always null terminate the copy. */
260                 len = strlen(buf) + 1;
261                 if ( flags == UNI_STR_DBLTERMINATE )
262                         len++;
263         }
264
265         if (buf == NULL || len == 0) {
266                 /* no buffer -- nothing to do */
267                 str->uni_max_len = 0;
268                 str->offset = 0;
269                 str->uni_str_len = 0;
270
271                 return;
272         }
273         
274
275         str->buffer = TALLOC_ZERO_ARRAY(talloc_tos(), uint16, len);
276         if (str->buffer == NULL) {
277                 smb_panic("init_unistr2: malloc fail");
278                 return;
279         }
280
281         /* Ensure len is the length in *bytes* */
282         len *= sizeof(uint16);
283
284         /*
285          * The UNISTR2 must be initialized !!!
286          * jfm, 7/7/2001.
287          */
288         if (buf) {
289                 rpcstr_push((char *)str->buffer, buf, len, STR_TERMINATE);
290                 num_chars = strlen_w(str->buffer);
291                 if (flags == UNI_STR_TERMINATE || flags == UNI_MAXLEN_TERMINATE) {
292                         num_chars++;
293                 }
294                 if ( flags == UNI_STR_DBLTERMINATE )
295                         num_chars += 2;
296         }
297
298         str->uni_max_len = num_chars;
299         str->offset = 0;
300         str->uni_str_len = num_chars;
301         if ( num_chars && ((flags == UNI_MAXLEN_TERMINATE) || (flags == UNI_BROKEN_NON_NULL)) )
302                 str->uni_max_len++;
303 }
304
305 /** 
306  *  Inits a UNISTR2 structure.
307  *  @param  ctx talloc context to allocate string on
308  *  @param  str pointer to string to create
309  *  @param  buf UCS2 null-terminated buffer to init from
310 */
311
312 void init_unistr2_w(TALLOC_CTX *ctx, UNISTR2 *str, const smb_ucs2_t *buf)
313 {
314         uint32 len = buf ? strlen_w(buf) : 0;
315
316         ZERO_STRUCTP(str);
317
318         /* set up string lengths. */
319         str->uni_max_len = len;
320         str->offset = 0;
321         str->uni_str_len = len;
322
323         if (len + 1) {
324                 str->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, len + 1);
325                 if (str->buffer == NULL) {
326                         smb_panic("init_unistr2_w: talloc fail");
327                         return;
328                 }
329         } else {
330                 str->buffer = NULL;
331         }
332         
333         /*
334          * don't move this test above ! The UNISTR2 must be initialized !!!
335          * jfm, 7/7/2001.
336          */
337         if (buf==NULL)
338                 return;
339         
340         /* Yes, this is a strncpy( foo, bar, strlen(bar)) - but as
341            long as the buffer above is talloc()ed correctly then this
342            is the correct thing to do */
343         if (len+1) {
344                 strncpy_w(str->buffer, buf, len + 1);
345         }
346 }
347
348 /*******************************************************************
349  Inits a UNISTR2 structure from a UNISTR
350 ********************************************************************/
351
352 void init_unistr2_from_unistr(TALLOC_CTX *ctx, UNISTR2 *to, const UNISTR *from)
353 {
354         uint32 i;
355
356         /* the destination UNISTR2 should never be NULL.
357            if it is it is a programming error */
358
359         /* if the source UNISTR is NULL, then zero out
360            the destination string and return */
361         ZERO_STRUCTP (to);
362         if ((from == NULL) || (from->buffer == NULL))
363                 return;
364
365         /* get the length; UNISTR must be NULL terminated */
366         i = 0;
367         while ((from->buffer)[i]!='\0')
368                 i++;
369         i++;    /* one more to catch the terminating NULL */
370                 /* is this necessary -- jerry?  I need to think */
371
372         /* set up string lengths; uni_max_len is set to i+1
373            because we need to account for the final NULL termination */
374         to->uni_max_len = i;
375         to->offset = 0;
376         to->uni_str_len = i;
377
378         /* allocate the space and copy the string buffer */
379         if (i) {
380                 to->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, i);
381                 if (to->buffer == NULL)
382                         smb_panic("init_unistr2_from_unistr: talloc fail");
383                 memcpy(to->buffer, from->buffer, i*sizeof(uint16));
384         } else {
385                 to->buffer = NULL;
386         }
387         return;
388 }
389
390 /*******************************************************************
391   Inits a UNISTR2 structure from a DATA_BLOB.
392   The length of the data_blob must count the bytes of the buffer.
393   Copies the blob data.
394 ********************************************************************/
395
396 void init_unistr2_from_datablob(UNISTR2 *str, DATA_BLOB *blob) 
397 {
398         /* Allocs the unistring */
399         init_unistr2(str, NULL, UNI_FLAGS_NONE);
400         
401         /* Sets the values */
402         str->uni_str_len = blob->length / sizeof(uint16);
403         str->uni_max_len = str->uni_str_len;
404         str->offset = 0;
405         if (blob->length) {
406                 str->buffer = (uint16 *) memdup(blob->data, blob->length);
407         } else {
408                 str->buffer = NULL;
409         }
410         if ((str->buffer == NULL) && (blob->length > 0)) {
411                 smb_panic("init_unistr2_from_datablob: malloc fail");
412         }
413 }
414
415 /*******************************************************************
416  UNISTR2* are a little different in that the pointer and the UNISTR2
417  are not necessarily read/written back to back.  So we break it up 
418  into 2 separate functions.
419  See SPOOL_USER_1 in include/rpc_spoolss.h for an example.
420 ********************************************************************/
421
422 bool prs_io_unistr2_p(const char *desc, prs_struct *ps, int depth, UNISTR2 **uni2)
423 {
424         uint32 data_p;
425
426         /* caputure the pointer value to stream */
427
428         data_p = *uni2 ? 0xf000baaa : 0;
429
430         if ( !prs_uint32("ptr", ps, depth, &data_p ))
431                 return False;
432
433         /* we're done if there is no data */
434
435         if ( !data_p )
436                 return True;
437
438         if (UNMARSHALLING(ps)) {
439                 if ( !(*uni2 = PRS_ALLOC_MEM(ps, UNISTR2, 1)) )
440                         return False;
441         }
442
443         return True;
444 }
445
446 /*******************************************************************
447  now read/write the actual UNISTR2.  Memory for the UNISTR2 (but
448  not UNISTR2.buffer) has been allocated previously by prs_unistr2_p()
449 ********************************************************************/
450
451 bool prs_io_unistr2(const char *desc, prs_struct *ps, int depth, UNISTR2 *uni2 )
452 {
453         /* just return true if there is no pointer to deal with.
454            the memory must have been previously allocated on unmarshalling
455            by prs_unistr2_p() */
456
457         if ( !uni2 )
458                 return True;
459
460         /* just pass off to smb_io_unstr2() passing the uni2 address as 
461            the pointer (like you would expect) */
462
463         return smb_io_unistr2( desc, uni2, uni2 ? 1 : 0, ps, depth );
464 }
465
466 /*******************************************************************
467  Reads or writes a UNISTR2 structure.
468  XXXX NOTE: UNISTR2 structures need NOT be null-terminated.
469    the uni_str_len member tells you how long the string is;
470    the uni_max_len member tells you how large the buffer is.
471 ********************************************************************/
472
473 bool smb_io_unistr2(const char *desc, UNISTR2 *uni2, uint32 buffer, prs_struct *ps, int depth)
474 {
475         if (uni2 == NULL)
476                 return False;
477
478         if (buffer) {
479
480                 prs_debug(ps, depth, desc, "smb_io_unistr2");
481                 depth++;
482
483                 if(!prs_align(ps))
484                         return False;
485                 
486                 if(!prs_uint32("uni_max_len", ps, depth, &uni2->uni_max_len))
487                         return False;
488                 if(!prs_uint32("offset     ", ps, depth, &uni2->offset))
489                         return False;
490                 if(!prs_uint32("uni_str_len", ps, depth, &uni2->uni_str_len))
491                         return False;
492
493                 /* buffer advanced by indicated length of string
494                    NOT by searching for null-termination */
495                 if(!prs_unistr2(True, "buffer     ", ps, depth, uni2))
496                         return False;
497
498         } else {
499
500                 prs_debug(ps, depth, desc, "smb_io_unistr2 - NULL");
501                 depth++;
502                 memset((char *)uni2, '\0', sizeof(*uni2));
503
504         }
505
506         return True;
507 }
508
509 /*******************************************************************
510  Reads or writes an POLICY_HND structure.
511 ********************************************************************/
512
513 bool smb_io_pol_hnd(const char *desc, POLICY_HND *pol, prs_struct *ps, int depth)
514 {
515         if (pol == NULL)
516                 return False;
517
518         prs_debug(ps, depth, desc, "smb_io_pol_hnd");
519         depth++;
520
521         if(!prs_align(ps))
522                 return False;
523
524         if(UNMARSHALLING(ps))
525                 ZERO_STRUCTP(pol);
526         
527         if (!prs_uint32("handle_type", ps, depth, &pol->handle_type))
528                 return False;
529         if (!smb_io_uuid("uuid", (struct GUID*)&pol->uuid, ps, depth))
530                 return False;
531
532         return True;
533 }
534
535 /*******************************************************************
536  Create a UNISTR3.
537 ********************************************************************/
538
539 void init_unistr3(UNISTR3 *str, const char *buf)
540 {
541         if (buf == NULL) {
542                 str->uni_str_len=0;
543                 str->str.buffer = NULL;
544                 return;
545         }
546
547         str->uni_str_len = strlen(buf) + 1;
548
549         if (str->uni_str_len) {
550                 str->str.buffer = TALLOC_ZERO_ARRAY(talloc_tos(), uint16, str->uni_str_len);
551                 if (str->str.buffer == NULL)
552                         smb_panic("init_unistr3: malloc fail");
553
554                 rpcstr_push((char *)str->str.buffer, buf, str->uni_str_len * sizeof(uint16), STR_TERMINATE);
555         } else {
556                 str->str.buffer = NULL;
557         }
558 }
559
560 /*******************************************************************
561  Reads or writes a UNISTR3 structure.
562 ********************************************************************/
563
564 bool smb_io_unistr3(const char *desc, UNISTR3 *name, prs_struct *ps, int depth)
565 {
566         if (name == NULL)
567                 return False;
568
569         prs_debug(ps, depth, desc, "smb_io_unistr3");
570         depth++;
571
572         if(!prs_align(ps))
573                 return False;
574         
575         if(!prs_uint32("uni_str_len", ps, depth, &name->uni_str_len))
576                 return False;
577                 
578         /* we're done if there is no string */
579         
580         if ( name->uni_str_len == 0 )
581                 return True;
582
583         /* don't know if len is specified by uni_str_len member... */
584         /* assume unicode string is unicode-null-terminated, instead */
585
586         if(!prs_unistr3(True, "unistr", name, ps, depth))
587                 return False;
588
589         return True;
590 }
591
592 /*******************************************************************
593  Stream a uint64_struct
594  ********************************************************************/
595 bool prs_uint64(const char *name, prs_struct *ps, int depth, uint64 *data64)
596 {
597         if (UNMARSHALLING(ps)) {
598                 uint32 high, low;
599
600                 if (!prs_uint32(name, ps, depth+1, &low))
601                         return False;
602
603                 if (!prs_uint32(name, ps, depth+1, &high))
604                         return False;
605
606                 *data64 = ((uint64_t)high << 32) + low;
607
608                 return True;
609         } else {
610                 uint32 high = (*data64) >> 32, low = (*data64) & 0xFFFFFFFF;
611                 return prs_uint32(name, ps, depth+1, &low) && 
612                            prs_uint32(name, ps, depth+1, &high);
613         }
614 }
615
616 /*******************************************************************
617 return the length of a UNISTR string.
618 ********************************************************************/  
619
620 uint32 str_len_uni(UNISTR *source)
621 {
622         uint32 i=0;
623
624         if (!source->buffer)
625                 return 0;
626
627         while (source->buffer[i])
628                 i++;
629
630         return i;
631 }
632
633 /*******************************************************************
634  Verifies policy handle
635 ********************************************************************/
636
637 bool policy_handle_is_valid(const POLICY_HND *hnd)
638 {
639         POLICY_HND zero_pol;
640
641         ZERO_STRUCT(zero_pol);
642         return ((memcmp(&zero_pol, hnd, sizeof(POLICY_HND)) == 0) ? false : true );
643 }