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
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.
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.
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/>.
25 #include "librpc/gen_ndr/ndr_dcerpc.h"
28 #define DBGC_CLASS DBGC_RPC_PARSE
30 /*******************************************************************
31 Inits an RPC_HDR structure.
32 ********************************************************************/
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)
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 */
50 /*******************************************************************
51 Reads or writes an RPC_HDR structure.
52 ********************************************************************/
54 bool smb_io_rpc_hdr(const char *desc, RPC_HDR *rpc, prs_struct *ps, int depth)
59 prs_debug(ps, depth, desc, "smb_io_rpc_hdr");
62 if(!prs_uint8 ("major ", ps, depth, &rpc->major))
65 if(!prs_uint8 ("minor ", ps, depth, &rpc->minor))
67 if(!prs_uint8 ("pkt_type ", ps, depth, &rpc->pkt_type))
69 if(!prs_uint8 ("flags ", ps, depth, &rpc->flags))
72 /* We always marshall in little endian format. */
74 rpc->pack_type[0] = 0x10;
76 if(!prs_uint8("pack_type0", ps, depth, &rpc->pack_type[0]))
78 if(!prs_uint8("pack_type1", ps, depth, &rpc->pack_type[1]))
80 if(!prs_uint8("pack_type2", ps, depth, &rpc->pack_type[2]))
82 if(!prs_uint8("pack_type3", ps, depth, &rpc->pack_type[3]))
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.
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);
95 if(!prs_uint16("frag_len ", ps, depth, &rpc->frag_len))
97 if(!prs_uint16("auth_len ", ps, depth, &rpc->auth_len))
99 if(!prs_uint32("call_id ", ps, depth, &rpc->call_id))
104 /*******************************************************************
105 Reads or writes an struct ndr_syntax_id structure.
106 ********************************************************************/
108 static bool smb_io_rpc_iface(const char *desc, struct ndr_syntax_id *ifc,
109 prs_struct *ps, int depth)
114 prs_debug(ps, depth, desc, "smb_io_rpc_iface");
120 if (!smb_io_uuid( "uuid", &ifc->uuid, ps, depth))
123 if(!prs_uint32 ("version", ps, depth, &ifc->if_version))
129 /*******************************************************************
130 Inits an RPC_ADDR_STR structure.
131 ********************************************************************/
133 static void init_rpc_addr_str(RPC_ADDR_STR *str, const char *name)
135 str->len = strlen(name) + 1;
136 fstrcpy(str->str, name);
139 /*******************************************************************
140 Reads or writes an RPC_ADDR_STR structure.
141 ********************************************************************/
143 static bool smb_io_rpc_addr_str(const char *desc, RPC_ADDR_STR *str, prs_struct *ps, int depth)
148 prs_debug(ps, depth, desc, "smb_io_rpc_addr_str");
153 if(!prs_uint16 ( "len", ps, depth, &str->len))
155 if(!prs_uint8s (True, "str", ps, depth, (uchar*)str->str, MIN(str->len, sizeof(str->str)) ))
160 /*******************************************************************
161 Inits an RPC_HDR_BBA structure.
162 ********************************************************************/
164 static void init_rpc_hdr_bba(RPC_HDR_BBA *bba, uint16 max_tsize, uint16 max_rsize, uint32 assoc_gid)
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) */
171 /*******************************************************************
172 Reads or writes an RPC_HDR_BBA structure.
173 ********************************************************************/
175 static bool smb_io_rpc_hdr_bba(const char *desc, RPC_HDR_BBA *rpc, prs_struct *ps, int depth)
180 prs_debug(ps, depth, desc, "smb_io_rpc_hdr_bba");
183 if(!prs_uint16("max_tsize", ps, depth, &rpc->max_tsize))
185 if(!prs_uint16("max_rsize", ps, depth, &rpc->max_rsize))
187 if(!prs_uint32("assoc_gid", ps, depth, &rpc->assoc_gid))
192 /*******************************************************************
193 Reads or writes a struct dcerpc_ctx_list structure.
194 ********************************************************************/
196 bool smb_io_rpc_context(const char *desc, struct dcerpc_ctx_list *rpc_ctx, prs_struct *ps, int depth)
205 if(!prs_uint16("context_id ", ps, depth, &rpc_ctx->context_id ))
207 if(!prs_uint8 ("num_transfer_syntaxes", ps, depth, &rpc_ctx->num_transfer_syntaxes))
210 /* num_transfer_syntaxes must not be zero. */
211 if (rpc_ctx->num_transfer_syntaxes == 0)
214 if(!smb_io_rpc_iface("", &rpc_ctx->abstract_syntax, ps, depth))
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) {
226 for (i = 0; i < rpc_ctx->num_transfer_syntaxes; i++ ) {
227 if (!smb_io_rpc_iface("", &rpc_ctx->transfer_syntaxes[i], ps, depth))
233 NTSTATUS dcerpc_pull_dcerpc_bind(TALLOC_CTX *mem_ctx,
234 const DATA_BLOB *blob,
235 struct dcerpc_bind *r)
237 enum ndr_err_code ndr_err;
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);
248 /*******************************************************************
249 Reads or writes an RPC_HDR_RB structure.
250 ********************************************************************/
252 bool smb_io_rpc_hdr_rb(const char *desc, RPC_HDR_RB *rpc, prs_struct *ps, int depth)
259 prs_debug(ps, depth, desc, "smb_io_rpc_hdr_rb");
262 if(!smb_io_rpc_hdr_bba("", &rpc->bba, ps, depth))
265 if(!prs_uint8("num_contexts", ps, depth, &rpc->num_contexts))
268 /* 3 pad bytes following - will be mopped up by the prs_align in smb_io_rpc_context(). */
270 /* num_contexts must not be zero. */
271 if (rpc->num_contexts == 0)
274 if (UNMARSHALLING(ps)) {
275 if (!(rpc->rpc_context = PRS_ALLOC_MEM(ps, struct dcerpc_ctx_list, rpc->num_contexts))) {
280 for (i = 0; i < rpc->num_contexts; i++ ) {
281 if (!smb_io_rpc_context("", &rpc->rpc_context[i], ps, depth))
288 /*******************************************************************
289 Inits an RPC_RESULTS structure.
291 lkclXXXX only one reason at the moment!
292 ********************************************************************/
294 static void init_rpc_results(RPC_RESULTS *res,
295 uint8 num_results, uint16 result, uint16 reason)
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) */
302 /*******************************************************************
303 Reads or writes an RPC_RESULTS structure.
305 lkclXXXX only one reason at the moment!
306 ********************************************************************/
308 static bool smb_io_rpc_results(const char *desc, RPC_RESULTS *res, prs_struct *ps, int depth)
313 prs_debug(ps, depth, desc, "smb_io_rpc_results");
319 if(!prs_uint8 ("num_results", ps, depth, &res->num_results))
325 if(!prs_uint16("result ", ps, depth, &res->result))
327 if(!prs_uint16("reason ", ps, depth, &res->reason))
332 /*******************************************************************
333 Init an RPC_HDR_BA structure.
335 lkclXXXX only one reason at the moment!
337 ********************************************************************/
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)
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);
349 /* the transfer syntax from the request */
350 memcpy(&rpc->transfer, transfer, sizeof(rpc->transfer));
353 /*******************************************************************
354 Reads or writes an RPC_HDR_BA structure.
355 ********************************************************************/
357 bool smb_io_rpc_hdr_ba(const char *desc, RPC_HDR_BA *rpc, prs_struct *ps, int depth)
362 prs_debug(ps, depth, desc, "smb_io_rpc_hdr_ba");
365 if(!smb_io_rpc_hdr_bba("", &rpc->bba, ps, depth))
367 if(!smb_io_rpc_addr_str("", &rpc->addr, ps, depth))
369 if(!smb_io_rpc_results("", &rpc->res, ps, depth))
371 if(!smb_io_rpc_iface("", &rpc->transfer, ps, depth))
376 /*******************************************************************
377 Init an RPC_HDR_REQ structure.
378 ********************************************************************/
380 void init_rpc_hdr_req(RPC_HDR_REQ *hdr, uint32 alloc_hint, uint16 opnum)
382 hdr->alloc_hint = alloc_hint; /* allocation hint */
383 hdr->context_id = 0; /* presentation context identifier */
384 hdr->opnum = opnum; /* opnum */
387 /*******************************************************************
388 Reads or writes an RPC_HDR_REQ structure.
389 ********************************************************************/
391 bool smb_io_rpc_hdr_req(const char *desc, RPC_HDR_REQ *rpc, prs_struct *ps, int depth)
396 prs_debug(ps, depth, desc, "smb_io_rpc_hdr_req");
399 if(!prs_uint32("alloc_hint", ps, depth, &rpc->alloc_hint))
401 if(!prs_uint16("context_id", ps, depth, &rpc->context_id))
403 if(!prs_uint16("opnum ", ps, depth, &rpc->opnum))
408 /*******************************************************************
409 Reads or writes an RPC_HDR_RESP structure.
410 ********************************************************************/
412 bool smb_io_rpc_hdr_resp(const char *desc, RPC_HDR_RESP *rpc, prs_struct *ps, int depth)
417 prs_debug(ps, depth, desc, "smb_io_rpc_hdr_resp");
420 if(!prs_uint32("alloc_hint", ps, depth, &rpc->alloc_hint))
422 if(!prs_uint16("context_id", ps, depth, &rpc->context_id))
424 if(!prs_uint8 ("cancel_ct ", ps, depth, &rpc->cancel_count))
426 if(!prs_uint8 ("reserved ", ps, depth, &rpc->reserved))
431 /*******************************************************************
432 Inits an RPC_HDR_AUTH structure.
433 ********************************************************************/
435 void init_rpc_hdr_auth(RPC_HDR_AUTH *rai,
436 uint8 auth_type, uint8 auth_level,
438 uint32 auth_context_id)
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;
447 /*******************************************************************
448 Reads or writes an RPC_HDR_AUTH structure.
449 NB This writes UNALIGNED. Ensure you're correctly aligned before
451 ********************************************************************/
453 bool smb_io_rpc_hdr_auth(const char *desc, RPC_HDR_AUTH *rai, prs_struct *ps, int depth)
458 prs_debug(ps, depth, desc, "smb_io_rpc_hdr_auth");
461 if(!prs_uint8 ("auth_type ", ps, depth, &rai->auth_type))
463 if(!prs_uint8 ("auth_level ", ps, depth, &rai->auth_level))
465 if(!prs_uint8 ("auth_pad_len ", ps, depth, &rai->auth_pad_len))
467 if(!prs_uint8 ("auth_reserved", ps, depth, &rai->auth_reserved))
469 if(!prs_uint32("auth_context_id", ps, depth, &rai->auth_context_id))