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