s3:dcerpc Add prs_parse_dcerpc_bind
[kamenim/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  *  Copyright (C) Simo Sorce                        2010
9  *  
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 3 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #include "includes.h"
25 #include "librpc/gen_ndr/ndr_dcerpc.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_RPC_PARSE
29
30 /*******************************************************************
31  Inits an RPC_HDR structure.
32 ********************************************************************/
33
34 void init_rpc_hdr(RPC_HDR *hdr, enum dcerpc_pkt_type pkt_type, uint8 flags,
35                                 uint32 call_id, int data_len, int auth_len)
36 {
37         hdr->major        = 5;               /* RPC version 5 */
38         hdr->minor        = 0;               /* minor version 0 */
39         hdr->pkt_type     = pkt_type;        /* RPC packet type */
40         hdr->flags        = flags;           /* dce/rpc flags */
41         hdr->pack_type[0] = 0x10;            /* little-endian data representation */
42         hdr->pack_type[1] = 0;               /* packed data representation */
43         hdr->pack_type[2] = 0;               /* packed data representation */
44         hdr->pack_type[3] = 0;               /* packed data representation */
45         hdr->frag_len     = data_len;        /* fragment length, fill in later */
46         hdr->auth_len     = auth_len;        /* authentication length */
47         hdr->call_id      = call_id;         /* call identifier - match incoming RPC */
48 }
49
50 /*******************************************************************
51  Reads or writes an RPC_HDR structure.
52 ********************************************************************/
53
54 bool smb_io_rpc_hdr(const char *desc,  RPC_HDR *rpc, prs_struct *ps, int depth)
55 {
56         if (rpc == NULL)
57                 return False;
58
59         prs_debug(ps, depth, desc, "smb_io_rpc_hdr");
60         depth++;
61
62         if(!prs_uint8 ("major     ", ps, depth, &rpc->major))
63                 return False;
64
65         if(!prs_uint8 ("minor     ", ps, depth, &rpc->minor))
66                 return False;
67         if(!prs_uint8 ("pkt_type  ", ps, depth, &rpc->pkt_type))
68                 return False;
69         if(!prs_uint8 ("flags     ", ps, depth, &rpc->flags))
70                 return False;
71
72         /* We always marshall in little endian format. */
73         if (MARSHALLING(ps))
74                 rpc->pack_type[0] = 0x10;
75
76         if(!prs_uint8("pack_type0", ps, depth, &rpc->pack_type[0]))
77                 return False;
78         if(!prs_uint8("pack_type1", ps, depth, &rpc->pack_type[1]))
79                 return False;
80         if(!prs_uint8("pack_type2", ps, depth, &rpc->pack_type[2]))
81                 return False;
82         if(!prs_uint8("pack_type3", ps, depth, &rpc->pack_type[3]))
83                 return False;
84
85         /*
86          * If reading and pack_type[0] == 0 then the data is in big-endian
87          * format. Set the flag in the prs_struct to specify reverse-endainness.
88          */
89
90         if (UNMARSHALLING(ps) && rpc->pack_type[0] == 0) {
91                 DEBUG(10,("smb_io_rpc_hdr: PDU data format is big-endian. Setting flag.\n"));
92                 prs_set_endian_data(ps, RPC_BIG_ENDIAN);
93         }
94
95         if(!prs_uint16("frag_len  ", ps, depth, &rpc->frag_len))
96                 return False;
97         if(!prs_uint16("auth_len  ", ps, depth, &rpc->auth_len))
98                 return False;
99         if(!prs_uint32("call_id   ", ps, depth, &rpc->call_id))
100                 return False;
101         return True;
102 }
103
104 /*******************************************************************
105  Reads or writes an struct ndr_syntax_id structure.
106 ********************************************************************/
107
108 static bool smb_io_rpc_iface(const char *desc, struct ndr_syntax_id *ifc,
109                              prs_struct *ps, int depth)
110 {
111         if (ifc == NULL)
112                 return False;
113
114         prs_debug(ps, depth, desc, "smb_io_rpc_iface");
115         depth++;
116
117         if (!prs_align(ps))
118                 return False;
119
120         if (!smb_io_uuid(  "uuid", &ifc->uuid, ps, depth))
121                 return False;
122
123         if(!prs_uint32 ("version", ps, depth, &ifc->if_version))
124                 return False;
125
126         return True;
127 }
128
129 /*******************************************************************
130  Inits an RPC_ADDR_STR structure.
131 ********************************************************************/
132
133 static void init_rpc_addr_str(RPC_ADDR_STR *str, const char *name)
134 {
135         str->len = strlen(name) + 1;
136         fstrcpy(str->str, name);
137 }
138
139 /*******************************************************************
140  Reads or writes an RPC_ADDR_STR structure.
141 ********************************************************************/
142
143 static bool smb_io_rpc_addr_str(const char *desc,  RPC_ADDR_STR *str, prs_struct *ps, int depth)
144 {
145         if (str == NULL)
146                 return False;
147
148         prs_debug(ps, depth, desc, "smb_io_rpc_addr_str");
149         depth++;
150         if(!prs_align(ps))
151                 return False;
152
153         if(!prs_uint16 (      "len", ps, depth, &str->len))
154                 return False;
155         if(!prs_uint8s (True, "str", ps, depth, (uchar*)str->str, MIN(str->len, sizeof(str->str)) ))
156                 return False;
157         return True;
158 }
159
160 /*******************************************************************
161  Inits an RPC_HDR_BBA structure.
162 ********************************************************************/
163
164 static void init_rpc_hdr_bba(RPC_HDR_BBA *bba, uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid)
165 {
166         bba->max_tsize = max_tsize; /* maximum transmission fragment size (0x1630) */
167         bba->max_rsize = max_rsize; /* max receive fragment size (0x1630) */   
168         bba->assoc_gid = assoc_gid; /* associated group id (0x0) */ 
169 }
170
171 /*******************************************************************
172  Reads or writes an RPC_HDR_BBA structure.
173 ********************************************************************/
174
175 static bool smb_io_rpc_hdr_bba(const char *desc,  RPC_HDR_BBA *rpc, prs_struct *ps, int depth)
176 {
177         if (rpc == NULL)
178                 return False;
179
180         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_bba");
181         depth++;
182
183         if(!prs_uint16("max_tsize", ps, depth, &rpc->max_tsize))
184                 return False;
185         if(!prs_uint16("max_rsize", ps, depth, &rpc->max_rsize))
186                 return False;
187         if(!prs_uint32("assoc_gid", ps, depth, &rpc->assoc_gid))
188                 return False;
189         return True;
190 }
191
192 /*******************************************************************
193  Reads or writes a struct dcerpc_ctx_list structure.
194 ********************************************************************/
195
196 bool smb_io_rpc_context(const char *desc, struct dcerpc_ctx_list *rpc_ctx, prs_struct *ps, int depth)
197 {
198         int i;
199
200         if (rpc_ctx == NULL)
201                 return False;
202
203         if(!prs_align(ps))
204                 return False;
205         if(!prs_uint16("context_id  ", ps, depth, &rpc_ctx->context_id ))
206                 return False;
207         if(!prs_uint8 ("num_transfer_syntaxes", ps, depth, &rpc_ctx->num_transfer_syntaxes))
208                 return False;
209
210         /* num_transfer_syntaxes must not be zero. */
211         if (rpc_ctx->num_transfer_syntaxes == 0)
212                 return False;
213
214         if(!smb_io_rpc_iface("", &rpc_ctx->abstract_syntax, ps, depth))
215                 return False;
216
217         if (UNMARSHALLING(ps)) {
218                 rpc_ctx->transfer_syntaxes =
219                         PRS_ALLOC_MEM(ps, struct ndr_syntax_id,
220                                         rpc_ctx->num_transfer_syntaxes);
221                 if (!rpc_ctx->transfer_syntaxes) {
222                         return False;
223                 }
224         }
225
226         for (i = 0; i < rpc_ctx->num_transfer_syntaxes; i++ ) {
227                 if (!smb_io_rpc_iface("", &rpc_ctx->transfer_syntaxes[i], ps, depth))
228                         return False;
229         }
230         return True;
231
232
233 NTSTATUS dcerpc_pull_dcerpc_bind(TALLOC_CTX *mem_ctx,
234                                  const DATA_BLOB *blob,
235                                  struct dcerpc_bind *r)
236 {
237         enum ndr_err_code ndr_err;
238
239         ndr_err = ndr_pull_struct_blob(blob, mem_ctx, r,
240                 (ndr_pull_flags_fn_t)ndr_pull_dcerpc_bind);
241         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
242                 return ndr_map_error2ntstatus(ndr_err);
243         }
244
245         return NT_STATUS_OK;
246 }
247
248 /*******************************************************************
249  Reads or writes an RPC_HDR_RB structure.
250 ********************************************************************/
251
252 bool smb_io_rpc_hdr_rb(const char *desc, RPC_HDR_RB *rpc, prs_struct *ps, int depth)
253 {
254         int i;
255         
256         if (rpc == NULL)
257                 return False;
258
259         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_rb");
260         depth++;
261
262         if(!smb_io_rpc_hdr_bba("", &rpc->bba, ps, depth))
263                 return False;
264
265         if(!prs_uint8("num_contexts", ps, depth, &rpc->num_contexts))
266                 return False;
267
268         /* 3 pad bytes following - will be mopped up by the prs_align in smb_io_rpc_context(). */
269
270         /* num_contexts must not be zero. */
271         if (rpc->num_contexts == 0)
272                 return False;
273
274         if (UNMARSHALLING(ps)) {
275                 if (!(rpc->rpc_context = PRS_ALLOC_MEM(ps, struct dcerpc_ctx_list, rpc->num_contexts))) {
276                         return False;
277                 }
278         }
279
280         for (i = 0; i < rpc->num_contexts; i++ ) {
281                 if (!smb_io_rpc_context("", &rpc->rpc_context[i], ps, depth))
282                         return False;
283         }
284
285         return True;
286 }
287
288 /*******************************************************************
289  Inits an RPC_RESULTS structure.
290
291  lkclXXXX only one reason at the moment!
292 ********************************************************************/
293
294 static void init_rpc_results(RPC_RESULTS *res, 
295                                 uint8 num_results, uint16 result, uint16 reason)
296 {
297         res->num_results = num_results; /* the number of results (0x01) */
298         res->result      = result     ;  /* result (0x00 = accept) */
299         res->reason      = reason     ;  /* reason (0x00 = no reason specified) */
300 }
301
302 /*******************************************************************
303  Reads or writes an RPC_RESULTS structure.
304
305  lkclXXXX only one reason at the moment!
306 ********************************************************************/
307
308 static bool smb_io_rpc_results(const char *desc, RPC_RESULTS *res, prs_struct *ps, int depth)
309 {
310         if (res == NULL)
311                 return False;
312
313         prs_debug(ps, depth, desc, "smb_io_rpc_results");
314         depth++;
315
316         if(!prs_align(ps))
317                 return False;
318         
319         if(!prs_uint8 ("num_results", ps, depth, &res->num_results))    
320                 return False;
321
322         if(!prs_align(ps))
323                 return False;
324         
325         if(!prs_uint16("result     ", ps, depth, &res->result))
326                 return False;
327         if(!prs_uint16("reason     ", ps, depth, &res->reason))
328                 return False;
329         return True;
330 }
331
332 /*******************************************************************
333  Init an RPC_HDR_BA structure.
334
335  lkclXXXX only one reason at the moment!
336
337 ********************************************************************/
338
339 void init_rpc_hdr_ba(RPC_HDR_BA *rpc, 
340                                 uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid,
341                                 const char *pipe_addr,
342                                 uint8 num_results, uint16 result, uint16 reason,
343                                 const struct ndr_syntax_id *transfer)
344 {
345         init_rpc_hdr_bba (&rpc->bba, max_tsize, max_rsize, assoc_gid);
346         init_rpc_addr_str(&rpc->addr, pipe_addr);
347         init_rpc_results (&rpc->res, num_results, result, reason);
348
349         /* the transfer syntax from the request */
350         memcpy(&rpc->transfer, transfer, sizeof(rpc->transfer));
351 }
352
353 /*******************************************************************
354  Reads or writes an RPC_HDR_BA structure.
355 ********************************************************************/
356
357 bool smb_io_rpc_hdr_ba(const char *desc, RPC_HDR_BA *rpc, prs_struct *ps, int depth)
358 {
359         if (rpc == NULL)
360                 return False;
361
362         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_ba");
363         depth++;
364
365         if(!smb_io_rpc_hdr_bba("", &rpc->bba, ps, depth))
366                 return False;
367         if(!smb_io_rpc_addr_str("", &rpc->addr, ps, depth))
368                 return False;
369         if(!smb_io_rpc_results("", &rpc->res, ps, depth))
370                 return False;
371         if(!smb_io_rpc_iface("", &rpc->transfer, ps, depth))
372                 return False;
373         return True;
374 }
375
376 /*******************************************************************
377  Init an RPC_HDR_REQ structure.
378 ********************************************************************/
379
380 void init_rpc_hdr_req(RPC_HDR_REQ *hdr, uint32 alloc_hint, uint16 opnum)
381 {
382         hdr->alloc_hint   = alloc_hint; /* allocation hint */
383         hdr->context_id   = 0;         /* presentation context identifier */
384         hdr->opnum        = opnum;     /* opnum */
385 }
386
387 /*******************************************************************
388  Reads or writes an RPC_HDR_REQ structure.
389 ********************************************************************/
390
391 bool smb_io_rpc_hdr_req(const char *desc, RPC_HDR_REQ *rpc, prs_struct *ps, int depth)
392 {
393         if (rpc == NULL)
394                 return False;
395
396         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_req");
397         depth++;
398
399         if(!prs_uint32("alloc_hint", ps, depth, &rpc->alloc_hint))
400                 return False;
401         if(!prs_uint16("context_id", ps, depth, &rpc->context_id))
402                 return False;
403         if(!prs_uint16("opnum     ", ps, depth, &rpc->opnum))
404                 return False;
405         return True;
406 }
407
408 /*******************************************************************
409  Reads or writes an RPC_HDR_RESP structure.
410 ********************************************************************/
411
412 bool smb_io_rpc_hdr_resp(const char *desc, RPC_HDR_RESP *rpc, prs_struct *ps, int depth)
413 {
414         if (rpc == NULL)
415                 return False;
416
417         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_resp");
418         depth++;
419
420         if(!prs_uint32("alloc_hint", ps, depth, &rpc->alloc_hint))
421                 return False;
422         if(!prs_uint16("context_id", ps, depth, &rpc->context_id))
423                 return False;
424         if(!prs_uint8 ("cancel_ct ", ps, depth, &rpc->cancel_count))
425                 return False;
426         if(!prs_uint8 ("reserved  ", ps, depth, &rpc->reserved))
427                 return False;
428         return True;
429 }
430
431 /*******************************************************************
432  Inits an RPC_HDR_AUTH structure.
433 ********************************************************************/
434
435 void init_rpc_hdr_auth(RPC_HDR_AUTH *rai,
436                                 uint8 auth_type, uint8 auth_level,
437                                 uint8 auth_pad_len,
438                                 uint32 auth_context_id)
439 {
440         rai->auth_type     = auth_type;
441         rai->auth_level    = auth_level;
442         rai->auth_pad_len  = auth_pad_len;
443         rai->auth_reserved = 0;
444         rai->auth_context_id = auth_context_id;
445 }
446
447 /*******************************************************************
448  Reads or writes an RPC_HDR_AUTH structure.
449  NB This writes UNALIGNED. Ensure you're correctly aligned before
450  calling.
451 ********************************************************************/
452
453 bool smb_io_rpc_hdr_auth(const char *desc, RPC_HDR_AUTH *rai, prs_struct *ps, int depth)
454 {
455         if (rai == NULL)
456                 return False;
457
458         prs_debug(ps, depth, desc, "smb_io_rpc_hdr_auth");
459         depth++;
460
461         if(!prs_uint8 ("auth_type    ", ps, depth, &rai->auth_type))
462                 return False;
463         if(!prs_uint8 ("auth_level   ", ps, depth, &rai->auth_level))
464                 return False;
465         if(!prs_uint8 ("auth_pad_len ", ps, depth, &rai->auth_pad_len))
466                 return False;
467         if(!prs_uint8 ("auth_reserved", ps, depth, &rai->auth_reserved))
468                 return False;
469         if(!prs_uint32("auth_context_id", ps, depth, &rai->auth_context_id))
470                 return False;
471
472         return True;
473 }