Having now compiled the last few days' changes with MSVC, it's turned
[u/mdw/putty] / pageant.c
index aad4226..6f94262 100644 (file)
--- a/pageant.c
+++ b/pageant.c
 #include <ctype.h>
 #include <tchar.h>
 
-// FIXME
-#define DEBUG
-#ifdef DEBUG
-void dprintf(char *fmt, ...);
-#define debug(x) (dprintf x)
-#else
-#define debug(x)
-#endif
-
-
 #include "ssh.h"
 #include "tree234.h"
 
@@ -66,6 +56,12 @@ static gsi_fn_t getsecurityinfo;
 #endif
 
 /*
+ * Exports from pageantc.c
+ */
+void agent_query(void *in, int inlen, void **out, int *outlen);
+int agent_exists(void);
+
+/*
  * We need this to link with the RSA code, because rsaencrypt()
  * pads its data with random bytes. Since we only use rsadecrypt()
  * and the signing functions, which are deterministic, this should
@@ -234,11 +230,11 @@ static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
 static void keylist_update(void) {
     struct RSAKey *rkey;
     struct ssh2_userkey *skey;
-    enum234 e;
+    int i;
 
     if (keylist) {
         SendDlgItemMessage(keylist, 100, LB_RESETCONTENT, 0, 0);
-        for (rkey = first234(rsakeys, &e); rkey; rkey = next234(&e)) {
+        for (i = 0; NULL != (rkey = index234(rsakeys, i)); i++) {
             char listentry[512], *p;
             /*
              * Replace two spaces in the fingerprint with tabs, for
@@ -252,7 +248,7 @@ static void keylist_update(void) {
             SendDlgItemMessage (keylist, 100, LB_ADDSTRING,
                                 0, (LPARAM)listentry);
         }
-        for (skey = first234(ssh2keys, &e); skey; skey = next234(&e)) {
+        for (i = 0; NULL != (skey = index234(ssh2keys, i)); i++) {
             char listentry[512], *p;
            int len;
             /*
@@ -296,22 +292,16 @@ static void add_keyfile(char *filename) {
         return;
     }
 
-debug(("ooh %d\n", __LINE__));
     if (ver == 1)
        needs_pass = rsakey_encrypted(filename, &comment);
     else
        needs_pass = ssh2_userkey_encrypted(filename, &comment);
-debug(("ooh %d\n", __LINE__));
     attempts = 0;
-debug(("ooh %d\n", __LINE__));
     if (ver == 1)
        rkey = smalloc(sizeof(*rkey));
-debug(("ooh %d\n", __LINE__));
     pps.passphrase = passphrase;
     pps.comment = comment;
-debug(("ooh %d\n", __LINE__));
     do {
-debug(("ooh %d\n", __LINE__));
         if (needs_pass) {
             int dlgret;
             dlgret = DialogBoxParam(instance, MAKEINTRESOURCE(210),
@@ -325,13 +315,10 @@ debug(("ooh %d\n", __LINE__));
             }
         } else
             *passphrase = '\0';
-debug(("ooh %d\n", __LINE__));
        if (ver == 1)
            ret = loadrsakey(filename, rkey, passphrase);
        else {
-debug(("ooh %d\n", __LINE__));
            skey = ssh2_load_userkey(filename, passphrase);
-debug(("ooh %d\n", __LINE__));
            if (skey == SSH2_WRONG_PASSPHRASE)
                ret = -1;
            else if (!skey)
@@ -341,9 +328,7 @@ debug(("ooh %d\n", __LINE__));
        }
         attempts++;
     } while (ret == -1);
-debug(("ooh %d\n", __LINE__));
     if (comment) sfree(comment);
-debug(("ooh %d\n", __LINE__));
     if (ret == 0) {
         MessageBox(NULL, "Couldn't load private key.", APPNAME,
                    MB_OK | MB_ICONERROR);
@@ -351,15 +336,12 @@ debug(("ooh %d\n", __LINE__));
            sfree(rkey);
         return;
     }
-debug(("ooh %d\n", __LINE__));
     if (ver == 1) {
        if (already_running) {
            unsigned char *request, *response;
            int reqlen, clen, resplen;
 
-debug(("ooh %d\n", __LINE__));
            clen = strlen(rkey->comment);
-debug(("ooh %d\n", __LINE__));
 
            reqlen = 4 + 1 +           /* length, message type */
                4 +                    /* bit count */
