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