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