2 Unix SMB/CIFS implementation.
4 server side dcerpc defines
6 Copyright (C) Andrew Tridgell 2003-2005
7 Copyright (C) Stefan (metze) Metzmacher 2004-2005
8 Copyright (C) Samuel Cabrero <scabrero@samba.org> 2019
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.
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.
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/>.
24 #ifndef _LIBRPC_RPC_DCESRV_CORE_H_
25 #define _LIBRPC_RPC_DCESRV_CORE_H_
27 #include "librpc/rpc/rpc_common.h"
28 #include "librpc/ndr/libndr.h"
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.
34 /* version 1 - initial version - metze */
35 #define DCERPC_MODULE_VERSION 1
37 struct dcesrv_connection
;
38 struct dcesrv_call_state
;
40 struct dcesrv_connection_context
;
41 struct dcesrv_iface_state
;
42 struct cli_credentials
;
44 struct dcesrv_interface
{
46 struct ndr_syntax_id syntax_id
;
48 /* this function is called when the client binds to this interface */
49 NTSTATUS (*bind
)(struct dcesrv_connection_context
*, const struct dcesrv_interface
*);
51 /* this function is called when the client disconnects the endpoint */
52 void (*unbind
)(struct dcesrv_connection_context
*, const struct dcesrv_interface
*);
54 /* the ndr_pull function for the chosen interface.
56 NTSTATUS (*ndr_pull
)(struct dcesrv_call_state
*, TALLOC_CTX
*, struct ndr_pull
*, void **);
58 /* the dispatch function for the chosen interface.
60 NTSTATUS (*dispatch
)(struct dcesrv_call_state
*, TALLOC_CTX
*, void *);
62 /* the reply function for the chosen interface.
64 NTSTATUS (*reply
)(struct dcesrv_call_state
*, TALLOC_CTX
*, void *);
66 /* the ndr_push function for the chosen interface.
68 NTSTATUS (*ndr_push
)(struct dcesrv_call_state
*, TALLOC_CTX
*, struct ndr_push
*, const void *);
70 /* the local dispatch function for the chosen interface.
72 NTSTATUS (*local
)(struct dcesrv_call_state
*, TALLOC_CTX
*, void *);
74 /* for any private use by the interface code */
75 const void *private_data
;
80 #define DCESRV_INTERFACE_FLAGS_HANDLES_NOT_USED 0x00000001
82 enum dcesrv_call_list
{
84 DCESRV_LIST_CALL_LIST
,
85 DCESRV_LIST_FRAGMENTED_CALL_LIST
,
86 DCESRV_LIST_PENDING_CALL_LIST
89 struct data_blob_list_item
{
90 struct data_blob_list_item
*prev
,*next
;
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
;
103 * Used during async bind/alter_context.
105 struct ncacn_packet ack_pkt
;
108 which list this request is in, if any
110 enum dcesrv_call_list list
;
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
117 * this is only allowed to the backend when DCESRV_CALL_STATE_FLAG_MAY_ASYNC
118 * is alerady set by the frontend
120 * the backend then needs to call dcesrv_reply() when it's
121 * ready to send the reply
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
;
129 /* the time the request arrived in the server */
132 /* the backend can use this event context for async replies */
133 struct tevent_context
*event_ctx
;
135 /* this is the pointer to the allocated function struct */
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.
143 struct ndr_pull
*ndr_pull
;
147 struct data_blob_list_item
*replies
;
149 /* this is used by the boilerplate code to generate DCERPC faults */
152 /* the reason why we terminate the connection after sending a response */
153 const char *terminate_reason
;
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
;
164 * The various handles that are used in the RPC servers should be
165 * created and fetch using the dcesrv_handle_* functions.
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.
172 * The handle can later be fetched again using:
174 * struct dcesrv_handle *dcesrv_handle_lookup(
175 * struct dcesrv_call_state *dce_call,
176 * struct policy_handle *p,
181 * TALLOC_FREE(struct dcesrv_handle *).
183 * User data should be stored in the 'data' member of the dcesrv_handle
187 #define DCESRV_HANDLE_ANY 255
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
;
195 enum dcerpc_AuthLevel min_auth_level
;
196 const struct dcesrv_interface
*iface
;
200 /* hold the authentication state information */
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
);
215 struct dcesrv_connection_context
{
216 struct dcesrv_connection_context
*next
, *prev
;
219 /* the connection this is on */
220 struct dcesrv_connection
*conn
;
222 /* the ndr function table for the chosen interface */
223 const struct dcesrv_interface
*iface
;
226 * the minimum required auth level for this interface
228 enum dcerpc_AuthLevel min_auth_level
;
231 /* the negotiated transfer syntax */
232 struct ndr_syntax_id transfer_syntax
;
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
;
241 /* the top level context for this server */
242 struct dcesrv_context
*dce_ctx
;
244 /* the endpoint that was opened */
245 const struct dcesrv_endpoint
*endpoint
;
247 /* a list of established context_ids */
248 struct dcesrv_connection_context
*contexts
;
250 /* the state of the current incoming call fragments */
251 struct dcesrv_call_state
*incoming_fragmented_call_list
;
253 /* the state of the async pending calls */
254 struct dcesrv_call_state
*pending_call_list
;
256 /* the state of the current outgoing calls */
257 struct dcesrv_call_state
*call_list
;
259 /* the maximum size the client wants to receive */
260 uint16_t max_recv_frag
;
261 uint16_t max_xmit_frag
;
263 DATA_BLOB partial_input
;
265 /* the event_context that will be used for this connection */
266 struct tevent_context
*event_ctx
;
268 /* is this connection pending termination? If so, why? */
269 const char *terminate
;
271 const char *packet_log_dir
;
273 /* this is the default state_flags for dcesrv_call_state structs */
274 uint32_t state_flags
;
278 void (*report_output_data
)(struct dcesrv_connection
*);
279 void (*terminate_connection
)(struct dcesrv_connection
*,
283 struct tstream_context
*stream
;
284 struct tevent_queue
*send_queue
;
286 const struct tsocket_address
*local_address
;
287 const struct tsocket_address
*remote_address
;
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
;
300 * remember which pdu types are allowed
305 /* the association group the connection belongs to */
306 struct dcesrv_assoc_group
*assoc_group
;
308 /* The maximum total payload of reassembled request pdus */
309 size_t max_total_request_size
;
312 * Our preferred transfer syntax.
314 const struct ndr_syntax_id
*preferred_transfer
;
317 * This is used to block the connection during
318 * pending authentication.
320 struct tevent_req
*(*wait_send
)(TALLOC_CTX
*mem_ctx
,
321 struct tevent_context
*ev
,
323 NTSTATUS (*wait_recv
)(struct tevent_req
*req
);
328 struct dcesrv_endpoint_server
{
329 /* this is the name of the endpoint server */
332 /* true if the endpoint server has been initialized */
335 /* this function should register endpoints and some other setup stuff,
336 * it is called when the dcesrv_context gets initialized.
338 NTSTATUS (*init_server
)(struct dcesrv_context
*, const struct dcesrv_endpoint_server
*);
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
*);
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 !
348 bool (*interface_by_uuid
)(struct dcesrv_interface
*iface
, const struct GUID
*, uint32_t);
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 !
354 bool (*interface_by_name
)(struct dcesrv_interface
*iface
, const char *);
358 /* one association groups */
359 struct dcesrv_assoc_group
{
363 /* The transport this is valid on */
364 enum dcerpc_transport_t transport
;
366 /* list of handles in this association group */
367 struct dcesrv_handle
*handles
;
370 * list of iface states per assoc/conn
372 struct dcesrv_iface_state
*iface_states
;
375 struct dcesrv_context
*dce_ctx
;
377 /* the negotiated bind time features */
378 uint16_t bind_time_features
;
381 struct dcesrv_context_callbacks
{
383 void (*successful_authz
)(struct dcesrv_call_state
*);
386 NTSTATUS (*gensec_prepare
)(TALLOC_CTX
*mem_ctx
,
387 struct dcesrv_call_state
*call
,
388 struct gensec_security
**out
);
391 NTSTATUS (*find
)(struct dcesrv_call_state
*);
395 /* server-wide context information for the dcerpc server */
396 struct dcesrv_context
{
398 * The euid at startup time.
400 * This is required for DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM
404 /* the list of endpoints that have registered
405 * by the configured endpoint servers
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
;
422 * Should this service be run in a single process (so far only
423 * NETLOGON is not run in a single process)
425 bool use_single_process
;
428 /* loadparm context to use for this connection */
429 struct loadparm_context
*lp_ctx
;
431 struct idr_context
*assoc_groups_idr
;
433 struct dcesrv_connection
*broken_connections
;
435 struct dcesrv_context_callbacks callbacks
;
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
;
452 NTSTATUS
dcesrv_interface_register(struct dcesrv_context
*dce_ctx
,
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
,
466 const struct dcesrv_endpoint_server
*dcesrv_ep_server_byname(const char *name
);
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
);
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
);
478 struct dcesrv_handle
*dcesrv_handle_lookup(struct dcesrv_call_state
*call
,
479 const struct policy_handle
*p
,
480 uint8_t handle_type
);
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
);
486 * Fetch the authentication session key if available.
488 * This is the key generated by a gensec authentication.
490 NTSTATUS
dcesrv_auth_session_key(struct dcesrv_call_state
*call
,
491 DATA_BLOB
*session_key
);
494 * Fetch the transport session key if available.
495 * Typically this is the SMB session key
496 * or a fixed key for local transports.
498 * The key is always truncated to 16 bytes.
500 NTSTATUS
dcesrv_transport_session_key(struct dcesrv_call_state
*call
,
501 DATA_BLOB
*session_key
);
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; \
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; \
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)
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
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)) { \
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); \
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)
541 * retrieve credentials from a dce_call
543 _PUBLIC_
struct cli_credentials
*dcesrv_call_credentials(struct dcesrv_call_state
*dce_call
);
546 * returns true if this is an authenticated call
548 _PUBLIC_
bool dcesrv_call_authenticated(struct dcesrv_call_state
*dce_call
);
551 * retrieve account_name for a dce_call
553 _PUBLIC_
const char *dcesrv_call_account_name(struct dcesrv_call_state
*dce_call
);
556 * retrieve session_info from a dce_call
558 _PUBLIC_
struct auth_session_info
*dcesrv_call_session_info(struct dcesrv_call_state
*dce_call
);
561 * retrieve auth type/level from a dce_call
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
);
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
);
576 _PUBLIC_ NTSTATUS
_dcesrv_iface_state_store_assoc(
577 struct dcesrv_call_state
*call
,
580 const char *location
);
581 #define dcesrv_iface_state_store_assoc(call, magic, ptr) \
582 _dcesrv_iface_state_store_assoc((call), (magic), (ptr), \
584 _PUBLIC_
void *_dcesrv_iface_state_find_assoc(
585 struct dcesrv_call_state
*call
,
587 #define dcesrv_iface_state_find_assoc(call, magic, _type) \
589 _dcesrv_iface_state_find_assoc((call), (magic)), \
592 _PUBLIC_ NTSTATUS
_dcesrv_iface_state_store_conn(
593 struct dcesrv_call_state
*call
,
596 const char *location
);
597 #define dcesrv_iface_state_store_conn(call, magic, ptr) \
598 _dcesrv_iface_state_store_conn((call), (magic), (ptr), \
600 _PUBLIC_
void *_dcesrv_iface_state_find_conn(
601 struct dcesrv_call_state
*call
,
603 #define dcesrv_iface_state_find_conn(call, magic, _type) \
605 _dcesrv_iface_state_find_conn((call), (magic)), \
608 _PUBLIC_
void dcesrv_cleanup_broken_connections(struct dcesrv_context
*dce_ctx
);
610 _PUBLIC_ NTSTATUS
dcesrv_endpoint_connect(struct dcesrv_context
*dce_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
);
621 _PUBLIC_
void dcesrv_terminate_connection(struct dcesrv_connection
*dce_conn
,
623 _PUBLIC_
void dcesrv_sock_report_output_data(struct dcesrv_connection
*dce_conn
);
625 _PUBLIC_ NTSTATUS
dcesrv_connection_loop_start(struct dcesrv_connection
*conn
);
627 _PUBLIC_ NTSTATUS
dcesrv_call_dispatch_local(struct dcesrv_call_state
*call
);
629 _PUBLIC_
const struct dcesrv_interface
*find_interface_by_uuid(
630 const struct dcesrv_endpoint
*endpoint
,
631 const struct GUID
*uuid
, uint32_t if_version
);
633 void _dcesrv_save_ndr_fuzz_seed(DATA_BLOB call_blob
,
634 struct dcesrv_call_state
*call
,
638 #define dcesrv_save_ndr_fuzz_seed(stub, call, flags) \
639 _dcesrv_save_ndr_fuzz_seed(stub, call, flags)
641 #define dcesrv_save_ndr_fuzz_seed(stub, call, flags) \
646 #endif /* _LIBRPC_RPC_DCESRV_CORE_H_ */