s4-dsdb Add ability to force a particular SID in the upgrade case
[idra/samba.git] / source3 / libsmb / clilist.c
index 280f876ba74f90d2d0ce670b633f14a1bd99adff..ac1079592000493b4eb416ba059688987d298f81 100644 (file)
 */
 
 #include "includes.h"
+#include "libsmb/libsmb.h"
+#include "../lib/util/tevent_ntstatus.h"
 #include "async_smb.h"
+#include "trans2.h"
 
 /****************************************************************************
  Calculate a safe next_entry_offset.
@@ -186,13 +189,15 @@ static size_t interpret_long_filename(TALLOC_CTX *ctx,
                                return pdata_end - base;
                        }
                        p += 2;
-                       {
-                               /* stupid NT bugs. grr */
-                               int flags = 0;
-                               if (p[1] == 0 && namelen > 1) flags |= STR_UNICODE;
-                               clistr_pull(base_ptr, finfo->short_name, p,
-                                           sizeof(finfo->short_name),
-                                           slen, flags);
+                       ret = clistr_pull_talloc(ctx,
+                                               base_ptr,
+                                               recv_flags2,
+                                               &finfo->short_name,
+                                               p,
+                                               slen,
+                                               STR_UNICODE);
+                       if (ret == (size_t)-1) {
+                               return pdata_end - base;
                        }
                        p += 24; /* short name? */
                        if (p + namelen < p || p + namelen > pdata_end) {
@@ -249,8 +254,8 @@ static bool interpret_short_filename(TALLOC_CTX *ctx,
        finfo->mtime_ts.tv_nsec = finfo->atime_ts.tv_nsec = 0;
        finfo->size = IVAL(p,26);
        ret = clistr_pull_talloc(ctx,
-                       cli->inbuf,
-                       SVAL(cli->inbuf, smb_flg2),
+                       NULL,
+                       0,
                        &finfo->name,
                        p+30,
                        12,
@@ -260,9 +265,10 @@ static bool interpret_short_filename(TALLOC_CTX *ctx,
        }
 
        if (finfo->name) {
-               strlcpy(finfo->short_name,
-                       finfo->name,
-                       sizeof(finfo->short_name));
+               finfo->short_name = talloc_strdup(ctx, finfo->name);
+               if (finfo->short_name == NULL) {
+                       return false;
+               }
        }
        return true;
 }
@@ -318,7 +324,7 @@ static struct tevent_req *cli_list_old_send(TALLOC_CTX *mem_ctx,
        bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), mask,
                                   strlen(mask)+1, NULL);
 
-       bytes = smb_bytes_push_bytes(bytes, 5, (uint8_t *)&zero, 2);
+       bytes = smb_bytes_push_bytes(bytes, 5, (const uint8_t *)&zero, 2);
        if (tevent_req_nomem(bytes, req)) {
                return tevent_req_post(req, ev);
        }
@@ -384,7 +390,7 @@ static void cli_list_old_done(struct tevent_req *subreq)
 
                dirlist_len = talloc_get_size(state->dirlist);
 
-               tmp = TALLOC_REALLOC_ARRAY(
+               tmp = talloc_realloc(
                        state, state->dirlist, uint8_t,
                        dirlist_len + received * DIR_STRUCT_SIZE);
                if (tevent_req_nomem(tmp, req)) {
@@ -449,7 +455,7 @@ static NTSTATUS cli_list_old_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
 
        num_received = talloc_array_length(state->dirlist) / DIR_STRUCT_SIZE;
 
-       finfo = TALLOC_ARRAY(mem_ctx, struct file_info, num_received);
+       finfo = talloc_array(mem_ctx, struct file_info, num_received);
        if (finfo == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -469,7 +475,7 @@ static NTSTATUS cli_list_old_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
 
 NTSTATUS cli_list_old(struct cli_state *cli, const char *mask,
                      uint16 attribute,
-                     void (*fn)(const char *, struct file_info *,
+                     NTSTATUS (*fn)(const char *, struct file_info *,
                                 const char *, void *), void *state)
 {
        TALLOC_CTX *frame = talloc_stackframe();
@@ -504,13 +510,13 @@ NTSTATUS cli_list_old(struct cli_state *cli, const char *mask,
        }
        num_finfo = talloc_array_length(finfo);
        for (i=0; i<num_finfo; i++) {
-               fn(cli->dfs_mountpoint, &finfo[i], mask, state);
+               status = fn(cli->dfs_mountpoint, &finfo[i], mask, state);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto fail;
+               }
        }
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -546,8 +552,7 @@ static struct tevent_req *cli_list_trans_send(TALLOC_CTX *mem_ctx,
 {
        struct tevent_req *req, *subreq;
        struct cli_list_trans_state *state;
-       size_t nlen, param_len;
-       char *p;
+       size_t param_len;
 
        req = tevent_req_create(mem_ctx, &state,
                                struct cli_list_trans_state);
@@ -567,10 +572,9 @@ static struct tevent_req *cli_list_trans_send(TALLOC_CTX *mem_ctx,
 
        state->max_matches = 1366; /* Match W2k */
 
-       state->setup[0] = TRANSACT2_FINDFIRST;
+       SSVAL(&state->setup[0], 0, TRANSACT2_FINDFIRST);
 
-       nlen = 2*(strlen(mask)+1);
-       state->param = TALLOC_ARRAY(state, uint8_t, 12+nlen+2);
+       state->param = talloc_array(state, uint8_t, 12);
        if (tevent_req_nomem(state->param, req)) {
                return tevent_req_post(req, ev);
        }
@@ -583,16 +587,19 @@ static struct tevent_req *cli_list_trans_send(TALLOC_CTX *mem_ctx,
        SSVAL(state->param, 6, state->info_level);
        SIVAL(state->param, 8, 0);
 
-       p = ((char *)state->param)+12;
-       p += clistr_push(state->cli, p, state->mask, nlen,
-                        STR_TERMINATE);
-       param_len = PTR_DIFF(p, state->param);
+       state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli),
+                                            state->mask, strlen(state->mask)+1,
+                                            NULL);
+       if (tevent_req_nomem(state->param, req)) {
+               return tevent_req_post(req, ev);
+       }
+       param_len = talloc_get_size(state->param);
 
        subreq = cli_trans_send(state, state->ev, state->cli,
                                SMBtrans2, NULL, -1, 0, 0,
                                state->setup, 1, 0,
                                state->param, param_len, 10,
-                               NULL, 0, cli->max_xmit);
+                               NULL, 0, CLI_BUFFER_SIZE);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -623,7 +630,7 @@ static void cli_list_trans_done(struct tevent_req *subreq)
        int i;
        DATA_BLOB last_name_raw;
        struct file_info *finfo = NULL;
-       size_t nlen, param_len;
+       size_t param_len;
 
        min_param = (state->first ? 6 : 4);
 
@@ -651,7 +658,7 @@ static void cli_list_trans_done(struct tevent_req *subreq)
 
        old_num_finfo = talloc_array_length(state->finfo);
 
-       tmp = TALLOC_REALLOC_ARRAY(state, state->finfo, struct file_info,
+       tmp = talloc_realloc(state, state->finfo, struct file_info,
                                   old_num_finfo + ff_searchcount);
        if (tevent_req_nomem(tmp, req)) {
                return;
@@ -708,7 +715,7 @@ static void cli_list_trans_done(struct tevent_req *subreq)
        /*
         * Shrink state->finfo to the real length we received
         */
-       tmp = TALLOC_REALLOC_ARRAY(state, state->finfo, struct file_info,
+       tmp = talloc_realloc(state, state->finfo, struct file_info,
                                   old_num_finfo + i);
        if (tevent_req_nomem(tmp, req)) {
                return;
@@ -729,12 +736,9 @@ static void cli_list_trans_done(struct tevent_req *subreq)
                return;
        }
 
-       state->setup[0] = TRANSACT2_FINDNEXT;
+       SSVAL(&state->setup[0], 0, TRANSACT2_FINDNEXT);
 
-       nlen = 2*(strlen(state->mask) + 1);
-
-       param = TALLOC_REALLOC_ARRAY(state, state->param, uint8_t,
-                                    12 + nlen + last_name_raw.length + 2);
+       param = talloc_realloc(state, state->param, uint8_t, 12);
        if (tevent_req_nomem(param, req)) {
                return;
        }
@@ -755,23 +759,31 @@ static void cli_list_trans_done(struct tevent_req *subreq)
         */
        SSVAL(param, 10, (FLAG_TRANS2_FIND_REQUIRE_RESUME
                          |FLAG_TRANS2_FIND_CLOSE_IF_END));
-       p = ((char *)param)+12;
        if (last_name_raw.length) {
-               memcpy(p, last_name_raw.data, last_name_raw.length);
-               p += last_name_raw.length;
+               state->param = trans2_bytes_push_bytes(state->param,
+                                                      last_name_raw.data,
+                                                      last_name_raw.length);
+               if (tevent_req_nomem(state->param, req)) {
+                       return;
+               }
                data_blob_free(&last_name_raw);
        } else {
-               p += clistr_push(state->cli, p, state->mask, nlen,
-                                STR_TERMINATE);
+               state->param = trans2_bytes_push_str(state->param,
+                                                    cli_ucs2(state->cli),
+                                                    state->mask,
+                                                    strlen(state->mask)+1,
+                                                    NULL);
+               if (tevent_req_nomem(state->param, req)) {
+                       return;
+               }
        }
-
-       param_len = PTR_DIFF(p, param);
+       param_len = talloc_get_size(state->param);
 
        subreq = cli_trans_send(state, state->ev, state->cli,
                                SMBtrans2, NULL, -1, 0, 0,
                                state->setup, 1, 0,
                                state->param, param_len, 10,
-                               NULL, 0, state->cli->max_xmit);
+                               NULL, 0, CLI_BUFFER_SIZE);
        if (tevent_req_nomem(subreq, req)) {
                return;
        }
@@ -795,7 +807,7 @@ static NTSTATUS cli_list_trans_recv(struct tevent_req *req,
 
 NTSTATUS cli_list_trans(struct cli_state *cli, const char *mask,
                        uint16_t attribute, int info_level,
-                       void (*fn)(const char *mnt, struct file_info *finfo,
+                       NTSTATUS (*fn)(const char *mnt, struct file_info *finfo,
                                   const char *mask, void *private_data),
                        void *private_data)
 {
@@ -830,13 +842,13 @@ NTSTATUS cli_list_trans(struct cli_state *cli, const char *mask,
        }
        num_finfo = talloc_array_length(finfo);
        for (i=0; i<num_finfo; i++) {
-               fn(cli->dfs_mountpoint, &finfo[i], mask, private_data);
+               status = fn(cli->dfs_mountpoint, &finfo[i], mask, private_data);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto fail;
+               }
        }
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -863,7 +875,7 @@ struct tevent_req *cli_list_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       if (cli->protocol <= PROTOCOL_LANMAN1) {
+       if (cli_state_protocol(cli) <= PROTOCOL_LANMAN1) {
                subreq = cli_list_old_send(state, ev, cli, mask, attribute);
                state->recv_fn = cli_list_old_recv;
        } else {
@@ -911,7 +923,7 @@ NTSTATUS cli_list_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
 }
 
 NTSTATUS cli_list(struct cli_state *cli, const char *mask, uint16 attribute,
-                 void (*fn)(const char *, struct file_info *, const char *,
+                 NTSTATUS (*fn)(const char *, struct file_info *, const char *,
                             void *), void *state)
 {
        TALLOC_CTX *frame = talloc_stackframe();
@@ -934,7 +946,7 @@ NTSTATUS cli_list(struct cli_state *cli, const char *mask, uint16 attribute,
                goto fail;
        }
 
-       info_level = (cli->capabilities & CAP_NT_SMBS)
+       info_level = (cli_state_capabilities(cli) & CAP_NT_SMBS)
                ? SMB_FIND_FILE_BOTH_DIRECTORY_INFO : SMB_FIND_INFO_STANDARD;
 
        req = cli_list_send(frame, ev, cli, mask, attribute, info_level);
@@ -952,12 +964,12 @@ NTSTATUS cli_list(struct cli_state *cli, const char *mask, uint16 attribute,
        }
 
        for (i=0; i<num_finfo; i++) {
-               fn(cli->dfs_mountpoint, &finfo[i], mask, state);
+               status = fn(cli->dfs_mountpoint, &finfo[i], mask, state);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto fail;
+               }
        }
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }