s4:librpc/rpc: add dcerpc_binding_handle dcerpc_pipe backend
[amitay/samba.git] / source4 / librpc / rpc / dcerpc.h
1 /* 
2    Unix SMB/CIFS implementation.
3
4    DCERPC client side interface structures
5
6    Copyright (C) Tim Potter 2003
7    Copyright (C) Andrew Tridgell 2003-2005
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 /* This is a public header file that is installed as part of Samba. 
24  * If you remove any functions or change their signature, update 
25  * the so version number. */
26
27 #ifndef __DCERPC_H__
28 #define __DCERPC_H__
29
30 #include "../lib/util/data_blob.h"
31 #include "librpc/gen_ndr/dcerpc.h"
32 #include "../librpc/ndr/libndr.h"
33
34 struct tevent_context;
35 struct tevent_req;
36 struct dcerpc_binding_handle;
37
38 enum dcerpc_transport_t {
39         NCA_UNKNOWN, NCACN_NP, NCACN_IP_TCP, NCACN_IP_UDP, NCACN_VNS_IPC, 
40         NCACN_VNS_SPP, NCACN_AT_DSP, NCADG_AT_DDP, NCALRPC, NCACN_UNIX_STREAM, 
41         NCADG_UNIX_DGRAM, NCACN_HTTP, NCADG_IPX, NCACN_SPX, NCACN_INTERNAL };
42
43 /*
44   this defines a generic security context for signed/sealed dcerpc pipes.
45 */
46 struct dcerpc_connection;
47 struct gensec_settings;
48 struct dcerpc_security {
49         struct dcerpc_auth *auth_info;
50         struct gensec_security *generic_state;
51
52         /* get the session key */
53         NTSTATUS (*session_key)(struct dcerpc_connection *, DATA_BLOB *);
54 };
55
56 /*
57   this holds the information that is not specific to a particular rpc context_id
58 */
59 struct dcerpc_connection {
60         uint32_t call_id;
61         uint32_t srv_max_xmit_frag;
62         uint32_t srv_max_recv_frag;
63         uint32_t flags;
64         struct dcerpc_security security_state;
65         const char *binding_string;
66         struct tevent_context *event_ctx;
67
68         /** Directory in which to save ndrdump-parseable files */
69         const char *packet_log_dir;
70
71         bool dead;
72         bool free_skipped;
73
74         struct dcerpc_transport {
75                 enum dcerpc_transport_t transport;
76                 void *private_data;
77
78                 NTSTATUS (*shutdown_pipe)(struct dcerpc_connection *, NTSTATUS status);
79
80                 const char *(*peer_name)(struct dcerpc_connection *);
81
82                 const char *(*target_hostname)(struct dcerpc_connection *);
83
84                 /* send a request to the server */
85                 NTSTATUS (*send_request)(struct dcerpc_connection *, DATA_BLOB *, bool trigger_read);
86
87                 /* send a read request to the server */
88                 NTSTATUS (*send_read)(struct dcerpc_connection *);
89
90                 /* a callback to the dcerpc code when a full fragment
91                    has been received */
92                 void (*recv_data)(struct dcerpc_connection *, DATA_BLOB *, NTSTATUS status);
93         } transport;
94
95         /* Requests that have been sent, waiting for a reply */
96         struct rpc_request *pending;
97
98         /* Sync requests waiting to be shipped */
99         struct rpc_request *request_queue;
100
101         /* the next context_id to be assigned */
102         uint32_t next_context_id;
103 };
104
105 /*
106   this encapsulates a full dcerpc client side pipe 
107 */
108 struct dcerpc_pipe {
109         struct dcerpc_binding_handle *binding_handle;
110
111         uint32_t context_id;
112
113         uint32_t assoc_group_id;
114
115         struct ndr_syntax_id syntax;
116         struct ndr_syntax_id transfer_syntax;
117
118         struct dcerpc_connection *conn;
119         struct dcerpc_binding *binding;
120
121         /** the last fault code from a DCERPC fault */
122         uint32_t last_fault_code;
123
124         /** timeout for individual rpc requests, in seconds */
125         uint32_t request_timeout;
126 };
127
128 /* default timeout for all rpc requests, in seconds */
129 #define DCERPC_REQUEST_TIMEOUT 60
130
131
132 /* dcerpc pipe flags */
133 #define DCERPC_DEBUG_PRINT_IN          (1<<0)
134 #define DCERPC_DEBUG_PRINT_OUT         (1<<1)
135 #define DCERPC_DEBUG_PRINT_BOTH (DCERPC_DEBUG_PRINT_IN | DCERPC_DEBUG_PRINT_OUT)
136
137 #define DCERPC_DEBUG_VALIDATE_IN       (1<<2)
138 #define DCERPC_DEBUG_VALIDATE_OUT      (1<<3)
139 #define DCERPC_DEBUG_VALIDATE_BOTH (DCERPC_DEBUG_VALIDATE_IN | DCERPC_DEBUG_VALIDATE_OUT)
140
141 #define DCERPC_CONNECT                 (1<<4)
142 #define DCERPC_SIGN                    (1<<5)
143 #define DCERPC_SEAL                    (1<<6)
144
145 #define DCERPC_PUSH_BIGENDIAN          (1<<7)
146 #define DCERPC_PULL_BIGENDIAN          (1<<8)
147
148 #define DCERPC_SCHANNEL                (1<<9)
149
150 #define DCERPC_ANON_FALLBACK           (1<<10)
151
152 /* use a 128 bit session key */
153 #define DCERPC_SCHANNEL_128            (1<<12)
154
155 /* check incoming pad bytes */
156 #define DCERPC_DEBUG_PAD_CHECK         (1<<13)
157
158 /* set LIBNDR_FLAG_REF_ALLOC flag when decoding NDR */
159 #define DCERPC_NDR_REF_ALLOC           (1<<14)
160
161 #define DCERPC_AUTH_OPTIONS    (DCERPC_SEAL|DCERPC_SIGN|DCERPC_SCHANNEL|DCERPC_AUTH_SPNEGO|DCERPC_AUTH_KRB5|DCERPC_AUTH_NTLM)
162
163 /* select spnego auth */
164 #define DCERPC_AUTH_SPNEGO             (1<<15)
165
166 /* select krb5 auth */
167 #define DCERPC_AUTH_KRB5               (1<<16)
168
169 #define DCERPC_SMB2                    (1<<17)
170
171 /* select NTLM auth */
172 #define DCERPC_AUTH_NTLM               (1<<18)
173
174 /* this triggers the DCERPC_PFC_FLAG_CONC_MPX flag in the bind request */
175 #define DCERPC_CONCURRENT_MULTIPLEX     (1<<19)
176
177 /* this triggers the DCERPC_PFC_FLAG_SUPPORT_HEADER_SIGN flag in the bind request */
178 #define DCERPC_HEADER_SIGNING          (1<<20)
179
180 /* use NDR64 transport */
181 #define DCERPC_NDR64                   (1<<21)
182
183 /* this describes a binding to a particular transport/pipe */
184 struct dcerpc_binding {
185         enum dcerpc_transport_t transport;
186         struct ndr_syntax_id object;
187         const char *host;
188         const char *target_hostname;
189         const char *endpoint;
190         const char **options;
191         uint32_t flags;
192         uint32_t assoc_group_id;
193 };
194
195
196 struct dcerpc_pipe_connect {
197         struct dcerpc_pipe *pipe;
198         struct dcerpc_binding *binding;
199         const char *pipe_name;
200         const struct ndr_interface_table *interface;
201         struct cli_credentials *creds;
202         struct resolve_context *resolve_ctx;
203 };
204
205
206 enum rpc_request_state {
207         RPC_REQUEST_QUEUED,
208         RPC_REQUEST_PENDING,
209         RPC_REQUEST_DONE
210 };
211
212 /*
213   handle for an async dcerpc request
214 */
215 struct rpc_request {
216         struct rpc_request *next, *prev;
217         struct dcerpc_pipe *p;
218         NTSTATUS status;
219         uint32_t call_id;
220         enum rpc_request_state state;
221         DATA_BLOB payload;
222         uint32_t flags;
223         uint32_t fault_code;
224
225         /* this is used to distinguish bind and alter_context requests
226            from normal requests */
227         void (*recv_handler)(struct rpc_request *conn, 
228                              DATA_BLOB *blob, struct ncacn_packet *pkt);
229
230         const struct GUID *object;
231         uint16_t opnum;
232         DATA_BLOB request_data;
233         bool ignore_timeout;
234
235         /* use by the ndr level async recv call */
236         struct {
237                 const struct ndr_interface_table *table;
238                 uint32_t opnum;
239                 void *struct_ptr;
240                 TALLOC_CTX *mem_ctx;
241         } ndr;
242
243         struct {
244                 void (*callback)(struct rpc_request *);
245                 void *private_data;
246         } async;
247 };
248
249 struct epm_tower;
250 struct epm_floor;
251
252 struct smbcli_tree;
253 struct smb2_tree;
254 struct socket_address;
255
256 NTSTATUS dcerpc_pipe_connect(TALLOC_CTX *parent_ctx, 
257                              struct dcerpc_pipe **pp, 
258                              const char *binding,
259                              const struct ndr_interface_table *table,
260                              struct cli_credentials *credentials,
261                              struct tevent_context *ev,
262                              struct loadparm_context *lp_ctx);
263 NTSTATUS dcerpc_ndr_request_recv(struct rpc_request *req);
264 struct rpc_request *dcerpc_ndr_request_send(struct dcerpc_pipe *p,
265                                                 const struct GUID *object,
266                                                 const struct ndr_interface_table *table,
267                                                 uint32_t opnum, 
268                                                 bool async,
269                                                 TALLOC_CTX *mem_ctx, 
270                                                 void *r);
271 const char *dcerpc_server_name(struct dcerpc_pipe *p);
272 struct dcerpc_pipe *dcerpc_pipe_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev);
273 NTSTATUS dcerpc_pipe_open_smb(struct dcerpc_pipe *p,
274                               struct smbcli_tree *tree,
275                               const char *pipe_name);
276 NTSTATUS dcerpc_bind_auth_none(struct dcerpc_pipe *p,
277                                const struct ndr_interface_table *table);
278 NTSTATUS dcerpc_fetch_session_key(struct dcerpc_pipe *p,
279                                   DATA_BLOB *session_key);
280 struct composite_context;
281 NTSTATUS dcerpc_secondary_connection_recv(struct composite_context *c,
282                                           struct dcerpc_pipe **p2);
283 NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *s, struct dcerpc_binding **b_out);
284
285 struct composite_context* dcerpc_pipe_connect_b_send(TALLOC_CTX *parent_ctx,
286                                                      struct dcerpc_binding *binding,
287                                                      const struct ndr_interface_table *table,
288                                                      struct cli_credentials *credentials,
289                                                      struct tevent_context *ev,
290                                                      struct loadparm_context *lp_ctx);
291
292 NTSTATUS dcerpc_pipe_connect_b_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
293                                     struct dcerpc_pipe **p);
294
295 NTSTATUS dcerpc_pipe_connect_b(TALLOC_CTX *parent_ctx,
296                                struct dcerpc_pipe **pp,
297                                struct dcerpc_binding *binding,
298                                const struct ndr_interface_table *table,
299                                struct cli_credentials *credentials,
300                                struct tevent_context *ev,
301                                struct loadparm_context *lp_ctx);
302 const char *dcerpc_errstr(TALLOC_CTX *mem_ctx, uint32_t fault_code);
303 NTSTATUS dcerpc_fault_to_nt_status(uint32_t fault_code);
304
305 NTSTATUS dcerpc_pipe_auth(TALLOC_CTX *mem_ctx,
306                           struct dcerpc_pipe **p, 
307                           struct dcerpc_binding *binding,
308                           const struct ndr_interface_table *table,
309                           struct cli_credentials *credentials,
310                           struct loadparm_context *lp_ctx);
311 char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b);
312 NTSTATUS dcerpc_secondary_connection(struct dcerpc_pipe *p,
313                                      struct dcerpc_pipe **p2,
314                                      struct dcerpc_binding *b);
315 NTSTATUS dcerpc_bind_auth_schannel(TALLOC_CTX *tmp_ctx, 
316                                    struct dcerpc_pipe *p,
317                                    const struct ndr_interface_table *table,
318                                    struct cli_credentials *credentials,
319                                    struct loadparm_context *lp_ctx,
320                                    uint8_t auth_level);
321 struct tevent_context *dcerpc_event_context(struct dcerpc_pipe *p);
322 NTSTATUS dcerpc_init(struct loadparm_context *lp_ctx);
323 struct smbcli_tree *dcerpc_smb_tree(struct dcerpc_connection *c);
324 uint16_t dcerpc_smb_fnum(struct dcerpc_connection *c);
325 NTSTATUS dcerpc_secondary_context(struct dcerpc_pipe *p, 
326                                   struct dcerpc_pipe **pp2,
327                                   const struct ndr_interface_table *table);
328 NTSTATUS dcerpc_alter_context(struct dcerpc_pipe *p, 
329                               TALLOC_CTX *mem_ctx,
330                               const struct ndr_syntax_id *syntax,
331                               const struct ndr_syntax_id *transfer_syntax);
332
333 NTSTATUS dcerpc_bind_auth(struct dcerpc_pipe *p,
334                           const struct ndr_interface_table *table,
335                           struct cli_credentials *credentials,
336                           struct gensec_settings *gensec_settings,
337                           uint8_t auth_type, uint8_t auth_level,
338                           const char *service);
339 struct composite_context* dcerpc_pipe_connect_send(TALLOC_CTX *parent_ctx,
340                                                    const char *binding,
341                                                    const struct ndr_interface_table *table,
342                                                    struct cli_credentials *credentials,
343                                                    struct tevent_context *ev, struct loadparm_context *lp_ctx);
344 NTSTATUS dcerpc_pipe_connect_recv(struct composite_context *c,
345                                   TALLOC_CTX *mem_ctx,
346                                   struct dcerpc_pipe **pp);
347
348 NTSTATUS dcerpc_epm_map_binding(TALLOC_CTX *mem_ctx, struct dcerpc_binding *binding,
349                                 const struct ndr_interface_table *table, struct tevent_context *ev,
350                                 struct loadparm_context *lp_ctx);
351 struct composite_context* dcerpc_secondary_auth_connection_send(struct dcerpc_pipe *p,
352                                                                 struct dcerpc_binding *binding,
353                                                                 const struct ndr_interface_table *table,
354                                                                 struct cli_credentials *credentials,
355                                                                 struct loadparm_context *lp_ctx);
356 NTSTATUS dcerpc_secondary_auth_connection_recv(struct composite_context *c, 
357                                                TALLOC_CTX *mem_ctx,
358                                                struct dcerpc_pipe **p);
359
360 struct composite_context* dcerpc_secondary_connection_send(struct dcerpc_pipe *p,
361                                                            struct dcerpc_binding *b);
362 void dcerpc_log_packet(const char *lockdir, 
363                        const struct ndr_interface_table *ndr,
364                        uint32_t opnum, uint32_t flags,
365                        const DATA_BLOB *pkt);
366 NTSTATUS dcerpc_binding_build_tower(TALLOC_CTX *mem_ctx,
367                                     const struct dcerpc_binding *binding,
368                                     struct epm_tower *tower);
369
370 NTSTATUS dcerpc_floor_get_lhs_data(const struct epm_floor *epm_floor, struct ndr_syntax_id *syntax);
371
372 enum dcerpc_transport_t dcerpc_transport_by_tower(const struct epm_tower *tower);
373 const char *derpc_transport_string_by_transport(enum dcerpc_transport_t t);
374
375 NTSTATUS dcerpc_ndr_request(struct dcerpc_pipe *p,
376                             const struct GUID *object,
377                             const struct ndr_interface_table *table,
378                             uint32_t opnum, 
379                             TALLOC_CTX *mem_ctx, 
380                             void *r);
381
382 NTSTATUS dcerpc_binding_from_tower(TALLOC_CTX *mem_ctx, 
383                                    struct epm_tower *tower, 
384                                    struct dcerpc_binding **b_out);
385
386 NTSTATUS dcerpc_request(struct dcerpc_pipe *p, 
387                         struct GUID *object,
388                         uint16_t opnum,
389                         TALLOC_CTX *mem_ctx,
390                         DATA_BLOB *stub_data_in,
391                         DATA_BLOB *stub_data_out);
392
393 enum dcerpc_transport_t dcerpc_transport_by_endpoint_protocol(int prot);
394
395 const char *dcerpc_floor_get_rhs_data(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor);
396
397 struct dcerpc_binding_handle_ops {
398         const char *name;
399
400         bool (*is_connected)(struct dcerpc_binding_handle *h);
401
402         struct tevent_req *(*raw_call_send)(TALLOC_CTX *mem_ctx,
403                                             struct tevent_context *ev,
404                                             struct dcerpc_binding_handle *h,
405                                             const struct GUID *object,
406                                             uint32_t opnum,
407                                             uint32_t in_flags,
408                                             const uint8_t *in_data,
409                                             size_t in_length);
410         NTSTATUS (*raw_call_recv)(struct tevent_req *req,
411                                   TALLOC_CTX *mem_ctx,
412                                   uint8_t **out_data,
413                                   size_t *out_length,
414                                   uint32_t *out_flags);
415
416         struct tevent_req *(*disconnect_send)(TALLOC_CTX *mem_ctx,
417                                               struct tevent_context *ev,
418                                               struct dcerpc_binding_handle *h);
419         NTSTATUS (*disconnect_recv)(struct tevent_req *req);
420
421         /* TODO: remove the following functions */
422         bool (*push_bigendian)(struct dcerpc_binding_handle *h);
423         bool (*ref_alloc)(struct dcerpc_binding_handle *h);
424         bool (*use_ndr64)(struct dcerpc_binding_handle *h);
425         void (*do_ndr_print)(struct dcerpc_binding_handle *h,
426                              int ndr_flags,
427                              const void *struct_ptr,
428                              const struct ndr_interface_call *call);
429         void (*ndr_push_failed)(struct dcerpc_binding_handle *h,
430                                 NTSTATUS error,
431                                 const void *struct_ptr,
432                                 const struct ndr_interface_call *call);
433         void (*ndr_pull_failed)(struct dcerpc_binding_handle *h,
434                                 NTSTATUS error,
435                                 const DATA_BLOB *blob,
436                                 const struct ndr_interface_call *call);
437         NTSTATUS (*ndr_validate_in)(struct dcerpc_binding_handle *h,
438                                     TALLOC_CTX *mem_ctx,
439                                     const DATA_BLOB *blob,
440                                     const struct ndr_interface_call *call);
441         NTSTATUS (*ndr_validate_out)(struct dcerpc_binding_handle *h,
442                                      struct ndr_pull *pull_in,
443                                      const void *struct_ptr,
444                                      const struct ndr_interface_call *call);
445 };
446
447 /* TODO: this needs to be completely private */
448 struct dcerpc_binding_handle {
449         void *private_data;
450         const struct dcerpc_binding_handle_ops *ops;
451         const char *location;
452         const struct GUID *object;
453         const struct ndr_interface_table *table;
454         struct tevent_context *sync_ev;
455 };
456
457 struct dcerpc_binding_handle *_dcerpc_binding_handle_create(TALLOC_CTX *mem_ctx,
458                                         const struct dcerpc_binding_handle_ops *ops,
459                                         const struct GUID *object,
460                                         const struct ndr_interface_table *table,
461                                         void *pstate,
462                                         size_t psize,
463                                         const char *type,
464                                         const char *location);
465 #define dcerpc_binding_handle_create(mem_ctx, ops, object, table, \
466                                 state, type, location) \
467         _dcerpc_binding_handle_create(mem_ctx, ops, object, table, \
468                                 state, sizeof(type), #type, location)
469
470 void *_dcerpc_binding_handle_data(struct dcerpc_binding_handle *h);
471 #define dcerpc_binding_handle_data(_h, _type) \
472         talloc_get_type_abort(_dcerpc_binding_handle_data(_h), _type)
473
474 _DEPRECATED_ void dcerpc_binding_handle_set_sync_ev(struct dcerpc_binding_handle *h,
475                                                     struct tevent_context *ev);
476
477 bool dcerpc_binding_handle_is_connected(struct dcerpc_binding_handle *h);
478
479 struct tevent_req *dcerpc_binding_handle_raw_call_send(TALLOC_CTX *mem_ctx,
480                                                 struct tevent_context *ev,
481                                                 struct dcerpc_binding_handle *h,
482                                                 const struct GUID *object,
483                                                 uint32_t opnum,
484                                                 uint32_t in_flags,
485                                                 const uint8_t *in_data,
486                                                 size_t in_length);
487 NTSTATUS dcerpc_binding_handle_raw_call_recv(struct tevent_req *req,
488                                              TALLOC_CTX *mem_ctx,
489                                              uint8_t **out_data,
490                                              size_t *out_length,
491                                              uint32_t *out_flags);
492
493 struct tevent_req *dcerpc_binding_handle_disconnect_send(TALLOC_CTX *mem_ctx,
494                                                 struct tevent_context *ev,
495                                                 struct dcerpc_binding_handle *h);
496 NTSTATUS dcerpc_binding_handle_disconnect_recv(struct tevent_req *req);
497
498 struct tevent_req *dcerpc_binding_handle_call_send(TALLOC_CTX *mem_ctx,
499                                         struct tevent_context *ev,
500                                         struct dcerpc_binding_handle *h,
501                                         const struct GUID *object,
502                                         const struct ndr_interface_table *table,
503                                         uint32_t opnum,
504                                         TALLOC_CTX *r_mem,
505                                         void *r_ptr);
506 NTSTATUS dcerpc_binding_handle_call_recv(struct tevent_req *req);
507 NTSTATUS dcerpc_binding_handle_call(struct dcerpc_binding_handle *h,
508                                     const struct GUID *object,
509                                     const struct ndr_interface_table *table,
510                                     uint32_t opnum,
511                                     TALLOC_CTX *r_mem,
512                                     void *r_ptr);
513
514 #endif /* __DCERPC_H__ */