d702e4b4d7b03a8c612fc5d8ed05de7a18feb55d
[ira/wip.git] / source3 / rpc_server / rpc_ncacn_np_internal.c
1 /*
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
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 #include "includes.h"
23 #include "rpc_server/srv_pipe_internal.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_SRV
27
28 static int pipes_open;
29
30 static pipes_struct *InternalPipes;
31
32 /* TODO
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.
36  *
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...
40  */
41
42 static int close_internal_rpc_pipe_hnd(struct pipes_struct *p);
43
44 /****************************************************************************
45  Internal Pipe iterator functions.
46 ****************************************************************************/
47
48 pipes_struct *get_first_internal_pipe(void)
49 {
50         return InternalPipes;
51 }
52
53 pipes_struct *get_next_internal_pipe(pipes_struct *p)
54 {
55         return p->next;
56 }
57
58 static void free_pipe_rpc_context_internal( PIPE_RPC_FNS *list )
59 {
60         PIPE_RPC_FNS *tmp = list;
61         PIPE_RPC_FNS *tmp2;
62
63         while (tmp) {
64                 tmp2 = tmp->next;
65                 SAFE_FREE(tmp);
66                 tmp = tmp2;
67         }
68
69         return;
70 }
71
72 bool check_open_pipes(void)
73 {
74         pipes_struct *p;
75
76         for (p = InternalPipes; p != NULL; p = p->next) {
77                 if (num_pipe_handles(p) != 0) {
78                         return true;
79                 }
80         }
81         return false;
82 }
83
84 /****************************************************************************
85  Close an rpc pipe.
86 ****************************************************************************/
87
88 static int close_internal_rpc_pipe_hnd(struct pipes_struct *p)
89 {
90         if (!p) {
91                 DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
92                 return False;
93         }
94
95         if (p->auth.auth_data_free_func) {
96                 (*p->auth.auth_data_free_func)(&p->auth);
97         }
98
99         free_pipe_rpc_context_internal( p->contexts );
100
101         /* Free the handles database. */
102         close_policy_by_pipe(p);
103
104         DLIST_REMOVE(InternalPipes, p);
105
106         ZERO_STRUCTP(p);
107
108         return 0;
109 }
110
111 /****************************************************************************
112  Make an internal namedpipes structure
113 ****************************************************************************/
114
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)
119 {
120         pipes_struct *p;
121
122         DEBUG(4,("Create pipe requested %s\n",
123                  get_pipe_name_from_syntax(talloc_tos(), syntax)));
124
125         p = TALLOC_ZERO_P(mem_ctx, struct pipes_struct);
126
127         if (!p) {
128                 DEBUG(0,("ERROR! no memory for pipes_struct!\n"));
129                 return NULL;
130         }
131
132         p->mem_ctx = talloc_named(p, 0, "pipe %s %p",
133                                  get_pipe_name_from_syntax(talloc_tos(),
134                                                            syntax), p);
135         if (p->mem_ctx == NULL) {
136                 DEBUG(0,("open_rpc_pipe_p: talloc_init failed.\n"));
137                 TALLOC_FREE(p);
138                 return NULL;
139         }
140
141         if (!init_pipe_handles(p, syntax)) {
142                 DEBUG(0,("open_rpc_pipe_p: init_pipe_handles failed.\n"));
143                 TALLOC_FREE(p);
144                 return NULL;
145         }
146
147         /*
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.
152          */
153
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);
157                 TALLOC_FREE(p);
158                 return NULL;
159         }
160
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);
165                 TALLOC_FREE(p);
166                 return NULL;
167         }
168
169         DLIST_ADD(InternalPipes, p);
170
171         strlcpy(p->client_address, client_address, sizeof(p->client_address));
172
173         p->endian = RPC_LITTLE_ENDIAN;
174
175         /*
176          * Initialize the outgoing RPC data buffer with no memory.
177          */
178         prs_init_empty(&p->out_data.rdata, p->mem_ctx, MARSHALL);
179
180         p->syntax = *syntax;
181
182         DEBUG(4,("Created internal pipe %s (pipes_open=%d)\n",
183                  get_pipe_name_from_syntax(talloc_tos(), syntax), pipes_open));
184
185         talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
186
187         return p;
188 }
189
190 /****************************************************************************
191 ****************************************************************************/
192
193 static NTSTATUS internal_ndr_push(TALLOC_CTX *mem_ctx,
194                                   struct rpc_pipe_client *cli,
195                                   const struct ndr_interface_table *table,
196                                   uint32_t opnum,
197                                   void *r)
198 {
199         const struct ndr_interface_call *call;
200         struct ndr_push *push;
201         enum ndr_err_code ndr_err;
202         DATA_BLOB blob;
203         bool ret;
204
205         if (!ndr_syntax_id_equal(&table->syntax_id, &cli->abstract_syntax) ||
206             (opnum >= table->num_calls)) {
207                 return NT_STATUS_INVALID_PARAMETER;
208         }
209
210         call = &table->calls[opnum];
211
212         if (DEBUGLEVEL >= 10) {
213                 ndr_print_function_debug(call->ndr_print,
214                                          call->name, NDR_IN, r);
215         }
216
217         push = ndr_push_init_ctx(mem_ctx);
218         if (push == NULL) {
219                 return NT_STATUS_NO_MEMORY;
220         }
221
222         ndr_err = call->ndr_push(push, NDR_IN, r);
223         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
224                 TALLOC_FREE(push);
225                 return ndr_map_error2ntstatus(ndr_err);
226         }
227
228         blob = ndr_push_blob(push);
229         ret = prs_init_data_blob(&cli->pipes_struct->in_data.data, &blob, mem_ctx);
230         TALLOC_FREE(push);
231         if (!ret) {
232                 return NT_STATUS_NO_MEMORY;
233         }
234
235         return NT_STATUS_OK;
236 }
237
238 /****************************************************************************
239 ****************************************************************************/
240
241 static NTSTATUS internal_ndr_pull(TALLOC_CTX *mem_ctx,
242                                   struct rpc_pipe_client *cli,
243                                   const struct ndr_interface_table *table,
244                                   uint32_t opnum,
245                                   void *r)
246 {
247         const struct ndr_interface_call *call;
248         struct ndr_pull *pull;
249         enum ndr_err_code ndr_err;
250         DATA_BLOB blob;
251         bool ret;
252
253         if (!ndr_syntax_id_equal(&table->syntax_id, &cli->abstract_syntax) ||
254             (opnum >= table->num_calls)) {
255                 return NT_STATUS_INVALID_PARAMETER;
256         }
257
258         call = &table->calls[opnum];
259
260         ret = prs_data_blob(&cli->pipes_struct->out_data.rdata, &blob, mem_ctx);
261         if (!ret) {
262                 return NT_STATUS_NO_MEMORY;
263         }
264
265         pull = ndr_pull_init_blob(&blob, mem_ctx);
266         if (pull == NULL) {
267                 return NT_STATUS_NO_MEMORY;
268         }
269
270         /* have the ndr parser alloc memory for us */
271         pull->flags |= LIBNDR_FLAG_REF_ALLOC;
272         ndr_err = call->ndr_pull(pull, NDR_OUT, r);
273         TALLOC_FREE(pull);
274
275         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
276                 return ndr_map_error2ntstatus(ndr_err);
277         }
278
279         if (DEBUGLEVEL >= 10) {
280                 ndr_print_function_debug(call->ndr_print,
281                                          call->name, NDR_OUT, r);
282         }
283
284         return NT_STATUS_OK;
285 }
286
287 /****************************************************************************
288 ****************************************************************************/
289
290 static NTSTATUS rpc_pipe_internal_dispatch(struct rpc_pipe_client *cli,
291                                            TALLOC_CTX *mem_ctx,
292                                            const struct ndr_interface_table *table,
293                                            uint32_t opnum, void *r)
294 {
295         NTSTATUS status;
296         int num_cmds = rpc_srv_get_pipe_num_cmds(&table->syntax_id);
297         const struct api_struct *cmds = rpc_srv_get_pipe_cmds(&table->syntax_id);
298         int i;
299
300         if (cli->pipes_struct == NULL) {
301                 return NT_STATUS_INVALID_PARAMETER;
302         }
303
304         /* set opnum in fake rpc header */
305         cli->pipes_struct->hdr_req.opnum = opnum;
306
307         for (i = 0; i < num_cmds; i++) {
308                 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
309                         break;
310                 }
311         }
312
313         if (i == num_cmds) {
314                 return NT_STATUS_INVALID_PARAMETER;
315         }
316
317         prs_init_empty(&cli->pipes_struct->out_data.rdata, cli->pipes_struct->mem_ctx, MARSHALL);
318
319         status = internal_ndr_push(mem_ctx, cli, table, opnum, r);
320         if (!NT_STATUS_IS_OK(status)) {
321                 return status;
322         }
323
324         if (!cmds[i].fn(cli->pipes_struct)) {
325                 return NT_STATUS_UNSUCCESSFUL;
326         }
327
328         status = internal_ndr_pull(mem_ctx, cli, table, opnum, r);
329         if (!NT_STATUS_IS_OK(status)) {
330                 return status;
331         }
332
333         prs_mem_free(&cli->pipes_struct->in_data.data);
334         prs_mem_free(&cli->pipes_struct->out_data.rdata);
335
336         return NT_STATUS_OK;
337 }
338
339 /**
340  * @brief Create a new RPC client context which uses a local dispatch function.
341  *
342  * @param[in]  mem_ctx  The memory context to use.
343  *
344  * @param[in]  abstract_syntax Normally the syntax_id of the autogenerated
345  *                             ndr_table_<name>.
346  *
347  * @param[in]  dispatch The corresponding autogenerated dispatch function
348  *                      rpc_<name>_dispatch.
349  *
350  * @param[in]  serversupplied_info The server supplied authentication function.
351  *
352  * @param[out] presult  A pointer to store the connected rpc client pipe.
353  *
354  * @return              NT_STATUS_OK on success, a corresponding NT status if an
355  *                      error occured.
356  *
357  * @code
358  *   struct rpc_pipe_client *winreg_pipe;
359  *   NTSTATUS status;
360  *
361  *   status = rpc_pipe_open_internal(tmp_ctx,
362  *                                   &ndr_table_winreg.syntax_id,
363  *                                   rpc_winreg_dispatch,
364  *                                   p->server_info,
365  *                                   &winreg_pipe);
366  * @endcode
367  */
368 NTSTATUS rpc_pipe_open_internal(TALLOC_CTX *mem_ctx,
369                                 const struct ndr_syntax_id *abstract_syntax,
370                                 struct auth_serversupplied_info *serversupplied_info,
371                                 struct rpc_pipe_client **presult)
372 {
373         struct rpc_pipe_client *result;
374
375         result = TALLOC_ZERO_P(mem_ctx, struct rpc_pipe_client);
376         if (result == NULL) {
377                 return NT_STATUS_NO_MEMORY;
378         }
379
380         result->abstract_syntax = *abstract_syntax;
381         result->transfer_syntax = ndr_transfer_syntax;
382         result->dispatch = rpc_pipe_internal_dispatch;
383
384         result->pipes_struct = make_internal_rpc_pipe_p(
385                 result, abstract_syntax, "", serversupplied_info);
386         if (result->pipes_struct == NULL) {
387                 TALLOC_FREE(result);
388                 return NT_STATUS_NO_MEMORY;
389         }
390
391         result->max_xmit_frag = -1;
392         result->max_recv_frag = -1;
393
394         *presult = result;
395         return NT_STATUS_OK;
396 }