Oops - free the key after removing it!
[u/mdw/putty] / pageant.c
CommitLineData
5c58ad2d 1/*
2 * Pageant: the PuTTY Authentication Agent.
3 */
4
5#include <windows.h>
d70f60ae 6#include <aclapi.h>
5c58ad2d 7#include <stdio.h> /* FIXME */
8#include "putty.h" /* FIXME */
9#include "ssh.h"
10#include "tree234.h"
11
12#define IDI_MAINICON 200
13#define IDI_TRAYICON 201
14
15#define WM_XUSER (WM_USER + 0x2000)
16#define WM_SYSTRAY (WM_XUSER + 6)
17#define WM_SYSTRAY2 (WM_XUSER + 7)
d70f60ae 18
19#define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
20
21/*
22 * FIXME: maybe some day we can sort this out ...
23 */
24#define AGENT_MAX_MSGLEN 8192
5c58ad2d 25
26#define IDM_CLOSE 0x0010
27#define IDM_VIEWKEYS 0x0020
28
29#define APPNAME "Pageant"
30
5c58ad2d 31#define SSH_AGENTC_REQUEST_RSA_IDENTITIES 1
32#define SSH_AGENT_RSA_IDENTITIES_ANSWER 2
33#define SSH_AGENTC_RSA_CHALLENGE 3
34#define SSH_AGENT_RSA_RESPONSE 4
35#define SSH_AGENT_FAILURE 5
36#define SSH_AGENT_SUCCESS 6
37#define SSH_AGENTC_ADD_RSA_IDENTITY 7
38#define SSH_AGENTC_REMOVE_RSA_IDENTITY 8
39
40HINSTANCE instance;
41HWND hwnd;
42HWND keylist;
43HMENU systray_menu;
44
45tree234 *rsakeys;
46
016ef8ab 47int has_security;
48typedef DWORD (WINAPI *gsi_fn_t)
49 (HANDLE, SE_OBJECT_TYPE, SECURITY_INFORMATION,
50 PSID *, PSID *, PACL *, PACL *,
51 PSECURITY_DESCRIPTOR *);
52gsi_fn_t getsecurityinfo;
53
5c58ad2d 54/*
55 * We need this to link with the RSA code, because rsaencrypt()
56 * pads its data with random bytes. Since we only use rsadecrypt(),
57 * which is deterministic, this should never be called.
58 *
59 * If it _is_ called, there is a _serious_ problem, because it
60 * won't generate true random numbers. So we must scream, panic,
61 * and exit immediately if that should happen.
62 */
63int random_byte(void) {
64 MessageBox(hwnd, "Internal Error", APPNAME, MB_OK | MB_ICONERROR);
65 exit(0);
66}
67
68/*
69 * This function is needed to link with the DES code. We need not
70 * have it do anything at all.
71 */
72void logevent(char *msg) {
73}
74
75#define GET_32BIT(cp) \
76 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
77 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
78 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
79 ((unsigned long)(unsigned char)(cp)[3]))
80
81#define PUT_32BIT(cp, value) { \
82 (cp)[0] = (unsigned char)((value) >> 24); \
83 (cp)[1] = (unsigned char)((value) >> 16); \
84 (cp)[2] = (unsigned char)((value) >> 8); \
85 (cp)[3] = (unsigned char)(value); }
86
87#define PASSPHRASE_MAXLEN 512
88
d4de2d2a 89struct PassphraseProcStruct {
90 char *passphrase;
91 char *comment;
92};
93
5c58ad2d 94/*
95 * Dialog-box function for the passphrase box.
96 */
97static int CALLBACK PassphraseProc(HWND hwnd, UINT msg,
98 WPARAM wParam, LPARAM lParam) {
99 static char *passphrase;
d4de2d2a 100 struct PassphraseProcStruct *p;
5c58ad2d 101
102 switch (msg) {
103 case WM_INITDIALOG:
d4de2d2a 104 p = (struct PassphraseProcStruct *)lParam;
105 passphrase = p->passphrase;
106 if (p->comment)
107 SetDlgItemText(hwnd, 101, p->comment);
5c58ad2d 108 *passphrase = 0;
109 return 0;
110 case WM_COMMAND:
111 switch (LOWORD(wParam)) {
112 case IDOK:
113 if (*passphrase)
114 EndDialog (hwnd, 1);
115 else
116 MessageBeep (0);
117 return 0;
118 case IDCANCEL:
119 EndDialog (hwnd, 0);
120 return 0;
121 case 102: /* edit box */
122 if (HIWORD(wParam) == EN_CHANGE) {
123 GetDlgItemText (hwnd, 102, passphrase, PASSPHRASE_MAXLEN-1);
124 passphrase[PASSPHRASE_MAXLEN-1] = '\0';
125 }
126 return 0;
127 }
128 return 0;
129 case WM_CLOSE:
130 EndDialog (hwnd, 0);
131 return 0;
132 }
133 return 0;
134}
135
136/*
3c0b3d06 137 * Update the visible key list.
138 */
139void keylist_update(void) {
140 struct RSAKey *key;
141 enum234 e;
142
143 if (keylist) {
144 SendDlgItemMessage(keylist, 100, LB_RESETCONTENT, 0, 0);
145 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
146 SendDlgItemMessage (keylist, 100, LB_ADDSTRING,
147 0, (LPARAM) key->comment);
148 }
149 SendDlgItemMessage (keylist, 100, LB_SETCURSEL, (WPARAM) -1, 0);
150 }
151}
152
153/*
5c58ad2d 154 * This function loads a key from a file and adds it.
155 */
156void add_keyfile(char *filename) {
157 char passphrase[PASSPHRASE_MAXLEN];
158 struct RSAKey *key;
159 int needs_pass;
160 int ret;
161 int attempts;
d4de2d2a 162 char *comment;
163 struct PassphraseProcStruct pps;
5c58ad2d 164
d4de2d2a 165 needs_pass = rsakey_encrypted(filename, &comment);
5c58ad2d 166 attempts = 0;
167 key = malloc(sizeof(*key));
d4de2d2a 168 pps.passphrase = passphrase;
169 pps.comment = comment;
5c58ad2d 170 do {
171 if (needs_pass) {
172 int dlgret;
173 dlgret = DialogBoxParam(instance, MAKEINTRESOURCE(210),
174 NULL, PassphraseProc,
d4de2d2a 175 (LPARAM)&pps);
5c58ad2d 176 if (!dlgret) {
d4de2d2a 177 if (comment) free(comment);
5c58ad2d 178 free(key);
179 return; /* operation cancelled */
180 }
181 } else
182 *passphrase = '\0';
183 ret = loadrsakey(filename, key, passphrase);
184 attempts++;
185 } while (ret == -1);
d4de2d2a 186 if (comment) free(comment);
5c58ad2d 187 if (ret == 0) {
188 MessageBox(NULL, "Couldn't load public key.", APPNAME,
189 MB_OK | MB_ICONERROR);
190 free(key);
191 return;
192 }
193 if (add234(rsakeys, key) != key)
194 free(key); /* already present, don't waste RAM */
195}
196
197/*
198 * This is the main agent function that answers messages.
199 */
d70f60ae 200void answer_msg(void *msg) {
201 unsigned char *p = msg;
202 unsigned char *ret = msg;
5c58ad2d 203 int type;
204
5c58ad2d 205 /*
206 * Get the message type.
207 */
208 type = p[4];
209
210 p += 5;
5c58ad2d 211 switch (type) {
212 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
213 /*
214 * Reply with SSH_AGENT_RSA_IDENTITIES_ANSWER.
215 */
216 {
217 enum234 e;
218 struct RSAKey *key;
219 int len, nkeys;
220
221 /*
222 * Count up the number and length of keys we hold.
223 */
224 len = nkeys = 0;
225 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
226 nkeys++;
227 len += 4; /* length field */
228 len += ssh1_bignum_length(key->exponent);
229 len += ssh1_bignum_length(key->modulus);
230 len += 4 + strlen(key->comment);
231 }
232
233 /*
234 * Packet header is the obvious five bytes, plus four
235 * bytes for the key count.
236 */
237 len += 5 + 4;
d70f60ae 238 if (len > AGENT_MAX_MSGLEN)
239 goto failure; /* aaargh! too much stuff! */
240 PUT_32BIT(ret, len-4);
241 ret[4] = SSH_AGENT_RSA_IDENTITIES_ANSWER;
242 PUT_32BIT(ret+5, nkeys);
243 p = ret + 5 + 4;
244 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
245 PUT_32BIT(p, ssh1_bignum_bitcount(key->modulus));
246 p += 4;
247 p += ssh1_write_bignum(p, key->exponent);
248 p += ssh1_write_bignum(p, key->modulus);
249 PUT_32BIT(p, strlen(key->comment));
250 memcpy(p+4, key->comment, strlen(key->comment));
251 p += 4 + strlen(key->comment);
5c58ad2d 252 }
253 }
254 break;
255 case SSH_AGENTC_RSA_CHALLENGE:
256 /*
257 * Reply with either SSH_AGENT_RSA_RESPONSE or
258 * SSH_AGENT_FAILURE, depending on whether we have that key
259 * or not.
260 */
261 {
262 struct RSAKey reqkey, *key;
263 Bignum challenge, response;
264 unsigned char response_source[48], response_md5[16];
265 struct MD5Context md5c;
266 int i, len;
267
268 p += 4;
269 p += ssh1_read_bignum(p, &reqkey.exponent);
270 p += ssh1_read_bignum(p, &reqkey.modulus);
271 p += ssh1_read_bignum(p, &challenge);
272 memcpy(response_source+32, p, 16); p += 16;
273 if (GET_32BIT(p) != 1 ||
274 (key = find234(rsakeys, &reqkey, NULL)) == NULL) {
275 freebn(reqkey.exponent);
276 freebn(reqkey.modulus);
277 freebn(challenge);
278 goto failure;
279 }
280 response = rsadecrypt(challenge, key);
281 for (i = 0; i < 32; i++)
282 response_source[i] = bignum_byte(response, 31-i);
283
284 MD5Init(&md5c);
285 MD5Update(&md5c, response_source, 48);
286 MD5Final(response_md5, &md5c);
287 memset(response_source, 0, 48); /* burn the evidence */
288 freebn(response); /* and that evidence */
289 freebn(challenge); /* yes, and that evidence */
290 freebn(reqkey.exponent); /* and free some memory ... */
291 freebn(reqkey.modulus); /* ... while we're at it. */
292
293 /*
294 * Packet is the obvious five byte header, plus sixteen
295 * bytes of MD5.
296 */
297 len = 5 + 16;
d70f60ae 298 PUT_32BIT(ret, len-4);
299 ret[4] = SSH_AGENT_RSA_RESPONSE;
300 memcpy(ret+5, response_md5, 16);
5c58ad2d 301 }
302 break;
5c58ad2d 303 case SSH_AGENTC_ADD_RSA_IDENTITY:
304 /*
305 * Add to the list and return SSH_AGENT_SUCCESS, or
306 * SSH_AGENT_FAILURE if the key was malformed.
307 */
3c0b3d06 308 {
309 struct RSAKey *key;
310 char *comment;
311 key = malloc(sizeof(struct RSAKey));
312 memset(key, 0, sizeof(key));
313 p += makekey(p, key, NULL, 1);
314 p += makeprivate(p, key);
315 p += ssh1_read_bignum(p, NULL); /* p^-1 mod q */
316 p += ssh1_read_bignum(p, NULL); /* p */
317 p += ssh1_read_bignum(p, NULL); /* q */
318 comment = malloc(GET_32BIT(p));
319 if (comment) {
320 memcpy(comment, p+4, GET_32BIT(p));
321 key->comment = comment;
322 }
323 PUT_32BIT(ret, 1);
324 ret[4] = SSH_AGENT_FAILURE;
325 if (add234(rsakeys, key) == key) {
326 keylist_update();
327 ret[4] = SSH_AGENT_SUCCESS;
328 } else {
329 freersakey(key);
330 }
331 }
5c58ad2d 332 break;
333 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
334 /*
335 * Remove from the list and return SSH_AGENT_SUCCESS, or
336 * perhaps SSH_AGENT_FAILURE if it wasn't in the list to
337 * start with.
338 */
3c0b3d06 339 {
340 struct RSAKey reqkey, *key;
341
342 p += makekey(p, &reqkey, NULL, 0);
343 key = find234(rsakeys, &reqkey, NULL);
344 freebn(reqkey.exponent);
345 freebn(reqkey.modulus);
346 PUT_32BIT(ret, 1);
347 ret[4] = SSH_AGENT_FAILURE;
348 if (key) {
349 del234(rsakeys, key);
350 keylist_update();
d4de2d2a 351 freersakey(key);
3c0b3d06 352 ret[4] = SSH_AGENT_SUCCESS;
353 }
354 }
5c58ad2d 355 break;
5c58ad2d 356 default:
357 failure:
358 /*
359 * Unrecognised message. Return SSH_AGENT_FAILURE.
360 */
d70f60ae 361 PUT_32BIT(ret, 1);
362 ret[4] = SSH_AGENT_FAILURE;
5c58ad2d 363 break;
364 }
5c58ad2d 365}
366
367/*
368 * Key comparison function for the 2-3-4 tree of RSA keys.
369 */
370int cmpkeys(void *av, void *bv) {
371 struct RSAKey *a = (struct RSAKey *)av;
372 struct RSAKey *b = (struct RSAKey *)bv;
373 Bignum am, bm;
374 int alen, blen;
375
376 am = a->modulus;
377 bm = b->modulus;
378 /*
379 * Compare by length of moduli.
380 */
381 alen = ssh1_bignum_bitcount(am);
382 blen = ssh1_bignum_bitcount(bm);
383 if (alen > blen) return +1; else if (alen < blen) return -1;
384 /*
385 * Now compare by moduli themselves.
386 */
387 alen = (alen + 7) / 8; /* byte count */
388 while (alen-- > 0) {
389 int abyte, bbyte;
390 abyte = bignum_byte(am, alen);
391 bbyte = bignum_byte(bm, alen);
392 if (abyte > bbyte) return +1; else if (abyte < bbyte) return -1;
393 }
394 /*
395 * Give up.
396 */
397 return 0;
398}
399
400static void error(char *s) {
401 MessageBox(hwnd, s, APPNAME, MB_OK | MB_ICONERROR);
402}
403
404/*
405 * Dialog-box function for the key list box.
406 */
407static int CALLBACK KeyListProc(HWND hwnd, UINT msg,
408 WPARAM wParam, LPARAM lParam) {
409 enum234 e;
410 struct RSAKey *key;
411 OPENFILENAME of;
412 char filename[FILENAME_MAX];
413
414 switch (msg) {
415 case WM_INITDIALOG:
416 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
417 SendDlgItemMessage (hwnd, 100, LB_ADDSTRING,
418 0, (LPARAM) key->comment);
419 }
420 return 0;
421 case WM_COMMAND:
422 switch (LOWORD(wParam)) {
423 case IDOK:
424 case IDCANCEL:
425 keylist = NULL;
426 DestroyWindow(hwnd);
427 return 0;
428 case 101: /* add key */
429 if (HIWORD(wParam) == BN_CLICKED ||
430 HIWORD(wParam) == BN_DOUBLECLICKED) {
431 memset(&of, 0, sizeof(of));
432#ifdef OPENFILENAME_SIZE_VERSION_400
433 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
434#else
435 of.lStructSize = sizeof(of);
436#endif
437 of.hwndOwner = hwnd;
438 of.lpstrFilter = "All Files\0*\0\0\0";
439 of.lpstrCustomFilter = NULL;
440 of.nFilterIndex = 1;
441 of.lpstrFile = filename; *filename = '\0';
442 of.nMaxFile = sizeof(filename);
443 of.lpstrFileTitle = NULL;
444 of.lpstrInitialDir = NULL;
445 of.lpstrTitle = "Select Public Key File";
446 of.Flags = 0;
447 if (GetOpenFileName(&of)) {
448 add_keyfile(filename);
3c0b3d06 449 keylist_update();
5c58ad2d 450 }
5c58ad2d 451 }
452 return 0;
453 case 102: /* remove key */
454 if (HIWORD(wParam) == BN_CLICKED ||
455 HIWORD(wParam) == BN_DOUBLECLICKED) {
456 int n = SendDlgItemMessage (hwnd, 100, LB_GETCURSEL, 0, 0);
457 if (n == LB_ERR || n == 0) {
458 MessageBeep(0);
459 break;
460 }
461 for (key = first234(rsakeys, &e); key; key = next234(&e))
462 if (n-- == 0)
463 break;
464 del234(rsakeys, key);
465 freersakey(key); free(key);
466 SendDlgItemMessage(hwnd, 100, LB_RESETCONTENT, 0, 0);
467 for (key = first234(rsakeys, &e); key; key = next234(&e)) {
468 SendDlgItemMessage (hwnd, 100, LB_ADDSTRING,
469 0, (LPARAM) key->comment);
470 }
471 SendDlgItemMessage (hwnd, 100, LB_SETCURSEL, (WPARAM) -1, 0);
472 }
473 return 0;
474 }
475 return 0;
476 case WM_CLOSE:
477 keylist = NULL;
478 DestroyWindow(hwnd);
479 return 0;
480 }
481 return 0;
482}
483
484static LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
485 WPARAM wParam, LPARAM lParam) {
486 int ret;
487 static int menuinprogress;
488
489 switch (message) {
490 case WM_SYSTRAY:
491 if (lParam == WM_RBUTTONUP) {
492 POINT cursorpos;
493 GetCursorPos(&cursorpos);
494 PostMessage(hwnd, WM_SYSTRAY2, cursorpos.x, cursorpos.y);
905beba5 495 } else if (lParam == WM_LBUTTONDBLCLK) {
496 /* Equivalent to IDM_VIEWKEYS. */
497 PostMessage(hwnd, WM_COMMAND, IDM_VIEWKEYS, 0);
5c58ad2d 498 }
499 break;
500 case WM_SYSTRAY2:
501 if (!menuinprogress) {
502 menuinprogress = 1;
503 SetForegroundWindow(hwnd);
504 ret = TrackPopupMenu(systray_menu,
505 TPM_RIGHTALIGN | TPM_BOTTOMALIGN |
506 TPM_RIGHTBUTTON,
507 wParam, lParam, 0, hwnd, NULL);
508 menuinprogress = 0;
509 }
510 break;
511 case WM_COMMAND:
512 case WM_SYSCOMMAND:
513 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
514 case IDM_CLOSE:
515 SendMessage(hwnd, WM_CLOSE, 0, 0);
516 break;
517 case IDM_VIEWKEYS:
518 if (!keylist) {
519 keylist = CreateDialog (instance, MAKEINTRESOURCE(211),
520 NULL, KeyListProc);
521 ShowWindow (keylist, SW_SHOWNORMAL);
905beba5 522 /*
523 * Sometimes the window comes up minimised / hidden
524 * for no obvious reason. Prevent this.
525 */
526 SetForegroundWindow(keylist);
527 SetWindowPos (keylist, HWND_TOP, 0, 0, 0, 0,
528 SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
5c58ad2d 529 }
530 break;
531 }
532 break;
533 case WM_DESTROY:
534 PostQuitMessage (0);
535 return 0;
536 case WM_COPYDATA:
537 {
538 COPYDATASTRUCT *cds;
d70f60ae 539 char *mapname;
540 void *p;
541 HANDLE filemap, proc;
542 PSID mapowner, procowner;
543 PSECURITY_DESCRIPTOR psd1 = NULL, psd2 = NULL;
544 int ret = 0;
5c58ad2d 545
546 cds = (COPYDATASTRUCT *)lParam;
d70f60ae 547 if (cds->dwData != AGENT_COPYDATA_ID)
548 return 0; /* not our message, mate */
549 mapname = (char *)cds->lpData;
550 if (mapname[cds->cbData - 1] != '\0')
551 return 0; /* failure to be ASCIZ! */
552#ifdef DEBUG_IPC
553 debug(("mapname is :%s:\r\n", mapname));
554#endif
555 filemap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, mapname);
556#ifdef DEBUG_IPC
557 debug(("filemap is %p\r\n", filemap));
558#endif
559 if (filemap != NULL && filemap != INVALID_HANDLE_VALUE) {
560 int rc;
016ef8ab 561 if (has_security) {
562 if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
563 GetCurrentProcessId())) == NULL) {
d70f60ae 564#ifdef DEBUG_IPC
016ef8ab 565 debug(("couldn't get handle for process\r\n"));
d70f60ae 566#endif
016ef8ab 567 return 0;
568 }
569 if (getsecurityinfo(proc, SE_KERNEL_OBJECT,
570 OWNER_SECURITY_INFORMATION,
571 &procowner, NULL, NULL, NULL,
572 &psd2) != ERROR_SUCCESS) {
d70f60ae 573#ifdef DEBUG_IPC
016ef8ab 574 debug(("couldn't get owner info for process\r\n"));
d70f60ae 575#endif
016ef8ab 576 CloseHandle(proc);
577 return 0; /* unable to get security info */
578 }
d70f60ae 579 CloseHandle(proc);
016ef8ab 580 if ((rc = getsecurityinfo(filemap, SE_KERNEL_OBJECT,
581 OWNER_SECURITY_INFORMATION,
582 &mapowner, NULL, NULL, NULL,
583 &psd1) != ERROR_SUCCESS)) {
d70f60ae 584#ifdef DEBUG_IPC
016ef8ab 585 debug(("couldn't get owner info for filemap: %d\r\n", rc));
d70f60ae 586#endif
016ef8ab 587 return 0;
588 }
589#ifdef DEBUG_IPC
590 debug(("got security stuff\r\n"));
591#endif
592 if (!EqualSid(mapowner, procowner))
593 return 0; /* security ID mismatch! */
d70f60ae 594#ifdef DEBUG_IPC
016ef8ab 595 debug(("security stuff matched\r\n"));
d70f60ae 596#endif
016ef8ab 597 LocalFree(psd1);
598 LocalFree(psd2);
599 } else {
d70f60ae 600#ifdef DEBUG_IPC
016ef8ab 601 debug(("security APIs not present\r\n"));
d70f60ae 602#endif
016ef8ab 603 }
d70f60ae 604 p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
605#ifdef DEBUG_IPC
606 debug(("p is %p\r\n", p));
607 {int i; for(i=0;i<5;i++)debug(("p[%d]=%02x\r\n", i, ((unsigned char *)p)[i]));}
608#endif
609 answer_msg(p);
610 ret = 1;
611 UnmapViewOfFile(p);
612 }
613 CloseHandle(filemap);
614 return ret;
5c58ad2d 615 }
5c58ad2d 616 }
617
618 return DefWindowProc (hwnd, message, wParam, lParam);
619}
620
621int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
622 WNDCLASS wndclass;
5c58ad2d 623 MSG msg;
016ef8ab 624 OSVERSIONINFO osi;
625 HMODULE advapi;
626
627 /*
628 * Determine whether we're an NT system (should have security
629 * APIs) or a non-NT system (don't do security).
630 */
631 memset(&osi, 0, sizeof(OSVERSIONINFO));
632 osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
633 if (GetVersionEx(&osi) && osi.dwPlatformId==VER_PLATFORM_WIN32_NT) {
634 has_security = TRUE;
635 } else
636 has_security = FALSE;
637
638 if (has_security) {
639 /*
640 * Attempt to ge the security API we need.
641 */
642 advapi = LoadLibrary("ADVAPI32.DLL");
643 getsecurityinfo = (gsi_fn_t)GetProcAddress(advapi, "GetSecurityInfo");
644 if (!getsecurityinfo) {
645 MessageBox(NULL,
646 "Unable to access security APIs. Pageant will\n"
647 "not run, in case it causes a security breach.",
648 "Pageant Fatal Error", MB_ICONERROR | MB_OK);
649 return 1;
650 }
651 } else
652 advapi = NULL;
5c58ad2d 653
2faac2e1 654 /*
655 * First bomb out totally if we are already running.
656 */
657 if (FindWindow("Pageant", "Pageant")) {
658 MessageBox(NULL, "Pageant is already running", "Pageant Error",
659 MB_ICONERROR | MB_OK);
016ef8ab 660 if (advapi) FreeLibrary(advapi);
2faac2e1 661 return 0;
662 }
663
5c58ad2d 664 instance = inst;
665
666 if (!prev) {
667 wndclass.style = 0;
668 wndclass.lpfnWndProc = WndProc;
669 wndclass.cbClsExtra = 0;
670 wndclass.cbWndExtra = 0;
671 wndclass.hInstance = inst;
672 wndclass.hIcon = LoadIcon (inst,
673 MAKEINTRESOURCE(IDI_MAINICON));
674 wndclass.hCursor = LoadCursor (NULL, IDC_IBEAM);
675 wndclass.hbrBackground = GetStockObject (BLACK_BRUSH);
676 wndclass.lpszMenuName = NULL;
677 wndclass.lpszClassName = APPNAME;
678
679 RegisterClass (&wndclass);
680 }
681
682 hwnd = keylist = NULL;
683
684 hwnd = CreateWindow (APPNAME, APPNAME,
685 WS_OVERLAPPEDWINDOW | WS_VSCROLL,
686 CW_USEDEFAULT, CW_USEDEFAULT,
687 100, 100, NULL, NULL, inst, NULL);
688
689 /* Set up a system tray icon */
690 {
691 BOOL res;
692 NOTIFYICONDATA tnid;
693 HICON hicon;
694
695#ifdef NIM_SETVERSION
696 tnid.uVersion = 0;
697 res = Shell_NotifyIcon(NIM_SETVERSION, &tnid);
698#endif
699
700 tnid.cbSize = sizeof(NOTIFYICONDATA);
701 tnid.hWnd = hwnd;
702 tnid.uID = 1; /* unique within this systray use */
703 tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
704 tnid.uCallbackMessage = WM_SYSTRAY;
705 tnid.hIcon = hicon = LoadIcon (instance, MAKEINTRESOURCE(201));
706 strcpy(tnid.szTip, "Pageant (PuTTY authentication agent)");
707
708 res = Shell_NotifyIcon(NIM_ADD, &tnid);
709
710 if (hicon)
711 DestroyIcon(hicon);
712
713 systray_menu = CreatePopupMenu();
70acd63f 714 AppendMenu (systray_menu, MF_ENABLED, IDM_VIEWKEYS, "&View Keys");
715 AppendMenu (systray_menu, MF_ENABLED, IDM_CLOSE, "E&xit");
5c58ad2d 716 }
717
718 ShowWindow (hwnd, SW_HIDE);
719
720 /*
dacbd0e8 721 * Initialise storage for RSA keys.
722 */
723 rsakeys = newtree234(cmpkeys);
724
725 /*
726 * Process the command line and add RSA keys as listed on it.
5c58ad2d 727 */
728 {
8c42f28c 729 char *p;
730 int inquotes = 0;
731 p = cmdline;
dacbd0e8 732 while (*p) {
733 while (*p && isspace(*p)) p++;
734 if (*p && !isspace(*p)) {
8c42f28c 735 char *q = p, *pp = p;
736 while (*p && (inquotes || !isspace(*p)))
737 {
738 if (*p == '"') {
739 inquotes = !inquotes;
740 p++;
741 continue;
742 }
743 *pp++ = *p++;
744 }
745 if (*pp) {
746 if (*p) p++;
747 *pp++ = '\0';
748 }
dacbd0e8 749 add_keyfile(q);
750 }
751 }
5c58ad2d 752 }
753
754 /*
dacbd0e8 755 * Main message loop.
5c58ad2d 756 */
5c58ad2d 757 while (GetMessage(&msg, NULL, 0, 0) == 1) {
758 TranslateMessage(&msg);
759 DispatchMessage(&msg);
760 }
761
762 /* Clean up the system tray icon */
763 {
764 NOTIFYICONDATA tnid;
765
766 tnid.cbSize = sizeof(NOTIFYICONDATA);
767 tnid.hWnd = hwnd;
768 tnid.uID = 1;
769
770 Shell_NotifyIcon(NIM_DELETE, &tnid);
771
772 DestroyMenu(systray_menu);
773 }
774
016ef8ab 775 if (advapi) FreeLibrary(advapi);
5c58ad2d 776 exit(msg.wParam);
777}