f9a0f00288dcd6b4e0cb30f16bf3999b84d1717b
[jelmer/samba4-debian.git] / source / 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/misc.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 *);
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;
70 };
71
72 enum dcesrv_call_list {
73         DCESRV_LIST_NONE,
74         DCESRV_LIST_CALL_LIST,
75         DCESRV_LIST_FRAGMENTED_CALL_LIST,
76         DCESRV_LIST_PENDING_CALL_LIST
77 };
78
79 /* the state of an ongoing dcerpc call */
80 struct dcesrv_call_state {
81         struct dcesrv_call_state *next, *prev;
82         struct dcesrv_connection *conn;
83         struct dcesrv_connection_context *context;
84         struct ncacn_packet pkt;
85
86         /*
87           which list this request is in, if any
88          */
89         enum dcesrv_call_list list;
90
91         /* the backend can mark the call
92          * with DCESRV_CALL_STATE_FLAG_ASYNC
93          * that will cause the frontend to not touch r->out
94          * and skip the reply
95          *
96          * this is only allowed to the backend when DCESRV_CALL_STATE_FLAG_MAY_ASYNC
97          * is alerady set by the frontend
98          *
99          * the backend then needs to call dcesrv_reply() when it's
100          * ready to send the reply
101          */
102 #define DCESRV_CALL_STATE_FLAG_ASYNC (1<<0)
103 #define DCESRV_CALL_STATE_FLAG_MAY_ASYNC (1<<1)
104         uint32_t state_flags;
105
106         /* the time the request arrived in the server */
107         struct timeval time;
108
109         /* the backend can use this event context for async replies */
110         struct event_context *event_ctx;
111
112         /* the message_context that will be used for async replies */
113         struct messaging_context *msg_ctx;
114
115         /* this is the pointer to the allocated function struct */
116         void *r;
117
118         /*
119          * that's the ndr pull context used in dcesrv_request()
120          * needed by dcesrv_reply() to carry over information
121          * for full pointer support.
122          */
123         struct ndr_pull *ndr_pull;
124
125         DATA_BLOB input;
126
127         struct data_blob_list_item *replies;
128
129         /* this is used by the boilerplate code to generate DCERPC faults */
130         uint32_t fault_code;
131 };
132
133 #define DCESRV_HANDLE_ANY 255
134
135 /* a dcerpc handle in internal format */
136 struct dcesrv_handle {
137         struct dcesrv_handle *next, *prev;
138         struct dcesrv_connection_context *context;
139         struct policy_handle wire_handle;
140         void *data;
141 };
142
143 /* hold the authentication state information */
144 struct dcesrv_auth {
145         struct dcerpc_auth *auth_info;
146         struct gensec_security *gensec_security;
147         struct auth_session_info *session_info;
148         NTSTATUS (*session_key)(struct dcesrv_connection *, DATA_BLOB *session_key);
149 };
150
151 struct dcesrv_connection_context {
152         struct dcesrv_connection_context *next, *prev;
153         uint32_t context_id;
154
155         /* the connection this is on */
156         struct dcesrv_connection *conn;
157
158         /* the ndr function table for the chosen interface */
159         const struct dcesrv_interface *iface;
160
161         /* private data for the interface implementation */
162         void *private;
163
164         /* current rpc handles - this is really the wrong scope for
165            them, but it will do for now */
166         struct dcesrv_handle *handles;
167 };
168
169
170 /* the state associated with a dcerpc server connection */
171 struct dcesrv_connection {
172         /* the top level context for this server */
173         struct dcesrv_context *dce_ctx;
174
175         /* the endpoint that was opened */
176         const struct dcesrv_endpoint *endpoint;
177
178         /* a list of established context_ids */
179         struct dcesrv_connection_context *contexts;
180
181         /* the state of the current incoming call fragments */
182         struct dcesrv_call_state *incoming_fragmented_call_list;
183
184         /* the state of the async pending calls */
185         struct dcesrv_call_state *pending_call_list;
186
187         /* the state of the current outgoing calls */
188         struct dcesrv_call_state *call_list;
189
190         /* the maximum size the client wants to receive */
191         uint32_t cli_max_recv_frag;
192
193         DATA_BLOB partial_input;
194
195         /* the current authentication state */
196         struct dcesrv_auth auth_state;
197
198         /* the event_context that will be used for this connection */
199         struct event_context *event_ctx;
200
201         /* the message_context that will be used for this connection */
202         struct messaging_context *msg_ctx;
203
204         /* the server_id that will be used for this connection */
205         struct server_id server_id;
206
207         /* the transport level session key */
208         DATA_BLOB transport_session_key;
209
210         bool processing;
211
212         /* this is the default state_flags for dcesrv_call_state structs */
213         uint32_t state_flags;
214
215         struct {
216                 void *private_data;
217                 void (*report_output_data)(struct dcesrv_connection *);
218                 struct socket_address *(*get_my_addr)(struct dcesrv_connection *, TALLOC_CTX *mem_ctx);
219                 struct socket_address *(*get_peer_addr)(struct dcesrv_connection *, TALLOC_CTX *mem_ctx);
220         } transport;
221 };
222
223
224 struct dcesrv_endpoint_server {
225         /* this is the name of the endpoint server */
226         const char *name;
227
228         /* this function should register endpoints and some other setup stuff,
229          * it is called when the dcesrv_context gets initialized.
230          */
231         NTSTATUS (*init_server)(struct dcesrv_context *, const struct dcesrv_endpoint_server *);
232
233         /* this function can be used by other endpoint servers to
234          * ask for a dcesrv_interface implementation
235          * - iface must be reference to an already existing struct !
236          */
237         bool (*interface_by_uuid)(struct dcesrv_interface *iface, const struct GUID *, uint32_t);
238
239         /* this function can be used by other endpoint servers to
240          * ask for a dcesrv_interface implementation
241          * - iface must be reference to an already existeng struct !
242          */
243         bool (*interface_by_name)(struct dcesrv_interface *iface, const char *);
244 };
245
246
247 /* server-wide context information for the dcerpc server */
248 struct dcesrv_context {
249         /* the list of endpoints that have registered 
250          * by the configured endpoint servers 
251          */
252         struct dcesrv_endpoint {
253                 struct dcesrv_endpoint *next, *prev;
254                 /* the type and location of the endpoint */
255                 struct dcerpc_binding *ep_description;
256                 /* the security descriptor for smb named pipes */
257                 struct security_descriptor *sd;
258                 /* the list of interfaces available on this endpoint */
259                 struct dcesrv_if_list {
260                         struct dcesrv_if_list *next, *prev;
261                         struct dcesrv_interface iface;
262                 } *interface_list;
263         } *endpoint_list;
264 };
265
266 /* this structure is used by modules to determine the size of some critical types */
267 struct dcesrv_critical_sizes {
268         int interface_version;
269         int sizeof_dcesrv_context;
270         int sizeof_dcesrv_endpoint;
271         int sizeof_dcesrv_endpoint_server;
272         int sizeof_dcesrv_interface;
273         int sizeof_dcesrv_if_list;
274         int sizeof_dcesrv_connection;
275         int sizeof_dcesrv_call_state;
276         int sizeof_dcesrv_auth;
277         int sizeof_dcesrv_handle;
278 };
279
280 struct model_ops;
281
282 #include "rpc_server/dcerpc_server_proto.h"
283
284 #endif /* SAMBA_DCERPC_SERVER_H */