s4-dns: dlz_bind9: Fix ipv6 updates
[samba.git] / source3 / client / client.c
index 22f861bb0289679af3faa26476933a1f7360dbb0..592258ddf430a4d511a161a61a2befb2891b6700 100644 (file)
@@ -26,6 +26,7 @@
 #include "popt_common.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"
@@ -37,6 +38,7 @@
 #include "trans2.h"
 #include "libsmb/nmblib.h"
 #include "include/ntioctl.h"
+#include "../libcli/smb/smbXcli_base.h"
 
 #ifndef REGISTER
 #define REGISTER 0
@@ -45,7 +47,6 @@
 extern int do_smb_browse(void); /* mDNS browsing */
 
 extern bool override_logfile;
-extern char tar_type;
 
 static int port = 0;
 static char *service;
@@ -54,20 +55,17 @@ static bool grepable = 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)
 
@@ -77,17 +75,12 @@ 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;
 static bool showacls = false;
 bool lowercase = false;
+static bool backup_intent = false;
 
 static struct sockaddr_storage dest_ss;
 static char dest_ss_str[INET6_ADDRSTRLEN];
@@ -233,7 +226,7 @@ static int readfile(uint8_t *b, int n, XFILE *f)
                return x_fread(b,1,n,f);
 
        i = 0;
-       while (i < (n - 1) && (i < BUFFER_SIZE)) {
+       while (i < (n - 1)) {
                if ((c = x_getc(f)) == EOF) {
                        break;
                }
@@ -250,7 +243,7 @@ static int readfile(uint8_t *b, int n, XFILE *f)
 
 struct push_state {
        XFILE *f;
-       SMB_OFF_T nread;
+       off_t nread;
 };
 
 static size_t push_source(uint8_t *buf, size_t n, void *priv)
@@ -426,7 +419,7 @@ static int do_cd(const char *new_dir)
        /* Use a trans2_qpathinfo to test directories for modern servers.
           Except Win9x doesn't support the qpathinfo_basic() call..... */
 
-       if (cli_state_protocol(targetcli) > PROTOCOL_LANMAN2 && !targetcli->win95) {
+       if (smbXcli_conn_protocol(targetcli->conn) > PROTOCOL_LANMAN2 && !targetcli->win95) {
 
                status = cli_qpathinfo_basic(targetcli, targetpath, &sbuf,
                                             &attributes);
@@ -580,7 +573,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)));
@@ -1069,9 +1062,9 @@ static int do_get(const char *rname, const char *lname_in, bool reget)
        bool newhandle = false;
        struct timespec tp_start;
        uint16 attr;
-       SMB_OFF_T size;
+       off_t size;
        off_t start = 0;
-       SMB_OFF_T nread = 0;
+       off_t nread = 0;
        int rc = 0;
        struct cli_state *targetcli = NULL;
        char *targetname = NULL;
@@ -1084,7 +1077,10 @@ static int do_get(const char *rname, const char *lname_in, bool reget)
        }
 
        if (lowercase) {
-               strlower_m(lname);
+               if (!strlower_m(lname)) {
+                       d_printf("strlower_m %s failed\n", lname);
+                       return 1;
+               }
        }
 
        status = cli_resolve_path(ctx, "", auth_info, cli, rname, &targetcli,
@@ -1107,16 +1103,16 @@ static int do_get(const char *rname, const char *lname_in, bool reget)
                handle = fileno(stdout);
        } else {
                if (reget) {
-                       handle = sys_open(lname, O_WRONLY|O_CREAT, 0644);
+                       handle = open(lname, O_WRONLY|O_CREAT, 0644);
                        if (handle >= 0) {
-                               start = sys_lseek(handle, 0, SEEK_END);
+                               start = lseek(handle, 0, SEEK_END);
                                if (start == -1) {
                                        d_printf("Error seeking local file\n");
                                        return 1;
                                }
                        }
                } else {
-                       handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
+                       handle = open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
                }
                newhandle = true;
        }
@@ -1294,7 +1290,9 @@ static NTSTATUS do_mget(struct cli_state *cli_state, struct file_info *finfo,
 
        string_replace(finfo->name,'\\','/');
        if (lowercase) {
-               strlower_m(finfo->name);
+               if (!strlower_m(finfo->name)) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
        }
 
        if (!directory_exist(finfo->name) &&
@@ -1318,8 +1316,17 @@ 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)) {
+       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;
        }
 
