Set WM_TRANSIENT_FOR appropriately on the "about" box so that fvwm doesn't
[u/mdw/putty] / unix / uxcons.c
index 16e69fb..0468f3f 100644 (file)
@@ -161,7 +161,8 @@ int verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
        newmode.c_lflag |= ECHO | ISIG | ICANON;
        tcsetattr(0, TCSANOW, &newmode);
        line[0] = '\0';
-       read(0, line, sizeof(line) - 1);
+       if (read(0, line, sizeof(line) - 1) <= 0)
+           /* handled below */;
        tcsetattr(0, TCSANOW, &oldmode);
     }
 
@@ -213,7 +214,8 @@ int askalg(void *frontend, const char *algtype, const char *algname,
        newmode.c_lflag |= ECHO | ISIG | ICANON;
        tcsetattr(0, TCSANOW, &newmode);
        line[0] = '\0';
-       read(0, line, sizeof(line) - 1);
+       if (read(0, line, sizeof(line) - 1) <= 0)
+           /* handled below */;
        tcsetattr(0, TCSANOW, &oldmode);
     }
 
@@ -266,7 +268,8 @@ int askappend(void *frontend, Filename filename,
        newmode.c_lflag |= ECHO | ISIG | ICANON;
        tcsetattr(0, TCSANOW, &newmode);
        line[0] = '\0';
-       read(0, line, sizeof(line) - 1);
+       if (read(0, line, sizeof(line) - 1) <= 0)
+           /* handled below */;
        tcsetattr(0, TCSANOW, &oldmode);
     }
 
@@ -322,18 +325,32 @@ void logevent(void *frontend, const char *string)
     postmsg(&cf);
 }
 
-static void console_data_untrusted(const char *data, int len)
+/*
+ * Special function to print text to the console for password
+ * prompts and the like. Uses /dev/tty or stderr, in that order of
+ * preference; also sanitises escape sequences out of the text, on
+ * the basis that it might have been sent by a hostile SSH server
+ * doing malicious keyboard-interactive.
+ */
+static void console_prompt_text(FILE **confp, const char *data, int len)
 {
     int i;
+
+    if (!*confp) {
+       if ((*confp = fopen("/dev/tty", "w")) == NULL)
+           *confp = stderr;
+    }
+
     for (i = 0; i < len; i++)
        if ((data[i] & 0x60) || (data[i] == '\n'))
-           fputc(data[i], stdout);
-    fflush(stdout);
+           fputc(data[i], *confp);
+    fflush(*confp);
 }
 
 int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
 {
     size_t curr_prompt;
+    FILE *confp = NULL;
 
     /*
      * Zero all the results, in case we abort half-way through.
@@ -344,7 +361,7 @@ int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
            memset(p->prompts[i]->result, 0, p->prompts[i]->result_len);
     }
 
-    if (console_batch_mode)
+    if (p->n_prompts && console_batch_mode)
        return 0;
 
     /*
@@ -353,16 +370,16 @@ int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
     /* We only print the `name' caption if we have to... */
     if (p->name_reqd && p->name) {
        size_t l = strlen(p->name);
-       console_data_untrusted(p->name, l);
+       console_prompt_text(&confp, p->name, l);
        if (p->name[l-1] != '\n')
-           console_data_untrusted("\n", 1);
+           console_prompt_text(&confp, "\n", 1);
     }
     /* ...but we always print any `instruction'. */
     if (p->instruction) {
        size_t l = strlen(p->instruction);
-       console_data_untrusted(p->instruction, l);
+       console_prompt_text(&confp, p->instruction, l);
        if (p->instruction[l-1] != '\n')
-           console_data_untrusted("\n", 1);
+           console_prompt_text(&confp, "\n", 1);
     }
 
     for (curr_prompt = 0; curr_prompt < p->n_prompts; curr_prompt++) {
@@ -380,7 +397,7 @@ int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
            newmode.c_lflag |= ECHO;
        tcsetattr(0, TCSANOW, &newmode);
 
-       console_data_untrusted(pr->prompt, strlen(pr->prompt));
+       console_prompt_text(&confp, pr->prompt, strlen(pr->prompt));
 
        i = read(0, pr->result, pr->result_len - 1);
 
@@ -391,12 +408,14 @@ int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
        pr->result[i] = '\0';
 
        if (!pr->echo)
-           fputs("\n", stdout);
+           console_prompt_text(&confp, "\n", 1);
 
     }
 
-    return 1; /* success */
+    if (confp && confp != stderr)
+       fclose(confp);
 
+    return 1; /* success */
 }
 
 void frontend_keypress(void *handle)
@@ -416,8 +435,6 @@ int is_interactive(void)
  * X11-forwarding-related things suitable for console.
  */
 
-const char platform_x11_best_transport[] = "unix";
-
 char *platform_get_x_display(void) {
     return dupstr(getenv("DISPLAY"));
 }