pidl:NDR/ServerCompat: Generate local dispatching function
[samba.git] / librpc / rpc / dcesrv_core.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    Copyright (C) Samuel Cabrero <scabrero@samba.org> 2019
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #ifndef _LIBRPC_RPC_DCESRV_CORE_H_
25 #define _LIBRPC_RPC_DCESRV_CORE_H_
26
27 #include "librpc/rpc/rpc_common.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 struct dcesrv_iface_state;
42 struct cli_credentials;
43
44 struct dcesrv_interface {
45         const char *name;
46         struct ndr_syntax_id syntax_id;
47
48         /* this function is called when the client binds to this interface  */
49         NTSTATUS (*bind)(struct dcesrv_connection_context *, const struct dcesrv_interface *);
50
51         /* this function is called when the client disconnects the endpoint */
52         void (*unbind)(struct dcesrv_connection_context *, const struct dcesrv_interface *);
53
54         /* the ndr_pull function for the chosen interface.
55          */
56         NTSTATUS (*ndr_pull)(struct dcesrv_call_state *, TALLOC_CTX *, struct ndr_pull *, void **);
57
58         /* the dispatch function for the chosen interface.
59          */
60         NTSTATUS (*dispatch)(struct dcesrv_call_state *, TALLOC_CTX *, void *);
61
62         /* the reply function for the chosen interface.
63          */
64         NTSTATUS (*reply)(struct dcesrv_call_state *, TALLOC_CTX *, void *);
65
66         /* the ndr_push function for the chosen interface.
67          */
68         NTSTATUS (*ndr_push)(struct dcesrv_call_state *, TALLOC_CTX *, struct ndr_push *, const void *);
69
70         /* the local dispatch function for the chosen interface.
71          */
72         NTSTATUS (*local)(void *p, int opnum, TALLOC_CTX *, const DATA_BLOB *in, DATA_BLOB *out);
73
74         /* for any private use by the interface code */
75         const void *private_data;
76
77         uint64_t flags;
78 };
79
80 #define DCESRV_INTERFACE_FLAGS_HANDLES_NOT_USED 0x00000001
81
82 enum dcesrv_call_list {
83         DCESRV_LIST_NONE,
84         DCESRV_LIST_CALL_LIST,
85         DCESRV_LIST_FRAGMENTED_CALL_LIST,
86         DCESRV_LIST_PENDING_CALL_LIST
87 };
88
89 struct data_blob_list_item {
90         struct data_blob_list_item *prev,*next;
91         DATA_BLOB blob;
92 };
93
94 /* the state of an ongoing dcerpc call */
95 struct dcesrv_call_state {
96         struct dcesrv_call_state *next, *prev;
97         struct dcesrv_auth *auth_state;
98         struct dcesrv_connection *conn;
99         struct dcesrv_connection_context *context;
100         struct ncacn_packet pkt;
101
102         /*
103          * Used during async bind/alter_context.
104          */
105         struct ncacn_packet ack_pkt;
106
107         /*
108           which list this request is in, if any
109          */
110         enum dcesrv_call_list list;
111
112         /* the backend can mark the call
113          * with DCESRV_CALL_STATE_FLAG_ASYNC
114          * that will cause the frontend to not touch r->out
115          * and skip the reply
116          *
117          * this is only allowed to the backend when DCESRV_CALL_STATE_FLAG_MAY_ASYNC
118          * is alerady set by the frontend
119          *
120          * the backend then needs to call dcesrv_reply() when it's
121          * ready to send the reply
122          */
123 #define DCESRV_CALL_STATE_FLAG_ASYNC (1<<0)
124 #define DCESRV_CALL_STATE_FLAG_MAY_ASYNC (1<<1)
125 #define DCESRV_CALL_STATE_FLAG_MULTIPLEXED (1<<3)
126 #define DCESRV_CALL_STATE_FLAG_PROCESS_PENDING_CALL (1<<4)
127         uint32_t state_flags;
128
129         /* the time the request arrived in the server */
130         struct timeval time;
131
132         /* the backend can use this event context for async replies */
133         struct tevent_context *event_ctx;
134
135         /* this is the pointer to the allocated function struct */
136         void *r;
137
138         /*
139          * that's the ndr pull context used in dcesrv_request()
140          * needed by dcesrv_reply() to carry over information
141          * for full pointer support.
142          */
143         struct ndr_pull *ndr_pull;
144
145         DATA_BLOB input;
146
147         struct data_blob_list_item *replies;
148
149         /* this is used by the boilerplate code to generate DCERPC faults */
150         uint32_t fault_code;
151
152         /* the reason why we terminate the connection after sending a response */
153         const char *terminate_reason;
154
155         /* temporary auth_info fields */
156         struct dcerpc_auth in_auth_info;
157         struct dcerpc_auth _out_auth_info;
158         struct dcerpc_auth *out_auth_info;
159 };
160
161 /*
162 * DCERPC Handles
163 * --------------
164 * The various handles that are used in the RPC servers should be
165 * created and fetch using the dcesrv_handle_* functions.
166 *
167 * Use
168 * dcesrv_handle_create(struct dcesrv_call_state \*, uint8 handle_type)
169 * to obtain a new handle of the specified type. Handle types are
170 * unique within each pipe.
171 *
172 * The handle can later be fetched again using:
173 *
174 * struct dcesrv_handle *dcesrv_handle_lookup(
175 *         struct dcesrv_call_state *dce_call,
176 *         struct policy_handle *p,
177 *         uint8 handle_type)
178 *
179 * and destroyed by:
180 *
181 *       TALLOC_FREE(struct dcesrv_handle *).
182 *
183 * User data should be stored in the 'data' member of the dcesrv_handle
184 * struct.
185 */
186
187 #define DCESRV_HANDLE_ANY 255
188
189 /* a dcerpc handle in internal format */
190 struct dcesrv_handle {
191         struct dcesrv_handle *next, *prev;
192         struct dcesrv_assoc_group *assoc_group;
193         struct policy_handle wire_handle;
194         struct dom_sid *sid;
195         enum dcerpc_AuthLevel min_auth_level;
196         const struct dcesrv_interface *iface;
197         void *data;
198 };
199
200 /* hold the authentication state information */
201 struct dcesrv_auth {
202         struct dcesrv_auth *prev, *next;
203         enum dcerpc_AuthType auth_type;
204         enum dcerpc_AuthLevel auth_level;
205         uint32_t auth_context_id;
206         struct gensec_security *gensec_security;
207         struct auth_session_info *session_info;
208         NTSTATUS (*session_key_fn)(struct dcesrv_auth *, DATA_BLOB *session_key);
209         bool auth_started;
210         bool auth_finished;
211         bool auth_audited;
212         bool auth_invalid;
213 };
214
215 struct dcesrv_connection_context {
216         struct dcesrv_connection_context *next, *prev;
217         uint16_t context_id;
218
219         /* the connection this is on */
220         struct dcesrv_connection *conn;
221
222         /* the ndr function table for the chosen interface */
223         const struct dcesrv_interface *iface;
224
225         /*
226          * the minimum required auth level for this interface
227          */
228         enum dcerpc_AuthLevel min_auth_level;
229         bool allow_connect;
230
231         /* the negotiated transfer syntax */
232         struct ndr_syntax_id transfer_syntax;
233 };
234
235
236 /* the state associated with a dcerpc server connection */
237 struct dcesrv_connection {
238         /* for the broken_connections DLIST */
239         struct dcesrv_connection *prev, *next;
240
241         /* the top level context for this server */
242         struct dcesrv_context *dce_ctx;
243
244         /* the endpoint that was opened */
245         const struct dcesrv_endpoint *endpoint;
246
247         /* a list of established context_ids */
248         struct dcesrv_connection_context *contexts;
249
250         /* the state of the current incoming call fragments */
251         struct dcesrv_call_state *incoming_fragmented_call_list;
252
253         /* the state of the async pending calls */
254         struct dcesrv_call_state *pending_call_list;
255
256         /* the state of the current outgoing calls */
257         struct dcesrv_call_state *call_list;
258
259         /* the maximum size the client wants to receive */
260         uint16_t max_recv_frag;
261         uint16_t max_xmit_frag;
262
263         DATA_BLOB partial_input;
264
265         /* the event_context that will be used for this connection */
266         struct tevent_context *event_ctx;
267
268         /* is this connection pending termination?  If so, why? */
269         const char *terminate;
270
271         const char *packet_log_dir;
272
273         /* this is the default state_flags for dcesrv_call_state structs */
274         uint32_t state_flags;
275
276         struct {
277                 void *private_data;
278                 void (*report_output_data)(struct dcesrv_connection *);
279                 void (*terminate_connection)(struct dcesrv_connection *,
280                                              const char *);
281         } transport;
282
283         struct tstream_context *stream;
284         struct tevent_queue *send_queue;
285
286         const struct tsocket_address *local_address;
287         const struct tsocket_address *remote_address;
288
289         /* the current authentication state */
290         struct dcesrv_auth *default_auth_state;
291         size_t max_auth_states;
292         struct dcesrv_auth *auth_states;
293         bool got_explicit_auth_level_connect;
294         struct dcesrv_auth *default_auth_level_connect;
295         bool client_hdr_signing;
296         bool support_hdr_signing;
297         bool negotiated_hdr_signing;
298
299         /*
300          * remember which pdu types are allowed
301          */
302         bool allow_bind;
303         bool allow_alter;
304
305         /* the association group the connection belongs to */
306         struct dcesrv_assoc_group *assoc_group;
307
308         /* The maximum total payload of reassembled request pdus */
309         size_t max_total_request_size;
310
311         /*
312          * Our preferred transfer syntax.
313          */
314         const struct ndr_syntax_id *preferred_transfer;
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         /* true if the endpoint server has been initialized */
333         bool initialized;
334
335         /* this function should register endpoints and some other setup stuff,
336          * it is called when the dcesrv_context gets initialized.
337          */
338         NTSTATUS (*init_server)(struct dcesrv_context *, const struct dcesrv_endpoint_server *);
339
340         /* this function should cleanup endpoint server state and unregister
341          * the endpoint server from dcesrv_context */
342         NTSTATUS (*shutdown_server)(struct dcesrv_context *, const struct dcesrv_endpoint_server *);
343
344         /* this function can be used by other endpoint servers to
345          * ask for a dcesrv_interface implementation
346          * - iface must be reference to an already existing struct !
347          */
348         bool (*interface_by_uuid)(struct dcesrv_interface *iface, const struct GUID *, uint32_t);
349
350         /* this function can be used by other endpoint servers to
351          * ask for a dcesrv_interface implementation
352          * - iface must be reference to an already existeng struct !
353          */
354         bool (*interface_by_name)(struct dcesrv_interface *iface, const char *);
355 };
356
357
358 /* one association groups */
359 struct dcesrv_assoc_group {
360         /* the wire id */
361         uint32_t id;
362
363         /* The transport this is valid on */
364         enum dcerpc_transport_t transport;
365
366         /* list of handles in this association group */
367         struct dcesrv_handle *handles;
368
369         /*
370          * list of iface states per assoc/conn
371          */
372         struct dcesrv_iface_state *iface_states;
373
374         /* parent context */
375         struct dcesrv_context *dce_ctx;
376
377         /* the negotiated bind time features */
378         uint16_t bind_time_features;
379 };
380
381 struct dcesrv_context_callbacks {
382         struct {
383                 void (*successful_authz)(struct dcesrv_call_state *);
384         } log;
385         struct {
386                 NTSTATUS (*gensec_prepare)(TALLOC_CTX *mem_ctx,
387                                         struct dcesrv_call_state *call,
388                                         struct gensec_security **out);
389         } auth;
390         struct {
391                 NTSTATUS (*find)(struct dcesrv_call_state *);
392         } assoc_group;
393 };
394
395 /* server-wide context information for the dcerpc server */
396 struct dcesrv_context {
397         /*
398          * The euid at startup time.
399          *
400          * This is required for DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM
401          */
402         uid_t initial_euid;
403
404         /* the list of endpoints that have registered
405          * by the configured endpoint servers
406          */
407         struct dcesrv_endpoint {
408                 struct dcesrv_endpoint *next, *prev;
409                 /* the type and location of the endpoint */
410                 struct dcerpc_binding *ep_description;
411                 /* the secondary endpoint description for the BIND_ACK */
412                 struct dcerpc_binding *ep_2nd_description;
413                 /* the security descriptor for smb named pipes */
414                 struct security_descriptor *sd;
415                 /* the list of interfaces available on this endpoint */
416                 struct dcesrv_if_list {
417                         struct dcesrv_if_list *next, *prev;
418                         struct dcesrv_interface *iface;
419                 } *interface_list;
420
421                 /*
422                  * Should this service be run in a single process (so far only
423                  * NETLOGON is not run in a single process)
424                  */
425                 bool use_single_process;
426         } *endpoint_list;
427
428         /* loadparm context to use for this connection */
429         struct loadparm_context *lp_ctx;
430
431         struct idr_context *assoc_groups_idr;
432
433         struct dcesrv_connection *broken_connections;
434
435         struct dcesrv_context_callbacks callbacks;
436 };
437
438 /* this structure is used by modules to determine the size of some critical types */
439 struct dcesrv_critical_sizes {
440         int interface_version;
441         int sizeof_dcesrv_context;
442         int sizeof_dcesrv_endpoint;
443         int sizeof_dcesrv_endpoint_server;
444         int sizeof_dcesrv_interface;
445         int sizeof_dcesrv_if_list;
446         int sizeof_dcesrv_connection;
447         int sizeof_dcesrv_call_state;
448         int sizeof_dcesrv_auth;
449         int sizeof_dcesrv_handle;
450 };
451
452 NTSTATUS dcesrv_interface_register(struct dcesrv_context *dce_ctx,
453                                    const char *ep_name,
454                                    const char *ncacn_np_secondary_endpoint,
455                                    const struct dcesrv_interface *iface,
456                                    const struct security_descriptor *sd);
457 NTSTATUS dcerpc_register_ep_server(const struct dcesrv_endpoint_server *ep_server);
458 NTSTATUS dcesrv_init_ep_servers(struct dcesrv_context *dce_ctx,
459                                 const char **ep_servers);
460 NTSTATUS dcesrv_init_registered_ep_servers(struct dcesrv_context *dce_ctx);
461 NTSTATUS dcesrv_shutdown_registered_ep_servers(struct dcesrv_context *dce_ctx);
462 NTSTATUS dcesrv_init_ep_server(struct dcesrv_context *dce_ctx,
463                                const char *ep_server_name);
464 NTSTATUS dcesrv_shutdown_ep_server(struct dcesrv_context *dce_ctx,
465                                    const char *name);
466 const struct dcesrv_endpoint_server *dcesrv_ep_server_byname(const char *name);
467
468 NTSTATUS dcesrv_init_context(TALLOC_CTX *mem_ctx,
469                              struct loadparm_context *lp_ctx,
470                              struct dcesrv_context_callbacks *cb,
471                              struct dcesrv_context **_dce_ctx);
472 NTSTATUS dcesrv_reinit_context(struct dcesrv_context *dce_ctx);
473
474 NTSTATUS dcesrv_reply(struct dcesrv_call_state *call);
475 struct dcesrv_handle *dcesrv_handle_create(struct dcesrv_call_state *call,
476                                            uint8_t handle_type);
477
478 struct dcesrv_handle *dcesrv_handle_lookup(struct dcesrv_call_state *call,
479                                            const struct policy_handle *p,
480                                            uint8_t handle_type);
481
482 const struct tsocket_address *dcesrv_connection_get_local_address(struct dcesrv_connection *conn);
483 const struct tsocket_address *dcesrv_connection_get_remote_address(struct dcesrv_connection *conn);
484
485 /*
486  * Fetch the authentication session key if available.
487  *
488  * This is the key generated by a gensec authentication.
489  */
490 NTSTATUS dcesrv_auth_session_key(struct dcesrv_call_state *call,
491                                  DATA_BLOB *session_key);
492
493 /*
494  * Fetch the transport session key if available.
495  * Typically this is the SMB session key
496  * or a fixed key for local transports.
497  *
498  * The key is always truncated to 16 bytes.
499 */
500 NTSTATUS dcesrv_transport_session_key(struct dcesrv_call_state *call,
501                                       DATA_BLOB *session_key);
502
503 /* a useful macro for generating a RPC fault in the backend code */
504 #define DCESRV_FAULT(code) do { \
505         dce_call->fault_code = code; \
506         return r->out.result; \
507 } while(0)
508
509 /* a useful macro for generating a RPC fault in the backend code */
510 #define DCESRV_FAULT_VOID(code) do { \
511         dce_call->fault_code = code; \
512         return; \
513 } while(0)
514
515 /* a useful macro for checking the validity of a dcerpc policy handle
516    and giving the right fault code if invalid */
517 #define DCESRV_CHECK_HANDLE(h) do {if (!(h)) DCESRV_FAULT(DCERPC_FAULT_CONTEXT_MISMATCH); } while (0)
518
519 /* this checks for a valid policy handle, and gives a fault if an
520    invalid handle or retval if the handle is of the
521    wrong type */
522 #define DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, retval) do { \
523         (h) = dcesrv_handle_lookup(dce_call, (inhandle), DCESRV_HANDLE_ANY); \
524         DCESRV_CHECK_HANDLE(h); \
525         if ((t) != DCESRV_HANDLE_ANY && (h)->wire_handle.handle_type != (t)) { \
526                 return retval; \
527         } \
528 } while (0)
529
530 /* this checks for a valid policy handle and gives a dcerpc fault
531    if its the wrong type of handle */
532 #define DCESRV_PULL_HANDLE_FAULT(h, inhandle, t) do { \
533         (h) = dcesrv_handle_lookup(dce_call, (inhandle), t); \
534         DCESRV_CHECK_HANDLE(h); \
535 } while (0)
536
537 #define DCESRV_PULL_HANDLE(h, inhandle, t) DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, NT_STATUS_INVALID_HANDLE)
538 #define DCESRV_PULL_HANDLE_WERR(h, inhandle, t) DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, WERR_INVALID_HANDLE)
539
540 /**
541  * retrieve credentials from a dce_call
542  */
543 _PUBLIC_ struct cli_credentials *dcesrv_call_credentials(struct dcesrv_call_state *dce_call);
544
545 /**
546  * returns true if this is an authenticated call
547  */
548 _PUBLIC_ bool dcesrv_call_authenticated(struct dcesrv_call_state *dce_call);
549
550 /**
551  * retrieve account_name for a dce_call
552  */
553 _PUBLIC_ const char *dcesrv_call_account_name(struct dcesrv_call_state *dce_call);
554
555 /**
556  * retrieve session_info from a dce_call
557  */
558 _PUBLIC_ struct auth_session_info *dcesrv_call_session_info(struct dcesrv_call_state *dce_call);
559
560 /**
561  * retrieve auth type/level from a dce_call
562  */
563 _PUBLIC_ void dcesrv_call_auth_info(struct dcesrv_call_state *dce_call,
564                                     enum dcerpc_AuthType *auth_type,
565                                     enum dcerpc_AuthLevel *auth_level);
566
567 _PUBLIC_ NTSTATUS dcesrv_interface_bind_require_integrity(struct dcesrv_connection_context *context,
568                                                           const struct dcesrv_interface *iface);
569 _PUBLIC_ NTSTATUS dcesrv_interface_bind_require_privacy(struct dcesrv_connection_context *context,
570                                                         const struct dcesrv_interface *iface);
571 _PUBLIC_ NTSTATUS dcesrv_interface_bind_reject_connect(struct dcesrv_connection_context *context,
572                                                        const struct dcesrv_interface *iface);
573 _PUBLIC_ NTSTATUS dcesrv_interface_bind_allow_connect(struct dcesrv_connection_context *context,
574                                                       const struct dcesrv_interface *iface);
575
576 _PUBLIC_ NTSTATUS _dcesrv_iface_state_store_assoc(
577                 struct dcesrv_call_state *call,
578                 uint64_t magic,
579                 void *ptr,
580                 const char *location);
581 #define dcesrv_iface_state_store_assoc(call, magic, ptr) \
582         _dcesrv_iface_state_store_assoc((call), (magic), (ptr), \
583                                         __location__)
584 _PUBLIC_ void *_dcesrv_iface_state_find_assoc(
585                 struct dcesrv_call_state *call,
586                 uint64_t magic);
587 #define dcesrv_iface_state_find_assoc(call, magic, _type) \
588         talloc_get_type( \
589                 _dcesrv_iface_state_find_assoc((call), (magic)), \
590                 _type)
591
592 _PUBLIC_ NTSTATUS _dcesrv_iface_state_store_conn(
593                 struct dcesrv_call_state *call,
594                 uint64_t magic,
595                 void *_pptr,
596                 const char *location);
597 #define dcesrv_iface_state_store_conn(call, magic, ptr) \
598         _dcesrv_iface_state_store_conn((call), (magic), (ptr), \
599                                         __location__)
600 _PUBLIC_ void *_dcesrv_iface_state_find_conn(
601                 struct dcesrv_call_state *call,
602                 uint64_t magic);
603 #define dcesrv_iface_state_find_conn(call, magic, _type) \
604         talloc_get_type( \
605                 _dcesrv_iface_state_find_conn((call), (magic)), \
606                 _type)
607
608 _PUBLIC_ void dcesrv_cleanup_broken_connections(struct dcesrv_context *dce_ctx);
609
610 _PUBLIC_ NTSTATUS dcesrv_endpoint_connect(struct dcesrv_context *dce_ctx,
611                                 TALLOC_CTX *mem_ctx,
612                                 const struct dcesrv_endpoint *ep,
613                                 struct auth_session_info *session_info,
614                                 struct tevent_context *event_ctx,
615                                 uint32_t state_flags,
616                                 struct dcesrv_connection **_p);
617 _PUBLIC_ NTSTATUS dcesrv_find_endpoint(struct dcesrv_context *dce_ctx,
618                                 const struct dcerpc_binding *ep_description,
619                                 struct dcesrv_endpoint **_out);
620
621 _PUBLIC_ void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn,
622                                           const char *reason);
623 _PUBLIC_ void dcesrv_sock_report_output_data(struct dcesrv_connection *dce_conn);
624
625 _PUBLIC_ NTSTATUS dcesrv_connection_loop_start(struct dcesrv_connection *conn);
626
627
628 void _dcesrv_save_ndr_fuzz_seed(DATA_BLOB call_blob,
629                                 struct dcesrv_call_state *call,
630                                 int flags);
631
632 #if DEVELOPER
633 #define  dcesrv_save_ndr_fuzz_seed(stub, call, flags) \
634         _dcesrv_save_ndr_fuzz_seed(stub, call, flags)
635 #else
636 #define  dcesrv_save_ndr_fuzz_seed(stub, call, flags) \
637         /* */
638 #endif
639
640
641 #endif /* _LIBRPC_RPC_DCESRV_CORE_H_ */