s3:rpc_client: pass object and table to rpccli_bh_create()
[mat/samba.git] / source3 / rpc_client / rpc_transport_np.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  RPC client transport over named pipes
4  *  Copyright (C) Volker Lendecke 2009
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "../lib/util/tevent_ntstatus.h"
22 #include "rpc_client/rpc_transport.h"
23 #include "libsmb/cli_np_tstream.h"
24 #include "librpc/ndr/ndr_table.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_RPC_CLI
28
29 struct rpc_transport_np_init_state {
30         struct rpc_cli_transport *transport;
31 };
32
33 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq);
34
35 struct tevent_req *rpc_transport_np_init_send(TALLOC_CTX *mem_ctx,
36                                               struct tevent_context *ev,
37                                               struct cli_state *cli,
38                                               const struct ndr_interface_table *table)
39 {
40         struct tevent_req *req;
41         struct rpc_transport_np_init_state *state;
42         const char *pipe_name;
43         struct tevent_req *subreq;
44
45         req = tevent_req_create(mem_ctx, &state,
46                                 struct rpc_transport_np_init_state);
47         if (req == NULL) {
48                 return NULL;
49         }
50
51         pipe_name = dcerpc_default_transport_endpoint(mem_ctx, NCACN_NP, table);
52         if (tevent_req_nomem(pipe_name, req)) {
53                 return tevent_req_post(req, ev);
54         }
55
56         while (pipe_name[0] == '\\') {
57                 pipe_name++;
58         }
59
60         subreq = tstream_cli_np_open_send(state, ev, cli, pipe_name);
61         if (tevent_req_nomem(subreq, req)) {
62                 return tevent_req_post(req, ev);
63         }
64         tevent_req_set_callback(subreq, rpc_transport_np_init_pipe_open, req);
65
66         return req;
67 }
68
69 static void rpc_transport_np_init_pipe_open(struct tevent_req *subreq)
70 {
71         struct tevent_req *req = tevent_req_callback_data(
72                 subreq, struct tevent_req);
73         struct rpc_transport_np_init_state *state = tevent_req_data(
74                 req, struct rpc_transport_np_init_state);
75         NTSTATUS status;
76         struct tstream_context *stream;
77
78         status = tstream_cli_np_open_recv(subreq, state, &stream);
79         TALLOC_FREE(subreq);
80         if (!NT_STATUS_IS_OK(status)) {
81                 tevent_req_nterror(req, status);
82                 return;
83         }
84
85         status = rpc_transport_tstream_init(state,
86                                             &stream,
87                                             &state->transport);
88         if (!NT_STATUS_IS_OK(status)) {
89                 tevent_req_nterror(req, status);
90                 return;
91         }
92
93         tevent_req_done(req);
94 }
95
96 NTSTATUS rpc_transport_np_init_recv(struct tevent_req *req,
97                                     TALLOC_CTX *mem_ctx,
98                                     struct rpc_cli_transport **presult)
99 {
100         struct rpc_transport_np_init_state *state = tevent_req_data(
101                 req, struct rpc_transport_np_init_state);
102         NTSTATUS status;
103
104         if (tevent_req_is_nterror(req, &status)) {
105                 return status;
106         }
107
108         *presult = talloc_move(mem_ctx, &state->transport);
109         return NT_STATUS_OK;
110 }
111
112 NTSTATUS rpc_transport_np_init(TALLOC_CTX *mem_ctx, struct cli_state *cli,
113                                const struct ndr_interface_table *table,
114                                struct rpc_cli_transport **presult)
115 {
116         TALLOC_CTX *frame = talloc_stackframe();
117         struct tevent_context *ev;
118         struct tevent_req *req;
119         NTSTATUS status = NT_STATUS_OK;
120
121         ev = samba_tevent_context_init(frame);
122         if (ev == NULL) {
123                 status = NT_STATUS_NO_MEMORY;
124                 goto fail;
125         }
126
127         req = rpc_transport_np_init_send(frame, ev, cli, table);
128         if (req == NULL) {
129                 status = NT_STATUS_NO_MEMORY;
130                 goto fail;
131         }
132
133         if (!tevent_req_poll(req, ev)) {
134                 status = map_nt_error_from_unix(errno);
135                 goto fail;
136         }
137
138         status = rpc_transport_np_init_recv(req, mem_ctx, presult);
139  fail:
140         TALLOC_FREE(frame);
141         return status;
142 }