Revamp SSH authentication code so that user interaction is more
[u/mdw/putty] / windows / winplink.c
index b99646a..f6596ee 100644 (file)
@@ -12,7 +12,7 @@
 #include "storage.h"
 #include "tree234.h"
 
-#define WM_AGENT_CALLBACK (WM_XUSER + 4)
+#define WM_AGENT_CALLBACK (WM_APP + 4)
 
 #define MAX_STDIN_BACKLOG 4096
 
@@ -94,6 +94,8 @@ void ldisc_update(void *frontend, int echo, int edit)
     SetConsoleMode(inhandle, mode);
 }
 
+char *get_ttymode(void *frontend, const char *mode) { return NULL; }
+
 struct input_data {
     DWORD len;
     char buffer[4096];
@@ -187,6 +189,25 @@ int from_backend(void *frontend_handle, int is_stderr,
     return osize + esize;
 }
 
+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 get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
+{
+    int ret;
+    ret = cmdline_get_passwd_input(p, in, inlen);
+    if (ret == -1)
+       ret = console_get_userpass_input(p, in, inlen);
+    return ret;
+}
+
 static DWORD main_thread_id;
 
 void agent_schedule_callback(void (*callback)(void *, void *, int),
@@ -210,14 +231,14 @@ static void usage(void)
     printf("Usage: plink [options] [user@]host [command]\n");
     printf("       (\"host\" can also be a PuTTY saved session name)\n");
     printf("Options:\n");
-    printf("  -V        print version information\n");
+    printf("  -V        print version information and exit\n");
+    printf("  -pgpfp    print PGP key fingerprints and exit\n");
     printf("  -v        show verbose messages\n");
     printf("  -load sessname  Load settings from saved session\n");
     printf("  -ssh -telnet -rlogin -raw\n");
     printf("            force use of a particular protocol\n");
     printf("  -P port   connect to specified port\n");
     printf("  -l user   connect with specified username\n");
-    printf("  -m file   read remote command(s) from file\n");
     printf("  -batch    disable all interactive prompts\n");
     printf("The following options only apply to SSH connections:\n");
     printf("  -pw passw login with specified password\n");
@@ -231,8 +252,10 @@ static void usage(void)
     printf("  -A -a     enable / disable agent forwarding\n");
     printf("  -t -T     enable / disable pty allocation\n");
     printf("  -1 -2     force use of particular protocol version\n");
+    printf("  -4 -6     force use of IPv4 or IPv6\n");
     printf("  -C        enable compression\n");
     printf("  -i key    private key file for authentication\n");
+    printf("  -m file   read remote command(s) from file\n");
     printf("  -s        remote command is an SSH subsystem (SSH-2 only)\n");
     printf("  -N        don't start a shell/command (SSH-2 only)\n");
     exit(1);
@@ -270,7 +293,7 @@ int main(int argc, char **argv)
     HANDLE handles[4];
     DWORD in_threadid, out_threadid, err_threadid;
     struct input_data idata;
-    int reading;
+    int reading = FALSE;
     int sending;
     int portnumber = -1;
     SOCKET *sklist;
@@ -279,8 +302,7 @@ int main(int argc, char **argv)
     int exitcode;
     int errors;
     int use_subsystem = 0;
-
-    ssh_get_line = console_get_line;
+    long now, next;
 
     sklist = NULL;
     skcount = sksize = 0;
@@ -337,6 +359,9 @@ int main(int argc, char **argv)
                use_subsystem = 1;
            } else if (!strcmp(p, "-V")) {
                 version();
+            } else if (!strcmp(p, "-pgpfp")) {
+                pgp_fingerprints();
+                exit(1);
            } else {
                fprintf(stderr, "plink: unknown option \"%s\"\n", p);
                errors = 1;
@@ -423,8 +448,6 @@ int main(int argc, char **argv)
                            cfg.port = default_port;
                        } else {
                            cfg = cfg2;
-                           /* Ick: patch up internal pointer after copy */
-                           cfg.remote_cmd_ptr = cfg.remote_cmd;
                        }
                    }
 
@@ -527,7 +550,7 @@ int main(int argc, char **argv)
        cfg.host[p1] = '\0';
     }
 
