Yet more global-removal. The static variables in logging.c are now
[u/mdw/putty] / puttygen.c
index fc3c71a..956cdaf 100644 (file)
 
 static int requested_help;
 
+static char *cmdline_keyfile = NULL;
+
+/*
+ * Print a modal (Really Bad) message box and perform a fatal exit.
+ */
+void modalfatalbox(char *fmt, ...)
+{
+    va_list ap;
+    char stuff[200];
+
+    va_start(ap, fmt);
+    vsprintf(stuff, fmt, ap);
+    va_end(ap);
+    MessageBox(NULL, stuff, "PuTTYgen Fatal Error",
+              MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
+    exit(1);
+}
+
 /* ----------------------------------------------------------------------
  * Progress report code. This is really horrible :-)
  */
@@ -204,14 +222,6 @@ static int prompt_keyfile(HWND hwnd, char *dlgtitle,
 }
 
 /*
- * This function is needed to link with the DES code. We need not
- * have it do anything at all.
- */
-void logevent(char *msg)
-{
-}
-
-/*
  * Dialog-box function for the Licence box.
  */
 static int CALLBACK LicenceProc(HWND hwnd, UINT msg,
@@ -610,6 +620,175 @@ void ui_set_state(HWND hwnd, struct MainDlgState *state, int status)
     }
 }
 
+void load_key_file(HWND hwnd, struct MainDlgState *state,
+                  char *filename, int was_import_cmd)
+{
+    char passphrase[PASSPHRASE_MAXLEN];
+    int needs_pass;
+    int type, realtype;
+    int ret;
+    char *comment;
+    struct PassphraseProcStruct pps;
+    struct RSAKey newkey1;
+    struct ssh2_userkey *newkey2 = NULL;
+
+    type = realtype = key_type(filename);
+    if (type != SSH_KEYTYPE_SSH1 &&
+       type != SSH_KEYTYPE_SSH2 &&
+       !import_possible(type)) {
+       char msg[256];
+       sprintf(msg, "Couldn't load private key (%s)",
+               key_type_to_str(type));
+       MessageBox(NULL, msg,
+                  "PuTTYgen Error", MB_OK | MB_ICONERROR);
+       return;
+    }
+
+    if (type != SSH_KEYTYPE_SSH1 &&
+       type != SSH_KEYTYPE_SSH2) {
+       realtype = type;
+       type = import_target_type(type);
+    }
+
+    comment = NULL;
+    if (realtype == SSH_KEYTYPE_SSH1)
+       needs_pass = rsakey_encrypted(filename, &comment);
+    else if (realtype == SSH_KEYTYPE_SSH2)
+       needs_pass =
+       ssh2_userkey_encrypted(filename, &comment);
+    else
+       needs_pass = import_encrypted(filename, realtype,
+                                     &comment);
+    pps.passphrase = passphrase;
+    pps.comment = comment;
+    do {
+       if (needs_pass) {
+           int dlgret;
+           dlgret = DialogBoxParam(hinst,
+                                   MAKEINTRESOURCE(210),
+                                   NULL, PassphraseProc,
+                                   (LPARAM) & pps);
+           if (!dlgret) {
+               ret = -2;
+               break;
+           }
+       } else
+           *passphrase = '\0';
+       if (type == SSH_KEYTYPE_SSH1) {
+           if (realtype == type)
+               ret = loadrsakey(filename, &newkey1,
+                                passphrase);
+           else
+               ret = import_ssh1(filename, realtype,
+                                 &newkey1, passphrase);
+       } else {
+           if (realtype == type)
+               newkey2 = ssh2_load_userkey(filename,
+                                           passphrase);
+           else
+               newkey2 = import_ssh2(filename, realtype,
+                                     passphrase);
+           if (newkey2 == SSH2_WRONG_PASSPHRASE)
+               ret = -1;
+           else if (!newkey2)
+               ret = 0;
+           else
+               ret = 1;
+       }
+    } while (ret == -1);
+    if (comment)
+       sfree(comment);
+    if (ret == 0) {
+       MessageBox(NULL, "Couldn't load private key.",
+                  "PuTTYgen Error", MB_OK | MB_ICONERROR);
+    } else if (ret == 1) {
+       /*
+        * Now update the key controls with all the
+        * key data.
+        */
+       {
+           SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
+                          passphrase);
+           SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
+                          passphrase);
+           if (type == SSH_KEYTYPE_SSH1) {
+               char buf[128];
+               char *savecomment;
+
+               state->ssh2 = FALSE;
+               state->commentptr = &state->key.comment;
+               state->key = newkey1;
+
+               /*
+                * Set the key fingerprint.
+                */
+               savecomment = state->key.comment;
+               state->key.comment = NULL;
+               rsa_fingerprint(buf, sizeof(buf),
+                               &state->key);
+               state->key.comment = savecomment;
+
+               SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
+               /*
+                * Construct a decimal representation
+                * of the key, for pasting into
+                * .ssh/authorized_keys on a Unix box.
+                */
+               setupbigedit1(hwnd, IDC_KEYDISPLAY,
+                             IDC_PKSTATIC, &state->key);
+           } else {
+               char *fp;
+               char *savecomment;
+
+               state->ssh2 = TRUE;
+               state->commentptr =
+                   &state->ssh2key.comment;
+               state->ssh2key = *newkey2;      /* structure copy */
+               sfree(newkey2);
+
+               savecomment = state->ssh2key.comment;
+               state->ssh2key.comment = NULL;
+               fp =
+                   state->ssh2key.alg->
+                   fingerprint(state->ssh2key.data);
+               state->ssh2key.comment = savecomment;
+
+               SetDlgItemText(hwnd, IDC_FINGERPRINT, fp);
+               sfree(fp);
+
+               setupbigedit2(hwnd, IDC_KEYDISPLAY,
+                             IDC_PKSTATIC, &state->ssh2key);
+           }
+           SetDlgItemText(hwnd, IDC_COMMENTEDIT,
+                          *state->commentptr);
+       }
+       /*
+        * Finally, hide the progress bar and show
+        * the key data.
+        */
+       ui_set_state(hwnd, state, 2);
+       state->key_exists = TRUE;
+
+       /*
+        * If the user has imported a foreign key
+        * using the Load command, let them know.
+        * If they've used the Import command, be
+        * silent.
+        */
+       if (realtype != type && !was_import_cmd) {
+           char msg[512];
+           sprintf(msg, "Successfully imported foreign key\n"
+                   "(%s).\n"
+                   "To use this key with PuTTY, you need to\n"
+                   "use the \"Save private key\" command to\n"
+                   "save it in PuTTY's own format.",
+                   key_type_to_str(realtype));
+           MessageBox(NULL, msg, "PuTTYgen Notice",
+                      MB_OK | MB_ICONINFORMATION);
+       }
+    }
+}
+
 /*
  * Dialog-box function for the main PuTTYgen dialog box.
  */
