client: enable allinfo and altname tab completion
[samba.git] / source3 / client / client.c
index 0e2e07bd71d08d56594d142ff1ed067f4a865d16..7a7ecd92eb6f1ff2a24b4f88b315f7adbbf4b7b3 100644 (file)
 
 #include "includes.h"
 #include "system/filesys.h"
-#include "popt_common.h"
+#include "popt_common_cmdline.h"
 #include "rpc_client/cli_pipe.h"
 #include "client/client_proto.h"
+#include "client/clitar_proto.h"
 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
 #include "../lib/util/select.h"
 #include "system/readline.h"
 extern int do_smb_browse(void); /* mDNS browsing */
 
 extern bool override_logfile;
-extern char tar_type;
 
 static int port = 0;
 static char *service;
 static char *desthost;
 static bool grepable = false;
+static bool quiet = false;
 static char *cmdstr = NULL;
 const char *cmd_ptr = NULL;
 
-static int io_bufsize = 524288;
+static int io_bufsize = 0; /* we use the default size */
+static int io_timeout = (CLIENT_TIMEOUT/1000); /* Per operation timeout (in seconds). */
 
 static int name_type = 0x20;
-static int max_protocol = PROTOCOL_NT1;
+static int max_protocol = -1;
 
 static int process_tok(char *tok);
 static int cmd_help(void);
 
 #define CREATE_ACCESS_READ READ_CONTROL_ACCESS
 
-/* 30 second timeout on most commands */
-#define CLIENT_TIMEOUT (30*1000)
-#define SHORT_TIMEOUT (5*1000)
-
 /* value for unused fid field in trans2 secondary request */
 #define FID_UNUSED (0xFFFF)
 
@@ -78,12 +76,6 @@ static int archive_level = 0;
 static bool translation = false;
 static bool have_ip;
 
-/* clitar bits insert */
-extern int blocksize;
-extern bool tar_inc;
-extern bool tar_reset;
-/* clitar bits end */
-
 static bool prompt = true;
 
 static bool recurse = false;
@@ -117,9 +109,6 @@ struct cli_state *cli;
 static char CLI_DIRSEP_CHAR = '\\';
 static char CLI_DIRSEP_STR[] = { '\\', '\0' };
 
-/* Authentication for client connections. */
-struct user_auth_info *auth_info;
-
 /* Accessor functions for directory paths. */
 static char *fileselection;
 static const char *client_get_fileselection(void)
@@ -198,16 +187,20 @@ static bool yesno(const char *p)
  number taken from the buffer. This may not equal the number written.
 ****************************************************************************/
 
-static int writefile(int f, char *b, int n)
+static ssize_t writefile(int f, char *b, size_t n)
 {
-       int i;
+       size_t i = 0;
+
+       if (n == 0) {
+               errno = EINVAL;
+               return -1;
+       }
 
        if (!translation) {
                return write(f,b,n);
        }
 
-       i = 0;
-       while (i < n) {
+       do {
                if (*b == '\r' && (i<(n-1)) && *(b+1) == '\n') {
                        b++;i++;
                }
@@ -216,9 +209,9 @@ static int writefile(int f, char *b, int n)
                }
                b++;
                i++;
-       }
+       } while (i < n);
 
-       return(i);
+       return (ssize_t)i;
 }
 
 /****************************************************************************
@@ -226,17 +219,17 @@ static int writefile(int f, char *b, int n)
  number read. read approx n bytes.
 ****************************************************************************/
 
-static int readfile(uint8_t *b, int n, XFILE *f)
+static int readfile(uint8_t *b, int n, FILE *f)
 {
        int i;
        int c;
 
        if (!translation)
-               return x_fread(b,1,n,f);
+               return fread(b,1,n,f);
 
        i = 0;
-       while (i < (n - 1) && (i < BUFFER_SIZE)) {
-               if ((c = x_getc(f)) == EOF) {
+       while (i < (n - 1)) {
+               if ((c = getc(f)) == EOF) {
                        break;
                }
 
@@ -251,7 +244,7 @@ static int readfile(uint8_t *b, int n, XFILE *f)
 }
 
 struct push_state {
-       XFILE *f;
+       FILE *f;
        off_t nread;
 };
 
@@ -260,7 +253,7 @@ static size_t push_source(uint8_t *buf, size_t n, void *priv)
        struct push_state *state = (struct push_state *)priv;
        int result;
 
-       if (x_feof(state->f)) {
+       if (feof(state->f)) {
                return 0;
        }
 
@@ -307,13 +300,13 @@ static void send_message(const char *username)
 
 static int do_dskattr(void)
 {
-       int total, bsize, avail;
+       uint64_t total, bsize, avail;
        struct cli_state *targetcli = NULL;
        char *targetpath = NULL;
        TALLOC_CTX *ctx = talloc_tos();
        NTSTATUS status;
 
-       status = cli_resolve_path(ctx, "", auth_info, cli,
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(), cli,
                                  client_get_cur_dir(), &targetcli,
                                  &targetpath);
        if (!NT_STATUS_IS_OK(status)) {
@@ -321,14 +314,16 @@ static int do_dskattr(void)
                return 1;
        }
 
-       status = cli_dskattr(targetcli, &bsize, &total, &avail);
+       status = cli_disk_size(targetcli, targetpath, &bsize, &total, &avail);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Error in dskattr: %s\n", nt_errstr(status));
                return 1;
        }
 
-       d_printf("\n\t\t%d blocks of size %d. %d blocks available\n",
-                total, bsize, avail);
+       d_printf("\n\t\t%" PRIu64
+               " blocks of size %" PRIu64
+               ". %" PRIu64 " blocks available\n",
+               total, bsize, avail);
 
        return 0;
 }
@@ -355,6 +350,37 @@ static void normalize_name(char *newdir)
        }
 }
 
+/****************************************************************************
+ Local name cleanup before sending to server. SMB1 allows relative pathnames,
+ but SMB2 does not, so we need to resolve them locally.
+****************************************************************************/
+
+char *client_clean_name(TALLOC_CTX *ctx, const char *name)
+{
+       char *newname = NULL;
+       if (name == NULL) {
+               return NULL;
+       }
+
+       /* First ensure any path separators are correct. */
+       newname = talloc_strdup(ctx, name);
+       if (newname == NULL) {
+               return NULL;
+       }
+       normalize_name(newname);
+
+       /* Now remove any relative (..) path components. */
+       if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
+               newname = unix_clean_name(ctx, newname);
+       } else {
+               newname = clean_name(ctx, newname);
+       }
+       if (newname == NULL) {
+               return NULL;
+       }
+       return newname;
+}
+
 /****************************************************************************
  Change directory - inner section.
 ****************************************************************************/
@@ -367,7 +393,7 @@ static int do_cd(const char *new_dir)
        char *targetpath = NULL;
        struct cli_state *targetcli = NULL;
        SMB_STRUCT_STAT sbuf;
-       uint32 attributes;
+       uint32_t attributes;
        int ret = 1;
        TALLOC_CTX *ctx = talloc_stackframe();
        NTSTATUS status;
@@ -409,11 +435,11 @@ static int do_cd(const char *new_dir)
        }
        client_set_cur_dir(new_cd);
 
-       new_cd = clean_name(ctx, new_cd);
+       new_cd = client_clean_name(ctx, new_cd);
        client_set_cur_dir(new_cd);
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, new_cd,
-                                 &targetcli, &targetpath);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                               cli, new_cd, &targetcli, &targetpath);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("cd %s: %s\n", new_cd, nt_errstr(status));
                client_set_cur_dir(saved_dir);
@@ -453,7 +479,7 @@ static int do_cd(const char *new_dir)
                        client_set_cur_dir(saved_dir);
                        goto out;
                }
-               targetpath = clean_name(ctx, targetpath);
+               targetpath = client_clean_name(ctx, targetpath);
                if (!targetpath) {
                        client_set_cur_dir(saved_dir);
                        goto out;
@@ -582,7 +608,7 @@ static NTSTATUS display_finfo(struct cli_state *cli_state, struct file_info *fin
                status = cli_ntcreate(cli_state, afname, 0,
                                      CREATE_ACCESS_READ, 0,
                                      FILE_SHARE_READ|FILE_SHARE_WRITE,
-                                     FILE_OPEN, 0x0, 0x0, &fnum);
+                                     FILE_OPEN, 0x0, 0x0, &fnum, NULL);
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
                                   afname, nt_errstr(status)));
