librpc/rpc: make the dcerpc_binding_handle_raw_call_send/recv() wrapper more thin
[metze/samba/wip.git] / librpc / rpc / binding_handle.c
index 1e90827dbe175507825d1afc9c98488051a25745..bb5245772e1ca75fc578bf5d573582a44a7287c0 100644 (file)
@@ -98,11 +98,35 @@ uint32_t dcerpc_binding_handle_set_timeout(struct dcerpc_binding_handle *h,
        return h->ops->set_timeout(h, timeout);
 }
 
+void dcerpc_binding_handle_auth_info(struct dcerpc_binding_handle *h,
+                                    enum dcerpc_AuthType *auth_type,
+                                    enum dcerpc_AuthLevel *auth_level)
+{
+       enum dcerpc_AuthType _auth_type;
+       enum dcerpc_AuthLevel _auth_level;
+
+       if (auth_type == NULL) {
+               auth_type = &_auth_type;
+       }
+
+       if (auth_level == NULL) {
+               auth_level = &_auth_level;
+       }
+
+       *auth_type = DCERPC_AUTH_TYPE_NONE;
+       *auth_level = DCERPC_AUTH_LEVEL_NONE;
+
+       if (h->ops->auth_info == NULL) {
+               return;
+       }
+
+       h->ops->auth_info(h, auth_type, auth_level);
+}
+
 struct dcerpc_binding_handle_raw_call_state {
        const struct dcerpc_binding_handle_ops *ops;
-       uint8_t *out_data;
-       size_t out_length;
-       uint32_t out_flags;
+       struct tevent_context *ev;
+       struct tevent_req *subreq;
 };
 
 static void dcerpc_binding_handle_raw_call_done(struct tevent_req *subreq);
