smbclient: Handle ENUM_DIR in "notify" command
[metze/samba/wip.git] / source3 / client / client.c
index fbb24590a98d7cf6823b42d2dbbd6854d8bfff2c..1429b44e9cf1a75f5212749ecf123baf83133b02 100644 (file)
@@ -108,9 +108,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)
@@ -189,16 +186,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++;
                }
@@ -207,9 +208,9 @@ static int writefile(int f, char *b, int n)
                }
                b++;
                i++;
-       }
+       } while (i < n);
 
-       return(i);
+       return (ssize_t)i;
 }
 
 /****************************************************************************
@@ -217,17 +218,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)) {
-               if ((c = x_getc(f)) == EOF) {
+               if ((c = getc(f)) == EOF) {
                        break;
                }
 
@@ -242,7 +243,7 @@ static int readfile(uint8_t *b, int n, XFILE *f)
 }
 
 struct push_state {
-       XFILE *f;
+       FILE *f;
        off_t nread;
 };
 
@@ -251,7 +252,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;
        }
 
@@ -304,7 +305,7 @@ static int do_dskattr(void)
        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)) {
@@ -348,6 +349,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.
 ****************************************************************************/
@@ -402,11 +434,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);
@@ -446,7 +478,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;
@@ -857,9 +889,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));
@@ -900,8 +932,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);
@@ -955,6 +988,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());
@@ -1007,6 +1045,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)) {
@@ -1050,7 +1096,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;
@@ -1085,8 +1134,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;
@@ -1203,7 +1252,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;
        }
@@ -1269,6 +1318,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;
@@ -1288,6 +1341,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,'\\','/');
@@ -1318,6 +1375,10 @@ static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
                return NT_STATUS_NO_MEMORY;
        }
 
+       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
@@ -1387,7 +1448,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;
        }
@@ -1445,6 +1506,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;
@@ -1463,6 +1528,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;
@@ -1483,8 +1552,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;
@@ -1527,6 +1596,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;
@@ -1558,6 +1628,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;
@@ -1572,8 +1646,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;
                }
@@ -1628,6 +1703,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;
 }
@@ -1858,7 +1937,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;
@@ -1872,7 +1954,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;
@@ -1881,8 +1963,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;
@@ -1921,14 +2003,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;
                        }
                }
@@ -1942,7 +2024,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;
@@ -1958,14 +2040,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);
        }
 
        {
@@ -1982,8 +2064,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);
        }
 
@@ -2020,7 +2103,7 @@ static int cmd_put(void)
                return 1;
        }
 
-       rname = clean_name(ctx, rname);
+       rname = client_clean_name(ctx, rname);
        if (!rname) {
                return 1;
        }
@@ -2060,8 +2143,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);
        }
 }
 
@@ -2106,8 +2188,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;
@@ -2127,7 +2212,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;
                }
 
@@ -2138,29 +2224,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);
                }
         }
 
@@ -2184,7 +2274,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;
@@ -2222,6 +2312,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..."));
@@ -2252,6 +2355,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);
@@ -2422,6 +2537,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)) {
@@ -2430,6 +2549,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.
 ****************************************************************************/
@@ -2462,9 +2762,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;
@@ -2503,8 +2807,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;
@@ -2535,35 +2844,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 {
@@ -2599,6 +2927,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");
@@ -2606,8 +2938,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;
@@ -2654,6 +2986,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");
@@ -2661,8 +2997,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;
@@ -2698,9 +3034,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;
@@ -2737,9 +3077,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;
@@ -3039,9 +3383,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;
@@ -3083,6 +3431,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(),
@@ -3090,9 +3442,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;
@@ -3137,9 +3493,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;
@@ -3170,7 +3530,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;
@@ -3179,11 +3539,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(),
@@ -3191,23 +3551,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;
        }
 
@@ -3241,11 +3607,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;
@@ -3400,9 +3770,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;
@@ -3568,9 +3942,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;
@@ -3625,9 +4003,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;
@@ -3671,9 +4053,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;
@@ -3780,8 +4166,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;
@@ -3815,10 +4205,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;
        }
 
@@ -3829,6 +4220,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",
@@ -3837,22 +4232,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),
@@ -3917,6 +4321,10 @@ static int cmd_scopy(void)
        if (!src) {
                return 1;
        }
