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