s3: Add cli_shadow_copy_data
[obnox/samba-ctdb.git] / source3 / libsmb / clifile.c
index b3032a08eb078e0d160f4ab88b45e109a381f389..83faf940c1c04c86fb4220158320d14c868abf21 100644 (file)
@@ -66,7 +66,7 @@ static bool cli_link_internal(struct cli_state *cli, const char *oldname, const
                        -1, 0,                          /* fid, flags */
                        &setup, 1, 0,                   /* setup, length, max */
                        param, param_len, 2,            /* param, length, max */
-                       (char *)&data,  data_len, cli->max_xmit /* data, length, max */
+                       data,  data_len, cli->max_xmit /* data, length, max */
                        )) {
                SAFE_FREE(data);
                SAFE_FREE(param);
@@ -294,31 +294,31 @@ bool cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbu
                return false;
        }
 
-       sbuf->st_size = IVAL2_TO_SMB_BIG_UINT(rdata,0);     /* total size, in bytes */
-       sbuf->st_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8);   /* number of blocks allocated */
+       sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(rdata,0);     /* total size, in bytes */
+       sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8);   /* number of blocks allocated */
 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
-       sbuf->st_blocks /= STAT_ST_BLOCKSIZE;
+       sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
 #else
        /* assume 512 byte blocks */
-       sbuf->st_blocks /= 512;
+       sbuf->st_ex_blocks /= 512;
 #endif
-       set_ctimespec(sbuf, interpret_long_date(rdata + 16));    /* time of last change */
-       set_atimespec(sbuf, interpret_long_date(rdata + 24));    /* time of last access */
-       set_mtimespec(sbuf, interpret_long_date(rdata + 32));    /* time of last modification */
+       sbuf->st_ex_ctime = interpret_long_date(rdata + 16);    /* time of last change */
+       sbuf->st_ex_atime = interpret_long_date(rdata + 24);    /* time of last access */
+       sbuf->st_ex_mtime = interpret_long_date(rdata + 32);    /* time of last modification */
 
-       sbuf->st_uid = (uid_t) IVAL(rdata,40);      /* user ID of owner */
-       sbuf->st_gid = (gid_t) IVAL(rdata,48);      /* group ID of owner */
-       sbuf->st_mode |= unix_filetype_from_wire(IVAL(rdata, 56));
+       sbuf->st_ex_uid = (uid_t) IVAL(rdata,40);      /* user ID of owner */
+       sbuf->st_ex_gid = (gid_t) IVAL(rdata,48);      /* group ID of owner */
+       sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(rdata, 56));
 #if defined(HAVE_MAKEDEV)
        {
-               uint32 dev_major = IVAL(rdata,60);
-               uint32 dev_minor = IVAL(rdata,68);
-               sbuf->st_rdev = makedev(dev_major, dev_minor);
+               uint32_t dev_major = IVAL(rdata,60);
+               uint32_t dev_minor = IVAL(rdata,68);
+               sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
        }
 #endif
-       sbuf->st_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76);      /* inode */
-       sbuf->st_mode |= wire_perms_to_unix(IVAL(rdata,84));     /* protection */
-       sbuf->st_nlink = IVAL(rdata,92);    /* number of hard links */
+       sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76);      /* inode */
+       sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(rdata,84));     /* protection */
+       sbuf->st_ex_nlink = IVAL(rdata,92);    /* number of hard links */
 
        SAFE_FREE(rdata);
        SAFE_FREE(rparam);
@@ -771,6 +771,138 @@ int cli_nt_create_full(struct cli_state *cli, const char *fname,
        return SVAL(cli->inbuf,smb_vwv2 + 1);
 }
 
