Revert "s3: Do not reference the ndr_tables in the server calls directly"
[ira/wip.git] / source3 / winbindd / winbindd_dual_ndr.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Provide parent->child communication based on NDR marshalling
5
6    Copyright (C) Volker Lendecke 2009
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 /*
23  * This file implements an RPC between winbind parent and child processes,
24  * leveraging the autogenerated marshalling routines for MSRPC. This is not
25  * MSRPC, as it does not go through the whole DCERPC fragmentation, we just
26  * leverage much the same infrastructure we already have for it.
27  */
28
29 #include "includes.h"
30 #include "winbindd/winbindd.h"
31 #include "winbindd/winbindd_proto.h"
32 #include "librpc/gen_ndr/srv_wbint.h"
33
34 struct wb_ndr_transport_priv {
35         struct winbindd_domain *domain;
36         struct winbindd_child *child;
37 };
38
39 struct wb_ndr_dispatch_state {
40         struct wb_ndr_transport_priv *transport;
41         uint32_t opnum;
42         const struct ndr_interface_call *call;
43         void *r;
44         DATA_BLOB req_blob, resp_blob;
45         struct winbindd_request request;
46         struct winbindd_response *response;
47 };
48
49 static void wb_ndr_dispatch_done(struct tevent_req *subreq);
50
51 static struct tevent_req *wb_ndr_dispatch_send(TALLOC_CTX *mem_ctx,
52                                                struct tevent_context *ev,
53                                                struct rpc_pipe_client *cli,
54                                                const char *interface,
55                                                uint32_t interface_version,
56                                                uint32_t opnum,
57                                                void *r)
58 {
59         struct tevent_req *req, *subreq;
60         struct wb_ndr_dispatch_state *state;
61         struct wb_ndr_transport_priv *transport = talloc_get_type_abort(
62                 cli->transport->priv, struct wb_ndr_transport_priv);
63         struct ndr_push *push;
64         enum ndr_err_code ndr_err;
65
66         req = tevent_req_create(mem_ctx, &state,
67                                 struct wb_ndr_dispatch_state);
68         if (req == NULL) {
69                 return NULL;
70         }
71
72         state->r = r;
73         state->call = &ndr_table_wbint.calls[opnum];
74         state->transport = transport;
75         state->opnum = opnum;
76
77         push = ndr_push_init_ctx(state, NULL);
78         if (tevent_req_nomem(push, req)) {
79                 return tevent_req_post(req, ev);
80         }
81
82         ndr_err = state->call->ndr_push(push, NDR_IN, r);
83         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
84                 tevent_req_nterror(req, ndr_map_error2ntstatus(ndr_err));
85                 TALLOC_FREE(push);
86                 return tevent_req_post(req, ev);
87         }
88
89         state->req_blob = ndr_push_blob(push);
90
91         if ((transport->domain != NULL)
92             && wcache_fetch_ndr(state, transport->domain, opnum,
93                                 &state->req_blob, &state->resp_blob)) {
94                 tevent_req_done(req);
95                 return tevent_req_post(req, ev);
96         }
97
98         state->request.cmd = WINBINDD_DUAL_NDRCMD;
99         state->request.data.ndrcmd = opnum;
100         state->request.extra_data.data = (char *)state->req_blob.data;
101         state->request.extra_len = state->req_blob.length;
102
103         subreq = wb_child_request_send(state, ev, transport->child,
104                                        &state->request);
105         if (tevent_req_nomem(subreq, req)) {
106                 return tevent_req_post(req, ev);
107         }
108         tevent_req_set_callback(subreq, wb_ndr_dispatch_done, req);
109         return req;
110 }
111
112 static void wb_ndr_dispatch_done(struct tevent_req *subreq)
113 {
114         struct tevent_req *req = tevent_req_callback_data(
115                 subreq, struct tevent_req);
116         struct wb_ndr_dispatch_state *state = tevent_req_data(
117                 req, struct wb_ndr_dispatch_state);
118         int ret, err;
119
120         ret = wb_child_request_recv(subreq, state, &state->response, &err);
121         TALLOC_FREE(subreq);
122         if (ret == -1) {
123                 tevent_req_nterror(req, map_nt_error_from_unix(err));
124                 return;
125         }
126
127         state->resp_blob = data_blob_const(
128                 state->response->extra_data.data,
129                 state->response->length - sizeof(struct winbindd_response));
130
131         if (state->transport->domain != NULL) {
132                 wcache_store_ndr(state->transport->domain, state->opnum,
133                                  &state->req_blob, &state->resp_blob);
134         }
135
136         tevent_req_done(req);
137 }
138
139 static NTSTATUS wb_ndr_dispatch_recv(struct tevent_req *req,
140                                      TALLOC_CTX *mem_ctx)
141 {
142         struct wb_ndr_dispatch_state *state = tevent_req_data(
143                 req, struct wb_ndr_dispatch_state);
144         NTSTATUS status;
145         struct ndr_pull *pull;
146         enum ndr_err_code ndr_err;
147
148         if (tevent_req_is_nterror(req, &status)) {
149                 return status;
150         }
151
152         pull = ndr_pull_init_blob(&state->resp_blob, mem_ctx, NULL);
153         if (pull == NULL) {
154                 return NT_STATUS_NO_MEMORY;
155         }
156
157         /* have the ndr parser alloc memory for us */
158         pull->flags |= LIBNDR_FLAG_REF_ALLOC;
159         ndr_err = state->call->ndr_pull(pull, NDR_OUT, state->r);
160         TALLOC_FREE(pull);
161
162         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
163                 return ndr_map_error2ntstatus(ndr_err);
164         }
165
166         return NT_STATUS_OK;
167 }
168
169 static NTSTATUS wb_ndr_dispatch(struct rpc_pipe_client *cli,
170                                 TALLOC_CTX *mem_ctx,
171                                 const char *interface,
172                                 uint32_t interface_version,
173                                 uint32_t opnum, void *r)
174 {
175         TALLOC_CTX *frame = talloc_stackframe();
176         struct event_context *ev;
177         struct tevent_req *req;
178         NTSTATUS status = NT_STATUS_OK;
179
180         ev = event_context_init(frame);
181         if (ev == NULL) {
182                 status = NT_STATUS_NO_MEMORY;
183                 goto fail;
184         }
185
186         req = wb_ndr_dispatch_send(frame, ev, cli, interface,
187                                    interface_version, opnum, r);
188         if (req == NULL) {
189                 status = NT_STATUS_NO_MEMORY;
190                 goto fail;
191         }
192
193         if (!tevent_req_poll(req, ev)) {
194                 status = map_nt_error_from_unix(errno);
195                 goto fail;
196         }
197
198         status = wb_ndr_dispatch_recv(req, mem_ctx);
199  fail:
200         TALLOC_FREE(frame);
201         return status;
202 }
203
204 struct rpc_pipe_client *wbint_rpccli_create(TALLOC_CTX *mem_ctx,
205                                             struct winbindd_domain *domain,
206                                             struct winbindd_child *child)
207 {
208         struct rpc_pipe_client *result;
209         struct wb_ndr_transport_priv *transp;
210
211         result = talloc(mem_ctx, struct rpc_pipe_client);
212         if (result == NULL) {
213                 return NULL;
214         }
215         result->abstract_syntax = ndr_table_wbint.syntax_id;
216         result->transfer_syntax = ndr_transfer_syntax;
217         result->dispatch = wb_ndr_dispatch;
218         result->dispatch_send = wb_ndr_dispatch_send;
219         result->dispatch_recv = wb_ndr_dispatch_recv;
220         result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
221         result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
222         result->desthost = NULL;
223         result->srv_name_slash = NULL;
224
225         /*
226          * Initialize a fake transport. Due to our own wb_ndr_dispatch
227          * function we don't use all the fragmentation engine in
228          * cli_pipe, which would use all the _read and _write
229          * functions in rpc_cli_transport. But we need a place to
230          * store the child struct in, and we're re-using
231          * result->transport->priv for that.
232          */
233
234         result->transport = talloc_zero(result, struct rpc_cli_transport);
235         if (result->transport == NULL) {
236                 TALLOC_FREE(result);
237                 return NULL;
238         }
239         transp = talloc(result->transport, struct wb_ndr_transport_priv);
240         if (transp == NULL) {
241                 TALLOC_FREE(result);
242                 return NULL;
243         }
244         transp->domain = domain;
245         transp->child = child;
246         result->transport->priv = transp;
247         return result;
248 }
249
250 enum winbindd_result winbindd_dual_ndrcmd(struct winbindd_domain *domain,
251                                           struct winbindd_cli_state *state)
252 {
253         pipes_struct p;
254         struct api_struct *fns;
255         int num_fns;
256         bool ret;
257
258         wbint_get_pipe_fns(&fns, &num_fns);
259
260         if (state->request->data.ndrcmd >= num_fns) {
261                 return WINBINDD_ERROR;
262         }
263
264         DEBUG(10, ("winbindd_dual_ndrcmd: Running command %s (%s)\n",
265                    fns[state->request->data.ndrcmd].name,
266                    domain ? domain->name : "no domain"));
267
268         ZERO_STRUCT(p);
269         p.mem_ctx = talloc_stackframe();
270         p.in_data.data.buffer_size = state->request->extra_len;
271         p.in_data.data.data_p = state->request->extra_data.data;
272         prs_init(&p.out_data.rdata, 0, state->mem_ctx, false);
273
274         ret = fns[state->request->data.ndrcmd].fn(&p);
275         TALLOC_FREE(p.mem_ctx);
276         if (!ret) {
277                 return WINBINDD_ERROR;
278         }
279
280         state->response->extra_data.data =
281                 talloc_memdup(state->mem_ctx, p.out_data.rdata.data_p,
282                               p.out_data.rdata.data_offset);
283         state->response->length += p.out_data.rdata.data_offset;
284         prs_mem_free(&p.out_data.rdata);
285         if (state->response->extra_data.data == NULL) {
286                 return WINBINDD_ERROR;
287         }
288         return WINBINDD_OK;
289 }
290
291 /*
292  * Just a dummy to make srv_wbint.c happy
293  */
294 NTSTATUS rpc_srv_register(int version, const char *clnt, const char *srv,
295                           const struct ndr_interface_table *iface,
296                           const struct api_struct *cmds, int size)
297 {
298         return NT_STATUS_OK;
299 }