@@ -820,7 +846,7 @@ static NTSTATUS do_list_helper(const char *mntpoint, struct file_info *f,
 ****************************************************************************/
 
 NTSTATUS do_list(const char *mask,
-                       uint16 attribute,
+                       uint16_t attribute,
                        NTSTATUS (*fn)(struct cli_state *cli_state, struct file_info *,
                                   const char *dir),
                        bool rec,
@@ -864,9 +890,9 @@ NTSTATUS do_list(const char *mask,
 
                        /* check for dfs */
 
-                       status = cli_resolve_path(ctx, "", auth_info, cli,
-                                                 head, &targetcli,
-                                                 &targetpath);
+                       status = cli_resolve_path(ctx, "",
+                                       popt_get_cmdline_auth_info(),
+                                       cli, head, &targetcli, &targetpath);
                        if (!NT_STATUS_IS_OK(status)) {
                                d_printf("do_list: [%s] %s\n", head,
                                         nt_errstr(status));
@@ -907,8 +933,9 @@ NTSTATUS do_list(const char *mask,
                }
        } else {
                /* check for dfs */
-               status = cli_resolve_path(ctx, "", auth_info, cli, mask,
-                                         &targetcli, &targetpath);
+               status = cli_resolve_path(ctx, "",
+                               popt_get_cmdline_auth_info(), cli, mask,
+                                 &targetcli, &targetpath);
                if (NT_STATUS_IS_OK(status)) {
                        status = cli_list(targetcli, targetpath, attribute,
                                          do_list_helper, targetcli);
@@ -936,7 +963,7 @@ NTSTATUS do_list(const char *mask,
 static int cmd_dir(void)
 {
        TALLOC_CTX *ctx = talloc_tos();
-       uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
+       uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
        char *mask = NULL;
        char *buf = NULL;
        int rc = 1;
@@ -962,6 +989,11 @@ static int cmd_dir(void)
                return 1;
        }
 
+       mask = client_clean_name(ctx, mask);
+       if (mask == NULL) {
+               return 1;
+       }
+
        if (showacls) {
                /* cwd is only used if showacls is on */
                client_set_cwd(client_get_cur_dir());
@@ -986,7 +1018,7 @@ static int cmd_dir(void)
 static int cmd_du(void)
 {
        TALLOC_CTX *ctx = talloc_tos();
-       uint16 attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
+       uint16_t attribute = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
        char *mask = NULL;
        char *buf = NULL;
        NTSTATUS status;
@@ -1014,6 +1046,14 @@ static int cmd_du(void)
        } else {
                mask = talloc_strdup(ctx, "*");
        }
+       if (!mask) {
+               return 1;
+       }
+
+       mask = client_clean_name(ctx, mask);
+       if (mask == NULL) {
+               return 1;
+       }
 
        status = do_list(mask, attribute, do_du, recurse, true);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1057,7 +1097,10 @@ static int cmd_echo(void)
 static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
 {
        int *pfd = (int *)priv;
-       if (writefile(*pfd, buf, n) == -1) {
+       ssize_t rc;
+
+       rc = writefile(*pfd, buf, n);
+       if (rc == -1) {
                return map_nt_error_from_unix(errno);
        }
        return NT_STATUS_OK;
@@ -1070,7 +1113,7 @@ static int do_get(const char *rname, const char *lname_in, bool reget)
        uint16_t fnum;
        bool newhandle = false;
        struct timespec tp_start;
-       uint16 attr;
+       uint16_t attr;
        off_t size;
        off_t start = 0;
        off_t nread = 0;
@@ -1092,8 +1135,8 @@ static int do_get(const char *rname, const char *lname_in, bool reget)
                }
        }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, rname, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                               cli, rname, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
                return 1;
@@ -1117,6 +1160,7 @@ static int do_get(const char *rname, const char *lname_in, bool reget)
                                start = lseek(handle, 0, SEEK_END);
                                if (start == -1) {
                                        d_printf("Error seeking local file\n");
+                                       close(handle);
                                        return 1;
                                }
                        }
@@ -1138,6 +1182,9 @@ static int do_get(const char *rname, const char *lname_in, bool reget)
                                      NULL);
                if(!NT_STATUS_IS_OK(status)) {
                        d_printf("getattrib: %s\n", nt_errstr(status));
+                       if (newhandle) {
+                               close(handle);
+                       }
                        return 1;
                }
        }
@@ -1150,6 +1197,9 @@ static int do_get(const char *rname, const char *lname_in, bool reget)
        if (!NT_STATUS_IS_OK(status)) {
                d_fprintf(stderr, "parallel_read returned %s\n",
                          nt_errstr(status));
+               if (newhandle) {
+                       close(handle);
+               }
                cli_close(targetcli, fnum);
                return 1;
        }
@@ -1165,7 +1215,7 @@ static int do_get(const char *rname, const char *lname_in, bool reget)
        }
 
        if (archive_level >= 2 && (attr & FILE_ATTRIBUTE_ARCHIVE)) {
-               cli_setatr(cli, rname, attr & ~(uint16)FILE_ATTRIBUTE_ARCHIVE, 0);
+               cli_setatr(cli, rname, attr & ~(uint16_t)FILE_ATTRIBUTE_ARCHIVE, 0);
        }
 
        {
@@ -1210,7 +1260,7 @@ static int cmd_get(void)
        if (!rname) {
                return 1;
        }
-       rname = clean_name(ctx, rname);
+       rname = client_clean_name(ctx, rname);
        if (!rname) {
                return 1;
        }
@@ -1276,6 +1326,10 @@ static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
                if (!rname) {
                        return NT_STATUS_NO_MEMORY;
                }
+               rname = client_clean_name(ctx, rname);
+               if (rname == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
                do_get(rname, finfo->name, false);
                TALLOC_FREE(rname);
                return NT_STATUS_OK;
@@ -1295,6 +1349,10 @@ static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
        if (!new_cd) {
                return NT_STATUS_NO_MEMORY;
        }
+       new_cd = client_clean_name(ctx, new_cd);
+       if (new_cd == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
        client_set_cur_dir(new_cd);
 
        string_replace(finfo->name,'\\','/');
@@ -1325,8 +1383,21 @@ static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
                return NT_STATUS_NO_MEMORY;
        }
 
-       status = do_list(mget_mask, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY,do_mget,false, true);
-       if (!NT_STATUS_IS_OK(status)) {
+       mget_mask = client_clean_name(ctx, mget_mask);
+       if (mget_mask == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       status = do_list(mget_mask,
+                        (FILE_ATTRIBUTE_SYSTEM
+                         | FILE_ATTRIBUTE_HIDDEN
+                         | FILE_ATTRIBUTE_DIRECTORY),
+                        do_mget, false, true);
+       if (!NT_STATUS_IS_OK(status)
+        && !NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
+               /*
+                * Ignore access denied errors to ensure all permitted files are
+                * pulled down.
+                */
                return status;
        }
 
@@ -1356,6 +1427,7 @@ static int cmd_more(void)
        const char *pager;
        int fd;
        int rc = 0;
+       mode_t mask;
 
        rname = talloc_strdup(ctx, client_get_cur_dir());
        if (!rname) {
@@ -1366,7 +1438,9 @@ static int cmd_more(void)
        if (!lname) {
                return 1;
        }
+       mask = umask(S_IRWXO | S_IRWXG);
        fd = mkstemp(lname);
+       umask(mask);
        if (fd == -1) {
                d_printf("failed to create temporary file for more\n");
                return 1;
@@ -1382,7 +1456,7 @@ static int cmd_more(void)
        if (!rname) {
                return 1;
        }
-       rname = clean_name(ctx,rname);
+       rname = client_clean_name(ctx,rname);
        if (!rname) {
                return 1;
        }
@@ -1414,7 +1488,7 @@ static int cmd_more(void)
 static int cmd_mget(void)
 {
        TALLOC_CTX *ctx = talloc_tos();
-       uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
+       uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
        char *mget_mask = NULL;
        char *buf = NULL;
        NTSTATUS status = NT_STATUS_OK;
@@ -1440,6 +1514,10 @@ static int cmd_mget(void)
                if (!mget_mask) {
                        return 1;
                }
+               mget_mask = client_clean_name(ctx, mget_mask);
+               if (mget_mask == NULL) {
+                       return 1;
+               }
                status = do_list(mget_mask, attribute, do_mget, false, true);
                if (!NT_STATUS_IS_OK(status)) {
                        return 1;
@@ -1458,6 +1536,10 @@ static int cmd_mget(void)
                if (!mget_mask) {
                        return 1;
                }
+               mget_mask = client_clean_name(ctx, mget_mask);
+               if (mget_mask == NULL) {
+                       return 1;
+               }
                status = do_list(mget_mask, attribute, do_mget, false, true);
                if (!NT_STATUS_IS_OK(status)) {
                        return 1;
@@ -1478,8 +1560,8 @@ static bool do_mkdir(const char *name)
        char *targetname = NULL;
        NTSTATUS status;
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                               cli, name, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("mkdir %s: %s\n", name, nt_errstr(status));
                return false;
@@ -1522,6 +1604,7 @@ static bool do_altname(const char *name)
 static int cmd_quit(void)
 {
        cli_shutdown(cli);
+       popt_free_cmdline_auth_info();
        exit(0);
        /* NOTREACHED */
        return 0;
@@ -1553,6 +1636,10 @@ static int cmd_mkdir(void)
        if (!mask) {
                return 1;
        }
+       mask = client_clean_name(ctx, mask);
+       if (mask == NULL) {
+               return 1;
+       }
 
        if (recurse) {
                char *ddir = NULL;
@@ -1567,8 +1654,9 @@ static int cmd_mkdir(void)
                        return 1;
                }
 
-               status = cli_resolve_path(ctx, "", auth_info, cli, mask,
-                                         &targetcli, &targetname);
+               status = cli_resolve_path(ctx, "",
+                               popt_get_cmdline_auth_info(), cli, mask,
+                               &targetcli, &targetname);
                if (!NT_STATUS_IS_OK(status)) {
                        return 1;
                }
@@ -1623,6 +1711,10 @@ static int cmd_altname(void)
        if (!name) {
                return 1;
        }
+       name = client_clean_name(ctx, name);
+       if (name == NULL) {
+               return 1;
+       }
        do_altname(name);
        return 0;
 }
@@ -1686,13 +1778,12 @@ static int do_allinfo(const char *name)
        struct timespec b_time, a_time, m_time, c_time;
        off_t size;
        uint16_t mode;
-       SMB_INO_T ino;
        NTTIME tmp;
        uint16_t fnum;
        unsigned int num_streams;
        struct stream_struct *streams;
        int num_snapshots;
-       char **snapshots;
+       char **snapshots = NULL;
        unsigned int i;
        NTSTATUS status;
 
@@ -1700,28 +1791,37 @@ static int do_allinfo(const char *name)
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("%s getting alt name for %s\n", nt_errstr(status),
                         name);
-               return false;
+               /*
+                * Ignore not supported or not implemented, it does not
+                * hurt if we can't list alternate names.
+                */
+               if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED) ||
+                   NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED)) {
+                       altname[0] = '\0';
+               } else {
+                       return false;
+               }
        }
        d_printf("altname: %s\n", altname);
 
-       status = cli_qpathinfo2(cli, name, &b_time, &a_time, &m_time, &c_time,
-                               &size, &mode, &ino);
+       status = cli_qpathinfo3(cli, name, &b_time, &a_time, &m_time, &c_time,
+                               &size, &mode, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("%s getting pathinfo for %s\n", nt_errstr(status),
                         name);
                return false;
        }
 
-       unix_timespec_to_nt_time(&tmp, b_time);
+       tmp = unix_timespec_to_nt_time(b_time);
        d_printf("create_time:    %s\n", nt_time_string(talloc_tos(), tmp));
 
-       unix_timespec_to_nt_time(&tmp, a_time);
+       tmp = unix_timespec_to_nt_time(a_time);
        d_printf("access_time:    %s\n", nt_time_string(talloc_tos(), tmp));
 
-       unix_timespec_to_nt_time(&tmp, m_time);
+       tmp = unix_timespec_to_nt_time(m_time);
        d_printf("write_time:     %s\n", nt_time_string(talloc_tos(), tmp));
 
-       unix_timespec_to_nt_time(&tmp, c_time);
+       tmp = unix_timespec_to_nt_time(c_time);
        d_printf("change_time:    %s\n", nt_time_string(talloc_tos(), tmp));
 
        d_printf("attributes: %s (%x)\n", attr_str(talloc_tos(), mode), mode);
@@ -1761,7 +1861,7 @@ static int do_allinfo(const char *name)
                              SEC_STD_SYNCHRONIZE, 0,
                              FILE_SHARE_READ|FILE_SHARE_WRITE
                              |FILE_SHARE_DELETE,
-                             FILE_OPEN, 0x0, 0x0, &fnum);
+                             FILE_OPEN, 0x0, 0x0, &fnum, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                /*
                 * Ignore failure, it does not hurt if we can't list
@@ -1769,6 +1869,20 @@ static int do_allinfo(const char *name)
                 */
                return 0;
        }
+       /*
+        * In order to get shadow copy data over SMB1 we
+        * must call twice, once with 'get_names = false'
+        * to get the size, then again with 'get_names = true'
+        * to get the data or a Windows server fails to return
+        * valid info. Samba doesn't have this bug. JRA.
+        */
+
+       status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
+                                     false, &snapshots, &num_snapshots);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_close(cli, fnum);
+               return 0;
+       }
        status = cli_shadow_copy_data(talloc_tos(), cli, fnum,
                                      true, &snapshots, &num_snapshots);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1782,7 +1896,7 @@ static int do_allinfo(const char *name)
                d_printf("%s\n", snapshots[i]);
                snap_name = talloc_asprintf(talloc_tos(), "%s%s",
                                            snapshots[i], name);
-               status = cli_qpathinfo2(cli, snap_name, &b_time, &a_time,
+               status = cli_qpathinfo3(cli, snap_name, &b_time, &a_time,
                                        &m_time, &c_time, &size,
                                        NULL, NULL);
                if (!NT_STATUS_IS_OK(status)) {
@@ -1791,18 +1905,19 @@ static int do_allinfo(const char *name)
                        TALLOC_FREE(snap_name);
                        continue;
                }
-               unix_timespec_to_nt_time(&tmp, b_time);
+               tmp = unix_timespec_to_nt_time(b_time);
                d_printf("create_time:    %s\n", nt_time_string(talloc_tos(), tmp));
-               unix_timespec_to_nt_time(&tmp, a_time);
+               tmp = unix_timespec_to_nt_time(a_time);
                d_printf("access_time:    %s\n", nt_time_string(talloc_tos(), tmp));
-               unix_timespec_to_nt_time(&tmp, m_time);
+               tmp =unix_timespec_to_nt_time(m_time);
                d_printf("write_time:     %s\n", nt_time_string(talloc_tos(), tmp));
-               unix_timespec_to_nt_time(&tmp, c_time);
+               tmp = unix_timespec_to_nt_time(c_time);
                d_printf("change_time:    %s\n", nt_time_string(talloc_tos(), tmp));
                d_printf("size: %d\n", (int)size);
        }
 
        TALLOC_FREE(snapshots);
+       cli_close(cli, fnum);
 
        return 0;
 }
@@ -1830,7 +1945,10 @@ static int cmd_allinfo(void)
        if (!name) {
                return 1;
        }
-
+       name = client_clean_name(ctx, name);
+       if (name == NULL) {
+               return 1;
+       }
        do_allinfo(name);
 
        return 0;
@@ -1844,7 +1962,7 @@ static int do_put(const char *rname, const char *lname, bool reput)
 {
        TALLOC_CTX *ctx = talloc_tos();
        uint16_t fnum;
-       XFILE *f;
+       FILE *f;
        off_t start = 0;
        int rc = 0;
        struct timespec tp_start;
@@ -1853,8 +1971,8 @@ static int do_put(const char *rname, const char *lname, bool reput)
        struct push_state state;
        NTSTATUS status;
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, rname,
-                                 &targetcli, &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                               cli, rname, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Failed to open %s: %s\n", rname, nt_errstr(status));
                return 1;
@@ -1893,14 +2011,14 @@ static int do_put(const char *rname, const char *lname, bool reput)
           Note that in this case this function will exit(0) rather
           than returning. */
        if (!strcmp(lname, "-")) {
-               f = x_stdin;
+               f = stdin;
                /* size of file is not known */
        } else {
-               f = x_fopen(lname,O_RDONLY, 0);
+               f = fopen(lname, "r");
                if (f && reput) {
-                       if (x_tseek(f, start, SEEK_SET) == -1) {
+                       if (fseek(f, start, SEEK_SET) == -1) {
                                d_printf("Error seeking local file\n");
-                               x_fclose(f);
+                               fclose(f);
                                return 1;
                        }
                }
@@ -1914,7 +2032,7 @@ static int do_put(const char *rname, const char *lname, bool reput)
        DEBUG(1,("putting file %s as %s ",lname,
                 rname));
 
-       x_setvbuf(f, NULL, X_IOFBF, io_bufsize);
+       setvbuf(f, NULL, _IOFBF, io_bufsize);
 
        state.f = f;
        state.nread = 0;
@@ -1930,14 +2048,14 @@ static int do_put(const char *rname, const char *lname, bool reput)
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("%s closing remote file %s\n", nt_errstr(status),
                         rname);
-               if (f != x_stdin) {
-                       x_fclose(f);
+               if (f != stdin) {
+                       fclose(f);
                }
                return 1;
        }
 
-       if (f != x_stdin) {
-               x_fclose(f);
+       if (f != stdin) {
+               fclose(f);
        }
 
        {
@@ -1954,8 +2072,9 @@ static int do_put(const char *rname, const char *lname, bool reput)
                         put_total_size / (1.024*put_total_time_ms)));
        }
 
-       if (f == x_stdin) {
+       if (f == stdin) {
                cli_shutdown(cli);
+               popt_free_cmdline_auth_info();
                exit(rc);
        }
 
@@ -1992,7 +2111,7 @@ static int cmd_put(void)
                return 1;
        }
 
-       rname = clean_name(ctx, rname);
+       rname = client_clean_name(ctx, rname);
        if (!rname) {
                return 1;
        }
@@ -2032,8 +2151,7 @@ static void free_file_list (struct file_list *l_head)
        for (list = l_head; list; list = next) {
                next = list->next;
                DLIST_REMOVE(l_head, list);
-               SAFE_FREE(list->file_path);
-               SAFE_FREE(list);
+               TALLOC_FREE(list);
        }
 }
 
@@ -2078,8 +2196,11 @@ static int cmd_select(void)
   match must be always set to true when calling this function
 ****************************************************************************/
 
-static int file_find(struct file_list **list, const char *directory,
-                     const char *expression, bool match)
+static int file_find(TALLOC_CTX *ctx,
+                       struct file_list **list,
+                       const char *directory,
+                       const char *expression,
+                       bool match)
 {
        DIR *dir;
        struct file_list *entry;
@@ -2099,7 +2220,8 @@ static int file_find(struct file_list **list, const char *directory,
                if (!strcmp(".", dname))
                        continue;
 
-               if (asprintf(&path, "%s/%s", directory, dname) <= 0) {
+               path = talloc_asprintf(ctx, "%s/%s", directory, dname);
+               if (path == NULL) {
                        continue;
                }
 
@@ -2110,29 +2232,33 @@ static int file_find(struct file_list **list, const char *directory,
                                if (ret == 0) {
                                        if (S_ISDIR(statbuf.st_mode)) {
                                                isdir = true;
-                                               ret = file_find(list, path, expression, false);
+                                               ret = file_find(ctx,
+                                                               list,
+                                                               path,
+                                                               expression,
+                                                               false);
                                        }
                                } else {
                                        d_printf("file_find: cannot stat file %s\n", path);
                                }
 
                                if (ret == -1) {
-                                       SAFE_FREE(path);
+                                       TALLOC_FREE(path);
                                        closedir(dir);
                                        return -1;
                                }
                        }
-                       entry = SMB_MALLOC_P(struct file_list);
+                       entry = talloc_zero(ctx, struct file_list);
                        if (!entry) {
                                d_printf("Out of memory in file_find\n");
                                closedir(dir);
                                return -1;
                        }
-                       entry->file_path = path;
+                       entry->file_path = talloc_move(entry, &path);
                        entry->isdir = isdir;
                         DLIST_ADD(*list, entry);
                } else {
-                       SAFE_FREE(path);
+                       TALLOC_FREE(path);
                }
         }
 
@@ -2156,7 +2282,7 @@ static int cmd_mput(void)
 
                file_list = NULL;
 
-               ret = file_find(&file_list, ".", p, true);
+               ret = file_find(ctx, &file_list, ".", p, true);
                if (ret) {
                        free_file_list(file_list);
                        continue;
@@ -2194,6 +2320,19 @@ static int cmd_mput(void)
                                                break;
                                        }
                                        normalize_name(rname);
+                                       {
+                                               char *tmp_rname =
+                                                       client_clean_name(ctx, rname);
+                                               if (tmp_rname == NULL) {
+                                                       break;
+                                               }
+                                               SAFE_FREE(rname);
+                                               rname = smb_xstrdup(tmp_rname);
+                                               TALLOC_FREE(tmp_rname);
+                                               if (rname == NULL) {
+                                                       break;
+                                               }
+                                       }
                                        if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
                                            !do_mkdir(rname)) {
                                                DEBUG (0, ("Unable to make dir, skipping..."));
@@ -2224,6 +2363,18 @@ static int cmd_mput(void)
 
                        normalize_name(rname);
 
+                       {
+                               char *tmp_rname = client_clean_name(ctx, rname);
+                               if (tmp_rname == NULL) {
+                                       break;
+                               }
+                               SAFE_FREE(rname);
+                               rname = smb_xstrdup(tmp_rname);
+                               TALLOC_FREE(tmp_rname);
+                               if (rname == NULL) {
+                                       break;
+                               }
+                       }
                        do_put(rname, lname, false);
                }
                free_file_list(file_list);
@@ -2376,7 +2527,7 @@ static int cmd_del(void)
        char *mask = NULL;
        char *buf = NULL;
        NTSTATUS status = NT_STATUS_OK;
-       uint16 attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
+       uint16_t attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
 
        if (recurse) {
                attribute |= FILE_ATTRIBUTE_DIRECTORY;
@@ -2394,6 +2545,10 @@ static int cmd_del(void)
        if (!mask) {
                return 1;
        }
+       mask = client_clean_name(ctx, mask);
+       if (mask == NULL) {
+               return 1;
+       }
 
        status = do_list(mask,attribute,do_del,false,false);
        if (!NT_STATUS_IS_OK(status)) {
@@ -2402,6 +2557,187 @@ static int cmd_del(void)
        return 0;
 }
 
+/****************************************************************************
+ Delete some files.
+****************************************************************************/
+
+static NTSTATUS delete_remote_files_list(struct cli_state *cli_state,
+                                        struct file_list *flist)
+{
+       NTSTATUS status = NT_STATUS_OK;
+       struct file_list *deltree_list_iter = NULL;
+
+       for (deltree_list_iter = flist;
+                       deltree_list_iter != NULL;
+                       deltree_list_iter = deltree_list_iter->next) {
+               if (CLI_DIRSEP_CHAR == '/') {
+                       /* POSIX. */
+                       status = cli_posix_unlink(cli_state,
+                                       deltree_list_iter->file_path);
+               } else if (deltree_list_iter->isdir) {
+                       status = cli_rmdir(cli_state,
+                                       deltree_list_iter->file_path);
+               } else {
+                       status = cli_unlink(cli_state,
+                                       deltree_list_iter->file_path,
+                                       FILE_ATTRIBUTE_SYSTEM |
+                                       FILE_ATTRIBUTE_HIDDEN);
+               }
+               if (!NT_STATUS_IS_OK(status)) {
+                       d_printf("%s deleting remote %s %s\n",
+                               nt_errstr(status),
+                               deltree_list_iter->isdir ?
+                               "directory" : "file",
+                               deltree_list_iter->file_path);
+                       return status;
+               }
+       }
+       return NT_STATUS_OK;
+}
+
+/****************************************************************************
+ Save a list of files to delete.
+****************************************************************************/
+
+static struct file_list *deltree_list_head;
+
+static NTSTATUS do_deltree_list(struct cli_state *cli_state,
+                               struct file_info *finfo,
+                               const char *dir)
+{
+       struct file_list **file_list_head_pp = &deltree_list_head;
+       struct file_list *dt = NULL;
+
+       if (!do_this_one(finfo)) {
+               return NT_STATUS_OK;
+       }
+
+       /* skip if this is . or .. */
+       if (ISDOT(finfo->name) || ISDOTDOT(finfo->name)) {
+               return NT_STATUS_OK;
+       }
+
+       dt = talloc_zero(NULL, struct file_list);
+       if (dt == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       /* create absolute filename for cli_ntcreate() */
+       dt->file_path = talloc_asprintf(dt,
+                                       "%s%s%s",
+                                       dir,
+                                       CLI_DIRSEP_STR,
+                                       finfo->name);
+       if (dt->file_path == NULL) {
+               TALLOC_FREE(dt);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
+               dt->isdir = true;
+       }
+
+       DLIST_ADD(*file_list_head_pp, dt);
+       return NT_STATUS_OK;
+}
+
+static int cmd_deltree(void)
+{
+       TALLOC_CTX *ctx = talloc_tos();
+       char *buf = NULL;
+       NTSTATUS status = NT_STATUS_OK;
+       struct file_list *deltree_list_norecurse = NULL;
+       struct file_list *deltree_list_iter = NULL;
+       uint16_t attribute = FILE_ATTRIBUTE_SYSTEM |
+                            FILE_ATTRIBUTE_HIDDEN |
+                            FILE_ATTRIBUTE_DIRECTORY;
+       bool ok;
+       char *mask = talloc_strdup(ctx, client_get_cur_dir());
+       if (mask == NULL) {
+               return 1;
+       }
+       ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
+       if (!ok) {
+               d_printf("deltree <filename>\n");
+               return 1;
+       }
+       mask = talloc_asprintf_append(mask, "%s", buf);
+       if (mask == NULL) {
+               return 1;
+       }
+       mask = client_clean_name(ctx, mask);
+       if (mask == NULL) {
+               return 1;
+       }
+
+       deltree_list_head = NULL;
+
+       /*
+        * Get the list of directories to
+        * delete (in case mask has a wildcard).
+        */
+       status = do_list(mask, attribute, do_deltree_list, false, true);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto err;
+       }
+       deltree_list_norecurse = deltree_list_head;
+       deltree_list_head = NULL;
+
+       for (deltree_list_iter = deltree_list_norecurse;
+            deltree_list_iter != NULL;
+            deltree_list_iter = deltree_list_iter->next) {
+
+               if (deltree_list_iter->isdir == false) {
+                       /* Just a regular file. */
+                       if (CLI_DIRSEP_CHAR == '/') {
+                               /* POSIX. */
+                               status = cli_posix_unlink(cli,
+                                       deltree_list_iter->file_path);
+                       } else {
+                               status = cli_unlink(cli,
+                                       deltree_list_iter->file_path,
+                                       FILE_ATTRIBUTE_SYSTEM |
+                                       FILE_ATTRIBUTE_HIDDEN);
+                       }
+                       if (!NT_STATUS_IS_OK(status)) {
+                               goto err;
+                       }
+                       continue;
+               }
+
+               /*
+                * Get the list of files or directories to
+                * delete in depth order.
+                */
+               status = do_list(deltree_list_iter->file_path,
+                                attribute,
+                                do_deltree_list,
+                                true,
+                                true);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto err;
+               }
+               status = delete_remote_files_list(cli, deltree_list_head);
+               free_file_list(deltree_list_head);
+               deltree_list_head = NULL;
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto err;
+               }
+       }
+
+       free_file_list(deltree_list_norecurse);
+       free_file_list(deltree_list_head);
+       return 0;
+
+  err:
+
+       free_file_list(deltree_list_norecurse);
+       free_file_list(deltree_list_head);
+       deltree_list_head = NULL;
+       return 1;
+}
+
+
 /****************************************************************************
  Wildcard delete some files.
 ****************************************************************************/
@@ -2411,7 +2747,7 @@ static int cmd_wdel(void)
        TALLOC_CTX *ctx = talloc_tos();
        char *mask = NULL;
        char *buf = NULL;
-       uint16 attribute;
+       uint16_t attribute;
        struct cli_state *targetcli;
        char *targetname = NULL;
        NTSTATUS status;
@@ -2421,7 +2757,7 @@ static int cmd_wdel(void)
                return 1;
        }
 
-       attribute = (uint16)strtol(buf, (char **)NULL, 16);
+       attribute = (uint16_t)strtol(buf, (char **)NULL, 16);
 
        if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
                d_printf("wdel 0x<attrib> <wcard>\n");
@@ -2434,9 +2770,13 @@ static int cmd_wdel(void)
        if (!mask) {
                return 1;
        }
+       mask = client_clean_name(ctx, mask);
+       if (mask == NULL) {
+               return 1;
+       }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                               cli, mask, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("cmd_wdel %s: %s\n", mask, nt_errstr(status));
                return 1;
@@ -2475,8 +2815,13 @@ static int cmd_open(void)
                return 1;
        }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
-                                 &targetname);
+       mask = client_clean_name(ctx, mask);
+       if (mask == NULL) {
+               return 1;
+       }
+
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                       cli, mask, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("open %s: %s\n", mask, nt_errstr(status));
                return 1;
