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"
26 #define DBGC_CLASS DBGC_RPC_SRV
28 static int pipes_open;
30 static pipes_struct *InternalPipes;
33 * the following prototypes are declared here to avoid
34 * code being moved about too much for a patch to be
35 * disrupted / less obvious.
37 * these functions, and associated functions that they
38 * call, should be moved behind a .so module-loading
39 * system _anyway_. so that's the next step...
42 static int close_internal_rpc_pipe_hnd(struct pipes_struct *p);
44 /****************************************************************************
45 Internal Pipe iterator functions.
46 ****************************************************************************/
48 pipes_struct *get_first_internal_pipe(void)
53 pipes_struct *get_next_internal_pipe(pipes_struct *p)
58 static void free_pipe_rpc_context_internal( PIPE_RPC_FNS *list )
60 PIPE_RPC_FNS *tmp = list;
72 bool check_open_pipes(void)
76 for (p = InternalPipes; p != NULL; p = p->next) {
77 if (num_pipe_handles(p) != 0) {
84 /****************************************************************************
86 ****************************************************************************/
88 static int close_internal_rpc_pipe_hnd(struct pipes_struct *p)
91 DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
95 if (p->auth.auth_data_free_func) {
96 (*p->auth.auth_data_free_func)(&p->auth);
99 free_pipe_rpc_context_internal( p->contexts );
101 /* Free the handles database. */
102 close_policy_by_pipe(p);
104 DLIST_REMOVE(InternalPipes, p);
111 /****************************************************************************
112 Make an internal namedpipes structure
113 ****************************************************************************/
115 struct pipes_struct *make_internal_rpc_pipe_p(TALLOC_CTX *mem_ctx,
116 const struct ndr_syntax_id *syntax,
117 const char *client_address,
118 struct auth_serversupplied_info *server_info)
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"));
148 * Initialize the incoming RPC data buffer with one PDU worth of memory.
149 * We cheat here and say we're marshalling, as we intend to add incoming
150 * data directly into the prs_struct and we want it to auto grow. We will
151 * change the type to UNMARSALLING before processing the stream.
154 if(!prs_init(&p->in_data.data, 128, p->mem_ctx, MARSHALL)) {
155 DEBUG(0,("open_rpc_pipe_p: malloc fail for in_data struct.\n"));
156 close_policy_by_pipe(p);
161 p->server_info = copy_serverinfo(p, server_info);
162 if (p->server_info == NULL) {
163 DEBUG(0, ("open_rpc_pipe_p: copy_serverinfo failed\n"));
164 close_policy_by_pipe(p);
169 DLIST_ADD(InternalPipes, p);
171 strlcpy(p->client_address, client_address, sizeof(p->client_address));
173 p->endian = RPC_LITTLE_ENDIAN;
177 DEBUG(4,("Created internal pipe %s (pipes_open=%d)\n",
178 get_pipe_name_from_syntax(talloc_tos(), syntax), pipes_open));
180 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
185 /****************************************************************************
186 ****************************************************************************/
188 static NTSTATUS internal_ndr_push(TALLOC_CTX *mem_ctx,
189 struct rpc_pipe_client *cli,
190 const struct ndr_interface_table *table,
194 const struct ndr_interface_call *call;
195 struct ndr_push *push;
196 enum ndr_err_code ndr_err;
200 if (!ndr_syntax_id_equal(&table->syntax_id, &cli->abstract_syntax) ||
201 (opnum >= table->num_calls)) {
202 return NT_STATUS_INVALID_PARAMETER;
205 call = &table->calls[opnum];
207 if (DEBUGLEVEL >= 10) {
208 ndr_print_function_debug(call->ndr_print,
209 call->name, NDR_IN, r);
212 push = ndr_push_init_ctx(mem_ctx);
214 return NT_STATUS_NO_MEMORY;
217 ndr_err = call->ndr_push(push, NDR_IN, r);
218 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
220 return ndr_map_error2ntstatus(ndr_err);
223 blob = ndr_push_blob(push);
224 ret = prs_init_data_blob(&cli->pipes_struct->in_data.data, &blob, mem_ctx);
227 return NT_STATUS_NO_MEMORY;
233 /****************************************************************************
234 ****************************************************************************/
236 static NTSTATUS internal_ndr_pull(TALLOC_CTX *mem_ctx,
237 struct rpc_pipe_client *cli,
238 const struct ndr_interface_table *table,
242 const struct ndr_interface_call *call;
243 struct ndr_pull *pull;
244 enum ndr_err_code ndr_err;
246 if (!ndr_syntax_id_equal(&table->syntax_id, &cli->abstract_syntax) ||
247 (opnum >= table->num_calls)) {
248 return NT_STATUS_INVALID_PARAMETER;
251 call = &table->calls[opnum];
253 pull = ndr_pull_init_blob(&cli->pipes_struct->out_data.rdata,
256 return NT_STATUS_NO_MEMORY;
259 /* have the ndr parser alloc memory for us */
260 pull->flags |= LIBNDR_FLAG_REF_ALLOC;
261 ndr_err = call->ndr_pull(pull, NDR_OUT, r);
264 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
265 return ndr_map_error2ntstatus(ndr_err);
268 if (DEBUGLEVEL >= 10) {
269 ndr_print_function_debug(call->ndr_print,
270 call->name, NDR_OUT, r);
276 /****************************************************************************
277 ****************************************************************************/
279 static NTSTATUS rpc_pipe_internal_dispatch(struct rpc_pipe_client *cli,
281 const struct ndr_interface_table *table,
282 uint32_t opnum, void *r)
285 int num_cmds = rpc_srv_get_pipe_num_cmds(&table->syntax_id);
286 const struct api_struct *cmds = rpc_srv_get_pipe_cmds(&table->syntax_id);
289 if (cli->pipes_struct == NULL) {
290 return NT_STATUS_INVALID_PARAMETER;
294 cli->pipes_struct->opnum = opnum;
296 for (i = 0; i < num_cmds; i++) {
297 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
303 return NT_STATUS_INVALID_PARAMETER;
306 status = internal_ndr_push(mem_ctx, cli, table, opnum, r);
307 if (!NT_STATUS_IS_OK(status)) {
311 if (!cmds[i].fn(cli->pipes_struct)) {
312 return NT_STATUS_UNSUCCESSFUL;
315 status = internal_ndr_pull(mem_ctx, cli, table, opnum, r);
316 if (!NT_STATUS_IS_OK(status)) {
320 prs_mem_free(&cli->pipes_struct->in_data.data);
321 data_blob_free(&cli->pipes_struct->out_data.rdata);
327 * @brief Create a new RPC client context which uses a local dispatch function.
329 * @param[in] mem_ctx The memory context to use.
331 * @param[in] abstract_syntax Normally the syntax_id of the autogenerated
334 * @param[in] dispatch The corresponding autogenerated dispatch function
335 * rpc_<name>_dispatch.
337 * @param[in] serversupplied_info The server supplied authentication function.
339 * @param[out] presult A pointer to store the connected rpc client pipe.
341 * @return NT_STATUS_OK on success, a corresponding NT status if an
345 * struct rpc_pipe_client *winreg_pipe;
348 * status = rpc_pipe_open_internal(tmp_ctx,
349 * &ndr_table_winreg.syntax_id,
350 * rpc_winreg_dispatch,
355 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
356 const struct ndr_syntax_id *abstract_syntax,
357 struct auth_serversupplied_info *serversupplied_info,
358 struct rpc_pipe_client **presult)
360 struct rpc_pipe_client *result;
362 result = TALLOC_ZERO_P(mem_ctx, struct rpc_pipe_client);
363 if (result == NULL) {
364 return NT_STATUS_NO_MEMORY;
367 result->abstract_syntax = *abstract_syntax;
368 result->transfer_syntax = ndr_transfer_syntax;
369 result->dispatch = rpc_pipe_internal_dispatch;
371 result->pipes_struct = make_internal_rpc_pipe_p(
372 result, abstract_syntax, "", serversupplied_info);
373 if (result->pipes_struct == NULL) {
375 return NT_STATUS_NO_MEMORY;
378 result->max_xmit_frag = -1;
379 result->max_recv_frag = -1;