r5102: This is a major simplification of the logic for controlling top level
[nivanova/samba-autobuild/.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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #ifndef SAMBA_DCERPC_SERVER_H
25 #define SAMBA_DCERPC_SERVER_H
26
27 /* modules can use the following to determine if the interface has changed
28  * please increment the version number after each interface change
29  * with a comment and maybe update struct dcesrv_critical_sizes.
30  */
31 /* version 1 - initial version - metze */
32 #define DCERPC_MODULE_VERSION 1
33
34 struct dcesrv_connection;
35 struct dcesrv_call_state;
36 struct dcesrv_auth;
37
38 struct dcesrv_interface {
39         const char *name;
40         const char *uuid;
41         uint32_t if_version;
42
43         /* this function is called when the client binds to this interface  */
44         NTSTATUS (*bind)(struct dcesrv_call_state *, const struct dcesrv_interface *);
45
46         /* this function is called when the client disconnects the endpoint */
47         void (*unbind)(struct dcesrv_connection_context *, const struct dcesrv_interface *);
48
49         /* the ndr_pull function for the chosen interface.
50          */
51         NTSTATUS (*ndr_pull)(struct dcesrv_call_state *, TALLOC_CTX *, struct ndr_pull *, void **);;
52         
53         /* the dispatch function for the chosen interface.
54          */
55         NTSTATUS (*dispatch)(struct dcesrv_call_state *, TALLOC_CTX *, void *);
56
57         /* the reply function for the chosen interface.
58          */
59         NTSTATUS (*reply)(struct dcesrv_call_state *, TALLOC_CTX *, void *);
60
61         /* the ndr_push function for the chosen interface.
62          */
63         NTSTATUS (*ndr_push)(struct dcesrv_call_state *, TALLOC_CTX *, struct ndr_push *,void *);
64
65         /* for any private use by the interface code */
66         const void *private;
67 };
68
69 /* the state of an ongoing dcerpc call */
70 struct dcesrv_call_state {
71         struct dcesrv_call_state *next, *prev;
72         struct dcesrv_connection *conn;
73         struct dcesrv_connection_context *context;
74         struct dcerpc_packet pkt;
75
76         /* the backend can mark the call
77          * with DCESRV_CALL_STATE_FLAG_ASYNC
78          * that will cause the frontend to not touch r->out
79          * and skip the reply
80          *
81          * this is only allowed to the backend when DCESRV_CALL_STATE_FLAG_MAY_ASYNC
82          * is alerady set by the frontend
83          *
84          * the backend then needs to call dcesrv_reply() when it's
85          * ready to send the reply
86          */
87 #define DCESRV_CALL_STATE_FLAG_ASYNC (1<<0)
88 #define DCESRV_CALL_STATE_FLAG_MAY_ASYNC (1<<1)
89         uint32_t state_flags;
90
91         /* the time the request arrived in the server */
92         struct timeval time;
93
94         /* the backend can use this event context for async replies */
95         struct event_context *event_ctx;
96
97         /* this is the pointer to the allocated function struct */
98         void *r;
99
100         /* that's the ndr push context used in dcesrv_request */
101         struct ndr_pull *ndr_pull;
102
103         DATA_BLOB input;
104
105         struct dcesrv_call_reply {
106                 struct dcesrv_call_reply *next, *prev;
107                 DATA_BLOB data;
108         } *replies;
109
110         /* this is used by the boilerplate code to generate DCERPC faults */
111         uint32_t fault_code;
112 };
113
114 #define DCESRV_HANDLE_ANY 255
115
116 /* a dcerpc handle in internal format */
117 struct dcesrv_handle {
118         struct dcesrv_handle *next, *prev;
119         struct dcesrv_connection_context *context;
120         struct policy_handle wire_handle;
121         void *data;
122         void (*destroy)(struct dcesrv_connection_context *, struct dcesrv_handle *);
123 };
124
125 /* hold the authentication state information */
126 struct dcesrv_auth {
127         struct dcerpc_auth *auth_info;
128         struct gensec_security *gensec_security;
129         struct auth_session_info *session_info;
130         NTSTATUS (*session_key)(struct dcesrv_connection *, DATA_BLOB *session_key);
131 };
132
133 struct dcesrv_connection_context {
134         struct dcesrv_connection_context *next, *prev;
135         uint32_t context_id;
136
137         /* the connection this is on */
138         struct dcesrv_connection *conn;
139
140         /* the ndr function table for the chosen interface */
141         const struct dcesrv_interface *iface;
142
143         /* private data for the interface implementation */
144         void *private;
145
146         /* current rpc handles - this is really the wrong scope for
147            them, but it will do for now */
148         struct dcesrv_handle *handles;
149 };
150
151
152 /* the state associated with a dcerpc server connection */
153 struct dcesrv_connection {
154         /* the top level context for this server */
155         struct dcesrv_context *dce_ctx;
156
157         /* the endpoint that was opened */
158         const struct dcesrv_endpoint *endpoint;
159
160         /* a list of established context_ids */
161         struct dcesrv_connection_context *contexts;
162
163         /* the state of the current calls */
164         struct dcesrv_call_state *call_list;
165
166         /* the state of the async pending calls */
167         struct dcesrv_call_state *pending_call_list;
168
169         /* the maximum size the client wants to receive */
170         uint32_t cli_max_recv_frag;
171
172         DATA_BLOB partial_input;
173
174         /* the current authentication state */
175         struct dcesrv_auth auth_state;
176
177         struct stream_connection *srv_conn;
178
179         /* the transport level session key */
180         DATA_BLOB transport_session_key;
181 };
182
183
184 struct dcesrv_endpoint_server {
185         /* this is the name of the endpoint server */
186         const char *name;
187
188         /* this function should register endpoints and some other setup stuff,
189          * it is called when the dcesrv_context gets initialized.
190          */
191         NTSTATUS (*init_server)(struct dcesrv_context *, const struct dcesrv_endpoint_server *);
192
193         /* this function can be used by other endpoint servers to
194          * ask for a dcesrv_interface implementation
195          * - iface must be reference to an already existing struct !
196          */
197         BOOL (*interface_by_uuid)(struct dcesrv_interface *iface, const char *, uint32_t);
198
199         /* this function can be used by other endpoint servers to
200          * ask for a dcesrv_interface implementation
201          * - iface must be reference to an already existeng struct !
202          */
203         BOOL (*interface_by_name)(struct dcesrv_interface *iface, const char *);
204 };
205
206
207 /* server-wide context information for the dcerpc server */
208 struct dcesrv_context {
209         /* the list of endpoints that have registered 
210          * by the configured endpoint servers 
211          */
212         struct dcesrv_endpoint {
213                 struct dcesrv_endpoint *next, *prev;
214                 /* the type and location of the endpoint */
215                 struct dcerpc_binding ep_description;
216                 /* the security descriptor for smb named pipes */
217                 struct security_descriptor *sd;
218                 /* the list of interfaces available on this endpoint */
219                 struct dcesrv_if_list {
220                         struct dcesrv_if_list *next, *prev;
221                         struct dcesrv_interface iface;
222                 } *interface_list;
223         } *endpoint_list;
224
225         /* this is the default state_flags for dcesrv_call_state structs */
226         uint32_t state_flags;
227 };
228
229 /* this structure is used by modules to determine the size of some critical types */
230 struct dcesrv_critical_sizes {
231         int interface_version;
232         int sizeof_dcesrv_context;
233         int sizeof_dcesrv_endpoint;
234         int sizeof_dcesrv_endpoint_server;
235         int sizeof_dcesrv_interface;
236         int sizeof_dcesrv_if_list;
237         int sizeof_dcesrv_connection;
238         int sizeof_dcesrv_call_state;
239         int sizeof_dcesrv_auth;
240         int sizeof_dcesrv_handle;
241 };
242
243 #endif /* SAMBA_DCERPC_SERVER_H */