@@ -371,14 +353,10 @@ debug(("ooh %d\n", __LINE__));
                ssh1_bignum_length(rkey->q) +
                4 + clen               /* comment */
                ;
-debug(("ooh %d %d\n", __LINE__, reqlen));
 
            request = smalloc(reqlen);
-debug(("ooh %d\n", __LINE__));
 
-debug(("ooh %d\n", __LINE__));
            request[4] = SSH1_AGENTC_ADD_RSA_IDENTITY;
-debug(("ooh %d\n", __LINE__));
            reqlen = 5;
            PUT_32BIT(request+reqlen, bignum_bitcount(rkey->modulus));
            reqlen += 4;
@@ -393,9 +371,7 @@ debug(("ooh %d\n", __LINE__));
            reqlen += 4+clen;
            PUT_32BIT(request, reqlen-4);
 
-debug(("ooh %d\n", __LINE__));
            agent_query(request, reqlen, &response, &resplen);
-debug(("ooh %d\n", __LINE__));
            if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS)
                MessageBox(NULL, "The already running Pageant "
                           "refused to add the key.", APPNAME,
@@ -408,58 +384,37 @@ debug(("ooh %d\n", __LINE__));
        if (already_running) {
            unsigned char *request, *response;
            int reqlen, alglen, clen, keybloblen, resplen;
-debug(("ooh %d\n", __LINE__));
            alglen = strlen(skey->alg->name);
-debug(("ooh %d\n", __LINE__));
            clen = strlen(skey->comment);
-debug(("ooh %d\n", __LINE__));
 
-debug(("ooh %d\n", __LINE__));
            keybloblen = skey->alg->openssh_fmtkey(skey->data, NULL, 0);
-debug(("ooh %d\n", __LINE__));
 
-debug(("ooh %d\n", __LINE__));
            reqlen = 4 + 1 +           /* length, message type */
                4 + alglen +           /* algorithm name */
                keybloblen +           /* key data */
                4 + clen               /* comment */
                ;
-debug(("ooh %d\n", __LINE__));
 
-debug(("ooh %d\n", __LINE__));
            request = smalloc(reqlen);
-debug(("ooh %d\n", __LINE__));
 
            request[4] = SSH2_AGENTC_ADD_IDENTITY;
-debug(("ooh %d\n", __LINE__));
            reqlen = 5;
-debug(("ooh %d\n", __LINE__));
            PUT_32BIT(request+reqlen, alglen);
-debug(("ooh %d\n", __LINE__));
            reqlen += 4;
-debug(("ooh %d\n", __LINE__));
            memcpy(request+reqlen, skey->alg->name, alglen);
-debug(("ooh %d\n", __LINE__));
            reqlen += alglen;
-debug(("ooh %d\n", __LINE__));
            reqlen += skey->alg->openssh_fmtkey(skey->data,
                                                request+reqlen, keybloblen);
-debug(("ooh %d\n", __LINE__));
            PUT_32BIT(request+reqlen, clen);
-debug(("ooh %d\n", __LINE__));
            memcpy(request+reqlen+4, skey->comment, clen);
-debug(("ooh %d\n", __LINE__));
            PUT_32BIT(request, reqlen-4);
-debug(("ooh %d\n", __LINE__));
            reqlen += clen+4;
 
            agent_query(request, reqlen, &response, &resplen);
-debug(("ooh %d\n", __LINE__));
            if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS)
                MessageBox(NULL, "The already running Pageant"
                           "refused to add the key.", APPNAME,
                           MB_OK | MB_ICONERROR);
-debug(("ooh %d\n", __LINE__));
        } else {
            if (add234(ssh2keys, skey) != skey) {
                skey->alg->freekey(skey->data);
@@ -489,15 +444,15 @@ static void answer_msg(void *msg) {
          * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
          */
         {
-            enum234 e;
             struct RSAKey *key;
             int len, nkeys;
+           int i;
 
             /*
              * Count up the number and length of keys we hold.
              */
             len = nkeys = 0;
-            for (key = first234(rsakeys, &e); key; key = next234(&e)) {
+            for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
                 nkeys++;
                 len += 4;              /* length field */
                 len += ssh1_bignum_length(key->exponent);
@@ -516,7 +471,7 @@ static void answer_msg(void *msg) {
             ret[4] = SSH1_AGENT_RSA_IDENTITIES_ANSWER;
             PUT_32BIT(ret+5, nkeys);
             p = ret + 5 + 4;
-            for (key = first234(rsakeys, &e); key; key = next234(&e)) {
+            for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
                 PUT_32BIT(p, bignum_bitcount(key->modulus));
                 p += 4;
                 p += ssh1_write_bignum(p, key->exponent);
@@ -532,17 +487,17 @@ static void answer_msg(void *msg) {
          * Reply with SSH2_AGENT_IDENTITIES_ANSWER.
          */
         {
-            enum234 e;
             struct ssh2_userkey *key;
             int len, nkeys;
            unsigned char *blob;
            int bloblen;
+           int i;
 
             /*
              * Count up the number and length of keys we hold.
              */
             len = nkeys = 0;
-            for (key = first234(ssh2keys, &e); key; key = next234(&e)) {
+            for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
                 nkeys++;
                 len += 4;              /* length field */
                blob = key->alg->public_blob(key->data, &bloblen);
@@ -562,7 +517,7 @@ static void answer_msg(void *msg) {
             ret[4] = SSH2_AGENT_IDENTITIES_ANSWER;
             PUT_32BIT(ret+5, nkeys);
             p = ret + 5 + 4;
-            for (key = first234(ssh2keys, &e); key; key = next234(&e)) {
+            for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
                blob = key->alg->public_blob(key->data, &bloblen);
                 PUT_32BIT(p, bloblen);
                 p += 4;
@@ -794,9 +749,8 @@ static void answer_msg(void *msg) {
          */
         {
            struct RSAKey *rkey;
-           enum234 e;
 
-            while ( (rkey = first234(rsakeys, &e)) != NULL ) {
+            while ( (rkey = index234(rsakeys, 0)) != NULL ) {
                 del234(rsakeys, rkey);
                freersakey(rkey);
                sfree(rkey);
@@ -813,9 +767,8 @@ static void answer_msg(void *msg) {
          */
         {
             struct ssh2_userkey *skey;
-           enum234 e;
 
-            while ( (skey = first234(ssh2keys, &e)) != NULL ) {
+            while ( (skey = index234(ssh2keys, 0)) != NULL ) {
                 del234(ssh2keys, skey);
                skey->alg->freekey(skey->data);
                sfree(skey);
@@ -976,7 +929,6 @@ static void prompt_add_keyfile(void) {
  */
 static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
                                 WPARAM wParam, LPARAM lParam) {
-    enum234 e;
     struct RSAKey *rkey;
     struct ssh2_userkey *skey;
 
@@ -1021,11 +973,12 @@ static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
            if (HIWORD(wParam) == BN_CLICKED ||
                HIWORD(wParam) == BN_DOUBLECLICKED) {
                int n = SendDlgItemMessage (hwnd, 100, LB_GETCURSEL, 0, 0);
+               int i;
                if (n == LB_ERR) {
                    MessageBeep(0);
                    break;
                }
-                for (rkey = first234(rsakeys, &e); rkey; rkey = next234(&e))
+                for (i = 0; NULL != (rkey = index234(rsakeys, i)); i++)
                     if (n-- == 0)
                         break;
                if (rkey) {
@@ -1033,7 +986,7 @@ static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
                    freersakey(rkey);
                    sfree(rkey);
                } else {
-                   for (skey = first234(ssh2keys, &e); skey; skey = next234(&e))
+                   for (i = 0; NULL != (skey = index234(ssh2keys, i)); i++)
                        if (n-- == 0)
                            break;
                    if (skey) {
@@ -1273,7 +1226,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
      * Find out if Pageant is already running.
      */
     already_running = FALSE;
-    if (FindWindow("Pageant", "Pageant"))
+    if (agent_exists())
        already_running = TRUE;
     else {