@@ -1349,6 +1356,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) {
@@ -1359,7 +1367,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;
@@ -1677,9 +1687,8 @@ static int do_allinfo(const char *name)
 {
        fstring altname;
        struct timespec b_time, a_time, m_time, c_time;
-       SMB_OFF_T size;
+       off_t size;
        uint16_t mode;
-       SMB_INO_T ino;
        NTTIME tmp;
        uint16_t fnum;
        unsigned int num_streams;
@@ -1693,12 +1702,21 @@ 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);
@@ -1750,10 +1768,11 @@ static int do_allinfo(const char *name)
        }
 
        status = cli_ntcreate(cli, name, 0,
-                             CREATE_ACCESS_READ, 0,
+                             SEC_FILE_READ_DATA | SEC_FILE_READ_ATTRIBUTE |
+                             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
@@ -1774,7 +1793,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)) {
@@ -1837,7 +1856,7 @@ static int do_put(const char *rname, const char *lname, bool reput)
        TALLOC_CTX *ctx = talloc_tos();
        uint16_t fnum;
        XFILE *f;
-       SMB_OFF_T start = 0;
+       off_t start = 0;
        int rc = 0;
        struct timespec tp_start;
        struct cli_state *targetcli;
@@ -2073,7 +2092,7 @@ static int cmd_select(void)
 static int file_find(struct file_list **list, const char *directory,
                      const char *expression, bool match)
 {
-       SMB_STRUCT_DIR *dir;
+       DIR *dir;
        struct file_list *entry;
         struct stat statbuf;
         int ret;
@@ -2081,7 +2100,7 @@ static int file_find(struct file_list **list, const char *directory,
        bool isdir;
        const char *dname;
 
-        dir = sys_opendir(directory);
+        dir = opendir(directory);
        if (!dir)
                return -1;
 
@@ -2110,14 +2129,14 @@ static int file_find(struct file_list **list, const char *directory,
 
                                if (ret == -1) {
                                        SAFE_FREE(path);
-                                       sys_closedir(dir);
+                                       closedir(dir);
                                        return -1;
                                }
                        }
                        entry = SMB_MALLOC_P(struct file_list);
                        if (!entry) {
                                d_printf("Out of memory in file_find\n");
-                               sys_closedir(dir);
+                               closedir(dir);
                                return -1;
                        }
                        entry->file_path = path;
@@ -2128,7 +2147,7 @@ static int file_find(struct file_list **list, const char *directory,
                }
         }
 
-       sys_closedir(dir);
+       closedir(dir);
        return 0;
 }
 
@@ -2291,12 +2310,12 @@ static int cmd_print(void)
                rname = talloc_asprintf(ctx,
                                        "%s-%d",
                                        p+1,
-                                       (int)sys_getpid());
+                                       (int)getpid());
        }
        if (strequal(lname,"-")) {
                rname = talloc_asprintf(ctx,
                                "stdin-%d",
-                               (int)sys_getpid());
+                               (int)getpid());
        }
        if (!rname) {
                return 1;
@@ -2477,12 +2496,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 {
@@ -3462,11 +3481,6 @@ static int cmd_getfacl(void)
        return 0;
 }
 
-static void printf_cb(const char *buf, void *private_data)
-{
-       printf("%s", buf);
-}
-
 /****************************************************************************
  Get the EA list of a file
 ****************************************************************************/
@@ -3510,8 +3524,8 @@ static int cmd_geteas(void)
 
        for (i=0; i<num_eas; i++) {
                d_printf("%s (%d) =\n", eas[i].name, (int)eas[i].flags);
-               dump_data_cb(eas[i].value.data, eas[i].value.length, false,
-                            printf_cb, NULL);
+               dump_data_file(eas[i].value.data, eas[i].value.length, false,
+                              stdout);
                d_printf("\n");
        }
 
@@ -3904,6 +3918,62 @@ static int cmd_newer(void)
        return 0;
 }
 
+/****************************************************************************
+ Watch directory changes
+****************************************************************************/
+
+static int cmd_notify(void)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       char *name, *buf;
+       NTSTATUS status;
+       uint16_t fnum;
+
+       name = talloc_strdup(talloc_tos(), client_get_cur_dir());
+       if (name == NULL) {
+               goto fail;
+       }
+       if (!next_token_talloc(talloc_tos(), &cmd_ptr, &buf, NULL)) {
+               goto usage;
+       }
+       name = talloc_asprintf_append(name, "%s", buf);
+       if (name == NULL) {
+               goto fail;
+       }
+       status = cli_ntcreate(
+               cli, name, 0, FILE_READ_DATA, 0,
+               FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
+               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;
+
+               status = cli_notify(cli, fnum, 1000, FILE_NOTIFY_CHANGE_ALL,
+                                   true,
+                                   talloc_tos(), &num_changes, &changes);
+               if (!NT_STATUS_IS_OK(status)) {
+                       d_printf("notify returned %s\n",
+                                nt_errstr(status));
+                       goto fail;
+               }
+               for (i=0; i<num_changes; i++) {
+                       printf("%4.4x %s\n", changes[i].action,
+                              changes[i].name);
+               }
+               TALLOC_FREE(changes);
+       }
+usage:
+       d_printf("notify <file>\n");
+fail:
+       TALLOC_FREE(frame);
+       return 1;
+}
+
 /****************************************************************************
  Set the archive level.
 ****************************************************************************/
