lib: remove unused function nttime_from_string()
[bbaumbach/samba-autobuild/.git] / source3 / libsmb / clirap.c
index c6b8cfb1cc0a49e64de7236de2c576bf74ee59d2..e80dfc92a77b0bd2dd6c614e6f05d2f24dda8e1e 100644 (file)
 #include "../libcli/auth/libcli_auth.h"
 #include "../librpc/gen_ndr/rap.h"
 #include "../lib/crypto/arcfour.h"
+#include "../lib/util/tevent_ntstatus.h"
 #include "async_smb.h"
+#include "libsmb/libsmb.h"
+#include "libsmb/clirap.h"
+#include "trans2.h"
+#include "../libcli/smb/smbXcli_base.h"
+#include "cli_smb2_fnum.h"
+
+#define PIPE_LANMAN   "\\PIPE\\LANMAN"
 
 /****************************************************************************
  Call a remote api
@@ -35,17 +43,54 @@ bool cli_api(struct cli_state *cli,
             char **rparam, unsigned int *rprcnt,
             char **rdata, unsigned int *rdrcnt)
 {
-       cli_send_trans(cli,SMBtrans,
-                 PIPE_LANMAN,             /* Name */
-                 0,0,                     /* fid, flags */
-                 NULL,0,0,                /* Setup, length, max */
-                 param, prcnt, mprcnt,    /* Params, length, max */
-                 data, drcnt, mdrcnt      /* Data, length, max */
-                );
-
-       return (cli_receive_trans(cli,SMBtrans,
-                            rparam, rprcnt,
-                            rdata, rdrcnt));
+       NTSTATUS status;
+
+       uint8_t *my_rparam, *my_rdata;
+       uint32_t num_my_rparam, num_my_rdata;
+
+       status = cli_trans(talloc_tos(), cli, SMBtrans,
+                          PIPE_LANMAN, 0, /* name, fid */
+                          0, 0,           /* function, flags */
+                          NULL, 0, 0,     /* setup */
+                          (uint8_t *)param, prcnt, mprcnt, /* Params, length, max */
+                          (uint8_t *)data, drcnt, mdrcnt,  /* Data, length, max */
+                          NULL,                 /* recv_flags2 */
+                          NULL, 0, NULL,        /* rsetup */
+                          &my_rparam, 0, &num_my_rparam,
+                          &my_rdata, 0, &num_my_rdata);
+       if (!NT_STATUS_IS_OK(status)) {
+               return false;
+       }
+
+       /*
+        * I know this memcpy massively hurts, but there are just tons
+        * of callers of cli_api that eventually need changing to
+        * talloc
+        */
+
+       *rparam = (char *)smb_memdup(my_rparam, num_my_rparam);
+       if (*rparam == NULL) {
+               goto fail;
+       }
+       *rprcnt = num_my_rparam;
+       TALLOC_FREE(my_rparam);
+
+       *rdata = (char *)smb_memdup(my_rdata, num_my_rdata);
+       if (*rdata == NULL) {
+               goto fail;
+       }
+       *rdrcnt = num_my_rdata;
+       TALLOC_FREE(my_rdata);
+
+       return true;
+fail:
+       TALLOC_FREE(my_rdata);
+       TALLOC_FREE(my_rparam);
+       *rparam = NULL;
+       *rprcnt = 0;
+       *rdata = NULL;
+       *rdrcnt = 0;
+       return false;
 }
 
 /****************************************************************************
@@ -73,13 +118,17 @@ bool cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
        SSVAL(p,0,1);
        p += 2;
        strlcpy(p,user,sizeof(param)-PTR_DIFF(p,param));
-       strupper_m(p);
+       if (!strupper_m(p)) {
+               return false;
+       }
        p += 21;
        p++;
        p += 15;
        p++;
        strlcpy(p, workstation,sizeof(param)-PTR_DIFF(p,param));
-       strupper_m(p);
+       if (!strupper_m(p)) {
+               return false;
+       }
        p += 16;
        SSVAL(p, 0, CLI_BUFFER_SIZE);
        p += 2;
@@ -97,7 +146,10 @@ bool cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
 
                if (cli->rap_error == 0) {
                        DEBUG(4,("NetWkstaUserLogon success\n"));
-                       cli->privileges = SVAL(p, 24);
+                       /*
+                        * The cli->privileges = SVAL(p, 24); field was set here
+                        * but it was not use anywhere else.
+                        */
                        /* The cli->eff_name field used to be set here
                           but it wasn't used anywhere else. */
                } else {
@@ -114,7 +166,7 @@ bool cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
  Call a NetShareEnum - try and browse available connections on a host.
 ****************************************************************************/
 
-int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, const char *, void *), void *state)
+int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32_t, const char *, void *), void *state)
 {
        char *rparam = NULL;
        char *rdata = NULL;
@@ -221,8 +273,8 @@ int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, co
  the comment and a state pointer.
 ****************************************************************************/
 
-bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
-                      void (*fn)(const char *, uint32, const char *, void *),
+bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32_t stype,
+                      void (*fn)(const char *, uint32_t, const char *, void *),
                       void *state)
 {
        char *rparam = NULL;
@@ -233,7 +285,7 @@ bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
        char param[1024];
        int uLevel = 1;
        size_t len;
-       uint32 func = RAP_NetServerEnum2;
+       uint32_t func = RAP_NetServerEnum2;
        char *last_entry = NULL;
        int total_cnt = 0;
        int return_cnt = 0;
@@ -276,7 +328,7 @@ bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
                                sizeof(param) - PTR_DIFF(p,param) - 1,
                                STR_TERMINATE|STR_UPPER);
 
