Merge branch 'master' of ssh://git.samba.org/data/git/samba
[kai/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 *);
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
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 #define DCESRV_CALL_STATE_FLAG_HEADER_SIGNING (1<<2)
105         uint32_t state_flags;
106
107         /* the time the request arrived in the server */
108         struct timeval time;
109
110         /* the backend can use this event context for async replies */
111         struct tevent_context *event_ctx;
112
113         /* the message_context that will be used for async replies */
114         struct messaging_context *msg_ctx;
115
116         /* this is the pointer to the allocated function struct */
117         void *r;
118
119         /*
120          * that's the ndr pull context used in dcesrv_request()
121          * needed by dcesrv_reply() to carry over information
122          * for full pointer support.
123          */
124         struct ndr_pull *ndr_pull;
125
126         DATA_BLOB input;
127
128         struct data_blob_list_item *replies;
129
130         /* this is used by the boilerplate code to generate DCERPC faults */
131         uint32_t fault_code;
132 };
133
134 #define DCESRV_HANDLE_ANY 255
135
136 /* a dcerpc handle in internal format */
137 struct dcesrv_handle {
138         struct dcesrv_handle *next, *prev;
139         struct dcesrv_connection_context *context;
140         struct policy_handle wire_handle;
141         void *data;
142 };
143
144 /* hold the authentication state information */
145 struct dcesrv_auth {
146         struct dcerpc_auth *auth_info;
147         struct gensec_security *gensec_security;
148         struct auth_session_info *session_info;
149         NTSTATUS (*session_key)(struct dcesrv_connection *, DATA_BLOB *session_key);
150 };
151
152 struct dcesrv_connection_context {
153         struct dcesrv_connection_context *next, *prev;
154         uint32_t context_id;
155
156         uint32_t assoc_group_id;
157
158         /* the connection this is on */
159         struct dcesrv_connection *conn;
160
161         /* the ndr function table for the chosen interface */
162         const struct dcesrv_interface *iface;
163
164         /* private data for the interface implementation */
165         void *private_data;
166
167         /* current rpc handles - this is really the wrong scope for
168            them, but it will do for now */
169         struct dcesrv_handle *handles;
170 };
171
172
173 /* the state associated with a dcerpc server connection */
174 struct dcesrv_connection {
175         /* the top level context for this server */
176         struct dcesrv_context *dce_ctx;
177
178         /* the endpoint that was opened */
179         const struct dcesrv_endpoint *endpoint;
180
181         /* a list of established context_ids */
182         struct dcesrv_connection_context *contexts;
183
184         /* the state of the current incoming call fragments */
185         struct dcesrv_call_state *incoming_fragmented_call_list;
186
187         /* the state of the async pending calls */
188         struct dcesrv_call_state *pending_call_list;
189
190         /* the state of the current outgoing calls */
191         struct dcesrv_call_state *call_list;
192
193         /* the maximum size the client wants to receive */
194         uint32_t cli_max_recv_frag;
195
196         DATA_BLOB partial_input;
197
198         /* the current authentication state */
199         struct dcesrv_auth auth_state;
200
201         /* the event_context that will be used for this connection */
202         struct tevent_context *event_ctx;
203
204         /* the message_context that will be used for this connection */
205         struct messaging_context *msg_ctx;
206
207         /* the server_id that will be used for this connection */
208         struct server_id server_id;
209
210         /* the transport level session key */
211         DATA_BLOB transport_session_key;
212
213         bool processing;
214
215         const char *packet_log_dir;
216
217         /* this is the default state_flags for dcesrv_call_state structs */
218         uint32_t state_flags;
219
220         struct {
221                 void *private_data;
222                 void (*report_output_data)(struct dcesrv_connection *);
223                 struct socket_address *(*get_my_addr)(struct dcesrv_connection *, TALLOC_CTX *mem_ctx);
224                 struct socket_address *(*get_peer_addr)(struct dcesrv_connection *, TALLOC_CTX *mem_ctx);
225         } transport;
226 };
227
228
229 struct dcesrv_endpoint_server {
230         /* this is the name of the endpoint server */
231         const char *name;
232
233         /* this function should register endpoints and some other setup stuff,
234          * it is called when the dcesrv_context gets initialized.
235          */
236         NTSTATUS (*init_server)(struct dcesrv_context *, const struct dcesrv_endpoint_server *);
237
238         /* this function can be used by other endpoint servers to
239          * ask for a dcesrv_interface implementation
240          * - iface must be reference to an already existing struct !
241          */
242         bool (*interface_by_uuid)(struct dcesrv_interface *iface, const struct GUID *, uint32_t);
243
244         /* this function can be used by other endpoint servers to
245          * ask for a dcesrv_interface implementation
246          * - iface must be reference to an already existeng struct !
247          */
248         bool (*interface_by_name)(struct dcesrv_interface *iface, const char *);
249 };
250
251
252 /* server-wide context information for the dcerpc server */
253 struct dcesrv_context {
254         /* the list of endpoints that have registered 
255          * by the configured endpoint servers 
256          */
257         struct dcesrv_endpoint {
258                 struct dcesrv_endpoint *next, *prev;
259                 /* the type and location of the endpoint */
260                 struct dcerpc_binding *ep_description;
261                 /* the security descriptor for smb named pipes */
262                 struct security_descriptor *sd;
263                 /* the list of interfaces available on this endpoint */
264                 struct dcesrv_if_list {
265                         struct dcesrv_if_list *next, *prev;
266                         struct dcesrv_interface iface;
267                 } *interface_list;
268         } *endpoint_list;
269
270         /* loadparm context to use for this connection */
271         struct loadparm_context *lp_ctx;
272 };
273
274 /* this structure is used by modules to determine the size of some critical types */
275 struct dcesrv_critical_sizes {
276         int interface_version;
277         int sizeof_dcesrv_context;
278         int sizeof_dcesrv_endpoint;
279         int sizeof_dcesrv_endpoint_server;
280         int sizeof_dcesrv_interface;
281         int sizeof_dcesrv_if_list;
282         int sizeof_dcesrv_connection;
283         int sizeof_dcesrv_call_state;
284         int sizeof_dcesrv_auth;
285         int sizeof_dcesrv_handle;
286 };
287
288 struct model_ops;
289
290 NTSTATUS dcesrv_interface_register(struct dcesrv_context *dce_ctx,
291                                    const char *ep_name,
292                                    const struct dcesrv_interface *iface,
293                                    const struct security_descriptor *sd);
294 NTSTATUS dcerpc_register_ep_server(const void *_ep_server);
295 NTSTATUS dcesrv_init_context(TALLOC_CTX *mem_ctx, 
296                                       struct loadparm_context *lp_ctx,
297                                       const char **endpoint_servers, struct dcesrv_context **_dce_ctx);
298 NTSTATUS dcesrv_init_ipc_context(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx,
299                                           struct dcesrv_context **_dce_ctx);
300 NTSTATUS dcesrv_endpoint_search_connect(struct dcesrv_context *dce_ctx,
301                                         TALLOC_CTX *mem_ctx,
302                                         const struct dcerpc_binding *ep_description,
303                                         struct auth_session_info *session_info,
304                                         struct tevent_context *event_ctx,
305                                         struct messaging_context *msg_ctx,
306                                         struct server_id server_id,
307                                         uint32_t state_flags,
308                                         struct dcesrv_connection **dce_conn_p);
309 NTSTATUS dcesrv_output(struct dcesrv_connection *dce_conn, 
310                        void *private_data,
311                        NTSTATUS (*write_fn)(void *private_data, DATA_BLOB *output, size_t *nwritten));
312 NTSTATUS dcesrv_input(struct dcesrv_connection *dce_conn, const DATA_BLOB *data);
313 NTSTATUS dcesrv_endpoint_connect(struct dcesrv_context *dce_ctx,
314                                  TALLOC_CTX *mem_ctx,
315                                  const struct dcesrv_endpoint *ep,
316                                  struct auth_session_info *session_info,
317                                  struct tevent_context *event_ctx,
318                                  struct messaging_context *msg_ctx,
319                                  struct server_id server_id,
320                                  uint32_t state_flags,
321                                  struct dcesrv_connection **_p);
322
323 NTSTATUS dcesrv_reply(struct dcesrv_call_state *call);
324 struct dcesrv_handle *dcesrv_handle_new(struct dcesrv_connection_context *context, 
325                                         uint8_t handle_type);
326
327 struct dcesrv_handle *dcesrv_handle_fetch(
328                                           struct dcesrv_connection_context *context, 
329                                           struct policy_handle *p,
330                                           uint8_t handle_type);
331 struct socket_address *dcesrv_connection_get_my_addr(struct dcesrv_connection *conn, TALLOC_CTX *mem_ctx);
332
333 struct socket_address *dcesrv_connection_get_peer_addr(struct dcesrv_connection *conn, TALLOC_CTX *mem_ctx);
334
335 NTSTATUS dcesrv_fetch_session_key(struct dcesrv_connection *p, DATA_BLOB *session_key);
336
337 /* a useful macro for generating a RPC fault in the backend code */
338 #define DCESRV_FAULT(code) do { \
339         dce_call->fault_code = code; \
340         return r->out.result; \
341 } while(0)
342
343 /* a useful macro for generating a RPC fault in the backend code */
344 #define DCESRV_FAULT_VOID(code) do { \
345         dce_call->fault_code = code; \
346         return; \
347 } while(0)
348
349 /* a useful macro for checking the validity of a dcerpc policy handle
350    and giving the right fault code if invalid */
351 #define DCESRV_CHECK_HANDLE(h) do {if (!(h)) DCESRV_FAULT(DCERPC_FAULT_CONTEXT_MISMATCH); } while (0)
352
353 /* this checks for a valid policy handle, and gives a fault if an
354    invalid handle or retval if the handle is of the
355    wrong type */
356 #define DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, retval) do { \
357         (h) = dcesrv_handle_fetch(dce_call->context, (inhandle), DCESRV_HANDLE_ANY); \
358         DCESRV_CHECK_HANDLE(h); \
359         if ((t) != DCESRV_HANDLE_ANY && (h)->wire_handle.handle_type != (t)) { \
360                 return retval; \
361         } \
362 } while (0)
363
364 /* this checks for a valid policy handle and gives a dcerpc fault 
365    if its the wrong type of handle */
366 #define DCESRV_PULL_HANDLE_FAULT(h, inhandle, t) do { \
367         (h) = dcesrv_handle_fetch(dce_call->context, (inhandle), t); \
368         DCESRV_CHECK_HANDLE(h); \
369 } while (0)
370
371 #define DCESRV_PULL_HANDLE(h, inhandle, t) DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, NT_STATUS_INVALID_HANDLE)
372 #define DCESRV_PULL_HANDLE_WERR(h, inhandle, t) DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, WERR_BADFID)
373
374
375
376 #endif /* SAMBA_DCERPC_SERVER_H */