Refactoring: Change calling conventions for cli_rpc_pipe_open_noauth
[kai/samba.git] / source3 / rpc_parse / parse_rpc.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) Jeremy Allison                    1999.
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  Inits an RPC_HDR structure.
30 ********************************************************************/
31
32 void init_rpc_hdr(RPC_HDR *hdr, enum RPC_PKT_TYPE pkt_type, uint8 flags,
33                                 uint32 call_id, int data_len, int auth_len)
34 {
35         hdr->major        = 5;               /* RPC version 5 */
36         hdr->minor        = 0;               /* minor version 0 */
37         hdr->pkt_type     = pkt_type;        /* RPC packet type */
38         hdr->flags        = flags;           /* dce/rpc flags */
39         hdr->pack_type[0] = 0x10;            /* little-endian data representation */
40         hdr->pack_type[1] = 0;               /* packed data representation */
41         hdr->pack_type[2] = 0;               /* packed data representation */
42         hdr->pack_type[3] = 0;               /* packed data representation */
43         hdr->frag_len     = data_len;        /* fragment length, fill in later */
44         hdr->auth_len     = auth_len;        /* authentication length */
45         hdr->call_id      = call_id;         /* call identifier - match incoming RPC */
46 }
47
48 /*******************************************************************
49  Reads or writes an RPC_HDR structure.
50 ********************************************************************/
51
52 bool smb_io_rpc_hdr(const char *desc,  RPC_HDR *rpc, prs_struct *ps, int depth)
53 {
54         if (rpc == NULL)
55                 return False;
56
57         prs_debug(ps, depth, desc, "smb_io_rpc_hdr");
58         depth++;
59
60         if(!prs_uint8 ("major     ", ps, depth, &rpc->major))
61                 return False;
62
63         if(!prs_uint8 ("minor     ", ps, depth, &rpc->minor))
64                 return False;
65         if(!prs_uint8 ("pkt_type  ", ps, depth, &rpc->pkt_type))
66                 return False;
67         if(!prs_uint8 ("flags     ", ps, depth, &rpc->flags))
68                 return False;
69
70         /* We always marshall in little endian format. */
71         if (MARSHALLING(ps))
72                 rpc->pack_type[0] = 0x10;
73
74         if(!prs_uint8("pack_type0", ps, depth, &rpc->pack_type[0]))
75                 return False;
76         if(!prs_uint8("pack_type1", ps, depth, &rpc->pack_type[1]))
77                 return False;
78         if(!prs_uint8("pack_type2", ps, depth, &rpc->pack_type[2]))
79                 return False;
80         if(!prs_uint8("pack_type3", ps, depth, &rpc->pack_type[3]))
81                 return False;
82
83         /*
84          * If reading and pack_type[0] == 0 then the data is in big-endian
85          * format. Set the flag in the prs_struct to specify reverse-endainness.
86          */
87
88         if (UNMARSHALLING(ps) && rpc->pack_type[0] == 0) {
89                 DEBUG(10,("smb_io_rpc_hdr: PDU data format is big-endian. Setting flag.\n"));
90                 prs_set_endian_data(ps, RPC_BIG_ENDIAN);
91         }
92
93         if(!prs_uint16("frag_len  ", ps, depth, &rpc->frag_len))
94                 return False;
95         if(!prs_uint16("auth_len  ", ps, depth, &rpc->auth_len))
96                 return False;
97         if(!prs_uint32("call_id   ", ps, depth, &rpc->call_id))
98                 return False;
99         return True;
100 }
101
102 /*******************************************************************
103  Reads or writes an RPC_IFACE structure.
104 ********************************************************************/
105
106 static bool smb_io_rpc_iface(const char *desc, RPC_IFACE *ifc, prs_struct *ps, int depth)
107 {
108         if (ifc == NULL)
109                 return False;
110
111         prs_debug(ps, depth, desc, "smb_io_rpc_iface");
112         depth++;
113
114         if (!prs_align(ps))
115                 return False;
116
117         if (!smb_io_uuid(  "uuid", &ifc->uuid, ps, depth))
118                 return False;
119
120         if(!prs_uint32 ("version", ps, depth, &ifc->if_version))
121                 return False;
122
123         return True;
124 }
125
126 /*******************************************************************
127  Inits an RPC_ADDR_STR structure.
128 ********************************************************************/
129
130 static void init_rpc_addr_str(RPC_ADDR_STR *str, const char *name)
131 {
132         str->len = strlen(name) + 1;
133         fstrcpy(str->str, name);
134 }
135
136 /*******************************************************************
137  Reads or writes an RPC_ADDR_STR structure.
138 ********************************************************************/
139
140 static bool smb_io_rpc_addr_str(const char *desc,  RPC_ADDR_STR *str, prs_struct *ps, int depth)
141 {
142         if (str == NULL)
143                 return False;
144
145         prs_debug(ps, depth, desc, "smb_io_rpc_addr_str");
146         depth++;
147         if(!prs_align(ps))
148                 return False;
149
150         if(!prs_uint16 (      "len", ps, depth, &str->len))
151                 return False;
152         if(!prs_uint8s (True, "str", ps, depth, (uchar*)str->str, MIN(str->len, sizeof(str->str)) ))
153                 return False;
154         return True;
155 }
156
157 /*******************************************************************
158  Inits an RPC_HDR_BBA structure.
159 ********************************************************************/
160
161 static void init_rpc_hdr_bba(RPC_HDR_BBA *bba, uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid)
162 {
163         bba->max_tsize = max_tsize; /* maximum transmission fragment size (0x1630) */
164         bba->max_rsize = max_rsize; /* max receive fragment size (0x1630) */   
165         bba->assoc_gid = assoc_gid; /* associated group id (0x0) */ 
166 }
167
168 /*******************************************************************
169  Reads or writes an RPC_HDR_BBA structure.
170 ********************************************************************/
171
172 static bool smb_io_rpc_hdr_bba(const char *desc,  RPC_HDR_BBA *rpc, prs_struct *ps, int depth)
173 {
174         if (rpc == NULL)
175                 return False;
176
177         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_bba");
178         depth++;
179
180         if(!prs_uint16("max_tsize", ps, depth, &rpc->max_tsize))
181                 return False;
182         if(!prs_uint16("max_rsize", ps, depth, &rpc->max_rsize))
183                 return False;
184         if(!prs_uint32("assoc_gid", ps, depth, &rpc->assoc_gid))
185                 return False;
186         return True;
187 }
188
189 /*******************************************************************
190  Inits an RPC_CONTEXT structure.
191  Note the transfer pointer must remain valid until this is marshalled.
192 ********************************************************************/
193
194 void init_rpc_context(RPC_CONTEXT *rpc_ctx, uint16 context_id,
195                       const RPC_IFACE *abstract, const RPC_IFACE *transfer)
196 {
197         rpc_ctx->context_id   = context_id   ; /* presentation context identifier (0x0) */
198         rpc_ctx->num_transfer_syntaxes = 1 ; /* the number of syntaxes (has always been 1?)(0x1) */
199
200         /* num and vers. of interface client is using */
201         rpc_ctx->abstract = *abstract;
202
203         /* vers. of interface to use for replies */
204         rpc_ctx->transfer = CONST_DISCARD(RPC_IFACE *, transfer);
205 }
206
207 /*******************************************************************
208  Inits an RPC_HDR_RB structure.
209  Note the context pointer must remain valid until this is marshalled.
210 ********************************************************************/
211
212 void init_rpc_hdr_rb(RPC_HDR_RB *rpc, 
213                                 uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid,
214                                 RPC_CONTEXT *context)
215 {
216         init_rpc_hdr_bba(&rpc->bba, max_tsize, max_rsize, assoc_gid);
217
218         rpc->num_contexts = 1;
219         rpc->rpc_context = context;
220 }
221
222 /*******************************************************************
223  Reads or writes an RPC_CONTEXT structure.
224 ********************************************************************/
225
226 bool smb_io_rpc_context(const char *desc, RPC_CONTEXT *rpc_ctx, prs_struct *ps, int depth)
227 {
228         int i;
229
230         if (rpc_ctx == NULL)
231                 return False;
232
233         if(!prs_align(ps))
234                 return False;
235         if(!prs_uint16("context_id  ", ps, depth, &rpc_ctx->context_id ))
236                 return False;
237         if(!prs_uint8 ("num_transfer_syntaxes", ps, depth, &rpc_ctx->num_transfer_syntaxes))
238                 return False;
239
240         /* num_transfer_syntaxes must not be zero. */
241         if (rpc_ctx->num_transfer_syntaxes == 0)
242                 return False;
243
244         if(!smb_io_rpc_iface("", &rpc_ctx->abstract, ps, depth))
245                 return False;
246
247         if (UNMARSHALLING(ps)) {
248                 if (!(rpc_ctx->transfer = PRS_ALLOC_MEM(ps, RPC_IFACE, rpc_ctx->num_transfer_syntaxes))) {
249                         return False;
250                 }
251         }
252
253         for (i = 0; i < rpc_ctx->num_transfer_syntaxes; i++ ) {
254                 if (!smb_io_rpc_iface("", &rpc_ctx->transfer[i], ps, depth))
255                         return False;
256         }
257         return True;
258
259
260 /*******************************************************************
261  Reads or writes an RPC_HDR_RB structure.
262 ********************************************************************/
263
264 bool smb_io_rpc_hdr_rb(const char *desc, RPC_HDR_RB *rpc, prs_struct *ps, int depth)
265 {
266         int i;
267         
268         if (rpc == NULL)
269                 return False;
270
271         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_rb");
272         depth++;
273
274         if(!smb_io_rpc_hdr_bba("", &rpc->bba, ps, depth))
275                 return False;
276
277         if(!prs_uint8("num_contexts", ps, depth, &rpc->num_contexts))
278                 return False;
279
280         /* 3 pad bytes following - will be mopped up by the prs_align in smb_io_rpc_context(). */
281
282         /* num_contexts must not be zero. */
283         if (rpc->num_contexts == 0)
284                 return False;
285
286         if (UNMARSHALLING(ps)) {
287                 if (!(rpc->rpc_context = PRS_ALLOC_MEM(ps, RPC_CONTEXT, rpc->num_contexts))) {
288                         return False;
289                 }
290         }
291
292         for (i = 0; i < rpc->num_contexts; i++ ) {
293                 if (!smb_io_rpc_context("", &rpc->rpc_context[i], ps, depth))
294                         return False;
295         }
296
297         return True;
298 }
299
300 /*******************************************************************
301  Inits an RPC_RESULTS structure.
302
303  lkclXXXX only one reason at the moment!
304 ********************************************************************/
305
306 static void init_rpc_results(RPC_RESULTS *res, 
307                                 uint8 num_results, uint16 result, uint16 reason)
308 {
309         res->num_results = num_results; /* the number of results (0x01) */
310         res->result      = result     ;  /* result (0x00 = accept) */
311         res->reason      = reason     ;  /* reason (0x00 = no reason specified) */
312 }
313
314 /*******************************************************************
315  Reads or writes an RPC_RESULTS structure.
316
317  lkclXXXX only one reason at the moment!
318 ********************************************************************/
319
320 static bool smb_io_rpc_results(const char *desc, RPC_RESULTS *res, prs_struct *ps, int depth)
321 {
322         if (res == NULL)
323                 return False;
324
325         prs_debug(ps, depth, desc, "smb_io_rpc_results");
326         depth++;
327
328         if(!prs_align(ps))
329                 return False;
330         
331         if(!prs_uint8 ("num_results", ps, depth, &res->num_results))    
332                 return False;
333
334         if(!prs_align(ps))
335                 return False;
336         
337         if(!prs_uint16("result     ", ps, depth, &res->result))
338                 return False;
339         if(!prs_uint16("reason     ", ps, depth, &res->reason))
340                 return False;
341         return True;
342 }
343
344 /*******************************************************************
345  Init an RPC_HDR_BA structure.
346
347  lkclXXXX only one reason at the moment!
348
349 ********************************************************************/
350
351 void init_rpc_hdr_ba(RPC_HDR_BA *rpc, 
352                                 uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid,
353                                 const char *pipe_addr,
354                                 uint8 num_results, uint16 result, uint16 reason,
355                                 RPC_IFACE *transfer)
356 {
357         init_rpc_hdr_bba (&rpc->bba, max_tsize, max_rsize, assoc_gid);
358         init_rpc_addr_str(&rpc->addr, pipe_addr);
359         init_rpc_results (&rpc->res, num_results, result, reason);
360
361         /* the transfer syntax from the request */
362         memcpy(&rpc->transfer, transfer, sizeof(rpc->transfer));
363 }
364
365 /*******************************************************************
366  Reads or writes an RPC_HDR_BA structure.
367 ********************************************************************/
368
369 bool smb_io_rpc_hdr_ba(const char *desc, RPC_HDR_BA *rpc, prs_struct *ps, int depth)
370 {
371         if (rpc == NULL)
372                 return False;
373
374         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_ba");
375         depth++;
376
377         if(!smb_io_rpc_hdr_bba("", &rpc->bba, ps, depth))
378                 return False;
379         if(!smb_io_rpc_addr_str("", &rpc->addr, ps, depth))
380                 return False;
381         if(!smb_io_rpc_results("", &rpc->res, ps, depth))
382                 return False;
383         if(!smb_io_rpc_iface("", &rpc->transfer, ps, depth))
384                 return False;
385         return True;
386 }
387
388 /*******************************************************************
389  Init an RPC_HDR_REQ structure.
390 ********************************************************************/
391
392 void init_rpc_hdr_req(RPC_HDR_REQ *hdr, uint32 alloc_hint, uint16 opnum)
393 {
394         hdr->alloc_hint   = alloc_hint; /* allocation hint */
395         hdr->context_id   = 0;         /* presentation context identifier */
396         hdr->opnum        = opnum;     /* opnum */
397 }
398
399 /*******************************************************************
400  Reads or writes an RPC_HDR_REQ structure.
401 ********************************************************************/
402
403 bool smb_io_rpc_hdr_req(const char *desc, RPC_HDR_REQ *rpc, prs_struct *ps, int depth)
404 {
405         if (rpc == NULL)
406                 return False;
407
408         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_req");
409         depth++;
410
411         if(!prs_uint32("alloc_hint", ps, depth, &rpc->alloc_hint))
412                 return False;
413         if(!prs_uint16("context_id", ps, depth, &rpc->context_id))
414                 return False;
415         if(!prs_uint16("opnum     ", ps, depth, &rpc->opnum))
416                 return False;
417         return True;
418 }
419
420 /*******************************************************************
421  Reads or writes an RPC_HDR_RESP structure.
422 ********************************************************************/
423
424 bool smb_io_rpc_hdr_resp(const char *desc, RPC_HDR_RESP *rpc, prs_struct *ps, int depth)
425 {
426         if (rpc == NULL)
427                 return False;
428
429         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_resp");
430         depth++;
431
432         if(!prs_uint32("alloc_hint", ps, depth, &rpc->alloc_hint))
433                 return False;
434         if(!prs_uint16("context_id", ps, depth, &rpc->context_id))
435                 return False;
436         if(!prs_uint8 ("cancel_ct ", ps, depth, &rpc->cancel_count))
437                 return False;
438         if(!prs_uint8 ("reserved  ", ps, depth, &rpc->reserved))
439                 return False;
440         return True;
441 }
442
443 /*******************************************************************
444  Reads or writes an RPC_HDR_FAULT structure.
445 ********************************************************************/
446
447 bool smb_io_rpc_hdr_fault(const char *desc, RPC_HDR_FAULT *rpc, prs_struct *ps, int depth)
448 {
449         if (rpc == NULL)
450                 return False;
451
452         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_fault");
453         depth++;
454
455         if(!prs_dcerpc_status("status  ", ps, depth, &rpc->status))
456                 return False;
457         if(!prs_uint32("reserved", ps, depth, &rpc->reserved))
458                 return False;
459
460     return True;
461 }
462
463 /*******************************************************************
464  Inits an RPC_HDR_AUTH structure.
465 ********************************************************************/
466
467 void init_rpc_hdr_auth(RPC_HDR_AUTH *rai,
468                                 uint8 auth_type, uint8 auth_level,
469                                 uint8 auth_pad_len,
470                                 uint32 auth_context_id)
471 {
472         rai->auth_type     = auth_type;
473         rai->auth_level    = auth_level;
474         rai->auth_pad_len  = auth_pad_len;
475         rai->auth_reserved = 0;
476         rai->auth_context_id = auth_context_id;
477 }
478
479 /*******************************************************************
480  Reads or writes an RPC_HDR_AUTH structure.
481 ********************************************************************/
482
483 bool smb_io_rpc_hdr_auth(const char *desc, RPC_HDR_AUTH *rai, prs_struct *ps, int depth)
484 {
485         if (rai == NULL)
486                 return False;
487
488         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_auth");
489         depth++;
490
491         if(!prs_align(ps))
492                 return False;
493
494         if(!prs_uint8 ("auth_type    ", ps, depth, &rai->auth_type))
495                 return False;
496         if(!prs_uint8 ("auth_level   ", ps, depth, &rai->auth_level))
497                 return False;
498         if(!prs_uint8 ("auth_pad_len ", ps, depth, &rai->auth_pad_len))
499                 return False;
500         if(!prs_uint8 ("auth_reserved", ps, depth, &rai->auth_reserved))
501                 return False;
502         if(!prs_uint32("auth_context_id", ps, depth, &rai->auth_context_id))
503                 return False;
504
505         return True;
506 }
507
508 /*******************************************************************
509  Checks an RPC_AUTH_VERIFIER structure.
510 ********************************************************************/
511
512 bool rpc_auth_verifier_chk(RPC_AUTH_VERIFIER *rav,
513                                 const char *signature, uint32 msg_type)
514 {
515         return (strequal(rav->signature, signature) && rav->msg_type == msg_type);
516 }
517
518 /*******************************************************************
519  Inits an RPC_AUTH_VERIFIER structure.
520 ********************************************************************/
521
522 void init_rpc_auth_verifier(RPC_AUTH_VERIFIER *rav,
523                                 const char *signature, uint32 msg_type)
524 {
525         fstrcpy(rav->signature, signature); /* "NTLMSSP" */
526         rav->msg_type = msg_type; /* NTLMSSP_MESSAGE_TYPE */
527 }
528
529 /*******************************************************************
530  Reads or writes an RPC_AUTH_VERIFIER structure.
531 ********************************************************************/
532
533 bool smb_io_rpc_auth_verifier(const char *desc, RPC_AUTH_VERIFIER *rav, prs_struct *ps, int depth)
534 {
535         if (rav == NULL)
536                 return False;
537
538         prs_debug(ps, depth, desc, "smb_io_rpc_auth_verifier");
539         depth++;
540
541         /* "NTLMSSP" */
542         if(!prs_string("signature", ps, depth, rav->signature,
543                         sizeof(rav->signature)))
544                 return False;
545         if(!prs_uint32("msg_type ", ps, depth, &rav->msg_type)) /* NTLMSSP_MESSAGE_TYPE */
546                 return False;
547
548         return True;
549 }
550
551 /*******************************************************************
552  This parses an RPC_AUTH_VERIFIER for schannel. I think
553 ********************************************************************/
554
555 bool smb_io_rpc_schannel_verifier(const char *desc, RPC_AUTH_VERIFIER *rav, prs_struct *ps, int depth)
556 {
557         if (rav == NULL)
558                 return False;
559
560         prs_debug(ps, depth, desc, "smb_io_rpc_schannel_verifier");
561         depth++;
562
563         if(!prs_string("signature", ps, depth, rav->signature, sizeof(rav->signature)))
564                 return False;
565         if(!prs_uint32("msg_type ", ps, depth, &rav->msg_type))
566                 return False;
567
568         return True;
569 }
570
571 /*******************************************************************
572 creates an RPC_AUTH_SCHANNEL_NEG structure.
573 ********************************************************************/
574
575 void init_rpc_auth_schannel_neg(RPC_AUTH_SCHANNEL_NEG *neg,
576                               const char *domain, const char *myname)
577 {
578         neg->type1 = 0;
579         neg->type2 = 0x3;
580         fstrcpy(neg->domain, domain);
581         fstrcpy(neg->myname, myname);
582 }
583
584 /*******************************************************************
585  Reads or writes an RPC_AUTH_SCHANNEL_NEG structure.
586 ********************************************************************/
587
588 bool smb_io_rpc_auth_schannel_neg(const char *desc, RPC_AUTH_SCHANNEL_NEG *neg,
589                                 prs_struct *ps, int depth)
590 {
591         if (neg == NULL)
592                 return False;
593
594         prs_debug(ps, depth, desc, "smb_io_rpc_auth_schannel_neg");
595         depth++;
596
597         if(!prs_align(ps))
598                 return False;
599
600         if(!prs_uint32("type1", ps, depth, &neg->type1))
601                 return False;
602         if(!prs_uint32("type2", ps, depth, &neg->type2))
603                 return False;
604         if(!prs_string("domain  ", ps, depth, neg->domain, sizeof(neg->domain)))
605                 return False;
606         if(!prs_string("myname  ", ps, depth, neg->myname, sizeof(neg->myname)))
607                 return False;
608
609         return True;
610 }
611
612 /*******************************************************************
613 reads or writes an RPC_AUTH_SCHANNEL_CHK structure.
614 ********************************************************************/
615
616 bool smb_io_rpc_auth_schannel_chk(const char *desc, int auth_len, 
617                                 RPC_AUTH_SCHANNEL_CHK * chk,
618                                 prs_struct *ps, int depth)
619 {
620         if (chk == NULL)
621                 return False;
622
623         prs_debug(ps, depth, desc, "smb_io_rpc_auth_schannel_chk");
624         depth++;
625
626         if ( !prs_uint8s(False, "sig  ", ps, depth, chk->sig, sizeof(chk->sig)) )
627                 return False;
628                 
629         if ( !prs_uint8s(False, "seq_num", ps, depth, chk->seq_num, sizeof(chk->seq_num)) )
630                 return False;
631                 
632         if ( !prs_uint8s(False, "packet_digest", ps, depth, chk->packet_digest, sizeof(chk->packet_digest)) )
633                 return False;
634         
635         if ( auth_len == RPC_AUTH_SCHANNEL_SIGN_OR_SEAL_CHK_LEN ) {
636                 if ( !prs_uint8s(False, "confounder", ps, depth, chk->confounder, sizeof(chk->confounder)) )
637                         return False;
638         }
639
640         return True;
641 }
642
643 const struct ndr_syntax_id syntax_spoolss = {
644         {
645                 0x12345678, 0x1234, 0xabcd,
646                 { 0xef, 0x00 },
647                 { 0x01, 0x23,
648                   0x45, 0x67, 0x89, 0xab }
649         }, 0x01
650 };
651