@@ -2485,12 +2830,12 @@ static int cmd_open(void)
        status = cli_ntcreate(targetcli, targetname, 0,
                        FILE_READ_DATA|FILE_WRITE_DATA, 0,
                        FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
-                       0x0, 0x0, &fnum);
+                       0x0, 0x0, &fnum, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                status = cli_ntcreate(targetcli, targetname, 0,
                                FILE_READ_DATA, 0,
                                FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
-                               0x0, 0x0, &fnum);
+                               0x0, 0x0, &fnum, NULL);
                if (NT_STATUS_IS_OK(status)) {
                        d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
                } else {
@@ -2507,35 +2852,54 @@ static int cmd_posix_encrypt(void)
 {
        TALLOC_CTX *ctx = talloc_tos();
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
+       char *domain = NULL;
+       char *user = NULL;
+       char *password = NULL;
+       struct cli_credentials *creds = NULL;
+       struct cli_credentials *lcreds = NULL;
 
-       if (cli->use_kerberos) {
-               status = cli_gss_smb_encryption_start(cli);
-       } else {
-               char *domain = NULL;
-               char *user = NULL;
-               char *password = NULL;
+       if (next_token_talloc(ctx, &cmd_ptr, &domain, NULL)) {
 
-               if (!next_token_talloc(ctx, &cmd_ptr,&domain,NULL)) {
+               if (!next_token_talloc(ctx, &cmd_ptr, &user, NULL)) {
                        d_printf("posix_encrypt domain user password\n");
                        return 1;
                }
 
-               if (!next_token_talloc(ctx, &cmd_ptr,&user,NULL)) {
+               if (!next_token_talloc(ctx, &cmd_ptr, &password, NULL)) {
                        d_printf("posix_encrypt domain user password\n");
                        return 1;
                }
 
-               if (!next_token_talloc(ctx, &cmd_ptr,&password,NULL)) {
+               lcreds = cli_session_creds_init(ctx,
+                                               user,
+                                               domain,
+                                               NULL, /* realm */
+                                               password,
+                                               false, /* use_kerberos */
+                                               false, /* fallback_after_kerberos */
+                                               false, /* use_ccache */
+                                               false); /* password_is_nt_hash */
+               if (lcreds == NULL) {
+                       d_printf("cli_session_creds_init() failed.\n");
+                       return -1;
+               }
+               creds = lcreds;
+       } else {
+               bool auth_requested = false;
+
+               creds = get_cmdline_auth_info_creds(
+                               popt_get_cmdline_auth_info());
+
+               auth_requested = cli_credentials_authentication_requested(creds);
+               if (!auth_requested) {
                        d_printf("posix_encrypt domain user password\n");
                        return 1;
                }
-
-               status = cli_raw_ntlm_smb_encryption_start(cli,
-                                                       user,
-                                                       password,
-                                                       domain);
        }
 
+       status = cli_smb1_setup_encryption(cli, creds);
+       /* gensec currently references the creds so we can't free them here */
+       talloc_unlink(ctx, lcreds);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("posix_encrypt failed with error %s\n", nt_errstr(status));
        } else {
@@ -2571,6 +2935,10 @@ static int cmd_posix_open(void)
        if (!mask) {
                return 1;
        }
+       mask = client_clean_name(ctx, mask);
+       if (mask == NULL) {
+               return 1;
+       }
 
        if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
                d_printf("posix_open <filename> 0<mode>\n");
@@ -2578,8 +2946,8 @@ static int cmd_posix_open(void)
        }
        mode = (mode_t)strtol(buf, (char **)NULL, 8);
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                               cli, mask, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("posix_open %s: %s\n", mask, nt_errstr(status));
                return 1;
@@ -2626,6 +2994,10 @@ static int cmd_posix_mkdir(void)
        if (!mask) {
                return 1;
        }
+       mask = client_clean_name(ctx, mask);
+       if (mask == NULL) {
+               return 1;
+       }
 
        if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
                d_printf("posix_mkdir <filename> 0<mode>\n");
@@ -2633,8 +3005,8 @@ static int cmd_posix_mkdir(void)
        }
        mode = (mode_t)strtol(buf, (char **)NULL, 8);
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                               cli, mask, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("posix_mkdir %s: %s\n", mask, nt_errstr(status));
                return 1;
@@ -2670,9 +3042,13 @@ static int cmd_posix_unlink(void)
        if (!mask) {
                return 1;
        }
+       mask = client_clean_name(ctx, mask);
+       if (mask == NULL) {
+               return 1;
+       }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                               cli, mask, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("posix_unlink %s: %s\n", mask, nt_errstr(status));
                return 1;
@@ -2709,9 +3085,13 @@ static int cmd_posix_rmdir(void)
        if (!mask) {
                return 1;
        }
+       mask = client_clean_name(ctx, mask);
+       if (mask == NULL) {
+               return 1;
+       }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                       cli, mask, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("posix_rmdir %s: %s\n", mask, nt_errstr(status));
                return 1;
@@ -2753,8 +3133,8 @@ static int cmd_close(void)
 static int cmd_posix(void)
 {
        TALLOC_CTX *ctx = talloc_tos();
-       uint16 major, minor;
-       uint32 caplow, caphigh;
+       uint16_t major, minor;
+       uint32_t caplow, caphigh;
        char *caps;
        NTSTATUS status;
 
@@ -2942,6 +3322,51 @@ static int cmd_unlock(void)
        return 0;
 }
 
+static int cmd_posix_whoami(void)
+{
+       TALLOC_CTX *ctx = talloc_tos();
+       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
+       uint64_t uid = 0;
+       uint64_t gid = 0;
+       uint32_t num_gids = 0;
+       uint32_t num_sids = 0;
+       uint64_t *gids = NULL;
+       struct dom_sid *sids = NULL;
+       bool guest = false;
+       uint32_t i;
+
+       status = cli_posix_whoami(cli,
+                       ctx,
+                       &uid,
+                       &gid,
+                       &num_gids,
+                       &gids,
+                       &num_sids,
+                       &sids,
+                       &guest);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("posix_whoami failed with error %s\n", nt_errstr(status));
+               return 1;
+       }
+
+       d_printf("GUEST:%s\n", guest ? "True" : "False");
+       d_printf("UID:%" PRIu64 "\n", uid);
+       d_printf("GID:%" PRIu64 "\n", gid);
+       d_printf("NUM_GIDS:%" PRIu32 "\n", num_gids);
+       for (i = 0; i < num_gids; i++) {
+               d_printf("GIDS[%" PRIu32 "]:%" PRIu64 "\n", i, gids[i]);
+       }
+       d_printf("NUM_SIDS:%" PRIu32 "\n", num_sids);
+       for (i = 0; i < num_sids; i++) {
+               struct dom_sid_buf buf;
+               d_printf("SIDS[%" PRIu32 "]:%s\n",
+                        i,
+                        dom_sid_str_buf(&sids[i], &buf));
+       }
+       return 0;
+}
+
 
 /****************************************************************************
  Remove a directory.
@@ -2967,9 +3392,13 @@ static int cmd_rmdir(void)
        if (!mask) {
                return 1;
        }
+       mask = client_clean_name(ctx, mask);
+       if (mask == NULL) {
+               return 1;
+       }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                       cli, mask, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("rmdir %s: %s\n", mask, nt_errstr(status));
                return 1;
@@ -3011,6 +3440,10 @@ static int cmd_link(void)
        if (!oldname) {
                return 1;
        }
+       oldname = client_clean_name(ctx, oldname);
+       if (oldname == NULL) {
+               return 1;
+       }
        newname = talloc_asprintf(ctx,
                        "%s%s",
                        client_get_cur_dir(),
@@ -3018,9 +3451,13 @@ static int cmd_link(void)
        if (!newname) {
                return 1;
        }
+       newname = client_clean_name(ctx, newname);
+       if (newname == NULL) {
+               return 1;
+       }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, oldname, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                       cli, oldname, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("link %s: %s\n", oldname, nt_errstr(status));
                return 1;
@@ -3050,7 +3487,7 @@ static int cmd_readlink(void)
        char *name= NULL;
        char *buf = NULL;
        char *targetname = NULL;
-       char linkname[PATH_MAX+1];
+       char *linkname = NULL;
        struct cli_state *targetcli;
         NTSTATUS status;
 
@@ -3065,9 +3502,13 @@ static int cmd_readlink(void)
        if (!name) {
                return 1;
        }
+       name = client_clean_name(ctx, name);
+       if (name == NULL) {
+               return 1;
+       }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                       cli, name, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("readlink %s: %s\n", name, nt_errstr(status));
                return 1;
@@ -3078,7 +3519,7 @@ static int cmd_readlink(void)
                return 1;
        }
 
-       status = cli_posix_readlink(targetcli, name, linkname, PATH_MAX+1);
+       status = cli_posix_readlink(targetcli, name, talloc_tos(), &linkname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("%s readlink on file %s\n",
                         nt_errstr(status), name);
@@ -3087,6 +3528,8 @@ static int cmd_readlink(void)
 
        d_printf("%s -> %s\n", name, linkname);
 
+       TALLOC_FREE(linkname);
+
        return 0;
 }
 
@@ -3098,7 +3541,7 @@ static int cmd_readlink(void)
 static int cmd_symlink(void)
 {
        TALLOC_CTX *ctx = talloc_tos();
-       char *oldname = NULL;
+       char *link_target = NULL;
        char *newname = NULL;
        char *buf = NULL;
        char *buf2 = NULL;
@@ -3107,11 +3550,11 @@ static int cmd_symlink(void)
 
        if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
            !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
-               d_printf("symlink <oldname> <newname>\n");
+               d_printf("symlink <link_target> <newname>\n");
                return 1;
        }
        /* Oldname (link target) must be an untouched blob. */
-       oldname = buf;
+       link_target = buf;
 
        if (SERVER_HAS_UNIX_CIFS(cli)) {
                newname = talloc_asprintf(ctx, "%s%s", client_get_cur_dir(),
@@ -3119,23 +3562,29 @@ static int cmd_symlink(void)
                if (!newname) {
                        return 1;
                }
+               newname = client_clean_name(ctx, newname);
+               if (newname == NULL) {
+                       return 1;
+               }
                /* New name must be present in share namespace. */
-               status = cli_resolve_path(ctx, "", auth_info, cli, newname,
-                                         &newcli, &newname);
+               status = cli_resolve_path(ctx, "",
+                               popt_get_cmdline_auth_info(), cli, newname,
+                               &newcli, &newname);
                if (!NT_STATUS_IS_OK(status)) {
-                       d_printf("link %s: %s\n", oldname, nt_errstr(status));
+                       d_printf("link %s: %s\n", newname,
+                               nt_errstr(status));
                        return 1;
                }
-               status = cli_posix_symlink(newcli, oldname, newname);
+               status = cli_posix_symlink(newcli, link_target, newname);
        } else {
                status = cli_symlink(
-                       cli, oldname, buf2,
+                       cli, link_target, buf2,
                        buf2[0] == '\\' ? 0 : SYMLINK_FLAG_RELATIVE);
        }
 
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("%s symlinking files (%s -> %s)\n",
-                        nt_errstr(status), oldname, newname);
+                        nt_errstr(status), newname, link_target);
                return 1;
        }
 
@@ -3169,11 +3618,15 @@ static int cmd_chmod(void)
        if (!src) {
                return 1;
        }
+       src = client_clean_name(ctx, src);
+       if (src == NULL) {
+               return 1;
+       }
 
        mode = (mode_t)strtol(buf, NULL, 8);
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                       cli, src, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("chmod %s: %s\n", src, nt_errstr(status));
                return 1;
@@ -3307,14 +3760,14 @@ static int cmd_getfacl(void)
        char *name = NULL;
        char *targetname = NULL;
        struct cli_state *targetcli;
-       uint16 major, minor;
-       uint32 caplow, caphigh;
+       uint16_t major, minor;
+       uint32_t caplow, caphigh;
        char *retbuf = NULL;
        size_t rb_size = 0;
        SMB_STRUCT_STAT sbuf;
-       uint16 num_file_acls = 0;
-       uint16 num_dir_acls = 0;
-       uint16 i;
+       uint16_t num_file_acls = 0;
+       uint16_t num_dir_acls = 0;
+       uint16_t i;
        NTSTATUS status;
 
        if (!next_token_talloc(ctx, &cmd_ptr,&name,NULL)) {
@@ -3328,9 +3781,13 @@ static int cmd_getfacl(void)
        if (!src) {
                return 1;
        }
+       src = client_clean_name(ctx, src);
+       if (src == NULL) {
+               return 1;
+       }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                       cli, src, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("stat %s: %s\n", src, nt_errstr(status));
                return 1;
@@ -3362,7 +3819,7 @@ static int cmd_getfacl(void)
                return 1;
        }
 
-       status = cli_posix_getfacl(targetcli, targetname, ctx, &rb_size, &retbuf);
+       status = cli_posix_getacl(targetcli, targetname, ctx, &rb_size, &retbuf);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("%s getfacl file %s\n",
                         nt_errstr(status), src);
@@ -3394,7 +3851,7 @@ static int cmd_getfacl(void)
        }
 
        for (i = 0; i < num_file_acls; i++) {
-               uint32 uorg;
+               uint32_t uorg;
                fstring permstring;
                unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE));
                unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+(i*SMB_POSIX_ACL_ENTRY_SIZE)+1);
