rpc: Convert npa_tstream.c to use tstream_u32_read_send
authorVolker Lendecke <vl@samba.org>
Mon, 1 Apr 2019 13:23:11 +0000 (15:23 +0200)
committerJeremy Allison <jra@samba.org>
Fri, 5 Apr 2019 01:00:48 +0000 (01:00 +0000)
This avoids a bit of code duplication. Overall the last two commits
add a few lines, but that also contains the header file and another GPL
header for tstream_u32_read.c.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
Autobuild-User(master): Jeremy Allison <jra@samba.org>
Autobuild-Date(master): Fri Apr  5 01:00:48 UTC 2019 on sn-devel-144

libcli/named_pipe_auth/npa_tstream.c

index 3ba1c2cbf34d6d9cbdaa49b3309cdf48e9a6ba95..8fc03371a5065c7af66da629d6324ebb21610cb9 100644 (file)
@@ -24,6 +24,7 @@
 #include "../lib/tsocket/tsocket_internal.h"
 #include "../librpc/gen_ndr/ndr_named_pipe_auth.h"
 #include "../libcli/named_pipe_auth/npa_tstream.h"
+#include "../libcli/named_pipe_auth/tstream_u32_read.h"
 #include "../libcli/smb/smb_constants.h"
 
 static const struct tstream_context_ops tstream_npa_ops;
@@ -51,7 +52,6 @@ struct tstream_npa_connect_state {
        struct iovec auth_req_iov;
 
        struct named_pipe_auth_rep auth_rep;
-       DATA_BLOB auth_rep_blob;
 };
 
 static void tstream_npa_connect_unix_done(struct tevent_req *subreq);
@@ -210,11 +210,6 @@ static void tstream_npa_connect_unix_done(struct tevent_req *subreq)
        tevent_req_set_callback(subreq, tstream_npa_connect_writev_done, req);
 }
 
-static int tstream_npa_connect_next_vector(struct tstream_context *unix_stream,
-                                          void *private_data,
-                                          TALLOC_CTX *mem_ctx,
-                                          struct iovec **_vector,
-                                          size_t *_count);
 static void tstream_npa_connect_readv_done(struct tevent_req *subreq);
 
 static void tstream_npa_connect_writev_done(struct tevent_req *subreq)
@@ -235,81 +230,11 @@ static void tstream_npa_connect_writev_done(struct tevent_req *subreq)
                return;
        }
 
-       state->auth_rep_blob = data_blob_const(NULL, 0);
-
-       subreq = tstream_readv_pdu_send(state, state->caller.ev,
-                                       state->unix_stream,
-                                       tstream_npa_connect_next_vector,
-                                       state);
-       if (tevent_req_nomem(subreq, req)) {
-               return;
-       }
+       subreq = tstream_u32_read_send(
+               state, state->caller.ev, 0x00FFFFFF, state->unix_stream);
        tevent_req_set_callback(subreq, tstream_npa_connect_readv_done, req);
 }
 
