Jacob's patch for a drag-list to select SSH ciphers. Heavily hacked
[u/mdw/putty] / psftp.c
diff --git a/psftp.c b/psftp.c
index fbf730a..ef99cde 100644 (file)
--- a/psftp.c
+++ b/psftp.c
 #include "sftp.h"
 #include "int64.h"
 
+/*
+ * Since SFTP is a request-response oriented protocol, it requires
+ * no buffer management: when we send data, we stop and wait for an
+ * acknowledgement _anyway_, and so we can't possibly overfill our
+ * send buffer.
+ */
+
 /* ----------------------------------------------------------------------
  * String handling routines.
  */
@@ -460,6 +467,94 @@ int sftp_cmd_put(struct sftp_command *cmd)
     return 0;
 }
 
+int sftp_cmd_mkdir(struct sftp_command *cmd)
+{
+    char *dir;
+    int result;
+
+
+    if (cmd->nwords < 2) {
+       printf("mkdir: expects a directory\n");
+       return 0;
+    }
+
+    dir = canonify(cmd->words[1]);
+    if (!dir) {
+       printf("%s: %s\n", dir, fxp_error());
+       return 0;
+    }
+
+    result = fxp_mkdir(dir);
+    if (!result) {
+       printf("mkdir %s: %s\n", dir, fxp_error());
+       sfree(dir);
+       return 0;
+    }
+
+       sfree(dir);
+       return 0;
+
+}
+
+int sftp_cmd_rmdir(struct sftp_command *cmd)
+{
+    char *dir;
+    int result;
+
+
+    if (cmd->nwords < 2) {
+       printf("rmdir: expects a directory\n");
+       return 0;
+    }
+
+    dir = canonify(cmd->words[1]);
+    if (!dir) {
+       printf("%s: %s\n", dir, fxp_error());
+       return 0;
+    }
+
+    result = fxp_rmdir(dir);
+    if (!result) {
+       printf("rmdir %s: %s\n", dir, fxp_error());
+       sfree(dir);
+       return 0;
+    }
+
+       sfree(dir);
+       return 0;
+
+}
+
+int sftp_cmd_rm(struct sftp_command *cmd)
+{
+    char *fname;
+    int result;
+
+
+    if (cmd->nwords < 2) {
+       printf("rm: expects a filename\n");
+       return 0;
+    }
+
+    fname = canonify(cmd->words[1]);
+    if (!fname) {
+       printf("%s: %s\n", fname, fxp_error());
+       return 0;
+    }
+
+    result = fxp_rm(fname);
+    if (!result) {
+       printf("rm %s: %s\n", fname, fxp_error());
+       sfree(fname);
+       return 0;
+    }
+
+       sfree(fname);
+       return 0;
+
+}
+
+
 static struct sftp_cmd_lookup {
     char *name;
     int (*obey) (struct sftp_command *);
@@ -475,13 +570,16 @@ static struct sftp_cmd_lookup {
     "exit", sftp_cmd_quit}, {
     "get", sftp_cmd_get}, {
     "ls", sftp_cmd_ls}, {
+    "mkdir", sftp_cmd_mkdir}, {
     "put", sftp_cmd_put}, {
-"quit", sftp_cmd_quit},};
+       "quit", sftp_cmd_quit}, {
+       "rm", sftp_cmd_rm}, {
+       "rmdir", sftp_cmd_rmdir},};
 
 /* ----------------------------------------------------------------------
  * Command line reading and parsing.
  */
