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