0f83fef6d31f9730df219efbce29e517ab4fa81b
[samba.git] / source4 / rpc_server / dcerpc_server.h
1 /* 
2    Unix SMB/CIFS implementation.
3
4    server side dcerpc defines
5
6    Copyright (C) Andrew Tridgell 2003-2005
7    Copyright (C) Stefan (metze) Metzmacher 2004-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 #ifndef SAMBA_DCERPC_SERVER_H
24 #define SAMBA_DCERPC_SERVER_H
25
26 #include "librpc/rpc/rpc_common.h"
27 #include "librpc/ndr/libndr.h"
28
29 /* modules can use the following to determine if the interface has changed
30  * please increment the version number after each interface change
31  * with a comment and maybe update struct dcesrv_critical_sizes.
32  */
33 /* version 1 - initial version - metze */
34 #define DCERPC_MODULE_VERSION 1
35
36 struct dcesrv_connection;
37 struct dcesrv_call_state;
38 struct dcesrv_auth;
39 struct dcesrv_connection_context;
40 struct dcesrv_iface_state;
41 struct cli_credentials;
42
43 struct dcesrv_interface {
44         const char *name;
45         struct ndr_syntax_id syntax_id;
46
47         /* this function is called when the client binds to this interface  */
48         NTSTATUS (*bind)(struct dcesrv_connection_context *, const struct dcesrv_interface *);
49
50         /* this function is called when the client disconnects the endpoint */
51         void (*unbind)(struct dcesrv_connection_context *, const struct dcesrv_interface *);
52
53         /* the ndr_pull function for the chosen interface.
54          */
55         NTSTATUS (*ndr_pull)(struct dcesrv_call_state *, TALLOC_CTX *, struct ndr_pull *, void **);
56         
57         /* the dispatch function for the chosen interface.
58          */
59         NTSTATUS (*dispatch)(struct dcesrv_call_state *, TALLOC_CTX *, void *);
60
61         /* the reply function for the chosen interface.
62          */
63         NTSTATUS (*reply)(struct dcesrv_call_state *, TALLOC_CTX *, void *);
64
65         /* the ndr_push function for the chosen interface.
66          */
67         NTSTATUS (*ndr_push)(struct dcesrv_call_state *, TALLOC_CTX *, struct ndr_push *, const void *);
68
69         /* for any private use by the interface code */
70         const void *private_data;
71
72         uint64_t flags;
73 };
74
75 #define DCESRV_INTERFACE_FLAGS_HANDLES_NOT_USED 0x00000001
76
77 enum dcesrv_call_list {
78         DCESRV_LIST_NONE,
79         DCESRV_LIST_CALL_LIST,
80         DCESRV_LIST_FRAGMENTED_CALL_LIST,
81         DCESRV_LIST_PENDING_CALL_LIST
82 };
83
84 struct data_blob_list_item {
85         struct data_blob_list_item *prev,*next;
86         DATA_BLOB blob;
87 };
88
89 /* the state of an ongoing dcerpc call */
90 struct dcesrv_call_state {
91         struct dcesrv_call_state *next, *prev;
92         struct dcesrv_auth *auth_state;
93         struct dcesrv_connection *conn;
94         struct dcesrv_connection_context *context;
95         struct ncacn_packet pkt;
96
97         /*
98          * Used during async bind/alter_context.
99          */
100         struct ncacn_packet ack_pkt;
101
102         /*
103           which list this request is in, if any
104          */
105         enum dcesrv_call_list list;
106
107         /* the backend can mark the call
108          * with DCESRV_CALL_STATE_FLAG_ASYNC
109          * that will cause the frontend to not touch r->out
110          * and skip the reply
111          *
112          * this is only allowed to the backend when DCESRV_CALL_STATE_FLAG_MAY_ASYNC
113          * is alerady set by the frontend
114          *
115          * the backend then needs to call dcesrv_reply() when it's
116          * ready to send the reply
117          */
118 #define DCESRV_CALL_STATE_FLAG_ASYNC (1<<0)
119 #define DCESRV_CALL_STATE_FLAG_MAY_ASYNC (1<<1)
120 #define DCESRV_CALL_STATE_FLAG_MULTIPLEXED (1<<3)
121 #define DCESRV_CALL_STATE_FLAG_PROCESS_PENDING_CALL (1<<4)
122         uint32_t state_flags;
123
124         /* the time the request arrived in the server */
125         struct timeval time;
126
127         /* the backend can use this event context for async replies */
128         struct tevent_context *event_ctx;
129
130         /* this is the pointer to the allocated function struct */
131         void *r;
132
133         /*
134          * that's the ndr pull context used in dcesrv_request()
135          * needed by dcesrv_reply() to carry over information
136          * for full pointer support.
137          */
138         struct ndr_pull *ndr_pull;
139
140         DATA_BLOB input;
141
142         struct data_blob_list_item *replies;
143
144         /* this is used by the boilerplate code to generate DCERPC faults */
145         uint32_t fault_code;
146
147         /* the reason why we terminate the connection after sending a response */
148         const char *terminate_reason;
149
150         /* temporary auth_info fields */
151         struct dcerpc_auth in_auth_info;
152         struct dcerpc_auth _out_auth_info;
153         struct dcerpc_auth *out_auth_info;
154 };
155
156 /*
157 * DCERPC Handles
158 * --------------
159 * The various handles that are used in the RPC servers should be
160 * created and fetch using the dcesrv_handle_* functions.
161 *
162 * Use
163 * dcesrv_handle_create(struct dcesrv_call_state \*, uint8 handle_type)
164 * to obtain a new handle of the specified type. Handle types are
165 * unique within each pipe.
166 *
167 * The handle can later be fetched again using:
168 *
169 * struct dcesrv_handle *dcesrv_handle_lookup(
170 *         struct dcesrv_call_state *dce_call,
171 *         struct policy_handle *p,
172 *         uint8 handle_type)
173 *
174 * and destroyed by:
175 *
176 *       TALLOC_FREE(struct dcesrv_handle *).
177 *
178 * User data should be stored in the 'data' member of the dcesrv_handle
179 * struct.
180 */
181
182 #define DCESRV_HANDLE_ANY 255
183
184 /* a dcerpc handle in internal format */
185 struct dcesrv_handle {
186         struct dcesrv_handle *next, *prev;
187         struct dcesrv_assoc_group *assoc_group;
188         struct policy_handle wire_handle;
189         struct dom_sid *sid;
190         enum dcerpc_AuthLevel min_auth_level;
191         const struct dcesrv_interface *iface;
192         void *data;
193 };
194
195 /* hold the authentication state information */
196 struct dcesrv_auth {
197         struct dcesrv_auth *prev, *next;
198         enum dcerpc_AuthType auth_type;
199         enum dcerpc_AuthLevel auth_level;
200         uint32_t auth_context_id;
201         struct gensec_security *gensec_security;
202         struct auth_session_info *session_info;
203         NTSTATUS (*session_key_fn)(struct dcesrv_auth *, DATA_BLOB *session_key);
204         bool auth_started;
205         bool auth_finished;
206         bool auth_audited;
207         bool auth_invalid;
208 };
209
210 struct dcesrv_connection_context {
211         struct dcesrv_connection_context *next, *prev;
212         uint16_t context_id;
213
214         /* the connection this is on */
215         struct dcesrv_connection *conn;
216
217         /* the ndr function table for the chosen interface */
218         const struct dcesrv_interface *iface;
219
220         /*
221          * the minimum required auth level for this interface
222          */
223         enum dcerpc_AuthLevel min_auth_level;
224         bool allow_connect;
225
226         /* the negotiated transfer syntax */
227         struct ndr_syntax_id transfer_syntax;
228 };
229
230
231 /* the state associated with a dcerpc server connection */
232 struct dcesrv_connection {
233         /* for the broken_connections DLIST */
234         struct dcesrv_connection *prev, *next;
235
236         /* the top level context for this server */
237         struct dcesrv_context *dce_ctx;
238
239         /* the endpoint that was opened */
240         const struct dcesrv_endpoint *endpoint;
241
242         /* a list of established context_ids */
243         struct dcesrv_connection_context *contexts;
244
245         /* the state of the current incoming call fragments */
246         struct dcesrv_call_state *incoming_fragmented_call_list;
247
248         /* the state of the async pending calls */
249         struct dcesrv_call_state *pending_call_list;
250
251         /* the state of the current outgoing calls */
252         struct dcesrv_call_state *call_list;
253
254         /* the maximum size the client wants to receive */
255         uint16_t max_recv_frag;
256         uint16_t max_xmit_frag;
257
258         DATA_BLOB partial_input;
259
260         /* the event_context that will be used for this connection */
261         struct tevent_context *event_ctx;
262
263         /* is this connection pending termination?  If so, why? */
264         const char *terminate;
265
266         const char *packet_log_dir;
267
268         /* this is the default state_flags for dcesrv_call_state structs */
269         uint32_t state_flags;
270
271         struct {
272                 void *private_data;
273                 void (*report_output_data)(struct dcesrv_connection *);
274                 void (*terminate_connection)(struct dcesrv_connection *,
275                                              const char *);
276         } transport;
277
278         struct tstream_context *stream;
279         struct tevent_queue *send_queue;
280
281         const struct tsocket_address *local_address;
282         const struct tsocket_address *remote_address;
283
284         /* the current authentication state */
285         struct dcesrv_auth *default_auth_state;
286         size_t max_auth_states;
287         struct dcesrv_auth *auth_states;
288         bool got_explicit_auth_level_connect;
289         struct dcesrv_auth *default_auth_level_connect;
290         bool client_hdr_signing;
291         bool support_hdr_signing;
292         bool negotiated_hdr_signing;
293
294         /*
295          * remember which pdu types are allowed
296          */
297         bool allow_bind;
298         bool allow_alter;
299
300         /* the association group the connection belongs to */
301         struct dcesrv_assoc_group *assoc_group;
302
303         /* The maximum total payload of reassembled request pdus */
304         size_t max_total_request_size;
305
306         /*
307          * Our preferred transfer syntax.
308          */
309         const struct ndr_syntax_id *preferred_transfer;
310
311         /*
312          * This is used to block the connection during
313          * pending authentication.
314          */
315         struct tevent_req *(*wait_send)(TALLOC_CTX *mem_ctx,
316                                         struct tevent_context *ev,
317                                         void *private_data);
318         NTSTATUS (*wait_recv)(struct tevent_req *req);
319         void *wait_private;
320 };
321
322
323 struct dcesrv_endpoint_server {
324         /* this is the name of the endpoint server */
325         const char *name;
326
327         /* this function should register endpoints and some other setup stuff,
328          * it is called when the dcesrv_context gets initialized.
329          */
330         NTSTATUS (*init_server)(struct dcesrv_context *, const struct dcesrv_endpoint_server *);
331
332         /* this function can be used by other endpoint servers to
333          * ask for a dcesrv_interface implementation
334          * - iface must be reference to an already existing struct !
335          */
336         bool (*interface_by_uuid)(struct dcesrv_interface *iface, const struct GUID *, uint32_t);
337
338         /* this function can be used by other endpoint servers to
339          * ask for a dcesrv_interface implementation
340          * - iface must be reference to an already existeng struct !
341          */
342         bool (*interface_by_name)(struct dcesrv_interface *iface, const char *);
343 };
344
345
346 /* one association groups */
347 struct dcesrv_assoc_group {
348         /* the wire id */
349         uint32_t id;
350
351         /* The transport this is valid on */
352         enum dcerpc_transport_t transport;
353
354         /* list of handles in this association group */
355         struct dcesrv_handle *handles;
356
357         /*
358          * list of iface states per assoc/conn
359          */
360         struct dcesrv_iface_state *iface_states;
361
362         /* parent context */
363         struct dcesrv_context *dce_ctx;
364
365         /* the negotiated bind time features */
366         uint16_t bind_time_features;
367 };
368
369 struct dcesrv_context_callbacks {
370         struct {
371                 void (*successful_authz)(struct dcesrv_call_state *);
372         } log;
373         struct {
374                 NTSTATUS (*gensec_prepare)(TALLOC_CTX *mem_ctx,
375                                         struct dcesrv_call_state *call,
376                                         struct gensec_security **out);
377         } auth;
378         struct {
379                 NTSTATUS (*find)(struct dcesrv_call_state *);
380         } assoc_group;
381 };
382
383 /* server-wide context information for the dcerpc server */
384 struct dcesrv_context {
385         /*
386          * The euid at startup time.
387          *
388          * This is required for DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM
389          */
390         uid_t initial_euid;
391
392         /* the list of endpoints that have registered 
393          * by the configured endpoint servers 
394          */
395         struct dcesrv_endpoint {
396                 struct dcesrv_endpoint *next, *prev;
397                 /* the type and location of the endpoint */
398                 struct dcerpc_binding *ep_description;
399                 /* the secondary endpoint description for the BIND_ACK */
400                 struct dcerpc_binding *ep_2nd_description;
401                 /* the security descriptor for smb named pipes */
402                 struct security_descriptor *sd;
403                 /* the list of interfaces available on this endpoint */
404                 struct dcesrv_if_list {
405                         struct dcesrv_if_list *next, *prev;
406                         struct dcesrv_interface iface;
407                 } *interface_list;
408
409                 /*
410                  * Should this service be run in a single process (so far only
411                  * NETLOGON is not run in a single process)
412                  */
413                 bool use_single_process;
414         } *endpoint_list;
415
416         /* loadparm context to use for this connection */
417         struct loadparm_context *lp_ctx;
418
419         struct idr_context *assoc_groups_idr;
420
421         struct dcesrv_connection *broken_connections;
422
423         struct dcesrv_context_callbacks callbacks;
424 };
425
426 /* this structure is used by modules to determine the size of some critical types */
427 struct dcesrv_critical_sizes {
428         int interface_version;
429         int sizeof_dcesrv_context;
430         int sizeof_dcesrv_endpoint;
431         int sizeof_dcesrv_endpoint_server;
432         int sizeof_dcesrv_interface;
433         int sizeof_dcesrv_if_list;
434         int sizeof_dcesrv_connection;
435         int sizeof_dcesrv_call_state;
436         int sizeof_dcesrv_auth;
437         int sizeof_dcesrv_handle;
438 };
439
440 struct model_ops;
441
442 NTSTATUS dcesrv_interface_register(struct dcesrv_context *dce_ctx,
443                                    const char *ep_name,
444                                    const char *ncacn_np_secondary_endpoint,
445                                    const struct dcesrv_interface *iface,
446                                    const struct security_descriptor *sd);
447 NTSTATUS dcerpc_register_ep_server(const struct dcesrv_endpoint_server *ep_server);
448 NTSTATUS dcesrv_init_context(TALLOC_CTX *mem_ctx,
449                              struct loadparm_context *lp_ctx,
450                              const char **endpoint_servers,
451                              struct dcesrv_context_callbacks *cb,
452                              struct dcesrv_context **_dce_ctx);
453
454 NTSTATUS dcesrv_reply(struct dcesrv_call_state *call);
455 struct dcesrv_handle *dcesrv_handle_create(struct dcesrv_call_state *call,
456                                            uint8_t handle_type);
457
458 struct dcesrv_handle *dcesrv_handle_lookup(struct dcesrv_call_state *call,
459                                            const struct policy_handle *p,
460                                            uint8_t handle_type);
461
462 const struct tsocket_address *dcesrv_connection_get_local_address(struct dcesrv_connection *conn);
463 const struct tsocket_address *dcesrv_connection_get_remote_address(struct dcesrv_connection *conn);
464
465 /*
466  * Fetch the authentication session key if available.
467  *
468  * This is the key generated by a gensec authentication.
469  */
470 NTSTATUS dcesrv_auth_session_key(struct dcesrv_call_state *call,
471                                  DATA_BLOB *session_key);
472
473 /*
474  * Fetch the transport session key if available.
475  * Typically this is the SMB session key
476  * or a fixed key for local transports.
477  *
478  * The key is always truncated to 16 bytes.
479 */
480 NTSTATUS dcesrv_transport_session_key(struct dcesrv_call_state *call,
481                                       DATA_BLOB *session_key);
482
483 /* a useful macro for generating a RPC fault in the backend code */
484 #define DCESRV_FAULT(code) do { \
485         dce_call->fault_code = code; \
486         return r->out.result; \
487 } while(0)
488
489 /* a useful macro for generating a RPC fault in the backend code */
490 #define DCESRV_FAULT_VOID(code) do { \
491         dce_call->fault_code = code; \
492         return; \
493 } while(0)
494
495 /* a useful macro for checking the validity of a dcerpc policy handle
496    and giving the right fault code if invalid */
497 #define DCESRV_CHECK_HANDLE(h) do {if (!(h)) DCESRV_FAULT(DCERPC_FAULT_CONTEXT_MISMATCH); } while (0)
498
499 /* this checks for a valid policy handle, and gives a fault if an
500    invalid handle or retval if the handle is of the
501    wrong type */
502 #define DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, retval) do { \
503         (h) = dcesrv_handle_lookup(dce_call, (inhandle), DCESRV_HANDLE_ANY); \
504         DCESRV_CHECK_HANDLE(h); \
505         if ((t) != DCESRV_HANDLE_ANY && (h)->wire_handle.handle_type != (t)) { \
506                 return retval; \
507         } \
508 } while (0)
509
510 /* this checks for a valid policy handle and gives a dcerpc fault 
511    if its the wrong type of handle */
512 #define DCESRV_PULL_HANDLE_FAULT(h, inhandle, t) do { \
513         (h) = dcesrv_handle_lookup(dce_call, (inhandle), t); \
514         DCESRV_CHECK_HANDLE(h); \
515 } while (0)
516
517 #define DCESRV_PULL_HANDLE(h, inhandle, t) DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, NT_STATUS_INVALID_HANDLE)
518 #define DCESRV_PULL_HANDLE_WERR(h, inhandle, t) DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, WERR_INVALID_HANDLE)
519
520 NTSTATUS dcesrv_add_ep(struct dcesrv_context *dce_ctx,
521                        struct loadparm_context *lp_ctx,
522                        struct dcesrv_endpoint *e,
523                        struct tevent_context *event_ctx,
524                        const struct model_ops *model_ops,
525                        void *process_context);
526
527 /**
528  * retrieve credentials from a dce_call
529  */
530 _PUBLIC_ struct cli_credentials *dcesrv_call_credentials(struct dcesrv_call_state *dce_call);
531
532 /**
533  * returns true if this is an authenticated call
534  */
535 _PUBLIC_ bool dcesrv_call_authenticated(struct dcesrv_call_state *dce_call);
536
537 /**
538  * retrieve account_name for a dce_call
539  */
540 _PUBLIC_ const char *dcesrv_call_account_name(struct dcesrv_call_state *dce_call);
541
542 /**
543  * retrieve session_info from a dce_call
544  */
545 _PUBLIC_ struct auth_session_info *dcesrv_call_session_info(struct dcesrv_call_state *dce_call);
546
547 /**
548  * retrieve auth type/level from a dce_call
549  */
550 _PUBLIC_ void dcesrv_call_auth_info(struct dcesrv_call_state *dce_call,
551                                     enum dcerpc_AuthType *auth_type,
552                                     enum dcerpc_AuthLevel *auth_level);
553
554 _PUBLIC_ NTSTATUS dcesrv_interface_bind_require_integrity(struct dcesrv_connection_context *context,
555                                                           const struct dcesrv_interface *iface);
556 _PUBLIC_ NTSTATUS dcesrv_interface_bind_require_privacy(struct dcesrv_connection_context *context,
557                                                         const struct dcesrv_interface *iface);
558 _PUBLIC_ NTSTATUS dcesrv_interface_bind_reject_connect(struct dcesrv_connection_context *context,
559                                                        const struct dcesrv_interface *iface);
560 _PUBLIC_ NTSTATUS dcesrv_interface_bind_allow_connect(struct dcesrv_connection_context *context,
561                                                       const struct dcesrv_interface *iface);
562
563 _PUBLIC_ NTSTATUS _dcesrv_iface_state_store_assoc(
564                 struct dcesrv_call_state *call,
565                 uint64_t magic,
566                 void *ptr,
567                 const char *location);
568 #define dcesrv_iface_state_store_assoc(call, magic, ptr) \
569         _dcesrv_iface_state_store_assoc((call), (magic), (ptr), \
570                                         __location__)
571 _PUBLIC_ void *_dcesrv_iface_state_find_assoc(
572                 struct dcesrv_call_state *call,
573                 uint64_t magic);
574 #define dcesrv_iface_state_find_assoc(call, magic, _type) \
575         talloc_get_type( \
576                 _dcesrv_iface_state_find_assoc((call), (magic)), \
577                 _type)
578
579 _PUBLIC_ NTSTATUS _dcesrv_iface_state_store_conn(
580                 struct dcesrv_call_state *call,
581                 uint64_t magic,
582                 void *_pptr,
583                 const char *location);
584 #define dcesrv_iface_state_store_conn(call, magic, ptr) \
585         _dcesrv_iface_state_store_conn((call), (magic), (ptr), \
586                                         __location__)
587 _PUBLIC_ void *_dcesrv_iface_state_find_conn(
588                 struct dcesrv_call_state *call,
589                 uint64_t magic);
590 #define dcesrv_iface_state_find_conn(call, magic, _type) \
591         talloc_get_type( \
592                 _dcesrv_iface_state_find_conn((call), (magic)), \
593                 _type)
594
595 _PUBLIC_ struct imessaging_context *dcesrv_imessaging_context(
596                                        struct dcesrv_connection *conn);
597 _PUBLIC_ struct server_id dcesrv_server_id(struct dcesrv_connection *conn);
598
599 #endif /* SAMBA_DCERPC_SERVER_H */