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"
29 #include "librpc/gen_ndr/security.h"
31 /* modules can use the following to determine if the interface has changed
32 * please increment the version number after each interface change
33 * with a comment and maybe update struct dcesrv_critical_sizes.
35 /* version 1 - initial version - metze */
36 #define DCERPC_MODULE_VERSION 1
38 struct dcesrv_connection
;
39 struct dcesrv_call_state
;
41 struct dcesrv_connection_context
;
42 struct dcesrv_iface_state
;
43 struct cli_credentials
;
45 struct dcesrv_interface
{
47 struct ndr_syntax_id syntax_id
;
49 /* this function is called when the client binds to this interface */
50 NTSTATUS (*bind
)(struct dcesrv_connection_context
*, const struct dcesrv_interface
*);
52 /* this function is called when the client disconnects the endpoint */
53 void (*unbind
)(struct dcesrv_connection_context
*, const struct dcesrv_interface
*);
55 /* the ndr_pull function for the chosen interface.
57 NTSTATUS (*ndr_pull
)(struct dcesrv_call_state
*, TALLOC_CTX
*, struct ndr_pull
*, void **);
59 /* the dispatch function for the chosen interface.
61 NTSTATUS (*dispatch
)(struct dcesrv_call_state
*, TALLOC_CTX
*, void *);
63 /* the reply function for the chosen interface.
65 NTSTATUS (*reply
)(struct dcesrv_call_state
*, TALLOC_CTX
*, void *);
67 /* the ndr_push function for the chosen interface.
69 NTSTATUS (*ndr_push
)(struct dcesrv_call_state
*, TALLOC_CTX
*, struct ndr_push
*, const void *);
71 /* the local dispatch function for the chosen interface.
73 NTSTATUS (*local
)(struct dcesrv_call_state
*, TALLOC_CTX
*, void *);
75 /* for any private use by the interface code */
76 const void *private_data
;
81 #define DCESRV_INTERFACE_FLAGS_HANDLES_NOT_USED 0x00000001
83 enum dcesrv_call_list
{
85 DCESRV_LIST_CALL_LIST
,
86 DCESRV_LIST_FRAGMENTED_CALL_LIST
,
87 DCESRV_LIST_PENDING_CALL_LIST
90 struct data_blob_list_item
{
91 struct data_blob_list_item
*prev
,*next
;
95 /* the state of an ongoing dcerpc call */
96 struct dcesrv_call_state
{
97 struct dcesrv_call_state
*next
, *prev
;
98 struct dcesrv_auth
*auth_state
;
99 struct dcesrv_connection
*conn
;
100 struct dcesrv_connection_context
*context
;
101 struct ncacn_packet pkt
;
104 * Used during async bind/alter_context.
106 struct ncacn_packet ack_pkt
;
109 which list this request is in, if any
111 enum dcesrv_call_list list
;
113 /* the backend can mark the call
114 * with DCESRV_CALL_STATE_FLAG_ASYNC
115 * that will cause the frontend to not touch r->out
118 * this is only allowed to the backend when DCESRV_CALL_STATE_FLAG_MAY_ASYNC
119 * is already set by the frontend
121 * the backend then needs to call dcesrv_reply() when it's
122 * ready to send the reply
124 #define DCESRV_CALL_STATE_FLAG_ASYNC (1<<0)
125 #define DCESRV_CALL_STATE_FLAG_MAY_ASYNC (1<<1)
126 #define DCESRV_CALL_STATE_FLAG_MULTIPLEXED (1<<3)
127 #define DCESRV_CALL_STATE_FLAG_PROCESS_PENDING_CALL (1<<4)
128 #define DCESRV_CALL_STATE_FLAG_WINBIND_OFF (1 << 5)
129 uint32_t state_flags
;
131 /* the time the request arrived in the server */
134 /* the backend can use this event context for async replies */
135 struct tevent_context
*event_ctx
;
137 /* this is the pointer to the allocated function struct */
141 * that's the ndr pull context used in dcesrv_request()
142 * needed by dcesrv_reply() to carry over information
143 * for full pointer support.
145 struct ndr_pull
*ndr_pull
;
149 struct data_blob_list_item
*replies
;
151 /* this is used by the boilerplate code to generate DCERPC faults */
154 /* the reason why we terminate the connection after sending a response */
155 const char *terminate_reason
;
157 /* temporary auth_info fields */
158 struct dcerpc_auth in_auth_info
;
159 struct dcerpc_auth _out_auth_info
;
160 struct dcerpc_auth
*out_auth_info
;
166 * The various handles that are used in the RPC servers should be
167 * created and fetch using the dcesrv_handle_* functions.
170 * dcesrv_handle_create(struct dcesrv_call_state \*, uint8 handle_type)
171 * to obtain a new handle of the specified type. Handle types are
172 * unique within each pipe.
174 * The handle can later be fetched again using:
176 * struct dcesrv_handle *dcesrv_handle_lookup(
177 * struct dcesrv_call_state *dce_call,
178 * struct policy_handle *p,
183 * TALLOC_FREE(struct dcesrv_handle *).
185 * User data should be stored in the 'data' member of the dcesrv_handle
189 #define DCESRV_HANDLE_ANY 255
191 /* a dcerpc handle in internal format */
192 struct dcesrv_handle
{
193 struct dcesrv_handle
*next
, *prev
;
194 struct dcesrv_assoc_group
*assoc_group
;
195 struct policy_handle wire_handle
;
197 enum dcerpc_AuthLevel min_auth_level
;
198 const struct dcesrv_interface
*iface
;
202 /* hold the authentication state information */
204 struct dcesrv_auth
*prev
, *next
;
205 enum dcerpc_AuthType auth_type
;
206 enum dcerpc_AuthLevel auth_level
;
207 uint32_t auth_context_id
;
208 struct gensec_security
*gensec_security
;
209 struct auth_session_info
*session_info
;
210 NTSTATUS (*session_key_fn
)(struct dcesrv_auth
*, DATA_BLOB
*session_key
);
217 struct dcesrv_connection_context
{
218 struct dcesrv_connection_context
*next
, *prev
;
221 /* the connection this is on */
222 struct dcesrv_connection
*conn
;
224 /* the ndr function table for the chosen interface */
225 const struct dcesrv_interface
*iface
;
228 * the minimum required auth level for this interface
230 enum dcerpc_AuthLevel min_auth_level
;
233 /* the negotiated transfer syntax */
234 struct ndr_syntax_id transfer_syntax
;
239 /* the state associated with a dcerpc server connection */
240 struct dcesrv_connection
{
241 /* for the broken_connections DLIST */
242 struct dcesrv_connection
*prev
, *next
;
244 /* the top level context for this server */
245 struct dcesrv_context
*dce_ctx
;
247 /* the endpoint that was opened */
248 const struct dcesrv_endpoint
*endpoint
;
250 /* a list of established context_ids */
251 struct dcesrv_connection_context
*contexts
;
253 /* the state of the current incoming call fragments */
254 struct dcesrv_call_state
*incoming_fragmented_call_list
;
256 /* the state of the async pending calls */
257 struct dcesrv_call_state
*pending_call_list
;
259 /* the state of the current outgoing calls */
260 struct dcesrv_call_state
*call_list
;
262 /* the maximum size the client wants to receive */
263 uint16_t max_recv_frag
;
264 uint16_t max_xmit_frag
;
266 DATA_BLOB partial_input
;
268 /* the event_context that will be used for this connection */
269 struct tevent_context
*event_ctx
;
271 /* is this connection pending termination? If so, why? */
272 const char *terminate
;
274 const char *packet_log_dir
;
276 /* this is the default state_flags for dcesrv_call_state structs */
277 uint32_t state_flags
;
281 void (*report_output_data
)(struct dcesrv_connection
*);
282 void (*terminate_connection
)(struct dcesrv_connection
*,
286 struct tstream_context
*stream
;
287 struct tevent_queue
*send_queue
;
289 const struct tsocket_address
*local_address
;
290 const struct tsocket_address
*remote_address
;
292 /* the current authentication state */
293 struct dcesrv_auth
*default_auth_state
;
294 size_t max_auth_states
;
295 struct dcesrv_auth
*auth_states
;
296 bool got_explicit_auth_level_connect
;
297 struct dcesrv_auth
*default_auth_level_connect
;
298 bool client_hdr_signing
;
299 bool support_hdr_signing
;
300 bool negotiated_hdr_signing
;
303 * remember which pdu types are allowed
308 /* the association group the connection belongs to */
309 struct dcesrv_assoc_group
*assoc_group
;
311 /* The maximum total payload of reassembled request pdus */
312 size_t max_total_request_size
;
315 * Our preferred transfer syntax.
317 const struct ndr_syntax_id
*preferred_transfer
;
320 * This is used to block the connection during
321 * pending authentication.
323 struct tevent_req
*(*wait_send
)(TALLOC_CTX
*mem_ctx
,
324 struct tevent_context
*ev
,
326 NTSTATUS (*wait_recv
)(struct tevent_req
*req
);
331 struct dcesrv_endpoint_server
{
332 /* this is the name of the endpoint server */
335 /* true if the endpoint server has been initialized */
338 /* this function should register endpoints and some other setup stuff,
339 * it is called when the dcesrv_context gets initialized.
341 NTSTATUS (*init_server
)(struct dcesrv_context
*, const struct dcesrv_endpoint_server
*);
343 /* this function should cleanup endpoint server state and unregister
344 * the endpoint server from dcesrv_context */
345 NTSTATUS (*shutdown_server
)(struct dcesrv_context
*, const struct dcesrv_endpoint_server
*);
347 /* this function can be used by other endpoint servers to
348 * ask for a dcesrv_interface implementation
349 * - iface must be reference to an already existing struct !
351 bool (*interface_by_uuid
)(struct dcesrv_interface
*iface
, const struct GUID
*, uint32_t);
353 /* this function can be used by other endpoint servers to
354 * ask for a dcesrv_interface implementation
355 * - iface must be reference to an already existing struct !
357 bool (*interface_by_name
)(struct dcesrv_interface
*iface
, const char *);
361 /* one association groups */
362 struct dcesrv_assoc_group
{
366 /* The transport this is valid on */
367 enum dcerpc_transport_t transport
;
369 /* list of handles in this association group */
370 struct dcesrv_handle
*handles
;
373 * list of iface states per assoc/conn
375 struct dcesrv_iface_state
*iface_states
;
378 struct dcesrv_context
*dce_ctx
;
380 /* the negotiated bind time features */
381 uint16_t bind_time_features
;
384 struct dcesrv_context_callbacks
{
386 void (*successful_authz
)(
387 struct dcesrv_call_state
*call
, void *private_data
);
391 NTSTATUS (*gensec_prepare
)(
393 struct dcesrv_call_state
*call
,
394 struct gensec_security
**out
,
397 void (*become_root
)(void);
398 void (*unbecome_root
)(void);
402 struct dcesrv_call_state
*call
, void *private_data
);
407 /* server-wide context information for the dcerpc server */
408 struct dcesrv_context
{
410 * The euid at startup time.
412 * This is required for DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM
416 /* the list of endpoints that have registered
417 * by the configured endpoint servers
419 struct dcesrv_endpoint
{
420 struct dcesrv_endpoint
*next
, *prev
;
421 /* the type and location of the endpoint */
422 struct dcerpc_binding
*ep_description
;
423 /* the secondary endpoint description for the BIND_ACK */
424 struct dcerpc_binding
*ep_2nd_description
;
425 /* the security descriptor for smb named pipes */
426 struct security_descriptor
*sd
;
427 /* the list of interfaces available on this endpoint */
428 struct dcesrv_if_list
{
429 struct dcesrv_if_list
*next
, *prev
;
430 struct dcesrv_interface
*iface
;
434 * Should this service be run in a single process (so far only
435 * NETLOGON is not run in a single process)
437 bool use_single_process
;
441 * registered auth_type/principals
442 * for dcesrv_mgmt_inq_princ_name()
444 struct dcesrv_ctx_principal
{
445 struct dcesrv_ctx_principal
*next
, *prev
;
446 enum dcerpc_AuthType auth_type
;
447 const char *principal_name
;
450 /* loadparm context to use for this connection */
451 struct loadparm_context
*lp_ctx
;
453 struct idr_context
*assoc_groups_idr
;
454 uint32_t assoc_groups_num
;
456 struct dcesrv_connection
*broken_connections
;
459 * Our preferred transfer syntax.
461 const struct ndr_syntax_id
*preferred_transfer
;
463 struct dcesrv_context_callbacks
*callbacks
;
466 /* this structure is used by modules to determine the size of some critical types */
467 struct dcesrv_critical_sizes
{
468 int interface_version
;
469 int sizeof_dcesrv_context
;
470 int sizeof_dcesrv_endpoint
;
471 int sizeof_dcesrv_endpoint_server
;
472 int sizeof_dcesrv_interface
;
473 int sizeof_dcesrv_if_list
;
474 int sizeof_dcesrv_connection
;
475 int sizeof_dcesrv_call_state
;
476 int sizeof_dcesrv_auth
;
477 int sizeof_dcesrv_handle
;
480 NTSTATUS
dcesrv_auth_type_principal_register(struct dcesrv_context
*dce_ctx
,
481 enum dcerpc_AuthType auth_type
,
482 const char *principal_name
);
483 const char *dcesrv_auth_type_principal_find(struct dcesrv_context
*dce_ctx
,
484 enum dcerpc_AuthType auth_type
);
485 NTSTATUS
dcesrv_register_default_auth_types(struct dcesrv_context
*dce_ctx
,
486 const char *principal
);
487 NTSTATUS
dcesrv_register_default_auth_types_machine_principal(struct dcesrv_context
*dce_ctx
);
488 NTSTATUS
dcesrv_interface_register(struct dcesrv_context
*dce_ctx
,
490 const char *ncacn_np_secondary_endpoint
,
491 const struct dcesrv_interface
*iface
,
492 const struct security_descriptor
*sd
);
493 NTSTATUS
dcesrv_interface_register_b(struct dcesrv_context
*dce_ctx
,
494 struct dcerpc_binding
*binding
,
495 struct dcerpc_binding
*binding2
,
496 const struct dcesrv_interface
*iface
,
497 const struct security_descriptor
*sd
);
498 NTSTATUS
dcerpc_register_ep_server(const struct dcesrv_endpoint_server
*ep_server
);
499 NTSTATUS
dcesrv_init_ep_servers(struct dcesrv_context
*dce_ctx
,
500 const char **ep_servers
);
501 NTSTATUS
dcesrv_init_registered_ep_servers(struct dcesrv_context
*dce_ctx
);
502 NTSTATUS
dcesrv_shutdown_registered_ep_servers(struct dcesrv_context
*dce_ctx
);
503 NTSTATUS
dcesrv_init_ep_server(struct dcesrv_context
*dce_ctx
,
504 const char *ep_server_name
);
505 NTSTATUS
dcesrv_shutdown_ep_server(struct dcesrv_context
*dce_ctx
,
507 const struct dcesrv_endpoint_server
*dcesrv_ep_server_byname(const char *name
);
509 NTSTATUS
dcesrv_init_context(TALLOC_CTX
*mem_ctx
,
510 struct loadparm_context
*lp_ctx
,
511 struct dcesrv_context_callbacks
*cb
,
512 struct dcesrv_context
**_dce_ctx
);
513 void dcesrv_context_set_callbacks(
514 struct dcesrv_context
*dce_ctx
,
515 struct dcesrv_context_callbacks
*cb
);
518 * Use dcesrv_async_reply() in async code
520 NTSTATUS
dcesrv_reply(struct dcesrv_call_state
*call
);
521 void _dcesrv_async_reply(struct dcesrv_call_state
*call
,
523 const char *location
);
524 #define dcesrv_async_reply(__call) \
525 _dcesrv_async_reply(__call, __func__, __location__)
527 struct dcesrv_handle
*dcesrv_handle_create(struct dcesrv_call_state
*call
,
528 uint8_t handle_type
);
530 struct dcesrv_handle
*dcesrv_handle_lookup(struct dcesrv_call_state
*call
,
531 const struct policy_handle
*p
,
532 uint8_t handle_type
);
534 const struct tsocket_address
*dcesrv_connection_get_local_address(struct dcesrv_connection
*conn
);
535 const struct tsocket_address
*dcesrv_connection_get_remote_address(struct dcesrv_connection
*conn
);
538 * Fetch the authentication session key if available.
540 * This is the key generated by a gensec authentication.
542 NTSTATUS
dcesrv_auth_session_key(struct dcesrv_call_state
*call
,
543 DATA_BLOB
*session_key
);
546 * Fetch the transport session key if available.
547 * Typically this is the SMB session key
548 * or a fixed key for local transports.
550 * The key is always truncated to 16 bytes.
552 NTSTATUS
dcesrv_transport_session_key(struct dcesrv_call_state
*call
,
553 DATA_BLOB
*session_key
);
555 /* a useful macro for generating a RPC fault in the backend code */
556 #define DCESRV_FAULT(code) do { \
557 dce_call->fault_code = code; \
558 return r->out.result; \
561 /* a useful macro for generating a RPC fault in the backend code */
562 #define DCESRV_FAULT_VOID(code) do { \
563 dce_call->fault_code = code; \
567 /* a useful macro for checking the validity of a dcerpc policy handle
568 and giving the right fault code if invalid */
569 #define DCESRV_CHECK_HANDLE(h) do {if (!(h)) DCESRV_FAULT(DCERPC_FAULT_CONTEXT_MISMATCH); } while (0)
571 /* this checks for a valid policy handle, and gives a fault if an
572 invalid handle or retval if the handle is of the
574 #define DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, retval) do { \
575 (h) = dcesrv_handle_lookup(dce_call, (inhandle), DCESRV_HANDLE_ANY); \
576 DCESRV_CHECK_HANDLE(h); \
577 if ((t) != DCESRV_HANDLE_ANY && (h)->wire_handle.handle_type != (t)) { \
582 /* this checks for a valid policy handle and gives a dcerpc fault
583 if its the wrong type of handle */
584 #define DCESRV_PULL_HANDLE_FAULT(h, inhandle, t) do { \
585 (h) = dcesrv_handle_lookup(dce_call, (inhandle), t); \
586 DCESRV_CHECK_HANDLE(h); \
589 #define DCESRV_PULL_HANDLE(h, inhandle, t) DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, NT_STATUS_INVALID_HANDLE)
590 #define DCESRV_PULL_HANDLE_WERR(h, inhandle, t) DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, WERR_INVALID_HANDLE)
593 * retrieve credentials from a dce_call
595 _PUBLIC_
struct cli_credentials
*dcesrv_call_credentials(struct dcesrv_call_state
*dce_call
);
598 * returns true if this is an authenticated call
600 _PUBLIC_
bool dcesrv_call_authenticated(struct dcesrv_call_state
*dce_call
);
603 * retrieve account_name for a dce_call
605 _PUBLIC_
const char *dcesrv_call_account_name(struct dcesrv_call_state
*dce_call
);
608 * retrieve session_info from a dce_call
610 _PUBLIC_
struct auth_session_info
*dcesrv_call_session_info(struct dcesrv_call_state
*dce_call
);
613 * retrieve auth type/level from a dce_call
615 _PUBLIC_
void dcesrv_call_auth_info(struct dcesrv_call_state
*dce_call
,
616 enum dcerpc_AuthType
*auth_type
,
617 enum dcerpc_AuthLevel
*auth_level
);
619 _PUBLIC_ NTSTATUS
dcesrv_interface_bind_require_integrity(struct dcesrv_connection_context
*context
,
620 const struct dcesrv_interface
*iface
);
621 _PUBLIC_ NTSTATUS
dcesrv_interface_bind_require_privacy(struct dcesrv_connection_context
*context
,
622 const struct dcesrv_interface
*iface
);
623 _PUBLIC_ NTSTATUS
dcesrv_interface_bind_reject_connect(struct dcesrv_connection_context
*context
,
624 const struct dcesrv_interface
*iface
);
625 _PUBLIC_ NTSTATUS
dcesrv_interface_bind_allow_connect(struct dcesrv_connection_context
*context
,
626 const struct dcesrv_interface
*iface
);
628 _PUBLIC_ NTSTATUS
_dcesrv_iface_state_store_assoc(
629 struct dcesrv_call_state
*call
,
632 const char *location
);
633 #define dcesrv_iface_state_store_assoc(call, magic, ptr) \
634 _dcesrv_iface_state_store_assoc((call), (magic), (ptr), \
636 _PUBLIC_
void *_dcesrv_iface_state_find_assoc(
637 struct dcesrv_call_state
*call
,
639 #define dcesrv_iface_state_find_assoc(call, magic, _type) \
641 _dcesrv_iface_state_find_assoc((call), (magic)), \
644 _PUBLIC_ NTSTATUS
_dcesrv_iface_state_store_conn(
645 struct dcesrv_call_state
*call
,
648 const char *location
);
649 #define dcesrv_iface_state_store_conn(call, magic, ptr) \
650 _dcesrv_iface_state_store_conn((call), (magic), (ptr), \
652 _PUBLIC_
void *_dcesrv_iface_state_find_conn(
653 struct dcesrv_call_state
*call
,
655 #define dcesrv_iface_state_find_conn(call, magic, _type) \
657 _dcesrv_iface_state_find_conn((call), (magic)), \
660 _PUBLIC_
void dcesrv_cleanup_broken_connections(struct dcesrv_context
*dce_ctx
);
662 _PUBLIC_ NTSTATUS
dcesrv_endpoint_connect(struct dcesrv_context
*dce_ctx
,
664 const struct dcesrv_endpoint
*ep
,
665 struct auth_session_info
*session_info
,
666 struct tevent_context
*event_ctx
,
667 uint32_t state_flags
,
668 struct dcesrv_connection
**_p
);
669 _PUBLIC_ NTSTATUS
dcesrv_find_endpoint(struct dcesrv_context
*dce_ctx
,
670 const struct dcerpc_binding
*ep_description
,
671 struct dcesrv_endpoint
**_out
);
673 _PUBLIC_
void dcesrv_terminate_connection(struct dcesrv_connection
*dce_conn
,
675 _PUBLIC_
void dcesrv_sock_report_output_data(struct dcesrv_connection
*dce_conn
);
677 _PUBLIC_ NTSTATUS
dcesrv_connection_loop_start(struct dcesrv_connection
*conn
);
679 _PUBLIC_
void dcesrv_loop_next_packet(
680 struct dcesrv_connection
*dce_conn
,
681 struct ncacn_packet
*pkt
,
684 _PUBLIC_ NTSTATUS
dcesrv_call_dispatch_local(struct dcesrv_call_state
*call
);
686 _PUBLIC_
const struct dcesrv_interface
*find_interface_by_syntax_id(
687 const struct dcesrv_endpoint
*endpoint
,
688 const struct ndr_syntax_id
*interface
);
690 void _dcesrv_save_ndr_fuzz_seed(DATA_BLOB call_blob
,
691 struct dcesrv_call_state
*call
,
692 ndr_flags_type flags
);
695 #define dcesrv_save_ndr_fuzz_seed(stub, call, flags) \
696 _dcesrv_save_ndr_fuzz_seed(stub, call, flags)
698 #define dcesrv_save_ndr_fuzz_seed(stub, call, flags) \
703 #endif /* _LIBRPC_RPC_DCESRV_CORE_H_ */