pthreadpool: Add test for fork crash
[Samba.git] / librpc / rpc / binding_handle.c
blobef2b7bd4b444b4d4182722e8b6142abdb852f182
1 /*
2 Unix SMB/CIFS implementation.
4 dcerpc binding handle functions
6 Copyright (C) Stefan Metzmacher 2010
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/>.
22 #include "includes.h"
23 #include <tevent.h>
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "librpc/rpc/dcerpc.h"
26 #include "rpc_common.h"
28 struct dcerpc_binding_handle {
29 void *private_data;
30 const struct dcerpc_binding_handle_ops *ops;
31 const char *location;
32 const struct GUID *object;
33 const struct ndr_interface_table *table;
34 struct tevent_context *sync_ev;
37 static int dcerpc_binding_handle_destructor(struct dcerpc_binding_handle *b)
39 return 0;
42 struct dcerpc_binding_handle *_dcerpc_binding_handle_create(TALLOC_CTX *mem_ctx,
43 const struct dcerpc_binding_handle_ops *ops,
44 const struct GUID *object,
45 const struct ndr_interface_table *table,
46 void *pstate,
47 size_t psize,
48 const char *type,
49 const char *location)
51 struct dcerpc_binding_handle *h;
52 void **ppstate = (void **)pstate;
53 void *state;
55 h = talloc_zero(mem_ctx, struct dcerpc_binding_handle);
56 if (h == NULL) {
57 return NULL;
59 h->ops = ops;
60 h->location = location;
61 h->object = object;
62 h->table = table;
64 state = talloc_zero_size(h, psize);
65 if (state == NULL) {
66 talloc_free(h);
67 return NULL;
69 talloc_set_name_const(state, type);
71 h->private_data = state;
73 talloc_set_destructor(h, dcerpc_binding_handle_destructor);
75 *ppstate = state;
76 return h;
79 void *_dcerpc_binding_handle_data(struct dcerpc_binding_handle *h)
81 return h->private_data;
84 void dcerpc_binding_handle_set_sync_ev(struct dcerpc_binding_handle *h,
85 struct tevent_context *ev)
87 h->sync_ev = ev;
90 bool dcerpc_binding_handle_is_connected(struct dcerpc_binding_handle *h)
92 return h->ops->is_connected(h);
95 uint32_t dcerpc_binding_handle_set_timeout(struct dcerpc_binding_handle *h,
96 uint32_t timeout)
98 return h->ops->set_timeout(h, timeout);
101 void dcerpc_binding_handle_auth_info(struct dcerpc_binding_handle *h,
102 enum dcerpc_AuthType *auth_type,
103 enum dcerpc_AuthLevel *auth_level)
105 enum dcerpc_AuthType _auth_type;
106 enum dcerpc_AuthLevel _auth_level;
108 if (auth_type == NULL) {
109 auth_type = &_auth_type;
112 if (auth_level == NULL) {
113 auth_level = &_auth_level;
116 *auth_type = DCERPC_AUTH_TYPE_NONE;
117 *auth_level = DCERPC_AUTH_LEVEL_NONE;
119 if (h->ops->auth_info == NULL) {
120 return;
123 h->ops->auth_info(h, auth_type, auth_level);
126 struct dcerpc_binding_handle_raw_call_state {
127 const struct dcerpc_binding_handle_ops *ops;
128 uint8_t *out_data;
129 size_t out_length;
130 uint32_t out_flags;
133 static void dcerpc_binding_handle_raw_call_done(struct tevent_req *subreq);
135 struct tevent_req *dcerpc_binding_handle_raw_call_send(TALLOC_CTX *mem_ctx,
136 struct tevent_context *ev,
137 struct dcerpc_binding_handle *h,
138 const struct GUID *object,
139 uint32_t opnum,
140 uint32_t in_flags,
141 const uint8_t *in_data,
142 size_t in_length)
144 struct tevent_req *req;
145 struct dcerpc_binding_handle_raw_call_state *state;
146 struct tevent_req *subreq;
148 req = tevent_req_create(mem_ctx, &state,
149 struct dcerpc_binding_handle_raw_call_state);
150 if (req == NULL) {
151 return NULL;
153 state->ops = h->ops;
154 state->out_data = NULL;
155 state->out_length = 0;
156 state->out_flags = 0;
158 subreq = state->ops->raw_call_send(state, ev, h,
159 object, opnum,
160 in_flags, in_data, in_length);
161 if (tevent_req_nomem(subreq, req)) {
162 return tevent_req_post(req, ev);
164 tevent_req_set_callback(subreq, dcerpc_binding_handle_raw_call_done, req);
166 return req;
169 static void dcerpc_binding_handle_raw_call_done(struct tevent_req *subreq)
171 struct tevent_req *req = tevent_req_callback_data(subreq,
172 struct tevent_req);
173 struct dcerpc_binding_handle_raw_call_state *state =
174 tevent_req_data(req,
175 struct dcerpc_binding_handle_raw_call_state);
176 NTSTATUS error;
178 error = state->ops->raw_call_recv(subreq, state,
179 &state->out_data,
180 &state->out_length,
181 &state->out_flags);
182 TALLOC_FREE(subreq);
183 if (tevent_req_nterror(req, error)) {
184 return;
187 tevent_req_done(req);
190 NTSTATUS dcerpc_binding_handle_raw_call_recv(struct tevent_req *req,
191 TALLOC_CTX *mem_ctx,
192 uint8_t **out_data,
193 size_t *out_length,
194 uint32_t *out_flags)
196 struct dcerpc_binding_handle_raw_call_state *state =
197 tevent_req_data(req,
198 struct dcerpc_binding_handle_raw_call_state);
199 NTSTATUS error;
201 if (tevent_req_is_nterror(req, &error)) {
202 tevent_req_received(req);
203 return error;
206 *out_data = talloc_move(mem_ctx, &state->out_data);
207 *out_length = state->out_length;
208 *out_flags = state->out_flags;
209 tevent_req_received(req);
210 return NT_STATUS_OK;
213 NTSTATUS dcerpc_binding_handle_raw_call(struct dcerpc_binding_handle *h,
214 const struct GUID *object,
215 uint32_t opnum,
216 uint32_t in_flags,
217 const uint8_t *in_data,
218 size_t in_length,
219 TALLOC_CTX *mem_ctx,
220 uint8_t **out_data,
221 size_t *out_length,
222 uint32_t *out_flags)
224 TALLOC_CTX *frame = talloc_stackframe();
225 struct tevent_context *ev;
226 struct tevent_req *subreq;
227 NTSTATUS status;
230 * TODO: allow only one sync call
233 if (h->sync_ev) {
234 ev = h->sync_ev;
235 } else {
236 ev = samba_tevent_context_init(frame);
238 if (ev == NULL) {
239 talloc_free(frame);
240 return NT_STATUS_NO_MEMORY;
243 subreq = dcerpc_binding_handle_raw_call_send(frame, ev,
244 h, object, opnum,
245 in_flags,
246 in_data,
247 in_length);
248 if (subreq == NULL) {
249 talloc_free(frame);
250 return NT_STATUS_NO_MEMORY;
253 if (!tevent_req_poll(subreq, ev)) {
254 status = map_nt_error_from_unix_common(errno);
255 talloc_free(frame);
256 return status;
259 status = dcerpc_binding_handle_raw_call_recv(subreq,
260 mem_ctx,
261 out_data,
262 out_length,
263 out_flags);
264 if (!NT_STATUS_IS_OK(status)) {
265 talloc_free(frame);
266 return status;
269 TALLOC_FREE(frame);
270 return NT_STATUS_OK;
273 struct dcerpc_binding_handle_disconnect_state {
274 const struct dcerpc_binding_handle_ops *ops;
277 static void dcerpc_binding_handle_disconnect_done(struct tevent_req *subreq);
279 struct tevent_req *dcerpc_binding_handle_disconnect_send(TALLOC_CTX *mem_ctx,
280 struct tevent_context *ev,
281 struct dcerpc_binding_handle *h)
283 struct tevent_req *req;
284 struct dcerpc_binding_handle_disconnect_state *state;
285 struct tevent_req *subreq;
287 req = tevent_req_create(mem_ctx, &state,
288 struct dcerpc_binding_handle_disconnect_state);
289 if (req == NULL) {
290 return NULL;
293 state->ops = h->ops;
295 subreq = state->ops->disconnect_send(state, ev, h);
296 if (tevent_req_nomem(subreq, req)) {
297 return tevent_req_post(req, ev);
299 tevent_req_set_callback(subreq, dcerpc_binding_handle_disconnect_done, req);
301 return req;
304 static void dcerpc_binding_handle_disconnect_done(struct tevent_req *subreq)
306 struct tevent_req *req = tevent_req_callback_data(subreq,
307 struct tevent_req);
308 struct dcerpc_binding_handle_disconnect_state *state =
309 tevent_req_data(req,
310 struct dcerpc_binding_handle_disconnect_state);
311 NTSTATUS error;
313 error = state->ops->disconnect_recv(subreq);
314 TALLOC_FREE(subreq);
315 if (tevent_req_nterror(req, error)) {
316 return;
319 tevent_req_done(req);
322 NTSTATUS dcerpc_binding_handle_disconnect_recv(struct tevent_req *req)
324 NTSTATUS error;
326 if (tevent_req_is_nterror(req, &error)) {
327 tevent_req_received(req);
328 return error;
331 tevent_req_received(req);
332 return NT_STATUS_OK;
335 struct dcerpc_binding_handle_call_state {
336 struct dcerpc_binding_handle *h;
337 const struct ndr_interface_call *call;
338 TALLOC_CTX *r_mem;
339 void *r_ptr;
340 struct ndr_push *push;
341 DATA_BLOB request;
342 DATA_BLOB response;
343 struct ndr_pull *pull;
346 static void dcerpc_binding_handle_call_done(struct tevent_req *subreq);
348 struct tevent_req *dcerpc_binding_handle_call_send(TALLOC_CTX *mem_ctx,
349 struct tevent_context *ev,
350 struct dcerpc_binding_handle *h,
351 const struct GUID *object,
352 const struct ndr_interface_table *table,
353 uint32_t opnum,
354 TALLOC_CTX *r_mem,
355 void *r_ptr)
357 struct tevent_req *req;
358 struct dcerpc_binding_handle_call_state *state;
359 struct tevent_req *subreq;
360 enum ndr_err_code ndr_err;
362 req = tevent_req_create(mem_ctx, &state,
363 struct dcerpc_binding_handle_call_state);
364 if (req == NULL) {
365 return NULL;
368 #if 0 /* TODO: activate this when the callers are fixed */
369 if (table != h->table) {
370 tevent_req_nterror(req, NT_STATUS_INVALID_HANDLE);
371 return tevent_req_post(req, ev);
373 #endif
375 if (opnum >= table->num_calls) {
376 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
377 return tevent_req_post(req, ev);
380 state->h = h;
381 state->call = &table->calls[opnum];
383 state->r_mem = r_mem;
384 state->r_ptr = r_ptr;
386 /* setup for a ndr_push_* call */
387 state->push = ndr_push_init_ctx(state);
388 if (tevent_req_nomem(state->push, req)) {
389 return tevent_req_post(req, ev);
392 if (h->ops->ref_alloc && h->ops->ref_alloc(h)) {
393 state->push->flags |= LIBNDR_FLAG_REF_ALLOC;
396 if (h->ops->push_bigendian && h->ops->push_bigendian(h)) {
397 state->push->flags |= LIBNDR_FLAG_BIGENDIAN;
400 if (h->ops->use_ndr64 && h->ops->use_ndr64(h)) {
401 state->push->flags |= LIBNDR_FLAG_NDR64;
404 if (h->ops->do_ndr_print) {
405 h->ops->do_ndr_print(h, NDR_IN | NDR_SET_VALUES,
406 state->r_ptr, state->call);
409 /* push the structure into a blob */
410 ndr_err = state->call->ndr_push(state->push, NDR_IN, state->r_ptr);
411 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
412 NTSTATUS error;
413 error = ndr_map_error2ntstatus(ndr_err);
414 if (h->ops->ndr_push_failed) {
415 h->ops->ndr_push_failed(h, error,
416 state->r_ptr,
417 state->call);
419 tevent_req_nterror(req, error);
420 return tevent_req_post(req, ev);
423 /* retrieve the blob */
424 state->request = ndr_push_blob(state->push);
426 if (h->ops->ndr_validate_in) {
427 NTSTATUS error;
428 error = h->ops->ndr_validate_in(h, state,
429 &state->request,
430 state->call);
431 if (!NT_STATUS_IS_OK(error)) {
432 tevent_req_nterror(req, error);
433 return tevent_req_post(req, ev);
437 subreq = dcerpc_binding_handle_raw_call_send(state, ev,
438 h, object, opnum,
439 state->push->flags,
440 state->request.data,
441 state->request.length);
442 if (tevent_req_nomem(subreq, req)) {
443 return tevent_req_post(req, ev);
445 tevent_req_set_callback(subreq, dcerpc_binding_handle_call_done, req);
447 return req;
450 static void dcerpc_binding_handle_call_done(struct tevent_req *subreq)
452 struct tevent_req *req = tevent_req_callback_data(subreq,
453 struct tevent_req);
454 struct dcerpc_binding_handle_call_state *state =
455 tevent_req_data(req,
456 struct dcerpc_binding_handle_call_state);
457 struct dcerpc_binding_handle *h = state->h;
458 NTSTATUS error;
459 uint32_t out_flags = 0;
460 enum ndr_err_code ndr_err;
462 error = dcerpc_binding_handle_raw_call_recv(subreq, state,
463 &state->response.data,
464 &state->response.length,
465 &out_flags);
466 TALLOC_FREE(subreq);
467 if (tevent_req_nterror(req, error)) {
468 return;
471 state->pull = ndr_pull_init_blob(&state->response, state);
472 if (tevent_req_nomem(state->pull, req)) {
473 return;
475 state->pull->flags = state->push->flags;
477 if (out_flags & LIBNDR_FLAG_BIGENDIAN) {
478 state->pull->flags |= LIBNDR_FLAG_BIGENDIAN;
479 } else {
480 state->pull->flags &= ~LIBNDR_FLAG_BIGENDIAN;
483 state->pull->current_mem_ctx = state->r_mem;
485 /* pull the structure from the blob */
486 ndr_err = state->call->ndr_pull(state->pull, NDR_OUT, state->r_ptr);
487 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
488 error = ndr_map_error2ntstatus(ndr_err);
489 if (h->ops->ndr_pull_failed) {
490 h->ops->ndr_pull_failed(h, error,
491 &state->response,
492 state->call);
494 tevent_req_nterror(req, error);
495 return;
498 if (h->ops->do_ndr_print) {
499 h->ops->do_ndr_print(h, NDR_OUT,
500 state->r_ptr, state->call);
503 if (h->ops->ndr_validate_out) {
504 error = h->ops->ndr_validate_out(h,
505 state->pull,
506 state->r_ptr,
507 state->call);
508 if (!NT_STATUS_IS_OK(error)) {
509 tevent_req_nterror(req, error);
510 return;
514 tevent_req_done(req);
517 NTSTATUS dcerpc_binding_handle_call_recv(struct tevent_req *req)
519 return tevent_req_simple_recv_ntstatus(req);
522 NTSTATUS dcerpc_binding_handle_call(struct dcerpc_binding_handle *h,
523 const struct GUID *object,
524 const struct ndr_interface_table *table,
525 uint32_t opnum,
526 TALLOC_CTX *r_mem,
527 void *r_ptr)
529 TALLOC_CTX *frame = talloc_stackframe();
530 struct tevent_context *ev;
531 struct tevent_req *subreq;
532 NTSTATUS status = NT_STATUS_NO_MEMORY;
535 * TODO: allow only one sync call
538 if (h->sync_ev) {
539 ev = h->sync_ev;
540 } else {
541 ev = samba_tevent_context_init(frame);
543 if (ev == NULL) {
544 goto fail;
547 subreq = dcerpc_binding_handle_call_send(frame, ev,
548 h, object, table,
549 opnum, r_mem, r_ptr);
550 if (subreq == NULL) {
551 goto fail;
554 if (!tevent_req_poll_ntstatus(subreq, ev, &status)) {
555 goto fail;
558 status = dcerpc_binding_handle_call_recv(subreq);
559 fail:
560 TALLOC_FREE(frame);
561 return status;