@@ -3431,7 +3888,7 @@ static int cmd_getfacl(void)
        }
 
        for (i = 0; i < num_dir_acls; i++) {
-               uint32 uorg;
+               uint32_t uorg;
                fstring permstring;
                unsigned char tagtype = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE));
                unsigned char perms = CVAL(retbuf, SMB_POSIX_ACL_HEADER_SIZE+((i+num_file_acls)*SMB_POSIX_ACL_ENTRY_SIZE)+1);
@@ -3496,9 +3953,13 @@ static int cmd_geteas(void)
        if (!src) {
                return 1;
        }
+       src = client_clean_name(ctx, src);
+       if (src == NULL) {
+               return 1;
+       }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                       cli, src, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("stat %s: %s\n", src, nt_errstr(status));
                return 1;
@@ -3553,9 +4014,13 @@ static int cmd_setea(void)
        if (!src) {
                return 1;
        }
+       src = client_clean_name(ctx, src);
+       if (src == NULL) {
+               return 1;
+       }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                       cli, src, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("stat %s: %s\n", src, nt_errstr(status));
                return 1;
@@ -3599,9 +4064,13 @@ static int cmd_stat(void)
        if (!src) {
                return 1;
        }
+       src = client_clean_name(ctx, src);
+       if (src == NULL) {
+               return 1;
+       }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                       cli, src, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("stat %s: %s\n", src, nt_errstr(status));
                return 1;
@@ -3708,8 +4177,12 @@ static int cmd_chown(void)
        if (!src) {
                return 1;
        }
-       status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
-                                 &targetname);
+       src = client_clean_name(ctx, src);
+       if (src == NULL) {
+               return 1;
+       }
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                       cli, src, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("chown %s: %s\n", src, nt_errstr(status));
                return 1;
@@ -3743,10 +4216,11 @@ static int cmd_rename(void)
        char *targetsrc;
        char *targetdest;
         NTSTATUS status;
+       bool replace = false;
 
        if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
            !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
