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