s3: smbd: Change create_junction() to use parse_dfs_path_strict().
[Samba.git] / librpc / rpc / dcesrv_core.h
blob69815b71f3dd91b025e49c51fb655081f2068408
1 /*
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;
40 struct dcesrv_auth;
41 struct dcesrv_connection_context;
42 struct dcesrv_iface_state;
43 struct cli_credentials;
45 struct dcesrv_interface {
46 const char *name;
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;
78 uint64_t flags;
81 #define DCESRV_INTERFACE_FLAGS_HANDLES_NOT_USED 0x00000001
83 enum dcesrv_call_list {
84 DCESRV_LIST_NONE,
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;
92 DATA_BLOB blob;
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
116 * and skip the reply
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 uint32_t state_flags;
130 /* the time the request arrived in the server */
131 struct timeval time;
133 /* the backend can use this event context for async replies */
134 struct tevent_context *event_ctx;
136 /* this is the pointer to the allocated function struct */
137 void *r;
140 * that's the ndr pull context used in dcesrv_request()
141 * needed by dcesrv_reply() to carry over information
142 * for full pointer support.
144 struct ndr_pull *ndr_pull;
146 DATA_BLOB input;
148 struct data_blob_list_item *replies;
150 /* this is used by the boilerplate code to generate DCERPC faults */
151 uint32_t fault_code;
153 /* the reason why we terminate the connection after sending a response */
154 const char *terminate_reason;
156 /* temporary auth_info fields */
157 struct dcerpc_auth in_auth_info;
158 struct dcerpc_auth _out_auth_info;
159 struct dcerpc_auth *out_auth_info;
163 * DCERPC Handles
164 * --------------
165 * The various handles that are used in the RPC servers should be
166 * created and fetch using the dcesrv_handle_* functions.
168 * Use
169 * dcesrv_handle_create(struct dcesrv_call_state \*, uint8 handle_type)
170 * to obtain a new handle of the specified type. Handle types are
171 * unique within each pipe.
173 * The handle can later be fetched again using:
175 * struct dcesrv_handle *dcesrv_handle_lookup(
176 * struct dcesrv_call_state *dce_call,
177 * struct policy_handle *p,
178 * uint8 handle_type)
180 * and destroyed by:
182 * TALLOC_FREE(struct dcesrv_handle *).
184 * User data should be stored in the 'data' member of the dcesrv_handle
185 * struct.
188 #define DCESRV_HANDLE_ANY 255
190 /* a dcerpc handle in internal format */
191 struct dcesrv_handle {
192 struct dcesrv_handle *next, *prev;
193 struct dcesrv_assoc_group *assoc_group;
194 struct policy_handle wire_handle;
195 struct dom_sid sid;
196 enum dcerpc_AuthLevel min_auth_level;
197 const struct dcesrv_interface *iface;
198 void *data;
201 /* hold the authentication state information */
202 struct dcesrv_auth {
203 struct dcesrv_auth *prev, *next;
204 enum dcerpc_AuthType auth_type;
205 enum dcerpc_AuthLevel auth_level;
206 uint32_t auth_context_id;
207 struct gensec_security *gensec_security;
208 struct auth_session_info *session_info;
209 NTSTATUS (*session_key_fn)(struct dcesrv_auth *, DATA_BLOB *session_key);
210 bool auth_started;
211 bool auth_finished;
212 bool auth_audited;
213 bool auth_invalid;
216 struct dcesrv_connection_context {
217 struct dcesrv_connection_context *next, *prev;
218 uint16_t context_id;
220 /* the connection this is on */
221 struct dcesrv_connection *conn;
223 /* the ndr function table for the chosen interface */
224 const struct dcesrv_interface *iface;
227 * the minimum required auth level for this interface
229 enum dcerpc_AuthLevel min_auth_level;
230 bool allow_connect;
232 /* the negotiated transfer syntax */
233 struct ndr_syntax_id transfer_syntax;
237 /* the state associated with a dcerpc server connection */
238 struct dcesrv_connection {
239 /* for the broken_connections DLIST */
240 struct dcesrv_connection *prev, *next;
242 /* the top level context for this server */
243 struct dcesrv_context *dce_ctx;
245 /* the endpoint that was opened */
246 const struct dcesrv_endpoint *endpoint;
248 /* a list of established context_ids */
249 struct dcesrv_connection_context *contexts;
251 /* the state of the current incoming call fragments */
252 struct dcesrv_call_state *incoming_fragmented_call_list;
254 /* the state of the async pending calls */
255 struct dcesrv_call_state *pending_call_list;
257 /* the state of the current outgoing calls */
258 struct dcesrv_call_state *call_list;
260 /* the maximum size the client wants to receive */
261 uint16_t max_recv_frag;
262 uint16_t max_xmit_frag;
264 DATA_BLOB partial_input;
266 /* the event_context that will be used for this connection */
267 struct tevent_context *event_ctx;
269 /* is this connection pending termination? If so, why? */
270 const char *terminate;
272 const char *packet_log_dir;
274 /* this is the default state_flags for dcesrv_call_state structs */
275 uint32_t state_flags;
277 struct {
278 void *private_data;
279 void (*report_output_data)(struct dcesrv_connection *);
280 void (*terminate_connection)(struct dcesrv_connection *,
281 const char *);
282 } transport;
284 struct tstream_context *stream;
285 struct tevent_queue *send_queue;
287 const struct tsocket_address *local_address;
288 const struct tsocket_address *remote_address;
290 /* the current authentication state */
291 struct dcesrv_auth *default_auth_state;
292 size_t max_auth_states;
293 struct dcesrv_auth *auth_states;
294 bool got_explicit_auth_level_connect;
295 struct dcesrv_auth *default_auth_level_connect;
296 bool client_hdr_signing;
297 bool support_hdr_signing;
298 bool negotiated_hdr_signing;
301 * remember which pdu types are allowed
303 bool allow_bind;
304 bool allow_alter;
306 /* the association group the connection belongs to */
307 struct dcesrv_assoc_group *assoc_group;
309 /* The maximum total payload of reassembled request pdus */
310 size_t max_total_request_size;
313 * Our preferred transfer syntax.
315 const struct ndr_syntax_id *preferred_transfer;
318 * This is used to block the connection during
319 * pending authentication.
321 struct tevent_req *(*wait_send)(TALLOC_CTX *mem_ctx,
322 struct tevent_context *ev,
323 void *private_data);
324 NTSTATUS (*wait_recv)(struct tevent_req *req);
325 void *wait_private;
329 struct dcesrv_endpoint_server {
330 /* this is the name of the endpoint server */
331 const char *name;
333 /* true if the endpoint server has been initialized */
334 bool initialized;
336 /* this function should register endpoints and some other setup stuff,
337 * it is called when the dcesrv_context gets initialized.
339 NTSTATUS (*init_server)(struct dcesrv_context *, const struct dcesrv_endpoint_server *);
341 /* this function should cleanup endpoint server state and unregister
342 * the endpoint server from dcesrv_context */
343 NTSTATUS (*shutdown_server)(struct dcesrv_context *, const struct dcesrv_endpoint_server *);
345 /* this function can be used by other endpoint servers to
346 * ask for a dcesrv_interface implementation
347 * - iface must be reference to an already existing struct !
349 bool (*interface_by_uuid)(struct dcesrv_interface *iface, const struct GUID *, uint32_t);
351 /* this function can be used by other endpoint servers to
352 * ask for a dcesrv_interface implementation
353 * - iface must be reference to an already existing struct !
355 bool (*interface_by_name)(struct dcesrv_interface *iface, const char *);
359 /* one association groups */
360 struct dcesrv_assoc_group {
361 /* the wire id */
362 uint32_t id;
364 /* The transport this is valid on */
365 enum dcerpc_transport_t transport;
367 /* list of handles in this association group */
368 struct dcesrv_handle *handles;
371 * list of iface states per assoc/conn
373 struct dcesrv_iface_state *iface_states;
375 /* parent context */
376 struct dcesrv_context *dce_ctx;
378 /* the negotiated bind time features */
379 uint16_t bind_time_features;
382 struct dcesrv_context_callbacks {
383 struct {
384 void (*successful_authz)(
385 struct dcesrv_call_state *call, void *private_data);
386 void *private_data;
387 } log;
388 struct {
389 NTSTATUS (*gensec_prepare)(
390 TALLOC_CTX *mem_ctx,
391 struct dcesrv_call_state *call,
392 struct gensec_security **out,
393 void *private_data);
394 void *private_data;
395 void (*become_root)(void);
396 void (*unbecome_root)(void);
397 } auth;
398 struct {
399 NTSTATUS (*find)(
400 struct dcesrv_call_state *call, void *private_data);
401 void *private_data;
402 } assoc_group;
405 /* server-wide context information for the dcerpc server */
406 struct dcesrv_context {
408 * The euid at startup time.
410 * This is required for DCERPC_AUTH_TYPE_NCALRPC_AS_SYSTEM
412 uid_t initial_euid;
414 /* the list of endpoints that have registered
415 * by the configured endpoint servers
417 struct dcesrv_endpoint {
418 struct dcesrv_endpoint *next, *prev;
419 /* the type and location of the endpoint */
420 struct dcerpc_binding *ep_description;
421 /* the secondary endpoint description for the BIND_ACK */
422 struct dcerpc_binding *ep_2nd_description;
423 /* the security descriptor for smb named pipes */
424 struct security_descriptor *sd;
425 /* the list of interfaces available on this endpoint */
426 struct dcesrv_if_list {
427 struct dcesrv_if_list *next, *prev;
428 struct dcesrv_interface *iface;
429 } *interface_list;
432 * Should this service be run in a single process (so far only
433 * NETLOGON is not run in a single process)
435 bool use_single_process;
436 } *endpoint_list;
438 /* loadparm context to use for this connection */
439 struct loadparm_context *lp_ctx;
441 struct idr_context *assoc_groups_idr;
443 struct dcesrv_connection *broken_connections;
445 struct dcesrv_context_callbacks *callbacks;
448 /* this structure is used by modules to determine the size of some critical types */
449 struct dcesrv_critical_sizes {
450 int interface_version;
451 int sizeof_dcesrv_context;
452 int sizeof_dcesrv_endpoint;
453 int sizeof_dcesrv_endpoint_server;
454 int sizeof_dcesrv_interface;
455 int sizeof_dcesrv_if_list;
456 int sizeof_dcesrv_connection;
457 int sizeof_dcesrv_call_state;
458 int sizeof_dcesrv_auth;
459 int sizeof_dcesrv_handle;
462 NTSTATUS dcesrv_interface_register(struct dcesrv_context *dce_ctx,
463 const char *ep_name,
464 const char *ncacn_np_secondary_endpoint,
465 const struct dcesrv_interface *iface,
466 const struct security_descriptor *sd);
467 NTSTATUS dcesrv_interface_register_b(struct dcesrv_context *dce_ctx,
468 struct dcerpc_binding *binding,
469 struct dcerpc_binding *binding2,
470 const struct dcesrv_interface *iface,
471 const struct security_descriptor *sd);
472 NTSTATUS dcerpc_register_ep_server(const struct dcesrv_endpoint_server *ep_server);
473 NTSTATUS dcesrv_init_ep_servers(struct dcesrv_context *dce_ctx,
474 const char **ep_servers);
475 NTSTATUS dcesrv_init_registered_ep_servers(struct dcesrv_context *dce_ctx);
476 NTSTATUS dcesrv_shutdown_registered_ep_servers(struct dcesrv_context *dce_ctx);
477 NTSTATUS dcesrv_init_ep_server(struct dcesrv_context *dce_ctx,
478 const char *ep_server_name);
479 NTSTATUS dcesrv_shutdown_ep_server(struct dcesrv_context *dce_ctx,
480 const char *name);
481 const struct dcesrv_endpoint_server *dcesrv_ep_server_byname(const char *name);
483 NTSTATUS dcesrv_init_context(TALLOC_CTX *mem_ctx,
484 struct loadparm_context *lp_ctx,
485 struct dcesrv_context_callbacks *cb,
486 struct dcesrv_context **_dce_ctx);
487 void dcesrv_context_set_callbacks(
488 struct dcesrv_context *dce_ctx,
489 struct dcesrv_context_callbacks *cb);
491 NTSTATUS dcesrv_reply(struct dcesrv_call_state *call);
492 struct dcesrv_handle *dcesrv_handle_create(struct dcesrv_call_state *call,
493 uint8_t handle_type);
495 struct dcesrv_handle *dcesrv_handle_lookup(struct dcesrv_call_state *call,
496 const struct policy_handle *p,
497 uint8_t handle_type);
499 const struct tsocket_address *dcesrv_connection_get_local_address(struct dcesrv_connection *conn);
500 const struct tsocket_address *dcesrv_connection_get_remote_address(struct dcesrv_connection *conn);
503 * Fetch the authentication session key if available.
505 * This is the key generated by a gensec authentication.
507 NTSTATUS dcesrv_auth_session_key(struct dcesrv_call_state *call,
508 DATA_BLOB *session_key);
511 * Fetch the transport session key if available.
512 * Typically this is the SMB session key
513 * or a fixed key for local transports.
515 * The key is always truncated to 16 bytes.
517 NTSTATUS dcesrv_transport_session_key(struct dcesrv_call_state *call,
518 DATA_BLOB *session_key);
520 /* a useful macro for generating a RPC fault in the backend code */
521 #define DCESRV_FAULT(code) do { \
522 dce_call->fault_code = code; \
523 return r->out.result; \
524 } while(0)
526 /* a useful macro for generating a RPC fault in the backend code */
527 #define DCESRV_FAULT_VOID(code) do { \
528 dce_call->fault_code = code; \
529 return; \
530 } while(0)
532 /* a useful macro for checking the validity of a dcerpc policy handle
533 and giving the right fault code if invalid */
534 #define DCESRV_CHECK_HANDLE(h) do {if (!(h)) DCESRV_FAULT(DCERPC_FAULT_CONTEXT_MISMATCH); } while (0)
536 /* this checks for a valid policy handle, and gives a fault if an
537 invalid handle or retval if the handle is of the
538 wrong type */
539 #define DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, retval) do { \
540 (h) = dcesrv_handle_lookup(dce_call, (inhandle), DCESRV_HANDLE_ANY); \
541 DCESRV_CHECK_HANDLE(h); \
542 if ((t) != DCESRV_HANDLE_ANY && (h)->wire_handle.handle_type != (t)) { \
543 return retval; \
545 } while (0)
547 /* this checks for a valid policy handle and gives a dcerpc fault
548 if its the wrong type of handle */
549 #define DCESRV_PULL_HANDLE_FAULT(h, inhandle, t) do { \
550 (h) = dcesrv_handle_lookup(dce_call, (inhandle), t); \
551 DCESRV_CHECK_HANDLE(h); \
552 } while (0)
554 #define DCESRV_PULL_HANDLE(h, inhandle, t) DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, NT_STATUS_INVALID_HANDLE)
555 #define DCESRV_PULL_HANDLE_WERR(h, inhandle, t) DCESRV_PULL_HANDLE_RETVAL(h, inhandle, t, WERR_INVALID_HANDLE)
558 * retrieve credentials from a dce_call
560 _PUBLIC_ struct cli_credentials *dcesrv_call_credentials(struct dcesrv_call_state *dce_call);
563 * returns true if this is an authenticated call
565 _PUBLIC_ bool dcesrv_call_authenticated(struct dcesrv_call_state *dce_call);
568 * retrieve account_name for a dce_call
570 _PUBLIC_ const char *dcesrv_call_account_name(struct dcesrv_call_state *dce_call);
573 * retrieve session_info from a dce_call
575 _PUBLIC_ struct auth_session_info *dcesrv_call_session_info(struct dcesrv_call_state *dce_call);
578 * retrieve auth type/level from a dce_call
580 _PUBLIC_ void dcesrv_call_auth_info(struct dcesrv_call_state *dce_call,
581 enum dcerpc_AuthType *auth_type,
582 enum dcerpc_AuthLevel *auth_level);
584 _PUBLIC_ NTSTATUS dcesrv_interface_bind_require_integrity(struct dcesrv_connection_context *context,
585 const struct dcesrv_interface *iface);
586 _PUBLIC_ NTSTATUS dcesrv_interface_bind_require_privacy(struct dcesrv_connection_context *context,
587 const struct dcesrv_interface *iface);
588 _PUBLIC_ NTSTATUS dcesrv_interface_bind_reject_connect(struct dcesrv_connection_context *context,
589 const struct dcesrv_interface *iface);
590 _PUBLIC_ NTSTATUS dcesrv_interface_bind_allow_connect(struct dcesrv_connection_context *context,
591 const struct dcesrv_interface *iface);
593 _PUBLIC_ NTSTATUS _dcesrv_iface_state_store_assoc(
594 struct dcesrv_call_state *call,
595 uint64_t magic,
596 void *ptr,
597 const char *location);
598 #define dcesrv_iface_state_store_assoc(call, magic, ptr) \
599 _dcesrv_iface_state_store_assoc((call), (magic), (ptr), \
600 __location__)
601 _PUBLIC_ void *_dcesrv_iface_state_find_assoc(
602 struct dcesrv_call_state *call,
603 uint64_t magic);
604 #define dcesrv_iface_state_find_assoc(call, magic, _type) \
605 talloc_get_type( \
606 _dcesrv_iface_state_find_assoc((call), (magic)), \
607 _type)
609 _PUBLIC_ NTSTATUS _dcesrv_iface_state_store_conn(
610 struct dcesrv_call_state *call,
611 uint64_t magic,
612 void *_pptr,
613 const char *location);
614 #define dcesrv_iface_state_store_conn(call, magic, ptr) \
615 _dcesrv_iface_state_store_conn((call), (magic), (ptr), \
616 __location__)
617 _PUBLIC_ void *_dcesrv_iface_state_find_conn(
618 struct dcesrv_call_state *call,
619 uint64_t magic);
620 #define dcesrv_iface_state_find_conn(call, magic, _type) \
621 talloc_get_type( \
622 _dcesrv_iface_state_find_conn((call), (magic)), \
623 _type)
625 _PUBLIC_ void dcesrv_cleanup_broken_connections(struct dcesrv_context *dce_ctx);
627 _PUBLIC_ NTSTATUS dcesrv_endpoint_connect(struct dcesrv_context *dce_ctx,
628 TALLOC_CTX *mem_ctx,
629 const struct dcesrv_endpoint *ep,
630 struct auth_session_info *session_info,
631 struct tevent_context *event_ctx,
632 uint32_t state_flags,
633 struct dcesrv_connection **_p);
634 _PUBLIC_ NTSTATUS dcesrv_find_endpoint(struct dcesrv_context *dce_ctx,
635 const struct dcerpc_binding *ep_description,
636 struct dcesrv_endpoint **_out);
638 _PUBLIC_ void dcesrv_terminate_connection(struct dcesrv_connection *dce_conn,
639 const char *reason);
640 _PUBLIC_ void dcesrv_sock_report_output_data(struct dcesrv_connection *dce_conn);
642 _PUBLIC_ NTSTATUS dcesrv_connection_loop_start(struct dcesrv_connection *conn);
644 _PUBLIC_ void dcesrv_loop_next_packet(
645 struct dcesrv_connection *dce_conn,
646 struct ncacn_packet *pkt,
647 DATA_BLOB buffer);
649 _PUBLIC_ NTSTATUS dcesrv_call_dispatch_local(struct dcesrv_call_state *call);
651 _PUBLIC_ const struct dcesrv_interface *find_interface_by_syntax_id(
652 const struct dcesrv_endpoint *endpoint,
653 const struct ndr_syntax_id *interface);
655 void _dcesrv_save_ndr_fuzz_seed(DATA_BLOB call_blob,
656 struct dcesrv_call_state *call,
657 int flags);
659 #if DEVELOPER
660 #define dcesrv_save_ndr_fuzz_seed(stub, call, flags) \
661 _dcesrv_save_ndr_fuzz_seed(stub, call, flags)
662 #else
663 #define dcesrv_save_ndr_fuzz_seed(stub, call, flags) \
664 /* */
665 #endif
668 #endif /* _LIBRPC_RPC_DCESRV_CORE_H_ */