-               if (len == (size_t)-1) {
+               if (len == 0) {
                        SAFE_FREE(last_entry);
                        return false;
                }
@@ -288,7 +340,7 @@ bool cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
                                        sizeof(param) - PTR_DIFF(p,param) - 1,
                                        STR_TERMINATE);
 
-                       if (len == (size_t)-1) {
+                       if (len == 0) {
                                SAFE_FREE(last_entry);
                                return false;
                        }
@@ -531,7 +583,7 @@ struct cli_qpathinfo1_state {
 static void cli_qpathinfo1_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_qpathinfo1_send(TALLOC_CTX *mem_ctx,
-                                      struct event_context *ev,
+                                      struct tevent_context *ev,
                                       struct cli_state *cli,
                                       const char *fname)
 {
@@ -544,7 +596,7 @@ struct tevent_req *cli_qpathinfo1_send(TALLOC_CTX *mem_ctx,
        }
        state->cli = cli;
        subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_INFO_STANDARD,
-                                   22, cli->max_xmit);
+                                   22, CLI_BUFFER_SIZE);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -574,8 +626,8 @@ NTSTATUS cli_qpathinfo1_recv(struct tevent_req *req,
                             time_t *change_time,
                             time_t *access_time,
                             time_t *write_time,
-                            SMB_OFF_T *size,
-                            uint16 *mode)
+                            off_t *size,
+                            uint16_t *mode)
 {
        struct cli_qpathinfo1_state *state = tevent_req_data(
                req, struct cli_qpathinfo1_state);
@@ -594,13 +646,13 @@ NTSTATUS cli_qpathinfo1_recv(struct tevent_req *req,
        }
 
        if (change_time) {
-               *change_time = date_fn(state->data+0, state->cli->serverzone);
+               *change_time = date_fn(state->data+0, smb1cli_conn_server_time_zone(state->cli->conn));
        }
        if (access_time) {
-               *access_time = date_fn(state->data+4, state->cli->serverzone);
+               *access_time = date_fn(state->data+4, smb1cli_conn_server_time_zone(state->cli->conn));
        }
        if (write_time) {
-               *write_time = date_fn(state->data+8, state->cli->serverzone);
+               *write_time = date_fn(state->data+8, smb1cli_conn_server_time_zone(state->cli->conn));
        }
        if (size) {
                *size = IVAL(state->data, 12);
@@ -616,22 +668,22 @@ NTSTATUS cli_qpathinfo1(struct cli_state *cli,
                        time_t *change_time,
                        time_t *access_time,
                        time_t *write_time,
-                       SMB_OFF_T *size,
-                       uint16 *mode)
+                       off_t *size,
+                       uint16_t *mode)
 {
        TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       struct tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_NO_MEMORY;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
                status = NT_STATUS_INVALID_PARAMETER;
                goto fail;
        }
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                goto fail;
        }
