Oops. Forgot to delete now unused local from the previous patch.
[ira/wip.git] / source3 / client / client.c
index cfe33c46c2fb131f29c7f91d4d9e44a03417535a..f4d9963c16a1e4529c2a205287e49e8c3c57120b 100644 (file)
@@ -103,6 +103,9 @@ 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)
@@ -299,12 +302,12 @@ static int do_dskattr(void)
        char *targetpath = NULL;
        TALLOC_CTX *ctx = talloc_tos();
 
-       if ( !cli_resolve_path(ctx, "", cli, client_get_cur_dir(), &targetcli, &targetpath)) {
+       if ( !cli_resolve_path(ctx, "", auth_info, cli, client_get_cur_dir(), &targetcli, &targetpath)) {
                d_printf("Error in dskattr: %s\n", cli_errstr(cli));
                return 1;
        }
 
-       if (!cli_dskattr(targetcli, &bsize, &total, &avail)) {
+       if (!NT_STATUS_IS_OK(cli_dskattr(targetcli, &bsize, &total, &avail))) {
                d_printf("Error in dskattr: %s\n",cli_errstr(targetcli));
                return 1;
        }
@@ -393,7 +396,7 @@ static int do_cd(const char *new_dir)
        new_cd = clean_name(ctx, new_cd);
        client_set_cur_dir(new_cd);
 
-       if ( !cli_resolve_path(ctx, "", cli, new_cd, &targetcli, &targetpath)) {
+       if ( !cli_resolve_path(ctx, "", auth_info, cli, new_cd, &targetcli, &targetpath)) {
                d_printf("cd %s: %s\n", new_cd, cli_errstr(cli));
                client_set_cur_dir(saved_dir);
                goto out;
@@ -434,7 +437,7 @@ static int do_cd(const char *new_dir)
                        goto out;
                }
 
-               if (!cli_chkpath(targetcli, targetpath)) {
+               if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, targetpath))) {
                        d_printf("cd %s: %s\n", new_cd, cli_errstr(targetcli));
                        client_set_cur_dir(saved_dir);
                        goto out;
@@ -532,12 +535,12 @@ static void display_finfo(file_info *finfo, const char *dir)
                dir_total += finfo->size;
        } else {
                char *afname = NULL;
-               int fnum;
+               uint16_t fnum;
 
                /* skip if this is . or .. */
                if ( strequal(finfo->name,"..") || strequal(finfo->name,".") )
                        return;
-               /* create absolute filename for cli_nt_create() FIXME */
+               /* create absolute filename for cli_ntcreate() FIXME */
                afname = talloc_asprintf(ctx,
                                        "%s%s%s",
                                        dir,
@@ -551,8 +554,9 @@ static void display_finfo(file_info *finfo, const char *dir)
                d_printf( "MODE:%s\n", attrib_string(finfo->mode));
                d_printf( "SIZE:%.0f\n", (double)finfo->size);
                d_printf( "MTIME:%s", time_to_asc(t));
-               fnum = cli_nt_create(finfo->cli, afname, CREATE_ACCESS_READ);
-               if (fnum == -1) {
+               if (!NT_STATUS_IS_OK(cli_ntcreate(finfo->cli, afname, 0,
+                               CREATE_ACCESS_READ, 0, FILE_SHARE_READ|FILE_SHARE_WRITE,
+                               FILE_OPEN, 0x0, 0x0, &fnum))) {
                        DEBUG( 0, ("display_finfo() Failed to open %s: %s\n",
                                afname,
                                cli_errstr( finfo->cli)));
@@ -819,7 +823,7 @@ void do_list(const char *mask,
 
                        /* check for dfs */
 
-                       if ( !cli_resolve_path(ctx, "", cli, head, &targetcli, &targetpath ) ) {
+                       if ( !cli_resolve_path(ctx, "", auth_info, cli, head, &targetcli, &targetpath ) ) {
                                d_printf("do_list: [%s] %s\n", head, cli_errstr(cli));
                                remove_do_list_queue_head();
                                continue;
@@ -852,7 +856,7 @@ void do_list(const char *mask,
                }
        } else {
                /* check for dfs */
-               if (cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetpath)) {
+               if (cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetpath)) {
                        if (cli_list(targetcli, targetpath, attribute, do_list_helper, NULL) == -1) {
                                d_printf("%s listing %s\n",
                                        cli_errstr(targetcli), targetpath);
@@ -996,7 +1000,8 @@ static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
 static int do_get(const char *rname, const char *lname_in, bool reget)
 {
        TALLOC_CTX *ctx = talloc_tos();
-       int handle = 0, fnum;
+       int handle = 0;
+       uint16_t fnum;
        bool newhandle = false;
        struct timeval tp_start;
        uint16 attr;
@@ -1018,16 +1023,14 @@ static int do_get(const char *rname, const char *lname_in, bool reget)
                strlower_m(lname);
        }
 
-       if (!cli_resolve_path(ctx, "", cli, rname, &targetcli, &targetname ) ) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, rname, &targetcli, &targetname ) ) {
                d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
                return 1;
        }
 
        GetTimeOfDay(&tp_start);
 
-       fnum = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE);
-
-       if (fnum == -1) {
+       if (!NT_STATUS_IS_OK(cli_open(targetcli, targetname, O_RDONLY, DENY_NONE, &fnum))) {
                d_printf("%s opening remote file %s\n",cli_errstr(cli),rname);
                return 1;
        }
@@ -1057,8 +1060,8 @@ static int do_get(const char *rname, const char *lname_in, bool reget)
 
        if (!cli_qfileinfo(targetcli, fnum,
                           &attr, &size, NULL, NULL, NULL, NULL, NULL) &&
-           !cli_getattrE(targetcli, fnum,
-                         &attr, &size, NULL, NULL, NULL)) {
+           !NT_STATUS_IS_OK(cli_getattrE(targetcli, fnum,
+                         &attr, &size, NULL, NULL, NULL))) {
                d_printf("getattrib: %s\n",cli_errstr(targetcli));
                return 1;
        }
@@ -1075,7 +1078,7 @@ static int do_get(const char *rname, const char *lname_in, bool reget)
                return 1;
        }
 
-       if (!cli_close(targetcli, fnum)) {
+       if (!NT_STATUS_IS_OK(cli_close(targetcli, fnum))) {
                d_printf("Error %s closing remote file\n",cli_errstr(cli));
                rc = 1;
        }
@@ -1278,7 +1281,7 @@ static int cmd_more(void)
        if (!lname) {
                return 1;
        }
-       fd = smb_mkstemp(lname);
+       fd = mkstemp(lname);
        if (fd == -1) {
                d_printf("failed to create temporary file for more\n");
                return 1;
@@ -1381,12 +1384,12 @@ static bool do_mkdir(const char *name)
        struct cli_state *targetcli;
        char *targetname = NULL;
 
-       if (!cli_resolve_path(ctx, "", cli, name, &targetcli, &targetname)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, name, &targetcli, &targetname)) {
                d_printf("mkdir %s: %s\n", name, cli_errstr(cli));
                return false;
        }
 
-       if (!cli_mkdir(targetcli, targetname)) {
+       if (!NT_STATUS_IS_OK(cli_mkdir(targetcli, targetname))) {
                d_printf("%s making remote directory %s\n",
                         cli_errstr(targetcli),name);
                return false;
@@ -1419,7 +1422,7 @@ static bool do_altname(const char *name)
 
 static int cmd_quit(void)
 {
-       cli_cm_shutdown();
+       cli_shutdown(cli);
        exit(0);
        /* NOTREACHED */
        return 0;
@@ -1464,7 +1467,7 @@ static int cmd_mkdir(void)
                        return 1;
                }
 
-               if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
+               if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
                        return 1;
                }
 
@@ -1479,7 +1482,7 @@ static int cmd_mkdir(void)
                        if (!ddir2) {
                                return 1;
                        }
-                       if (!cli_chkpath(targetcli, ddir2)) {
+                       if (!NT_STATUS_IS_OK(cli_chkpath(targetcli, ddir2))) {
                                do_mkdir(ddir2);
                        }
                        ddir2 = talloc_asprintf_append(ddir2, "%s", CLI_DIRSEP_STR);
@@ -1615,7 +1618,7 @@ static int cmd_allinfo(void)
 static int do_put(const char *rname, const char *lname, bool reput)
 {
        TALLOC_CTX *ctx = talloc_tos();
-       int fnum;
+       uint16_t fnum;
        XFILE *f;
        SMB_OFF_T start = 0;
        int rc = 0;
@@ -1625,7 +1628,7 @@ static int do_put(const char *rname, const char *lname, bool reput)
        struct push_state state;
        NTSTATUS status;
 
-       if (!cli_resolve_path(ctx, "", cli, rname, &targetcli, &targetname)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, rname, &targetcli, &targetname)) {
                d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli));
                return 1;
        }
@@ -1633,19 +1636,19 @@ static int do_put(const char *rname, const char *lname, bool reput)
        GetTimeOfDay(&tp_start);
 
        if (reput) {
-               fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE);
-               if (fnum >= 0) {
+               status = cli_open(targetcli, targetname, O_RDWR|O_CREAT, DENY_NONE, &fnum);
+               if (NT_STATUS_IS_OK(status)) {
                        if (!cli_qfileinfo(targetcli, fnum, NULL, &start, NULL, NULL, NULL, NULL, NULL) &&
-                           !cli_getattrE(targetcli, fnum, NULL, &start, NULL, NULL, NULL)) {
+                           !NT_STATUS_IS_OK(cli_getattrE(targetcli, fnum, NULL, &start, NULL, NULL, NULL))) {
                                d_printf("getattrib: %s\n",cli_errstr(cli));
                                return 1;
                        }
                }
        } else {
-               fnum = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
+               status = cli_open(targetcli, targetname, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE, &fnum);
        }
 
-       if (fnum == -1) {
+       if (!NT_STATUS_IS_OK(status)) {
                d_printf("%s opening remote file %s\n",cli_errstr(targetcli),rname);
                return 1;
        }
@@ -1687,7 +1690,7 @@ static int do_put(const char *rname, const char *lname, bool reput)
                d_fprintf(stderr, "cli_push returned %s\n", nt_errstr(status));
        }
 
-       if (!cli_close(targetcli, fnum)) {
+       if (!NT_STATUS_IS_OK(cli_close(targetcli, fnum))) {
                d_printf("%s closing remote file %s\n",cli_errstr(cli),rname);
                x_fclose(f);
                return 1;
@@ -1714,7 +1717,7 @@ static int do_put(const char *rname, const char *lname, bool reput)
        }
 
        if (f == x_stdin) {
-               cli_cm_shutdown();
+               cli_shutdown(cli);
                exit(0);
        }
 
@@ -1784,13 +1787,13 @@ static struct file_list {
  Free a file_list structure.
 ****************************************************************************/
 
-static void free_file_list (struct file_list *list_head)
+static void free_file_list (struct file_list *l_head)
 {
        struct file_list *list, *next;
 
-       for (list = list_head; list; list = next) {
+       for (list = l_head; list; list = next) {
                next = list->next;
-               DLIST_REMOVE(list_head, list);
+               DLIST_REMOVE(l_head, list);
                SAFE_FREE(list->file_path);
                SAFE_FREE(list);
        }
@@ -1953,7 +1956,7 @@ static int cmd_mput(void)
                                                break;
                                        }
                                        normalize_name(rname);
-                                       if (!cli_chkpath(cli, rname) &&
+                                       if (!NT_STATUS_IS_OK(cli_chkpath(cli, rname)) &&
                                            !do_mkdir(rname)) {
                                                DEBUG (0, ("Unable to make dir, skipping..."));
                                                /* Skip the directory */
@@ -2112,7 +2115,7 @@ static void do_del(file_info *finfo, const char *dir)
                return;
        }
 
-       if (!cli_unlink(finfo->cli, mask)) {
+       if (!NT_STATUS_IS_OK(cli_unlink(finfo->cli, mask, aSYSTEM | aHIDDEN))) {
                d_printf("%s deleting remote file %s\n",
                                cli_errstr(finfo->cli),mask);
        }
@@ -2183,12 +2186,12 @@ static int cmd_wdel(void)
                return 1;
        }
 
-       if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
                d_printf("cmd_wdel %s: %s\n", mask, cli_errstr(cli));
                return 1;
        }
 
-       if (!cli_unlink_full(targetcli, targetname, attribute)) {
+       if (!NT_STATUS_IS_OK(cli_unlink(targetcli, targetname, attribute))) {
                d_printf("%s deleting remote files %s\n",cli_errstr(targetcli),targetname);
        }
        return 0;
@@ -2204,7 +2207,7 @@ static int cmd_open(void)
        char *buf = NULL;
        char *targetname = NULL;
        struct cli_state *targetcli;
-       int fnum;
+       uint16_t fnum = (uint16_t)-1;
 
        if (!next_token_talloc(ctx, &cmd_ptr,&buf,NULL)) {
                d_printf("open <filename>\n");
@@ -2218,15 +2221,17 @@ static int cmd_open(void)
                return 1;
        }
 
-       if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
                d_printf("open %s: %s\n", mask, cli_errstr(cli));
                return 1;
        }
 
-       fnum = cli_nt_create(targetcli, targetname, FILE_READ_DATA|FILE_WRITE_DATA);
-       if (fnum == -1) {
-               fnum = cli_nt_create(targetcli, targetname, FILE_READ_DATA);
-               if (fnum != -1) {
+       if (!NT_STATUS_IS_OK(cli_ntcreate(targetcli, targetname, 0,
+                       FILE_READ_DATA|FILE_WRITE_DATA, 0,
+                       FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
+               if (NT_STATUS_IS_OK(cli_ntcreate(targetcli, targetname, 0,
+                               FILE_READ_DATA, 0,
+                               FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
                        d_printf("open file %s: for read/write fnum %d\n", targetname, fnum);
                } else {
                        d_printf("Failed to open file %s. %s\n", targetname, cli_errstr(cli));
@@ -2311,7 +2316,7 @@ static int cmd_posix_open(void)
        }
        mode = (mode_t)strtol(buf, (char **)NULL, 8);
 
-       if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
                d_printf("posix_open %s: %s\n", mask, cli_errstr(cli));
                return 1;
        }
@@ -2359,7 +2364,7 @@ static int cmd_posix_mkdir(void)
        }
        mode = (mode_t)strtol(buf, (char **)NULL, 8);
 
-       if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
                d_printf("posix_mkdir %s: %s\n", mask, cli_errstr(cli));
                return 1;
        }
@@ -2393,12 +2398,12 @@ static int cmd_posix_unlink(void)
                return 1;
        }
 
-       if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
                d_printf("posix_unlink %s: %s\n", mask, cli_errstr(cli));
                return 1;
        }
 
-       if (!cli_posix_unlink(targetcli, targetname)) {
+       if (!NT_STATUS_IS_OK(cli_posix_unlink(targetcli, targetname))) {
                d_printf("Failed to unlink file %s. %s\n", targetname, cli_errstr(cli));
        } else {
                d_printf("posix_unlink deleted file %s\n", targetname);
@@ -2427,12 +2432,12 @@ static int cmd_posix_rmdir(void)
                return 1;
        }
 
-       if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
                d_printf("posix_rmdir %s: %s\n", mask, cli_errstr(cli));
                return 1;
        }
 
-       if (!cli_posix_rmdir(targetcli, targetname)) {
+       if (!NT_STATUS_IS_OK(cli_posix_rmdir(targetcli, targetname))) {
                d_printf("Failed to unlink directory %s. %s\n", targetname, cli_errstr(cli));
        } else {
                d_printf("posix_rmdir deleted directory %s\n", targetname);
@@ -2454,7 +2459,7 @@ static int cmd_close(void)
 
        fnum = atoi(buf);
        /* We really should use the targetcli here.... */
-       if (!cli_close(cli, fnum)) {
+       if (!NT_STATUS_IS_OK(cli_close(cli, fnum))) {
                d_printf("close %d: %s\n", fnum, cli_errstr(cli));
                return 1;
        }
@@ -2667,12 +2672,12 @@ static int cmd_rmdir(void)
                return 1;
        }
 
-       if (!cli_resolve_path(ctx, "", cli, mask, &targetcli, &targetname)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, mask, &targetcli, &targetname)) {
                d_printf("rmdir %s: %s\n", mask, cli_errstr(cli));
                return 1;
        }
 
-       if (!cli_rmdir(targetcli, targetname)) {
+       if (!NT_STATUS_IS_OK(cli_rmdir(targetcli, targetname))) {
                d_printf("%s removing remote directory file %s\n",
                         cli_errstr(targetcli),mask);
        }
@@ -2714,7 +2719,7 @@ static int cmd_link(void)
                return 1;
        }
 
-       if (!cli_resolve_path(ctx, "", cli, oldname, &targetcli, &targetname)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, oldname, &targetcli, &targetname)) {
                d_printf("link %s: %s\n", oldname, cli_errstr(cli));
                return 1;
        }
@@ -2765,7 +2770,7 @@ static int cmd_symlink(void)
                return 1;
        }
 
-       if (!cli_resolve_path(ctx, "", cli, oldname, &targetcli, &targetname)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, oldname, &targetcli, &targetname)) {
                d_printf("link %s: %s\n", oldname, cli_errstr(cli));
                return 1;
        }
@@ -2813,7 +2818,7 @@ static int cmd_chmod(void)
 
        mode = (mode_t)strtol(buf, NULL, 8);
 
-       if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetname)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
                d_printf("chmod %s: %s\n", src, cli_errstr(cli));
                return 1;
        }
@@ -2966,7 +2971,7 @@ static int cmd_getfacl(void)
                return 1;
        }
 
-       if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetname)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
                d_printf("stat %s: %s\n", src, cli_errstr(cli));
                return 1;
        }
@@ -3132,7 +3137,7 @@ static int cmd_stat(void)
                return 1;
        }
 
-       if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetname)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
                d_printf("stat %s: %s\n", src, cli_errstr(cli));
                return 1;
        }
@@ -3233,7 +3238,7 @@ static int cmd_chown(void)
        if (!src) {
                return 1;
        }
-       if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetname) ) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname) ) {
                d_printf("chown %s: %s\n", src, cli_errstr(cli));
                return 1;
        }