-struct sftp_command *sftp_getcmd(void)
+struct sftp_command *sftp_getcmd(FILE *fp, int mode, int modeflags)
 {
     char *line;
     int linelen, linesize;
@@ -489,7 +587,9 @@ struct sftp_command *sftp_getcmd(void)
     char *p, *q, *r;
     int quoting;
 
-    printf("psftp> ");
+       if ((mode == 0) || (modeflags & 1)) {
+           printf("psftp> ");
+       }
     fflush(stdout);
 
     cmd = smalloc(sizeof(struct sftp_command));
@@ -505,7 +605,10 @@ struct sftp_command *sftp_getcmd(void)
 
        linesize += 512;
        line = srealloc(line, linesize);
-       ret = fgets(line + linelen, linesize - linelen, stdin);
+       ret = fgets(line + linelen, linesize - linelen, fp);
+       if (modeflags & 1) {
+               printf("%s", ret);
+       }
 
        if (!ret || (linelen == 0 && line[0] == '\0')) {
            cmd->obey = sftp_cmd_quit;
@@ -596,8 +699,10 @@ struct sftp_command *sftp_getcmd(void)
     return cmd;
 }
 
-void do_sftp(void)
+void do_sftp(int mode, int modeflags, char *batchfile)
 {
+    FILE *fp;
+
     /*
      * Do protocol initialisation. 
      */
@@ -621,16 +726,42 @@ void do_sftp(void)
     }
     pwd = dupstr(homedir);
 
-    /* ------------------------------------------------------------------
-     * Now we're ready to do Real Stuff.
+    /*
+     * Batch mode?
      */
-    while (1) {
-       struct sftp_command *cmd;
-       cmd = sftp_getcmd();
-       if (!cmd)
-           break;
-       if (cmd->obey(cmd) < 0)
-           break;
+    if (mode == 0) {
+
+        /* ------------------------------------------------------------------
+         * Now we're ready to do Real Stuff.
+         */
+        while (1) {
+       struct sftp_command *cmd;
+       cmd = sftp_getcmd(stdin, 0, 0);
+       if (!cmd)
+           break;
+               if (cmd->obey(cmd) < 0)
+                   break;
+           }
+    } else {
+        fp = fopen(batchfile, "r");
+        if (!fp) {
+        printf("Fatal: unable to open %s\n", batchfile);
+        return;
+        }
+        while (1) {
+       struct sftp_command *cmd;
+       cmd = sftp_getcmd(fp, mode, modeflags);
+       if (!cmd)
+           break;
+               if (cmd->obey(cmd) < 0)
+                   break;
+               if (fxp_error() != NULL) {
+                       if (!(modeflags & 2))
+                               break;
+               }
+        }
+           fclose(fp);
+
     }
 }
 
@@ -644,6 +775,8 @@ void verify_ssh_host_key(char *host, int port, char *keytype,
                         char *keystr, char *fingerprint)
 {
     int ret;
+    HANDLE hin;
+    DWORD savemode, i;
 
     static const char absentmsg[] =
        "The server's host key is not cached in the registry. You\n"
@@ -653,8 +786,11 @@ void verify_ssh_host_key(char *host, int port, char *keytype,
        "%s\n"
        "If you trust this host, enter \"y\" to add the key to\n"
        "PuTTY's cache and carry on connecting.\n"
-       "If you do not trust this host, enter \"n\" to abandon the\n"
-       "connection.\n" "Continue connecting? (y/n) ";
+       "If you want to carry on connecting just once, without\n"
+       "adding the key to the cache, enter \"n\".\n"
+       "If you do not trust this host, press Return to abandon the\n"
+       "connection.\n"
+       "Store key in cache? (y/n) ";
 
     static const char wrongmsg[] =
        "WARNING - POTENTIAL SECURITY BREACH!\n"
@@ -666,9 +802,9 @@ void verify_ssh_host_key(char *host, int port, char *keytype,
        "The new key fingerprint is:\n"
        "%s\n"
        "If you were expecting this change and trust the new key,\n"
-       "enter Yes to update PuTTY's cache and continue connecting.\n"
+       "enter \"y\" to update PuTTY's cache and continue connecting.\n"
        "If you want to carry on connecting but without updating\n"
-       "the cache, enter No.\n"
+       "the cache, enter \"n\".\n"
        "If you want to abandon the connection completely, press\n"
        "Return to cancel. Pressing Return is the ONLY guaranteed\n"
        "safe choice.\n"
@@ -685,26 +821,69 @@ void verify_ssh_host_key(char *host, int port, char *keytype,
 
     if (ret == 0)                     /* success - key matched OK */
        return;
+
     if (ret == 2) {                   /* key was different */
        fprintf(stderr, wrongmsg, fingerprint);
-       if (fgets(line, sizeof(line), stdin) &&
-           line[0] != '\0' && line[0] != '\n') {
-           if (line[0] == 'y' || line[0] == 'Y')
-               store_host_key(host, port, keytype, keystr);
-       } else {
-           fprintf(stderr, abandoned);
-           exit(0);
-       }
+       fflush(stderr);
     }
     if (ret == 1) {                   /* key was absent */
        fprintf(stderr, absentmsg, fingerprint);
-       if (fgets(line, sizeof(line), stdin) &&
-           (line[0] == 'y' || line[0] == 'Y'))
+       fflush(stderr);
+    }
+
+    hin = GetStdHandle(STD_INPUT_HANDLE);
+    GetConsoleMode(hin, &savemode);
+    SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
+                        ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
+    ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
+    SetConsoleMode(hin, savemode);
+
+    if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') {
+       if (line[0] == 'y' || line[0] == 'Y')
            store_host_key(host, port, keytype, keystr);
-       else {
-           fprintf(stderr, abandoned);
-           exit(0);
-       }
+    } else {
+       fprintf(stderr, abandoned);
+       exit(0);
+    }
+}
+
+/*
+ * Ask whether the selected cipher is acceptable (since it was
+ * below the configured 'warn' threshold).
+ * cs: 0 = both ways, 1 = client->server, 2 = server->client
+ */
+void askcipher(char *ciphername, int cs)
+{
+    HANDLE hin;
+    DWORD savemode, i;
+
+    static const char msg[] =
+       "The first %scipher supported by the server is\n"
+       "%s, which is below the configured warning threshold.\n"
+       "Continue with connection? (y/n) ";
+    static const char abandoned[] = "Connection abandoned.\n";
+
+    char line[32];
+
+    fprintf(stderr, msg,
+           (cs == 0) ? "" :
+           (cs == 1) ? "client-to-server " :
+                       "server-to-client ",
+           ciphername);
+    fflush(stderr);
+
+    hin = GetStdHandle(STD_INPUT_HANDLE);
+    GetConsoleMode(hin, &savemode);
+    SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
+                        ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
+    ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
+    SetConsoleMode(hin, savemode);
+
+    if (line[0] == 'y' || line[0] == 'Y') {
+       return;
+    } else {
+       fprintf(stderr, abandoned);
+       exit(0);
     }
 }
 
@@ -780,7 +959,7 @@ static unsigned char *outptr;              /* where to put the data */
 static unsigned outlen;                       /* how much data required */
 static unsigned char *pending = NULL;  /* any spare data */
 static unsigned pendlen = 0, pendsize = 0;     /* length and phys. size of buffer */
-void from_backend(int is_stderr, char *data, int datalen)
+int from_backend(int is_stderr, char *data, int datalen)
 {
     unsigned char *p = (unsigned char *) data;
     unsigned len = (unsigned) datalen;
@@ -791,14 +970,14 @@ void from_backend(int is_stderr, char *data, int datalen)
      */
     if (is_stderr) {
        fwrite(data, 1, len, stderr);
-       return;
+       return 0;
     }
 
     /*
      * If this is before the real session begins, just return.
      */
     if (!outptr)
-       return;
+       return 0;
 
     if (outlen > 0) {
        unsigned used = outlen;
@@ -822,6 +1001,8 @@ void from_backend(int is_stderr, char *data, int datalen)
        memcpy(pending + pendlen, p, len);
        pendlen += len;
     }
+
+    return 0;
 }
 int sftp_recvdata(char *buf, int len)
 {
@@ -964,6 +1145,9 @@ static void usage(void)
     printf("%s\n", ver);
     printf("Usage: psftp [options] user@host\n");
     printf("Options:\n");
+    printf("  -b file   use specified batchfile\n");
+    printf("  -bc       output batchfile commands\n");
+    printf("  -be       don't stop batchfile processing if errors\n");
     printf("  -v        show verbose messages\n");
     printf("  -P port   connect to specified port\n");
     printf("  -pw passw login with specified password\n");
@@ -979,6 +1163,9 @@ int main(int argc, char *argv[])
     int portnumber = 0;
     char *user, *host, *userhost, *realhost;
     char *err;
+    int mode = 0;
+    int modeflags = 0;
+    char *batchfile = NULL;
 
     flags = FLAG_STDERR;
     ssh_get_line = &get_line;
@@ -1004,6 +1191,13 @@ int main(int argc, char *argv[])
            portnumber = atoi(argv[++i]);
        } else if (strcmp(argv[i], "-pw") == 0 && i + 1 < argc) {
            password = argv[++i];
+    } else if (strcmp(argv[i], "-b") == 0 && i + 1 < argc) {
+           mode = 1;
+        batchfile = argv[++i];
+    } else if (strcmp(argv[i], "-bc") == 0 && i + 1 < argc) {
+           modeflags = modeflags | 1;
+    } else if (strcmp(argv[i], "-be") == 0 && i + 1 < argc) {
+           modeflags = modeflags | 2;
        } else if (strcmp(argv[i], "--") == 0) {
            i++;
            break;
@@ -1084,7 +1278,7 @@ int main(int argc, char *argv[])
     if (verbose && realhost != NULL)
        printf("Connected to %s\n", realhost);
 
-    do_sftp();
+    do_sftp(mode, modeflags, batchfile);
 
     if (back != NULL && back->socket() != NULL) {
        char ch;