+struct async_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
+                                   struct event_context *ev,
+                                   struct cli_state *cli,
+                                   const char *fname,
+                                   uint32_t CreatFlags,
+                                   uint32_t DesiredAccess,
+                                   uint32_t FileAttributes,
+                                   uint32_t ShareAccess,
+                                   uint32_t CreateDisposition,
+                                   uint32_t CreateOptions,
+                                   uint8_t SecurityFlags)
+{
+       struct async_req *result;
+       uint8_t *bytes;
+       size_t converted_len;
+       uint16_t vwv[24];
+
+       SCVAL(vwv+0, 0, 0xFF);
+       SCVAL(vwv+0, 1, 0);
+       SSVAL(vwv+1, 0, 0);
+       SCVAL(vwv+2, 0, 0);
+
+       if (cli->use_oplocks) {
+               CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
+       }
+       SIVAL(vwv+3, 1, CreatFlags);
+       SIVAL(vwv+5, 1, 0x0);   /* RootDirectoryFid */
+       SIVAL(vwv+7, 1, DesiredAccess);
+       SIVAL(vwv+9, 1, 0x0);   /* AllocationSize */
+       SIVAL(vwv+11, 1, 0x0);  /* AllocationSize */
+       SIVAL(vwv+13, 1, FileAttributes);
+       SIVAL(vwv+15, 1, ShareAccess);
+       SIVAL(vwv+17, 1, CreateDisposition);
+       SIVAL(vwv+19, 1, CreateOptions);
+       SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
+       SCVAL(vwv+23, 1, SecurityFlags);
+
+       bytes = talloc_array(talloc_tos(), uint8_t, 0);
+       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
+                                  fname, strlen(fname)+1,
+                                  &converted_len);
+
+       /* sigh. this copes with broken netapp filer behaviour */
+       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
+
+       if (bytes == NULL) {
+               return NULL;
+       }
+
+       SSVAL(vwv+2, 1, converted_len);
+
+       result = cli_request_send(mem_ctx, ev, cli, SMBntcreateX, 0,
+                                 24, vwv, 0, talloc_get_size(bytes), bytes);
+       TALLOC_FREE(bytes);
+       return result;
+}
+
+NTSTATUS cli_ntcreate_recv(struct async_req *req, uint16_t *pfnum)
+{
+       uint8_t wct;
+       uint16_t *vwv;
+       uint16_t num_bytes;
+       uint8_t *bytes;
+       NTSTATUS status;
+
+       if (async_req_is_nterror(req, &status)) {
+               return status;
+       }
+
+       status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       if (wct < 3) {
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+
+       *pfnum = SVAL(vwv+2, 1);
+
+       return NT_STATUS_OK;
+}
+
+NTSTATUS cli_ntcreate(struct cli_state *cli,
+                     const char *fname,
+                     uint32_t CreatFlags,
+                     uint32_t DesiredAccess,
+                     uint32_t FileAttributes,
+                     uint32_t ShareAccess,
+                     uint32_t CreateDisposition,
+                     uint32_t CreateOptions,
+                     uint8_t SecurityFlags,
+                     uint16_t *pfid)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev;
+       struct async_req *req;
+       NTSTATUS status;
+
+       if (cli->fd_event != NULL) {
+               /*
+                * 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) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
+                               DesiredAccess, FileAttributes, ShareAccess,
+                               CreateDisposition, CreateOptions,
+                               SecurityFlags);
+       if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       while (req->state < ASYNC_REQ_DONE) {
+               event_loop_once(ev);
+       }
+
+       status = cli_ntcreate_recv(req, pfid);
+ fail:
+       TALLOC_FREE(frame);
+       return status;
+}
+
 /****************************************************************************
  Open a file.
 ****************************************************************************/
@@ -781,12 +913,19 @@ int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess
                                FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
 }
 
-static uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2, const char *str)
+uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
+                           const char *str, size_t str_len,
+                           size_t *pconverted_size)
 {
-       size_t buflen = talloc_get_size(buf);
+       size_t buflen;
        char *converted;
        size_t converted_size;
 
+       if (buf == NULL) {
+               return NULL;
+       }
+
+       buflen = talloc_get_size(buf);
        /*
         * We're pushing into an SMB buffer, align odd
         */
@@ -801,7 +940,7 @@ static uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2, const char *str)
 
        if (!convert_string_allocate(talloc_tos(), CH_UNIX,
                                     ucs2 ? CH_UTF16LE : CH_DOS,
-                                    str, strlen(str)+1, &converted,
+                                    str, str_len, &converted,
                                     &converted_size, true)) {
                return NULL;
        }
@@ -809,12 +948,18 @@ static uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2, const char *str)
        buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
                                   buflen + converted_size);
        if (buf == NULL) {
+               TALLOC_FREE(converted);
                return NULL;
        }
 
        memcpy(buf + buflen, converted, converted_size);
 
        TALLOC_FREE(converted);
+
+       if (pconverted_size) {
+               *pconverted_size = converted_size;
+       }
+
        return buf;
 }
 
@@ -884,18 +1029,14 @@ struct async_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
        }
 
        bytes = talloc_array(talloc_tos(), uint8_t, 0);
-       if (bytes == NULL) {
-               return NULL;
-       }
-
-       bytes = smb_bytes_push_str(
-               bytes, (cli->capabilities & CAP_UNICODE) != 0, fname);
+       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
+                                  strlen(fname)+1, NULL);
        if (bytes == NULL) {
                return NULL;
        }
 
        result = cli_request_send(mem_ctx, ev, cli, SMBopenX, additional_flags,
-                                 15, vwv, talloc_get_size(bytes), bytes);
+                                 15, vwv, 0, talloc_get_size(bytes), bytes);
        TALLOC_FREE(bytes);
        return result;
 }
