Only update the system caret when we have the focus. This was
[u/mdw/putty] / pageant.c
CommitLineData
5c58ad2d 1/*
2 * Pageant: the PuTTY Authentication Agent.
3 */
4
5#include <windows.h>
123bc6ea 6#ifndef NO_SECURITY
d70f60ae 7#include <aclapi.h>
123bc6ea 8#endif
9b581c37 9#include <stdio.h>
49bad831 10#include <stdlib.h>
11#include <ctype.h>
ddecd643 12#include <tchar.h>
13
5c58ad2d 14#include "ssh.h"
15#include "tree234.h"
16
17#define IDI_MAINICON 200
18#define IDI_TRAYICON 201
19
20#define WM_XUSER (WM_USER + 0x2000)
21#define WM_SYSTRAY (WM_XUSER + 6)
22#define WM_SYSTRAY2 (WM_XUSER + 7)
d70f60ae 23
24#define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
25
26/*
27 * FIXME: maybe some day we can sort this out ...
28 */
29#define AGENT_MAX_MSGLEN 8192
5c58ad2d 30
31#define IDM_CLOSE 0x0010
32#define IDM_VIEWKEYS 0x0020
ab162329 33#define IDM_ADDKEY 0x0030
34#define IDM_ABOUT 0x0040
5c58ad2d 35
36#define APPNAME "Pageant"
37
5af3a3b6 38extern char ver[];
39
75cab814 40static HINSTANCE instance;
41static HWND hwnd;
42static HWND keylist;
43static HWND aboutbox;
44static HMENU systray_menu;
ddecd643 45static int already_running;
5c58ad2d 46
45cebe79 47static tree234 *rsakeys, *ssh2keys;
5c58ad2d 48
75cab814 49static int has_security;
123bc6ea 50#ifndef NO_SECURITY
016ef8ab 51typedef DWORD (WINAPI *gsi_fn_t)
52 (HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION,
53 PSID *, PSID *, PACL *, PACL *,
54 PSECURITY_DESCRIPTOR *);
75cab814 55static gsi_fn_t getsecurityinfo;
123bc6ea 56#endif
016ef8ab 57
5c58ad2d 58/*
260f3dec 59 * Exports from pageantc.c
60 */
61void agent_query(void *in, int inlen, void **out, int *outlen);
62int agent_exists(void);
63
64/*
5c58ad2d 65 * We need this to link with the RSA code, because rsaencrypt()
45cebe79 66 * pads its data with random bytes. Since we only use rsadecrypt()
67 * and the signing functions, which are deterministic, this should
68 * never be called.
69 *
5c58ad2d 70 * If it _is_ called, there is a _serious_ problem, because it
71 * won't generate true random numbers. So we must scream, panic,
72 * and exit immediately if that should happen.
73 */
74int random_byte(void) {
75 MessageBox(hwnd, "Internal Error", APPNAME, MB_OK | MB_ICONERROR);
76 exit(0);
77}
78
79/*
45cebe79 80 * Blob structure for passing to the asymmetric SSH2 key compare
81 * function, prototyped here.
82 */
83struct blob {
84 unsigned char *blob;
85 int len;
86};
87static int cmpkeys_ssh2_asymm(void *av, void *bv);
88
89/*
5c58ad2d 90 * This function is needed to link with the DES code. We need not
91 * have it do anything at all.
92 */
93void logevent(char *msg) {
94}
95
96#define GET_32BIT(cp) \
97 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
98 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
99 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
100 ((unsigned long)(unsigned char)(cp)[3]))
101
102#define PUT_32BIT(cp, value) { \
103 (cp)[0] = (unsigned char)((value) >> 24); \
104 (cp)[1] = (unsigned char)((value) >> 16); \
105 (cp)[2] = (unsigned char)((value) >> 8); \
106 (cp)[3] = (unsigned char)(value); }
107
108#define PASSPHRASE_MAXLEN 512
109
d4de2d2a 110struct PassphraseProcStruct {
111 char *passphrase;
112 char *comment;
113};
114
5c58ad2d 115/*
5af3a3b6 116 * Dialog-box function for the Licence box.
117 */
118static int CALLBACK LicenceProc (HWND hwnd, UINT msg,
119 WPARAM wParam, LPARAM lParam) {
120 switch (msg) {
121 case WM_INITDIALOG:
122 return 1;
123 case WM_COMMAND:
124 switch (LOWORD(wParam)) {
125 case IDOK:
126 EndDialog(hwnd, 1);
127 return 0;
128 }
129 return 0;
130 case WM_CLOSE:
131 EndDialog(hwnd, 1);
132 return 0;
133 }
134 return 0;
135}
136
137/*
138 * Dialog-box function for the About box.
139 */
140static int CALLBACK AboutProc (HWND hwnd, UINT msg,
141 WPARAM wParam, LPARAM lParam) {
142 switch (msg) {
143 case WM_INITDIALOG:
144 SetDlgItemText (hwnd, 100, ver);
145 return 1;
146 case WM_COMMAND:
147 switch (LOWORD(wParam)) {
148 case IDOK:
149 aboutbox = NULL;
150 DestroyWindow (hwnd);
151 return 0;
152 case 101:
153 EnableWindow(hwnd, 0);
154 DialogBox (instance, MAKEINTRESOURCE(214), NULL, LicenceProc);
155 EnableWindow(hwnd, 1);
156 SetActiveWindow(hwnd);
157 return 0;
158 }
159 return 0;
160 case WM_CLOSE:
161 aboutbox = NULL;
162 DestroyWindow (hwnd);
163 return 0;
164 }
165 return 0;
166}
167
168/*
5c58ad2d 169 * Dialog-box function for the passphrase box.
170 */
171static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
172 WPARAM wParam, LPARAM lParam) {
173 static char *passphrase;
d4de2d2a 174 struct PassphraseProcStruct *p;
5c58ad2d 175
176 switch (msg) {
177 case WM_INITDIALOG:
45cebe79 178 /*
179 * Centre the window.
180 */
181 { /* centre the window */
182 RECT rs, rd;
183 HWND hw;
184
185 hw = GetDesktopWindow();
186 if (GetWindowRect (hw, &rs) && GetWindowRect (hwnd, &rd))
187 MoveWindow (hwnd, (rs.right + rs.left + rd.left - rd.right)/2,
188 (rs.bottom + rs.top + rd.top - rd.bottom)/2,
189 rd.right-rd.left, rd.bottom-rd.top, TRUE);
190 }
191
ab162329 192 SetForegroundWindow(hwnd);
193 SetWindowPos (hwnd, HWND_TOP, 0, 0, 0, 0,
194 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
d4de2d2a 195 p = (struct PassphraseProcStruct *)lParam;
196 passphrase = p->passphrase;
197 if (p->comment)
198 SetDlgItemText(hwnd, 101, p->comment);
5c58ad2d 199 *passphrase = 0;
200 return 0;
201 case WM_COMMAND:
202 switch (LOWORD(wParam)) {
203 case IDOK:
204 if (*passphrase)
205 EndDialog (hwnd, 1);
206 else
207 MessageBeep (0);
208 return 0;
209 case IDCANCEL:
210 EndDialog (hwnd, 0);
211 return 0;
212 case 102: /* edit box */
213 if (HIWORD(wParam) == EN_CHANGE) {
214 GetDlgItemText (hwnd, 102, passphrase, PASSPHRASE_MAXLEN-1);
215 passphrase[PASSPHRASE_MAXLEN-1] = '\0';
216 }
217 return 0;
218 }
219 return 0;
220 case WM_CLOSE:
221 EndDialog (hwnd, 0);
222 return 0;
223 }
224 return 0;
225}
226
227/*
3c0b3d06 228 * Update the visible key list.
229 */
75cab814 230static void keylist_update(void) {
45cebe79 231 struct RSAKey *rkey;
232 struct ssh2_userkey *skey;
d2371c81 233 int i;
3c0b3d06 234
235 if (keylist) {
236 SendDlgItemMessage(keylist, 100, LB_RESETCONTENT, 0, 0);
d2371c81 237 for (i = 0; NULL != (rkey = index234(rsakeys, i)); i++) {
45cebe79 238 char listentry[512], *p;
239 /*
240 * Replace two spaces in the fingerprint with tabs, for
241 * nice alignment in the box.
242 */
243 strcpy(listentry, "ssh1\t");
244 p = listentry+strlen(listentry);
245 rsa_fingerprint(p, sizeof(listentry)-(p-listentry), rkey);
246 p = strchr(listentry, ' '); if (p) *p = '\t';
247 p = strchr(listentry, ' '); if (p) *p = '\t';
248 SendDlgItemMessage (keylist, 100, LB_ADDSTRING,
249 0, (LPARAM)listentry);
250 }
d2371c81 251 for (i = 0; NULL != (skey = index234(ssh2keys, i)); i++) {
1c2a93c4 252 char listentry[512], *p;
45cebe79 253 int len;
1c2a93c4 254 /*
255 * Replace two spaces in the fingerprint with tabs, for
256 * nice alignment in the box.
257 */
45cebe79 258 p = skey->alg->fingerprint(skey->data);
259 strncpy(listentry, p, sizeof(listentry));
1c2a93c4 260 p = strchr(listentry, ' '); if (p) *p = '\t';
261 p = strchr(listentry, ' '); if (p) *p = '\t';
45cebe79 262 len = strlen(listentry);
263 if (len < sizeof(listentry)-2) {
264 listentry[len] = '\t';
265 strncpy(listentry+len+1, skey->comment, sizeof(listentry)-len-1);
266 }
3c0b3d06 267 SendDlgItemMessage (keylist, 100, LB_ADDSTRING,
1c2a93c4 268 0, (LPARAM)listentry);
3c0b3d06 269 }
270 SendDlgItemMessage (keylist, 100, LB_SETCURSEL, (WPARAM) -1, 0);
271 }
272}
273
274/*
5c58ad2d 275 * This function loads a key from a file and adds it.
276 */
75cab814 277static void add_keyfile(char *filename) {
5c58ad2d 278 char passphrase[PASSPHRASE_MAXLEN];
45cebe79 279 struct RSAKey *rkey;
280 struct ssh2_userkey *skey;
5c58ad2d 281 int needs_pass;
282 int ret;
283 int attempts;
d4de2d2a 284 char *comment;
285 struct PassphraseProcStruct pps;
45cebe79 286 int ver;
287
288 ver = keyfile_version(filename);
289 if (ver == 0) {
290 MessageBox(NULL, "Couldn't load private key.", APPNAME,
291 MB_OK | MB_ICONERROR);
292 return;
293 }
5c58ad2d 294
45cebe79 295 if (ver == 1)
296 needs_pass = rsakey_encrypted(filename, &comment);
297 else
298 needs_pass = ssh2_userkey_encrypted(filename, &comment);
5c58ad2d 299 attempts = 0;
45cebe79 300 if (ver == 1)
301 rkey = smalloc(sizeof(*rkey));
d4de2d2a 302 pps.passphrase = passphrase;
303 pps.comment = comment;
5c58ad2d 304 do {
305 if (needs_pass) {
306 int dlgret;
307 dlgret = DialogBoxParam(instance, MAKEINTRESOURCE(210),
308 NULL, PassphraseProc,
d4de2d2a 309 (LPARAM)&pps);
5c58ad2d 310 if (!dlgret) {
dcbde236 311 if (comment) sfree(comment);
45cebe79 312 if (ver == 1)
313 sfree(rkey);
5c58ad2d 314 return; /* operation cancelled */
315 }
316 } else
317 *passphrase = '\0';
45cebe79 318 if (ver == 1)
319 ret = loadrsakey(filename, rkey, passphrase);
320 else {
321 skey = ssh2_load_userkey(filename, passphrase);
322 if (skey == SSH2_WRONG_PASSPHRASE)
323 ret = -1;
324 else if (!skey)
325 ret = 0;
326 else
327 ret = 1;
328 }
5c58ad2d 329 attempts++;
330 } while (ret == -1);
dcbde236 331 if (comment) sfree(comment);
5c58ad2d 332 if (ret == 0) {
5af3a3b6 333 MessageBox(NULL, "Couldn't load private key.", APPNAME,
5c58ad2d 334 MB_OK | MB_ICONERROR);
45cebe79 335 if (ver == 1)
336 sfree(rkey);
5c58ad2d 337 return;
338 }
45cebe79 339 if (ver == 1) {
ddecd643 340 if (already_running) {
341 unsigned char *request, *response;
342 int reqlen, clen, resplen;
343
ddecd643 344 clen = strlen(rkey->comment);
ddecd643 345
346 reqlen = 4 + 1 + /* length, message type */
347 4 + /* bit count */
348 ssh1_bignum_length(rkey->modulus) +
349 ssh1_bignum_length(rkey->exponent) +
350 ssh1_bignum_length(rkey->private_exponent) +
351 ssh1_bignum_length(rkey->iqmp) +
352 ssh1_bignum_length(rkey->p) +
353 ssh1_bignum_length(rkey->q) +
354 4 + clen /* comment */
355 ;
ddecd643 356
357 request = smalloc(reqlen);
ddecd643 358
ddecd643 359 request[4] = SSH1_AGENTC_ADD_RSA_IDENTITY;
ddecd643 360 reqlen = 5;
361 PUT_32BIT(request+reqlen, bignum_bitcount(rkey->modulus));
362 reqlen += 4;
363 reqlen += ssh1_write_bignum(request+reqlen, rkey->modulus);
364 reqlen += ssh1_write_bignum(request+reqlen, rkey->exponent);
365 reqlen += ssh1_write_bignum(request+reqlen, rkey->private_exponent);
366 reqlen += ssh1_write_bignum(request+reqlen, rkey->iqmp);
367 reqlen += ssh1_write_bignum(request+reqlen, rkey->p);
368 reqlen += ssh1_write_bignum(request+reqlen, rkey->q);
369 PUT_32BIT(request+reqlen, clen);
370 memcpy(request+reqlen+4, rkey->comment, clen);
371 reqlen += 4+clen;
372 PUT_32BIT(request, reqlen-4);
373
ddecd643 374 agent_query(request, reqlen, &response, &resplen);
ddecd643 375 if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS)
376 MessageBox(NULL, "The already running Pageant "
377 "refused to add the key.", APPNAME,
378 MB_OK | MB_ICONERROR);
379 } else {
380 if (add234(rsakeys, rkey) != rkey)
381 sfree(rkey); /* already present, don't waste RAM */
382 }
45cebe79 383 } else {
ddecd643 384 if (already_running) {
385 unsigned char *request, *response;
386 int reqlen, alglen, clen, keybloblen, resplen;
ddecd643 387 alglen = strlen(skey->alg->name);
ddecd643 388 clen = strlen(skey->comment);
ddecd643 389
ddecd643 390 keybloblen = skey->alg->openssh_fmtkey(skey->data, NULL, 0);
ddecd643 391
ddecd643 392 reqlen = 4 + 1 + /* length, message type */
393 4 + alglen + /* algorithm name */
394 keybloblen + /* key data */
395 4 + clen /* comment */
396 ;
ddecd643 397
ddecd643 398 request = smalloc(reqlen);
ddecd643 399
400 request[4] = SSH2_AGENTC_ADD_IDENTITY;
ddecd643 401 reqlen = 5;
ddecd643 402 PUT_32BIT(request+reqlen, alglen);
ddecd643 403 reqlen += 4;
ddecd643 404 memcpy(request+reqlen, skey->alg->name, alglen);
ddecd643 405 reqlen += alglen;
ddecd643 406 reqlen += skey->alg->openssh_fmtkey(skey->data,
407 request+reqlen, keybloblen);
ddecd643 408 PUT_32BIT(request+reqlen, clen);
ddecd643 409 memcpy(request+reqlen+4, skey->comment, clen);
ddecd643 410 PUT_32BIT(request, reqlen-4);
ddecd643 411 reqlen += clen+4;
412
413 agent_query(request, reqlen, &response, &resplen);
ddecd643 414 if (resplen < 5 || response[4] != SSH_AGENT_SUCCESS)
415 MessageBox(NULL, "The already running Pageant"
416 "refused to add the key.", APPNAME,
417 MB_OK | MB_ICONERROR);
ddecd643 418 } else {
419 if (add234(ssh2keys, skey) != skey) {
420 skey->alg->freekey(skey->data);
421 sfree(skey); /* already present, don't waste RAM */
422 }
45cebe79 423 }
424 }
5c58ad2d 425}
426
427/*
428 * This is the main agent function that answers messages.
429 */
75cab814 430static void answer_msg(void *msg) {
d70f60ae 431 unsigned char *p = msg;
432 unsigned char *ret = msg;
5c58ad2d 433 int type;
434
5c58ad2d 435 /*
436 * Get the message type.
437 */
438 type = p[4];
439
440 p += 5;
5c58ad2d 441 switch (type) {
45cebe79 442 case SSH1_AGENTC_REQUEST_RSA_IDENTITIES:
5c58ad2d 443 /*
45cebe79 444 * Reply with SSH1_AGENT_RSA_IDENTITIES_ANSWER.
5c58ad2d 445 */
446 {
5c58ad2d 447 struct RSAKey *key;
448 int len, nkeys;
d2371c81 449 int i;
5c58ad2d 450
451 /*
452 * Count up the number and length of keys we hold.
453 */
454 len = nkeys = 0;
d2371c81 455 for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
5c58ad2d 456 nkeys++;
457 len += 4; /* length field */
458 len += ssh1_bignum_length(key->exponent);
459 len += ssh1_bignum_length(key->modulus);
460 len += 4 + strlen(key->comment);
461 }
462
463 /*
464 * Packet header is the obvious five bytes, plus four
465 * bytes for the key count.
466 */
467 len += 5 + 4;
d70f60ae 468 if (len > AGENT_MAX_MSGLEN)
469 goto failure; /* aaargh! too much stuff! */
470 PUT_32BIT(ret, len-4);
45cebe79 471 ret[4] = SSH1_AGENT_RSA_IDENTITIES_ANSWER;
d70f60ae 472 PUT_32BIT(ret+5, nkeys);
473 p = ret + 5 + 4;
d2371c81 474 for (i = 0; NULL != (key = index234(rsakeys, i)); i++) {
ddecd643 475 PUT_32BIT(p, bignum_bitcount(key->modulus));
d70f60ae 476 p += 4;
477 p += ssh1_write_bignum(p, key->exponent);
478 p += ssh1_write_bignum(p, key->modulus);
479 PUT_32BIT(p, strlen(key->comment));
480 memcpy(p+4, key->comment, strlen(key->comment));
481 p += 4 + strlen(key->comment);
5c58ad2d 482 }
483 }
484 break;
45cebe79 485 case SSH2_AGENTC_REQUEST_IDENTITIES:
5c58ad2d 486 /*
45cebe79 487 * Reply with SSH2_AGENT_IDENTITIES_ANSWER.
488 */
489 {
45cebe79 490 struct ssh2_userkey *key;
491 int len, nkeys;
492 unsigned char *blob;
493 int bloblen;
d2371c81 494 int i;
45cebe79 495
496 /*
497 * Count up the number and length of keys we hold.
498 */
499 len = nkeys = 0;
d2371c81 500 for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
45cebe79 501 nkeys++;
502 len += 4; /* length field */
503 blob = key->alg->public_blob(key->data, &bloblen);
504 len += bloblen;
505 sfree(blob);
506 len += 4 + strlen(key->comment);
507 }
508
509 /*
510 * Packet header is the obvious five bytes, plus four
511 * bytes for the key count.
512 */
513 len += 5 + 4;
514 if (len > AGENT_MAX_MSGLEN)
515 goto failure; /* aaargh! too much stuff! */
516 PUT_32BIT(ret, len-4);
517 ret[4] = SSH2_AGENT_IDENTITIES_ANSWER;
518 PUT_32BIT(ret+5, nkeys);
519 p = ret + 5 + 4;
d2371c81 520 for (i = 0; NULL != (key = index234(ssh2keys, i)); i++) {
45cebe79 521 blob = key->alg->public_blob(key->data, &bloblen);
522 PUT_32BIT(p, bloblen);
523 p += 4;
524 memcpy(p, blob, bloblen);
525 p += bloblen;
526 sfree(blob);
527 PUT_32BIT(p, strlen(key->comment));
528 memcpy(p+4, key->comment, strlen(key->comment));
529 p += 4 + strlen(key->comment);
530 }
531 }
532 break;
533 case SSH1_AGENTC_RSA_CHALLENGE:
534 /*
535 * Reply with either SSH1_AGENT_RSA_RESPONSE or
5c58ad2d 536 * SSH_AGENT_FAILURE, depending on whether we have that key
537 * or not.
538 */
539 {
540 struct RSAKey reqkey, *key;
541 Bignum challenge, response;
542 unsigned char response_source[48], response_md5[16];
543 struct MD5Context md5c;
544 int i, len;
545
546 p += 4;
547 p += ssh1_read_bignum(p, &reqkey.exponent);
548 p += ssh1_read_bignum(p, &reqkey.modulus);
549 p += ssh1_read_bignum(p, &challenge);
550 memcpy(response_source+32, p, 16); p += 16;
551 if (GET_32BIT(p) != 1 ||
552 (key = find234(rsakeys, &reqkey, NULL)) == NULL) {
553 freebn(reqkey.exponent);
554 freebn(reqkey.modulus);
555 freebn(challenge);
556 goto failure;
557 }
558 response = rsadecrypt(challenge, key);
559 for (i = 0; i < 32; i++)
560 response_source[i] = bignum_byte(response, 31-i);
561
562 MD5Init(&md5c);
563 MD5Update(&md5c, response_source, 48);
564 MD5Final(response_md5, &md5c);
565 memset(response_source, 0, 48); /* burn the evidence */
566 freebn(response); /* and that evidence */
567 freebn(challenge); /* yes, and that evidence */
568 freebn(reqkey.exponent); /* and free some memory ... */
569 freebn(reqkey.modulus); /* ... while we're at it. */
570
571 /*
572 * Packet is the obvious five byte header, plus sixteen
573 * bytes of MD5.
574 */
575 len = 5 + 16;
d70f60ae 576 PUT_32BIT(ret, len-4);
45cebe79 577 ret[4] = SSH1_AGENT_RSA_RESPONSE;
d70f60ae 578 memcpy(ret+5, response_md5, 16);
5c58ad2d 579 }
580 break;
45cebe79 581 case SSH2_AGENTC_SIGN_REQUEST:
582 /*
583 * Reply with either SSH2_AGENT_RSA_RESPONSE or
584 * SSH_AGENT_FAILURE, depending on whether we have that key
585 * or not.
586 */
587 {
588 struct ssh2_userkey *key;
589 struct blob b;
590 unsigned char *data, *signature;
591 int datalen, siglen, len;
592
593 b.len = GET_32BIT(p);
594 p += 4;
595 b.blob = p;
596 p += b.len;
597 datalen = GET_32BIT(p);
598 p += 4;
599 data = p;
600 key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm);
601 if (!key)
602 goto failure;
603 signature = key->alg->sign(key->data, data, datalen, &siglen);
604 len = 5+4+siglen;
605 PUT_32BIT(ret, len-4);
606 ret[4] = SSH2_AGENT_SIGN_RESPONSE;
607 PUT_32BIT(ret+5, siglen);
608 memcpy(ret+5+4, signature, siglen);
609 sfree(signature);
610 }
611 break;
612 case SSH1_AGENTC_ADD_RSA_IDENTITY:
5c58ad2d 613 /*
614 * Add to the list and return SSH_AGENT_SUCCESS, or
615 * SSH_AGENT_FAILURE if the key was malformed.
616 */
3c0b3d06 617 {
618 struct RSAKey *key;
619 char *comment;
dcbde236 620 key = smalloc(sizeof(struct RSAKey));
3c0b3d06 621 memset(key, 0, sizeof(key));
622 p += makekey(p, key, NULL, 1);
623 p += makeprivate(p, key);
45cebe79 624 p += ssh1_read_bignum(p, key->iqmp); /* p^-1 mod q */
625 p += ssh1_read_bignum(p, key->p); /* p */
626 p += ssh1_read_bignum(p, key->q); /* q */
dcbde236 627 comment = smalloc(GET_32BIT(p));
3c0b3d06 628 if (comment) {
629 memcpy(comment, p+4, GET_32BIT(p));
630 key->comment = comment;
631 }
632 PUT_32BIT(ret, 1);
633 ret[4] = SSH_AGENT_FAILURE;
634 if (add234(rsakeys, key) == key) {
635 keylist_update();
636 ret[4] = SSH_AGENT_SUCCESS;
637 } else {
638 freersakey(key);
dcbde236 639 sfree(key);
3c0b3d06 640 }
641 }
5c58ad2d 642 break;
45cebe79 643 case SSH2_AGENTC_ADD_IDENTITY:
644 /*
645 * Add to the list and return SSH_AGENT_SUCCESS, or
646 * SSH_AGENT_FAILURE if the key was malformed.
647 */
648 {
649 struct ssh2_userkey *key;
650 char *comment, *alg;
651 int alglen, commlen;
652 int bloblen;
653
654 key = smalloc(sizeof(struct ssh2_userkey));
655
656 alglen = GET_32BIT(p); p += 4;
657 alg = p; p += alglen;
658 /* Add further algorithm names here. */
659 if (alglen == 7 && !memcmp(alg, "ssh-rsa", 7))
660 key->alg = &ssh_rsa;
661 else {
662 sfree(key);
663 goto failure;
664 }
665
666 bloblen = GET_32BIT((unsigned char *)msg) - (p-(unsigned char *)msg-4);
667 key->data = key->alg->openssh_createkey(&p, &bloblen);
668 if (!key->data) {
669 sfree(key);
670 goto failure;
671 }
672 commlen = GET_32BIT(p); p += 4;
673
674 comment = smalloc(commlen+1);
675 if (comment) {
676 memcpy(comment, p, commlen);
677 comment[commlen] = '\0';
678 }
679 key->comment = comment;
680
681 PUT_32BIT(ret, 1);
682 ret[4] = SSH_AGENT_FAILURE;
683 if (add234(ssh2keys, key) == key) {
684 keylist_update();
685 ret[4] = SSH_AGENT_SUCCESS;
686 } else {
687 key->alg->freekey(key->data);
688 sfree(key->comment);
689 sfree(key);
690 }
691 }
692 break;
693 case SSH1_AGENTC_REMOVE_RSA_IDENTITY:
5c58ad2d 694 /*
695 * Remove from the list and return SSH_AGENT_SUCCESS, or
696 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
697 * start with.
698 */
3c0b3d06 699 {
700 struct RSAKey reqkey, *key;
701
702 p += makekey(p, &reqkey, NULL, 0);
703 key = find234(rsakeys, &reqkey, NULL);
704 freebn(reqkey.exponent);
705 freebn(reqkey.modulus);
706 PUT_32BIT(ret, 1);
707 ret[4] = SSH_AGENT_FAILURE;
708 if (key) {
709 del234(rsakeys, key);
710 keylist_update();
d4de2d2a 711 freersakey(key);
45cebe79 712 sfree(key);
3c0b3d06 713 ret[4] = SSH_AGENT_SUCCESS;
714 }
715 }
5c58ad2d 716 break;
45cebe79 717 case SSH2_AGENTC_REMOVE_IDENTITY:
718 /*
719 * Remove from the list and return SSH_AGENT_SUCCESS, or
720 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
721 * start with.
722 */
723 {
724 struct ssh2_userkey *key;
725 struct blob b;
726
727 b.len = GET_32BIT(p);
728 p += 4;
729 b.blob = p;
730 p += b.len;
731 key = find234(ssh2keys, &b, cmpkeys_ssh2_asymm);
732 if (!key)
733 goto failure;
734
735 PUT_32BIT(ret, 1);
736 ret[4] = SSH_AGENT_FAILURE;
737 if (key) {
738 del234(ssh2keys, key);
739 keylist_update();
740 key->alg->freekey(key->data);
741 sfree(key);
742 ret[4] = SSH_AGENT_SUCCESS;
743 }
744 }
745 break;
746 case SSH1_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
747 /*
748 * Remove all SSH1 keys. Always returns success.
749 */
750 {
751 struct RSAKey *rkey;
45cebe79 752
d2371c81 753 while ( (rkey = index234(rsakeys, 0)) != NULL ) {
45cebe79 754 del234(rsakeys, rkey);
755 freersakey(rkey);
756 sfree(rkey);
757 }
758 keylist_update();
759
760 PUT_32BIT(ret, 1);
761 ret[4] = SSH_AGENT_SUCCESS;
762 }
763 break;
764 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
765 /*
766 * Remove all SSH2 keys. Always returns success.
767 */
768 {
769 struct ssh2_userkey *skey;
45cebe79 770
d2371c81 771 while ( (skey = index234(ssh2keys, 0)) != NULL ) {
45cebe79 772 del234(ssh2keys, skey);
773 skey->alg->freekey(skey->data);
774 sfree(skey);
775 }
776 keylist_update();
777
778 PUT_32BIT(ret, 1);
779 ret[4] = SSH_AGENT_SUCCESS;
780 }
781 break;
5c58ad2d 782 default:
783 failure:
784 /*
785 * Unrecognised message. Return SSH_AGENT_FAILURE.
786 */
d70f60ae 787 PUT_32BIT(ret, 1);
788 ret[4] = SSH_AGENT_FAILURE;
5c58ad2d 789 break;
790 }
5c58ad2d 791}
792
793/*
794 * Key comparison function for the 2-3-4 tree of RSA keys.
795 */
45cebe79 796static int cmpkeys_rsa(void *av, void *bv) {
5c58ad2d 797 struct RSAKey *a = (struct RSAKey *)av;
798 struct RSAKey *b = (struct RSAKey *)bv;
799 Bignum am, bm;
800 int alen, blen;
801
802 am = a->modulus;
803 bm = b->modulus;
804 /*
805 * Compare by length of moduli.
806 */
ddecd643 807 alen = bignum_bitcount(am);
808 blen = bignum_bitcount(bm);
5c58ad2d 809 if (alen > blen) return +1; else if (alen < blen) return -1;
810 /*
811 * Now compare by moduli themselves.
812 */
813 alen = (alen + 7) / 8; /* byte count */
814 while (alen-- > 0) {
815 int abyte, bbyte;
816 abyte = bignum_byte(am, alen);
817 bbyte = bignum_byte(bm, alen);
818 if (abyte > bbyte) return +1; else if (abyte < bbyte) return -1;
819 }
820 /*
821 * Give up.
822 */
823 return 0;
824}
825
45cebe79 826/*
827 * Key comparison function for the 2-3-4 tree of SSH2 keys.
828 */
829static int cmpkeys_ssh2(void *av, void *bv) {
830 struct ssh2_userkey *a = (struct ssh2_userkey *)av;
831 struct ssh2_userkey *b = (struct ssh2_userkey *)bv;
832 int i;
833 int alen, blen;
834 unsigned char *ablob, *bblob;
835 int c;
836
837 /*
838 * Compare purely by public blob.
839 */
840 ablob = a->alg->public_blob(a->data, &alen);
841 bblob = b->alg->public_blob(b->data, &blen);
842
843 c = 0;
844 for (i = 0; i < alen && i < blen; i++) {
845 if (ablob[i] < bblob[i]) {
846 c = -1; break;
847 } else if (ablob[i] > bblob[i]) {
848 c = +1; break;
849 }
850 }
851 if (c == 0 && i < alen) c = +1; /* a is longer */
852 if (c == 0 && i < blen) c = -1; /* a is longer */
853
854 sfree(ablob);
855 sfree(bblob);
856
857 return c;
858}
859
860/*
861 * Key comparison function for looking up a blob in the 2-3-4 tree
862 * of SSH2 keys.
863 */
864static int cmpkeys_ssh2_asymm(void *av, void *bv) {
865 struct blob *a = (struct blob *)av;
866 struct ssh2_userkey *b = (struct ssh2_userkey *)bv;
867 int i;
868 int alen, blen;
869 unsigned char *ablob, *bblob;
870 int c;
871
872 /*
873 * Compare purely by public blob.
874 */
875 ablob = a->blob;
876 alen = a->len;
877 bblob = b->alg->public_blob(b->data, &blen);
878
879 c = 0;
880 for (i = 0; i < alen && i < blen; i++) {
881 if (ablob[i] < bblob[i]) {
882 c = -1; break;
883 } else if (ablob[i] > bblob[i]) {
884 c = +1; break;
885 }
886 }
887 if (c == 0 && i < alen) c = +1; /* a is longer */
888 if (c == 0 && i < blen) c = -1; /* a is longer */
889
890 sfree(bblob);
891
892 return c;
893}
894
5c58ad2d 895static void error(char *s) {
896 MessageBox(hwnd, s, APPNAME, MB_OK | MB_ICONERROR);
897}
898
899/*
ab162329 900 * Prompt for a key file to add, and add it.
901 */
902static void prompt_add_keyfile(void) {
903 OPENFILENAME of;
904 char filename[FILENAME_MAX];
905 memset(&of, 0, sizeof(of));
906#ifdef OPENFILENAME_SIZE_VERSION_400
907 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
908#else
909 of.lStructSize = sizeof(of);
910#endif
911 of.hwndOwner = hwnd;
912 of.lpstrFilter = "All Files\0*\0\0\0";
913 of.lpstrCustomFilter = NULL;
914 of.nFilterIndex = 1;
915 of.lpstrFile = filename; *filename = '\0';
916 of.nMaxFile = sizeof(filename);
917 of.lpstrFileTitle = NULL;
918 of.lpstrInitialDir = NULL;
919 of.lpstrTitle = "Select Private Key File";
920 of.Flags = 0;
921 if (GetOpenFileName(&of)) {
922 add_keyfile(filename);
923 keylist_update();
924 }
925}
926
927/*
5c58ad2d 928 * Dialog-box function for the key list box.
929 */
930static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
931 WPARAM wParam, LPARAM lParam) {
45cebe79 932 struct RSAKey *rkey;
933 struct ssh2_userkey *skey;
5c58ad2d 934
935 switch (msg) {
936 case WM_INITDIALOG:
45cebe79 937 /*
938 * Centre the window.
939 */
940 { /* centre the window */
941 RECT rs, rd;
942 HWND hw;
943
944 hw = GetDesktopWindow();
945 if (GetWindowRect (hw, &rs) && GetWindowRect (hwnd, &rd))
946 MoveWindow (hwnd, (rs.right + rs.left + rd.left - rd.right)/2,
947 (rs.bottom + rs.top + rd.top - rd.bottom)/2,
948 rd.right-rd.left, rd.bottom-rd.top, TRUE);
949 }
950
1c2a93c4 951 keylist = hwnd;
952 {
45cebe79 953 static int tabs[] = {35, 60, 210};
954 SendDlgItemMessage (hwnd, 100, LB_SETTABSTOPS,
955 sizeof(tabs)/sizeof(*tabs), (LPARAM) tabs);
1c2a93c4 956 }
957 keylist_update();
5c58ad2d 958 return 0;
959 case WM_COMMAND:
960 switch (LOWORD(wParam)) {
961 case IDOK:
962 case IDCANCEL:
963 keylist = NULL;
964 DestroyWindow(hwnd);
965 return 0;
966 case 101: /* add key */
967 if (HIWORD(wParam) == BN_CLICKED ||
968 HIWORD(wParam) == BN_DOUBLECLICKED) {
ab162329 969 prompt_add_keyfile();
5c58ad2d 970 }
971 return 0;
972 case 102: /* remove key */
973 if (HIWORD(wParam) == BN_CLICKED ||
974 HIWORD(wParam) == BN_DOUBLECLICKED) {
975 int n = SendDlgItemMessage (hwnd, 100, LB_GETCURSEL, 0, 0);
d2371c81 976 int i;
f70a606e 977 if (n == LB_ERR) {
5c58ad2d 978 MessageBeep(0);
979 break;
980 }
d2371c81 981 for (i = 0; NULL != (rkey = index234(rsakeys, i)); i++)
5c58ad2d 982 if (n-- == 0)
983 break;
45cebe79 984 if (rkey) {
985 del234(rsakeys, rkey);
986 freersakey(rkey);
987 sfree(rkey);
988 } else {
d2371c81 989 for (i = 0; NULL != (skey = index234(ssh2keys, i)); i++)
45cebe79 990 if (n-- == 0)
991 break;
992 if (skey) {
993 del234(ssh2keys, skey);
994 skey->alg->freekey(skey->data);
995 sfree(skey);
996 }
997 }
1c2a93c4 998 keylist_update();
5c58ad2d 999 }
1000 return 0;
1001 }
1002 return 0;
1003 case WM_CLOSE:
1004 keylist = NULL;
1005 DestroyWindow(hwnd);
1006 return 0;
1007 }
1008 return 0;
1009}
1010
1011static LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
1012 WPARAM wParam, LPARAM lParam) {
1013 int ret;
1014 static int menuinprogress;
1015
1016 switch (message) {
1017 case WM_SYSTRAY:
1018 if (lParam == WM_RBUTTONUP) {
1019 POINT cursorpos;
1020 GetCursorPos(&cursorpos);
1021 PostMessage(hwnd, WM_SYSTRAY2, cursorpos.x, cursorpos.y);
905beba5 1022 } else if (lParam == WM_LBUTTONDBLCLK) {
1023 /* Equivalent to IDM_VIEWKEYS. */
1024 PostMessage(hwnd, WM_COMMAND, IDM_VIEWKEYS, 0);
5c58ad2d 1025 }
1026 break;
1027 case WM_SYSTRAY2:
1028 if (!menuinprogress) {
1029 menuinprogress = 1;
1030 SetForegroundWindow(hwnd);
1031 ret = TrackPopupMenu(systray_menu,
1032 TPM_RIGHTALIGN | TPM_BOTTOMALIGN |
1033 TPM_RIGHTBUTTON,
1034 wParam, lParam, 0, hwnd, NULL);
1035 menuinprogress = 0;
1036 }
1037 break;
1038 case WM_COMMAND:
1039 case WM_SYSCOMMAND:
1040 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
1041 case IDM_CLOSE:
1042 SendMessage(hwnd, WM_CLOSE, 0, 0);
1043 break;
1044 case IDM_VIEWKEYS:
1045 if (!keylist) {
1046 keylist = CreateDialog (instance, MAKEINTRESOURCE(211),
1047 NULL, KeyListProc);
1048 ShowWindow (keylist, SW_SHOWNORMAL);
905beba5 1049 /*
1050 * Sometimes the window comes up minimised / hidden
1051 * for no obvious reason. Prevent this.
1052 */
1053 SetForegroundWindow(keylist);
1054 SetWindowPos (keylist, HWND_TOP, 0, 0, 0, 0,
1055 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
5c58ad2d 1056 }
1057 break;
ab162329 1058 case IDM_ADDKEY:
1059 prompt_add_keyfile();
1060 break;
5af3a3b6 1061 case IDM_ABOUT:
1062 if (!aboutbox) {
1063 aboutbox = CreateDialog (instance, MAKEINTRESOURCE(213),
1064 NULL, AboutProc);
1065 ShowWindow (aboutbox, SW_SHOWNORMAL);
1066 /*
1067 * Sometimes the window comes up minimised / hidden
1068 * for no obvious reason. Prevent this.
1069 */
1070 SetForegroundWindow(aboutbox);
1071 SetWindowPos (aboutbox, HWND_TOP, 0, 0, 0, 0,
1072 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
1073 }
1074 break;
5c58ad2d 1075 }
1076 break;
1077 case WM_DESTROY:
1078 PostQuitMessage (0);
1079 return 0;
1080 case WM_COPYDATA:
1081 {
1082 COPYDATASTRUCT *cds;
d70f60ae 1083 char *mapname;
1084 void *p;
1085 HANDLE filemap, proc;
1086 PSID mapowner, procowner;
1087 PSECURITY_DESCRIPTOR psd1 = NULL, psd2 = NULL;
1088 int ret = 0;
5c58ad2d 1089
1090 cds = (COPYDATASTRUCT *)lParam;
d70f60ae 1091 if (cds->dwData != AGENT_COPYDATA_ID)
1092 return 0; /* not our message, mate */
1093 mapname = (char *)cds->lpData;
1094 if (mapname[cds->cbData - 1] != '\0')
1095 return 0; /* failure to be ASCIZ! */
1096#ifdef DEBUG_IPC
1097 debug(("mapname is :%s:\r\n", mapname));
1098#endif
1099 filemap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, mapname);
1100#ifdef DEBUG_IPC
1101 debug(("filemap is %p\r\n", filemap));
1102#endif
1103 if (filemap != NULL && filemap != INVALID_HANDLE_VALUE) {
1104 int rc;
123bc6ea 1105#ifndef NO_SECURITY
016ef8ab 1106 if (has_security) {
1107 if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
1108 GetCurrentProcessId())) == NULL) {
d70f60ae 1109#ifdef DEBUG_IPC
016ef8ab 1110 debug(("couldn't get handle for process\r\n"));
d70f60ae 1111#endif
016ef8ab 1112 return 0;
1113 }
1114 if (getsecurityinfo(proc, SE_KERNEL_OBJECT,
1115 OWNER_SECURITY_INFORMATION,
1116 &procowner, NULL, NULL, NULL,
1117 &psd2) != ERROR_SUCCESS) {
d70f60ae 1118#ifdef DEBUG_IPC
016ef8ab 1119 debug(("couldn't get owner info for process\r\n"));
d70f60ae 1120#endif
016ef8ab 1121 CloseHandle(proc);
1122 return 0; /* unable to get security info */
1123 }
d70f60ae 1124 CloseHandle(proc);
016ef8ab 1125 if ((rc = getsecurityinfo(filemap, SE_KERNEL_OBJECT,
1126 OWNER_SECURITY_INFORMATION,
1127 &mapowner, NULL, NULL, NULL,
1128 &psd1) != ERROR_SUCCESS)) {
d70f60ae 1129#ifdef DEBUG_IPC
016ef8ab 1130 debug(("couldn't get owner info for filemap: %d\r\n", rc));
d70f60ae 1131#endif
016ef8ab 1132 return 0;
1133 }
1134#ifdef DEBUG_IPC
1135 debug(("got security stuff\r\n"));
1136#endif
1137 if (!EqualSid(mapowner, procowner))
1138 return 0; /* security ID mismatch! */
d70f60ae 1139#ifdef DEBUG_IPC
016ef8ab 1140 debug(("security stuff matched\r\n"));
d70f60ae 1141#endif
016ef8ab 1142 LocalFree(psd1);
1143 LocalFree(psd2);
1144 } else {
d70f60ae 1145#ifdef DEBUG_IPC
016ef8ab 1146 debug(("security APIs not present\r\n"));
d70f60ae 1147#endif
016ef8ab 1148 }
123bc6ea 1149#endif
d70f60ae 1150 p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
1151#ifdef DEBUG_IPC
1152 debug(("p is %p\r\n", p));
1153 {int i; for(i=0;i<5;i++)debug(("p[%d]=%02x\r\n", i, ((unsigned char *)p)[i]));}
1154#endif
1155 answer_msg(p);
1156 ret = 1;
1157 UnmapViewOfFile(p);
1158 }
1159 CloseHandle(filemap);
1160 return ret;
5c58ad2d 1161 }
5c58ad2d 1162 }
1163
1164 return DefWindowProc (hwnd, message, wParam, lParam);
1165}
1166
ddecd643 1167/*
1168 * Fork and Exec the command in cmdline. [DBW]
1169 */
1170void spawn_cmd(char *cmdline, int show) {
1171 if (ShellExecute(NULL, _T("open"), cmdline,
1172 NULL, NULL, show) <= (HINSTANCE) 32) {
1173 TCHAR sMsg[140];
1174 sprintf(sMsg, _T("Failed to run \"%.100s\", Error: %d"), cmdline,
1175 GetLastError());
1176 MessageBox(NULL, sMsg, APPNAME, MB_OK | MB_ICONEXCLAMATION);
1177 }
1178}
1179
5c58ad2d 1180int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
1181 WNDCLASS wndclass;
5c58ad2d 1182 MSG msg;
016ef8ab 1183 OSVERSIONINFO osi;
1184 HMODULE advapi;
ddecd643 1185 char *command = NULL;
1186 int added_keys = 0;
016ef8ab 1187
1188 /*
1189 * Determine whether we're an NT system (should have security
1190 * APIs) or a non-NT system (don't do security).
1191 */
1192 memset(&osi, 0, sizeof(OSVERSIONINFO));
1193 osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1194 if (GetVersionEx(&osi) && osi.dwPlatformId==VER_PLATFORM_WIN32_NT) {
1195 has_security = TRUE;
1196 } else
1197 has_security = FALSE;
1198
1199 if (has_security) {
123bc6ea 1200#ifndef NO_SECURITY
016ef8ab 1201 /*
ddecd643 1202 * Attempt to get the security API we need.
016ef8ab 1203 */
1204 advapi = LoadLibrary("ADVAPI32.DLL");
1205 getsecurityinfo = (gsi_fn_t)GetProcAddress(advapi, "GetSecurityInfo");
1206 if (!getsecurityinfo) {
1207 MessageBox(NULL,
1208 "Unable to access security APIs. Pageant will\n"
1209 "not run, in case it causes a security breach.",
1210 "Pageant Fatal Error", MB_ICONERROR | MB_OK);
1211 return 1;
1212 }
123bc6ea 1213#else
1214 MessageBox(NULL,
1215 "This program has been compiled for Win9X and will\n"
1216 "not run on NT, in case it causes a security breach.",
1217 "Pageant Fatal Error", MB_ICONERROR | MB_OK);
1218 return 1;
1219#endif
016ef8ab 1220 } else
1221 advapi = NULL;
5c58ad2d 1222
1223 instance = inst;
1224
ddecd643 1225 /*
1226 * Find out if Pageant is already running.
1227 */
1228 already_running = FALSE;
260f3dec 1229 if (agent_exists())
ddecd643 1230 already_running = TRUE;
1231 else {
1232
1233 if (!prev) {
1234 wndclass.style = 0;
1235 wndclass.lpfnWndProc = WndProc;
1236 wndclass.cbClsExtra = 0;
1237 wndclass.cbWndExtra = 0;
1238 wndclass.hInstance = inst;
1239 wndclass.hIcon = LoadIcon (inst,
1240 MAKEINTRESOURCE(IDI_MAINICON));
1241 wndclass.hCursor = LoadCursor (NULL, IDC_IBEAM);
1242 wndclass.hbrBackground = GetStockObject (BLACK_BRUSH);
1243 wndclass.lpszMenuName = NULL;
1244 wndclass.lpszClassName = APPNAME;
1245
1246 RegisterClass (&wndclass);
1247 }
5c58ad2d 1248
ddecd643 1249 hwnd = keylist = NULL;
5c58ad2d 1250
ddecd643 1251 hwnd = CreateWindow (APPNAME, APPNAME,
1252 WS_OVERLAPPEDWINDOW | WS_VSCROLL,
1253 CW_USEDEFAULT, CW_USEDEFAULT,
1254 100, 100, NULL, NULL, inst, NULL);
5c58ad2d 1255
ddecd643 1256 /* Set up a system tray icon */
1257 {
1258 BOOL res;
1259 NOTIFYICONDATA tnid;
1260 HICON hicon;
5c58ad2d 1261
1262#ifdef NIM_SETVERSION
ddecd643 1263 tnid.uVersion = 0;
1264 res = Shell_NotifyIcon(NIM_SETVERSION, &tnid);
5c58ad2d 1265#endif
1266
ddecd643 1267 tnid.cbSize = sizeof(NOTIFYICONDATA);
1268 tnid.hWnd = hwnd;
1269 tnid.uID = 1; /* unique within this systray use */
1270 tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
1271 tnid.uCallbackMessage = WM_SYSTRAY;
1272 tnid.hIcon = hicon = LoadIcon (instance, MAKEINTRESOURCE(201));
1273 strcpy(tnid.szTip, "Pageant (PuTTY authentication agent)");
1274
1275 res = Shell_NotifyIcon(NIM_ADD, &tnid);
1276
1277 if (hicon)
1278 DestroyIcon(hicon);
1279
1280 systray_menu = CreatePopupMenu();
1281 /* accelerators used: vkxa */
1282 AppendMenu (systray_menu, MF_ENABLED, IDM_VIEWKEYS, "&View Keys");
1283 AppendMenu (systray_menu, MF_ENABLED, IDM_ADDKEY, "Add &Key");
1284 AppendMenu (systray_menu, MF_ENABLED, IDM_ABOUT, "&About");
1285 AppendMenu (systray_menu, MF_ENABLED, IDM_CLOSE, "E&xit");
1286 }
5c58ad2d 1287
ddecd643 1288 ShowWindow (hwnd, SW_HIDE);
5c58ad2d 1289
ddecd643 1290 /*
1291 * Initialise storage for RSA keys.
1292 */
1293 rsakeys = newtree234(cmpkeys_rsa);
1294 ssh2keys = newtree234(cmpkeys_ssh2);
1295
1296 }
dacbd0e8 1297
1298 /*
45cebe79 1299 * Process the command line and add keys as listed on it.
ddecd643 1300 * If we already determined that we need to spawn a program from above we
1301 * need to ignore the first two arguments. [DBW]
5c58ad2d 1302 */
1303 {
8c42f28c 1304 char *p;
1305 int inquotes = 0;
ddecd643 1306 int ignorearg = 0;
8c42f28c 1307 p = cmdline;
dacbd0e8 1308 while (*p) {
1309 while (*p && isspace(*p)) p++;
1310 if (*p && !isspace(*p)) {
8c42f28c 1311 char *q = p, *pp = p;
1312 while (*p && (inquotes || !isspace(*p)))
1313 {
1314 if (*p == '"') {
1315 inquotes = !inquotes;
1316 p++;
1317 continue;
1318 }
1319 *pp++ = *p++;
1320 }
1321 if (*pp) {
1322 if (*p) p++;
1323 *pp++ = '\0';
1324 }
ddecd643 1325 if (!strcmp(q, "-c")) {
1326 /*
1327 * If we see `-c', then the rest of the
1328 * command line should be treated as a
1329 * command to be spawned.
1330 */
1331 while (*p && isspace(*p)) p++;
1332 command = p;
1333 break;
1334 } else {
1335 add_keyfile(q);
1336 added_keys = TRUE;
1337 }
dacbd0e8 1338 }
1339 }
5c58ad2d 1340 }
1341
ddecd643 1342 if (command) spawn_cmd (command, show);
1343
1344 /*
1345 * If Pageant was already running, we leave now. If we haven't
1346 * even taken any auxiliary action (spawned a command or added
1347 * keys), complain.
1348 */
1349 if (already_running) {
1350 if (!command && !added_keys) {
1351 MessageBox(NULL, "Pageant is already running", "Pageant Error",
1352 MB_ICONERROR | MB_OK);
1353 }
1354 if (advapi) FreeLibrary(advapi);
1355 return 0;
1356 }
1357
5c58ad2d 1358 /*
dacbd0e8 1359 * Main message loop.
5c58ad2d 1360 */
5c58ad2d 1361 while (GetMessage(&msg, NULL, 0, 0) == 1) {
1362 TranslateMessage(&msg);
1363 DispatchMessage(&msg);
1364 }
1365
1366 /* Clean up the system tray icon */
1367 {
1368 NOTIFYICONDATA tnid;
1369
1370 tnid.cbSize = sizeof(NOTIFYICONDATA);
1371 tnid.hWnd = hwnd;
1372 tnid.uID = 1;
1373
1374 Shell_NotifyIcon(NIM_DELETE, &tnid);
1375
1376 DestroyMenu(systray_menu);
1377 }
1378
016ef8ab 1379 if (advapi) FreeLibrary(advapi);
5c58ad2d 1380 exit(msg.wParam);
1381}