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/>.
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "librpc/rpc/dcerpc.h"
26 #include "rpc_common.h"
28 struct dcerpc_binding_handle {
30 const struct dcerpc_binding_handle_ops *ops;
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)
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,
51 struct dcerpc_binding_handle *h;
52 void **ppstate = (void **)pstate;
55 h = talloc_zero(mem_ctx, struct dcerpc_binding_handle);
60 h->location = location;
64 state = talloc_zero_size(h, psize);
69 talloc_set_name_const(state, type);
71 h->private_data = state;
73 talloc_set_destructor(h, dcerpc_binding_handle_destructor);
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)
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,
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) {
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 struct tevent_context *ev;
129 struct tevent_req *subreq;
132 static void dcerpc_binding_handle_raw_call_done(struct tevent_req *subreq);
134 struct tevent_req *dcerpc_binding_handle_raw_call_send(TALLOC_CTX *mem_ctx,
135 struct tevent_context *ev,
136 struct dcerpc_binding_handle *h,
137 const struct GUID *object,
140 const uint8_t *in_data,
143 struct tevent_req *req;
144 struct dcerpc_binding_handle_raw_call_state *state;
146 req = tevent_req_create(mem_ctx, &state,
147 struct dcerpc_binding_handle_raw_call_state);
154 if (h->object != NULL) {
156 * If an object is set on the binding handle,
157 * per request object passing is not allowed.
159 if (object != NULL) {
160 tevent_req_nterror(req, NT_STATUS_INVALID_HANDLE);
161 return tevent_req_post(req, ev);
165 * We use the object from the binding handle
170 if (state->ops->raw_call_in_send == NULL) {
171 if (in_flags & LIBNDR_FLAG_INCOMPLETE_BUFFER) {
172 tevent_req_nterror(req, NT_STATUS_RPC_CANNOT_SUPPORT);
173 return tevent_req_post(req, ev);
177 state->subreq = state->ops->raw_call_send(state, ev, h,
179 in_flags, in_data, in_length);
180 if (tevent_req_nomem(state->subreq, req)) {
181 return tevent_req_post(req, ev);
183 tevent_req_set_callback(state->subreq,
184 dcerpc_binding_handle_raw_call_done,
190 static void dcerpc_binding_handle_raw_call_done(struct tevent_req *subreq)
192 struct tevent_req *req =
193 tevent_req_callback_data(subreq,
196 if (tevent_req_is_in_progress(subreq)) {
197 tevent_req_notify_callback(req);
201 tevent_req_done(req);
204 NTSTATUS dcerpc_binding_handle_raw_call_recv(struct tevent_req *req,
210 struct dcerpc_binding_handle_raw_call_state *state =
212 struct dcerpc_binding_handle_raw_call_state);
215 if (!tevent_req_is_in_progress(req)) {
216 if (tevent_req_is_nterror(req, &error)) {
217 tevent_req_received(req);
222 error = state->ops->raw_call_recv(state->subreq,
227 if (!NT_STATUS_IS_OK(error)) {
228 tevent_req_received(req);
232 if (tevent_req_is_in_progress(state->subreq)) {
236 tevent_req_received(req);
240 NTSTATUS dcerpc_binding_handle_raw_call(struct dcerpc_binding_handle *h,
241 const struct GUID *object,
244 const uint8_t *in_data,
251 TALLOC_CTX *frame = talloc_stackframe();
252 struct tevent_context *ev;
253 struct tevent_req *subreq;
254 NTSTATUS status = NT_STATUS_NO_MEMORY;
257 * TODO: allow only one sync call
263 ev = samba_tevent_context_init(frame);
269 if (in_flags & LIBNDR_FLAG_INCOMPLETE_BUFFER) {
271 return NT_STATUS_INVALID_PARAMETER_MIX;
274 subreq = dcerpc_binding_handle_raw_call_send(frame, ev,
279 if (subreq == NULL) {
283 if (!tevent_req_poll_ntstatus(subreq, ev, &status)) {
287 status = dcerpc_binding_handle_raw_call_recv(subreq,
297 struct dcerpc_binding_handle_raw_call_in_state {
298 const struct dcerpc_binding_handle_ops *ops;
299 struct tevent_context *ev;
300 struct tevent_req *subreq;
303 static void dcerpc_binding_handle_raw_call_in_done(struct tevent_req *subreq);
305 struct tevent_req *dcerpc_binding_handle_raw_call_in_send(TALLOC_CTX *mem_ctx,
306 struct tevent_context *ev,
307 struct tevent_req *raw_call_req,
309 const uint8_t *in_data,
312 struct dcerpc_binding_handle_raw_call_state *raw_call_state =
313 tevent_req_data(raw_call_req,
314 struct dcerpc_binding_handle_raw_call_state);
315 struct tevent_req *req;
316 struct dcerpc_binding_handle_raw_call_in_state *state;
318 req = tevent_req_create(mem_ctx, &state,
319 struct dcerpc_binding_handle_raw_call_in_state);
323 state->ops = raw_call_state->ops;
326 if (state->ev != raw_call_state->ev) {
327 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
328 return tevent_req_post(req, ev);
331 if (state->ops->raw_call_in_send == NULL) {
332 tevent_req_nterror(req, NT_STATUS_RPC_CANNOT_SUPPORT);
333 return tevent_req_post(req, ev);
336 state->subreq = state->ops->raw_call_in_send(state, ev,
337 raw_call_state->subreq,
341 if (tevent_req_nomem(state->subreq, req)) {
342 return tevent_req_post(req, ev);
345 tevent_req_set_callback(state->subreq,
346 dcerpc_binding_handle_raw_call_in_done,
351 static void dcerpc_binding_handle_raw_call_in_done(struct tevent_req *subreq)
353 struct tevent_req *req =
354 tevent_req_callback_data(subreq,
357 if (tevent_req_is_in_progress(subreq)) {
358 tevent_req_notify_callback(req);
362 tevent_req_done(req);
365 NTSTATUS dcerpc_binding_handle_raw_call_in_recv(struct tevent_req *req)
367 struct dcerpc_binding_handle_raw_call_in_state *state =
369 struct dcerpc_binding_handle_raw_call_in_state);
372 if (!tevent_req_is_in_progress(req)) {
373 if (tevent_req_is_nterror(req, &error)) {
374 tevent_req_received(req);
379 error = state->ops->raw_call_in_recv(state->subreq);
380 if (!NT_STATUS_IS_OK(error)) {
381 tevent_req_received(req);
385 if (tevent_req_is_in_progress(state->subreq)) {
389 tevent_req_received(req);
393 struct dcerpc_binding_handle_disconnect_state {
394 const struct dcerpc_binding_handle_ops *ops;
397 static void dcerpc_binding_handle_disconnect_done(struct tevent_req *subreq);
399 struct tevent_req *dcerpc_binding_handle_disconnect_send(TALLOC_CTX *mem_ctx,
400 struct tevent_context *ev,
401 struct dcerpc_binding_handle *h)
403 struct tevent_req *req;
404 struct dcerpc_binding_handle_disconnect_state *state;
405 struct tevent_req *subreq;
407 req = tevent_req_create(mem_ctx, &state,
408 struct dcerpc_binding_handle_disconnect_state);
415 subreq = state->ops->disconnect_send(state, ev, h);
416 if (tevent_req_nomem(subreq, req)) {
417 return tevent_req_post(req, ev);
419 tevent_req_set_callback(subreq, dcerpc_binding_handle_disconnect_done, req);
424 static void dcerpc_binding_handle_disconnect_done(struct tevent_req *subreq)
426 struct tevent_req *req = tevent_req_callback_data(subreq,
428 struct dcerpc_binding_handle_disconnect_state *state =
430 struct dcerpc_binding_handle_disconnect_state);
433 error = state->ops->disconnect_recv(subreq);
435 if (tevent_req_nterror(req, error)) {
439 tevent_req_done(req);
442 NTSTATUS dcerpc_binding_handle_disconnect_recv(struct tevent_req *req)
446 if (tevent_req_is_nterror(req, &error)) {
447 tevent_req_received(req);
451 tevent_req_received(req);
455 struct dcerpc_binding_handle_call_params_state {
456 struct dcerpc_binding_handle *h;
457 const struct ndr_interface_call *call;
458 struct dcerpc_binding_handle_call_params *params;
460 struct ndr_push *push;
462 struct tevent_req *subreq;
464 struct ndr_pull *pull;
466 struct dcerpc_pipe_handle *ph;
467 struct dcerpc_pipe_handle_connection *pc;
468 const struct ndr_interface_call_pipe *call_pipe;
469 uint32_t in_pipe_idx;
470 uint32_t out_pipe_idx;
473 static void dcerpc_binding_handle_call_params_in_done(struct tevent_req *subreq);
474 static void dcerpc_binding_handle_call_params_next_pipe(struct tevent_req *req);
475 static void dcerpc_binding_handle_call_params_done(struct tevent_req *subreq);
476 static void dcerpc_binding_handle_call_params_response(struct tevent_req *req);
477 static void dcerpc_binding_handle_call_params_pipe_setup(struct tevent_req *call_req);
478 static void dcerpc_binding_handle_call_params_pipe_notify(struct dcerpc_pipe_handle *p);
480 struct tevent_req *dcerpc_binding_handle_call_params_send(TALLOC_CTX *mem_ctx,
481 struct tevent_context *ev,
482 struct dcerpc_binding_handle *h,
483 const struct GUID *object,
484 const struct ndr_interface_table *table,
486 struct dcerpc_binding_handle_call_params *params)
488 struct tevent_req *req;
489 struct dcerpc_binding_handle_call_params_state *state;
490 enum ndr_err_code ndr_err;
492 req = tevent_req_create(mem_ctx, &state,
493 struct dcerpc_binding_handle_call_params_state);
498 if (table != h->table) {
499 tevent_req_nterror(req, NT_STATUS_INVALID_HANDLE);
500 return tevent_req_post(req, ev);
503 if (opnum >= table->num_calls) {
504 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
505 return tevent_req_post(req, ev);
509 state->call = &table->calls[opnum];
510 state->params = params;
512 if (params->in.num_pipes != state->call->in_pipes.num_pipes) {
513 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
514 return tevent_req_post(req, ev);
517 if (params->out.num_pipes != state->call->out_pipes.num_pipes) {
518 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
519 return tevent_req_post(req, ev);
522 /* setup for a ndr_push_* call */
523 state->push = ndr_push_init_ctx(state);
524 if (tevent_req_nomem(state->push, req)) {
525 return tevent_req_post(req, ev);
528 if (h->ops->ref_alloc && h->ops->ref_alloc(h)) {
529 state->push->flags |= LIBNDR_FLAG_REF_ALLOC;
532 if (h->ops->push_bigendian && h->ops->push_bigendian(h)) {
533 state->push->flags |= LIBNDR_FLAG_BIGENDIAN;
534 state->in_flags |= LIBNDR_FLAG_BIGENDIAN;
537 if (h->ops->use_ndr64 && h->ops->use_ndr64(h)) {
538 state->push->flags |= LIBNDR_FLAG_NDR64;
541 if (h->ops->do_ndr_print) {
542 h->ops->do_ndr_print(h, NDR_IN | NDR_SET_VALUES,
543 state->params->r_ptr, state->call);
546 /* push the structure into a blob */
547 ndr_err = state->call->ndr_push(state->push, NDR_IN, state->params->r_ptr);
548 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
550 error = ndr_map_error2ntstatus(ndr_err);
551 if (h->ops->ndr_push_failed) {
552 h->ops->ndr_push_failed(h, error,
553 state->params->r_ptr,
556 tevent_req_nterror(req, error);
557 return tevent_req_post(req, ev);
560 /* retrieve the blob */
561 state->request = ndr_push_blob(state->push);
563 if (h->ops->ndr_validate_in) {
565 error = h->ops->ndr_validate_in(h, state,
568 if (!NT_STATUS_IS_OK(error)) {
569 tevent_req_nterror(req, error);
570 return tevent_req_post(req, ev);
574 if (params->in.num_pipes != 0) {
575 state->in_flags |= LIBNDR_FLAG_INCOMPLETE_BUFFER;
578 * push alignment for the next pipe chunk
580 ndr_err = ndr_push_align(state->push, 5);
581 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
583 error = ndr_map_error2ntstatus(ndr_err);
584 tevent_req_nterror(req, error);
585 return tevent_req_post(req, ev);
589 if (params->out.num_pipes != 0) {
591 * even if we only have output pipes we need to indicate that
592 * we want to get incomplete results
594 state->in_flags |= LIBNDR_FLAG_INCOMPLETE_BUFFER;
597 if (state->in_flags & LIBNDR_FLAG_INCOMPLETE_BUFFER) {
598 tevent_req_defer_callback(req, ev);
599 dcerpc_binding_handle_call_params_pipe_setup(req);
600 if (!tevent_req_is_in_progress(req)) {
601 return tevent_req_post(req, ev);
605 /* retrieve the blob - including possible pipe chunk alignment */
606 state->request = ndr_push_blob(state->push);
608 state->subreq = dcerpc_binding_handle_raw_call_send(state, ev,
612 state->request.length);
613 if (tevent_req_nomem(state->subreq, req)) {
614 return tevent_req_post(req, ev);
616 tevent_req_set_callback(state->subreq,
617 dcerpc_binding_handle_call_params_done,
620 if (!(state->in_flags & LIBNDR_FLAG_INCOMPLETE_BUFFER)) {
622 * we didn't ask for any pipes
624 * Indicate that all pipes are done.
626 state->in_pipe_idx = UINT32_MAX;
627 state->out_pipe_idx = UINT32_MAX;
632 * If the subreq is already finished, the backend
633 * may not support LIBNDR_FLAG_INCOMPLETE_BUFFER
635 if (!tevent_req_is_in_progress(state->subreq)) {
639 dcerpc_binding_handle_call_params_next_pipe(req);
640 if (!tevent_req_is_in_progress(req)) {
641 return tevent_req_post(req, ev);
644 if (state->params->in.num_pipes == 0) {
645 struct tevent_req *subreq;
648 * We have only out pipes,
649 * so indicate that we're done with sending in_data.
651 state->in_pipe_idx = UINT32_MAX;
652 state->in_flags &= ~LIBNDR_FLAG_INCOMPLETE_BUFFER;
653 subreq = dcerpc_binding_handle_raw_call_in_send(state, ev,
658 if (tevent_req_nomem(subreq, req)) {
659 return tevent_req_post(req, ev);
661 tevent_req_set_callback(subreq,
662 dcerpc_binding_handle_call_params_in_done,
670 static void dcerpc_binding_handle_call_params_in_done(struct tevent_req *subreq)
672 struct tevent_req *req = tevent_req_callback_data(subreq,
676 error = dcerpc_binding_handle_raw_call_in_recv(subreq);
678 if (tevent_req_nterror(req, error)) {
687 static void dcerpc_binding_handle_call_params_next_pipe(struct tevent_req *req)
689 struct dcerpc_binding_handle_call_params_state *state =
691 struct dcerpc_binding_handle_call_params_state);
692 struct dcerpc_binding_handle_call_params *params = state->params;
695 dcerpc_pipe_handle_connection_disconnect(state->pc);
697 state->call_pipe = NULL;
699 if (state->in_pipe_idx < params->in.num_pipes) {
700 uint32_t idx = state->in_pipe_idx++;
702 state->pc = params->in.pipes[idx];
703 state->call_pipe = &state->call->in_pipes.pipes[idx];
705 ok = dcerpc_pipe_handle_connection_push_connect(state->pc,
706 state->call_pipe->chunk_struct_name,
707 state->call_pipe->chunk_struct_size,
710 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
716 state->in_pipe_idx = UINT32_MAX;
718 if (state->out_pipe_idx < params->out.num_pipes) {
719 uint32_t idx = state->out_pipe_idx++;
721 state->pc = params->out.pipes[idx];
722 state->call_pipe = &state->call->out_pipes.pipes[idx];
724 ok = dcerpc_pipe_handle_connection_pull_connect(state->pc,
725 state->call_pipe->chunk_struct_name,
726 state->call_pipe->chunk_struct_size,
729 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER_MIX);
735 state->out_pipe_idx = UINT32_MAX;
737 if (state->pull == NULL) {
741 if (state->pull->flags & LIBNDR_FLAG_INCOMPLETE_BUFFER) {
745 dcerpc_binding_handle_call_params_response(req);
748 static void dcerpc_binding_handle_call_params_done(struct tevent_req *subreq)
750 struct tevent_req *req = tevent_req_callback_data(subreq,
752 struct dcerpc_binding_handle_call_params_state *state =
754 struct dcerpc_binding_handle_call_params_state);
755 struct dcerpc_binding_handle *h = state->h;
757 uint32_t out_flags = 0;
758 enum ndr_err_code ndr_err;
760 error = dcerpc_binding_handle_raw_call_recv(subreq, state,
761 &state->response.data,
762 &state->response.length,
765 if (!NT_STATUS_IS_OK(error)) {
766 TALLOC_FREE(state->subreq);
767 tevent_req_nterror(req, error);
771 if (!(out_flags & LIBNDR_FLAG_INCOMPLETE_BUFFER)) {
772 TALLOC_FREE(state->subreq);
775 if (state->in_pipe_idx != UINT32_MAX) {
777 * we haven't send all data yet,
778 * this is a protocol error
780 tevent_req_nterror(req, NT_STATUS_RPC_PROTOCOL_ERROR);
784 if (state->pull == NULL) {
785 state->pull = ndr_pull_init_blob(&state->response, state);
786 if (tevent_req_nomem(state->pull, req)) {
789 state->pull->flags = state->push->flags;
791 if (out_flags & LIBNDR_FLAG_BIGENDIAN) {
792 state->pull->flags |= LIBNDR_FLAG_BIGENDIAN;
794 state->pull->flags &= ~LIBNDR_FLAG_BIGENDIAN;
797 if (out_flags & LIBNDR_FLAG_INCOMPLETE_BUFFER) {
798 state->pull->flags |= LIBNDR_FLAG_INCOMPLETE_BUFFER;
801 if (!(out_flags & LIBNDR_FLAG_INCOMPLETE_BUFFER)) {
802 state->pull->flags &= ~LIBNDR_FLAG_INCOMPLETE_BUFFER;
805 ndr_err = ndr_pull_append(state->pull,
807 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
808 error = ndr_map_error2ntstatus(ndr_err);
809 if (h->ops->ndr_pull_failed) {
810 h->ops->ndr_pull_failed(h, error,
814 tevent_req_nterror(req, error);
819 if (state->out_pipe_idx != UINT32_MAX) {
820 dcerpc_binding_handle_call_params_pipe_notify(state->ph);
824 dcerpc_binding_handle_call_params_response(req);
827 static void dcerpc_binding_handle_call_params_response(struct tevent_req *req)
829 struct dcerpc_binding_handle_call_params_state *state =
831 struct dcerpc_binding_handle_call_params_state);
832 struct dcerpc_binding_handle *h = state->h;
834 enum ndr_err_code ndr_err;
836 state->pull->current_mem_ctx = state->params->r_mem;
838 /* pull the structure from the blob */
839 ndr_err = state->call->ndr_pull(state->pull, NDR_OUT,
840 state->params->r_ptr);
841 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
842 error = ndr_map_error2ntstatus(ndr_err);
843 if (h->ops->ndr_pull_failed) {
844 h->ops->ndr_pull_failed(h, error,
848 tevent_req_nterror(req, error);
852 if (h->ops->do_ndr_print) {
853 h->ops->do_ndr_print(h, NDR_OUT,
854 state->params->r_ptr, state->call);
857 if (h->ops->ndr_validate_out) {
858 error = h->ops->ndr_validate_out(h,
860 state->params->r_ptr,
862 if (!NT_STATUS_IS_OK(error)) {
863 tevent_req_nterror(req, error);
868 tevent_req_done(req);
871 NTSTATUS dcerpc_binding_handle_call_params_recv(struct tevent_req *req)
873 return tevent_req_simple_recv_ntstatus(req);
876 struct dcerpc_binding_handle_call_params_pipe {
877 struct tevent_req *call_req;
878 struct tevent_req *pull_req;
881 struct dcerpc_binding_handle_call_params_push_state {
882 struct tevent_context *ev;
883 struct dcerpc_pipe_handle *p;
884 struct ndr_push *push;
885 DATA_BLOB chunk_blob;
889 static int dcerpc_binding_handle_call_params_push_state_destructor(
890 struct dcerpc_binding_handle_call_params_push_state *state)
892 struct dcerpc_binding_handle_call_params_pipe *pp =
893 dcerpc_pipe_handle_data(state->p,
894 struct dcerpc_binding_handle_call_params_pipe);
895 struct dcerpc_binding_handle_call_params_state *call_state =
896 tevent_req_data(pp->call_req,
897 struct dcerpc_binding_handle_call_params_state);
899 dcerpc_pipe_handle_connection_disconnect(call_state->pc);
900 call_state->pc = NULL;
901 call_state->call_pipe = NULL;
905 static void dcerpc_binding_handle_call_params_push_done(struct tevent_req *subreq);
907 static struct tevent_req *dcerpc_binding_handle_call_params_push_send(TALLOC_CTX *mem_ctx,
908 struct tevent_context *ev,
909 struct dcerpc_pipe_handle *p,
910 const void *chunk_ptr)
912 struct dcerpc_binding_handle_call_params_pipe *pp =
913 dcerpc_pipe_handle_data(p,
914 struct dcerpc_binding_handle_call_params_pipe);
915 struct dcerpc_binding_handle_call_params_state *call_state =
916 tevent_req_data(pp->call_req,
917 struct dcerpc_binding_handle_call_params_state);
918 struct tevent_req *req;
919 struct dcerpc_binding_handle_call_params_push_state *state;
920 struct tevent_req *subreq;
921 enum ndr_err_code ndr_err;
922 const uint32_t *count = NULL;
923 bool is_last_pipe = false;
925 req = tevent_req_create(mem_ctx, &state,
926 struct dcerpc_binding_handle_call_params_push_state);
933 tevent_req_defer_callback(req, state->ev);
935 talloc_set_destructor(state,
936 dcerpc_binding_handle_call_params_push_state_destructor);
938 /* setup for a ndr_push_* call */
939 state->push = ndr_push_init_ctx(state);
940 if (tevent_req_nomem(state->push, req)) {
941 return tevent_req_post(req, ev);
944 state->push->flags = call_state->push->flags;
946 //if (h->ops->do_ndr_print) {
947 // h->ops->do_ndr_print(h, NDR_IN | NDR_SET_VALUES,
948 // state->params->r_ptr, state->call);
951 /* push the structure into a blob */
952 ndr_err = call_state->call_pipe->ndr_push(state->push,
953 NDR_SCALARS|NDR_BUFFERS,
955 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
957 error = ndr_map_error2ntstatus(ndr_err);
958 //if (h->ops->ndr_push_failed) {
959 // h->ops->ndr_push_failed(h, error,
960 // state->params->r_ptr,
963 tevent_req_nterror(req, error);
964 return tevent_req_post(req, ev);
968 * Note: the first struct member is always
971 count = (const uint32_t *)chunk_ptr;
973 state->is_last_chunk = false;
975 state->is_last_chunk = true;
978 if (call_state->in_pipe_idx >= call_state->params->in.num_pipes) {
982 if (is_last_pipe && state->is_last_chunk) {
983 call_state->in_flags &= ~LIBNDR_FLAG_INCOMPLETE_BUFFER;
986 * push alignment for the next pipe chunk
988 ndr_err = ndr_push_align(state->push, 5);
989 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
991 error = ndr_map_error2ntstatus(ndr_err);
992 tevent_req_nterror(req, error);
993 return tevent_req_post(req, ev);
997 /* retrieve the blob - including possible alignment for the next chunk */
998 state->chunk_blob = ndr_push_blob(state->push);
1000 subreq = dcerpc_binding_handle_raw_call_in_send(state, ev,
1002 call_state->in_flags,
1003 state->chunk_blob.data,
1004 state->chunk_blob.length);
1005 if (tevent_req_nomem(subreq, req)) {
1006 return tevent_req_post(req, ev);
1008 tevent_req_set_callback(subreq,
1009 dcerpc_binding_handle_call_params_push_done,
1015 static void dcerpc_binding_handle_call_params_push_done(struct tevent_req *subreq)
1017 struct tevent_req *req =
1018 tevent_req_callback_data(subreq,
1020 struct dcerpc_binding_handle_call_params_push_state *state =
1021 tevent_req_data(req,
1022 struct dcerpc_binding_handle_call_params_push_state);
1023 struct dcerpc_binding_handle_call_params_pipe *pp =
1024 dcerpc_pipe_handle_data(state->p,
1025 struct dcerpc_binding_handle_call_params_pipe);
1028 status = dcerpc_binding_handle_raw_call_in_recv(subreq);
1029 TALLOC_FREE(subreq);
1030 TALLOC_FREE(state->push);
1031 if (tevent_req_nterror(req, status)) {
1035 talloc_set_destructor(state, NULL);
1037 if (!state->is_last_chunk) {
1038 tevent_req_done(req);
1042 tevent_req_done(req);
1043 dcerpc_binding_handle_call_params_next_pipe(pp->call_req);
1046 static NTSTATUS dcerpc_binding_handle_call_params_push_recv(struct tevent_req *req)
1048 return tevent_req_simple_recv_ntstatus(req);
1051 struct dcerpc_binding_handle_call_params_pull_state {
1052 struct tevent_context *ev;
1053 struct dcerpc_pipe_handle *p;
1058 static int dcerpc_binding_handle_call_params_pull_state_destructor(
1059 struct dcerpc_binding_handle_call_params_pull_state *state)
1061 struct dcerpc_binding_handle_call_params_pipe *pp =
1062 dcerpc_pipe_handle_data(state->p,
1063 struct dcerpc_binding_handle_call_params_pipe);
1064 struct dcerpc_binding_handle_call_params_state *call_state =
1065 tevent_req_data(pp->call_req,
1066 struct dcerpc_binding_handle_call_params_state);
1068 pp->pull_req = NULL;
1070 dcerpc_pipe_handle_connection_disconnect(call_state->pc);
1071 call_state->pc = NULL;
1072 call_state->call_pipe = NULL;
1076 static void dcerpc_binding_handle_call_params_pull_notify(struct tevent_req *req);
1078 static struct tevent_req *dcerpc_binding_handle_call_params_pull_send(TALLOC_CTX *mem_ctx,
1079 struct tevent_context *ev,
1080 struct dcerpc_pipe_handle *p,
1084 struct dcerpc_binding_handle_call_params_pipe *pp =
1085 dcerpc_pipe_handle_data(p,
1086 struct dcerpc_binding_handle_call_params_pipe);
1087 struct tevent_req *req;
1088 struct dcerpc_binding_handle_call_params_pull_state *state;
1090 req = tevent_req_create(mem_ctx, &state,
1091 struct dcerpc_binding_handle_call_params_pull_state);
1097 state->chunk_mem = chunk_mem;
1098 state->chunk_ptr = chunk_ptr;
1100 tevent_req_defer_callback(req, state->ev);
1102 dcerpc_binding_handle_call_params_pull_notify(req);
1103 if (!tevent_req_is_in_progress(req)) {
1104 return tevent_req_post(req, ev);
1107 talloc_set_destructor(state,
1108 dcerpc_binding_handle_call_params_pull_state_destructor);
1114 static void dcerpc_binding_handle_call_params_pull_notify(struct tevent_req *req)
1116 struct dcerpc_binding_handle_call_params_pull_state *state =
1117 tevent_req_data(req,
1118 struct dcerpc_binding_handle_call_params_pull_state);
1119 struct dcerpc_binding_handle_call_params_pipe *pp =
1120 dcerpc_pipe_handle_data(state->p,
1121 struct dcerpc_binding_handle_call_params_pipe);
1122 struct dcerpc_binding_handle_call_params_state *call_state =
1123 tevent_req_data(pp->call_req,
1124 struct dcerpc_binding_handle_call_params_state);
1125 enum ndr_err_code ndr_err;
1126 const uint32_t *count = NULL;
1128 if (call_state->pull == NULL) {
1132 if (call_state->pull->data_size == 0) {
1136 call_state->pull->current_mem_ctx = state->chunk_mem;
1138 /* pull the structure from the blob */
1139 ndr_err = call_state->call_pipe->ndr_pull(call_state->pull,
1140 NDR_SCALARS|NDR_BUFFERS,
1142 if (ndr_err == NDR_ERR_INCOMPLETE_BUFFER) {
1146 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1148 error = ndr_map_error2ntstatus(ndr_err);
1149 //if (h->ops->ndr_pull_failed) {
1150 // h->ops->ndr_pull_failed(h, error,
1151 // &state->response,
1154 tevent_req_nterror(req, error);
1158 //if (h->ops->do_ndr_print) {
1159 // h->ops->do_ndr_print(h, NDR_OUT,
1160 // state->params->r_ptr, state->call);
1163 //if (h->ops->ndr_validate_out) {
1164 // error = h->ops->ndr_validate_out(h,
1166 // state->params->r_ptr,
1168 // if (!NT_STATUS_IS_OK(error)) {
1169 // tevent_req_nterror(req, error);
1174 //ndr_err = ndr_pull_pop(call_state->pull);
1175 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1177 error = ndr_map_error2ntstatus(ndr_err);
1178 tevent_req_nterror(req, error);
1182 pp->pull_req = NULL;
1183 talloc_set_destructor(state, NULL);
1186 * Note: the first struct member is always
1189 count = (const uint32_t *)state->chunk_ptr;
1192 tevent_req_done(req);
1196 tevent_req_done(req);
1197 dcerpc_binding_handle_call_params_next_pipe(pp->call_req);
1201 static NTSTATUS dcerpc_binding_handle_call_params_pull_recv(struct tevent_req *req)
1203 return tevent_req_simple_recv_ntstatus(req);
1206 static struct dcerpc_pipe_handle_ops dcerpc_binding_handle_call_params_pipe_ops = {
1207 .name = "dcerpc_binding_handle_call_params_pipe",
1209 .chunk_push_send = dcerpc_binding_handle_call_params_push_send,
1210 .chunk_push_recv = dcerpc_binding_handle_call_params_push_recv,
1212 .chunk_pull_send = dcerpc_binding_handle_call_params_pull_send,
1213 .chunk_pull_recv = dcerpc_binding_handle_call_params_pull_recv,
1216 static void dcerpc_binding_handle_call_params_pipe_setup(struct tevent_req *call_req)
1218 struct dcerpc_binding_handle_call_params_state *call_state =
1219 tevent_req_data(call_req,
1220 struct dcerpc_binding_handle_call_params_state);
1221 struct dcerpc_binding_handle_call_params_pipe *pp;
1223 call_state->ph = dcerpc_pipe_handle_create(call_state,
1224 &dcerpc_binding_handle_call_params_pipe_ops,
1226 struct dcerpc_binding_handle_call_params_pipe);
1227 if (tevent_req_nomem(call_state->ph, call_req)) {
1231 pp->call_req = call_req;
1234 static void dcerpc_binding_handle_call_params_pipe_notify(struct dcerpc_pipe_handle *p)
1236 struct dcerpc_binding_handle_call_params_pipe *pp =
1237 dcerpc_pipe_handle_data(p,
1238 struct dcerpc_binding_handle_call_params_pipe);
1240 if (pp->pull_req == NULL) {
1244 dcerpc_binding_handle_call_params_pull_notify(pp->pull_req);
1247 struct dcerpc_binding_handle_call_state {
1248 struct dcerpc_binding_handle_call_params params;
1251 static void dcerpc_binding_handle_call_done(struct tevent_req *subreq);
1253 struct tevent_req *dcerpc_binding_handle_call_send(TALLOC_CTX *mem_ctx,
1254 struct tevent_context *ev,
1255 struct dcerpc_binding_handle *h,
1256 const struct GUID *object,
1257 const struct ndr_interface_table *table,
1262 struct tevent_req *req;
1263 struct dcerpc_binding_handle_call_state *state;
1264 struct tevent_req *subreq;
1266 req = tevent_req_create(mem_ctx, &state,
1267 struct dcerpc_binding_handle_call_state);
1272 state->params.r_mem = r_mem;
1273 state->params.r_ptr = r_ptr;
1275 subreq = dcerpc_binding_handle_call_params_send(state, ev, h,
1276 object, table, opnum,
1278 if (tevent_req_nomem(subreq, req)) {
1279 return tevent_req_post(req, ev);
1281 tevent_req_set_callback(subreq, dcerpc_binding_handle_call_done, req);
1286 static void dcerpc_binding_handle_call_done(struct tevent_req *subreq)
1288 struct tevent_req *req = tevent_req_callback_data(subreq,
1292 error = dcerpc_binding_handle_call_params_recv(subreq);
1293 TALLOC_FREE(subreq);
1294 if (tevent_req_nterror(req, error)) {
1298 tevent_req_done(req);
1301 NTSTATUS dcerpc_binding_handle_call_recv(struct tevent_req *req)
1303 return tevent_req_simple_recv_ntstatus(req);
1306 NTSTATUS dcerpc_binding_handle_call(struct dcerpc_binding_handle *h,
1307 const struct GUID *object,
1308 const struct ndr_interface_table *table,
1313 TALLOC_CTX *frame = talloc_stackframe();
1314 struct tevent_context *ev;
1315 struct tevent_req *subreq;
1316 struct dcerpc_binding_handle_call_params params;
1317 NTSTATUS status = NT_STATUS_NO_MEMORY;
1320 * TODO: allow only one sync call
1326 ev = samba_tevent_context_init(frame);
1332 ZERO_STRUCT(params);
1333 params.r_mem = r_mem;
1334 params.r_ptr = r_ptr;
1336 subreq = dcerpc_binding_handle_call_params_send(frame, ev, h,
1337 object, table, opnum,
1339 if (subreq == NULL) {
1343 if (!tevent_req_poll_ntstatus(subreq, ev, &status)) {
1347 status = dcerpc_binding_handle_call_params_recv(subreq);