s4-rpc_server: Allow listener for RPC servers to use multiple processes
[metze/samba/wip.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
42 struct dcesrv_interface {
43         const char *name;
44         struct ndr_syntax_id syntax_id;
45
46         /* this function is called when the client binds to this interface  */
47         NTSTATUS (*bind)(struct dcesrv_call_state *, const struct dcesrv_interface *, uint32_t if_version);
48
49         /* this function is called when the client disconnects the endpoint */
50         void (*unbind)(struct dcesrv_connection_context *, const struct dcesrv_interface *);
51
52         /* the ndr_pull function for the chosen interface.
53          */
54         NTSTATUS (*ndr_pull)(struct dcesrv_call_state *, TALLOC_CTX *, struct ndr_pull *, void **);
55         
56         /* the dispatch function for the chosen interface.
57          */
58         NTSTATUS (*dispatch)(struct dcesrv_call_state *, TALLOC_CTX *, void *);
59
60         /* the reply function for the chosen interface.
61          */
62         NTSTATUS (*reply)(struct dcesrv_call_state *, TALLOC_CTX *, void *);
63
64         /* the ndr_push function for the chosen interface.
65          */
66         NTSTATUS (*ndr_push)(struct dcesrv_call_state *, TALLOC_CTX *, struct ndr_push *, const void *);
67
68         /* for any private use by the interface code */
69         const void *private_data;
70
71         uint64_t flags;
72 };
73
74 #define DCESRV_INTERFACE_FLAGS_HANDLES_NOT_USED 0x00000001
75
76 enum dcesrv_call_list {
77         DCESRV_LIST_NONE,
78         DCESRV_LIST_CALL_LIST,
79         DCESRV_LIST_FRAGMENTED_CALL_LIST,
80         DCESRV_LIST_PENDING_CALL_LIST
81 };
82
83 struct data_blob_list_item {
84         struct data_blob_list_item *prev,*next;
85         DATA_BLOB blob;
86 };
87
88 /* the state of an ongoing dcerpc call */
89 struct dcesrv_call_state {
90         struct dcesrv_call_state *next, *prev;
91         struct dcesrv_connection *conn;
92         struct dcesrv_connection_context *context;
93         struct ncacn_packet pkt;
94
95         /*
96           which list this request is in, if any
97          */
98         enum dcesrv_call_list list;
99
100         /* the backend can mark the call
101          * with DCESRV_CALL_STATE_FLAG_ASYNC
102          * that will cause the frontend to not touch r->out
103          * and skip the reply
104          *
105          * this is only allowed to the backend when DCESRV_CALL_STATE_FLAG_MAY_ASYNC
106          * is alerady set by the frontend
107          *
108          * the backend then needs to call dcesrv_reply() when it's
109          * ready to send the reply
110          */
111 #define DCESRV_CALL_STATE_FLAG_ASYNC (1<<0)
112 #define DCESRV_CALL_STATE_FLAG_MAY_ASYNC (1<<1)
113 #define DCESRV_CALL_STATE_FLAG_MULTIPLEXED (1<<3)
114 #define DCESRV_CALL_STATE_FLAG_PROCESS_PENDING_CALL (1<<4)
115         uint32_t state_flags;
116
117         /* the time the request arrived in the server */
118         struct timeval time;
119
120         /* the backend can use this event context for async replies */
121         struct tevent_context *event_ctx;
122
123         /* the message_context that will be used for async replies */
124         struct imessaging_context *msg_ctx;
125
126         /* this is the pointer to the allocated function struct */
127         void *r;
128
129         /*
130          * that's the ndr pull context used in dcesrv_request()
131          * needed by dcesrv_reply() to carry over information
132          * for full pointer support.
133          */
134         struct ndr_pull *ndr_pull;
135
136         DATA_BLOB input;
137
138         struct data_blob_list_item *replies;
139
140         /* this is used by the boilerplate code to generate DCERPC faults */
141         uint32_t fault_code;
142
143         /* the reason why we terminate the connection after sending a response */
144         const char *terminate_reason;
145
146         /* temporary auth_info fields */
147         struct dcerpc_auth in_auth_info;
148         struct dcerpc_auth _out_auth_info;
149         struct dcerpc_auth *out_auth_info;
150 };
151
152 #define DCESRV_HANDLE_ANY 255
153
154 /* a dcerpc handle in internal format */
155 struct dcesrv_handle {
156         struct dcesrv_handle *next, *prev;
157         struct dcesrv_assoc_group *assoc_group;
158         struct policy_handle wire_handle;
159         struct dom_sid *sid;
160         const struct dcesrv_interface *iface;
161         void *data;
162 };
163
164 /* hold the authentication state information */
165 struct dcesrv_auth {
166         enum dcerpc_AuthType auth_type;
167         enum dcerpc_AuthLevel auth_level;
168         uint32_t auth_context_id;
169         struct gensec_security *gensec_security;
170         struct auth_session_info *session_info;
171         NTSTATUS (*session_key)(struct dcesrv_connection *, DATA_BLOB *session_key);
172         bool client_hdr_signing;
173         bool hdr_signing;
174         bool auth_finished;
175         bool auth_invalid;
176 };
177
178 struct dcesrv_connection_context {
179         struct dcesrv_connection_context *next, *prev;
180         uint16_t context_id;
181
182         /* the connection this is on */
183         struct dcesrv_connection *conn;
184
185         /* the ndr function table for the chosen interface */
186         const struct dcesrv_interface *iface;
187
188         /* private data for the interface implementation */
189         void *private_data;
190
191         /*
192          * the minimum required auth level for this interface
193          */
194         enum dcerpc_AuthLevel min_auth_level;
195         bool allow_connect;
196
197         /* the negotiated transfer syntax */
198         struct ndr_syntax_id transfer_syntax;
199 };
200
201
202 /* the state associated with a dcerpc server connection */
203 struct dcesrv_connection {
204         /* for the broken_connections DLIST */
205         struct dcesrv_connection *prev, *next;
206
207         /* the top level context for this server */
208         struct dcesrv_context *dce_ctx;
209
210         /* the endpoint that was opened */
211         const struct dcesrv_endpoint *endpoint;
212
213         /* a list of established context_ids */
214         struct dcesrv_connection_context *contexts;
215
216         /* the state of the current incoming call fragments */
217         struct dcesrv_call_state *incoming_fragmented_call_list;
218
219         /* the state of the async pending calls */
220         struct dcesrv_call_state *pending_call_list;
221
222         /* the state of the current outgoing calls */
223         struct dcesrv_call_state *call_list;
224
225         /* the maximum size the client wants to receive */
226         uint16_t max_recv_frag;
227         uint16_t max_xmit_frag;
228
229         DATA_BLOB partial_input;
230
231         /* the event_context that will be used for this connection */
232         struct tevent_context *event_ctx;
233
234         /* the message_context that will be used for this connection */
235         struct imessaging_context *msg_ctx;
236
237         /* the server_id that will be used for this connection */
238         struct server_id server_id;
239
240         /* the transport level session key */
241         DATA_BLOB transport_session_key;
242
243         /* is this connection pending termination?  If so, why? */
244         const char *terminate;
245
246         const char *packet_log_dir;
247
248         /* this is the default state_flags for dcesrv_call_state structs */
249         uint32_t state_flags;
250
251         struct {
252                 void *private_data;
253                 void (*report_output_data)(struct dcesrv_connection *);
254         } transport;
255
256         struct tstream_context *stream;
257         struct tevent_queue *send_queue;
258
259         const struct tsocket_address *local_address;
260         const struct tsocket_address *remote_address;
261
262         /* the current authentication state */
263         struct dcesrv_auth auth_state;
264
265         /*
266          * remember which pdu types are allowed
267          */
268         bool allow_bind;
269         bool allow_auth3;
270         bool allow_alter;
271         bool allow_request;
272
273         /* the association group the connection belongs to */
274         struct dcesrv_assoc_group *assoc_group;
275
276         /* The maximum total payload of reassembled request pdus */
277         size_t max_total_request_size;
278
279         /*
280          * Our preferred transfer syntax.
281          */
282         const struct ndr_syntax_id *preferred_transfer;
283
284         /* the negotiated bind time features */
285         uint16_t bind_time_features;
286 };
287
288
289 struct dcesrv_endpoint_server {
290         /* this is the name of the endpoint server */
291         const char *name;
292
293         /* this function should register endpoints and some other setup stuff,
294          * it is called when the dcesrv_context gets initialized.
295          */
296         NTSTATUS (*init_server)(struct dcesrv_context *, const struct dcesrv_endpoint_server *);
297
298         /* this function can be used by other endpoint servers to
299          * ask for a dcesrv_interface implementation
300          * - iface must be reference to an already existing struct !
301          */
302         bool (*interface_by_uuid)(struct dcesrv_interface *iface, const struct GUID *, uint32_t);
303
304         /* this function can be used by other endpoint servers to
305          * ask for a dcesrv_interface implementation
306          * - iface must be reference to an already existeng struct !
307          */
308         bool (*interface_by_name)(struct dcesrv_interface *iface, const char *);
309 };
310
311
312 /* one association groups */
313 struct dcesrv_assoc_group {
314         /* the wire id */
315         uint32_t id;
316         
317         /* list of handles in this association group */
318         struct dcesrv_handle *handles;
319
320         /* parent context */
321         struct dcesrv_context *dce_ctx;
322
323         /* Remote association group ID (if proxied) */
324         uint32_t proxied_id;
325 };
326
327 /* server-wide context information for the dcerpc server */
328 struct dcesrv_context {
329         /*
330          * The euid at startup time.
331          *
332          * This is required for DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM
333          */
334         uid_t initial_euid;
335
336         /* the list of endpoints that have registered 
337          * by the configured endpoint servers 
338          */
339         struct dcesrv_endpoint {
340                 struct dcesrv_endpoint *next, *prev;
341                 /* the type and location of the endpoint */
342                 struct dcerpc_binding *ep_description;
343                 /* the security descriptor for smb named pipes */
344                 struct security_descriptor *sd;
345                 /* the list of interfaces available on this endpoint */
346                 struct dcesrv_if_list {
347                         struct dcesrv_if_list *next, *prev;
348                         struct dcesrv_interface iface;
349                 } *interface_list;
350
351                 /*
352                  * Should this service be run in a single process (so far only
353                  * NETLOGON is not run in a single process)
354                  */
355                 bool use_single_process;
356         } *endpoint_list;
357
358         /* loadparm context to use for this connection */
359         struct loadparm_context *lp_ctx;
360
361         struct idr_context *assoc_groups_idr;
362
363         struct dcesrv_connection *broken_connections;
364 };
365
366 /* this structure is used by modules to determine the size of some critical types */
367 struct dcesrv_critical_sizes {
368         int interface_version;
369         int sizeof_dcesrv_context;
370         int sizeof_dcesrv_endpoint;
371         int sizeof_dcesrv_endpoint_server;
372         int sizeof_dcesrv_interface;
373         int sizeof_dcesrv_if_list;
374         int sizeof_dcesrv_connection;
375         int sizeof_dcesrv_call_state;
376         int sizeof_dcesrv_auth;
377         int sizeof_dcesrv_handle;
378 };
379
380 struct model_ops;
381
382 NTSTATUS dcesrv_interface_register(struct dcesrv_context *dce_ctx,
383                                    const char *ep_name,
384                                    const struct dcesrv_interface *iface,
385                                    const struct security_descriptor *sd);
386 NTSTATUS dcerpc_register_ep_server(const struct dcesrv_endpoint_server *ep_server);
387 NTSTATUS dcesrv_init_context(TALLOC_CTX *mem_ctx, 
388                                       struct loadparm_context *lp_ctx,
389                                       const char **endpoint_servers, struct dcesrv_context **_dce_ctx);
390 NTSTATUS dcesrv_endpoint_connect(struct dcesrv_context *dce_ctx,
391                                  TALLOC_CTX *mem_ctx,
392                                  const struct dcesrv_endpoint *ep,
393                                  struct auth_session_info *session_info,
394                                  struct tevent_context *event_ctx,
395                                  struct imessaging_context *msg_ctx,
396                                  struct server_id server_id,
397                                  uint32_t state_flags,
398                                  struct dcesrv_connection **_p);
399
400 NTSTATUS dcesrv_reply(struct dcesrv_call_state *call);
401 struct dcesrv_handle *dcesrv_handle_new(struct dcesrv_connection_context *context, 
402                                         uint8_t handle_type);
403
404 struct dcesrv_handle *dcesrv_handle_fetch(
405                                           struct dcesrv_connection_context *context, 
406                                           struct policy_handle *p,
407                                           uint8_t handle_type);
408 struct socket_address *dcesrv_connection_get_my_addr(struct dcesrv_connection *conn, TALLOC_CTX *mem_ctx);
409
410 struct socket_address *dcesrv_connection_get_peer_addr(struct dcesrv_connection *conn, TALLOC_CTX *mem_ctx);
411 const struct tsocket_address *dcesrv_connection_get_local_address(struct dcesrv_connection *conn);
412 const struct tsocket_address *dcesrv_connection_get_remote_address(struct dcesrv_connection *conn);
413
414 NTSTATUS dcesrv_fetch_session_key(struct dcesrv_connection *p, DATA_BLOB *session_key);
415
416 /* a useful macro for generating a RPC fault in the backend code */
417 #define DCESRV_FAULT(code) do { \
418         dce_call->fault_code = code; \
419         return r->out.result; \
420 } while(0)
421
422 /* a useful macro for generating a RPC fault in the backend code */
423 #define DCESRV_FAULT_VOID(code) do { \
424         dce_call->fault_code = code; \
425         return; \
426 } while(0)
427
428 /* a useful macro for checking the validity of a dcerpc policy handle
429    and giving the right fault code if invalid */
430 #define DCESRV_CHECK_HANDLE(h) do {if (!(h)) DCESRV_FAULT(DCERPC_FAULT_CONTEXT_MISMATCH); } while (0)
431
432 /* this checks for a valid policy handle, and gives a fault if an
433    invalid handle or retval if the handle is of the
434    wrong type */
435 #define DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, retval) do { \
436         (h) = dcesrv_handle_fetch(dce_call->context, (inhandle), DCESRV_HANDLE_ANY); \
437         DCESRV_CHECK_HANDLE(h); \
438         if ((t) != DCESRV_HANDLE_ANY && (h)->wire_handle.handle_type != (t)) { \
439                 return retval; \
440         } \
441 } while (0)
442
443 /* this checks for a valid policy handle and gives a dcerpc fault 
444    if its the wrong type of handle */
445 #define DCESRV_PULL_HANDLE_FAULT(h, inhandle, t) do { \
446         (h) = dcesrv_handle_fetch(dce_call->context, (inhandle), t); \
447         DCESRV_CHECK_HANDLE(h); \
448 } while (0)
449
450 #define DCESRV_PULL_HANDLE(h, inhandle, t) DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, NT_STATUS_INVALID_HANDLE)
451 #define DCESRV_PULL_HANDLE_WERR(h, inhandle, t) DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, WERR_INVALID_HANDLE)
452
453 NTSTATUS dcesrv_add_ep(struct dcesrv_context *dce_ctx,
454                        struct loadparm_context *lp_ctx,
455                        struct dcesrv_endpoint *e,
456                        struct tevent_context *event_ctx,
457                        const struct model_ops *model_ops);
458
459 /**
460  * retrieve credentials from a dce_call
461  */
462 _PUBLIC_ struct cli_credentials *dcesrv_call_credentials(struct dcesrv_call_state *dce_call);
463
464 /**
465  * returns true if this is an authenticated call
466  */
467 _PUBLIC_ bool dcesrv_call_authenticated(struct dcesrv_call_state *dce_call);
468
469 /**
470  * retrieve account_name for a dce_call
471  */
472 _PUBLIC_ const char *dcesrv_call_account_name(struct dcesrv_call_state *dce_call);
473
474 _PUBLIC_ NTSTATUS dcesrv_interface_bind_require_integrity(struct dcesrv_call_state *dce_call,
475                                                           const struct dcesrv_interface *iface);
476 _PUBLIC_ NTSTATUS dcesrv_interface_bind_require_privacy(struct dcesrv_call_state *dce_call,
477                                                         const struct dcesrv_interface *iface);
478 _PUBLIC_ NTSTATUS dcesrv_interface_bind_reject_connect(struct dcesrv_call_state *dce_call,
479                                                        const struct dcesrv_interface *iface);
480 _PUBLIC_ NTSTATUS dcesrv_interface_bind_allow_connect(struct dcesrv_call_state *dce_call,
481                                                       const struct dcesrv_interface *iface);
482
483 #endif /* SAMBA_DCERPC_SERVER_H */