-               d_printf("rename <src> <dest>\n");
+               d_printf("rename <src> <dest> [-f]\n");
                return 1;
        }
 
@@ -3757,6 +4231,10 @@ static int cmd_rename(void)
        if (!src) {
                return 1;
        }
+       src = client_clean_name(ctx, src);
+       if (src == NULL) {
+               return 1;
+       }
 
        dest = talloc_asprintf(ctx,
                        "%s%s",
@@ -3765,22 +4243,31 @@ static int cmd_rename(void)
        if (!dest) {
                return 1;
        }
+       dest = client_clean_name(ctx, dest);
+       if (dest == NULL) {
+               return 1;
+       }
+
+       if (next_token_talloc(ctx, &cmd_ptr, &buf, NULL) &&
+           strcsequal(buf, "-f")) {
+               replace = true;
+       }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
-                                 &targetsrc);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                       cli, src, &targetcli, &targetsrc);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("rename %s: %s\n", src, nt_errstr(status));
                return 1;
        }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, dest, &targetcli,
-                                 &targetdest);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                       cli, dest, &targetcli, &targetdest);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("rename %s: %s\n", dest, nt_errstr(status));
                return 1;
        }
 
-       status = cli_rename(targetcli, targetsrc, targetdest);
+       status = cli_rename(targetcli, targetsrc, targetdest, replace);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("%s renaming files %s -> %s \n",
                        nt_errstr(status),
@@ -3792,6 +4279,147 @@ static int cmd_rename(void)
        return 0;
 }
 
+struct scopy_timing {
+       struct timespec tp_start;
+};
+
+static int scopy_status(off_t written, void *priv)
+{
+       struct timespec tp_end;
+       unsigned int scopy_total_time_ms;
+       struct scopy_timing *st = priv;
+
+       clock_gettime_mono(&tp_end);
+       scopy_total_time_ms = nsec_time_diff(&tp_end,&st->tp_start)/1000000;
+
+       DEBUG(5,("Copied %jd bytes at an average %3.1f kb/s\n",
+                (intmax_t)written, written / (1.024*scopy_total_time_ms)));
+
+       return true;
+}
+
+/****************************************************************************
+ Server-Side copy some file.
+****************************************************************************/
+
+static int cmd_scopy(void)
+{
+       TALLOC_CTX *ctx = talloc_tos();
+       char *src, *dest;
+       char *buf, *buf2;
+       struct cli_state *targetcli;
+       char *targetsrc;
+       char *targetdest;
+       uint32_t DesiredAccess, ShareAccess, CreateDisposition, CreateOptions;
+       struct smb_create_returns cr;
+       uint16_t destfnum = (uint16_t)-1;
+       uint16_t srcfnum = (uint16_t)-1;
+       off_t written = 0;
+       struct scopy_timing st;
+       int rc = 0;
+       NTSTATUS status;
+
+       if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL) ||
+                       !next_token_talloc(ctx, &cmd_ptr,&buf2,NULL)) {
+               d_printf("scopy <src> <dest>\n");
+               return 1;
+       }
+
+       src = talloc_asprintf(ctx,
+                       "%s%s",
+                       client_get_cur_dir(),
+                       buf);
+       if (!src) {
+               return 1;
+       }
+       src = client_clean_name(ctx, src);
+       if (src == NULL) {
+               return 1;
+       }
+
+       dest = talloc_asprintf(ctx,
+                       "%s%s",
+                       client_get_cur_dir(),
+                       buf2);
+       if (!dest) {
+               return 1;
+       }
+       dest = client_clean_name(ctx, dest);
+       if (dest == NULL) {
+               return 1;
+       }
+
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                       cli, src, &targetcli, &targetsrc);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("scopy %s: %s\n", src, nt_errstr(status));
+               return 1;
+       }
+
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                       cli, dest, &targetcli, &targetdest);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("scopy %s: %s\n", dest, nt_errstr(status));
+               return 1;
+       }
+
+
+       DesiredAccess = (FILE_READ_DATA|FILE_READ_EA|FILE_READ_ATTRIBUTES|
+                       READ_CONTROL_ACCESS|SYNCHRONIZE_ACCESS);
+       ShareAccess = FILE_SHARE_READ|FILE_SHARE_DELETE;
+       CreateDisposition = FILE_OPEN;
+       CreateOptions = (FILE_SEQUENTIAL_ONLY|FILE_NON_DIRECTORY_FILE|
+                       FILE_OPEN_REPARSE_POINT);
+       status = cli_ntcreate(targetcli, targetsrc, 0, DesiredAccess, 0,
+                       ShareAccess, CreateDisposition, CreateOptions, 0x0,
+                       &srcfnum, &cr);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("Failed to open file %s. %s\n",
+                               targetsrc, nt_errstr(status));
+               return 1;
+       }
+
+       DesiredAccess = (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_READ_EA|
+                       FILE_WRITE_EA|FILE_READ_ATTRIBUTES|FILE_WRITE_ATTRIBUTES|
+                       DELETE_ACCESS|READ_CONTROL_ACCESS|WRITE_DAC_ACCESS|SYNCHRONIZE_ACCESS);
+       ShareAccess = FILE_SHARE_NONE;
+       CreateDisposition = FILE_CREATE;
+       CreateOptions = FILE_SEQUENTIAL_ONLY|FILE_NON_DIRECTORY_FILE;
+       status = cli_ntcreate(targetcli, targetdest, 0, DesiredAccess,
+                       FILE_ATTRIBUTE_ARCHIVE, ShareAccess, CreateDisposition,
+                       CreateOptions, 0x0, &destfnum, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("Failed to create file %s. %s\n",
+                               targetdest, nt_errstr(status));
+               cli_close(targetcli, srcfnum);
+               return 1;
+       }
+
+       clock_gettime_mono(&st.tp_start);
+       status = cli_splice(targetcli, targetcli, srcfnum, destfnum,
+                       cr.end_of_file, 0, 0, &written, scopy_status, &st);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("%s copying file %s -> %s \n",
+                               nt_errstr(status),
+                               targetsrc,
+                               targetdest);
+               rc = 1;
+       }
+
+       status = cli_close(targetcli, srcfnum);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("Error %s closing remote source file\n", nt_errstr(status));
+               rc = 1;
+       }
+       status = cli_close(targetcli, destfnum);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("Error %s closing remote dest file\n", nt_errstr(status));
+               rc = 1;
+       }
+
+       return rc;
+}
+
 /****************************************************************************
  Print the volume name.
 ****************************************************************************/
@@ -3842,6 +4470,10 @@ static int cmd_hardlink(void)
        if (!src) {
                return 1;
        }
+       src = client_clean_name(ctx, src);
+       if (src == NULL) {
+               return 1;
+       }
 
        dest = talloc_asprintf(ctx,
                        "%s%s",
@@ -3850,15 +4482,19 @@ static int cmd_hardlink(void)
        if (!dest) {
                return 1;
        }
+       dest = client_clean_name(ctx, dest);
+       if (dest == NULL) {
+               return 1;
+       }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli,
-                                 &targetname);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                               cli, src, &targetcli, &targetname);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("hardlink %s: %s\n", src, nt_errstr(status));
                return 1;
        }
 
-       status = cli_nt_hardlink(targetcli, targetname, dest);
+       status = cli_hardlink(targetcli, targetname, dest);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("%s doing an NT hard link of files\n",
                         nt_errstr(status));
@@ -3929,22 +4565,31 @@ static int cmd_notify(void)
        if (name == NULL) {
                goto fail;
        }
+       name = client_clean_name(talloc_tos(), name);
+       if (name == NULL) {
+               return 1;
+       }
        status = cli_ntcreate(
                cli, name, 0, FILE_READ_DATA, 0,
                FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
-               FILE_OPEN, 0, 0, &fnum);
+               FILE_OPEN, 0, 0, &fnum, NULL);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Could not open file: %s\n", nt_errstr(status));
                goto fail;
        }
 
        while (1) {
-               uint32_t i, num_changes;
-               struct notify_change *changes;
+               uint32_t i;
+               uint32_t num_changes = 0;
+               struct notify_change *changes = NULL;
 
                status = cli_notify(cli, fnum, 1000, FILE_NOTIFY_CHANGE_ALL,
                                    true,
                                    talloc_tos(), &num_changes, &changes);
+               if (NT_STATUS_EQUAL(status, STATUS_NOTIFY_ENUM_DIR)) {
+                       printf("NOTIFY_ENUM_DIR\n");
+                       status = NT_STATUS_OK;
+               }
                if (!NT_STATUS_IS_OK(status)) {
                        d_printf("notify returned %s\n",
                                 nt_errstr(status));
@@ -3957,7 +4602,7 @@ static int cmd_notify(void)
                TALLOC_FREE(changes);
        }
 usage:
-       d_printf("notify <file>\n");
+       d_printf("notify <dir name>\n");
 fail:
        TALLOC_FREE(frame);
        return 1;
@@ -4103,7 +4748,7 @@ static int cmd_reget(void)
        if (!remote_name) {
                return 1;
        }
-       remote_name = clean_name(ctx,remote_name);
+       remote_name = client_clean_name(ctx,remote_name);
        if (!remote_name) {
                return 1;
        }
@@ -4155,7 +4800,7 @@ static int cmd_reput(void)
                return 1;
        }
 
-       remote_name = clean_name(ctx, remote_name);
+       remote_name = client_clean_name(ctx, remote_name);
        if (!remote_name) {
                return 1;
        }
@@ -4167,7 +4812,7 @@ static int cmd_reput(void)
  List a share name.
  ****************************************************************************/
 
