s3-rpc_server: Convert rpc_connect_spoolss_pipe into a generic interface.
[abartlet/samba.git/.git] / source3 / rpc_server / rpc_ncacn_np.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 #include "rpc_dce.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_RPC_SRV
28
29 static int pipes_open;
30
31 static struct pipes_struct *InternalPipes;
32
33 /* TODO
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.
37  *
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...
41  */
42
43 /****************************************************************************
44  Internal Pipe iterator functions.
45 ****************************************************************************/
46
47 struct pipes_struct *get_first_internal_pipe(void)
48 {
49         return InternalPipes;
50 }
51
52 struct pipes_struct *get_next_internal_pipe(struct pipes_struct *p)
53 {
54         return p->next;
55 }
56
57 static void free_pipe_rpc_context_internal( PIPE_RPC_FNS *list )
58 {
59         PIPE_RPC_FNS *tmp = list;
60         PIPE_RPC_FNS *tmp2;
61
62         while (tmp) {
63                 tmp2 = tmp->next;
64                 SAFE_FREE(tmp);
65                 tmp = tmp2;
66         }
67
68         return;
69 }
70
71 bool check_open_pipes(void)
72 {
73         struct pipes_struct *p;
74
75         for (p = InternalPipes; p != NULL; p = p->next) {
76                 if (num_pipe_handles(p) != 0) {
77                         return true;
78                 }
79         }
80         return false;
81 }
82
83 /****************************************************************************
84  Close an rpc pipe.
85 ****************************************************************************/
86
87 int close_internal_rpc_pipe_hnd(struct pipes_struct *p)
88 {
89         if (!p) {
90                 DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
91                 return False;
92         }
93
94         if (p->auth.auth_data_free_func) {
95                 (*p->auth.auth_data_free_func)(&p->auth);
96         }
97
98         free_pipe_rpc_context_internal( p->contexts );
99
100         /* Free the handles database. */
101         close_policy_by_pipe(p);
102
103         DLIST_REMOVE(InternalPipes, p);
104
105         ZERO_STRUCTP(p);
106
107         return 0;
108 }
109
110 /****************************************************************************
111  Make an internal namedpipes structure
112 ****************************************************************************/
113
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)
119 {
120         struct 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         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);
151                 TALLOC_FREE(p);
152                 return NULL;
153         }
154
155         p->msg_ctx = msg_ctx;
156
157         DLIST_ADD(InternalPipes, p);
158
159         p->client_id = client_id;
160
161         p->endian = RPC_LITTLE_ENDIAN;
162
163         p->syntax = *syntax;
164
165         DEBUG(4,("Created internal pipe %s (pipes_open=%d)\n",
166                  get_pipe_name_from_syntax(talloc_tos(), syntax), pipes_open));
167
168         talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
169
170         return p;
171 }
172
173 static NTSTATUS rpcint_dispatch(struct pipes_struct *p,
174                                 TALLOC_CTX *mem_ctx,
175                                 uint32_t opnum,
176                                 const DATA_BLOB *in_data,
177                                 DATA_BLOB *out_data)
178 {
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);
181         uint32_t i;
182         bool ok;
183
184         /* set opnum */
185         p->opnum = opnum;
186
187         for (i = 0; i < num_cmds; i++) {
188                 if (cmds[i].opnum == opnum && cmds[i].fn != NULL) {
189                         break;
190                 }
191         }
192
193         if (i == num_cmds) {
194                 return NT_STATUS_RPC_PROCNUM_OUT_OF_RANGE;
195         }
196
197         p->in_data.data = *in_data;
198         p->out_data.rdata = data_blob_null;
199
200         ok = cmds[i].fn(p);
201         p->in_data.data = data_blob_null;
202         if (!ok) {
203                 data_blob_free(&p->out_data.rdata);
204                 talloc_free_children(p->mem_ctx);
205                 return NT_STATUS_RPC_CALL_FAILED;
206         }
207
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;
213         }
214
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;
220         }
221
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;
227         }
228
229         *out_data = p->out_data.rdata;
230         talloc_steal(mem_ctx, out_data->data);
231         p->out_data.rdata = data_blob_null;
232
233         talloc_free_children(p->mem_ctx);
234         return NT_STATUS_OK;
235 }
236
237 struct rpcint_bh_state {
238         struct pipes_struct *p;
239 };
240
241 static bool rpcint_bh_is_connected(struct dcerpc_binding_handle *h)
242 {
243         struct rpcint_bh_state *hs = dcerpc_binding_handle_data(h,
244                                      struct rpcint_bh_state);
245
246         if (!hs->p) {
247                 return false;
248         }
249
250         return true;
251 }
252
253 static uint32_t rpcint_bh_set_timeout(struct dcerpc_binding_handle *h,
254                                       uint32_t timeout)
255 {
256         /* TODO: implement timeouts */
257         return UINT32_MAX;
258 }
259
260 struct rpcint_bh_raw_call_state {
261         DATA_BLOB in_data;
262         DATA_BLOB out_data;
263         uint32_t out_flags;
264 };
265
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,
270                                                   uint32_t opnum,
271                                                   uint32_t in_flags,
272                                                   const uint8_t *in_data,
273                                                   size_t in_length)
274 {
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;
280         bool ok;
281         NTSTATUS status;
282
283         req = tevent_req_create(mem_ctx, &state,
284                                 struct rpcint_bh_raw_call_state);
285         if (req == NULL) {
286                 return NULL;
287         }
288         state->in_data.data = discard_const_p(uint8_t, in_data);
289         state->in_data.length = in_length;
290
291         ok = rpcint_bh_is_connected(h);
292         if (!ok) {
293                 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
294                 return tevent_req_post(req, ev);
295         }
296
297         /* TODO: allow async */
298         status = rpcint_dispatch(hs->p, state, opnum,
299                                  &state->in_data,
300                                  &state->out_data);
301         if (!NT_STATUS_IS_OK(status)) {
302                 tevent_req_nterror(req, status);
303                 return tevent_req_post(req, ev);
304         }
305
306         tevent_req_done(req);
307         return tevent_req_post(req, ev);
308 }
309
310 static NTSTATUS rpcint_bh_raw_call_recv(struct tevent_req *req,
311                                         TALLOC_CTX *mem_ctx,
312                                         uint8_t **out_data,
313                                         size_t *out_length,
314                                         uint32_t *out_flags)
315 {
316         struct rpcint_bh_raw_call_state *state =
317                 tevent_req_data(req,
318                 struct rpcint_bh_raw_call_state);
319         NTSTATUS status;
320
321         if (tevent_req_is_nterror(req, &status)) {
322                 tevent_req_received(req);
323                 return status;
324         }
325
326         *out_data = talloc_move(mem_ctx, &state->out_data.data);
327         *out_length = state->out_data.length;
328         *out_flags = 0;
329         tevent_req_received(req);
330         return NT_STATUS_OK;
331 }
332
333 struct rpcint_bh_disconnect_state {
334         uint8_t _dummy;
335 };
336
337 static struct tevent_req *rpcint_bh_disconnect_send(TALLOC_CTX *mem_ctx,
338                                                 struct tevent_context *ev,
339                                                 struct dcerpc_binding_handle *h)
340 {
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;
345         bool ok;
346
347         req = tevent_req_create(mem_ctx, &state,
348                                 struct rpcint_bh_disconnect_state);
349         if (req == NULL) {
350                 return NULL;
351         }
352
353         ok = rpcint_bh_is_connected(h);
354         if (!ok) {
355                 tevent_req_nterror(req, NT_STATUS_INVALID_CONNECTION);
356                 return tevent_req_post(req, ev);
357         }
358
359         /*
360          * TODO: do a real async disconnect ...
361          *
362          * For now the caller needs to free pipes_struct
363          */
364         hs->p = NULL;
365
366         tevent_req_done(req);
367         return tevent_req_post(req, ev);
368 }
369
370 static NTSTATUS rpcint_bh_disconnect_recv(struct tevent_req *req)
371 {
372         NTSTATUS status;
373
374         if (tevent_req_is_nterror(req, &status)) {
375                 tevent_req_received(req);
376                 return status;
377         }
378
379         tevent_req_received(req);
380         return NT_STATUS_OK;
381 }
382
383 static bool rpcint_bh_ref_alloc(struct dcerpc_binding_handle *h)
384 {
385         return true;
386 }
387
388 static void rpcint_bh_do_ndr_print(struct dcerpc_binding_handle *h,
389                                    int ndr_flags,
390                                    const void *_struct_ptr,
391                                    const struct ndr_interface_call *call)
392 {
393         void *struct_ptr = discard_const(_struct_ptr);
394
395         if (DEBUGLEVEL < 10) {
396                 return;
397         }
398
399         if (ndr_flags & NDR_IN) {
400                 ndr_print_function_debug(call->ndr_print,
401                                          call->name,
402                                          ndr_flags,
403                                          struct_ptr);
404         }
405         if (ndr_flags & NDR_OUT) {
406                 ndr_print_function_debug(call->ndr_print,
407                                          call->name,
408                                          ndr_flags,
409                                          struct_ptr);
410         }
411 }
412
413 static const struct dcerpc_binding_handle_ops rpcint_bh_ops = {
414         .name                   = "rpcint",
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,
421
422         .ref_alloc              = rpcint_bh_ref_alloc,
423         .do_ndr_print           = rpcint_bh_do_ndr_print,
424 };
425
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)
433 {
434         struct dcerpc_binding_handle *h;
435         struct rpcint_bh_state *hs;
436
437         if (ndr_table) {
438                 abstract_syntax = &ndr_table->syntax_id;
439         }
440
441         h = dcerpc_binding_handle_create(mem_ctx,
442                                          &rpcint_bh_ops,
443                                          NULL,
444                                          ndr_table,
445                                          &hs,
446                                          struct rpcint_bh_state,
447                                          __location__);
448         if (h == NULL) {
449                 return NT_STATUS_NO_MEMORY;
450         }
451         hs->p = make_internal_rpc_pipe_p(hs,
452                                          abstract_syntax,
453                                          client_id,
454                                          server_info,
455                                          msg_ctx);
456         if (hs->p == NULL) {
457                 TALLOC_FREE(h);
458                 return NT_STATUS_NO_MEMORY;
459         }
460
461         *binding_handle = h;
462         return NT_STATUS_OK;
463 }
464 /**
465  * @brief Create a new DCERPC Binding Handle which uses a local dispatch function.
466  *
467  * @param[in]  mem_ctx  The memory context to use.
468  *
469  * @param[in]  ndr_table Normally the ndr_table_<name>.
470  *
471  * @param[in]  client_id The info about the connected client.
472  *
473  * @param[in]  serversupplied_info The server supplied authentication function.
474  *
475  * @param[in]  msg_ctx   The messaging context that can be used by the server
476  *
477  * @param[out] binding_handle  A pointer to store the connected
478  *                             dcerpc_binding_handle
479  *
480  * @return              NT_STATUS_OK on success, a corresponding NT status if an
481  *                      error occured.
482  *
483  * @code
484  *   struct dcerpc_binding_handle *winreg_binding;
485  *   NTSTATUS status;
486  *
487  *   status = rpcint_binding_handle(tmp_ctx,
488  *                                  &ndr_table_winreg,
489  *                                  p->client_id,
490  *                                  p->server_info,
491  *                                  p->msg_ctx
492  *                                  &winreg_binding);
493  * @endcode
494  */
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)
501 {
502         return rpcint_binding_handle_ex(mem_ctx, NULL, ndr_table, client_id,
503                                         server_info, msg_ctx, binding_handle);
504 }
505
506 /**
507  * @brief Create a new RPC client context which uses a local dispatch function.
508  *
509  * @param[in]  mem_ctx  The memory context to use.
510  *
511  * @param[in]  abstract_syntax Normally the syntax_id of the autogenerated
512  *                             ndr_table_<name>.
513  *
514  * @param[in]  dispatch The corresponding autogenerated dispatch function
515  *                      rpc_<name>_dispatch.
516  *
517  * @param[in]  serversupplied_info The server supplied authentication function.
518  *
519  * @param[out] presult  A pointer to store the connected rpc client pipe.
520  *
521  * @return              NT_STATUS_OK on success, a corresponding NT status if an
522  *                      error occured.
523  *
524  * @code
525  *   struct rpc_pipe_client *winreg_pipe;
526  *   NTSTATUS status;
527  *
528  *   status = rpc_pipe_open_internal(tmp_ctx,
529  *                                   &ndr_table_winreg.syntax_id,
530  *                                   rpc_winreg_dispatch,
531  *                                   p->server_info,
532  *                                   &winreg_pipe);
533  * @endcode
534  */
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)
541 {
542         struct rpc_pipe_client *result;
543         NTSTATUS status;
544
545         result = TALLOC_ZERO_P(mem_ctx, struct rpc_pipe_client);
546         if (result == NULL) {
547                 return NT_STATUS_NO_MEMORY;
548         }
549
550         result->abstract_syntax = *abstract_syntax;
551         result->transfer_syntax = ndr_transfer_syntax;
552
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;
558         }
559
560         result->max_xmit_frag = -1;
561         result->max_recv_frag = -1;
562
563         status = rpcint_binding_handle_ex(result,
564                                           abstract_syntax,
565                                           NULL,
566                                           client_id,
567                                           serversupplied_info,
568                                           msg_ctx,
569                                           &result->binding_handle);
570         if (!NT_STATUS_IS_OK(status)) {
571                 TALLOC_FREE(result);
572                 return status;
573         }
574
575         *presult = result;
576         return NT_STATUS_OK;
577 }