2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1998,
5 * Largely re-written : 2005
6 * Copyright (C) Jeremy Allison 1998 - 2005
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "rpc_server/srv_pipe_internal.h"
27 #define DBGC_CLASS DBGC_RPC_SRV
29 static int pipes_open;
31 static struct pipes_struct *InternalPipes;
34 * the following prototypes are declared here to avoid
35 * code being moved about too much for a patch to be
36 * disrupted / less obvious.
38 * these functions, and associated functions that they
39 * call, should be moved behind a .so module-loading
40 * system _anyway_. so that's the next step...
43 /****************************************************************************
44 Internal Pipe iterator functions.
45 ****************************************************************************/
47 struct pipes_struct *get_first_internal_pipe(void)
52 struct pipes_struct *get_next_internal_pipe(struct pipes_struct *p)
57 static void free_pipe_rpc_context_internal( PIPE_RPC_FNS *list )
59 PIPE_RPC_FNS *tmp = list;
71 bool check_open_pipes(void)
73 struct pipes_struct *p;
75 for (p = InternalPipes; p != NULL; p = p->next) {
76 if (num_pipe_handles(p) != 0) {
83 /****************************************************************************
85 ****************************************************************************/
87 int close_internal_rpc_pipe_hnd(struct pipes_struct *p)
90 DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
94 if (p->auth.auth_data_free_func) {
95 (*p->auth.auth_data_free_func)(&p->auth);
98 free_pipe_rpc_context_internal( p->contexts );
100 /* Free the handles database. */
101 close_policy_by_pipe(p);
103 DLIST_REMOVE(InternalPipes, p);
110 /****************************************************************************
111 Make an internal namedpipes structure
112 ****************************************************************************/
114 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
115 const struct ndr_syntax_id *syntax,
116 struct client_address *client_id,
117 const struct auth_serversupplied_info *server_info,
118 struct messaging_context *msg_ctx)
120 struct pipes_struct *p;
122 DEBUG(4,("Create pipe requested %s\n",
123 get_pipe_name_from_syntax(talloc_tos(), syntax)));
125 p = TALLOC_ZERO_P(mem_ctx, struct pipes_struct);
128 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
132 p->mem_ctx = talloc_named(p, 0, "pipe %s %p",
133 get_pipe_name_from_syntax(talloc_tos(),
135 if (p->mem_ctx == NULL) {
136 DEBUG(0,("open_rpc_pipe_p: talloc_init failed.\n"));
141 if (!init_pipe_handles(p, syntax)) {
142 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
147 p->server_info = copy_serverinfo(p, server_info);
148 if (p->server_info == NULL) {
149 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
150 close_policy_by_pipe(p);
155 p->msg_ctx = msg_ctx;
157 DLIST_ADD(InternalPipes, p);
159 p->client_id = client_id;
161 p->endian = RPC_LITTLE_ENDIAN;
165 DEBUG(4,("Created internal pipe %s (pipes_open=%d)\n",
166 get_pipe_name_from_syntax(talloc_tos(), syntax), pipes_open));
168 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
173 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
176 const DATA_BLOB *in_data,
179 uint32_t num_cmds = rpc_srv_get_pipe_num_cmds(&p->syntax);
180 const struct api_struct *cmds = rpc_srv_get_pipe_cmds(&p->syntax);
187 for (i = 0; i < num_cmds; i++) {
188 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
194 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
197 p->in_data.data = *in_data;
198 p->out_data.rdata = data_blob_null;
201 p->in_data.data = data_blob_null;
203 data_blob_free(&p->out_data.rdata);
204 talloc_free_children(p->mem_ctx);
205 return NT_STATUS_RPC_CALL_FAILED;
208 if (p->fault_state) {
209 p->fault_state = false;
210 data_blob_free(&p->out_data.rdata);
211 talloc_free_children(p->mem_ctx);
212 return NT_STATUS_RPC_CALL_FAILED;
215 if (p->bad_handle_fault_state) {
216 p->bad_handle_fault_state = false;
217 data_blob_free(&p->out_data.rdata);
218 talloc_free_children(p->mem_ctx);
219 return NT_STATUS_RPC_SS_CONTEXT_MISMATCH;
222 if (p->rng_fault_state) {
223 p->rng_fault_state = false;
224 data_blob_free(&p->out_data.rdata);
225 talloc_free_children(p->mem_ctx);
226 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
229 *out_data = p->out_data.rdata;
230 talloc_steal(mem_ctx, out_data->data);
231 p->out_data.rdata = data_blob_null;
233 talloc_free_children(p->mem_ctx);
237 struct rpcint_bh_state {
238 struct pipes_struct *p;
241 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
243 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
244 struct rpcint_bh_state);
253 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
256 /* TODO: implement timeouts */
260 struct rpcint_bh_raw_call_state {
266 static struct tevent_req *rpcint_bh_raw_call_send(TALLOC_CTX *mem_ctx,
267 struct tevent_context *ev,
268 struct dcerpc_binding_handle *h,
269 const struct GUID *object,
272 const uint8_t *in_data,
275 struct rpcint_bh_state *hs =
276 dcerpc_binding_handle_data(h,
277 struct rpcint_bh_state);
278 struct tevent_req *req;
279 struct rpcint_bh_raw_call_state *state;
283 req = tevent_req_create(mem_ctx, &state,
284 struct rpcint_bh_raw_call_state);
288 state->in_data.data = discard_const_p(uint8_t, in_data);
289 state->in_data.length = in_length;
291 ok = rpcint_bh_is_connected(h);
293 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
294 return tevent_req_post(req, ev);
297 /* TODO: allow async */
298 status = rpcint_dispatch(hs->p, state, opnum,
301 if (!NT_STATUS_IS_OK(status)) {
302 tevent_req_nterror(req, status);
303 return tevent_req_post(req, ev);
306 tevent_req_done(req);
307 return tevent_req_post(req, ev);
310 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
316 struct rpcint_bh_raw_call_state *state =
318 struct rpcint_bh_raw_call_state);
321 if (tevent_req_is_nterror(req, &status)) {
322 tevent_req_received(req);
326 *out_data = talloc_move(mem_ctx, &state->out_data.data);
327 *out_length = state->out_data.length;
329 tevent_req_received(req);
333 struct rpcint_bh_disconnect_state {
337 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
338 struct tevent_context *ev,
339 struct dcerpc_binding_handle *h)
341 struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
342 struct rpcint_bh_state);
343 struct tevent_req *req;
344 struct rpcint_bh_disconnect_state *state;
347 req = tevent_req_create(mem_ctx, &state,
348 struct rpcint_bh_disconnect_state);
353 ok = rpcint_bh_is_connected(h);
355 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
356 return tevent_req_post(req, ev);
360 * TODO: do a real async disconnect ...
362 * For now the caller needs to free pipes_struct
366 tevent_req_done(req);
367 return tevent_req_post(req, ev);
370 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
374 if (tevent_req_is_nterror(req, &status)) {
375 tevent_req_received(req);
379 tevent_req_received(req);
383 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
388 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
390 const void *_struct_ptr,
391 const struct ndr_interface_call *call)
393 void *struct_ptr = discard_const(_struct_ptr);
395 if (DEBUGLEVEL < 10) {
399 if (ndr_flags & NDR_IN) {
400 ndr_print_function_debug(call->ndr_print,
405 if (ndr_flags & NDR_OUT) {
406 ndr_print_function_debug(call->ndr_print,
413 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
415 .is_connected = rpcint_bh_is_connected,
416 .set_timeout = rpcint_bh_set_timeout,
417 .raw_call_send = rpcint_bh_raw_call_send,
418 .raw_call_recv = rpcint_bh_raw_call_recv,
419 .disconnect_send = rpcint_bh_disconnect_send,
420 .disconnect_recv = rpcint_bh_disconnect_recv,
422 .ref_alloc = rpcint_bh_ref_alloc,
423 .do_ndr_print = rpcint_bh_do_ndr_print,
426 static NTSTATUS rpcint_binding_handle_ex(TALLOC_CTX *mem_ctx,
427 const struct ndr_syntax_id *abstract_syntax,
428 const struct ndr_interface_table *ndr_table,
429 struct client_address *client_id,
430 const struct auth_serversupplied_info *server_info,
431 struct messaging_context *msg_ctx,
432 struct dcerpc_binding_handle **binding_handle)
434 struct dcerpc_binding_handle *h;
435 struct rpcint_bh_state *hs;
438 abstract_syntax = &ndr_table->syntax_id;
441 h = dcerpc_binding_handle_create(mem_ctx,
446 struct rpcint_bh_state,
449 return NT_STATUS_NO_MEMORY;
451 hs->p = make_internal_rpc_pipe_p(hs,
458 return NT_STATUS_NO_MEMORY;
465 * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
467 * @param[in] mem_ctx The memory context to use.
469 * @param[in] ndr_table Normally the ndr_table_<name>.
471 * @param[in] client_id The info about the connected client.
473 * @param[in] serversupplied_info The server supplied authentication function.
475 * @param[in] msg_ctx The messaging context that can be used by the server
477 * @param[out] binding_handle A pointer to store the connected
478 * dcerpc_binding_handle
480 * @return NT_STATUS_OK on success, a corresponding NT status if an
484 * struct dcerpc_binding_handle *winreg_binding;
487 * status = rpcint_binding_handle(tmp_ctx,
495 NTSTATUS rpcint_binding_handle(TALLOC_CTX *mem_ctx,
496 const struct ndr_interface_table *ndr_table,
497 struct client_address *client_id,
498 const struct auth_serversupplied_info *server_info,
499 struct messaging_context *msg_ctx,
500 struct dcerpc_binding_handle **binding_handle)
502 return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, client_id,
503 server_info, msg_ctx, binding_handle);
507 * @brief Create a new RPC client context which uses a local dispatch function.
509 * @param[in] mem_ctx The memory context to use.
511 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
514 * @param[in] dispatch The corresponding autogenerated dispatch function
515 * rpc_<name>_dispatch.
517 * @param[in] serversupplied_info The server supplied authentication function.
519 * @param[out] presult A pointer to store the connected rpc client pipe.
521 * @return NT_STATUS_OK on success, a corresponding NT status if an
525 * struct rpc_pipe_client *winreg_pipe;
528 * status = rpc_pipe_open_internal(tmp_ctx,
529 * &ndr_table_winreg.syntax_id,
530 * rpc_winreg_dispatch,
535 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
536 const struct ndr_syntax_id *abstract_syntax,
537 struct auth_serversupplied_info *serversupplied_info,
538 struct client_address *client_id,
539 struct messaging_context *msg_ctx,
540 struct rpc_pipe_client **presult)
542 struct rpc_pipe_client *result;
545 result = TALLOC_ZERO_P(mem_ctx, struct rpc_pipe_client);
546 if (result == NULL) {
547 return NT_STATUS_NO_MEMORY;
550 result->abstract_syntax = *abstract_syntax;
551 result->transfer_syntax = ndr_transfer_syntax;
553 if (client_id == NULL) {
554 static struct client_address unknown;
555 strlcpy(unknown.addr, "<UNKNOWN>", sizeof(unknown.addr));
556 unknown.name = "<UNKNOWN>";
557 client_id = &unknown;
560 result->max_xmit_frag = -1;
561 result->max_recv_frag = -1;
563 status = rpcint_binding_handle_ex(result,
569 &result->binding_handle);
570 if (!NT_STATUS_IS_OK(status)) {