-static void browse_fn(const char *name, uint32 m,
+static void browse_fn(const char *name, uint32_t m,
                       const char *comment, void *state)
 {
        const char *typestr = "";
@@ -4210,7 +4855,7 @@ static bool browse_host_rpc(bool sort)
        int i;
        struct dcerpc_binding_handle *b;
 
-       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
+       status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc,
                                          &pipe_hnd);
 
        if (!NT_STATUS_IS_OK(status)) {
@@ -4259,6 +4904,7 @@ static bool browse_host_rpc(bool sort)
 static bool browse_host(bool sort)
 {
        int ret;
+
        if (!grepable) {
                d_printf("\n\tSharename       Type      Comment\n");
                d_printf("\t---------       ----      -------\n");
@@ -4268,7 +4914,12 @@ static bool browse_host(bool sort)
                return true;
        }
 
-       if((ret = cli_RNetShareEnum(cli, browse_fn, NULL)) == -1) {
+       if (lp_client_min_protocol() > PROTOCOL_NT1) {
+               return false;
+       }
+
+       ret = cli_RNetShareEnum(cli, browse_fn, NULL);
+       if (ret == -1) {
                NTSTATUS status = cli_nt_error(cli);
                d_printf("Error returning browse list: %s\n",
                         nt_errstr(status));
@@ -4281,7 +4932,7 @@ static bool browse_host(bool sort)
  List a server name.
 ****************************************************************************/
 
-static void server_fn(const char *name, uint32 m,
+static void server_fn(const char *name, uint32_t m,
                       const char *comment, void *state)
 {
 
@@ -4349,6 +5000,7 @@ static int cmd_logon(void)
 {
        TALLOC_CTX *ctx = talloc_tos();
        char *l_username, *l_password;
+       struct cli_credentials *creds = NULL;
        NTSTATUS nt_status;
 
        if (!next_token_talloc(ctx, &cmd_ptr,&l_username,NULL)) {
@@ -4369,10 +5021,21 @@ static int cmd_logon(void)
                return 1;
        }
 
-       nt_status = cli_session_setup(cli, l_username,
-                                     l_password, strlen(l_password),
-                                     l_password, strlen(l_password),
-                                     lp_workgroup());
+       creds = cli_session_creds_init(ctx,
+                                      l_username,
+                                      lp_workgroup(),
+                                      NULL, /* realm */
+                                      l_password,
+                                      false, /* use_kerberos */
+                                      false, /* fallback_after_kerberos */
+                                      false, /* use_ccache */
+                                      false); /* password_is_nt_hash */
+       if (creds == NULL) {
+               d_printf("cli_session_creds_init() failed.\n");
+               return -1;
+       }
+       nt_status = cli_session_setup_creds(cli, creds);
+       TALLOC_FREE(creds);
        if (!NT_STATUS_IS_OK(nt_status)) {
                d_printf("session setup failed: %s\n", nt_errstr(nt_status));
                return -1;
@@ -4420,7 +5083,7 @@ static int cmd_tcon(void)
                return 1;
        }
 
-       status = cli_tree_connect(cli, sharename, "?????", "", 0);
+       status = cli_tree_connect(cli, sharename, "?????", NULL);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("tcon failed: %s\n", nt_errstr(status));
                return -1;
@@ -4468,7 +5131,10 @@ static int cmd_tid(void)
                        d_printf("no tcon currently\n");
                }
        } else {
-               uint16_t tid = atoi(tid_str);
+               uint32_t tid = atoi(tid_str);
+               if (!cli_state_has_tcon(cli)) {
+                       d_printf("no tcon currently\n");
+               }
                cli_state_set_tid(cli, tid);
        }
 
@@ -4497,7 +5163,7 @@ static int cmd_show_connect( void )
        char *targetpath;
        NTSTATUS status;
 
-       status = cli_resolve_path(ctx, "", auth_info, cli,
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(), cli,
                                  client_get_cur_dir(), &targetcli,
                                  &targetpath);
        if (!NT_STATUS_IS_OK(status)) {
@@ -4509,6 +5175,254 @@ static int cmd_show_connect( void )
        return 0;
 }
 
+/**
+ * set_remote_times - set times of a remote file
+ * @filename: path to the file name
+ * @create_time: New create time
+ * @access_time: New access time
+ * @write_time: New write time
+ * @change_time: New metadata change time
+ *
+ * Update the file times with the ones provided.
+ */
+static int set_remote_times(const char *filename, time_t create_time,
+                       time_t access_time, time_t write_time,
+                       time_t change_time)
+{
+       extern struct cli_state *cli;
+       NTSTATUS status;
+
+       status = cli_setpathinfo_basic(cli, filename, create_time,
+                                       access_time, write_time,
+                                       change_time, -1);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("cli_setpathinfo_basic failed: %s\n",
+                        nt_errstr(status));
+               return 1;
+       }
+
+       return 0;
+}
+
+/**
+ * cmd_utimes - interactive command to set the four times
+ *
+ * Read a filename and four times from the client command line and update
+ * the file times. A value of -1 for a time means don't change.
+ */
+static int cmd_utimes(void)
+{
+       const extern char *cmd_ptr;
+       char *buf;
+       char *fname = NULL;
+       time_t times[4] = {0, 0, 0, 0};
+       int time_count = 0;
+       int err = 0;
+       bool ok;
+       TALLOC_CTX *ctx = talloc_new(NULL);
+       if (ctx == NULL) {
+               return 1;
+       }
+
+       ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
+       if (!ok) {
+               d_printf("utimes <filename> <create-time> <access-time> "
+                        "<write-time> <change-time>\n");
+               d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
+                       "or -1 for no change\n");
+               err = 1;
+               goto out;
+       }
+
+       fname = talloc_asprintf(ctx,
+                               "%s%s",
+                               client_get_cur_dir(),
+                               buf);
+       if (fname == NULL) {
+               err = 1;
+               goto out;
+       }
+       fname = client_clean_name(ctx, fname);
+       if (fname == NULL) {
+               err = 1;
+               goto out;
+       }
+
+       while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL) &&
+               time_count < 4) {
+               const char *s = buf;
+               struct tm tm = {0,};
+               char *ret;
+
+               if (strlen(s) == 2 && strcmp(s, "-1") == 0) {
+                       times[time_count] = 0;
+                       time_count++;
+                       continue;
+               } else {
+                       ret = strptime(s, "%y:%m:%d-%H:%M:%S", &tm);
+               }
+
+               /* We could not match all the chars, so print error */
+               if (ret == NULL || *ret != 0) {
+                       d_printf("Invalid date format: %s\n", s);
+                       d_printf("utimes <filename> <create-time> "
+                               "<access-time> <write-time> <change-time>\n");
+                       d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
+                               "or -1 for no change\n");
+                       err = 1;
+                       goto out;
+               }
+
+               /* Convert tm to a time_t */
+               times[time_count] = mktime(&tm);
+               time_count++;
+       }
+
+       if (time_count < 4) {
+               d_printf("Insufficient dates: %d\n", time_count);
+               d_printf("utimes <filename> <create-time> <access-time> "
+                       "<write-time> <change-time>\n");
+               d_printf("Dates should be in YY:MM:DD-HH:MM:SS format "
+                       "or -1 for no change\n");
+               err = 1;
+               goto out;
+       }
+
+       DEBUG(10, ("times\nCreate: %sAccess: %s Write: %sChange: %s\n",
+               talloc_strdup(ctx, ctime(&times[0])),
+               talloc_strdup(ctx, ctime(&times[1])),
+               talloc_strdup(ctx, ctime(&times[2])),
+               talloc_strdup(ctx, ctime(&times[3]))));
+
+       set_remote_times(fname, times[0], times[1], times[2], times[3]);
+out:
+       talloc_free(ctx);
+       return err;
+}
+
+/**
+ * set_remote_attr - set DOS attributes of a remote file
+ * @filename: path to the file name
+ * @new_attr: attribute bit mask to use
+ * @mode: one of ATTR_SET or ATTR_UNSET
+ *
+ * Update the file attributes with the one provided.
+ */
+int set_remote_attr(const char *filename, uint16_t new_attr, int mode)
+{
+       extern struct cli_state *cli;
+       uint16_t old_attr;
+       NTSTATUS status;
+
+       status = cli_getatr(cli, filename, &old_attr, NULL, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("cli_getatr failed: %s\n", nt_errstr(status));
+               return 1;
+       }
+
+       if (mode == ATTR_SET) {
+               new_attr |= old_attr;
+       } else {
+               new_attr = old_attr & ~new_attr;
+       }
+
+       status = cli_setatr(cli, filename, new_attr, 0);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("cli_setatr failed: %s\n", nt_errstr(status));
+               return 1;
+       }
+
+       return 0;
+}
+
+/**
+ * cmd_setmode - interactive command to set DOS attributes
+ *
+ * Read a filename and mode from the client command line and update
+ * the file DOS attributes.
+ */
+int cmd_setmode(void)
+{
+       const extern char *cmd_ptr;
+       char *buf;
+       char *fname = NULL;
+       uint16_t attr[2] = {0};
+       int mode = ATTR_SET;
+       int err = 0;
+       bool ok;
+       TALLOC_CTX *ctx = talloc_new(NULL);
+       if (ctx == NULL) {
+               return 1;
+       }
+
+       ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
+       if (!ok) {
+               d_printf("setmode <filename> <[+|-]rsha>\n");
+               err = 1;
+               goto out;
+       }
+
+       fname = talloc_asprintf(ctx,
+                               "%s%s",
+                               client_get_cur_dir(),
+                               buf);
+       if (fname == NULL) {
+               err = 1;
+               goto out;
+       }
+       fname = client_clean_name(ctx, fname);
+       if (fname == NULL) {
+               err = 1;
+               goto out;
+       }
+
+       while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
+               const char *s = buf;
+
+               while (*s) {
+                       switch (*s++) {
+                       case '+':
+                               mode = ATTR_SET;
+                               break;
+                       case '-':
+                               mode = ATTR_UNSET;
+                               break;
+                       case 'r':
+                               attr[mode] |= FILE_ATTRIBUTE_READONLY;
+                               break;
+                       case 'h':
+                               attr[mode] |= FILE_ATTRIBUTE_HIDDEN;
+                               break;
+                       case 's':
+                               attr[mode] |= FILE_ATTRIBUTE_SYSTEM;
+                               break;
+                       case 'a':
+                               attr[mode] |= FILE_ATTRIBUTE_ARCHIVE;
+                               break;
+                       default:
+                               d_printf("setmode <filename> <perm=[+|-]rsha>\n");
+                               err = 1;
+                               goto out;
+                       }
+               }
+       }
+
+       if (attr[ATTR_SET] == 0 && attr[ATTR_UNSET] == 0) {
+               d_printf("setmode <filename> <[+|-]rsha>\n");
+               err = 1;
+               goto out;
+       }
+
+       DEBUG(2, ("perm set %d %d\n", attr[ATTR_SET], attr[ATTR_UNSET]));
+
+       /* ignore return value: server might not store DOS attributes */
+       set_remote_attr(fname, attr[ATTR_SET], ATTR_SET);
+       set_remote_attr(fname, attr[ATTR_UNSET], ATTR_UNSET);
+out:
+       talloc_free(ctx);
+       return err;
+}
+
 /****************************************************************************
  iosize command
 ***************************************************************************/
@@ -4520,29 +5434,35 @@ int cmd_iosize(void)
        int iosize;
 
        if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
-               if (!smb_encrypt) {
-                       d_printf("iosize <n> or iosize 0x<n>. "
-                               "Minimum is 16384 (0x4000), "
-                               "max is 16776960 (0xFFFF00)\n");
+               if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
+                       if (!smb_encrypt) {
+                               d_printf("iosize <n> or iosize 0x<n>. "
+                                       "Minimum is 0 (default), "
+                                       "max is 16776960 (0xFFFF00)\n");
+                       } else {
+                               d_printf("iosize <n> or iosize 0x<n>. "
+                                       "(Encrypted connection) ,"
+                                       "Minimum is 0 (default), "
+                                       "max is 130048 (0x1FC00)\n");
+                       }
                } else {
-                       d_printf("iosize <n> or iosize 0x<n>. "
-                               "(Encrypted connection) ,"
-                               "Minimum is 16384 (0x4000), "
-                               "max is 130048 (0x1FC00)\n");
+                       d_printf("iosize <n> or iosize 0x<n>.\n");
                }
                return 1;
        }
 
        iosize = strtol(buf,NULL,0);
-       if (smb_encrypt && (iosize < 0x4000 || iosize > 0xFC00)) {
-               d_printf("iosize out of range for encrypted "
-                       "connection (min = 16384 (0x4000), "
-                       "max = 130048 (0x1FC00)");
-               return 1;
-       } else if (!smb_encrypt && (iosize < 0x4000 || iosize > 0xFFFF00)) {
-               d_printf("iosize out of range (min = 16384 (0x4000), "
-                       "max = 16776960 (0xFFFF00)");
-               return 1;
+       if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
+               if (smb_encrypt && (iosize < 0 || iosize > 0xFC00)) {
+                       d_printf("iosize out of range for encrypted "
+                               "connection (min = 0 (default), "
+                               "max = 130048 (0x1FC00)\n");
+                       return 1;
+               } else if (!smb_encrypt && (iosize < 0 || iosize > 0xFFFF00)) {
+                       d_printf("iosize out of range (min = 0 (default), "
+                               "max = 16776960 (0xFFFF00)\n");
+                       return 1;
+               }
        }
 
        io_bufsize = iosize;
@@ -4550,6 +5470,31 @@ int cmd_iosize(void)
        return 0;
 }
 
+/****************************************************************************
+ timeout command
+***************************************************************************/
+
+static int cmd_timeout(void)
+{
+       TALLOC_CTX *ctx = talloc_tos();
+       char *buf;
+
+       if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
+               unsigned int old_timeout = cli_set_timeout(cli, 0);
+               cli_set_timeout(cli, old_timeout);
+               d_printf("timeout <n> (per-operation timeout "
+                       "in seconds - currently %u).\n",
+                       old_timeout/1000);
+               return 1;
+       }
+
+       io_timeout = strtol(buf,NULL,0);
+       cli_set_timeout(cli, io_timeout*1000);
+       d_printf("io_timeout per operation is now %d\n", io_timeout);
+       return 0;
+}
+
+
 /****************************************************************************
 history
 ****************************************************************************/
@@ -4590,26 +5535,27 @@ static struct {
 } commands[] = {
   {"?",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
   {"allinfo",cmd_allinfo,"<file> show all available info",
-   {COMPL_NONE,COMPL_NONE}},
-  {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,COMPL_NONE}},
+   {COMPL_REMOTE,COMPL_NONE}},
+  {"altname",cmd_altname,"<file> show alt name",{COMPL_REMOTE,COMPL_NONE}},
   {"archive",cmd_archive,"<level>\n0=ignore archive bit\n1=only get archive files\n2=only get archive files and reset archive bit\n3=get all files and reset archive bit",{COMPL_NONE,COMPL_NONE}},
-  {"backup",cmd_backup,"toggle backup intent state",{COMPL_NONE,COMPL_NONE}},  
+  {"backup",cmd_backup,"toggle backup intent state",{COMPL_NONE,COMPL_NONE}},
   {"blocksize",cmd_block,"blocksize <number> (default 20)",{COMPL_NONE,COMPL_NONE}},
   {"cancel",cmd_cancel,"<jobid> cancel a print queue entry",{COMPL_NONE,COMPL_NONE}},
   {"case_sensitive",cmd_setcase,"toggle the case sensitive flag to server",{COMPL_NONE,COMPL_NONE}},
   {"cd",cmd_cd,"[directory] change/report the remote directory",{COMPL_REMOTE,COMPL_NONE}},
