s3: Add cli_shadow_copy_data
authorVolker Lendecke <vl@samba.org>
Mon, 29 Nov 2010 16:39:43 +0000 (17:39 +0100)
committerVolker Lendecke <vl@samba.org>
Tue, 30 Nov 2010 10:36:53 +0000 (11:36 +0100)
source3/include/proto.h
source3/libsmb/clifile.c

index 9f00e6da5c8134c66ec7b5aa08eac7892e6f0429..85a7294d1a6fa32bd0b1eefaeb2e6a4243cbbf9a 100644 (file)
@@ -2381,6 +2381,17 @@ struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
 NTSTATUS cli_flush_recv(struct tevent_req *req);
 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum);
 
+struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
+                                            struct tevent_context *ev,
+                                            struct cli_state *cli,
+                                            uint16_t fnum,
+                                            bool get_names);
+NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+                                  char ***pnames, int *pnum_names);
+NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
+                             uint16_t fnum, bool get_names,
+                             char ***pnames, int *pnum_names);
+
 /* The following definitions come from libsmb/clirap2.c  */
 struct rap_group_info_1;
 struct rap_user_info_1;
index b40f7d15d99ab1a70d806c17c07b3f1148d1c28b..331777b7255ffc24e782e1002ea5c0d16220809e 100644 (file)
@@ -5338,3 +5338,152 @@ NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
        }
        return status;
 }
+
+struct cli_shadow_copy_data_state {
+       uint16_t setup[4];
+       uint8_t *data;
+       uint32_t num_data;
+       bool get_names;
+};
+
+static void cli_shadow_copy_data_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
+                                            struct tevent_context *ev,
+                                            struct cli_state *cli,
+                                            uint16_t fnum,
+                                            bool get_names)
+{
+       struct tevent_req *req, *subreq;
+       struct cli_shadow_copy_data_state *state;
+       uint32_t ret_size;
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct cli_shadow_copy_data_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       state->get_names = get_names;
+       ret_size = get_names ? cli->max_xmit : 16;
+
+       SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
+       SSVAL(state->setup + 2, 0, fnum);
+       SCVAL(state->setup + 3, 0, 0); /* isFsctl */
+       SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
+
+       subreq = cli_trans_send(
+               state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
+               state->setup, ARRAY_SIZE(state->setup), 0,
+               NULL, 0, 0,
+               NULL, 0, ret_size);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
+       return req;
+}
+
+static void cli_shadow_copy_data_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_shadow_copy_data_state *state = tevent_req_data(
+               req, struct cli_shadow_copy_data_state);
+       NTSTATUS status;
+
+       status = cli_trans_recv(subreq, state, NULL,
+                               NULL, 0, NULL, /* setup */
+                               NULL, 0, NULL, /* param */
+                               &state->data, 12, &state->num_data);
+       TALLOC_FREE(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return;
+       }
+       tevent_req_done(req);
+}
+
+NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
+                                  char ***pnames, int *pnum_names)
+{
+       struct cli_shadow_copy_data_state *state = tevent_req_data(
+               req, struct cli_shadow_copy_data_state);
+       char **names;
+       int i, num_names;
+       uint32_t dlength;
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       num_names = IVAL(state->data, 4);
+       dlength = IVAL(state->data, 8);
+
+       if (!state->get_names) {
+               *pnum_names = num_names;
+               return NT_STATUS_OK;
+       }
+
+       if (dlength+12 > state->num_data) {
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+       names = talloc_array(mem_ctx, char *, num_names);
+       if (names == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       for (i=0; i<num_names; i++) {
+               bool ret;
+               uint8_t *src;
+               size_t converted_size;
+
+               src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
+               ret = convert_string_talloc(
+                       names, CH_UTF16LE, CH_UNIX,
+                       src, 2 * sizeof(SHADOW_COPY_LABEL),
+                       &names[i], &converted_size, True);
+               if (!ret) {
+                       TALLOC_FREE(names);
+                       return NT_STATUS_INVALID_NETWORK_RESPONSE;
+               }
+       }
+       *pnum_names = num_names;
+       *pnames = names;
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
+                             uint16_t fnum, bool get_names,
+                             char ***pnames, int *pnum_names)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_NO_MEMORY;
+
+       if (cli_has_async_calls(cli)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               goto fail;
+       }
+       req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
+       if (req == NULL) {
+               goto fail;
+       }
+       if (!tevent_req_poll_ntstatus(req, ev, &status)) {
+               goto fail;
+       }
+       status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
+}