-    if (!*cfg.remote_cmd_ptr)
+    if (!cfg.remote_cmd_ptr && !*cfg.remote_cmd)
        flags |= FLAG_INTERACTIVE;
 
     /*
@@ -592,16 +615,16 @@ int main(int argc, char **argv)
     inhandle = GetStdHandle(STD_INPUT_HANDLE);
     outhandle = GetStdHandle(STD_OUTPUT_HANDLE);
     errhandle = GetStdHandle(STD_ERROR_HANDLE);
-    GetConsoleMode(inhandle, &orig_console_mode);
-    SetConsoleMode(inhandle, ENABLE_PROCESSED_INPUT);
-
-    main_thread_id = GetCurrentThreadId();
-
     /*
      * Turn off ECHO and LINE input modes. We don't care if this
      * call fails, because we know we aren't necessarily running in
      * a console.
      */
+    GetConsoleMode(inhandle, &orig_console_mode);
+    SetConsoleMode(inhandle, ENABLE_PROCESSED_INPUT);
+
+    main_thread_id = GetCurrentThreadId();
+
     handles[0] = netevent;
     handles[1] = stdinevent;
     handles[2] = stdoutevent;
@@ -631,8 +654,11 @@ int main(int argc, char **argv)
        cleanup_exit(1);
     }
 
+    now = GETTICKCOUNT();
+
     while (1) {
        int n;
+       DWORD ticks;
 
        if (!sending && back->sendok(backhandle)) {
            /*
@@ -659,11 +685,19 @@ int main(int argc, char **argv)
                cleanup_exit(1);
            }
            sending = TRUE;
+           reading = TRUE;
+       }
+
+       if (run_timers(now, &next)) {
+           ticks = next - GETTICKCOUNT();
+           if (ticks < 0) ticks = 0;  /* just in case */
+       } else {
+           ticks = INFINITE;
        }
 
-       n = MsgWaitForMultipleObjects(4, handles, FALSE, INFINITE,
+       n = MsgWaitForMultipleObjects(4, handles, FALSE, ticks,
                                      QS_POSTMESSAGE);
-       if (n == 0) {
+       if (n == WAIT_OBJECT_0 + 0) {
            WSANETWORKEVENTS things;
            SOCKET socket;
            extern SOCKET first_socket(int *), next_socket(int *);
@@ -724,7 +758,7 @@ int main(int argc, char **argv)
                         }
                }
            }
-       } else if (n == 1) {
+       } else if (n == WAIT_OBJECT_0 + 1) {
            reading = 0;
            noise_ultralight(idata.len);
            if (connopen && back->socket(backhandle) != NULL) {
@@ -734,7 +768,7 @@ int main(int argc, char **argv)
                    back->special(backhandle, TS_EOF);
                }
            }
-       } else if (n == 2) {
+       } else if (n == WAIT_OBJECT_0 + 2) {
            odata.busy = 0;
            if (!odata.writeret) {
                fprintf(stderr, "Unable to write to standard output\n");
@@ -747,7 +781,7 @@ int main(int argc, char **argv)
                back->unthrottle(backhandle, bufchain_size(&stdout_data) +
                                 bufchain_size(&stderr_data));
            }
-       } else if (n == 3) {
+       } else if (n == WAIT_OBJECT_0 + 3) {
            edata.busy = 0;
            if (!edata.writeret) {
                fprintf(stderr, "Unable to write to standard output\n");
@@ -760,7 +794,7 @@ int main(int argc, char **argv)
                back->unthrottle(backhandle, bufchain_size(&stdout_data) +
                                 bufchain_size(&stderr_data));
            }
-       } else if (n == 4) {
+       } else if (n == WAIT_OBJECT_0 + 4) {
            MSG msg;
            while (PeekMessage(&msg, INVALID_HANDLE_VALUE,
                               WM_AGENT_CALLBACK, WM_AGENT_CALLBACK,
@@ -770,6 +804,13 @@ int main(int argc, char **argv)
                sfree(c);
            }
        }
+
+       if (n == WAIT_TIMEOUT) {
+           now = next;
+       } else {
+           now = GETTICKCOUNT();
+       }
+
        if (!reading && back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) {
            SetEvent(idata.eventback);
            reading = 1;