@@ -757,6 +936,12 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
         */
        ui_set_state(hwnd, state, 0);
 
+       /*
+        * Load a key file if one was provided on the command line.
+        */
+       if (cmdline_keyfile)
+           load_key_file(hwnd, state, cmdline_keyfile, 0);
+
        return 1;
       case WM_MOUSEMOVE:
        state = (struct MainDlgState *) GetWindowLong(hwnd, GWL_USERDATA);
@@ -1040,172 +1225,9 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg,
            if (!state->generation_thread_exists) {
                char filename[FILENAME_MAX];
                if (prompt_keyfile(hwnd, "Load private key:",
-                                  filename, 0, LOWORD(wParam)==IDC_LOAD)) {
-                   char passphrase[PASSPHRASE_MAXLEN];
-                   int needs_pass;
-                   int type, realtype;
-                   int ret;
-                   char *comment;
-                   struct PassphraseProcStruct pps;
-                   struct RSAKey newkey1;
-                   struct ssh2_userkey *newkey2 = NULL;
-
-                   type = realtype = key_type(filename);
-                   if (type != SSH_KEYTYPE_SSH1 &&
-                       type != SSH_KEYTYPE_SSH2 &&
-                       !import_possible(type)) {
-                       char msg[256];
-                       sprintf(msg, "Couldn't load private key (%s)",
-                               key_type_to_str(type));
-                       MessageBox(NULL, msg,
-                                  "PuTTYgen Error", MB_OK | MB_ICONERROR);
-                       break;
-                   }
-
-                   if (type != SSH_KEYTYPE_SSH1 &&
-                       type != SSH_KEYTYPE_SSH2) {
-                       realtype = type;
-                       type = import_target_type(type);
-                   }
-
-                   comment = NULL;
-                   if (realtype == SSH_KEYTYPE_SSH1)
-                       needs_pass = rsakey_encrypted(filename, &comment);
-                   else if (realtype == SSH_KEYTYPE_SSH2)
-                       needs_pass =
-                           ssh2_userkey_encrypted(filename, &comment);
-                   else
-                       needs_pass = import_encrypted(filename, realtype,
-                                                     &comment);
-                   pps.passphrase = passphrase;
-                   pps.comment = comment;
-                   do {
-                       if (needs_pass) {
-                           int dlgret;
-                           dlgret = DialogBoxParam(hinst,
-                                                   MAKEINTRESOURCE(210),
-                                                   NULL, PassphraseProc,
-                                                   (LPARAM) & pps);
-                           if (!dlgret) {
-                               ret = -2;
-                               break;
-                           }
-                       } else
-                           *passphrase = '\0';
-                       if (type == SSH_KEYTYPE_SSH1) {
-                           if (realtype == type)
-                               ret = loadrsakey(filename, &newkey1,
-                                                passphrase);
-                           else
-                               ret = import_ssh1(filename, realtype,
-                                                 &newkey1, passphrase);
-                       } else {
-                           if (realtype == type)
-                               newkey2 = ssh2_load_userkey(filename,
-                                                           passphrase);
-                           else
-                               newkey2 = import_ssh2(filename, realtype,
-                                                     passphrase);
-                           if (newkey2 == SSH2_WRONG_PASSPHRASE)
-                               ret = -1;
-                           else if (!newkey2)
-                               ret = 0;
-                           else
-                               ret = 1;
-                       }
-                   } while (ret == -1);
-                   if (comment)
-                       sfree(comment);
-                   if (ret == 0) {
-                       MessageBox(NULL, "Couldn't load private key.",
-                                  "PuTTYgen Error", MB_OK | MB_ICONERROR);
-                   } else if (ret == 1) {
-                       /*
-                        * Now update the key controls with all the
-                        * key data.
-                        */
-                       {
-                           SetDlgItemText(hwnd, IDC_PASSPHRASE1EDIT,
-                                          passphrase);
-                           SetDlgItemText(hwnd, IDC_PASSPHRASE2EDIT,
-                                          passphrase);
-                           if (type == SSH_KEYTYPE_SSH1) {
-                               char buf[128];
-                               char *savecomment;
-
-                               state->ssh2 = FALSE;
-                               state->commentptr = &state->key.comment;
-                               state->key = newkey1;
-
-                               /*
-                                * Set the key fingerprint.
-                                */
-                               savecomment = state->key.comment;
-                               state->key.comment = NULL;
-                               rsa_fingerprint(buf, sizeof(buf),
-                                               &state->key);
-                               state->key.comment = savecomment;
-
-                               SetDlgItemText(hwnd, IDC_FINGERPRINT, buf);
-                               /*
-                                * Construct a decimal representation
-                                * of the key, for pasting into
-                                * .ssh/authorized_keys on a Unix box.
-                                */
-                               setupbigedit1(hwnd, IDC_KEYDISPLAY,
-                                             IDC_PKSTATIC, &state->key);
-                           } else {
-                               char *fp;
-                               char *savecomment;
-
-                               state->ssh2 = TRUE;
-                               state->commentptr =
-                                   &state->ssh2key.comment;
-                               state->ssh2key = *newkey2;      /* structure copy */
-                               sfree(newkey2);
-
-                               savecomment = state->ssh2key.comment;
-                               state->ssh2key.comment = NULL;
-                               fp =
-                                   state->ssh2key.alg->
-                                   fingerprint(state->ssh2key.data);
-                               state->ssh2key.comment = savecomment;
-
-                               SetDlgItemText(hwnd, IDC_FINGERPRINT, fp);
-                               sfree(fp);
-
-                               setupbigedit2(hwnd, IDC_KEYDISPLAY,
-                                             IDC_PKSTATIC, &state->ssh2key);
-                           }
-                           SetDlgItemText(hwnd, IDC_COMMENTEDIT,
-                                          *state->commentptr);
-                       }
-                       /*
-                        * Finally, hide the progress bar and show
-                        * the key data.
-                        */
-                       ui_set_state(hwnd, state, 2);
-                       state->key_exists = TRUE;
-
-                       /*
-                        * If the user has imported a foreign key
-                        * using the Load command, let them know.
-                        * If they've used the Import command, be
-                        * silent.
-                        */
-                       if (realtype != type && LOWORD(wParam) == IDC_LOAD) {
-                           char msg[512];
-                           sprintf(msg, "Successfully imported foreign key\n"
-                                   "(%s).\n"
-                                   "To use this key with PuTTY, you need to\n"
-                                   "use the \"Save private key\" command to\n"
-                                   "save it in PuTTY's own format.",
-                                   key_type_to_str(realtype));
-                           MessageBox(NULL, msg, "PuTTYgen Notice",
-                                      MB_OK | MB_ICONINFORMATION);
-                       }
-                   }
-               }
+                                  filename, 0, LOWORD(wParam)==IDC_LOAD))
+                   load_key_file(hwnd, state, filename,
+                                 LOWORD(wParam) != IDC_LOAD);
            }
            break;
        }
@@ -1367,6 +1389,19 @@ void cleanup_exit(int code) { exit(code); }
 
 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
 {
+    int argc;
+    char **argv;
+
+    split_into_argv(cmdline, &argc, &argv, NULL);
+
+    if (argc > 0) {
+       /*
+        * Assume the first argument to be a private key file, and
+        * attempt to load it.
+        */
+       cmdline_keyfile = argv[0];
+    }
+
     InitCommonControls();
     hinst = inst;