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