@@ -3287,17 +3292,17 @@ static int cmd_rename(void)
                return 1;
        }
 
-       if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetsrc)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetsrc)) {
                d_printf("rename %s: %s\n", src, cli_errstr(cli));
                return 1;
        }
 
-       if (!cli_resolve_path(ctx, "", cli, dest, &targetcli, &targetdest)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, dest, &targetcli, &targetdest)) {
                d_printf("rename %s: %s\n", dest, cli_errstr(cli));
                return 1;
        }
 
-       if (!cli_rename(targetcli, targetsrc, targetdest)) {
+       if (!NT_STATUS_IS_OK(cli_rename(targetcli, targetsrc, targetdest))) {
                d_printf("%s renaming files %s -> %s \n",
                        cli_errstr(targetcli),
                        targetsrc,
@@ -3362,12 +3367,12 @@ static int cmd_hardlink(void)
                return 1;
        }
 
-       if (!cli_resolve_path(ctx, "", cli, src, &targetcli, &targetname)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, src, &targetcli, &targetname)) {
                d_printf("hardlink %s: %s\n", src, cli_errstr(cli));
                return 1;
        }
 
-       if (!cli_nt_hardlink(targetcli, targetname, dest)) {
+       if (!NT_STATUS_IS_OK(cli_nt_hardlink(targetcli, targetname, dest))) {
                d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
                return 1;
        }
@@ -3815,7 +3820,7 @@ static int cmd_logon(void)
 
 static int cmd_list_connect(void)
 {
-       cli_cm_display();
+       cli_cm_display(cli);
        return 0;
 }
 
@@ -3829,7 +3834,7 @@ static int cmd_show_connect( void )
        struct cli_state *targetcli;
        char *targetpath;
 
-       if (!cli_resolve_path(ctx, "", cli, client_get_cur_dir(),
+       if (!cli_resolve_path(ctx, "", auth_info, cli, client_get_cur_dir(),
                                &targetcli, &targetpath ) ) {
                d_printf("showconnect %s: %s\n", cur_dir, cli_errstr(cli));
                return 1;
@@ -4051,8 +4056,9 @@ static int process_command_string(const char *cmd_in)
        if (!cli) {
                cli = cli_cm_open(talloc_tos(), NULL,
                                have_ip ? dest_ss_str : desthost,
-                               service, true, smb_encrypt,
-                               max_protocol);
+                               service, auth_info,
+                               true, smb_encrypt,
+                               max_protocol, port, name_type);
                if (!cli) {
                        return 1;
                }
@@ -4220,7 +4226,7 @@ static char **remote_completion(const char *text, int len)
                goto cleanup;
        }
 
-       if (!cli_resolve_path(ctx, "", cli, dirmask, &targetcli, &targetpath)) {
+       if (!cli_resolve_path(ctx, "", auth_info, cli, dirmask, &targetcli, &targetpath)) {
                goto cleanup;
        }
        if (cli_list(targetcli, targetpath, aDIR | aSYSTEM | aHIDDEN,
@@ -4517,8 +4523,8 @@ static int process(const char *base_directory)
 
        cli = cli_cm_open(talloc_tos(), NULL,
                        have_ip ? dest_ss_str : desthost,
-                       service, true, smb_encrypt,
-                       max_protocol);
+                       service, auth_info, true, smb_encrypt,
+                       max_protocol, port, name_type);
        if (!cli) {
                return 1;
        }
@@ -4526,7 +4532,7 @@ static int process(const char *base_directory)
        if (base_directory && *base_directory) {
                rc = do_cd(base_directory);
                if (rc) {
-                       cli_cm_shutdown();
+                       cli_shutdown(cli);
                        return rc;
                }
        }
@@ -4537,7 +4543,7 @@ static int process(const char *base_directory)
                process_stdin();
        }
 
-       cli_cm_shutdown();
+       cli_shutdown(cli);
        return rc;
 }
 
@@ -4547,20 +4553,24 @@ static int process(const char *base_directory)
 
 static int do_host_query(const char *query_host)
 {
-       struct sockaddr_storage ss;
-
        cli = cli_cm_open(talloc_tos(), NULL,
-                       query_host, "IPC$", true, smb_encrypt,
-                       max_protocol);
+                       query_host, "IPC$", auth_info, true, smb_encrypt,
+                       max_protocol, port, name_type);
        if (!cli)
                return 1;
 
        browse_host(true);
 
-       if (interpret_string_addr(&ss, query_host, 0) && (ss.ss_family != AF_INET)) {
-               d_printf("%s is an IPv6 address -- no workgroup available\n",
-                       query_host);
-               return 1;
+       /* Ensure that the host can do IPv4 */
+
+       if (!interpret_addr(query_host)) {
+               struct sockaddr_storage ss;
+               if (interpret_string_addr(&ss, query_host, 0) &&
+                               (ss.ss_family != AF_INET)) {
+                       d_printf("%s is an IPv6 address -- no workgroup available\n",
+                               query_host);
+                       return 1;
+               }
        }
 
        if (port != 139) {
@@ -4568,11 +4578,10 @@ static int do_host_query(const char *query_host)
                /* Workgroups simply don't make sense over anything
                   else but port 139... */
 
-               cli_cm_shutdown();
-               cli_cm_set_port( 139 );
+               cli_shutdown(cli);
                cli = cli_cm_open(talloc_tos(), NULL,
-                               query_host, "IPC$", true, smb_encrypt,
-                               max_protocol);
+                               query_host, "IPC$", auth_info, true, smb_encrypt,
+                               max_protocol, 139, name_type);
        }
 
        if (cli == NULL) {
@@ -4582,7 +4591,7 @@ static int do_host_query(const char *query_host)
 
        list_servers(lp_workgroup());
 
-       cli_cm_shutdown();
+       cli_shutdown(cli);
 
        return(0);
 }
@@ -4599,8 +4608,8 @@ static int do_tar_op(const char *base_directory)
        if (!cli) {
                cli = cli_cm_open(talloc_tos(), NULL,
                        have_ip ? dest_ss_str : desthost,
-                       service, true, smb_encrypt,
-                       max_protocol);
+                       service, auth_info, true, smb_encrypt,
+                       max_protocol, port, name_type);
                if (!cli)
                        return 1;
        }
@@ -4610,14 +4619,14 @@ static int do_tar_op(const char *base_directory)
        if (base_directory && *base_directory)  {
                ret = do_cd(base_directory);
                if (ret) {
-                       cli_cm_shutdown();
+                       cli_shutdown(cli);
                        return ret;
                }
        }
 
        ret=process_tar();
 
-       cli_cm_shutdown();
+       cli_shutdown(cli);
 
        return(ret);
 }
@@ -4626,7 +4635,7 @@ static int do_tar_op(const char *base_directory)
  Handle a message operation.
 ****************************************************************************/
 
-static int do_message_op(struct user_auth_info *auth_info)
+static int do_message_op(struct user_auth_info *a_info)
 {
        struct sockaddr_storage ss;
        struct nmb_name called, calling;
@@ -4650,10 +4659,11 @@ static int do_message_op(struct user_auth_info *auth_info)
 
        msg_port = port ? port : 139;
 
-       if (!(cli=cli_initialise()) || (cli_set_port(cli, msg_port) != msg_port)) {
+       if (!(cli=cli_initialise())) {
                d_printf("Connection to %s failed\n", desthost);
                return 1;
        }
+       cli_set_port(cli, msg_port);
 
        status = cli_connect(cli, server_name, &ss);
        if (!NT_STATUS_IS_OK(status)) {
@@ -4663,12 +4673,12 @@ static int do_message_op(struct user_auth_info *auth_info)
 
        if (!cli_session_request(cli, &calling, &called)) {
                d_printf("session request failed\n");
-               cli_cm_shutdown();
+               cli_shutdown(cli);
                return 1;
        }
 
-       send_message(get_cmdline_auth_info_username(auth_info));
-       cli_cm_shutdown();
+       send_message(get_cmdline_auth_info_username(a_info));
+       cli_shutdown(cli);
 
        return 0;
 }
@@ -4714,7 +4724,6 @@ static int do_message_op(struct user_auth_info *auth_info)
                POPT_TABLEEND
        };
        TALLOC_CTX *frame = talloc_stackframe();
-       struct user_auth_info *auth_info;
 
        if (!client_set_cur_dir("\\")) {
                exit(ENOMEM);
@@ -4791,13 +4800,12 @@ static int do_message_op(struct user_auth_info *auth_info)
                         * to port 139 instead of port 445. srl,crh
                         */
                        name_type = 0x03;
-                       cli_cm_set_dest_name_type( name_type );
                        desthost = talloc_strdup(frame,poptGetOptArg(pc));
                        if (!desthost) {
                                exit(ENOMEM);
                        }
                        if( !port )
-                               cli_cm_set_port( 139 );
+                               port = 139;
                        message = true;
                        break;
                case 'I':
@@ -4893,11 +4901,6 @@ static int do_message_op(struct user_auth_info *auth_info)
                                               poptGetArg(pc));
        }
 
-       /* check for the -P option */
-
-       if ( port != 0 )
-               cli_cm_set_port( port );
-
        /*
         * Don't load debug level from smb.conf. It should be
         * set by cmdline arg or remain default (0)
@@ -4976,11 +4979,10 @@ static int do_message_op(struct user_auth_info *auth_info)
 
        poptFreeContext(pc);
 
-       /* Store the username and password for dfs support */
-
-       cli_cm_set_credentials(auth_info);
+       DEBUG(3,("Client started (version %s).\n", samba_version_string()));
 
-       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) {
                if (cmdstr)
@@ -5004,7 +5006,6 @@ static int do_message_op(struct user_auth_info *auth_info)
                        *p = 0;
                        p++;
                        sscanf(p, "%x", &name_type);
-                       cli_cm_set_dest_name_type( name_type );
                }
 
                return do_host_query(qhost);