-static int tstream_npa_connect_next_vector(struct tstream_context *unix_stream,
-                                          void *private_data,
-                                          TALLOC_CTX *mem_ctx,
-                                          struct iovec **_vector,
-                                          size_t *_count)
-{
-       struct tstream_npa_connect_state *state = talloc_get_type_abort(private_data,
-                                       struct tstream_npa_connect_state);
-       struct iovec *vector;
-       size_t count;
-       off_t ofs = 0;
-
-       if (state->auth_rep_blob.length == 0) {
-               state->auth_rep_blob = data_blob_talloc(state, NULL, 4);
-               if (!state->auth_rep_blob.data) {
-                       return -1;
-               }
-       } else if (state->auth_rep_blob.length == 4) {
-               uint32_t msg_len;
-
-               ofs = 4;
-
-               msg_len = RIVAL(state->auth_rep_blob.data, 0);
-
-               if (msg_len > 0x00FFFFFF) {
-                       errno = EMSGSIZE;
-                       return -1;
-               }
-
-               if (msg_len == 0) {
-                       errno = EMSGSIZE;
-                       return -1;
-               }
-
-               msg_len += ofs;
-
-               state->auth_rep_blob.data = talloc_realloc(state,
-                                               state->auth_rep_blob.data,
-                                               uint8_t, msg_len);
-               if (!state->auth_rep_blob.data) {
-                       return -1;
-               }
-               state->auth_rep_blob.length = msg_len;
-       } else {
-               *_vector = NULL;
-               *_count = 0;
-               return 0;
-       }
-
-       /* we need to get a message header */
-       vector = talloc_array(mem_ctx, struct iovec, 1);
-       if (!vector) {
-               return -1;
-       }
-       vector[0].iov_base = (char *) (state->auth_rep_blob.data + ofs);
-       vector[0].iov_len = state->auth_rep_blob.length - ofs;
-       count = 1;
-
-       *_vector = vector;
-       *_count = count;
-       return 0;
-}
-
 static void tstream_npa_connect_readv_done(struct tevent_req *subreq)
 {
        struct tevent_req *req =
@@ -318,23 +243,23 @@ static void tstream_npa_connect_readv_done(struct tevent_req *subreq)
        struct tstream_npa_connect_state *state =
                tevent_req_data(req,
                struct tstream_npa_connect_state);
-       int ret;
-       int sys_errno;
+       DATA_BLOB in;
+       int err;
        enum ndr_err_code ndr_err;
 
-       ret = tstream_readv_pdu_recv(subreq, &sys_errno);
+       err = tstream_u32_read_recv(subreq, state, &in.data, &in.length);
        TALLOC_FREE(subreq);
-       if (ret == -1) {
-               tevent_req_error(req, sys_errno);
+       if (err != 0) {
+               tevent_req_error(req, err);
                return;
        }
 
-       DEBUG(10,("name_pipe_auth_rep(client)[%u]\n",
-                (uint32_t)state->auth_rep_blob.length));
-       dump_data(11, state->auth_rep_blob.data, state->auth_rep_blob.length);
+       DBG_DEBUG("name_pipe_auth_rep(client)[%zu]\n", in.length);
+       dump_data(11, in.data, in.length);
 
-       ndr_err = ndr_pull_struct_blob(
-               &state->auth_rep_blob, state,
+       ndr_err = ndr_pull_struct_blob_all(
+               &in,
+               state,
                &state->auth_rep,
                (ndr_pull_flags_fn_t)ndr_pull_named_pipe_auth_rep);
 
@@ -1086,11 +1011,6 @@ struct tstream_npa_accept_state {
        struct auth_session_info_transport *session_info;
 };
 
-static int tstream_npa_accept_next_vector(struct tstream_context *unix_stream,
-                                         void *private_data,
-                                         TALLOC_CTX *mem_ctx,
-                                         struct iovec **_vector,
-                                         size_t *_count);
 static void tstream_npa_accept_existing_reply(struct tevent_req *subreq);
 static void tstream_npa_accept_existing_done(struct tevent_req *subreq);
 
@@ -1126,13 +1046,7 @@ struct tevent_req *tstream_npa_accept_existing_send(TALLOC_CTX *mem_ctx,
        state->device_state = device_state;
        state->alloc_size = allocation_size;
 
-       /*
-        * The named pipe pdu's have the length as 8 byte (initial_read_size),
-        * named_pipe_full_request provides the pdu length then.
-        */
-       subreq = tstream_readv_pdu_send(state, ev, plain,
-                                       tstream_npa_accept_next_vector,
-                                       state);
+       subreq = tstream_u32_read_send(state, ev, 0x00FFFFFF, plain);
        if (tevent_req_nomem(subreq, req)) {
                goto post;
        }
@@ -1147,82 +1061,6 @@ post:
        return req;
 }
 
-static int tstream_npa_accept_next_vector(struct tstream_context *unix_stream,
-                                         void *private_data,
-                                         TALLOC_CTX *mem_ctx,
-                                         struct iovec **_vector,
-                                         size_t *_count)
-{
-       struct tstream_npa_accept_state *state =
-               talloc_get_type_abort(private_data,
-                                       struct tstream_npa_accept_state);
-       struct iovec *vector;
-       size_t count;
-       off_t ofs = 0;
-
-       if (state->npa_blob.length == 0) {
-               state->npa_blob = data_blob_talloc(state, NULL, 4);
-               if (!state->npa_blob.data) {
-                       return -1;
-               }
-       } else if (state->npa_blob.length == 4) {
-               uint32_t msg_len;
-
-               ofs = 4;
-
-               msg_len = RIVAL(state->npa_blob.data, 0);
-
-               if (msg_len > 0x00FFFFFF) {
-                       errno = EMSGSIZE;
-                       return -1;
-               }
-
-               if (msg_len == 0) {
-                       errno = EMSGSIZE;
-                       return -1;
-               }
-
-               msg_len += ofs;
-
-               state->npa_blob.data = talloc_realloc(state,
-                                                     state->npa_blob.data,
-                                                     uint8_t, msg_len);
-               if (!state->npa_blob.data) {
-                       return -1;
-               }
-               state->npa_blob.length = msg_len;
-       } else {
-               if (memcmp(&state->npa_blob.data[4],
-                          NAMED_PIPE_AUTH_MAGIC, 4) != 0) {
-                       DEBUG(0, ("Wrong protocol\n"));
-#if defined(EPROTONOSUPPORT)
-                       errno = EPROTONOSUPPORT;
-#elif defined(EPROTO)
-                       errno = EPROTO;
-#else
-                       errno = EINVAL;
-#endif
-                       return -1;
-               }
-               *_vector = NULL;
-               *_count = 0;
-               return 0;
-       }
-
-       /* we need to get a message header */
-       vector = talloc_array(mem_ctx, struct iovec, 1);
-       if (!vector) {
-               return -1;
-       }
-       vector[0].iov_base = (char *) (state->npa_blob.data + ofs);
-       vector[0].iov_len = state->npa_blob.length - ofs;
-       count = 1;
-
-       *_vector = vector;
-       *_count = count;
-       return 0;
-}
-
 static void tstream_npa_accept_existing_reply(struct tevent_req *subreq)
 {
        struct tevent_req *req =
@@ -1233,20 +1071,35 @@ static void tstream_npa_accept_existing_reply(struct tevent_req *subreq)
        struct named_pipe_auth_rep pipe_reply;
        struct named_pipe_auth_req_info4 i4;
        enum ndr_err_code ndr_err;
-       DATA_BLOB out;
-       int sys_errno;
+       DATA_BLOB in, out;
+       int err;
        int ret;
 
-       ret = tstream_readv_pdu_recv(subreq, &sys_errno);
-       TALLOC_FREE(subreq);
-       if (ret == -1) {
-               tevent_req_error(req, sys_errno);
+       err = tstream_u32_read_recv(subreq, state, &in.data, &in.length);
+       if (err != 0) {
+               tevent_req_error(req, err);
+               return;
+       }
+       if (in.length < 8) {
+               tevent_req_error(req, EMSGSIZE);
+               return;
+       }
+
+       if (memcmp(&in.data[4], NAMED_PIPE_AUTH_MAGIC, 4) != 0) {
+               DBG_ERR("Wrong protocol\n");
+#if defined(EPROTONOSUPPORT)
+               err = EPROTONOSUPPORT;
+#elif defined(EPROTO)
+               err = EPROTO;
+#else
+               err = EINVAL;
+#endif
+               tevent_req_error(req, err);
                return;
        }
 
-       DEBUG(10, ("Received packet of length %lu\n",
-                  (long)state->npa_blob.length));
-       dump_data(11, state->npa_blob.data, state->npa_blob.length);
+       DBG_DEBUG("Received packet of length %zu\n", in.length);
+       dump_data(11, in.data, in.length);
 
        ZERO_STRUCT(pipe_reply);
        pipe_reply.level = 0;
@@ -1263,8 +1116,10 @@ static void tstream_npa_accept_existing_reply(struct tevent_req *subreq)
 
        /* parse the passed credentials */
        ndr_err = ndr_pull_struct_blob_all(
-                       &state->npa_blob, pipe_request, pipe_request,
-                       (ndr_pull_flags_fn_t)ndr_pull_named_pipe_auth_req);
+               &in,
+               pipe_request,
+               pipe_request,
+               (ndr_pull_flags_fn_t)ndr_pull_named_pipe_auth_req);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                pipe_reply.status = ndr_map_error2ntstatus(ndr_err);
                DEBUG(2, ("Could not unmarshall named_pipe_auth_req: %s\n",