@@ -646,9 +698,6 @@ NTSTATUS cli_qpathinfo1(struct cli_state *cli,
                                     write_time, size, mode);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -656,44 +705,16 @@ NTSTATUS cli_qpathinfo1(struct cli_state *cli,
  Send a setpathinfo call.
 ****************************************************************************/
 
-bool cli_setpathinfo(struct cli_state *cli, const char *fname,
-                     time_t create_time,
-                     time_t access_time,
-                     time_t write_time,
-                     time_t change_time,
-                     uint16 mode)
+NTSTATUS cli_setpathinfo_basic(struct cli_state *cli, const char *fname,
+                              time_t create_time,
+                              time_t access_time,
+                              time_t write_time,
+                              time_t change_time,
+                              uint16_t mode)
 {
        unsigned int data_len = 0;
-       unsigned int param_len = 0;
-       unsigned int rparam_len, rdata_len;
-       uint16 setup = TRANSACT2_SETPATHINFO;
-       char *param;
        char data[40];
-       char *rparam=NULL, *rdata=NULL;
-       int count=8;
-       bool ret;
        char *p;
-       size_t nlen = 2*(strlen(fname)+1);
-
-       param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
-       if (!param) {
-               return false;
-       }
-       memset(param, '\0', 6);
-       memset(data, 0, sizeof(data));
-
-        p = param;
-
-        /* Add the information level */
-       SSVAL(p, 0, SMB_FILE_BASIC_INFORMATION);
-
-        /* Skip reserved */
-       p += 6;
-
-        /* Add the file name */
-       p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
-
-       param_len = PTR_DIFF(p, param);
 
         p = data;
 
@@ -712,8 +733,17 @@ bool cli_setpathinfo(struct cli_state *cli, const char *fname,
         put_long_date(p, change_time);
         p += 8;
 
-        /* Add attributes */
-        SIVAL(p, 0, mode);
+       if (mode == (uint16_t)-1 || mode == FILE_ATTRIBUTE_NORMAL) {
+               /* No change. */
+               mode = 0;
+       } else if (mode == 0) {
+               /* Clear all existing attributes. */
+               mode = FILE_ATTRIBUTE_NORMAL;
+       }
+
+       /* Add attributes */
+       SIVAL(p, 0, mode);
+
         p += 4;
 
         /* Add padding */
@@ -722,37 +752,21 @@ bool cli_setpathinfo(struct cli_state *cli, const char *fname,
 
         data_len = PTR_DIFF(p, data);
 
-       do {
-               ret = (cli_send_trans(cli, SMBtrans2,
-                                     NULL,           /* Name */
-                                     -1, 0,          /* fid, flags */
-                                     &setup, 1, 0,   /* setup, length, max */
-                                     param, param_len, 10, /* param, length, max */
-                                     data, data_len, cli->max_xmit /* data, length, max */
-                                     ) &&
-                      cli_receive_trans(cli, SMBtrans2,
-                                        &rparam, &rparam_len,
-                                        &rdata, &rdata_len));
-               if (!cli_is_dos_error(cli)) break;
-               if (!ret) {
-                       /* we need to work around a Win95 bug - sometimes
-                          it gives ERRSRV/ERRerror temprarily */
-                       uint8 eclass;
-                       uint32 ecode;
-                       cli_dos_error(cli, &eclass, &ecode);
-                       if (eclass != ERRSRV || ecode != ERRerror) break;
-                       smb_msleep(100);
-               }
-       } while (count-- && ret==False);
-
-       SAFE_FREE(param);
-       if (!ret) {
-               return False;
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               DATA_BLOB in_data = data_blob_const(data, data_len);
+               /*
+                * Split out SMB2 here as we need to select
+                * the correct info type and level.
+                */
+               return cli_smb2_setpathinfo(cli,
+                               fname,
+                               1, /* SMB2_SETINFO_FILE */
+                               SMB_FILE_BASIC_INFORMATION - 1000,
+                               &in_data);
        }
 
-       SAFE_FREE(rdata);
-       SAFE_FREE(rparam);
-       return True;
+       return cli_setpathinfo(cli, SMB_FILE_BASIC_INFORMATION, fname,
+                              (uint8_t *)data, data_len);
 }
 
 /****************************************************************************
@@ -767,7 +781,7 @@ struct cli_qpathinfo2_state {
 static void cli_qpathinfo2_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_qpathinfo2_send(TALLOC_CTX *mem_ctx,
-                                      struct event_context *ev,
+                                      struct tevent_context *ev,
                                       struct cli_state *cli,
                                       const char *fname)
 {
@@ -780,7 +794,7 @@ struct tevent_req *cli_qpathinfo2_send(TALLOC_CTX *mem_ctx,
        }
        subreq = cli_qpathinfo_send(state, ev, cli, fname,
                                    SMB_QUERY_FILE_ALL_INFO,
-                                   68, cli->max_xmit);
+                                   68, CLI_BUFFER_SIZE);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -811,7 +825,7 @@ NTSTATUS cli_qpathinfo2_recv(struct tevent_req *req,
                             struct timespec *access_time,
                             struct timespec *write_time,
                             struct timespec *change_time,
-                            SMB_OFF_T *size, uint16 *mode,
+                            off_t *size, uint16_t *mode,
                             SMB_INO_T *ino)
 {
        struct cli_qpathinfo2_state *state = tevent_req_data(
@@ -851,22 +865,36 @@ NTSTATUS cli_qpathinfo2(struct cli_state *cli, const char *fname,
                        struct timespec *access_time,
                        struct timespec *write_time,
                        struct timespec *change_time,
-                       SMB_OFF_T *size, uint16 *mode,
+                       off_t *size, uint16_t *mode,
                        SMB_INO_T *ino)
 {
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       TALLOC_CTX *frame = NULL;
+       struct tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_NO_MEMORY;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_qpathinfo2(cli,
+                                       fname,
+                                       create_time,
+                                       access_time,
+                                       write_time,
+                                       change_time,
+                                       size,
+                                       mode,
+                                       ino);
+       }
+
+       frame = talloc_stackframe();
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
                status = NT_STATUS_INVALID_PARAMETER;
                goto fail;
        }
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                goto fail;
        }
@@ -881,9 +909,6 @@ NTSTATUS cli_qpathinfo2(struct cli_state *cli, const char *fname,
                                     write_time, change_time, size, mode, ino);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -891,11 +916,6 @@ NTSTATUS cli_qpathinfo2(struct cli_state *cli, const char *fname,
  Get the stream info
 ****************************************************************************/
 
-static bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *data,
-                              size_t data_len,
-                              unsigned int *pnum_streams,
-                              struct stream_struct **pstreams);
-
 struct cli_qpathinfo_streams_state {
        uint32_t num_data;
        uint8_t *data;
@@ -918,7 +938,7 @@ struct tevent_req *cli_qpathinfo_streams_send(TALLOC_CTX *mem_ctx,
        }
        subreq = cli_qpathinfo_send(state, ev, cli, fname,
                                    SMB_FILE_STREAM_INFORMATION,
-                                   0, cli->max_xmit);
+                                   0, CLI_BUFFER_SIZE);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -968,19 +988,29 @@ NTSTATUS cli_qpathinfo_streams(struct cli_state *cli, const char *fname,
                               unsigned int *pnum_streams,
                               struct stream_struct **pstreams)
 {
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       TALLOC_CTX *frame = NULL;
+       struct tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_NO_MEMORY;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_qpathinfo_streams(cli,
+                                       fname,
+                                       mem_ctx,
+                                       pnum_streams,
+                                       pstreams);
+       }
+
+       frame = talloc_stackframe();
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
                status = NT_STATUS_INVALID_PARAMETER;
                goto fail;
        }
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                goto fail;
        }