+       src = client_clean_name(ctx, src);
+       if (src == NULL) {
+               return 1;
+       }
 
        dest = talloc_asprintf(ctx,
                        "%s%s",
@@ -3925,16 +4333,20 @@ static int cmd_scopy(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,
-                       &targetsrc);
+       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, "", 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("scopy %s: %s\n", dest, nt_errstr(status));
                return 1;
@@ -4047,6 +4459,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",
@@ -4055,9 +4471,13 @@ 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;
@@ -4134,6 +4554,10 @@ 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,
@@ -4144,12 +4568,17 @@ static int cmd_notify(void)
        }
 
        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));
@@ -4308,7 +4737,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;
        }
@@ -4360,7 +4789,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;
        }
@@ -4554,6 +4983,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)) {
@@ -4574,9 +5004,21 @@ static int cmd_logon(void)
                return 1;
        }
 
-       nt_status = cli_session_setup(cli, l_username,
-                                     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;
@@ -4624,7 +5066,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;
@@ -4672,7 +5114,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);
        }
 
@@ -4701,7 +5146,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)) {
@@ -4713,6 +5158,131 @@ 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
@@ -4783,6 +5353,11 @@ int cmd_setmode(void)
                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;
@@ -4955,6 +5530,7 @@ static struct {
   {"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}},
@@ -5026,6 +5602,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}},
 
@@ -5041,9 +5619,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)) {
@@ -5111,8 +5689,8 @@ 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)) {
@@ -5290,9 +5868,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;
        }
@@ -5384,7 +5966,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) {
@@ -5458,7 +6040,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;
        }
 
@@ -5478,6 +6066,8 @@ static int process_stdin(void)
 {
        int rc = 0;
 
+       d_printf("Try \"help\" to get a list of possible commands.\n");
+
        while (!finished) {
                TALLOC_CTX *frame = talloc_stackframe();
                char *tok = NULL;
@@ -5540,8 +6130,9 @@ 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;
        }
@@ -5576,8 +6167,9 @@ 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;
        }
@@ -5597,31 +6189,40 @@ static int do_host_query(const char *query_host)
                }
        }
 
+       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) {
+       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("Failed to connect with SMB1 "
+                                "-- no workgroup available\n");
+                       return 0;
                }
        }
 
-       if (cli == NULL) {
-               d_printf("NetBIOS over TCP disabled -- no workgroup available\n");
-               return 0;
-       }
-
        cli_set_timeout(cli, io_timeout*1000);
        list_servers(lp_workgroup());
 out:
@@ -5645,8 +6246,9 @@ 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)) {
             ret = 1;
             goto out;
@@ -5752,6 +6354,7 @@ int main(int argc,char *argv[])
 
        lp_set_cmdline("log level", "1");
 
+       popt_common_credentials_set_ignore_missing_conf();
        popt_common_credentials_set_delay_post();
 
        /* skip argv(0) */
@@ -5780,10 +6383,11 @@ int main(int argc,char *argv[])
 
                /* if the service has already been retrieved then check if we have also a password */
                if (service_opt
-                   && (!get_cmdline_auth_info_got_pass(cmdline_auth_info))
+                   && (!get_cmdline_auth_info_got_pass(
+                               popt_get_cmdline_auth_info()))
                    && poptPeekArg(pc)) {
-                       set_cmdline_auth_info_password(cmdline_auth_info,
-                                                      poptGetArg(pc));
+                       set_cmdline_auth_info_password(
+                               popt_get_cmdline_auth_info(), poptGetArg(pc));
                }
 
 
@@ -5882,22 +6486,12 @@ int main(int argc,char *argv[])
 
        /* if the service has already been retrieved then check if we have also a password */
        if (service_opt
-           && !get_cmdline_auth_info_got_pass(cmdline_auth_info)
+           && !get_cmdline_auth_info_got_pass(popt_get_cmdline_auth_info())
            && poptPeekArg(pc)) {
-               set_cmdline_auth_info_password(cmdline_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());
-       }
-
-       load_interfaces();
-
        if (service_opt && service) {
                size_t len;
 
@@ -5936,8 +6530,8 @@ int main(int argc,char *argv[])
 
        /* Ensure we have a password (or equivalent). */
        popt_common_credentials_post();
-       auth_info = cmdline_auth_info;
-       smb_encrypt = get_cmdline_auth_info_smb_encrypt(auth_info);
+       smb_encrypt = get_cmdline_auth_info_smb_encrypt(
+                       popt_get_cmdline_auth_info());
 
        max_protocol = lp_client_max_protocol();
 
@@ -5965,11 +6559,12 @@ int main(int argc,char *argv[])
 
                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;
 }