@@ -908,9 +1049,8 @@ NTSTATUS cli_open_recv(struct async_req *req, int *fnum)
        uint8_t *bytes;
        NTSTATUS status;
 
-       SMB_ASSERT(req->state >= ASYNC_REQ_DONE);
-       if (req->state == ASYNC_REQ_ERROR) {
-               return req->status;
+       if (async_req_is_nterror(req, &status)) {
+               return status;
        }
 
        status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
@@ -975,7 +1115,7 @@ struct async_req *cli_close_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
        SSVAL(vwv+0, 0, fnum);
        SIVALS(vwv+1, 0, -1);
 
-       return cli_request_send(mem_ctx, ev, cli, SMBclose, 0, 3, vwv,
+       return cli_request_send(mem_ctx, ev, cli, SMBclose, 0, 3, vwv, 0,
                                0, NULL);
 }
 
@@ -985,10 +1125,10 @@ NTSTATUS cli_close_recv(struct async_req *req)
        uint16_t *vwv;
        uint16_t num_bytes;
        uint8_t *bytes;
+       NTSTATUS status;
 
-       SMB_ASSERT(req->state >= ASYNC_REQ_DONE);
-       if (req->state == ASYNC_REQ_ERROR) {
-               return req->status;
+       if (async_req_is_nterror(req, &status)) {
+               return status;
        }
 
        return cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
@@ -1234,7 +1374,7 @@ bool cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
 ****************************************************************************/
 
 bool cli_lock64(struct cli_state *cli, int fnum,
-               SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
+               uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
 {
        char *p;
         int saved_timeout = cli->timeout;
@@ -1294,7 +1434,7 @@ bool cli_lock64(struct cli_state *cli, int fnum,
  Unlock a file with 64 bit offsets.
 ****************************************************************************/
 
-bool cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
+bool cli_unlock64(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
 {
        char *p;
 
@@ -1341,7 +1481,7 @@ bool cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_
 ****************************************************************************/
 
 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
-               SMB_BIG_UINT offset, SMB_BIG_UINT len, bool wait_lock, enum brl_type lock_type)
+               uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
 {
        unsigned int param_len = 4;
        unsigned int data_len = POSIX_LOCK_DATA_SIZE;
@@ -1412,7 +1552,7 @@ static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
 ****************************************************************************/
 
 bool cli_posix_lock(struct cli_state *cli, int fnum,
-                       SMB_BIG_UINT offset, SMB_BIG_UINT len,
+                       uint64_t offset, uint64_t len,
                        bool wait_lock, enum brl_type lock_type)
 {
        if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
@@ -1425,7 +1565,7 @@ bool cli_posix_lock(struct cli_state *cli, int fnum,
  POSIX Unlock a file.
 ****************************************************************************/
 
-bool cli_posix_unlock(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
+bool cli_posix_unlock(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
 {
        return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
 }
@@ -1434,7 +1574,7 @@ bool cli_posix_unlock(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_
  POSIX Get any lock covering a file.
 ****************************************************************************/
 
-bool cli_posix_getlock(struct cli_state *cli, int fnum, SMB_BIG_UINT *poffset, SMB_BIG_UINT *plen)
+bool cli_posix_getlock(struct cli_state *cli, int fnum, uint64_t *poffset, uint64_t *plen)
 {
        return True;
 }
@@ -1746,7 +1886,7 @@ int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
                if (!path2) {
                        return -1;
                }
-               clistr_pull(cli, path2, p,
+               clistr_pull(cli->inbuf, path2, p,
                            len+1, len, STR_ASCII);
                *tmp_path = path2;
        }
@@ -1892,7 +2032,7 @@ bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const
 }
 
 /*********************************************************
- Get an extended attribute list tility fn.
+ Get an extended attribute list utility fn.
 *********************************************************/
 
 static bool cli_get_ea_list(struct cli_state *cli,
@@ -2279,3 +2419,159 @@ int cli_posix_rmdir(struct cli_state *cli, const char *fname)
 {
        return cli_posix_unlink_internal(cli, fname, True);
 }
+
+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 async_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;
+       struct async_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);
+       }
+       subreq->async.fn = cli_shadow_copy_data_done;
+       subreq->async.priv = req;
+       return req;
+}
+
+static void cli_shadow_copy_data_done(struct async_req *subreq)
+{
+       struct tevent_req *req = talloc_get_type_abort(
+               subreq->async.priv, 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, /* setup */
+                               NULL, NULL, /* param */
+                               &state->data, &state->num_data);
+       TALLOC_FREE(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return;
+       }
+       if (state->num_data < 12) {
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+               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->fd_event != NULL) {
+               /*
+                * 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(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               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;
+}