Split up async_req into a generic and a NTSTATUS specific part
[tprouty/samba.git] / source3 / libsmb / clifile.c
index 12b10ba0a0dff399348de5b79d196dc62b60635e..0703f04c5ff900e6b853d99e7421f55d7c8460e3 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);
@@ -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;
+       }
+
+       SIVAL(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,19 +913,75 @@ int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess
                                FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
 }
 
+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;
+       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
+        */
+       if (ucs2 && (buflen % 2 == 0)) {
+               buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
+               if (buf == NULL) {
+                       return NULL;
+               }
+               buf[buflen] = '\0';
+               buflen += 1;
+       }
+
+       if (!convert_string_allocate(talloc_tos(), CH_UNIX,
+                                    ucs2 ? CH_UTF16LE : CH_DOS,
+                                    str, str_len, &converted,
+                                    &converted_size, true)) {
+               return NULL;
+       }
+
+       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;
+}
+
 /****************************************************************************
  Open a file
  WARNING: if you open with O_WRONLY then getattrE won't work!
 ****************************************************************************/
 
-int cli_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
+struct async_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
+                               struct cli_state *cli,
+                               const char *fname, int flags, int share_mode)
 {
-       char *p;
-       unsigned openfn=0;
-       unsigned accessmode=0;
+       unsigned openfn = 0;
+       unsigned accessmode = 0;
+       uint8_t additional_flags = 0;
+       uint8_t *bytes;
+       uint16_t vwv[15];
+       struct async_req *result;
 
-       if (flags & O_CREAT)
+       if (flags & O_CREAT) {
                openfn |= (1<<4);
+       }
        if (!(flags & O_EXCL)) {
                if (flags & O_TRUNC)
                        openfn |= (1<<1);
@@ -819,74 +1007,167 @@ int cli_open(struct cli_state *cli, const char *fname, int flags, int share_mode
                accessmode = 0xFF;
        }
 
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0',smb_size);
-
-       cli_set_message(cli->outbuf,15,0, true);
-
-       SCVAL(cli->outbuf,smb_com,SMBopenX);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
-
-       SSVAL(cli->outbuf,smb_vwv0,0xFF);
-       SSVAL(cli->outbuf,smb_vwv2,0);  /* no additional info */
-       SSVAL(cli->outbuf,smb_vwv3,accessmode);
-       SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
-       SSVAL(cli->outbuf,smb_vwv5,0);
-       SSVAL(cli->outbuf,smb_vwv8,openfn);
+       SCVAL(vwv + 0, 0, 0xFF);
+       SCVAL(vwv + 0, 1, 0);
+       SSVAL(vwv + 1, 0, 0);
+       SSVAL(vwv + 2, 0, 0);  /* no additional info */
+       SSVAL(vwv + 3, 0, accessmode);
+       SSVAL(vwv + 4, 0, aSYSTEM | aHIDDEN);
+       SSVAL(vwv + 5, 0, 0);
+       SIVAL(vwv + 6, 0, 0);
+       SSVAL(vwv + 8, 0, openfn);
+       SIVAL(vwv + 9, 0, 0);
+       SIVAL(vwv + 11, 0, 0);
+       SIVAL(vwv + 13, 0, 0);
 
        if (cli->use_oplocks) {
                /* if using oplocks then ask for a batch oplock via
                    core and extended methods */
-               SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
-                       FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
-               SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
+               additional_flags =
+                       FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
+               SSVAL(vwv+2, 0, SVAL(vwv+2, 0) | 6);
        }
 
-       p = smb_buf(cli->outbuf);
-       p += clistr_push(cli, p, fname,
-                       cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
+       bytes = talloc_array(talloc_tos(), uint8_t, 0);
+       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
+                                  strlen(fname)+1, NULL);
+       if (bytes == NULL) {
+               return NULL;
+       }
 
-       cli_setup_bcc(cli, p);
+       result = cli_request_send(mem_ctx, ev, cli, SMBopenX, additional_flags,
+                                 15, vwv, 0, talloc_get_size(bytes), bytes);
+       TALLOC_FREE(bytes);
+       return result;
+}
 
-       cli_send_smb(cli);
-       if (!cli_receive_smb(cli)) {
-               return -1;
+NTSTATUS cli_open_recv(struct async_req *req, int *fnum)
+{
+       uint8_t wct;
+       uint16_t *vwv;
+       uint16_t num_bytes;
+       uint8_t *bytes;
+       NTSTATUS status;
+
+       if (async_req_is_nterror(req, &status)) {
+               return status;
        }
 
-       if (cli_is_error(cli)) {
-               return -1;
+       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;
+       }
+
+       *fnum = SVAL(vwv+2, 0);
+
+       return NT_STATUS_OK;
+}
+
+int cli_open(struct cli_state *cli, const char *fname, int flags,
+            int share_mode)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev;
+       struct async_req *req;
+       int result = -1;
+
+       if (cli->fd_event != NULL) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               cli_set_error(cli, NT_STATUS_INVALID_PARAMETER);
+               goto fail;
+       }
+
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               goto fail;
        }
 
