]> asedeno.scripts.mit.edu Git - PuTTY.git/blobdiff - unix/uxpgnt.c
Giant const-correctness patch of doom!
[PuTTY.git] / unix / uxpgnt.c
index 6e5923c2b5068f7aea81402e2950117a8312e285..95646b3e4fafdb2c8dacd48d6954979e2cf0992c 100644 (file)
@@ -23,7 +23,7 @@
 SockAddr unix_sock_addr(const char *path);
 Socket new_unix_listener(SockAddr listenaddr, Plug plug);
 
-void fatalbox(char *p, ...)
+void fatalbox(const char *p, ...)
 {
     va_list ap;
     fprintf(stderr, "FATAL ERROR: ");
@@ -33,7 +33,7 @@ void fatalbox(char *p, ...)
     fputc('\n', stderr);
     exit(1);
 }
-void modalfatalbox(char *p, ...)
+void modalfatalbox(const char *p, ...)
 {
     va_list ap;
     fprintf(stderr, "FATAL ERROR: ");
@@ -43,7 +43,7 @@ void modalfatalbox(char *p, ...)
     fputc('\n', stderr);
     exit(1);
 }
-void nonfatal(char *p, ...)
+void nonfatal(const char *p, ...)
 {
     va_list ap;
     fprintf(stderr, "ERROR: ");
@@ -52,7 +52,7 @@ void nonfatal(char *p, ...)
     va_end(ap);
     fputc('\n', stderr);
 }
-void connection_fatal(void *frontend, char *p, ...)
+void connection_fatal(void *frontend, const char *p, ...)
 {
     va_list ap;
     fprintf(stderr, "FATAL ERROR: ");
@@ -62,7 +62,7 @@ void connection_fatal(void *frontend, char *p, ...)
     fputc('\n', stderr);
     exit(1);
 }