-  {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_REMOTE}},
-  {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_REMOTE}},
-  {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_REMOTE}},
+  {"chmod",cmd_chmod,"<src> <mode> chmod a file using UNIX permission",{COMPL_REMOTE,COMPL_NONE}},
+  {"chown",cmd_chown,"<src> <uid> <gid> chown a file using UNIX uids and gids",{COMPL_REMOTE,COMPL_NONE}},
+  {"close",cmd_close,"<fid> close a file given a fid",{COMPL_REMOTE,COMPL_NONE}},
   {"del",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
+  {"deltree",cmd_deltree,"<mask> recursively delete all matching files and directories",{COMPL_REMOTE,COMPL_NONE}},
   {"dir",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
   {"du",cmd_du,"<mask> computes the total size of the current directory",{COMPL_REMOTE,COMPL_NONE}},
   {"echo",cmd_echo,"ping the server",{COMPL_NONE,COMPL_NONE}},
   {"exit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
   {"get",cmd_get,"<remote name> [local name] get a file",{COMPL_REMOTE,COMPL_LOCAL}},
-  {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_LOCAL}},
+  {"getfacl",cmd_getfacl,"<file name> get the POSIX ACL on a file (UNIX extensions only)",{COMPL_REMOTE,COMPL_NONE}},
   {"geteas", cmd_geteas, "<file name> get the EA list of a file",
-   {COMPL_REMOTE, COMPL_LOCAL}},
+   {COMPL_REMOTE, COMPL_NONE}},
   {"hardlink",cmd_hardlink,"<src> <dest> create a Windows hard link",{COMPL_REMOTE,COMPL_REMOTE}},
   {"help",cmd_help,"[command] give help on a command",{COMPL_NONE,COMPL_NONE}},
   {"history",cmd_history,"displays the command history",{COMPL_NONE,COMPL_NONE}},
@@ -4617,14 +5563,14 @@ static struct {
   {"lcd",cmd_lcd,"[directory] change/report the local current working directory",{COMPL_LOCAL,COMPL_NONE}},
   {"link",cmd_link,"<oldname> <newname> create a UNIX hard link",{COMPL_REMOTE,COMPL_REMOTE}},
   {"lock",cmd_lock,"lock <fnum> [r|w] <hex-start> <hex-len> : set a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
-  {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},  
+  {"lowercase",cmd_lowercase,"toggle lowercasing of filenames for get",{COMPL_NONE,COMPL_NONE}},
   {"ls",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
   {"l",cmd_dir,"<mask> list the contents of the current directory",{COMPL_REMOTE,COMPL_NONE}},
   {"mask",cmd_select,"<mask> mask all filenames against this",{COMPL_REMOTE,COMPL_NONE}},
   {"md",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
   {"mget",cmd_mget,"<mask> get all the matching files",{COMPL_REMOTE,COMPL_NONE}},
   {"mkdir",cmd_mkdir,"<directory> make a directory",{COMPL_NONE,COMPL_NONE}},
-  {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},  
+  {"more",cmd_more,"<remote name> view a remote file with your pager",{COMPL_REMOTE,COMPL_NONE}},
   {"mput",cmd_mput,"<mask> put all matching files",{COMPL_REMOTE,COMPL_NONE}},
   {"newer",cmd_newer,"<file> only mget files newer than the specified local file",{COMPL_LOCAL,COMPL_NONE}},
   {"notify",cmd_notify,"<file>Get notified of dir changes",{COMPL_REMOTE,COMPL_NONE}},
@@ -4635,8 +5581,10 @@ static struct {
   {"posix_mkdir",cmd_posix_mkdir,"<name> 0<mode> creates a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
   {"posix_rmdir",cmd_posix_rmdir,"<name> removes a directory using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
   {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
+  {"posix_whoami",cmd_posix_whoami,"return logged on user information "
+                       "using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
   {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
-  {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},  
+  {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
   {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
   {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
   {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
@@ -4644,20 +5592,22 @@ static struct {
   {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
   {"readlink",cmd_readlink,"filename Do a UNIX extensions readlink call on a symlink",{COMPL_REMOTE,COMPL_REMOTE}},
   {"rd",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
-  {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},  
+  {"recurse",cmd_recurse,"toggle directory recursion for mget and mput",{COMPL_NONE,COMPL_NONE}},
   {"reget",cmd_reget,"<remote name> [local name] get a file restarting at end of local file",{COMPL_REMOTE,COMPL_LOCAL}},
   {"rename",cmd_rename,"<src> <dest> rename some files",{COMPL_REMOTE,COMPL_REMOTE}},
   {"reput",cmd_reput,"<local name> [remote name] put a file restarting at end of remote file",{COMPL_LOCAL,COMPL_REMOTE}},
   {"rm",cmd_del,"<mask> delete all matching files",{COMPL_REMOTE,COMPL_NONE}},
-  {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_NONE,COMPL_NONE}},
-  {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},  
+  {"rmdir",cmd_rmdir,"<directory> remove a directory",{COMPL_REMOTE,COMPL_NONE}},
+  {"showacls",cmd_showacls,"toggle if ACLs are shown or not",{COMPL_NONE,COMPL_NONE}},
   {"setea", cmd_setea, "<file name> <eaname> <eaval> Set an EA of a file",
    {COMPL_REMOTE, COMPL_LOCAL}},
-  {"setmode",cmd_setmode,"filename <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
-  {"stat",cmd_stat,"filename Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_REMOTE}},
+  {"setmode",cmd_setmode,"<file name> <setmode string> change modes of file",{COMPL_REMOTE,COMPL_NONE}},
+  {"scopy",cmd_scopy,"<src> <dest> server-side copy file",{COMPL_REMOTE,COMPL_REMOTE}},
+  {"stat",cmd_stat,"<file name> Do a UNIX extensions stat call on a file",{COMPL_REMOTE,COMPL_NONE}},
   {"symlink",cmd_symlink,"<oldname> <newname> create a UNIX symlink",{COMPL_REMOTE,COMPL_REMOTE}},
   {"tar",cmd_tar,"tar <c|x>[IXFqbgNan] current directory to/from <file name>",{COMPL_NONE,COMPL_NONE}},
   {"tarmode",cmd_tarmode,"<full|inc|reset|noreset> tar's behaviour towards archive bits",{COMPL_NONE,COMPL_NONE}},
+  {"timeout",cmd_timeout,"timeout <number> - set the per-operation timeout in seconds (default 20)",{COMPL_NONE,COMPL_NONE}},
   {"translate",cmd_translate,"toggle text translation for printing",{COMPL_NONE,COMPL_NONE}},
   {"unlock",cmd_unlock,"unlock <fnum> <hex-start> <hex-len> : remove a POSIX lock",{COMPL_REMOTE,COMPL_REMOTE}},
   {"volume",cmd_volume,"print the volume name",{COMPL_NONE,COMPL_NONE}},
@@ -4669,6 +5619,8 @@ static struct {
   {"tcon",cmd_tcon,"connect to a share" ,{COMPL_NONE,COMPL_NONE}},
   {"tdis",cmd_tdis,"disconnect from a share",{COMPL_NONE,COMPL_NONE}},
   {"tid",cmd_tid,"show or set the current tid (tree-id)",{COMPL_NONE,COMPL_NONE}},
+  {"utimes", cmd_utimes,"<file name> <create_time> <access_time> <mod_time> "
+       "<ctime> set times", {COMPL_REMOTE,COMPL_NONE}},
   {"logoff",cmd_logoff,"log off (close the session)",{COMPL_NONE,COMPL_NONE}},
   {"..",cmd_cd_oneup,"change the remote directory (up one level)",{COMPL_REMOTE,COMPL_NONE}},
 
@@ -4684,9 +5636,9 @@ static struct {
 
 static int process_tok(char *tok)
 {
-       int i = 0, matches = 0;
-       int cmd=0;
-       int tok_len = strlen(tok);
+       size_t i = 0, matches = 0;
+       size_t cmd=0;
+       size_t tok_len = strlen(tok);
 
        while (commands[i].fn != NULL) {
                if (strequal(commands[i].name,tok)) {
@@ -4754,13 +5706,14 @@ static int process_command_string(const char *cmd_in)
 
                status = cli_cm_open(talloc_tos(), NULL,
                                     have_ip ? dest_ss_str : desthost,
-                                    service, auth_info,
-                                    true, smb_encrypt,
+                                    service, popt_get_cmdline_auth_info(),
+                                    smb_encrypt,
                                     max_protocol, port, name_type,
                                     &cli);
                if (!NT_STATUS_IS_OK(status)) {
                        return 1;
                }
+               cli_set_timeout(cli, io_timeout*1000);
        }
 
        while (cmd[0] != '\0')    {
@@ -4932,9 +5885,13 @@ static char **remote_completion(const char *text, int len)
        if (!dirmask) {
                goto cleanup;
        }
+       dirmask = client_clean_name(ctx, dirmask);
+       if (dirmask == NULL) {
+               goto cleanup;
+       }
 
-       status = cli_resolve_path(ctx, "", auth_info, cli, dirmask, &targetcli,
-                                 &targetpath);
+       status = cli_resolve_path(ctx, "", popt_get_cmdline_auth_info(),
+                               cli, dirmask, &targetcli, &targetpath);
        if (!NT_STATUS_IS_OK(status)) {
                goto cleanup;
        }
@@ -5026,7 +5983,7 @@ static char **completion_fn(const char *text, int start, int end)
                        return NULL;
        } else {
                char **matches;
-               int i, len, samelen = 0, count=1;
+               size_t i, len, samelen = 0, count=1;
 
                matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
                if (!matches) {
@@ -5100,7 +6057,13 @@ static void readline_callback(void)
        /* Ping the server to keep the connection alive using SMBecho. */
        memset(garbage, 0xf0, sizeof(garbage));
        status = cli_echo(cli, 1, data_blob_const(garbage, sizeof(garbage)));
-       if (NT_STATUS_IS_OK(status)) {
+       if (NT_STATUS_IS_OK(status) ||
+                       NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
+               /*
+                * Even if server returns NT_STATUS_INVALID_PARAMETER
+                * it still responded.
+                * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13007
+                */
                return;
        }
 
@@ -5120,6 +6083,10 @@ static int process_stdin(void)
 {
        int rc = 0;
 
+       if (!quiet) {
+               d_printf("Try \"help\" to get a list of possible commands.\n");
+       }
+
        while (!finished) {
                TALLOC_CTX *frame = talloc_stackframe();
                char *tok = NULL;
@@ -5182,12 +6149,15 @@ static int process(const char *base_directory)
 
        status = cli_cm_open(talloc_tos(), NULL,
                             have_ip ? dest_ss_str : desthost,
-                            service, auth_info, true, smb_encrypt,
-                            max_protocol, port, name_type, &cli);
+                            service, popt_get_cmdline_auth_info(),
+                            smb_encrypt, max_protocol, port,
+                            name_type, &cli);
        if (!NT_STATUS_IS_OK(status)) {
                return 1;
        }
 
+       cli_set_timeout(cli, io_timeout*1000);
+
        if (base_directory && *base_directory) {
                rc = do_cd(base_directory);
                if (rc) {
@@ -5216,12 +6186,14 @@ static int do_host_query(const char *query_host)
 
        status = cli_cm_open(talloc_tos(), NULL,
                             have_ip ? dest_ss_str : query_host,
-                            "IPC$", auth_info, true, smb_encrypt,
-                            max_protocol, port, name_type, &cli);
+                            "IPC$", popt_get_cmdline_auth_info(),
+                            smb_encrypt, max_protocol, port,
+                            name_type, &cli);
        if (!NT_STATUS_IS_OK(status)) {
                return 1;
        }
 
+       cli_set_timeout(cli, io_timeout*1000);
        browse_host(true);
 
        /* Ensure that the host can do IPv4 */
@@ -5236,29 +6208,43 @@ static int do_host_query(const char *query_host)
                }
        }
 
-       if (port != NBT_SMB_PORT) {
+       if (lp_client_min_protocol() > PROTOCOL_NT1) {
+               d_printf("SMB1 disabled -- no workgroup available\n");
+               goto out;
+       }
+
+       if (lp_disable_netbios()) {
+               d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
+               goto out;
+       }
+
+       if (port != NBT_SMB_PORT ||
+           smbXcli_conn_protocol(cli->conn) > PROTOCOL_NT1)
+       {
+               int max_proto = MIN(max_protocol, PROTOCOL_NT1);
 
-               /* Workgroups simply don't make sense over anything
-                  else but port 139... */
+               /*
+                * Workgroups simply don't make sense over anything
+                * else but port 139 and SMB1.
+                */
 
                cli_shutdown(cli);
+               d_printf("Reconnecting with SMB1 for workgroup listing.\n");
                status = cli_cm_open(talloc_tos(), NULL,
                                     have_ip ? dest_ss_str : query_host,
-                                    "IPC$", auth_info, true, smb_encrypt,
-                                    max_protocol, NBT_SMB_PORT, name_type,
-                                    &cli);
+                                    "IPC$", popt_get_cmdline_auth_info(),
+                                    smb_encrypt, max_proto,
+                                    NBT_SMB_PORT, name_type, &cli);
                if (!NT_STATUS_IS_OK(status)) {
-                       cli = NULL;
+                       d_printf("Unable to connect with SMB1 "
+                                "-- no workgroup available\n");
+                       return 0;
                }
        }
 
-       if (cli == NULL) {
-               d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
-               return 1;
-       }
-
+       cli_set_timeout(cli, io_timeout*1000);
        list_servers(lp_workgroup());
-
+out:
        cli_shutdown(cli);
 
        return(0);
@@ -5270,7 +6256,8 @@ static int do_host_query(const char *query_host)
 
 static int do_tar_op(const char *base_directory)
 {
-       int ret;
+       struct tar *tar_ctx = tar_get_ctx();
+       int ret = 0;
 
        /* do we already have a connection? */
        if (!cli) {
@@ -5278,28 +6265,31 @@ static int do_tar_op(const char *base_directory)
 
                status = cli_cm_open(talloc_tos(), NULL,
                                     have_ip ? dest_ss_str : desthost,
-                                    service, auth_info, true, smb_encrypt,
-                                    max_protocol, port, name_type, &cli);
+                                    service, popt_get_cmdline_auth_info(),
+                                    smb_encrypt, max_protocol,
+                                    port, name_type, &cli);
                if (!NT_STATUS_IS_OK(status)) {
-                       return 1;
+            ret = 1;
+            goto out;
                }
+               cli_set_timeout(cli, io_timeout*1000);
        }
 
-       recurse=true;
+       recurse = true;
 
        if (base_directory && *base_directory)  {
                ret = do_cd(base_directory);
                if (ret) {
-                       cli_shutdown(cli);
-                       return ret;
+            goto out_cli;
                }
        }
 
-       ret=process_tar();
+       ret = tar_process(tar_ctx);
 
+ out_cli:
        cli_shutdown(cli);
-
-       return(ret);
+ out:
+       return ret;
 }
 
 /****************************************************************************
@@ -5310,6 +6300,11 @@ static int do_message_op(struct user_auth_info *a_info)
 {
        NTSTATUS status;
 
+       if (lp_disable_netbios()) {
+               d_printf("NetBIOS over TCP disabled.\n");
+               return 1;
+       }
+
        status = cli_connect_nb(desthost, have_ip ? &dest_ss : NULL,
                                port ? port : NBT_SMB_PORT, name_type,
                                lp_netbios_name(), SMB_SIGNING_DEFAULT, 0, &cli);
@@ -5318,6 +6313,7 @@ static int do_message_op(struct user_auth_info *a_info)
                return 1;
        }
 
+       cli_set_timeout(cli, io_timeout*1000);
        send_message(get_cmdline_auth_info_username(a_info));
        cli_shutdown(cli);
 
@@ -5328,8 +6324,9 @@ static int do_message_op(struct user_auth_info *a_info)
   main program
 ****************************************************************************/
 
- int main(int argc,char *argv[])
+int main(int argc,char *argv[])
 {
+       const char **const_argv = discard_const_p(const char *, argv);
        char *base_directory = NULL;
        int opt;
        char *query_host = NULL;
@@ -5340,22 +6337,141 @@ static int do_message_op(struct user_auth_info *a_info)
        int rc = 0;
        bool tar_opt = false;
        bool service_opt = false;
+       struct tar *tar_ctx = tar_get_ctx();
+
        struct poptOption long_options[] = {
                POPT_AUTOHELP
 
-               { "name-resolve", 'R', POPT_ARG_STRING, &new_name_resolve_order, 'R', "Use these name resolution services only", "NAME-RESOLVE-ORDER" },
-               { "message", 'M', POPT_ARG_STRING, NULL, 'M', "Send message", "HOST" },
-               { "ip-address", 'I', POPT_ARG_STRING, NULL, 'I', "Use this IP to connect to", "IP" },
-               { "stderr", 'E', POPT_ARG_NONE, NULL, 'E', "Write messages to stderr instead of stdout" },
-               { "list", 'L', POPT_ARG_STRING, NULL, 'L', "Get a list of shares available on a host", "HOST" },
-               { "max-protocol", 'm', POPT_ARG_STRING, NULL, 'm', "Set the max protocol level", "LEVEL" },
-               { "tar", 'T', POPT_ARG_STRING, NULL, 'T', "Command line tar", "<c|x>IXFqgbNan" },
-               { "directory", 'D', POPT_ARG_STRING, NULL, 'D', "Start from directory", "DIR" },
-               { "command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated commands" }, 
-               { "send-buffer", 'b', POPT_ARG_INT, &io_bufsize, 'b', "Changes the transmit/send buffer", "BYTES" },
-               { "port", 'p', POPT_ARG_INT, &port, 'p', "Port to connect to", "PORT" },
-               { "grepable", 'g', POPT_ARG_NONE, NULL, 'g', "Produce grepable output" },
-                { "browse", 'B', POPT_ARG_NONE, NULL, 'B', "Browse SMB servers using DNS" },
+               {
+                       .longName   = "name-resolve",
+                       .shortName  = 'R',
+                       .argInfo    = POPT_ARG_STRING,
+                       .arg        = &new_name_resolve_order,
+                       .val        = 'R',
+                       .descrip    = "Use these name resolution services only",
+                       .argDescrip = "NAME-RESOLVE-ORDER",
+               },
+               {
+                       .longName   = "message",
+                       .shortName  = 'M',
+                       .argInfo    = POPT_ARG_STRING,
+                       .arg        = NULL,
+                       .val        = 'M',
+                       .descrip    = "Send message",
+                       .argDescrip = "HOST",
+               },
+               {
+                       .longName   = "ip-address",
+                       .shortName  = 'I',
+                       .argInfo    = POPT_ARG_STRING,
+                       .arg        = NULL,
+                       .val        = 'I',
+                       .descrip    = "Use this IP to connect to",
+                       .argDescrip = "IP",
+               },
+               {
+                       .longName   = "stderr",
+                       .shortName  = 'E',
+                       .argInfo    = POPT_ARG_NONE,
+                       .arg        = NULL,
+                       .val        = 'E',
+                       .descrip    = "Write messages to stderr instead of stdout",
+               },
+               {
+                       .longName   = "list",
+                       .shortName  = 'L',
+                       .argInfo    = POPT_ARG_STRING,
+                       .arg        = NULL,
+                       .val        = 'L',
+                       .descrip    = "Get a list of shares available on a host",
+                       .argDescrip = "HOST",
+               },
+               {
+                       .longName   = "max-protocol",
+                       .shortName  = 'm',
+                       .argInfo    = POPT_ARG_STRING,
+                       .arg        = NULL,
+                       .val        = 'm',
+                       .descrip    = "Set the max protocol level",
+                       .argDescrip = "LEVEL",
+               },
+               {
+                       .longName   = "tar",
+                       .shortName  = 'T',
+                       .argInfo    = POPT_ARG_STRING,
+                       .arg        = NULL,
+                       .val        = 'T',
+                       .descrip    = "Command line tar",
+                       .argDescrip = "<c|x>IXFqgbNan",
+               },
+               {
+                       .longName   = "directory",
+                       .shortName  = 'D',
+                       .argInfo    = POPT_ARG_STRING,
+                       .arg        = NULL,
+                       .val        = 'D',
+                       .descrip    = "Start from directory",
+                       .argDescrip = "DIR",
+               },
+               {
+                       .longName   = "command",
+                       .shortName  = 'c',
+                       .argInfo    = POPT_ARG_STRING,
+                       .arg        = &cmdstr,
+                       .val        = 'c',
+                       .descrip    = "Execute semicolon separated commands",
+               },
+               {
+                       .longName   = "send-buffer",
+                       .shortName  = 'b',
+                       .argInfo    = POPT_ARG_INT,
+                       .arg        = &io_bufsize,
+                       .val        = 'b',
+                       .descrip    = "Changes the transmit/send buffer",
+                       .argDescrip = "BYTES",
+               },
+               {
+                       .longName   = "timeout",
+                       .shortName  = 't',
+                       .argInfo    = POPT_ARG_INT,
+                       .arg        = &io_timeout,
+                       .val        = 'b',
+                       .descrip    = "Changes the per-operation timeout",
+                       .argDescrip = "SECONDS",
+               },
+               {
+                       .longName   = "port",
+                       .shortName  = 'p',
+                       .argInfo    = POPT_ARG_INT,
+                       .arg        = &port,
+                       .val        = 'p',
+                       .descrip    = "Port to connect to",
+                       .argDescrip = "PORT",
+               },
+               {
+                       .longName   = "grepable",
+                       .shortName  = 'g',
+                       .argInfo    = POPT_ARG_NONE,
+                       .arg        = NULL,
+                       .val        = 'g',
+                       .descrip    = "Produce grepable output",
+               },
+               {
+                       .longName   = "quiet",
+                       .shortName  = 'q',
+                       .argInfo    = POPT_ARG_NONE,
+                       .arg        = NULL,
+                       .val        = 'q',
+                       .descrip    = "Suppress help message",
+               },
+               {
+                       .longName   = "browse",
+                       .shortName  = 'B',
+                       .argInfo    = POPT_ARG_NONE,
+                       .arg        = NULL,
+                       .val        = 'B',
+                       .descrip    = "Browse SMB servers using DNS",
+               },
                POPT_COMMON_SAMBA
                POPT_COMMON_CONNECTION
                POPT_COMMON_CREDENTIALS
@@ -5369,18 +6485,15 @@ static int do_message_op(struct user_auth_info *a_info)
 
         /* set default debug level to 1 regardless of what smb.conf sets */
        setup_logging( "smbclient", DEBUG_DEFAULT_STDERR );
-       load_case_tables();
+       smb_init_locale();
 
        lp_set_cmdline("log level", "1");
 
-       auth_info = user_auth_info_init(frame);
-       if (auth_info == NULL) {
-               exit(1);
-       }
-       popt_common_set_auth_info(auth_info);
+       popt_common_credentials_set_ignore_missing_conf();
+       popt_common_credentials_set_delay_post();
 
        /* skip argv(0) */
-       pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
+       pc = poptGetContext("smbclient", argc, const_argv, long_options, 0);
        poptSetOtherOptionHelp(pc, "service <password>");
 
        while ((opt = poptGetNextOpt(pc)) != -1) {
@@ -5405,10 +6518,11 @@ static int do_message_op(struct user_auth_info *a_info)
 
                /* if the service has already been retrieved then check if we have also a password */
                if (service_opt
-                   && (!get_cmdline_auth_info_got_pass(auth_info))
+                   && (!get_cmdline_auth_info_got_pass(
+                               popt_get_cmdline_auth_info()))
                    && poptPeekArg(pc)) {
-                       set_cmdline_auth_info_password(auth_info,
-                                                      poptGetArg(pc));
+                       set_cmdline_auth_info_password(
+                               popt_get_cmdline_auth_info(), poptGetArg(pc));
                }
 
 
@@ -5448,19 +6562,21 @@ static int do_message_op(struct user_auth_info *a_info)
                        }
                        break;
                case 'm':
-                       max_protocol = interpret_protocol(poptGetOptArg(pc), max_protocol);
+                       lp_set_cmdline("client max protocol", poptGetOptArg(pc));
                        break;
                case 'T':
                        /* We must use old option processing for this. Find the
                         * position of the -T option in the raw argv[]. */
                        {
                                int i;
+
                                for (i = 1; i < argc; i++) {
                                        if (strncmp("-T", argv[i],2)==0)
                                                break;
                                }
                                i++;
-                               if (!tar_parseargs(argc, argv, poptGetOptArg(pc), i)) {
+                               if (tar_parse_args(tar_ctx, poptGetOptArg(pc),
+                                                  const_argv + i, argc - i)) {
                                        poptPrintUsage(pc, stderr, 0);
                                        exit(1);
                                }
@@ -5477,6 +6593,9 @@ static int do_message_op(struct user_auth_info *a_info)
                case 'g':
                        grepable=true;
                        break;
+               case 'q':
+                       quiet=true;
+                       break;
                case 'e':
                        smb_encrypt=true;
                        break;
@@ -5505,27 +6624,12 @@ static int do_message_op(struct user_auth_info *a_info)
 
        /* if the service has already been retrieved then check if we have also a password */
        if (service_opt
-           && !get_cmdline_auth_info_got_pass(auth_info)
+           && !get_cmdline_auth_info_got_pass(popt_get_cmdline_auth_info())
            && poptPeekArg(pc)) {
-               set_cmdline_auth_info_password(auth_info,
+               set_cmdline_auth_info_password(popt_get_cmdline_auth_info(),
                                               poptGetArg(pc));
        }
 
-       if ( override_logfile )
-               setup_logging( lp_logfile(talloc_tos()), DEBUG_FILE );
-
-       if (!lp_load_client(get_dyn_CONFIGFILE())) {
-               fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
-                       argv[0], get_dyn_CONFIGFILE());
-       }
-
-       if (get_cmdline_auth_info_use_machine_account(auth_info) &&
-           !set_cmdline_auth_info_machine_account_creds(auth_info)) {
-               exit(-1);
-       }
-
-       load_interfaces();
-
        if (service_opt && service) {
                size_t len;
 
@@ -5544,7 +6648,6 @@ static int do_message_op(struct user_auth_info *a_info)
                }
        }
 
-       smb_encrypt = get_cmdline_auth_info_smb_encrypt(auth_info);
        if (!init_names()) {
                fprintf(stderr, "init_names() failed\n");
                exit(1);
@@ -5553,7 +6656,7 @@ static int do_message_op(struct user_auth_info *a_info)
        if(new_name_resolve_order)
                lp_set_cmdline("name resolve order", new_name_resolve_order);
 
-       if (!tar_type && !query_host && !service && !message) {
+       if (!tar_to_process(tar_ctx) && !query_host && !service && !message) {
                poptPrintUsage(pc, stderr, 0);
                exit(1);
        }
@@ -5564,9 +6667,13 @@ static int do_message_op(struct user_auth_info *a_info)
        DEBUG(3,("Client started (version %s).\n", samba_version_string()));
 
        /* Ensure we have a password (or equivalent). */
-       set_cmdline_auth_info_getpass(auth_info);
+       popt_common_credentials_post();
+       smb_encrypt = get_cmdline_auth_info_smb_encrypt(
+                       popt_get_cmdline_auth_info());
+
+       max_protocol = lp_client_max_protocol();
 
-       if (tar_type) {
+       if (tar_to_process(tar_ctx)) {
                if (cmdstr)
                        process_command_string(cmdstr);
                rc = do_tar_op(base_directory);
@@ -5590,11 +6697,12 @@ static int do_message_op(struct user_auth_info *a_info)
 
                rc = do_host_query(qhost);
        } else if (message) {
-               rc = do_message_op(auth_info);
+               rc = do_message_op(popt_get_cmdline_auth_info());
        } else if (process(base_directory)) {
                rc = 1;
        }
 
+       popt_free_cmdline_auth_info();
        TALLOC_FREE(frame);
        return rc;
 }