Revamp SSH authentication code so that user interaction is more
[u/mdw/putty] / psftp.c
diff --git a/psftp.c b/psftp.c
index 48c12a4..2705d45 100644 (file)
--- a/psftp.c
+++ b/psftp.c
@@ -189,6 +189,11 @@ static int bare_name_compare(const void *av, const void *bv)
     return strcmp(*a, *b);
 }
 
+static void not_connected(void)
+{
+    printf("psftp: not connected to a host; use \"open host.name\"\n");
+}
+
 /* ----------------------------------------------------------------------
  * The meat of the `get' and `put' commands.
  */
@@ -936,7 +941,7 @@ int sftp_cmd_quit(struct sftp_command *cmd)
 int sftp_cmd_close(struct sftp_command *cmd)
 {
     if (back == NULL) {
-       printf("psftp: not connected to a host; use \"open host.name\"\n");
+       not_connected();
        return 0;
     }
 
@@ -966,7 +971,7 @@ int sftp_cmd_ls(struct sftp_command *cmd)
     int i;
 
     if (back == NULL) {
-       printf("psftp: not connected to a host; use \"open host.name\"\n");
+       not_connected();
        return 0;
     }
 
@@ -1088,7 +1093,7 @@ int sftp_cmd_cd(struct sftp_command *cmd)
     char *dir;
 
     if (back == NULL) {
-       printf("psftp: not connected to a host; use \"open host.name\"\n");
+       not_connected();
        return 0;
     }
 
@@ -1131,7 +1136,7 @@ int sftp_cmd_cd(struct sftp_command *cmd)
 int sftp_cmd_pwd(struct sftp_command *cmd)
 {
     if (back == NULL) {
-       printf("psftp: not connected to a host; use \"open host.name\"\n");
+       not_connected();
        return 0;
     }
 
@@ -1155,7 +1160,7 @@ int sftp_general_get(struct sftp_command *cmd, int restart, int multiple)
     int recurse = FALSE;
 
     if (back == NULL) {
-       printf("psftp: not connected to a host; use \"open host.name\"\n");
+       not_connected();
        return 0;
     }
 
@@ -1269,7 +1274,7 @@ int sftp_general_put(struct sftp_command *cmd, int restart, int multiple)
     int recurse = FALSE;
 
     if (back == NULL) {
-       printf("psftp: not connected to a host; use \"open host.name\"\n");
+       not_connected();
        return 0;
     }
 
@@ -1370,7 +1375,7 @@ int sftp_cmd_mkdir(struct sftp_command *cmd)
     int i, ret;
 
     if (back == NULL) {
-       printf("psftp: not connected to a host; use \"open host.name\"\n");
+       not_connected();
        return 0;
     }
 
@@ -1394,9 +1399,9 @@ int sftp_cmd_mkdir(struct sftp_command *cmd)
 
        if (!result) {
            printf("mkdir %s: %s\n", dir, fxp_error());
-           sfree(dir);
            ret = 0;
-       }
+       } else
+           printf("mkdir %s: OK\n", dir);
 
        sfree(dir);
     }
@@ -1420,6 +1425,8 @@ static int sftp_action_rmdir(void *vctx, char *dir)
        return 0;
     }
 
+    printf("rmdir %s: OK\n", dir);
+
     return 1;
 }
 
@@ -1428,7 +1435,7 @@ int sftp_cmd_rmdir(struct sftp_command *cmd)
     int i, ret;
 
     if (back == NULL) {
-       printf("psftp: not connected to a host; use \"open host.name\"\n");
+       not_connected();
        return 0;
     }
 
@@ -1457,10 +1464,11 @@ static int sftp_action_rm(void *vctx, char *fname)
 
     if (!result) {
        printf("rm %s: %s\n", fname, fxp_error());
-       sfree(fname);
        return 0;
     }
 
+    printf("rm %s: OK\n", fname);
+
     return 1;
 }
 
@@ -1469,7 +1477,7 @@ int sftp_cmd_rm(struct sftp_command *cmd)
     int i, ret;
 
     if (back == NULL) {
-       printf("psftp: not connected to a host; use \"open host.name\"\n");
+       not_connected();
        return 0;
     }
 
@@ -1564,7 +1572,7 @@ int sftp_cmd_mv(struct sftp_command *cmd)
     int i, ret;
 
     if (back == NULL) {
-       printf("psftp: not connected to a host; use \"open host.name\"\n");
+       not_connected();
        return 0;
     }
 
@@ -1624,7 +1632,6 @@ static int sftp_action_chmod(void *vctx, char *fname)
     if (!result || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS)) {
        printf("get attrs for %s: %s\n", fname,
               result ? "file permissions not provided" : fxp_error());
-       sfree(fname);
        return 0;
     }
 