-void cmdline_error(char *p, ...)
+void cmdline_error(const char *p, ...)
 {
     va_list ap;
     fprintf(stderr, "pageant: ");
@@ -240,7 +240,8 @@ typedef enum {
     KEYACT_CLIENT_DEL,
     KEYACT_CLIENT_DEL_ALL,
     KEYACT_CLIENT_LIST,
-    KEYACT_CLIENT_LIST_FULL,
+    KEYACT_CLIENT_PUBLIC_OPENSSH,
+    KEYACT_CLIENT_PUBLIC
 } keyact;
 struct cmdline_key_action {
     struct cmdline_key_action *next;
@@ -268,6 +269,21 @@ void add_keyact(keyact action, const char *filename)
     keyact_tail = a;
 }
 
+int have_controlling_tty(void)
+{
+    int fd = open("/dev/tty", O_RDONLY);
+    if (fd < 0) {
+        if (errno != ENXIO) {
+            perror("/dev/tty: open");
+            exit(1);
+        }
+        return FALSE;
+    } else {
+        close(fd);
+        return TRUE;
+    }
+}
+
 char **exec_args = NULL;
 enum {
     LIFE_UNSPEC, LIFE_X11, LIFE_TTY, LIFE_DEBUG, LIFE_PERM, LIFE_EXEC
@@ -276,30 +292,50 @@ const char *display = NULL;
 
 static char *askpass(const char *comment)
 {
-    prompts_t *p = new_prompts(NULL);
-    int ret;
-
-    /*
-     * FIXME: if we don't have a terminal, and have to do this by X11,
-     * there's a big missing piece.
-     */
-
-    p->to_server = FALSE;
-    p->name = dupstr("Pageant passphrase prompt");
-    add_prompt(p,
-               dupprintf("Enter passphrase to load key '%s': ", comment),
-               FALSE);
-    ret = console_get_userpass_input(p, NULL, 0);
-    assert(ret >= 0);
-
-    if (!ret) {
-        perror("pageant: unable to read passphrase");
-        free_prompts(p);
-        return NULL;
-    } else {
-        char *passphrase = dupstr(p->prompts[0]->result);
-        free_prompts(p);
+    if (have_controlling_tty()) {
+        int ret;
+        prompts_t *p = new_prompts(NULL);
+        p->to_server = FALSE;
+        p->name = dupstr("Pageant passphrase prompt");
+        add_prompt(p,
+                   dupprintf("Enter passphrase to load key '%s': ", comment),
+                   FALSE);
+        ret = console_get_userpass_input(p, NULL, 0);
+        assert(ret >= 0);
+
+        if (!ret) {
+            perror("pageant: unable to read passphrase");
+            free_prompts(p);
+            return NULL;
+        } else {
+            char *passphrase = dupstr(p->prompts[0]->result);
+            free_prompts(p);
+            return passphrase;
+        }
+    } else if (display) {
+        char *prompt, *passphrase;
+        int success;
+
+        /* in gtkask.c */
+        char *gtk_askpass_main(const char *display, const char *wintitle,
+                               const char *prompt, int *success);
+
+        prompt = dupprintf("Enter passphrase to load key '%s': ", comment);
+        passphrase = gtk_askpass_main(display,
+                                      "Pageant passphrase prompt",
+                                      prompt, &success);
+        sfree(prompt);
+        if (!success) {
+            /* return value is error message */
+            fprintf(stderr, "%s\n", passphrase);
+            sfree(passphrase);
+            passphrase = NULL;
+        }
         return passphrase;
+    } else {
+        fprintf(stderr, "no way to read a passphrase without tty or "
+                "X display\n");
+        return NULL;
     }
 }
 
@@ -319,7 +355,6 @@ static int unix_add_keyfile(const char *filename_str)
         goto cleanup;
     } else if (status == PAGEANT_ACTION_FAILURE) {
         fprintf(stderr, "pageant: %s: %s\n", filename_str, err);
-        sfree(err);
         ret = FALSE;
         goto cleanup;
     }
@@ -330,6 +365,7 @@ static int unix_add_keyfile(const char *filename_str)
     while (1) {
         char *passphrase = askpass(err);
         sfree(err);
+        err = NULL;
         if (!passphrase)
             break;
 
@@ -343,7 +379,6 @@ static int unix_add_keyfile(const char *filename_str)
             goto cleanup;
         } else if (status == PAGEANT_ACTION_FAILURE) {
             fprintf(stderr, "pageant: %s: %s\n", filename_str, err);
-            sfree(err);
             ret = FALSE;
             goto cleanup;
         }
@@ -564,10 +599,39 @@ void run_client(void)
             if (key)
                 pageant_pubkey_free(key);
             break;
+          case KEYACT_CLIENT_PUBLIC_OPENSSH:
+          case KEYACT_CLIENT_PUBLIC:
+            key = NULL;
+            if (!(key = find_key(act->filename, &retstr))) {
+                fprintf(stderr, "pageant: finding key '%s': %s\n",
+                        act->filename, retstr);
+                sfree(retstr);
+                errors = TRUE;
+            } else {
+                FILE *fp = stdout;     /* FIXME: add a -o option? */
+
+                if (key->ssh_version == 1) {
+                    struct RSAKey rkey;
+                    memset(&rkey, 0, sizeof(rkey));
+                    rkey.comment = dupstr(key->comment);
+                    makekey(key->blob, key->bloblen, &rkey, NULL, 0);
+                    ssh1_write_pubkey(fp, &rkey);
+                    freersakey(&rkey);
+                } else {
+                    ssh2_write_pubkey(fp, key->comment, key->blob,key->bloblen,
+                                      (act->action == KEYACT_CLIENT_PUBLIC ?
+                                       SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 :
+                                       SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH));
+                }
+                pageant_pubkey_free(key);
+            }
+            break;
           case KEYACT_CLIENT_DEL_ALL:
-          case KEYACT_CLIENT_LIST_FULL:
-            fprintf(stderr, "NYI\n");
-            errors = TRUE;
+            if (pageant_delete_all_keys(&retstr) == PAGEANT_ACTION_FAILURE) {
+                fprintf(stderr, "pageant: deleting all keys: %s\n", retstr);
+                sfree(retstr);
+                errors = TRUE;
+            }
             break;
           default:
             assert(0 && "Invalid client action found");
@@ -651,8 +715,6 @@ void run_agent(void)
             NULL
         };
 
-        if (!display)
-            display = getenv("DISPLAY");
         if (!display) {
             fprintf(stderr, "pageant: no DISPLAY for -X mode\n");
             exit(1);
@@ -809,16 +871,9 @@ void run_agent(void)
              * our containing tty session has ended, so it's time to
              * clean up and leave.
              */
-            int fd = open("/dev/tty", O_RDONLY);
-            if (fd < 0) {
-                if (errno != ENXIO) {
-                    perror("/dev/tty: open");
-                    exit(1);
-                }
+            if (!have_controlling_tty()) {
                 time_to_die = TRUE;
                 break;
-            } else {
-                close(fd);
             }
         }
 
@@ -892,8 +947,10 @@ int main(int argc, char **argv)
                 add_keyact(KEYACT_CLIENT_DEL_ALL, NULL);
             } else if (!strcmp(p, "-l")) {
                 add_keyact(KEYACT_CLIENT_LIST, NULL);
-            } else if (!strcmp(p, "-L")) {
-                add_keyact(KEYACT_CLIENT_LIST_FULL, NULL);
+            } else if (!strcmp(p, "--public")) {
+                curr_keyact = KEYACT_CLIENT_PUBLIC;
+            } else if (!strcmp(p, "--public-openssh")) {
+                curr_keyact = KEYACT_CLIENT_PUBLIC_OPENSSH;
             } else if (!strcmp(p, "-X")) {
                 life = LIFE_X11;
             } else if (!strcmp(p, "-T")) {
@@ -941,6 +998,12 @@ int main(int argc, char **argv)
     sk_init();
     uxsel_init();
 
+    if (!display) {
+        display = getenv("DISPLAY");
+        if (display && !*display)
+            display = NULL;
+    }
+
     /*
      * Now distinguish our two main running modes. Either we're
      * actually starting up an agent, in which case we should have a