@@ -995,13 +1025,10 @@ NTSTATUS cli_qpathinfo_streams(struct cli_state *cli, const char *fname,
                                            pstreams);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
-static bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *rdata,
+bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *rdata,
                               size_t data_len,
                               unsigned int *pnum_streams,
                               struct stream_struct **pstreams)
@@ -1021,7 +1048,7 @@ static bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *rdata,
                struct stream_struct *tmp;
                uint8_t *tmp_buf;
 
-               tmp = TALLOC_REALLOC_ARRAY(mem_ctx, streams,
+               tmp = talloc_realloc(mem_ctx, streams,
                                           struct stream_struct,
                                           num_streams+1);
 
@@ -1046,7 +1073,7 @@ static bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *rdata,
                 * convert_string_talloc??
                 */
 
-               tmp_buf = TALLOC_ARRAY(streams, uint8_t, nlen+2);
+               tmp_buf = talloc_array(streams, uint8_t, nlen+2);
                if (tmp_buf == NULL) {
                        goto fail;
                }
@@ -1056,7 +1083,7 @@ static bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *rdata,
                tmp_buf[nlen+1] = 0;
 
                if (!convert_string_talloc(streams, CH_UTF16, CH_UNIX, tmp_buf,
-                                          nlen+2, &vstr, &size, false))
+                                          nlen+2, &vstr, &size))
                {
                        TALLOC_FREE(tmp_buf);
                        goto fail;
@@ -1087,23 +1114,44 @@ static bool parse_streams_blob(TALLOC_CTX *mem_ctx, const uint8_t *rdata,
  Send a qfileinfo QUERY_FILE_NAME_INFO call.
 ****************************************************************************/
 
-NTSTATUS cli_qfilename(struct cli_state *cli, uint16_t fnum, char *name,
-                      size_t namelen)
+NTSTATUS cli_qfilename(struct cli_state *cli, uint16_t fnum,
+                      TALLOC_CTX *mem_ctx, char **_name)
 {
+       uint16_t recv_flags2;
        uint8_t *rdata;
        uint32_t num_rdata;
        NTSTATUS status;
+       char *name = NULL;
+       uint32_t namelen;
 
        status = cli_qfileinfo(talloc_tos(), cli, fnum,
                               SMB_QUERY_FILE_NAME_INFO,
-                              4, cli->max_xmit,
+                              4, CLI_BUFFER_SIZE, &recv_flags2,
                               &rdata, &num_rdata);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       clistr_pull(cli->inbuf, name, rdata+4, namelen, IVAL(rdata, 0),
-                   STR_UNICODE);
+       namelen = IVAL(rdata, 0);
+       if (namelen > (num_rdata - 4)) {
+               TALLOC_FREE(rdata);
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+
+       clistr_pull_talloc(mem_ctx,
+                          (const char *)rdata,
+                          recv_flags2,
+                          &name,
+                          rdata + 4,
+                          namelen,
+                          STR_UNICODE);
+       if (name == NULL) {
+               status = map_nt_error_from_unix(errno);
+               TALLOC_FREE(rdata);
+               return status;
+       }
+
+       *_name = name;
        TALLOC_FREE(rdata);
        return NT_STATUS_OK;
 }
@@ -1113,7 +1161,7 @@ NTSTATUS cli_qfilename(struct cli_state *cli, uint16_t fnum, char *name,
 ****************************************************************************/
 
 NTSTATUS cli_qfileinfo_basic(struct cli_state *cli, uint16_t fnum,
-                            uint16 *mode, SMB_OFF_T *size,
+                            uint16_t *mode, off_t *size,
                             struct timespec *create_time,
                             struct timespec *access_time,
                             struct timespec *write_time,
@@ -1124,6 +1172,18 @@ NTSTATUS cli_qfileinfo_basic(struct cli_state *cli, uint16_t fnum,
        uint32_t num_rdata;
        NTSTATUS status;
 
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_qfileinfo_basic(cli,
+                                               fnum,
+                                               mode,
+                                               size,
+                                               create_time,
+                                               access_time,
+                                               write_time,
+                                               change_time,
+                                               ino);
+       }
+
        /* if its a win95 server then fail this - win95 totally screws it
           up */
        if (cli->win95) {
@@ -1132,7 +1192,8 @@ NTSTATUS cli_qfileinfo_basic(struct cli_state *cli, uint16_t fnum,
 
        status = cli_qfileinfo(talloc_tos(), cli, fnum,
                               SMB_QUERY_FILE_ALL_INFO,
-                              68, MIN(cli->max_xmit, 0xffff),
+                              68, CLI_BUFFER_SIZE,
+                              NULL,
                               &rdata, &num_rdata);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -1176,7 +1237,7 @@ struct cli_qpathinfo_basic_state {
 static void cli_qpathinfo_basic_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_qpathinfo_basic_send(TALLOC_CTX *mem_ctx,
-                                           struct event_context *ev,
+                                           struct tevent_context *ev,
                                            struct cli_state *cli,
                                            const char *fname)
 {
@@ -1190,7 +1251,7 @@ struct tevent_req *cli_qpathinfo_basic_send(TALLOC_CTX *mem_ctx,
        }
        subreq = cli_qpathinfo_send(state, ev, cli, fname,
                                    SMB_QUERY_FILE_BASIC_INFO,
-                                   36, cli->max_xmit);
+                                   36, CLI_BUFFER_SIZE);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -1217,7 +1278,7 @@ static void cli_qpathinfo_basic_done(struct tevent_req *subreq)
 }
 
 NTSTATUS cli_qpathinfo_basic_recv(struct tevent_req *req,
-                                 SMB_STRUCT_STAT *sbuf, uint32 *attributes)
+                                 SMB_STRUCT_STAT *sbuf, uint32_t *attributes)
 {
        struct cli_qpathinfo_basic_state *state = tevent_req_data(
                req, struct cli_qpathinfo_basic_state);
@@ -1227,6 +1288,7 @@ NTSTATUS cli_qpathinfo_basic_recv(struct tevent_req *req,
                return status;
        }
 
+       sbuf->st_ex_btime = interpret_long_date((char *)state->data);
        sbuf->st_ex_atime = interpret_long_date((char *)state->data+8);
        sbuf->st_ex_mtime = interpret_long_date((char *)state->data+16);
        sbuf->st_ex_ctime = interpret_long_date((char *)state->data+24);
@@ -1235,21 +1297,30 @@ NTSTATUS cli_qpathinfo_basic_recv(struct tevent_req *req,
 }
 
 NTSTATUS cli_qpathinfo_basic(struct cli_state *cli, const char *name,
-                            SMB_STRUCT_STAT *sbuf, uint32 *attributes)
+                            SMB_STRUCT_STAT *sbuf, uint32_t *attributes)
 {
-       TALLOC_CTX *frame = talloc_stackframe();
-       struct event_context *ev;
+       TALLOC_CTX *frame = NULL;
+       struct tevent_context *ev;
        struct tevent_req *req;
        NTSTATUS status = NT_STATUS_NO_MEMORY;
 
-       if (cli_has_async_calls(cli)) {
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_qpathinfo_basic(cli,
+                                               name,
+                                               sbuf,
+                                               attributes);
+       }
+
+       frame = talloc_stackframe();
+
+       if (smbXcli_conn_has_async_calls(cli->conn)) {
                /*
                 * Can't use sync call while an async call is in flight
                 */
                status = NT_STATUS_INVALID_PARAMETER;
                goto fail;
        }
-       ev = event_context_init(frame);
+       ev = samba_tevent_context_init(frame);
        if (ev == NULL) {
                goto fail;
        }
@@ -1263,9 +1334,6 @@ NTSTATUS cli_qpathinfo_basic(struct cli_state *cli, const char *name,
        status = cli_qpathinfo_basic_recv(req, sbuf, attributes);
  fail:
        TALLOC_FREE(frame);
-       if (!NT_STATUS_IS_OK(status)) {
-               cli_set_error(cli, status);
-       }
        return status;
 }
 
@@ -1275,67 +1343,155 @@ NTSTATUS cli_qpathinfo_basic(struct cli_state *cli, const char *name,
 
 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
 {
-       unsigned int data_len = 0;
-       unsigned int param_len = 0;
-       uint16 setup = TRANSACT2_QPATHINFO;
-       char *param;
-       char *rparam=NULL, *rdata=NULL;
-       int count=8;
-       char *p;
-       bool ret;
+       uint8_t *rdata;
+       uint32_t num_rdata;
        unsigned int len;
-       size_t nlen = 2*(strlen(fname)+1);
+       char *converted = NULL;
+       size_t converted_size = 0;
+       NTSTATUS status;
+
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_smb2_qpathinfo_alt_name(cli,
+                                               fname,
+                                               alt_name);
+       }
+
+       status = cli_qpathinfo(talloc_tos(), cli, fname,
+                              SMB_QUERY_FILE_ALT_NAME_INFO,
+                              4, CLI_BUFFER_SIZE, &rdata, &num_rdata);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       len = IVAL(rdata, 0);
 
-       param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
-       if (!param) {
+       if (len > num_rdata - 4) {
+               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       }
+
+       /* The returned data is a pushed string, not raw data. */
+       if (!convert_string_talloc(talloc_tos(),
+                                  smbXcli_conn_use_unicode(cli->conn) ? CH_UTF16LE : CH_DOS,
+                                  CH_UNIX,
+                                  rdata + 4,
+                                  len,
+                                  &converted,
+                                  &converted_size)) {
                return NT_STATUS_NO_MEMORY;
        }
-       p = param;
-       memset(param, '\0', 6);
-       SSVAL(p, 0, SMB_QUERY_FILE_ALT_NAME_INFO);
-       p += 6;
-       p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
-       param_len = PTR_DIFF(p, param);
+       fstrcpy(alt_name, converted);
 
-       do {
-               ret = (cli_send_trans(cli, SMBtrans2,
-                                     NULL,           /* Name */
-                                     -1, 0,          /* fid, flags */
-                                     &setup, 1, 0,   /* setup, length, max */
-                                     param, param_len, 10, /* param, length, max */
-                                     NULL, data_len, cli->max_xmit /* data, length, max */
-                                     ) &&
-                      cli_receive_trans(cli, SMBtrans2,
-                                        &rparam, &param_len,
-                                        &rdata, &data_len));
-               if (!ret && cli_is_dos_error(cli)) {
-                       /* we need to work around a Win95 bug - sometimes
-                          it gives ERRSRV/ERRerror temprarily */
-                       uint8 eclass;
-                       uint32 ecode;
-                       cli_dos_error(cli, &eclass, &ecode);
-                       if (eclass != ERRSRV || ecode != ERRerror) break;
-                       smb_msleep(100);
-               }
-       } while (count-- && ret==False);
+       TALLOC_FREE(converted);
+       TALLOC_FREE(rdata);
 
-       SAFE_FREE(param);
+       return NT_STATUS_OK;
+}
 
-       if (!ret || !rdata || data_len < 4) {
-               return NT_STATUS_UNSUCCESSFUL;
+/****************************************************************************
+ Send a qpathinfo SMB_QUERY_FILE_STADNDARD_INFO call.
+****************************************************************************/
+
+NTSTATUS cli_qpathinfo_standard(struct cli_state *cli, const char *fname,
+                               uint64_t *allocated, uint64_t *size,
+                               uint32_t *nlinks,
+                               bool *is_del_pending, bool *is_dir)
+{
+       uint8_t *rdata;
+       uint32_t num_rdata;
+       NTSTATUS status;
+
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return NT_STATUS_NOT_IMPLEMENTED;
        }
 
-       len = IVAL(rdata, 0);
+       status = cli_qpathinfo(talloc_tos(), cli, fname,
+                              SMB_QUERY_FILE_STANDARD_INFO,
+                              24, CLI_BUFFER_SIZE, &rdata, &num_rdata);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
-       if (len > data_len - 4) {
-               return NT_STATUS_INVALID_NETWORK_RESPONSE;
+       if (allocated) {
+               *allocated = BVAL(rdata, 0);
        }
 
-       clistr_pull(cli->inbuf, alt_name, rdata+4, sizeof(fstring), len,
-                   STR_UNICODE);
+       if (size) {
+               *size = BVAL(rdata, 8);
+       }
 
-       SAFE_FREE(rdata);
-       SAFE_FREE(rparam);
+       if (nlinks) {
+               *nlinks = IVAL(rdata, 16);
+       }
+
+       if (is_del_pending) {
+               *is_del_pending = CVAL(rdata, 20);
+       }
+
+       if (is_dir) {
+               *is_dir = CVAL(rdata, 20);
+       }
+
+       TALLOC_FREE(rdata);
+
+       return NT_STATUS_OK;
+}
+
+
+/* like cli_qpathinfo2 but do not use SMB_QUERY_FILE_ALL_INFO with smb1 */
+NTSTATUS cli_qpathinfo3(struct cli_state *cli, const char *fname,
+                       struct timespec *create_time,
+                       struct timespec *access_time,
+                       struct timespec *write_time,
+                       struct timespec *change_time,
+                       off_t *size, uint16_t *mode,
+                       SMB_INO_T *ino)
+{
+       NTSTATUS status = NT_STATUS_OK;
+       SMB_STRUCT_STAT st = { 0 };
+       uint32_t attr;
+       uint64_t pos;
+
+       if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+               return cli_qpathinfo2(cli, fname,
+                                     create_time, access_time, write_time, change_time,
+                                     size, mode, ino);
+       }
+
+       if (create_time || access_time || write_time || change_time || mode) {
+               status = cli_qpathinfo_basic(cli, fname, &st, &attr);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+       }
+
+       if (size) {
+               status = cli_qpathinfo_standard(cli, fname,
+                                               NULL, &pos, NULL, NULL, NULL);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+
+               *size = pos;
+       }
+
+       if (create_time) {
+               *create_time = st.st_ex_btime;
+       }
+       if (access_time) {
+               *access_time = st.st_ex_atime;
+       }
+       if (write_time) {
+               *write_time = st.st_ex_mtime;
+       }
+       if (change_time) {
+               *change_time = st.st_ex_ctime;
+       }
+       if (mode) {
+               *mode = attr;
+       }
+       if (ino) {
+               *ino = 0;
+       }
 
        return NT_STATUS_OK;
 }