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