librpc: add binding handle support for [smb1]
[Samba.git] / librpc / rpc / rpc_common.h
blobcdc9fe8ac88262feaeec5d096ab7658b79418027
1 /*
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 GUID;
30 struct ndr_interface_table;
31 struct ndr_interface_call;
32 struct ndr_push;
33 struct ndr_pull;
34 struct ncacn_packet;
35 struct epm_floor;
36 struct epm_tower;
37 struct tevent_context;
38 struct tstream_context;
39 struct gensec_security;
41 enum dcerpc_transport_t {
42 NCA_UNKNOWN, NCACN_NP, NCACN_IP_TCP, NCACN_IP_UDP, NCACN_VNS_IPC,
43 NCACN_VNS_SPP, NCACN_AT_DSP, NCADG_AT_DDP, NCALRPC, NCACN_UNIX_STREAM,
44 NCADG_UNIX_DGRAM, NCACN_HTTP, NCADG_IPX, NCACN_SPX, NCACN_INTERNAL };
46 /** this describes a binding to a particular transport/pipe */
47 struct dcerpc_binding;
49 /* dcerpc pipe flags */
50 #define DCERPC_DEBUG_PRINT_IN (1<<0)
51 #define DCERPC_DEBUG_PRINT_OUT (1<<1)
52 #define DCERPC_DEBUG_PRINT_BOTH (DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT)
54 #define DCERPC_DEBUG_VALIDATE_IN (1<<2)
55 #define DCERPC_DEBUG_VALIDATE_OUT (1<<3)
56 #define DCERPC_DEBUG_VALIDATE_BOTH (DCERPC_DEBUG_VALIDATE_IN | DCERPC_DEBUG_VALIDATE_OUT)
58 #define DCERPC_CONNECT (1<<4)
59 #define DCERPC_SIGN (1<<5)
60 #define DCERPC_SEAL (1<<6)
62 #define DCERPC_PUSH_BIGENDIAN (1<<7)
63 #define DCERPC_PULL_BIGENDIAN (1<<8)
65 #define DCERPC_SCHANNEL (1<<9)
67 #define DCERPC_ANON_FALLBACK (1<<10)
69 /* use a 128 bit session key */
70 #define DCERPC_SCHANNEL_128 (1<<12)
72 /* check incoming pad bytes */
73 #define DCERPC_DEBUG_PAD_CHECK (1<<13)
75 /* set LIBNDR_FLAG_REF_ALLOC flag when decoding NDR */
76 #define DCERPC_NDR_REF_ALLOC (1<<14)
78 #define DCERPC_AUTH_OPTIONS (DCERPC_SEAL|DCERPC_SIGN|DCERPC_SCHANNEL|DCERPC_AUTH_SPNEGO|DCERPC_AUTH_KRB5|DCERPC_AUTH_NTLM)
80 /* select spnego auth */
81 #define DCERPC_AUTH_SPNEGO (1<<15)
83 /* select krb5 auth */
84 #define DCERPC_AUTH_KRB5 (1<<16)
86 #define DCERPC_SMB2 (1<<17)
88 /* select NTLM auth */
89 #define DCERPC_AUTH_NTLM (1<<18)
91 /* this triggers the DCERPC_PFC_FLAG_CONC_MPX flag in the bind request */
92 #define DCERPC_CONCURRENT_MULTIPLEX (1<<19)
94 /* this indicates DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN flag was negotiated */
95 #define DCERPC_HEADER_SIGNING (1<<20)
97 /* use NDR64 transport */
98 #define DCERPC_NDR64 (1<<21)
100 /* handle upgrades or downgrades automatically */
101 #define DCERPC_SCHANNEL_AUTO (1<<23)
103 /* use aes schannel with hmac-sh256 session key */
104 #define DCERPC_SCHANNEL_AES (1<<24)
106 /* this triggers the DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN flag in the bind request */
107 #define DCERPC_PROPOSE_HEADER_SIGNING (1<<25)
109 #define DCERPC_PACKET (1<<26)
111 #define DCERPC_SMB1 (1<<27)
113 /* The following definitions come from ../librpc/rpc/dcerpc_error.c */
115 const char *dcerpc_errstr(TALLOC_CTX *mem_ctx, uint32_t fault_code);
116 NTSTATUS dcerpc_fault_to_nt_status(uint32_t fault_code);
117 uint32_t dcerpc_fault_from_nt_status(NTSTATUS nt_status);
119 /* The following definitions come from ../librpc/rpc/binding.c */
121 const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor);
122 char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor);
123 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot);
124 struct dcerpc_binding *dcerpc_binding_dup(TALLOC_CTX *mem_ctx,
125 const struct dcerpc_binding *b);
126 NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
127 const struct dcerpc_binding *binding,
128 struct epm_tower *tower);
129 NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx,
130 struct epm_tower *tower,
131 struct dcerpc_binding **b_out);
132 NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding **b_out);
133 char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b);
134 struct GUID dcerpc_binding_get_object(const struct dcerpc_binding *b);
135 NTSTATUS dcerpc_binding_set_object(struct dcerpc_binding *b,
136 struct GUID object);
137 enum dcerpc_transport_t dcerpc_binding_get_transport(const struct dcerpc_binding *b);
138 NTSTATUS dcerpc_binding_set_transport(struct dcerpc_binding *b,
139 enum dcerpc_transport_t transport);
140 void dcerpc_binding_get_auth_info(const struct dcerpc_binding *b,
141 enum dcerpc_AuthType *_auth_type,
142 enum dcerpc_AuthLevel *_auth_level);
143 uint32_t dcerpc_binding_get_assoc_group_id(const struct dcerpc_binding *b);
144 NTSTATUS dcerpc_binding_set_assoc_group_id(struct dcerpc_binding *b,
145 uint32_t assoc_group_id);
146 struct ndr_syntax_id dcerpc_binding_get_abstract_syntax(const struct dcerpc_binding *b);
147 NTSTATUS dcerpc_binding_set_abstract_syntax(struct dcerpc_binding *b,
148 const struct ndr_syntax_id *syntax);
149 const char *dcerpc_binding_get_string_option(const struct dcerpc_binding *b,
150 const char *name);
151 char *dcerpc_binding_copy_string_option(TALLOC_CTX *mem_ctx,
152 const struct dcerpc_binding *b,
153 const char *name);
154 NTSTATUS dcerpc_binding_set_string_option(struct dcerpc_binding *b,
155 const char *name,
156 const char *value);
157 uint32_t dcerpc_binding_get_flags(const struct dcerpc_binding *b);
158 NTSTATUS dcerpc_binding_set_flags(struct dcerpc_binding *b,
159 uint32_t additional,
160 uint32_t clear);
161 NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor, struct ndr_syntax_id *syntax);
162 const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t);
163 enum dcerpc_transport_t dcerpc_transport_by_name(const char *name);
164 enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower);
166 /* The following definitions come from ../librpc/rpc/dcerpc_util.c */
168 void dcerpc_set_frag_length(DATA_BLOB *blob, uint16_t v);
169 uint16_t dcerpc_get_frag_length(const DATA_BLOB *blob);
170 void dcerpc_set_auth_length(DATA_BLOB *blob, uint16_t v);
171 uint8_t dcerpc_get_endian_flag(DATA_BLOB *blob);
172 const char *dcerpc_default_transport_endpoint(TALLOC_CTX *mem_ctx,
173 enum dcerpc_transport_t transport,
174 const struct ndr_interface_table *table);
176 NTSTATUS dcerpc_pull_ncacn_packet(TALLOC_CTX *mem_ctx,
177 const DATA_BLOB *blob,
178 struct ncacn_packet *r);
181 * @brief Pull a dcerpc_auth structure, taking account of any auth
182 * padding in the blob. For request/response packets we pass
183 * the whole data blob, so auth_data_only must be set to false
184 * as the blob contains data+pad+auth and no just pad+auth.
186 * @param pkt - The ncacn_packet strcuture
187 * @param mem_ctx - The mem_ctx used to allocate dcerpc_auth elements
188 * @param pkt_trailer - The packet trailer data, usually the trailing
189 * auth_info blob, but in the request/response case
190 * this is the stub_and_verifier blob.
191 * @param auth - A preallocated dcerpc_auth *empty* structure
192 * @param auth_length - The length of the auth trail, sum of auth header
193 * lenght and pkt->auth_length
194 * @param auth_data_only - Whether the pkt_trailer includes only the auth_blob
195 * (+ padding) or also other data.
197 * @return - A NTSTATUS error code.
199 NTSTATUS dcerpc_pull_auth_trailer(const struct ncacn_packet *pkt,
200 TALLOC_CTX *mem_ctx,
201 const DATA_BLOB *pkt_trailer,
202 struct dcerpc_auth *auth,
203 uint32_t *auth_length,
204 bool auth_data_only);
205 NTSTATUS dcerpc_verify_ncacn_packet_header(const struct ncacn_packet *pkt,
206 enum dcerpc_pkt_type ptype,
207 size_t max_auth_info,
208 uint8_t required_flags,
209 uint8_t optional_flags);
210 NTSTATUS dcerpc_ncacn_pull_pkt_auth(const struct dcerpc_auth *auth_state,
211 struct gensec_security *gensec,
212 TALLOC_CTX *mem_ctx,
213 enum dcerpc_pkt_type ptype,
214 uint8_t required_flags,
215 uint8_t optional_flags,
216 uint8_t payload_offset,
217 DATA_BLOB *payload_and_verifier,
218 DATA_BLOB *raw_packet,
219 const struct ncacn_packet *pkt);
220 NTSTATUS dcerpc_ncacn_push_pkt_auth(const struct dcerpc_auth *auth_state,
221 struct gensec_security *gensec,
222 TALLOC_CTX *mem_ctx,
223 DATA_BLOB *raw_packet,
224 size_t sig_size,
225 uint8_t payload_offset,
226 const DATA_BLOB *payload,
227 const struct ncacn_packet *pkt);
228 struct tevent_req *dcerpc_read_ncacn_packet_send(TALLOC_CTX *mem_ctx,
229 struct tevent_context *ev,
230 struct tstream_context *stream);
231 NTSTATUS dcerpc_read_ncacn_packet_recv(struct tevent_req *req,
232 TALLOC_CTX *mem_ctx,
233 struct ncacn_packet **pkt,
234 DATA_BLOB *buffer);
236 /* The following definitions come from ../librpc/rpc/binding_handle.c */
238 struct dcerpc_binding_handle_ops {
239 const char *name;
241 bool (*is_connected)(struct dcerpc_binding_handle *h);
242 uint32_t (*set_timeout)(struct dcerpc_binding_handle *h,
243 uint32_t timeout);
245 void (*auth_info)(struct dcerpc_binding_handle *h,
246 enum dcerpc_AuthType *auth_type,
247 enum dcerpc_AuthLevel *auth_level);
249 struct tevent_req *(*raw_call_send)(TALLOC_CTX *mem_ctx,
250 struct tevent_context *ev,
251 struct dcerpc_binding_handle *h,
252 const struct GUID *object,
253 uint32_t opnum,
254 uint32_t in_flags,
255 const uint8_t *in_data,
256 size_t in_length);
257 NTSTATUS (*raw_call_recv)(struct tevent_req *req,
258 TALLOC_CTX *mem_ctx,
259 uint8_t **out_data,
260 size_t *out_length,
261 uint32_t *out_flags);
263 struct tevent_req *(*disconnect_send)(TALLOC_CTX *mem_ctx,
264 struct tevent_context *ev,
265 struct dcerpc_binding_handle *h);
266 NTSTATUS (*disconnect_recv)(struct tevent_req *req);
268 /* TODO: remove the following functions */
269 bool (*push_bigendian)(struct dcerpc_binding_handle *h);
270 bool (*ref_alloc)(struct dcerpc_binding_handle *h);
271 bool (*use_ndr64)(struct dcerpc_binding_handle *h);
272 void (*do_ndr_print)(struct dcerpc_binding_handle *h,
273 int ndr_flags,
274 const void *struct_ptr,
275 const struct ndr_interface_call *call);
276 void (*ndr_push_failed)(struct dcerpc_binding_handle *h,
277 NTSTATUS error,
278 const void *struct_ptr,
279 const struct ndr_interface_call *call);
280 void (*ndr_pull_failed)(struct dcerpc_binding_handle *h,
281 NTSTATUS error,
282 const DATA_BLOB *blob,
283 const struct ndr_interface_call *call);
284 NTSTATUS (*ndr_validate_in)(struct dcerpc_binding_handle *h,
285 TALLOC_CTX *mem_ctx,
286 const DATA_BLOB *blob,
287 const struct ndr_interface_call *call);
288 NTSTATUS (*ndr_validate_out)(struct dcerpc_binding_handle *h,
289 struct ndr_pull *pull_in,
290 const void *struct_ptr,
291 const struct ndr_interface_call *call);
294 struct dcerpc_binding_handle *_dcerpc_binding_handle_create(TALLOC_CTX *mem_ctx,
295 const struct dcerpc_binding_handle_ops *ops,
296 const struct GUID *object,
297 const struct ndr_interface_table *table,
298 void *pstate,
299 size_t psize,
300 const char *type,
301 const char *location);
302 #define dcerpc_binding_handle_create(mem_ctx, ops, object, table, \
303 state, type, location) \
304 _dcerpc_binding_handle_create(mem_ctx, ops, object, table, \
305 state, sizeof(type), #type, location)
307 void *_dcerpc_binding_handle_data(struct dcerpc_binding_handle *h);
308 #define dcerpc_binding_handle_data(_h, _type) \
309 talloc_get_type_abort(_dcerpc_binding_handle_data(_h), _type)
311 _DEPRECATED_ void dcerpc_binding_handle_set_sync_ev(struct dcerpc_binding_handle *h,
312 struct tevent_context *ev);
314 bool dcerpc_binding_handle_is_connected(struct dcerpc_binding_handle *h);
316 uint32_t dcerpc_binding_handle_set_timeout(struct dcerpc_binding_handle *h,
317 uint32_t timeout);
319 void dcerpc_binding_handle_auth_info(struct dcerpc_binding_handle *h,
320 enum dcerpc_AuthType *auth_type,
321 enum dcerpc_AuthLevel *auth_level);
323 struct tevent_req *dcerpc_binding_handle_raw_call_send(TALLOC_CTX *mem_ctx,
324 struct tevent_context *ev,
325 struct dcerpc_binding_handle *h,
326 const struct GUID *object,
327 uint32_t opnum,
328 uint32_t in_flags,
329 const uint8_t *in_data,
330 size_t in_length);
331 NTSTATUS dcerpc_binding_handle_raw_call_recv(struct tevent_req *req,
332 TALLOC_CTX *mem_ctx,
333 uint8_t **out_data,
334 size_t *out_length,
335 uint32_t *out_flags);
336 NTSTATUS dcerpc_binding_handle_raw_call(struct dcerpc_binding_handle *h,
337 const struct GUID *object,
338 uint32_t opnum,
339 uint32_t in_flags,
340 const uint8_t *in_data,
341 size_t in_length,
342 TALLOC_CTX *mem_ctx,
343 uint8_t **out_data,
344 size_t *out_length,
345 uint32_t *out_flags);
347 struct tevent_req *dcerpc_binding_handle_disconnect_send(TALLOC_CTX *mem_ctx,
348 struct tevent_context *ev,
349 struct dcerpc_binding_handle *h);
350 NTSTATUS dcerpc_binding_handle_disconnect_recv(struct tevent_req *req);
352 struct tevent_req *dcerpc_binding_handle_call_send(TALLOC_CTX *mem_ctx,
353 struct tevent_context *ev,
354 struct dcerpc_binding_handle *h,
355 const struct GUID *object,
356 const struct ndr_interface_table *table,
357 uint32_t opnum,
358 TALLOC_CTX *r_mem,
359 void *r_ptr);
360 NTSTATUS dcerpc_binding_handle_call_recv(struct tevent_req *req);
361 NTSTATUS dcerpc_binding_handle_call(struct dcerpc_binding_handle *h,
362 const struct GUID *object,
363 const struct ndr_interface_table *table,
364 uint32_t opnum,
365 TALLOC_CTX *r_mem,
366 void *r_ptr);
369 * Extract header information from a ncacn_packet
370 * as a dcerpc_sec_vt_header2 as used by the security verification trailer.
372 * @param[in] pkt a packet
374 * @return a dcerpc_sec_vt_header2
376 struct dcerpc_sec_vt_header2 dcerpc_sec_vt_header2_from_ncacn_packet(const struct ncacn_packet *pkt);
380 * Test if two dcerpc_sec_vt_header2 structures are equal
381 * without consideration of reserved fields.
383 * @param v1 a pointer to a dcerpc_sec_vt_header2 structure
384 * @param v2 a pointer to a dcerpc_sec_vt_header2 structure
386 * @retval true if *v1 equals *v2
388 bool dcerpc_sec_vt_header2_equal(const struct dcerpc_sec_vt_header2 *v1,
389 const struct dcerpc_sec_vt_header2 *v2);
392 * Check for consistency of the security verification trailer with the PDU header.
393 * See <a href="http://msdn.microsoft.com/en-us/library/cc243559.aspx">MS-RPCE 2.2.2.13</a>.
394 * A check with an empty trailer succeeds.
396 * @param[in] vt a pointer to the security verification trailer.
397 * @param[in] bitmask1 which flags were negotiated on the connection.
398 * @param[in] pcontext the syntaxes negotiatied for the presentation context.
399 * @param[in] header2 some fields from the PDU header.
401 * @retval true on success.
403 bool dcerpc_sec_verification_trailer_check(
404 const struct dcerpc_sec_verification_trailer *vt,
405 const uint32_t *bitmask1,
406 const struct dcerpc_sec_vt_pcontext *pcontext,
407 const struct dcerpc_sec_vt_header2 *header2);
410 * @brief check and optionally extract the Bind Time Features from
411 * the given ndr_syntax_id.
413 * <a href="http://msdn.microsoft.com/en-us/library/cc243715.aspx">MS-RPCE 3.3.1.5.3 Bind Time Feature Negotiation</a>.
415 * @param[in] s the syntax that should be checked.
417 * @param[out] features This is optional, it will be filled with the extracted
418 * features the on success, otherwise it's filled with 0.
420 * @return true if the syntax matches the 6CB71C2C-9812-4540 prefix with version 1, false otherwise.
422 * @see dcerpc_construct_bind_time_features
424 bool dcerpc_extract_bind_time_features(struct ndr_syntax_id syntax, uint64_t *features);
427 * @brief Construct a ndr_syntax_id used for Bind Time Features Negotiation.
429 * <a href="http://msdn.microsoft.com/en-us/library/cc243715.aspx">MS-RPCE 3.3.1.5.3 Bind Time Feature Negotiation</a>.
431 * @param[in] features The supported features.
433 * @return The ndr_syntax_id with the given features.
435 * @see dcerpc_extract_bind_time_features
437 struct ndr_syntax_id dcerpc_construct_bind_time_features(uint64_t features);
439 #define DCERPC_AUTH_PAD_LENGTH(stub_length) (\
440 (((stub_length) % DCERPC_AUTH_PAD_ALIGNMENT) > 0)?\
441 (DCERPC_AUTH_PAD_ALIGNMENT - (stub_length) % DCERPC_AUTH_PAD_ALIGNMENT):\
444 #endif /* __DEFAULT_LIBRPC_RPCCOMMON_H__ */