@@ -118,7 +142,6 @@ struct tevent_req *dcerpc_binding_handle_raw_call_send(TALLOC_CTX *mem_ctx,
 {
        struct tevent_req *req;
        struct dcerpc_binding_handle_raw_call_state *state;
-       struct tevent_req *subreq;
 
        req = tevent_req_create(mem_ctx, &state,
                                struct dcerpc_binding_handle_raw_call_state);
@@ -126,37 +149,45 @@ struct tevent_req *dcerpc_binding_handle_raw_call_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
        state->ops = h->ops;
-       state->out_data = NULL;
-       state->out_length = 0;
-       state->out_flags = 0;
+       state->ev = ev;
+
+       if (h->object != NULL) {
+               /*
+                * If an object is set on the binding handle,
+                * per request object passing is not allowed.
+                */
+               if (object != NULL) {
+                       tevent_req_nterror(req, NT_STATUS_INVALID_HANDLE);
+                       return tevent_req_post(req, ev);
+               }
 
-       subreq = state->ops->raw_call_send(state, ev, h,
-                                          object, opnum,
-                                          in_flags, in_data, in_length);
-       if (tevent_req_nomem(subreq, req)) {
+               /*
+                * We use the object from the binding handle
+                */
+               object = h->object;
+       }
+
+       state->subreq = state->ops->raw_call_send(state, ev, h,
+                                                 object, opnum,
+                                                 in_flags, in_data, in_length);
+       if (tevent_req_nomem(state->subreq, req)) {
                return tevent_req_post(req, ev);
        }
-       tevent_req_set_callback(subreq, dcerpc_binding_handle_raw_call_done, req);
+       tevent_req_set_callback(state->subreq,
+                               dcerpc_binding_handle_raw_call_done,
+                               req);
 
        return req;
 }
 
 static void dcerpc_binding_handle_raw_call_done(struct tevent_req *subreq)
 {
-       struct tevent_req *req = tevent_req_callback_data(subreq,
-                                struct tevent_req);
-       struct dcerpc_binding_handle_raw_call_state *state =
-               tevent_req_data(req,
-               struct dcerpc_binding_handle_raw_call_state);
-       NTSTATUS error;
+       struct tevent_req *req =
+               tevent_req_callback_data(subreq,
+               struct tevent_req);
 
-       error = state->ops->raw_call_recv(subreq, state,
-                                         &state->out_data,
-                                         &state->out_length,
-                                         &state->out_flags);
-       TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(error)) {
-               tevent_req_nterror(req, error);
+       if (tevent_req_is_in_progress(subreq)) {
+               tevent_req_notify_callback(req);
                return;
        }
 
@@ -174,14 +205,27 @@ NTSTATUS dcerpc_binding_handle_raw_call_recv(struct tevent_req *req,
                struct dcerpc_binding_handle_raw_call_state);
        NTSTATUS error;
 
-       if (tevent_req_is_nterror(req, &error)) {
+       if (!tevent_req_is_in_progress(req)) {
+               if (tevent_req_is_nterror(req, &error)) {
+                       tevent_req_received(req);
+                       return error;
+               }
+       }
+
+       error = state->ops->raw_call_recv(state->subreq,
+                                         mem_ctx,
+                                         out_data,
+                                         out_length,
+                                         out_flags);
+       if (!NT_STATUS_IS_OK(error)) {
                tevent_req_received(req);
                return error;
        }
 
-       *out_data = talloc_move(mem_ctx, &state->out_data);
-       *out_length = state->out_length;
-       *out_flags = state->out_flags;
+       if (tevent_req_is_in_progress(state->subreq)) {
+               return NT_STATUS_OK;
+       }
+
        tevent_req_received(req);
        return NT_STATUS_OK;
 }
@@ -200,7 +244,7 @@ NTSTATUS dcerpc_binding_handle_raw_call(struct dcerpc_binding_handle *h,
        TALLOC_CTX *frame = talloc_stackframe();
        struct tevent_context *ev;
        struct tevent_req *subreq;
-       NTSTATUS status;
+       NTSTATUS status = NT_STATUS_NO_MEMORY;
 
        /*
         * TODO: allow only one sync call
@@ -209,11 +253,10 @@ NTSTATUS dcerpc_binding_handle_raw_call(struct dcerpc_binding_handle *h,
        if (h->sync_ev) {
                ev = h->sync_ev;
        } else {
-               ev = tevent_context_init(frame);
+               ev = samba_tevent_context_init(frame);
        }
        if (ev == NULL) {
-               talloc_free(frame);
-               return NT_STATUS_NO_MEMORY;
+               goto fail;
        }
 
        subreq = dcerpc_binding_handle_raw_call_send(frame, ev,
@@ -222,14 +265,11 @@ NTSTATUS dcerpc_binding_handle_raw_call(struct dcerpc_binding_handle *h,
                                                     in_data,
                                                     in_length);
        if (subreq == NULL) {
-               talloc_free(frame);
-               return NT_STATUS_NO_MEMORY;
+               goto fail;
        }
 
-       if (!tevent_req_poll(subreq, ev)) {
-               status = map_nt_error_from_unix_common(errno);
-               talloc_free(frame);
-               return status;
+       if (!tevent_req_poll_ntstatus(subreq, ev, &status)) {
+               goto fail;
        }
 
        status = dcerpc_binding_handle_raw_call_recv(subreq,
@@ -237,13 +277,9 @@ NTSTATUS dcerpc_binding_handle_raw_call(struct dcerpc_binding_handle *h,
                                                     out_data,
                                                     out_length,
                                                     out_flags);
-       if (!NT_STATUS_IS_OK(status)) {
-               talloc_free(frame);
-               return status;
-       }
-
+fail:
        TALLOC_FREE(frame);
-       return NT_STATUS_OK;
+       return status;
 }
 
 struct dcerpc_binding_handle_disconnect_state {
@@ -288,8 +324,7 @@ static void dcerpc_binding_handle_disconnect_done(struct tevent_req *subreq)
 
        error = state->ops->disconnect_recv(subreq);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(error)) {
-               tevent_req_nterror(req, error);
+       if (tevent_req_nterror(req, error)) {
                return;
        }
 
@@ -342,12 +377,10 @@ struct tevent_req *dcerpc_binding_handle_call_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-#if 0 /* TODO: activate this when the callers are fixed */
        if (table != h->table) {
                tevent_req_nterror(req, NT_STATUS_INVALID_HANDLE);
                return tevent_req_post(req, ev);
        }
-#endif
 
        if (opnum >= table->num_calls) {
                tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
@@ -441,8 +474,7 @@ static void dcerpc_binding_handle_call_done(struct tevent_req *subreq)
                                                    &state->response.length,
                                                    &out_flags);
        TALLOC_FREE(subreq);
-       if (!NT_STATUS_IS_OK(error)) {
-               tevent_req_nterror(req, error);
+       if (tevent_req_nterror(req, error)) {
                return;
        }
 
@@ -494,15 +526,7 @@ static void dcerpc_binding_handle_call_done(struct tevent_req *subreq)
 
 NTSTATUS dcerpc_binding_handle_call_recv(struct tevent_req *req)
 {
-       NTSTATUS error;
-
-       if (tevent_req_is_nterror(req, &error)) {
-               tevent_req_received(req);
-               return error;
-       }
-
-       tevent_req_received(req);
-       return NT_STATUS_OK;
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 NTSTATUS dcerpc_binding_handle_call(struct dcerpc_binding_handle *h,
@@ -515,7 +539,7 @@ NTSTATUS dcerpc_binding_handle_call(struct dcerpc_binding_handle *h,
        TALLOC_CTX *frame = talloc_stackframe();
        struct tevent_context *ev;
        struct tevent_req *subreq;
-       NTSTATUS status;
+       NTSTATUS status = NT_STATUS_NO_MEMORY;
 
        /*
         * TODO: allow only one sync call
@@ -524,33 +548,25 @@ NTSTATUS dcerpc_binding_handle_call(struct dcerpc_binding_handle *h,
        if (h->sync_ev) {
                ev = h->sync_ev;
        } else {
-               ev = tevent_context_init(frame);
+               ev = samba_tevent_context_init(frame);
        }
        if (ev == NULL) {
-               talloc_free(frame);
-               return NT_STATUS_NO_MEMORY;
+               goto fail;
        }
 
        subreq = dcerpc_binding_handle_call_send(frame, ev,
                                                 h, object, table,
                                                 opnum, r_mem, r_ptr);
        if (subreq == NULL) {
-               talloc_free(frame);
-               return NT_STATUS_NO_MEMORY;
+               goto fail;
        }
 
-       if (!tevent_req_poll(subreq, ev)) {
-               status = map_nt_error_from_unix_common(errno);
-               talloc_free(frame);
-               return status;
+       if (!tevent_req_poll_ntstatus(subreq, ev, &status)) {
+               goto fail;
        }
 
        status = dcerpc_binding_handle_call_recv(subreq);
-       if (!NT_STATUS_IS_OK(status)) {
-               talloc_free(frame);
-               return status;
-       }
-
+fail:
        TALLOC_FREE(frame);
-       return NT_STATUS_OK;
+       return status;
 }