-       return SVAL(cli->inbuf,smb_vwv2);
+       req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
+       if (req == NULL) {
+               goto fail;
+       }
+
+       while (req->state < ASYNC_REQ_DONE) {
+               event_loop_once(ev);
+       }
+
+       cli_open_recv(req, &result);
+ fail:
+       TALLOC_FREE(frame);
+       return result;
 }
 
 /****************************************************************************
  Close a file.
 ****************************************************************************/
 
-bool cli_close(struct cli_state *cli, int fnum)
+struct async_req *cli_close_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
+                                struct cli_state *cli, int fnum)
 {
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0',smb_size);
+       uint16_t vwv[3];
 
-       cli_set_message(cli->outbuf,3,0,True);
+       SSVAL(vwv+0, 0, fnum);
+       SIVALS(vwv+1, 0, -1);
 
-       SCVAL(cli->outbuf,smb_com,SMBclose);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
+       return cli_request_send(mem_ctx, ev, cli, SMBclose, 0, 3, vwv, 0,
+                               0, NULL);
+}
 
-       SSVAL(cli->outbuf,smb_vwv0,fnum);
-       SIVALS(cli->outbuf,smb_vwv1,-1);
+NTSTATUS cli_close_recv(struct async_req *req)
+{
+       uint8_t wct;
+       uint16_t *vwv;
+       uint16_t num_bytes;
+       uint8_t *bytes;
+       NTSTATUS status;
 
-       cli_send_smb(cli);
-       if (!cli_receive_smb(cli)) {
-               return False;
+       if (async_req_is_nterror(req, &status)) {
+               return status;
        }
 
-       return !cli_is_error(cli);
+       return cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
 }
 
+bool cli_close(struct cli_state *cli, int fnum)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev;
+       struct async_req *req;
+       bool result = false;
+
+       if (cli->fd_event != NULL) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               cli_set_error(cli, NT_STATUS_INVALID_PARAMETER);
+               goto fail;
+       }
+
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               goto fail;
+       }
+
+       req = cli_close_send(frame, ev, cli, fnum);
+       if (req == NULL) {
+               goto fail;
+       }
+
+       while (req->state < ASYNC_REQ_DONE) {
+               event_loop_once(ev);
+       }
+
+       result = NT_STATUS_IS_OK(cli_close_recv(req));
+ fail:
+       TALLOC_FREE(frame);
+       return result;
+}
 
 /****************************************************************************
  Truncate a file to a specified size
@@ -1093,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;
@@ -1153,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;
 
@@ -1200,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;
@@ -1271,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) {
@@ -1284,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);
 }
@@ -1293,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;
 }
@@ -1605,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;
        }
@@ -1751,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,