s3:rpc user idl define dcerpc_ctx_list instead of custom RPC_CONTEXT
[Samba/vl.git] / source3 / rpc_parse / parse_rpc.c
blob74246c5b4effaeebbab1fac65eba6727a913ed41
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.
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.
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/>.
23 #include "includes.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_PARSE
28 /*******************************************************************
29 Inits an RPC_HDR structure.
30 ********************************************************************/
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)
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 */
48 /*******************************************************************
49 Reads or writes an RPC_HDR structure.
50 ********************************************************************/
52 bool smb_io_rpc_hdr(const char *desc, RPC_HDR *rpc, prs_struct *ps, int depth)
54 if (rpc == NULL)
55 return False;
57 prs_debug(ps, depth, desc, "smb_io_rpc_hdr");
58 depth++;
60 if(!prs_uint8 ("major ", ps, depth, &rpc->major))
61 return False;
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;
70 /* We always marshall in little endian format. */
71 if (MARSHALLING(ps))
72 rpc->pack_type[0] = 0x10;
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;
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.
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);
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;
102 /*******************************************************************
103 Reads or writes an struct ndr_syntax_id structure.
104 ********************************************************************/
106 static bool smb_io_rpc_iface(const char *desc, struct ndr_syntax_id *ifc,
107 prs_struct *ps, int depth)
109 if (ifc == NULL)
110 return False;
112 prs_debug(ps, depth, desc, "smb_io_rpc_iface");
113 depth++;
115 if (!prs_align(ps))
116 return False;
118 if (!smb_io_uuid( "uuid", &ifc->uuid, ps, depth))
119 return False;
121 if(!prs_uint32 ("version", ps, depth, &ifc->if_version))
122 return False;
124 return True;
127 /*******************************************************************
128 Inits an RPC_ADDR_STR structure.
129 ********************************************************************/
131 static void init_rpc_addr_str(RPC_ADDR_STR *str, const char *name)
133 str->len = strlen(name) + 1;
134 fstrcpy(str->str, name);
137 /*******************************************************************
138 Reads or writes an RPC_ADDR_STR structure.
139 ********************************************************************/
141 static bool smb_io_rpc_addr_str(const char *desc, RPC_ADDR_STR *str, prs_struct *ps, int depth)
143 if (str == NULL)
144 return False;
146 prs_debug(ps, depth, desc, "smb_io_rpc_addr_str");
147 depth++;
148 if(!prs_align(ps))
149 return False;
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;
158 /*******************************************************************
159 Inits an RPC_HDR_BBA structure.
160 ********************************************************************/
162 static void init_rpc_hdr_bba(RPC_HDR_BBA *bba, uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid)
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) */
169 /*******************************************************************
170 Reads or writes an RPC_HDR_BBA structure.
171 ********************************************************************/
173 static bool smb_io_rpc_hdr_bba(const char *desc, RPC_HDR_BBA *rpc, prs_struct *ps, int depth)
175 if (rpc == NULL)
176 return False;
178 prs_debug(ps, depth, desc, "smb_io_rpc_hdr_bba");
179 depth++;
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;
190 /*******************************************************************
191 Reads or writes a struct dcerpc_ctx_list structure.
192 ********************************************************************/
194 bool smb_io_rpc_context(const char *desc, struct dcerpc_ctx_list *rpc_ctx, prs_struct *ps, int depth)
196 int i;
198 if (rpc_ctx == NULL)
199 return False;
201 if(!prs_align(ps))
202 return False;
203 if(!prs_uint16("context_id ", ps, depth, &rpc_ctx->context_id ))
204 return False;
205 if(!prs_uint8 ("num_transfer_syntaxes", ps, depth, &rpc_ctx->num_transfer_syntaxes))
206 return False;
208 /* num_transfer_syntaxes must not be zero. */
209 if (rpc_ctx->num_transfer_syntaxes == 0)
210 return False;
212 if(!smb_io_rpc_iface("", &rpc_ctx->abstract_syntax, ps, depth))
213 return False;
215 if (UNMARSHALLING(ps)) {
216 rpc_ctx->transfer_syntaxes =
217 PRS_ALLOC_MEM(ps, struct ndr_syntax_id,
218 rpc_ctx->num_transfer_syntaxes);
219 if (!rpc_ctx->transfer_syntaxes) {
220 return False;
224 for (i = 0; i < rpc_ctx->num_transfer_syntaxes; i++ ) {
225 if (!smb_io_rpc_iface("", &rpc_ctx->transfer_syntaxes[i], ps, depth))
226 return False;
228 return True;
231 /*******************************************************************
232 Reads or writes an RPC_HDR_RB structure.
233 ********************************************************************/
235 bool smb_io_rpc_hdr_rb(const char *desc, RPC_HDR_RB *rpc, prs_struct *ps, int depth)
237 int i;
239 if (rpc == NULL)
240 return False;
242 prs_debug(ps, depth, desc, "smb_io_rpc_hdr_rb");
243 depth++;
245 if(!smb_io_rpc_hdr_bba("", &rpc->bba, ps, depth))
246 return False;
248 if(!prs_uint8("num_contexts", ps, depth, &rpc->num_contexts))
249 return False;
251 /* 3 pad bytes following - will be mopped up by the prs_align in smb_io_rpc_context(). */
253 /* num_contexts must not be zero. */
254 if (rpc->num_contexts == 0)
255 return False;
257 if (UNMARSHALLING(ps)) {
258 if (!(rpc->rpc_context = PRS_ALLOC_MEM(ps, struct dcerpc_ctx_list, rpc->num_contexts))) {
259 return False;
263 for (i = 0; i < rpc->num_contexts; i++ ) {
264 if (!smb_io_rpc_context("", &rpc->rpc_context[i], ps, depth))
265 return False;
268 return True;
271 /*******************************************************************
272 Inits an RPC_RESULTS structure.
274 lkclXXXX only one reason at the moment!
275 ********************************************************************/
277 static void init_rpc_results(RPC_RESULTS *res,
278 uint8 num_results, uint16 result, uint16 reason)
280 res->num_results = num_results; /* the number of results (0x01) */
281 res->result = result ; /* result (0x00 = accept) */
282 res->reason = reason ; /* reason (0x00 = no reason specified) */
285 /*******************************************************************
286 Reads or writes an RPC_RESULTS structure.
288 lkclXXXX only one reason at the moment!
289 ********************************************************************/
291 static bool smb_io_rpc_results(const char *desc, RPC_RESULTS *res, prs_struct *ps, int depth)
293 if (res == NULL)
294 return False;
296 prs_debug(ps, depth, desc, "smb_io_rpc_results");
297 depth++;
299 if(!prs_align(ps))
300 return False;
302 if(!prs_uint8 ("num_results", ps, depth, &res->num_results))
303 return False;
305 if(!prs_align(ps))
306 return False;
308 if(!prs_uint16("result ", ps, depth, &res->result))
309 return False;
310 if(!prs_uint16("reason ", ps, depth, &res->reason))
311 return False;
312 return True;
315 /*******************************************************************
316 Init an RPC_HDR_BA structure.
318 lkclXXXX only one reason at the moment!
320 ********************************************************************/
322 void init_rpc_hdr_ba(RPC_HDR_BA *rpc,
323 uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid,
324 const char *pipe_addr,
325 uint8 num_results, uint16 result, uint16 reason,
326 const struct ndr_syntax_id *transfer)
328 init_rpc_hdr_bba (&rpc->bba, max_tsize, max_rsize, assoc_gid);
329 init_rpc_addr_str(&rpc->addr, pipe_addr);
330 init_rpc_results (&rpc->res, num_results, result, reason);
332 /* the transfer syntax from the request */
333 memcpy(&rpc->transfer, transfer, sizeof(rpc->transfer));
336 /*******************************************************************
337 Reads or writes an RPC_HDR_BA structure.
338 ********************************************************************/
340 bool smb_io_rpc_hdr_ba(const char *desc, RPC_HDR_BA *rpc, prs_struct *ps, int depth)
342 if (rpc == NULL)
343 return False;
345 prs_debug(ps, depth, desc, "smb_io_rpc_hdr_ba");
346 depth++;
348 if(!smb_io_rpc_hdr_bba("", &rpc->bba, ps, depth))
349 return False;
350 if(!smb_io_rpc_addr_str("", &rpc->addr, ps, depth))
351 return False;
352 if(!smb_io_rpc_results("", &rpc->res, ps, depth))
353 return False;
354 if(!smb_io_rpc_iface("", &rpc->transfer, ps, depth))
355 return False;
356 return True;
359 /*******************************************************************
360 Init an RPC_HDR_REQ structure.
361 ********************************************************************/
363 void init_rpc_hdr_req(RPC_HDR_REQ *hdr, uint32 alloc_hint, uint16 opnum)
365 hdr->alloc_hint = alloc_hint; /* allocation hint */
366 hdr->context_id = 0; /* presentation context identifier */
367 hdr->opnum = opnum; /* opnum */
370 /*******************************************************************
371 Reads or writes an RPC_HDR_REQ structure.
372 ********************************************************************/
374 bool smb_io_rpc_hdr_req(const char *desc, RPC_HDR_REQ *rpc, prs_struct *ps, int depth)
376 if (rpc == NULL)
377 return False;
379 prs_debug(ps, depth, desc, "smb_io_rpc_hdr_req");
380 depth++;
382 if(!prs_uint32("alloc_hint", ps, depth, &rpc->alloc_hint))
383 return False;
384 if(!prs_uint16("context_id", ps, depth, &rpc->context_id))
385 return False;
386 if(!prs_uint16("opnum ", ps, depth, &rpc->opnum))
387 return False;
388 return True;
391 /*******************************************************************
392 Reads or writes an RPC_HDR_RESP structure.
393 ********************************************************************/
395 bool smb_io_rpc_hdr_resp(const char *desc, RPC_HDR_RESP *rpc, prs_struct *ps, int depth)
397 if (rpc == NULL)
398 return False;
400 prs_debug(ps, depth, desc, "smb_io_rpc_hdr_resp");
401 depth++;
403 if(!prs_uint32("alloc_hint", ps, depth, &rpc->alloc_hint))
404 return False;
405 if(!prs_uint16("context_id", ps, depth, &rpc->context_id))
406 return False;
407 if(!prs_uint8 ("cancel_ct ", ps, depth, &rpc->cancel_count))
408 return False;
409 if(!prs_uint8 ("reserved ", ps, depth, &rpc->reserved))
410 return False;
411 return True;
414 /*******************************************************************
415 Inits an RPC_HDR_AUTH structure.
416 ********************************************************************/
418 void init_rpc_hdr_auth(RPC_HDR_AUTH *rai,
419 uint8 auth_type, uint8 auth_level,
420 uint8 auth_pad_len,
421 uint32 auth_context_id)
423 rai->auth_type = auth_type;
424 rai->auth_level = auth_level;
425 rai->auth_pad_len = auth_pad_len;
426 rai->auth_reserved = 0;
427 rai->auth_context_id = auth_context_id;
430 /*******************************************************************
431 Reads or writes an RPC_HDR_AUTH structure.
432 NB This writes UNALIGNED. Ensure you're correctly aligned before
433 calling.
434 ********************************************************************/
436 bool smb_io_rpc_hdr_auth(const char *desc, RPC_HDR_AUTH *rai, prs_struct *ps, int depth)
438 if (rai == NULL)
439 return False;
441 prs_debug(ps, depth, desc, "smb_io_rpc_hdr_auth");
442 depth++;
444 if(!prs_uint8 ("auth_type ", ps, depth, &rai->auth_type))
445 return False;
446 if(!prs_uint8 ("auth_level ", ps, depth, &rai->auth_level))
447 return False;
448 if(!prs_uint8 ("auth_pad_len ", ps, depth, &rai->auth_pad_len))
449 return False;
450 if(!prs_uint8 ("auth_reserved", ps, depth, &rai->auth_reserved))
451 return False;
452 if(!prs_uint32("auth_context_id", ps, depth, &rai->auth_context_id))
453 return False;
455 return True;