@@ -3922,6 +3992,18 @@ static int cmd_archive(void)
        return 0;
 }
 
+/****************************************************************************
+ Toggle the backup_intent state.
+****************************************************************************/
+
+static int cmd_backup(void)
+{
+       backup_intent = !backup_intent;
+       cli_set_backup_intent(cli, backup_intent);
+       DEBUG(2,("backup intent is now %s\n",backup_intent?"on":"off"));
+       return 1;
+}
+
 /****************************************************************************
  Toggle the lowercaseflag.
 ****************************************************************************/
@@ -4139,7 +4221,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)) {
@@ -4286,9 +4368,12 @@ static int cmd_logon(void)
        }
 
        if (!next_token_talloc(ctx, &cmd_ptr,&l_password,NULL)) {
-               char *pass = getpass("Password: ");
-               if (pass) {
-                       l_password = talloc_strdup(ctx,pass);
+               char pwd[256] = {0};
+               int rc;
+
+               rc = samba_getpass("Password: ", pwd, sizeof(pwd), false, false);
+               if (rc == 0) {
+                       l_password = talloc_strdup(ctx, pwd);
                }
        }
        if (!l_password) {
@@ -4431,7 +4516,7 @@ static int cmd_show_connect( void )
                return 1;
        }
 
-       d_printf("//%s/%s\n", cli_state_remote_name(targetcli), targetcli->share);
+       d_printf("//%s/%s\n", smbXcli_conn_remote_name(targetcli->conn), targetcli->share);
        return 0;
 }
 
@@ -4446,29 +4531,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;
@@ -4476,6 +4567,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
 ****************************************************************************/
@@ -4519,22 +4635,23 @@ static struct {
    {COMPL_NONE,COMPL_NONE}},
   {"altname",cmd_altname,"<file> show alt name",{COMPL_NONE,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}},
   {"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}},
   {"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}},
@@ -4542,16 +4659,17 @@ 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}},
   {"open",cmd_open,"<mask> open a file",{COMPL_REMOTE,COMPL_NONE}},
   {"posix", cmd_posix, "turn on all POSIX capabilities", {COMPL_REMOTE,COMPL_NONE}},
   {"posix_encrypt",cmd_posix_encrypt,"<domain> <user> <password> start up transport encryption",{COMPL_REMOTE,COMPL_NONE}},
@@ -4560,7 +4678,7 @@ static struct {
   {"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}},
   {"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}},
@@ -4568,20 +4686,21 @@ 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}},
+  {"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}},
@@ -4685,6 +4804,7 @@ static int process_command_string(const char *cmd_in)
                if (!NT_STATUS_IS_OK(status)) {
                        return 1;
                }
+               cli_set_timeout(cli, io_timeout*1000);
        }
 
        while (cmd[0] != '\0')    {
@@ -4897,6 +5017,7 @@ static char **remote_completion(const char *text, int len)
 
        info.matches[0] = SMB_STRNDUP(info.matches[1], info.samelen);
        info.matches[info.count] = NULL;
+       TALLOC_FREE(ctx);
        return info.matches;
 
 cleanup:
@@ -5023,9 +5144,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)) {
-               DEBUG(0, ("SMBecho failed (%s). Maybe server has closed "
-                       "the connection\n", nt_errstr(status)));
+       if (NT_STATUS_IS_OK(status)) {
+               return;
+       }
+
+       if (!cli_state_is_connected(cli)) {
+               DEBUG(0,("SMBecho failed (%s). The connection is "
+                        "disconnected now\n", nt_errstr(status)));
                finished = true;
                smb_readline_done();
        }
@@ -5107,6 +5232,8 @@ static int process(const char *base_directory)
                return 1;
        }
 
+       cli_set_timeout(cli, io_timeout*1000);
+
        if (base_directory && *base_directory) {
                rc = do_cd(base_directory);
                if (rc) {
@@ -5141,6 +5268,7 @@ static int do_host_query(const char *query_host)
                return 1;
        }
 
+       cli_set_timeout(cli, io_timeout*1000);
        browse_host(true);
 
        /* Ensure that the host can do IPv4 */
@@ -5155,7 +5283,7 @@ static int do_host_query(const char *query_host)
                }
        }
 
