idl: rebuild drsuapi.idl
[Samba/aatanasov.git] / source3 / winbindd / winbindd_dual_ndr.c
blob8a23ce48e041f01f259c4cb954f043d0ec7fc02b
1 /*
2 Unix SMB/CIFS implementation.
4 Provide parent->child communication based on NDR marshalling
6 Copyright (C) Volker Lendecke 2009
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 * 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.
29 #include "includes.h"
30 #include "winbindd/winbindd.h"
31 #include "winbindd/winbindd_proto.h"
32 #include "librpc/gen_ndr/srv_wbint.h"
34 struct wb_ndr_transport_priv {
35 struct winbindd_domain *domain;
36 struct winbindd_child *child;
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;
49 static void wb_ndr_dispatch_done(struct tevent_req *subreq);
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 struct ndr_interface_table *table,
55 uint32_t opnum,
56 void *r)
58 struct tevent_req *req, *subreq;
59 struct wb_ndr_dispatch_state *state;
60 struct wb_ndr_transport_priv *transport = talloc_get_type_abort(
61 cli->transport->priv, struct wb_ndr_transport_priv);
62 struct ndr_push *push;
63 enum ndr_err_code ndr_err;
65 req = tevent_req_create(mem_ctx, &state,
66 struct wb_ndr_dispatch_state);
67 if (req == NULL) {
68 return NULL;
71 state->r = r;
72 state->call = &table->calls[opnum];
73 state->transport = transport;
74 state->opnum = opnum;
76 push = ndr_push_init_ctx(state, NULL);
77 if (tevent_req_nomem(push, req)) {
78 return tevent_req_post(req, ev);
81 ndr_err = state->call->ndr_push(push, NDR_IN, r);
82 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
83 tevent_req_nterror(req, ndr_map_error2ntstatus(ndr_err));
84 TALLOC_FREE(push);
85 return tevent_req_post(req, ev);
88 state->req_blob = ndr_push_blob(push);
90 if ((transport->domain != NULL)
91 && wcache_fetch_ndr(state, transport->domain, opnum,
92 &state->req_blob, &state->resp_blob)) {
93 tevent_req_done(req);
94 return tevent_req_post(req, ev);
97 state->request.cmd = WINBINDD_DUAL_NDRCMD;
98 state->request.data.ndrcmd = opnum;
99 state->request.extra_data.data = (char *)state->req_blob.data;
100 state->request.extra_len = state->req_blob.length;
102 subreq = wb_child_request_send(state, ev, transport->child,
103 &state->request);
104 if (tevent_req_nomem(subreq, req)) {
105 return tevent_req_post(req, ev);
107 tevent_req_set_callback(subreq, wb_ndr_dispatch_done, req);
108 return req;
111 static void wb_ndr_dispatch_done(struct tevent_req *subreq)
113 struct tevent_req *req = tevent_req_callback_data(
114 subreq, struct tevent_req);
115 struct wb_ndr_dispatch_state *state = tevent_req_data(
116 req, struct wb_ndr_dispatch_state);
117 int ret, err;
119 ret = wb_child_request_recv(subreq, state, &state->response, &err);
120 TALLOC_FREE(subreq);
121 if (ret == -1) {
122 tevent_req_nterror(req, map_nt_error_from_unix(err));
123 return;
126 state->resp_blob = data_blob_const(
127 state->response->extra_data.data,
128 state->response->length - sizeof(struct winbindd_response));
130 if (state->transport->domain != NULL) {
131 wcache_store_ndr(state->transport->domain, state->opnum,
132 &state->req_blob, &state->resp_blob);
135 tevent_req_done(req);
138 static NTSTATUS wb_ndr_dispatch_recv(struct tevent_req *req,
139 TALLOC_CTX *mem_ctx)
141 struct wb_ndr_dispatch_state *state = tevent_req_data(
142 req, struct wb_ndr_dispatch_state);
143 NTSTATUS status;
144 struct ndr_pull *pull;
145 enum ndr_err_code ndr_err;
147 if (tevent_req_is_nterror(req, &status)) {
148 return status;
151 pull = ndr_pull_init_blob(&state->resp_blob, mem_ctx, NULL);
152 if (pull == NULL) {
153 return NT_STATUS_NO_MEMORY;
156 /* have the ndr parser alloc memory for us */
157 pull->flags |= LIBNDR_FLAG_REF_ALLOC;
158 ndr_err = state->call->ndr_pull(pull, NDR_OUT, state->r);
159 TALLOC_FREE(pull);
161 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
162 return ndr_map_error2ntstatus(ndr_err);
165 return NT_STATUS_OK;
168 static NTSTATUS wb_ndr_dispatch(struct rpc_pipe_client *cli,
169 TALLOC_CTX *mem_ctx,
170 const struct ndr_interface_table *table,
171 uint32_t opnum, void *r)
173 TALLOC_CTX *frame = talloc_stackframe();
174 struct event_context *ev;
175 struct tevent_req *req;
176 NTSTATUS status = NT_STATUS_OK;
178 ev = event_context_init(frame);
179 if (ev == NULL) {
180 status = NT_STATUS_NO_MEMORY;
181 goto fail;
184 req = wb_ndr_dispatch_send(frame, ev, cli, table, opnum, r);
185 if (req == NULL) {
186 status = NT_STATUS_NO_MEMORY;
187 goto fail;
190 if (!tevent_req_poll(req, ev)) {
191 status = map_nt_error_from_unix(errno);
192 goto fail;
195 status = wb_ndr_dispatch_recv(req, mem_ctx);
196 fail:
197 TALLOC_FREE(frame);
198 return status;
201 struct rpc_pipe_client *wbint_rpccli_create(TALLOC_CTX *mem_ctx,
202 struct winbindd_domain *domain,
203 struct winbindd_child *child)
205 struct rpc_pipe_client *result;
206 struct wb_ndr_transport_priv *transp;
208 result = talloc(mem_ctx, struct rpc_pipe_client);
209 if (result == NULL) {
210 return NULL;
212 result->abstract_syntax = ndr_table_wbint.syntax_id;
213 result->transfer_syntax = ndr_transfer_syntax;
214 result->dispatch = wb_ndr_dispatch;
215 result->dispatch_send = wb_ndr_dispatch_send;
216 result->dispatch_recv = wb_ndr_dispatch_recv;
217 result->max_xmit_frag = RPC_MAX_PDU_FRAG_LEN;
218 result->max_recv_frag = RPC_MAX_PDU_FRAG_LEN;
219 result->desthost = NULL;
220 result->srv_name_slash = NULL;
223 * Initialize a fake transport. Due to our own wb_ndr_dispatch
224 * function we don't use all the fragmentation engine in
225 * cli_pipe, which would use all the _read and _write
226 * functions in rpc_cli_transport. But we need a place to
227 * store the child struct in, and we're re-using
228 * result->transport->priv for that.
231 result->transport = talloc_zero(result, struct rpc_cli_transport);
232 if (result->transport == NULL) {
233 TALLOC_FREE(result);
234 return NULL;
236 transp = talloc(result->transport, struct wb_ndr_transport_priv);
237 if (transp == NULL) {
238 TALLOC_FREE(result);
239 return NULL;
241 transp->domain = domain;
242 transp->child = child;
243 result->transport->priv = transp;
244 return result;
247 enum winbindd_result winbindd_dual_ndrcmd(struct winbindd_domain *domain,
248 struct winbindd_cli_state *state)
250 pipes_struct p;
251 struct api_struct *fns;
252 int num_fns;
253 bool ret;
255 wbint_get_pipe_fns(&fns, &num_fns);
257 if (state->request->data.ndrcmd >= num_fns) {
258 return WINBINDD_ERROR;
261 DEBUG(10, ("winbindd_dual_ndrcmd: Running command %s (%s)\n",
262 fns[state->request->data.ndrcmd].name,
263 domain ? domain->name : "no domain"));
265 ZERO_STRUCT(p);
266 p.mem_ctx = talloc_stackframe();
267 p.in_data.data.buffer_size = state->request->extra_len;
268 p.in_data.data.data_p = state->request->extra_data.data;
269 prs_init(&p.out_data.rdata, 0, state->mem_ctx, false);
271 ret = fns[state->request->data.ndrcmd].fn(&p);
272 TALLOC_FREE(p.mem_ctx);
273 if (!ret) {
274 return WINBINDD_ERROR;
277 state->response->extra_data.data =
278 talloc_memdup(state->mem_ctx, p.out_data.rdata.data_p,
279 p.out_data.rdata.data_offset);
280 state->response->length += p.out_data.rdata.data_offset;
281 prs_mem_free(&p.out_data.rdata);
282 if (state->response->extra_data.data == NULL) {
283 return WINBINDD_ERROR;
285 return WINBINDD_OK;
289 * Just a dummy to make srv_wbint.c happy
291 NTSTATUS rpc_srv_register(int version, const char *clnt, const char *srv,
292 const struct ndr_interface_table *iface,
293 const struct api_struct *cmds, int size)
295 return NT_STATUS_OK;