@@ -1644,7 +1651,6 @@ static int sftp_action_chmod(void *vctx, char *fname)
 
     if (!result) {
        printf("set attrs for %s: %s\n", fname, fxp_error());
-       sfree(fname);
        return 0;
     }
 
@@ -1660,7 +1666,7 @@ int sftp_cmd_chmod(struct sftp_command *cmd)
     struct sftp_context_chmod actx, *ctx = &actx;
 
     if (back == NULL) {
-       printf("psftp: not connected to a host; use \"open host.name\"\n");
+       not_connected();
        return 0;
     }
 
@@ -1894,7 +1900,7 @@ static struct sftp_cmd_lookup {
     },
     {
        "cd", TRUE, "change your remote working directory",
-           " [ <New working directory> ]\n"
+           " [ <new working directory> ]\n"
            "  Change the remote working directory for your SFTP session.\n"
            "  If a new working directory is not supplied, you will be\n"
            "  returned to your home directory.\n",
@@ -1902,10 +1908,11 @@ static struct sftp_cmd_lookup {
     },
     {
        "chmod", TRUE, "change file permissions and modes",
-           " ( <octal-digits> | <modifiers> ) <filename>\n"
-           "  Change the file permissions on a file or directory.\n"
-           "  <octal-digits> can be any octal Unix permission specifier.\n"
-           "  Alternatively, <modifiers> can include:\n"
+           " <modes> <filename-or-wildcard> [ <filename-or-wildcard>... ]\n"
+           "  Change the file permissions on one or more remote files or\n"
+           "  directories.\n"
+           "  <modes> can be any octal Unix permission specifier.\n"
+           "  Alternatively, <modes> can include the following modifiers:\n"
            "    u+r     make file readable by owning user\n"
            "    u+w     make file writable by owning user\n"
            "    u+x     make file executable by owning user\n"
@@ -1936,16 +1943,16 @@ static struct sftp_cmd_lookup {
            sftp_cmd_close
     },
     {
-       "del", TRUE, "delete a file",
-           " <filename>\n"
-           "  Delete a file.\n",
+       "del", TRUE, "delete files on the remote server",
+           " <filename-or-wildcard> [ <filename-or-wildcard>... ]\n"
+           "  Delete a file or files from the server.\n",
            sftp_cmd_rm
     },
     {
        "delete", FALSE, "del", NULL, sftp_cmd_rm
     },
     {
-       "dir", TRUE, "list contents of a remote directory",
+       "dir", TRUE, "list remote files",
            " [ <directory-name> ]/[ <wildcard> ]\n"
            "  List the contents of a specified directory on the server.\n"
            "  If <directory-name> is not given, the current working directory\n"
@@ -2002,9 +2009,9 @@ static struct sftp_cmd_lookup {
            sftp_cmd_mget
     },
     {
-       "mkdir", TRUE, "create a directory on the remote server",
-           " <directory-name>\n"
-           "  Creates a directory with the given name on the server.\n",
+       "mkdir", TRUE, "create directories on the remote server",
+           " <directory-name> [ <directory-name>... ]\n"
+           "  Creates directories with the given names on the server.\n",
            sftp_cmd_mkdir
     },
     {
@@ -2017,18 +2024,22 @@ static struct sftp_cmd_lookup {
            sftp_cmd_mput
     },
     {
-       "mv", TRUE, "move or rename a file on the remote server",
-           " <source-filename> <destination-filename>\n"
-           "  Moves or renames the file <source-filename> on the server,\n"
-           "  so that it is accessible under the name <destination-filename>.\n",
+       "mv", TRUE, "move or rename file(s) on the remote server",
+           " <source> [ <source>... ] <destination>\n"
+           "  Moves or renames <source>(s) on the server to <destination>,\n"
+           "  also on the server.\n"
+           "  If <destination> specifies an existing directory, then <source>\n"
+           "  may be a wildcard, and multiple <source>s may be given; all\n"
+           "  source files are moved into <destination>.\n"
+           "  Otherwise, <source> must specify a single file, which is moved\n"
+           "  or renamed so that it is accessible under the name <destination>.\n",
            sftp_cmd_mv
     },
     {
        "open", TRUE, "connect to a host",
            " [<user>@]<hostname> [<port>]\n"
            "  Establishes an SFTP connection to a given host. Only usable\n"
-           "  when you did not already specify a host name on the command\n"
-           "  line.\n",
+           "  when you are not already connected to a server.\n",
            sftp_cmd_open
     },
     {
@@ -2051,7 +2062,7 @@ static struct sftp_cmd_lookup {
            sftp_cmd_quit
     },
     {
-       "reget", TRUE, "continue downloading a file",
+       "reget", TRUE, "continue downloading files",
            " [ -r ] [ -- ] <filename> [ <local-filename> ]\n"
            "  Works exactly like the \"get\" command, but the local file\n"
            "  must already exist. The download will begin at the end of the\n"
@@ -2068,7 +2079,7 @@ static struct sftp_cmd_lookup {
            sftp_cmd_mv
     },
     {
-       "reput", TRUE, "continue uploading a file",
+       "reput", TRUE, "continue uploading files",
            " [ -r ] [ -- ] <filename> [ <remote-filename> ]\n"
            "  Works exactly like the \"put\" command, but the remote file\n"
            "  must already exist. The upload will begin at the end of the\n"
@@ -2081,10 +2092,11 @@ static struct sftp_cmd_lookup {
            sftp_cmd_rm
     },
     {
-       "rmdir", TRUE, "remove a directory on the remote server",
-           " <directory-name>\n"
+       "rmdir", TRUE, "remove directories on the remote server",
+           " <directory-name> [ <directory-name>... ]\n"
            "  Removes the directory with the given name on the server.\n"
-           "  The directory will not be removed unless it is empty.\n",
+           "  The directory will not be removed unless it is empty.\n"
+           "  Wildcards may be used to specify multiple directories.\n",
            sftp_cmd_rmdir
     }
 };
@@ -2511,6 +2523,15 @@ int from_backend(void *frontend, int is_stderr, const char *data, int datalen)
 
     return 0;
 }
+int from_backend_untrusted(void *frontend_handle, const char *data, int len)
+{
+    /*
+     * No "untrusted" output should get here (the way the code is
+     * currently, it's all diverted by FLAG_STDERR).
+     */
+    assert(!"Unexpected call to from_backend_untrusted()");
+    return 0; /* not reached */
+}
 int sftp_recvdata(char *buf, int len)
 {
     outptr = (unsigned char *) buf;
@@ -2560,6 +2581,8 @@ static void usage(void)
     printf("%s\n", ver);
     printf("Usage: psftp [options] [user@]host\n");
     printf("Options:\n");
+    printf("  -V        print version information and exit\n");
+    printf("  -pgpfp    print PGP key fingerprints and exit\n");
     printf("  -b file   use specified batchfile\n");
     printf("  -bc       output batchfile commands\n");
     printf("  -be       don't stop batchfile processing if errors\n");
@@ -2573,7 +2596,6 @@ static void usage(void)
     printf("  -C        enable compression\n");
     printf("  -i key    private key file for authentication\n");
     printf("  -batch    disable all interactive prompts\n");
-    printf("  -V        print version information\n");
     cleanup_exit(1);
 }
 
@@ -2701,14 +2723,21 @@ static int psftp_connect(char *userhost, char *user, int portnumber)
        cfg.username[sizeof(cfg.username) - 1] = '\0';
     }
     if (!cfg.username[0]) {
-       if (!console_get_line("login as: ",
-                             cfg.username, sizeof(cfg.username), FALSE)) {
+        /* FIXME: leave this to ssh.c? */
+        int ret;
+        prompts_t *p = new_prompts(NULL);
+        p->to_server = TRUE;
+        p->name = dupstr("SSH login name");
+        add_prompt(p, dupstr("login as: "), TRUE, lenof(cfg.username));
+        ret = get_userpass_input(p, NULL, 0);
+        assert(ret >= 0);
+        if (!ret) {
+            free_prompts(p);
            fprintf(stderr, "psftp: no username, aborting\n");
            cleanup_exit(1);
        } else {
-           int len = strlen(cfg.username);
-           if (cfg.username[len - 1] == '\n')
-               cfg.username[len - 1] = '\0';
+            memcpy(cfg.username, p->prompts[0]->result, lenof(cfg.username));
+            free_prompts(p);
        }
     }
 
@@ -2730,7 +2759,7 @@ static int psftp_connect(char *userhost, char *user, int portnumber)
     cfg.nopty = TRUE;
 
     /*
-     * Set up fallback option, for SSH1 servers or servers with the
+     * Set up fallback option, for SSH-1 servers or servers with the
      * sftp subsystem not enabled but the server binary installed
      * in the usual place. We only support fallback on Unix
      * systems, and we use a kludgy piece of shellery which should
@@ -2806,7 +2835,6 @@ int psftp_main(int argc, char *argv[])
 #endif
        ;
     cmdline_tooltype = TOOLTYPE_FILETRANSFER;
-    ssh_get_line = &console_get_line;
     sk_init();
 
     userhost = user = NULL;
@@ -2837,6 +2865,9 @@ int psftp_main(int argc, char *argv[])
        } else if (strcmp(argv[i], "-h") == 0 ||
                   strcmp(argv[i], "-?") == 0) {
            usage();
+        } else if (strcmp(argv[i], "-pgpfp") == 0) {
+            pgp_fingerprints();
+            return 1;
        } else if (strcmp(argv[i], "-V") == 0) {
            version();
        } else if (strcmp(argv[i], "-batch") == 0) {