-       if (port != 139) {
+       if (port != NBT_SMB_PORT) {
 
                /* Workgroups simply don't make sense over anything
                   else but port 139... */
@@ -5164,7 +5292,8 @@ 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, 139, name_type, &cli);
+                                    max_protocol, NBT_SMB_PORT, name_type,
+                                    &cli);
                if (!NT_STATUS_IS_OK(status)) {
                        cli = NULL;
                }
@@ -5175,6 +5304,7 @@ static int do_host_query(const char *query_host)
                return 1;
        }
 
+       cli_set_timeout(cli, io_timeout*1000);
        list_servers(lp_workgroup());
 
        cli_shutdown(cli);
@@ -5188,7 +5318,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) {
@@ -5199,25 +5330,27 @@ static int do_tar_op(const char *base_directory)
                                     service, auth_info, true, 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;
 }
 
 /****************************************************************************
@@ -5229,13 +5362,14 @@ static int do_message_op(struct user_auth_info *a_info)
        NTSTATUS status;
 
        status = cli_connect_nb(desthost, have_ip ? &dest_ss : NULL,
-                               port ? port : 139, name_type,
+                               port ? port : NBT_SMB_PORT, name_type,
                                lp_netbios_name(), SMB_SIGNING_DEFAULT, 0, &cli);
        if (!NT_STATUS_IS_OK(status)) {
                d_printf("Connection to %s failed. Error %s\n", desthost, nt_errstr(status));
                return 1;
        }
 
+       cli_set_timeout(cli, io_timeout*1000);
        send_message(get_cmdline_auth_info_username(a_info));
        cli_shutdown(cli);
 
@@ -5246,8 +5380,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;
@@ -5258,6 +5393,8 @@ 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
 
@@ -5269,8 +5406,9 @@ static int do_message_op(struct user_auth_info *a_info)
                { "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" }, 
+               { "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" },
+               { "timeout", 't', POPT_ARG_INT, &io_timeout, 'b', "Changes the per-operation timeout", "SECONDS" },
                { "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" },
@@ -5298,7 +5436,7 @@ static int do_message_op(struct user_auth_info *a_info)
        popt_common_set_auth_info(auth_info);
 
        /* 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) {
@@ -5329,6 +5467,7 @@ static int do_message_op(struct user_auth_info *a_info)
                                                       poptGetArg(pc));
                }
 
+
                switch (opt) {
                case 'M':
                        /* Messages are sent to NetBIOS name type 0x3
@@ -5341,7 +5480,7 @@ static int do_message_op(struct user_auth_info *a_info)
                                exit(ENOMEM);
                        }
                        if( !port )
-                               port = 139;
+                               port = NBT_SMB_PORT;
                        message = true;
                        break;
                case 'I':
@@ -5365,19 +5504,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);
                                }
@@ -5429,7 +5570,7 @@ static int do_message_op(struct user_auth_info *a_info)
        }
 
        if ( override_logfile )
-               setup_logging( lp_logfile(), DEBUG_FILE );
+               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",
@@ -5468,27 +5609,28 @@ static int do_message_op(struct user_auth_info *a_info)
        }
 
        if(new_name_resolve_order)
-               lp_set_name_resolve_order(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);
        }
 
        poptFreeContext(pc);
+       popt_burn_cmdline_password(argc, argv);
 
        DEBUG(3,("Client started (version %s).\n", samba_version_string()));
 
        /* Ensure we have a password (or equivalent). */
        set_cmdline_auth_info_getpass(auth_info);
 
-       if (tar_type) {
+       max_protocol = lp_client_max_protocol();
+
+       if (tar_to_process(tar_ctx)) {
                if (cmdstr)
                        process_command_string(cmdstr);
-               return do_tar_op(base_directory);
-       }
-
-       if (query_host && *query_host) {
+               rc = do_tar_op(base_directory);
+       } else if (query_host && *query_host) {
                char *qhost = query_host;
                char *slash;
 
@@ -5506,15 +5648,11 @@ static int do_message_op(struct user_auth_info *a_info)
                        sscanf(p, "%x", &name_type);
                }
 
-               return do_host_query(qhost);
-       }
-
-       if (message) {
-               return do_message_op(auth_info);
-       }
-
-       if (process(base_directory)) {
-               return 1;
+               rc = do_host_query(qhost);
+       } else if (message) {
+               rc = do_message_op(auth_info);
+       } else if (process(base_directory)) {
+               rc = 1;
        }
 
        TALLOC_FREE(frame);