Fix bug #7146 - Samba miss-parses authenticated RPC packets.
[abartlet/samba.git/.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 dcerpc_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 struct ndr_syntax_id structure.
104 ********************************************************************/
105
106 static bool smb_io_rpc_iface(const char *desc, struct ndr_syntax_id *ifc,
107                              prs_struct *ps, int depth)
108 {
109         if (ifc == NULL)
110                 return False;
111
112         prs_debug(ps, depth, desc, "smb_io_rpc_iface");
113         depth++;
114
115         if (!prs_align(ps))
116                 return False;
117
118         if (!smb_io_uuid(  "uuid", &ifc->uuid, ps, depth))
119                 return False;
120
121         if(!prs_uint32 ("version", ps, depth, &ifc->if_version))
122                 return False;
123
124         return True;
125 }
126
127 /*******************************************************************
128  Inits an RPC_ADDR_STR structure.
129 ********************************************************************/
130
131 static void init_rpc_addr_str(RPC_ADDR_STR *str, const char *name)
132 {
133         str->len = strlen(name) + 1;
134         fstrcpy(str->str, name);
135 }
136
137 /*******************************************************************
138  Reads or writes an RPC_ADDR_STR structure.
139 ********************************************************************/
140
141 static bool smb_io_rpc_addr_str(const char *desc,  RPC_ADDR_STR *str, prs_struct *ps, int depth)
142 {
143         if (str == NULL)
144                 return False;
145
146         prs_debug(ps, depth, desc, "smb_io_rpc_addr_str");
147         depth++;
148         if(!prs_align(ps))
149                 return False;
150
151         if(!prs_uint16 (      "len", ps, depth, &str->len))
152                 return False;
153         if(!prs_uint8s (True, "str", ps, depth, (uchar*)str->str, MIN(str->len, sizeof(str->str)) ))
154                 return False;
155         return True;
156 }
157
158 /*******************************************************************
159  Inits an RPC_HDR_BBA structure.
160 ********************************************************************/
161
162 static void init_rpc_hdr_bba(RPC_HDR_BBA *bba, uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid)
163 {
164         bba->max_tsize = max_tsize; /* maximum transmission fragment size (0x1630) */
165         bba->max_rsize = max_rsize; /* max receive fragment size (0x1630) */   
166         bba->assoc_gid = assoc_gid; /* associated group id (0x0) */ 
167 }
168
169 /*******************************************************************
170  Reads or writes an RPC_HDR_BBA structure.
171 ********************************************************************/
172
173 static bool smb_io_rpc_hdr_bba(const char *desc,  RPC_HDR_BBA *rpc, prs_struct *ps, int depth)
174 {
175         if (rpc == NULL)
176                 return False;
177
178         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_bba");
179         depth++;
180
181         if(!prs_uint16("max_tsize", ps, depth, &rpc->max_tsize))
182                 return False;
183         if(!prs_uint16("max_rsize", ps, depth, &rpc->max_rsize))
184                 return False;
185         if(!prs_uint32("assoc_gid", ps, depth, &rpc->assoc_gid))
186                 return False;
187         return True;
188 }
189
190 /*******************************************************************
191  Inits an RPC_CONTEXT structure.
192  Note the transfer pointer must remain valid until this is marshalled.
193 ********************************************************************/
194
195 void init_rpc_context(RPC_CONTEXT *rpc_ctx, uint16 context_id,
196                       const struct ndr_syntax_id *abstract,
197                       const struct ndr_syntax_id *transfer)
198 {
199         rpc_ctx->context_id   = context_id   ; /* presentation context identifier (0x0) */
200         rpc_ctx->num_transfer_syntaxes = 1 ; /* the number of syntaxes (has always been 1?)(0x1) */
201
202         /* num and vers. of interface client is using */
203         rpc_ctx->abstract = *abstract;
204
205         /* vers. of interface to use for replies */
206         rpc_ctx->transfer = CONST_DISCARD(struct ndr_syntax_id *, transfer);
207 }
208
209 /*******************************************************************
210  Inits an RPC_HDR_RB structure.
211  Note the context pointer must remain valid until this is marshalled.
212 ********************************************************************/
213
214 void init_rpc_hdr_rb(RPC_HDR_RB *rpc, 
215                                 uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid,
216                                 RPC_CONTEXT *context)
217 {
218         init_rpc_hdr_bba(&rpc->bba, max_tsize, max_rsize, assoc_gid);
219
220         rpc->num_contexts = 1;
221         rpc->rpc_context = context;
222 }
223
224 /*******************************************************************
225  Reads or writes an RPC_CONTEXT structure.
226 ********************************************************************/
227
228 bool smb_io_rpc_context(const char *desc, RPC_CONTEXT *rpc_ctx, prs_struct *ps, int depth)
229 {
230         int i;
231
232         if (rpc_ctx == NULL)
233                 return False;
234
235         if(!prs_align(ps))
236                 return False;
237         if(!prs_uint16("context_id  ", ps, depth, &rpc_ctx->context_id ))
238                 return False;
239         if(!prs_uint8 ("num_transfer_syntaxes", ps, depth, &rpc_ctx->num_transfer_syntaxes))
240                 return False;
241
242         /* num_transfer_syntaxes must not be zero. */
243         if (rpc_ctx->num_transfer_syntaxes == 0)
244                 return False;
245
246         if(!smb_io_rpc_iface("", &rpc_ctx->abstract, ps, depth))
247                 return False;
248
249         if (UNMARSHALLING(ps)) {
250                 if (!(rpc_ctx->transfer = PRS_ALLOC_MEM(ps, struct ndr_syntax_id, rpc_ctx->num_transfer_syntaxes))) {
251                         return False;
252                 }
253         }
254
255         for (i = 0; i < rpc_ctx->num_transfer_syntaxes; i++ ) {
256                 if (!smb_io_rpc_iface("", &rpc_ctx->transfer[i], ps, depth))
257                         return False;
258         }
259         return True;
260
261
262 /*******************************************************************
263  Reads or writes an RPC_HDR_RB structure.
264 ********************************************************************/
265
266 bool smb_io_rpc_hdr_rb(const char *desc, RPC_HDR_RB *rpc, prs_struct *ps, int depth)
267 {
268         int i;
269         
270         if (rpc == NULL)
271                 return False;
272
273         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_rb");
274         depth++;
275
276         if(!smb_io_rpc_hdr_bba("", &rpc->bba, ps, depth))
277                 return False;
278
279         if(!prs_uint8("num_contexts", ps, depth, &rpc->num_contexts))
280                 return False;
281
282         /* 3 pad bytes following - will be mopped up by the prs_align in smb_io_rpc_context(). */
283
284         /* num_contexts must not be zero. */
285         if (rpc->num_contexts == 0)
286                 return False;
287
288         if (UNMARSHALLING(ps)) {
289                 if (!(rpc->rpc_context = PRS_ALLOC_MEM(ps, RPC_CONTEXT, rpc->num_contexts))) {
290                         return False;
291                 }
292         }
293
294         for (i = 0; i < rpc->num_contexts; i++ ) {
295                 if (!smb_io_rpc_context("", &rpc->rpc_context[i], ps, depth))
296                         return False;
297         }
298
299         return True;
300 }
301
302 /*******************************************************************
303  Inits an RPC_RESULTS structure.
304
305  lkclXXXX only one reason at the moment!
306 ********************************************************************/
307
308 static void init_rpc_results(RPC_RESULTS *res, 
309                                 uint8 num_results, uint16 result, uint16 reason)
310 {
311         res->num_results = num_results; /* the number of results (0x01) */
312         res->result      = result     ;  /* result (0x00 = accept) */
313         res->reason      = reason     ;  /* reason (0x00 = no reason specified) */
314 }
315
316 /*******************************************************************
317  Reads or writes an RPC_RESULTS structure.
318
319  lkclXXXX only one reason at the moment!
320 ********************************************************************/
321
322 static bool smb_io_rpc_results(const char *desc, RPC_RESULTS *res, prs_struct *ps, int depth)
323 {
324         if (res == NULL)
325                 return False;
326
327         prs_debug(ps, depth, desc, "smb_io_rpc_results");
328         depth++;
329
330         if(!prs_align(ps))
331                 return False;
332         
333         if(!prs_uint8 ("num_results", ps, depth, &res->num_results))    
334                 return False;
335
336         if(!prs_align(ps))
337                 return False;
338         
339         if(!prs_uint16("result     ", ps, depth, &res->result))
340                 return False;
341         if(!prs_uint16("reason     ", ps, depth, &res->reason))
342                 return False;
343         return True;
344 }
345
346 /*******************************************************************
347  Init an RPC_HDR_BA structure.
348
349  lkclXXXX only one reason at the moment!
350
351 ********************************************************************/
352
353 void init_rpc_hdr_ba(RPC_HDR_BA *rpc, 
354                                 uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid,
355                                 const char *pipe_addr,
356                                 uint8 num_results, uint16 result, uint16 reason,
357                                 const struct ndr_syntax_id *transfer)
358 {
359         init_rpc_hdr_bba (&rpc->bba, max_tsize, max_rsize, assoc_gid);
360         init_rpc_addr_str(&rpc->addr, pipe_addr);
361         init_rpc_results (&rpc->res, num_results, result, reason);
362
363         /* the transfer syntax from the request */
364         memcpy(&rpc->transfer, transfer, sizeof(rpc->transfer));
365 }
366
367 /*******************************************************************
368  Reads or writes an RPC_HDR_BA structure.
369 ********************************************************************/
370
371 bool smb_io_rpc_hdr_ba(const char *desc, RPC_HDR_BA *rpc, prs_struct *ps, int depth)
372 {
373         if (rpc == NULL)
374                 return False;
375
376         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_ba");
377         depth++;
378
379         if(!smb_io_rpc_hdr_bba("", &rpc->bba, ps, depth))
380                 return False;
381         if(!smb_io_rpc_addr_str("", &rpc->addr, ps, depth))
382                 return False;
383         if(!smb_io_rpc_results("", &rpc->res, ps, depth))
384                 return False;
385         if(!smb_io_rpc_iface("", &rpc->transfer, ps, depth))
386                 return False;
387         return True;
388 }
389
390 /*******************************************************************
391  Init an RPC_HDR_REQ structure.
392 ********************************************************************/
393
394 void init_rpc_hdr_req(RPC_HDR_REQ *hdr, uint32 alloc_hint, uint16 opnum)
395 {
396         hdr->alloc_hint   = alloc_hint; /* allocation hint */
397         hdr->context_id   = 0;         /* presentation context identifier */
398         hdr->opnum        = opnum;     /* opnum */
399 }
400
401 /*******************************************************************
402  Reads or writes an RPC_HDR_REQ structure.
403 ********************************************************************/
404
405 bool smb_io_rpc_hdr_req(const char *desc, RPC_HDR_REQ *rpc, prs_struct *ps, int depth)
406 {
407         if (rpc == NULL)
408                 return False;
409
410         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_req");
411         depth++;
412
413         if(!prs_uint32("alloc_hint", ps, depth, &rpc->alloc_hint))
414                 return False;
415         if(!prs_uint16("context_id", ps, depth, &rpc->context_id))
416                 return False;
417         if(!prs_uint16("opnum     ", ps, depth, &rpc->opnum))
418                 return False;
419         return True;
420 }
421
422 /*******************************************************************
423  Reads or writes an RPC_HDR_RESP structure.
424 ********************************************************************/
425
426 bool smb_io_rpc_hdr_resp(const char *desc, RPC_HDR_RESP *rpc, prs_struct *ps, int depth)
427 {
428         if (rpc == NULL)
429                 return False;
430
431         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_resp");
432         depth++;
433
434         if(!prs_uint32("alloc_hint", ps, depth, &rpc->alloc_hint))
435                 return False;
436         if(!prs_uint16("context_id", ps, depth, &rpc->context_id))
437                 return False;
438         if(!prs_uint8 ("cancel_ct ", ps, depth, &rpc->cancel_count))
439                 return False;
440         if(!prs_uint8 ("reserved  ", ps, depth, &rpc->reserved))
441                 return False;
442         return True;
443 }
444
445 /*******************************************************************
446  Reads or writes an RPC_HDR_FAULT structure.
447 ********************************************************************/
448
449 bool smb_io_rpc_hdr_fault(const char *desc, RPC_HDR_FAULT *rpc, prs_struct *ps, int depth)
450 {
451         if (rpc == NULL)
452                 return False;
453
454         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_fault");
455         depth++;
456
457         if(!prs_dcerpc_status("status  ", ps, depth, &rpc->status))
458                 return False;
459         if(!prs_uint32("reserved", ps, depth, &rpc->reserved))
460                 return False;
461
462     return True;
463 }
464
465 /*******************************************************************
466  Inits an RPC_HDR_AUTH structure.
467 ********************************************************************/
468
469 void init_rpc_hdr_auth(RPC_HDR_AUTH *rai,
470                                 uint8 auth_type, uint8 auth_level,
471                                 uint8 auth_pad_len,
472                                 uint32 auth_context_id)
473 {
474         rai->auth_type     = auth_type;
475         rai->auth_level    = auth_level;
476         rai->auth_pad_len  = auth_pad_len;
477         rai->auth_reserved = 0;
478         rai->auth_context_id = auth_context_id;
479 }
480
481 /*******************************************************************
482  Reads or writes an RPC_HDR_AUTH structure.
483  NB This writes UNALIGNED. Ensure you're correctly aligned before
484  calling.
485 ********************************************************************/
486
487 bool smb_io_rpc_hdr_auth(const char *desc, RPC_HDR_AUTH *rai, prs_struct *ps, int depth)
488 {
489         if (rai == NULL)
490                 return False;
491
492         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_auth");
493         depth++;
494
495         if(!prs_uint8 ("auth_type    ", ps, depth, &rai->auth_type))
496                 return False;
497         if(!prs_uint8 ("auth_level   ", ps, depth, &rai->auth_level))
498                 return False;
499         if(!prs_uint8 ("auth_pad_len ", ps, depth, &rai->auth_pad_len))
500                 return False;
501         if(!prs_uint8 ("auth_reserved", ps, depth, &rai->auth_reserved))
502                 return False;
503         if(!prs_uint32("auth_context_id", ps, depth, &rai->auth_context_id))
504                 return False;
505
506         return True;
507 }