2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2010-2011
5 Copyright (C) Andrew Tridgell 2010-2011
6 Copyright (C) Simo Sorce 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef __DEFAULT_LIBRPC_RPCCOMMON_H__
23 #define __DEFAULT_LIBRPC_RPCCOMMON_H__
25 #include "gen_ndr/dcerpc.h"
26 #include "lib/util/attr.h"
28 struct dcerpc_binding_handle;
29 struct dcerpc_pipe_handle;
30 struct dcerpc_pipe_handle_connection;
32 struct ndr_interface_table;
33 struct ndr_interface_call;
39 struct tevent_context;
40 struct tstream_context;
41 struct gensec_security;
43 enum dcerpc_transport_t {
44 NCA_UNKNOWN, NCACN_NP, NCACN_IP_TCP, NCACN_IP_UDP, NCACN_VNS_IPC,
45 NCACN_VNS_SPP, NCACN_AT_DSP, NCADG_AT_DDP, NCALRPC, NCACN_UNIX_STREAM,
46 NCADG_UNIX_DGRAM, NCACN_HTTP, NCADG_IPX, NCACN_SPX, NCACN_INTERNAL };
48 /** this describes a binding to a particular transport/pipe */
49 struct dcerpc_binding;
51 /* dcerpc pipe flags */
52 #define DCERPC_DEBUG_PRINT_IN (1<<0)
53 #define DCERPC_DEBUG_PRINT_OUT (1<<1)
54 #define DCERPC_DEBUG_PRINT_BOTH (DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT)
56 #define DCERPC_DEBUG_VALIDATE_IN (1<<2)
57 #define DCERPC_DEBUG_VALIDATE_OUT (1<<3)
58 #define DCERPC_DEBUG_VALIDATE_BOTH (DCERPC_DEBUG_VALIDATE_IN | DCERPC_DEBUG_VALIDATE_OUT)
60 #define DCERPC_CONNECT (1<<4)
61 #define DCERPC_SIGN (1<<5)
62 #define DCERPC_SEAL (1<<6)
64 #define DCERPC_PUSH_BIGENDIAN (1<<7)
65 #define DCERPC_PULL_BIGENDIAN (1<<8)
67 #define DCERPC_SCHANNEL (1<<9)
69 #define DCERPC_ANON_FALLBACK (1<<10)
71 /* use a 128 bit session key */
72 #define DCERPC_SCHANNEL_128 (1<<12)
74 /* check incoming pad bytes */
75 #define DCERPC_DEBUG_PAD_CHECK (1<<13)
77 /* set LIBNDR_FLAG_REF_ALLOC flag when decoding NDR */
78 #define DCERPC_NDR_REF_ALLOC (1<<14)
80 #define DCERPC_AUTH_OPTIONS (DCERPC_SEAL|DCERPC_SIGN|DCERPC_SCHANNEL|DCERPC_AUTH_SPNEGO|DCERPC_AUTH_KRB5|DCERPC_AUTH_NTLM)
82 /* select spnego auth */
83 #define DCERPC_AUTH_SPNEGO (1<<15)
85 /* select krb5 auth */
86 #define DCERPC_AUTH_KRB5 (1<<16)
88 #define DCERPC_SMB2 (1<<17)
90 /* select NTLM auth */
91 #define DCERPC_AUTH_NTLM (1<<18)
93 /* this triggers the DCERPC_PFC_FLAG_CONC_MPX flag in the bind request */
94 #define DCERPC_CONCURRENT_MULTIPLEX (1<<19)
96 /* this indicates DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN flag was negotiated */
97 #define DCERPC_HEADER_SIGNING (1<<20)
99 /* use NDR64 transport */
100 #define DCERPC_NDR64 (1<<21)
102 /* handle upgrades or downgrades automatically */
103 #define DCERPC_SCHANNEL_AUTO (1<<23)
105 /* use aes schannel with hmac-sh256 session key */
106 #define DCERPC_SCHANNEL_AES (1<<24)
108 /* this triggers the DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN flag in the bind request */
109 #define DCERPC_PROPOSE_HEADER_SIGNING (1<<25)
111 #define DCERPC_PACKET (1<<26)
113 #define DCERPC_SMB1 (1<<27)
115 /* The following definitions come from ../librpc/rpc/dcerpc_error.c */
117 const char *dcerpc_errstr(TALLOC_CTX *mem_ctx, uint32_t fault_code);
118 NTSTATUS dcerpc_fault_to_nt_status(uint32_t fault_code);
119 uint32_t dcerpc_fault_from_nt_status(NTSTATUS nt_status);
121 /* The following definitions come from ../librpc/rpc/binding.c */
123 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor);
124 char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor);
125 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot);
126 struct dcerpc_binding *dcerpc_binding_dup(TALLOC_CTX *mem_ctx,
127 const struct dcerpc_binding *b);
128 NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
129 const struct dcerpc_binding *binding,
130 struct epm_tower *tower);
131 NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
132 struct epm_tower *tower,
133 struct dcerpc_binding **b_out);
134 NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding **b_out);
135 char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b);
136 struct GUID dcerpc_binding_get_object(const struct dcerpc_binding *b);
137 NTSTATUS dcerpc_binding_set_object(struct dcerpc_binding *b,
139 enum dcerpc_transport_t dcerpc_binding_get_transport(const struct dcerpc_binding *b);
140 NTSTATUS dcerpc_binding_set_transport(struct dcerpc_binding *b,
141 enum dcerpc_transport_t transport);
142 void dcerpc_binding_get_auth_info(const struct dcerpc_binding *b,
143 enum dcerpc_AuthType *_auth_type,
144 enum dcerpc_AuthLevel *_auth_level);
145 uint32_t dcerpc_binding_get_assoc_group_id(const struct dcerpc_binding *b);
146 NTSTATUS dcerpc_binding_set_assoc_group_id(struct dcerpc_binding *b,
147 uint32_t assoc_group_id);
148 struct ndr_syntax_id dcerpc_binding_get_abstract_syntax(const struct dcerpc_binding *b);
149 NTSTATUS dcerpc_binding_set_abstract_syntax(struct dcerpc_binding *b,
150 const struct ndr_syntax_id *syntax);
151 const char *dcerpc_binding_get_string_option(const struct dcerpc_binding *b,
153 char *dcerpc_binding_copy_string_option(TALLOC_CTX *mem_ctx,
154 const struct dcerpc_binding *b,
156 NTSTATUS dcerpc_binding_set_string_option(struct dcerpc_binding *b,
159 void *_dcerpc_binding_get_pointer_option(const struct dcerpc_binding *b,
162 #define dcerpc_binding_get_pointer_option(b, name, type) \
163 talloc_get_type(_dcerpc_binding_get_pointer_option(b, name, #type), type)
164 NTSTATUS _dcerpc_binding_set_pointer_option(struct dcerpc_binding *b,
168 #define dcerpc_binding_set_pointer_option(b, name, type, vp) \
169 _dcerpc_binding_set_pointer_option(b, name, #type, vp)
170 uint32_t dcerpc_binding_get_flags(const struct dcerpc_binding *b);
171 NTSTATUS dcerpc_binding_set_flags(struct dcerpc_binding *b,
174 NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor, struct ndr_syntax_id *syntax);
175 const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t);
176 enum dcerpc_transport_t dcerpc_transport_by_name(const char *name);
177 enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower);
179 /* The following definitions come from ../librpc/rpc/dcerpc_util.c */
181 void dcerpc_set_frag_length(DATA_BLOB *blob, uint16_t v);
182 uint16_t dcerpc_get_frag_length(const DATA_BLOB *blob);
183 void dcerpc_set_auth_length(DATA_BLOB *blob, uint16_t v);
184 uint16_t dcerpc_get_auth_length(const DATA_BLOB *blob);
185 uint8_t dcerpc_get_endian_flag(DATA_BLOB *blob);
186 uint8_t dcerpc_get_auth_type(const DATA_BLOB *blob);
187 uint8_t dcerpc_get_auth_level(const DATA_BLOB *blob);
188 uint32_t dcerpc_get_auth_context_id(const DATA_BLOB *blob);
189 const char *dcerpc_default_transport_endpoint(TALLOC_CTX *mem_ctx,
190 enum dcerpc_transport_t transport,
191 const struct ndr_interface_table *table);
193 NTSTATUS dcerpc_pull_ncacn_packet(TALLOC_CTX *mem_ctx,
194 const DATA_BLOB *blob,
195 struct ncacn_packet *r);
198 * @brief Pull a dcerpc_auth structure, taking account of any auth
199 * padding in the blob. For request/response packets we pass
200 * the whole data blob, so auth_data_only must be set to false
201 * as the blob contains data+pad+auth and no just pad+auth.
203 * @param pkt - The ncacn_packet strcuture
204 * @param mem_ctx - The mem_ctx used to allocate dcerpc_auth elements
205 * @param pkt_trailer - The packet trailer data, usually the trailing
206 * auth_info blob, but in the request/response case
207 * this is the stub_and_verifier blob.
208 * @param auth - A preallocated dcerpc_auth *empty* structure
209 * @param auth_length - The length of the auth trail, sum of auth header
210 * lenght and pkt->auth_length
211 * @param auth_data_only - Whether the pkt_trailer includes only the auth_blob
212 * (+ padding) or also other data.
214 * @return - A NTSTATUS error code.
216 NTSTATUS dcerpc_pull_auth_trailer(const struct ncacn_packet *pkt,
218 const DATA_BLOB *pkt_trailer,
219 struct dcerpc_auth *auth,
220 uint32_t *auth_length,
221 bool auth_data_only);
222 NTSTATUS dcerpc_verify_ncacn_packet_header(const struct ncacn_packet *pkt,
223 enum dcerpc_pkt_type ptype,
224 size_t max_auth_info,
225 uint8_t required_flags,
226 uint8_t optional_flags);
227 NTSTATUS dcerpc_ncacn_pull_pkt_auth(const struct dcerpc_auth *auth_state,
228 struct gensec_security *gensec,
230 enum dcerpc_pkt_type ptype,
231 uint8_t required_flags,
232 uint8_t optional_flags,
233 uint8_t payload_offset,
234 DATA_BLOB *payload_and_verifier,
235 DATA_BLOB *raw_packet,
236 const struct ncacn_packet *pkt);
237 NTSTATUS dcerpc_ncacn_push_pkt_auth(const struct dcerpc_auth *auth_state,
238 struct gensec_security *gensec,
240 DATA_BLOB *raw_packet,
242 uint8_t payload_offset,
243 const DATA_BLOB *payload,
244 const struct ncacn_packet *pkt);
245 struct tevent_req *dcerpc_read_ncacn_packet_send(TALLOC_CTX *mem_ctx,
246 struct tevent_context *ev,
247 struct tstream_context *stream);
248 NTSTATUS dcerpc_read_ncacn_packet_recv(struct tevent_req *req,
250 struct ncacn_packet **pkt,
253 /* The following definitions come from ../librpc/rpc/binding_handle.c */
255 struct dcerpc_binding_handle_ops {
258 bool (*is_connected)(struct dcerpc_binding_handle *h);
259 uint32_t (*set_timeout)(struct dcerpc_binding_handle *h,
262 void (*auth_info)(struct dcerpc_binding_handle *h,
263 enum dcerpc_AuthType *auth_type,
264 enum dcerpc_AuthLevel *auth_level);
266 struct tevent_req *(*raw_call_send)(TALLOC_CTX *mem_ctx,
267 struct tevent_context *ev,
268 struct dcerpc_binding_handle *h,
269 const struct GUID *object,
272 const uint8_t *in_data,
274 NTSTATUS (*raw_call_recv)(struct tevent_req *req,
278 uint32_t *out_flags);
280 struct tevent_req *(*disconnect_send)(TALLOC_CTX *mem_ctx,
281 struct tevent_context *ev,
282 struct dcerpc_binding_handle *h);
283 NTSTATUS (*disconnect_recv)(struct tevent_req *req);
285 /* TODO: remove the following functions */
286 bool (*push_bigendian)(struct dcerpc_binding_handle *h);
287 bool (*ref_alloc)(struct dcerpc_binding_handle *h);
288 bool (*use_ndr64)(struct dcerpc_binding_handle *h);
289 void (*do_ndr_print)(struct dcerpc_binding_handle *h,
291 const void *struct_ptr,
292 const struct ndr_interface_call *call);
293 void (*ndr_push_failed)(struct dcerpc_binding_handle *h,
295 const void *struct_ptr,
296 const struct ndr_interface_call *call);
297 void (*ndr_pull_failed)(struct dcerpc_binding_handle *h,
299 const DATA_BLOB *blob,
300 const struct ndr_interface_call *call);
301 NTSTATUS (*ndr_validate_in)(struct dcerpc_binding_handle *h,
303 const DATA_BLOB *blob,
304 const struct ndr_interface_call *call);
305 NTSTATUS (*ndr_validate_out)(struct dcerpc_binding_handle *h,
306 struct ndr_pull *pull_in,
307 const void *struct_ptr,
308 const struct ndr_interface_call *call);
311 struct dcerpc_binding_handle *_dcerpc_binding_handle_create(TALLOC_CTX *mem_ctx,
312 const struct dcerpc_binding_handle_ops *ops,
313 const struct GUID *object,
314 const struct ndr_interface_table *table,
318 const char *location);
319 #define dcerpc_binding_handle_create(mem_ctx, ops, object, table, \
320 state, type, location) \
321 _dcerpc_binding_handle_create(mem_ctx, ops, object, table, \
322 state, sizeof(type), #type, location)
324 void *_dcerpc_binding_handle_data(struct dcerpc_binding_handle *h);
325 #define dcerpc_binding_handle_data(_h, _type) \
326 talloc_get_type_abort(_dcerpc_binding_handle_data(_h), _type)
328 _DEPRECATED_ void dcerpc_binding_handle_set_sync_ev(struct dcerpc_binding_handle *h,
329 struct tevent_context *ev);
331 bool dcerpc_binding_handle_is_connected(struct dcerpc_binding_handle *h);
333 uint32_t dcerpc_binding_handle_set_timeout(struct dcerpc_binding_handle *h,
336 void dcerpc_binding_handle_auth_info(struct dcerpc_binding_handle *h,
337 enum dcerpc_AuthType *auth_type,
338 enum dcerpc_AuthLevel *auth_level);
340 struct tevent_req *dcerpc_binding_handle_raw_call_send(TALLOC_CTX *mem_ctx,
341 struct tevent_context *ev,
342 struct dcerpc_binding_handle *h,
343 const struct GUID *object,
346 const uint8_t *in_data,
348 NTSTATUS dcerpc_binding_handle_raw_call_recv(struct tevent_req *req,
352 uint32_t *out_flags);
353 NTSTATUS dcerpc_binding_handle_raw_call(struct dcerpc_binding_handle *h,
354 const struct GUID *object,
357 const uint8_t *in_data,
362 uint32_t *out_flags);
364 struct tevent_req *dcerpc_binding_handle_disconnect_send(TALLOC_CTX *mem_ctx,
365 struct tevent_context *ev,
366 struct dcerpc_binding_handle *h);
367 NTSTATUS dcerpc_binding_handle_disconnect_recv(struct tevent_req *req);
369 struct tevent_req *dcerpc_binding_handle_call_send(TALLOC_CTX *mem_ctx,
370 struct tevent_context *ev,
371 struct dcerpc_binding_handle *h,
372 const struct GUID *object,
373 const struct ndr_interface_table *table,
377 NTSTATUS dcerpc_binding_handle_call_recv(struct tevent_req *req);
378 NTSTATUS dcerpc_binding_handle_call(struct dcerpc_binding_handle *h,
379 const struct GUID *object,
380 const struct ndr_interface_table *table,
386 * Extract header information from a ncacn_packet
387 * as a dcerpc_sec_vt_header2 as used by the security verification trailer.
389 * @param[in] pkt a packet
391 * @return a dcerpc_sec_vt_header2
393 struct dcerpc_sec_vt_header2 dcerpc_sec_vt_header2_from_ncacn_packet(const struct ncacn_packet *pkt);
397 * Test if two dcerpc_sec_vt_header2 structures are equal
398 * without consideration of reserved fields.
400 * @param v1 a pointer to a dcerpc_sec_vt_header2 structure
401 * @param v2 a pointer to a dcerpc_sec_vt_header2 structure
403 * @retval true if *v1 equals *v2
405 bool dcerpc_sec_vt_header2_equal(const struct dcerpc_sec_vt_header2 *v1,
406 const struct dcerpc_sec_vt_header2 *v2);
409 * Check for consistency of the security verification trailer with the PDU header.
410 * See <a href="http://msdn.microsoft.com/en-us/library/cc243559.aspx">MS-RPCE 2.2.2.13</a>.
411 * A check with an empty trailer succeeds.
413 * @param[in] vt a pointer to the security verification trailer.
414 * @param[in] bitmask1 which flags were negotiated on the connection.
415 * @param[in] pcontext the syntaxes negotiatied for the presentation context.
416 * @param[in] header2 some fields from the PDU header.
418 * @retval true on success.
420 bool dcerpc_sec_verification_trailer_check(
421 const struct dcerpc_sec_verification_trailer *vt,
422 const uint32_t *bitmask1,
423 const struct dcerpc_sec_vt_pcontext *pcontext,
424 const struct dcerpc_sec_vt_header2 *header2);
427 * @brief check and optionally extract the Bind Time Features from
428 * the given ndr_syntax_id.
430 * <a href="http://msdn.microsoft.com/en-us/library/cc243715.aspx">MS-RPCE 3.3.1.5.3 Bind Time Feature Negotiation</a>.
432 * @param[in] s the syntax that should be checked.
434 * @param[out] features This is optional, it will be filled with the extracted
435 * features the on success, otherwise it's filled with 0.
437 * @return true if the syntax matches the 6CB71C2C-9812-4540 prefix with version 1, false otherwise.
439 * @see dcerpc_construct_bind_time_features
441 bool dcerpc_extract_bind_time_features(struct ndr_syntax_id syntax, uint64_t *features);
444 * @brief Construct a ndr_syntax_id used for Bind Time Features Negotiation.
446 * <a href="http://msdn.microsoft.com/en-us/library/cc243715.aspx">MS-RPCE 3.3.1.5.3 Bind Time Feature Negotiation</a>.
448 * @param[in] features The supported features.
450 * @return The ndr_syntax_id with the given features.
452 * @see dcerpc_extract_bind_time_features
454 struct ndr_syntax_id dcerpc_construct_bind_time_features(uint64_t features);
456 #define DCERPC_AUTH_PAD_LENGTH(stub_length) (\
457 (((stub_length) % DCERPC_AUTH_PAD_ALIGNMENT) > 0)?\
458 (DCERPC_AUTH_PAD_ALIGNMENT - (stub_length) % DCERPC_AUTH_PAD_ALIGNMENT):\
461 struct dcerpc_pipe_handle_connection *dcerpc_pipe_handle_connection_create(
463 const char *chunk_struct_name,
464 size_t chunk_struct_size);
465 bool _dcerpc_pipe_handle_connection_connect(
466 struct dcerpc_pipe_handle_connection *pc,
467 const char *chunk_struct_name,
468 size_t chunk_struct_size,
469 struct dcerpc_pipe_handle *p,
471 #define dcerpc_pipe_handle_connection_push_connect(pc, csn, css, p) \
472 _dcerpc_pipe_handle_connection_connect(pc, csn, css, p, true)
473 #define dcerpc_pipe_handle_connection_pull_connect(pc, csn, css, p) \
474 _dcerpc_pipe_handle_connection_connect(pc, csn, css, p, false)
475 void dcerpc_pipe_handle_connection_disconnect(
476 struct dcerpc_pipe_handle_connection *pc);
478 struct dcerpc_pipe_handle_ops {
481 struct tevent_req *(*chunk_push_send)(TALLOC_CTX *mem_ctx,
482 struct tevent_context *ev,
483 struct dcerpc_pipe_handle *p,
484 const void *chunk_ptr);
485 NTSTATUS (*chunk_push_recv)(struct tevent_req *req);
487 struct tevent_req *(*chunk_pull_send)(TALLOC_CTX *mem_ctx,
488 struct tevent_context *ev,
489 struct dcerpc_pipe_handle *p,
492 NTSTATUS (*chunk_pull_recv)(struct tevent_req *req);
495 struct dcerpc_pipe_handle *_dcerpc_pipe_handle_create(
497 const struct dcerpc_pipe_handle_ops *ops,
501 const char *location);
502 #define dcerpc_pipe_handle_create(mem_ctx, ops, \
504 _dcerpc_pipe_handle_create(mem_ctx, ops, \
505 state, sizeof(type), #type, __location__)
507 void *_dcerpc_pipe_handle_data(struct dcerpc_pipe_handle *p);
508 #define dcerpc_pipe_handle_data(_p, _type) \
509 talloc_get_type_abort(_dcerpc_pipe_handle_data(_p), _type)
511 struct tevent_req *dcerpc_pipe_handle_push_send(TALLOC_CTX *mem_ctx,
512 struct tevent_context *ev,
513 struct dcerpc_pipe_handle_connection *pc,
514 const void *chunk_ptr);
515 NTSTATUS dcerpc_pipe_handle_push_recv(struct tevent_req *req);
517 struct tevent_req *dcerpc_pipe_handle_pull_send(TALLOC_CTX *mem_ctx,
518 struct tevent_context *ev,
519 struct dcerpc_pipe_handle_connection *pc,
522 NTSTATUS dcerpc_pipe_handle_pull_recv(struct tevent_req *req);
524 #endif /* __DEFAULT_LIBRPC_RPCCOMMON_H__ */