CVE-2015-5370: s4:rpc_server: maintain dcesrv_auth->auth_{type,level,context_id}
[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
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_MULTIPLEXED (1<<3)
105 #define DCESRV_CALL_STATE_FLAG_PROCESS_PENDING_CALL (1<<4)
106         uint32_t state_flags;
107
108         /* the time the request arrived in the server */
109         struct timeval time;
110
111         /* the backend can use this event context for async replies */
112         struct tevent_context *event_ctx;
113
114         /* the message_context that will be used for async replies */
115         struct imessaging_context *msg_ctx;
116
117         /* this is the pointer to the allocated function struct */
118         void *r;
119
120         /*
121          * that's the ndr pull context used in dcesrv_request()
122          * needed by dcesrv_reply() to carry over information
123          * for full pointer support.
124          */
125         struct ndr_pull *ndr_pull;
126
127         DATA_BLOB input;
128
129         struct data_blob_list_item *replies;
130
131         /* this is used by the boilerplate code to generate DCERPC faults */
132         uint32_t fault_code;
133 };
134
135 #define DCESRV_HANDLE_ANY 255
136
137 /* a dcerpc handle in internal format */
138 struct dcesrv_handle {
139         struct dcesrv_handle *next, *prev;
140         struct dcesrv_assoc_group *assoc_group;
141         struct policy_handle wire_handle;
142         struct dom_sid *sid;
143         const struct dcesrv_interface *iface;
144         void *data;
145 };
146
147 /* hold the authentication state information */
148 struct dcesrv_auth {
149         enum dcerpc_AuthType auth_type;
150         enum dcerpc_AuthLevel auth_level;
151         uint32_t auth_context_id;
152         struct dcerpc_auth *auth_info;
153         struct gensec_security *gensec_security;
154         struct auth_session_info *session_info;
155         NTSTATUS (*session_key)(struct dcesrv_connection *, DATA_BLOB *session_key);
156         bool client_hdr_signing;
157         bool hdr_signing;
158 };
159
160 struct dcesrv_connection_context {
161         struct dcesrv_connection_context *next, *prev;
162         uint32_t context_id;
163
164         struct dcesrv_assoc_group *assoc_group;
165
166         /* the connection this is on */
167         struct dcesrv_connection *conn;
168
169         /* the ndr function table for the chosen interface */
170         const struct dcesrv_interface *iface;
171
172         /* private data for the interface implementation */
173         void *private_data;
174
175         /*
176          * the minimum required auth level for this interface
177          */
178         enum dcerpc_AuthLevel min_auth_level;
179         bool allow_connect;
180 };
181
182
183 /* the state associated with a dcerpc server connection */
184 struct dcesrv_connection {
185         /* for the broken_connections DLIST */
186         struct dcesrv_connection *prev, *next;
187
188         /* the top level context for this server */
189         struct dcesrv_context *dce_ctx;
190
191         /* the endpoint that was opened */
192         const struct dcesrv_endpoint *endpoint;
193
194         /* a list of established context_ids */
195         struct dcesrv_connection_context *contexts;
196
197         /* the state of the current incoming call fragments */
198         struct dcesrv_call_state *incoming_fragmented_call_list;
199
200         /* the state of the async pending calls */
201         struct dcesrv_call_state *pending_call_list;
202
203         /* the state of the current outgoing calls */
204         struct dcesrv_call_state *call_list;
205
206         /* the maximum size the client wants to receive */
207         uint32_t cli_max_recv_frag;
208
209         DATA_BLOB partial_input;
210
211         /* This can be removed in master... */
212         struct  {
213                 struct dcerpc_auth *auth_info;
214                 struct gensec_security *gensec_security;
215                 struct auth_session_info *session_info;
216                 NTSTATUS (*session_key)(struct dcesrv_connection *, DATA_BLOB *session_key);
217                 bool client_hdr_signing;
218                 bool hdr_signing;
219         } _unused_auth_state;
220
221         /* the event_context that will be used for this connection */
222         struct tevent_context *event_ctx;
223
224         /* the message_context that will be used for this connection */
225         struct imessaging_context *msg_ctx;
226
227         /* the server_id that will be used for this connection */
228         struct server_id server_id;
229
230         /* the transport level session key */
231         DATA_BLOB transport_session_key;
232
233         /* is this connection pending termination?  If so, why? */
234         const char *terminate;
235
236         const char *packet_log_dir;
237
238         /* this is the default state_flags for dcesrv_call_state structs */
239         uint32_t state_flags;
240
241         struct {
242                 void *private_data;
243                 void (*report_output_data)(struct dcesrv_connection *);
244         } transport;
245
246         struct tstream_context *stream;
247         struct tevent_queue *send_queue;
248
249         const struct tsocket_address *local_address;
250         const struct tsocket_address *remote_address;
251
252         /* the current authentication state */
253         struct dcesrv_auth auth_state;
254 };
255
256
257 struct dcesrv_endpoint_server {
258         /* this is the name of the endpoint server */
259         const char *name;
260
261         /* this function should register endpoints and some other setup stuff,
262          * it is called when the dcesrv_context gets initialized.
263          */
264         NTSTATUS (*init_server)(struct dcesrv_context *, const struct dcesrv_endpoint_server *);
265
266         /* this function can be used by other endpoint servers to
267          * ask for a dcesrv_interface implementation
268          * - iface must be reference to an already existing struct !
269          */
270         bool (*interface_by_uuid)(struct dcesrv_interface *iface, const struct GUID *, uint32_t);
271
272         /* this function can be used by other endpoint servers to
273          * ask for a dcesrv_interface implementation
274          * - iface must be reference to an already existeng struct !
275          */
276         bool (*interface_by_name)(struct dcesrv_interface *iface, const char *);
277 };
278
279
280 /* one association groups */
281 struct dcesrv_assoc_group {
282         /* the wire id */
283         uint32_t id;
284         
285         /* list of handles in this association group */
286         struct dcesrv_handle *handles;
287
288         /* parent context */
289         struct dcesrv_context *dce_ctx;
290
291         /* Remote association group ID (if proxied) */
292         uint32_t proxied_id;
293 };
294
295 /* server-wide context information for the dcerpc server */
296 struct dcesrv_context {
297         /* the list of endpoints that have registered 
298          * by the configured endpoint servers 
299          */
300         struct dcesrv_endpoint {
301                 struct dcesrv_endpoint *next, *prev;
302                 /* the type and location of the endpoint */
303                 struct dcerpc_binding *ep_description;
304                 /* the security descriptor for smb named pipes */
305                 struct security_descriptor *sd;
306                 /* the list of interfaces available on this endpoint */
307                 struct dcesrv_if_list {
308                         struct dcesrv_if_list *next, *prev;
309                         struct dcesrv_interface iface;
310                 } *interface_list;
311         } *endpoint_list;
312
313         /* loadparm context to use for this connection */
314         struct loadparm_context *lp_ctx;
315
316         struct idr_context *assoc_groups_idr;
317
318         struct dcesrv_connection *broken_connections;
319 };
320
321 /* this structure is used by modules to determine the size of some critical types */
322 struct dcesrv_critical_sizes {
323         int interface_version;
324         int sizeof_dcesrv_context;
325         int sizeof_dcesrv_endpoint;
326         int sizeof_dcesrv_endpoint_server;
327         int sizeof_dcesrv_interface;
328         int sizeof_dcesrv_if_list;
329         int sizeof_dcesrv_connection;
330         int sizeof_dcesrv_call_state;
331         int sizeof_dcesrv_auth;
332         int sizeof_dcesrv_handle;
333 };
334
335 struct model_ops;
336
337 NTSTATUS dcesrv_interface_register(struct dcesrv_context *dce_ctx,
338                                    const char *ep_name,
339                                    const struct dcesrv_interface *iface,
340                                    const struct security_descriptor *sd);
341 NTSTATUS dcerpc_register_ep_server(const void *_ep_server);
342 NTSTATUS dcesrv_init_context(TALLOC_CTX *mem_ctx, 
343                                       struct loadparm_context *lp_ctx,
344                                       const char **endpoint_servers, struct dcesrv_context **_dce_ctx);
345 NTSTATUS dcesrv_endpoint_connect(struct dcesrv_context *dce_ctx,
346                                  TALLOC_CTX *mem_ctx,
347                                  const struct dcesrv_endpoint *ep,
348                                  struct auth_session_info *session_info,
349                                  struct tevent_context *event_ctx,
350                                  struct imessaging_context *msg_ctx,
351                                  struct server_id server_id,
352                                  uint32_t state_flags,
353                                  struct dcesrv_connection **_p);
354
355 NTSTATUS dcesrv_reply(struct dcesrv_call_state *call);
356 struct dcesrv_handle *dcesrv_handle_new(struct dcesrv_connection_context *context, 
357                                         uint8_t handle_type);
358
359 struct dcesrv_handle *dcesrv_handle_fetch(
360                                           struct dcesrv_connection_context *context, 
361                                           struct policy_handle *p,
362                                           uint8_t handle_type);
363 struct socket_address *dcesrv_connection_get_my_addr(struct dcesrv_connection *conn, TALLOC_CTX *mem_ctx);
364
365 struct socket_address *dcesrv_connection_get_peer_addr(struct dcesrv_connection *conn, TALLOC_CTX *mem_ctx);
366 const struct tsocket_address *dcesrv_connection_get_local_address(struct dcesrv_connection *conn);
367 const struct tsocket_address *dcesrv_connection_get_remote_address(struct dcesrv_connection *conn);
368
369 NTSTATUS dcesrv_fetch_session_key(struct dcesrv_connection *p, DATA_BLOB *session_key);
370
371 /* a useful macro for generating a RPC fault in the backend code */
372 #define DCESRV_FAULT(code) do { \
373         dce_call->fault_code = code; \
374         return r->out.result; \
375 } while(0)
376
377 /* a useful macro for generating a RPC fault in the backend code */
378 #define DCESRV_FAULT_VOID(code) do { \
379         dce_call->fault_code = code; \
380         return; \
381 } while(0)
382
383 /* a useful macro for checking the validity of a dcerpc policy handle
384    and giving the right fault code if invalid */
385 #define DCESRV_CHECK_HANDLE(h) do {if (!(h)) DCESRV_FAULT(DCERPC_FAULT_CONTEXT_MISMATCH); } while (0)
386
387 /* this checks for a valid policy handle, and gives a fault if an
388    invalid handle or retval if the handle is of the
389    wrong type */
390 #define DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, retval) do { \
391         (h) = dcesrv_handle_fetch(dce_call->context, (inhandle), DCESRV_HANDLE_ANY); \
392         DCESRV_CHECK_HANDLE(h); \
393         if ((t) != DCESRV_HANDLE_ANY && (h)->wire_handle.handle_type != (t)) { \
394                 return retval; \
395         } \
396 } while (0)
397
398 /* this checks for a valid policy handle and gives a dcerpc fault 
399    if its the wrong type of handle */
400 #define DCESRV_PULL_HANDLE_FAULT(h, inhandle, t) do { \
401         (h) = dcesrv_handle_fetch(dce_call->context, (inhandle), t); \
402         DCESRV_CHECK_HANDLE(h); \
403 } while (0)
404
405 #define DCESRV_PULL_HANDLE(h, inhandle, t) DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, NT_STATUS_INVALID_HANDLE)
406 #define DCESRV_PULL_HANDLE_WERR(h, inhandle, t) DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, WERR_BADFID)
407
408 NTSTATUS dcesrv_add_ep(struct dcesrv_context *dce_ctx,
409                        struct loadparm_context *lp_ctx,
410                        struct dcesrv_endpoint *e,
411                        struct tevent_context *event_ctx,
412                        const struct model_ops *model_ops);
413
414 /**
415  * retrieve credentials from a dce_call
416  */
417 _PUBLIC_ struct cli_credentials *dcesrv_call_credentials(struct dcesrv_call_state *dce_call);
418
419 /**
420  * returns true if this is an authenticated call
421  */
422 _PUBLIC_ bool dcesrv_call_authenticated(struct dcesrv_call_state *dce_call);
423
424 /**
425  * retrieve account_name for a dce_call
426  */
427 _PUBLIC_ const char *dcesrv_call_account_name(struct dcesrv_call_state *dce_call);
428
429 _PUBLIC_ NTSTATUS dcesrv_interface_bind_require_integrity(struct dcesrv_call_state *dce_call,
430                                                           const struct dcesrv_interface *iface);
431 _PUBLIC_ NTSTATUS dcesrv_interface_bind_require_privacy(struct dcesrv_call_state *dce_call,
432                                                         const struct dcesrv_interface *iface);
433 _PUBLIC_ NTSTATUS dcesrv_interface_bind_reject_connect(struct dcesrv_call_state *dce_call,
434                                                        const struct dcesrv_interface *iface);
435 _PUBLIC_ NTSTATUS dcesrv_interface_bind_allow_connect(struct dcesrv_call_state *dce_call,
436                                                       const struct dcesrv_interface *iface);
437
438 #endif /* SAMBA_DCERPC_SERVER_H */