| 1 | /* |
| 2 | * Pageant: the PuTTY Authentication Agent. |
| 3 | */ |
| 4 | |
| 5 | #include <windows.h> |
| 6 | #include <stdio.h> /* FIXME */ |
| 7 | #include "putty.h" /* FIXME */ |
| 8 | #include "ssh.h" |
| 9 | #include "tree234.h" |
| 10 | |
| 11 | #define IDI_MAINICON 200 |
| 12 | #define IDI_TRAYICON 201 |
| 13 | |
| 14 | #define WM_XUSER (WM_USER + 0x2000) |
| 15 | #define WM_SYSTRAY (WM_XUSER + 6) |
| 16 | #define WM_SYSTRAY2 (WM_XUSER + 7) |
| 17 | #define WM_CLOSEMEM (WM_XUSER + 10) |
| 18 | |
| 19 | #define IDM_CLOSE 0x0010 |
| 20 | #define IDM_VIEWKEYS 0x0020 |
| 21 | |
| 22 | #define APPNAME "Pageant" |
| 23 | |
| 24 | #define SSH_AGENTC_REQUEST_RSA_IDENTITIES 1 |
| 25 | #define SSH_AGENT_RSA_IDENTITIES_ANSWER 2 |
| 26 | #define SSH_AGENTC_RSA_CHALLENGE 3 |
| 27 | #define SSH_AGENT_RSA_RESPONSE 4 |
| 28 | #define SSH_AGENT_FAILURE 5 |
| 29 | #define SSH_AGENT_SUCCESS 6 |
| 30 | #define SSH_AGENTC_ADD_RSA_IDENTITY 7 |
| 31 | #define SSH_AGENTC_REMOVE_RSA_IDENTITY 8 |
| 32 | |
| 33 | HINSTANCE instance; |
| 34 | HWND hwnd; |
| 35 | HWND keylist; |
| 36 | HMENU systray_menu; |
| 37 | |
| 38 | tree234 *rsakeys; |
| 39 | |
| 40 | /* |
| 41 | * We need this to link with the RSA code, because rsaencrypt() |
| 42 | * pads its data with random bytes. Since we only use rsadecrypt(), |
| 43 | * which is deterministic, this should never be called. |
| 44 | * |
| 45 | * If it _is_ called, there is a _serious_ problem, because it |
| 46 | * won't generate true random numbers. So we must scream, panic, |
| 47 | * and exit immediately if that should happen. |
| 48 | */ |
| 49 | int random_byte(void) { |
| 50 | MessageBox(hwnd, "Internal Error", APPNAME, MB_OK | MB_ICONERROR); |
| 51 | exit(0); |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * This function is needed to link with the DES code. We need not |
| 56 | * have it do anything at all. |
| 57 | */ |
| 58 | void logevent(char *msg) { |
| 59 | } |
| 60 | |
| 61 | #define GET_32BIT(cp) \ |
| 62 | (((unsigned long)(unsigned char)(cp)[0] << 24) | \ |
| 63 | ((unsigned long)(unsigned char)(cp)[1] << 16) | \ |
| 64 | ((unsigned long)(unsigned char)(cp)[2] << 8) | \ |
| 65 | ((unsigned long)(unsigned char)(cp)[3])) |
| 66 | |
| 67 | #define PUT_32BIT(cp, value) { \ |
| 68 | (cp)[0] = (unsigned char)((value) >> 24); \ |
| 69 | (cp)[1] = (unsigned char)((value) >> 16); \ |
| 70 | (cp)[2] = (unsigned char)((value) >> 8); \ |
| 71 | (cp)[3] = (unsigned char)(value); } |
| 72 | |
| 73 | #define PASSPHRASE_MAXLEN 512 |
| 74 | |
| 75 | /* |
| 76 | * Dialog-box function for the passphrase box. |
| 77 | */ |
| 78 | static int CALLBACK PassphraseProc(HWND hwnd, UINT msg, |
| 79 | WPARAM wParam, LPARAM lParam) { |
| 80 | static char *passphrase; |
| 81 | |
| 82 | switch (msg) { |
| 83 | case WM_INITDIALOG: |
| 84 | passphrase = (char *)lParam; |
| 85 | *passphrase = 0; |
| 86 | return 0; |
| 87 | case WM_COMMAND: |
| 88 | switch (LOWORD(wParam)) { |
| 89 | case IDOK: |
| 90 | if (*passphrase) |
| 91 | EndDialog (hwnd, 1); |
| 92 | else |
| 93 | MessageBeep (0); |
| 94 | return 0; |
| 95 | case IDCANCEL: |
| 96 | EndDialog (hwnd, 0); |
| 97 | return 0; |
| 98 | case 102: /* edit box */ |
| 99 | if (HIWORD(wParam) == EN_CHANGE) { |
| 100 | GetDlgItemText (hwnd, 102, passphrase, PASSPHRASE_MAXLEN-1); |
| 101 | passphrase[PASSPHRASE_MAXLEN-1] = '\0'; |
| 102 | } |
| 103 | return 0; |
| 104 | } |
| 105 | return 0; |
| 106 | case WM_CLOSE: |
| 107 | EndDialog (hwnd, 0); |
| 108 | return 0; |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * This function loads a key from a file and adds it. |
| 115 | */ |
| 116 | void add_keyfile(char *filename) { |
| 117 | char passphrase[PASSPHRASE_MAXLEN]; |
| 118 | struct RSAKey *key; |
| 119 | int needs_pass; |
| 120 | int ret; |
| 121 | int attempts; |
| 122 | |
| 123 | needs_pass = rsakey_encrypted(filename); |
| 124 | attempts = 0; |
| 125 | key = malloc(sizeof(*key)); |
| 126 | do { |
| 127 | if (needs_pass) { |
| 128 | int dlgret; |
| 129 | dlgret = DialogBoxParam(instance, MAKEINTRESOURCE(210), |
| 130 | NULL, PassphraseProc, |
| 131 | (LPARAM)passphrase); |
| 132 | if (!dlgret) { |
| 133 | free(key); |
| 134 | return; /* operation cancelled */ |
| 135 | } |
| 136 | } else |
| 137 | *passphrase = '\0'; |
| 138 | ret = loadrsakey(filename, key, passphrase); |
| 139 | attempts++; |
| 140 | } while (ret == -1); |
| 141 | if (ret == 0) { |
| 142 | MessageBox(NULL, "Couldn't load public key.", APPNAME, |
| 143 | MB_OK | MB_ICONERROR); |
| 144 | free(key); |
| 145 | return; |
| 146 | } |
| 147 | if (add234(rsakeys, key) != key) |
| 148 | free(key); /* already present, don't waste RAM */ |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * This is the main agent function that answers messages. |
| 153 | */ |
| 154 | void answer_msg(void *in, int inlen, void **out, int *outlen) { |
| 155 | unsigned char *ret; |
| 156 | unsigned char *p = in; |
| 157 | int type; |
| 158 | |
| 159 | *out = NULL; /* default `no go' response */ |
| 160 | |
| 161 | /* |
| 162 | * Basic sanity checks. len >= 5, and len[0:4] holds len-4. |
| 163 | */ |
| 164 | if (inlen < 5 || GET_32BIT(p) != (unsigned long)(inlen-4)) |
| 165 | return; |
| 166 | |
| 167 | /* |
| 168 | * Get the message type. |
| 169 | */ |
| 170 | type = p[4]; |
| 171 | |
| 172 | p += 5; |
| 173 | |
| 174 | switch (type) { |
| 175 | case SSH_AGENTC_REQUEST_RSA_IDENTITIES: |
| 176 | /* |
| 177 | * Reply with SSH_AGENT_RSA_IDENTITIES_ANSWER. |
| 178 | */ |
| 179 | { |
| 180 | enum234 e; |
| 181 | struct RSAKey *key; |
| 182 | int len, nkeys; |
| 183 | |
| 184 | /* |
| 185 | * Count up the number and length of keys we hold. |
| 186 | */ |
| 187 | len = nkeys = 0; |
| 188 | for (key = first234(rsakeys, &e); key; key = next234(&e)) { |
| 189 | nkeys++; |
| 190 | len += 4; /* length field */ |
| 191 | len += ssh1_bignum_length(key->exponent); |
| 192 | len += ssh1_bignum_length(key->modulus); |
| 193 | len += 4 + strlen(key->comment); |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Packet header is the obvious five bytes, plus four |
| 198 | * bytes for the key count. |
| 199 | */ |
| 200 | len += 5 + 4; |
| 201 | if ((ret = malloc(len)) != NULL) { |
| 202 | PUT_32BIT(ret, len-4); |
| 203 | ret[4] = SSH_AGENT_RSA_IDENTITIES_ANSWER; |
| 204 | PUT_32BIT(ret+5, nkeys); |
| 205 | p = ret + 5 + 4; |
| 206 | for (key = first234(rsakeys, &e); key; key = next234(&e)) { |
| 207 | PUT_32BIT(p, ssh1_bignum_bitcount(key->modulus)); |
| 208 | p += 4; |
| 209 | p += ssh1_write_bignum(p, key->exponent); |
| 210 | p += ssh1_write_bignum(p, key->modulus); |
| 211 | PUT_32BIT(p, strlen(key->comment)); |
| 212 | memcpy(p+4, key->comment, strlen(key->comment)); |
| 213 | p += 4 + strlen(key->comment); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | break; |
| 218 | case SSH_AGENTC_RSA_CHALLENGE: |
| 219 | /* |
| 220 | * Reply with either SSH_AGENT_RSA_RESPONSE or |
| 221 | * SSH_AGENT_FAILURE, depending on whether we have that key |
| 222 | * or not. |
| 223 | */ |
| 224 | { |
| 225 | struct RSAKey reqkey, *key; |
| 226 | Bignum challenge, response; |
| 227 | unsigned char response_source[48], response_md5[16]; |
| 228 | struct MD5Context md5c; |
| 229 | int i, len; |
| 230 | |
| 231 | p += 4; |
| 232 | p += ssh1_read_bignum(p, &reqkey.exponent); |
| 233 | p += ssh1_read_bignum(p, &reqkey.modulus); |
| 234 | p += ssh1_read_bignum(p, &challenge); |
| 235 | memcpy(response_source+32, p, 16); p += 16; |
| 236 | if (GET_32BIT(p) != 1 || |
| 237 | (key = find234(rsakeys, &reqkey, NULL)) == NULL) { |
| 238 | freebn(reqkey.exponent); |
| 239 | freebn(reqkey.modulus); |
| 240 | freebn(challenge); |
| 241 | goto failure; |
| 242 | } |
| 243 | response = rsadecrypt(challenge, key); |
| 244 | for (i = 0; i < 32; i++) |
| 245 | response_source[i] = bignum_byte(response, 31-i); |
| 246 | |
| 247 | MD5Init(&md5c); |
| 248 | MD5Update(&md5c, response_source, 48); |
| 249 | MD5Final(response_md5, &md5c); |
| 250 | memset(response_source, 0, 48); /* burn the evidence */ |
| 251 | freebn(response); /* and that evidence */ |
| 252 | freebn(challenge); /* yes, and that evidence */ |
| 253 | freebn(reqkey.exponent); /* and free some memory ... */ |
| 254 | freebn(reqkey.modulus); /* ... while we're at it. */ |
| 255 | |
| 256 | /* |
| 257 | * Packet is the obvious five byte header, plus sixteen |
| 258 | * bytes of MD5. |
| 259 | */ |
| 260 | len = 5 + 16; |
| 261 | if ((ret = malloc(len)) != NULL) { |
| 262 | PUT_32BIT(ret, len-4); |
| 263 | ret[4] = SSH_AGENT_RSA_RESPONSE; |
| 264 | memcpy(ret+5, response_md5, 16); |
| 265 | } |
| 266 | } |
| 267 | break; |
| 268 | #if 0 /* FIXME: implement these */ |
| 269 | case SSH_AGENTC_ADD_RSA_IDENTITY: |
| 270 | /* |
| 271 | * Add to the list and return SSH_AGENT_SUCCESS, or |
| 272 | * SSH_AGENT_FAILURE if the key was malformed. |
| 273 | */ |
| 274 | break; |
| 275 | case SSH_AGENTC_REMOVE_RSA_IDENTITY: |
| 276 | /* |
| 277 | * Remove from the list and return SSH_AGENT_SUCCESS, or |
| 278 | * perhaps SSH_AGENT_FAILURE if it wasn't in the list to |
| 279 | * start with. |
| 280 | */ |
| 281 | break; |
| 282 | #endif |
| 283 | default: |
| 284 | failure: |
| 285 | /* |
| 286 | * Unrecognised message. Return SSH_AGENT_FAILURE. |
| 287 | */ |
| 288 | if ((ret = malloc(5)) != NULL) { |
| 289 | PUT_32BIT(ret, 1); |
| 290 | ret[4] = SSH_AGENT_FAILURE; |
| 291 | } |
| 292 | break; |
| 293 | } |
| 294 | |
| 295 | if (ret) { |
| 296 | *out = ret; |
| 297 | *outlen = 4 + GET_32BIT(ret); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Key comparison function for the 2-3-4 tree of RSA keys. |
| 303 | */ |
| 304 | int cmpkeys(void *av, void *bv) { |
| 305 | struct RSAKey *a = (struct RSAKey *)av; |
| 306 | struct RSAKey *b = (struct RSAKey *)bv; |
| 307 | Bignum am, bm; |
| 308 | int alen, blen; |
| 309 | |
| 310 | am = a->modulus; |
| 311 | bm = b->modulus; |
| 312 | /* |
| 313 | * Compare by length of moduli. |
| 314 | */ |
| 315 | alen = ssh1_bignum_bitcount(am); |
| 316 | blen = ssh1_bignum_bitcount(bm); |
| 317 | if (alen > blen) return +1; else if (alen < blen) return -1; |
| 318 | /* |
| 319 | * Now compare by moduli themselves. |
| 320 | */ |
| 321 | alen = (alen + 7) / 8; /* byte count */ |
| 322 | while (alen-- > 0) { |
| 323 | int abyte, bbyte; |
| 324 | abyte = bignum_byte(am, alen); |
| 325 | bbyte = bignum_byte(bm, alen); |
| 326 | if (abyte > bbyte) return +1; else if (abyte < bbyte) return -1; |
| 327 | } |
| 328 | /* |
| 329 | * Give up. |
| 330 | */ |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static void error(char *s) { |
| 335 | MessageBox(hwnd, s, APPNAME, MB_OK | MB_ICONERROR); |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Dialog-box function for the key list box. |
| 340 | */ |
| 341 | static int CALLBACK KeyListProc(HWND hwnd, UINT msg, |
| 342 | WPARAM wParam, LPARAM lParam) { |
| 343 | enum234 e; |
| 344 | struct RSAKey *key; |
| 345 | OPENFILENAME of; |
| 346 | char filename[FILENAME_MAX]; |
| 347 | |
| 348 | switch (msg) { |
| 349 | case WM_INITDIALOG: |
| 350 | for (key = first234(rsakeys, &e); key; key = next234(&e)) { |
| 351 | SendDlgItemMessage (hwnd, 100, LB_ADDSTRING, |
| 352 | 0, (LPARAM) key->comment); |
| 353 | } |
| 354 | return 0; |
| 355 | case WM_COMMAND: |
| 356 | switch (LOWORD(wParam)) { |
| 357 | case IDOK: |
| 358 | case IDCANCEL: |
| 359 | keylist = NULL; |
| 360 | DestroyWindow(hwnd); |
| 361 | return 0; |
| 362 | case 101: /* add key */ |
| 363 | if (HIWORD(wParam) == BN_CLICKED || |
| 364 | HIWORD(wParam) == BN_DOUBLECLICKED) { |
| 365 | memset(&of, 0, sizeof(of)); |
| 366 | #ifdef OPENFILENAME_SIZE_VERSION_400 |
| 367 | of.lStructSize = OPENFILENAME_SIZE_VERSION_400; |
| 368 | #else |
| 369 | of.lStructSize = sizeof(of); |
| 370 | #endif |
| 371 | of.hwndOwner = hwnd; |
| 372 | of.lpstrFilter = "All Files\0*\0\0\0"; |
| 373 | of.lpstrCustomFilter = NULL; |
| 374 | of.nFilterIndex = 1; |
| 375 | of.lpstrFile = filename; *filename = '\0'; |
| 376 | of.nMaxFile = sizeof(filename); |
| 377 | of.lpstrFileTitle = NULL; |
| 378 | of.lpstrInitialDir = NULL; |
| 379 | of.lpstrTitle = "Select Public Key File"; |
| 380 | of.Flags = 0; |
| 381 | if (GetOpenFileName(&of)) { |
| 382 | add_keyfile(filename); |
| 383 | } |
| 384 | SendDlgItemMessage(hwnd, 100, LB_RESETCONTENT, 0, 0); |
| 385 | for (key = first234(rsakeys, &e); key; key = next234(&e)) { |
| 386 | SendDlgItemMessage (hwnd, 100, LB_ADDSTRING, |
| 387 | 0, (LPARAM) key->comment); |
| 388 | } |
| 389 | SendDlgItemMessage (hwnd, 100, LB_SETCURSEL, (WPARAM) -1, 0); |
| 390 | } |
| 391 | return 0; |
| 392 | case 102: /* remove key */ |
| 393 | if (HIWORD(wParam) == BN_CLICKED || |
| 394 | HIWORD(wParam) == BN_DOUBLECLICKED) { |
| 395 | int n = SendDlgItemMessage (hwnd, 100, LB_GETCURSEL, 0, 0); |
| 396 | if (n == LB_ERR || n == 0) { |
| 397 | MessageBeep(0); |
| 398 | break; |
| 399 | } |
| 400 | for (key = first234(rsakeys, &e); key; key = next234(&e)) |
| 401 | if (n-- == 0) |
| 402 | break; |
| 403 | del234(rsakeys, key); |
| 404 | freersakey(key); free(key); |
| 405 | SendDlgItemMessage(hwnd, 100, LB_RESETCONTENT, 0, 0); |
| 406 | for (key = first234(rsakeys, &e); key; key = next234(&e)) { |
| 407 | SendDlgItemMessage (hwnd, 100, LB_ADDSTRING, |
| 408 | 0, (LPARAM) key->comment); |
| 409 | } |
| 410 | SendDlgItemMessage (hwnd, 100, LB_SETCURSEL, (WPARAM) -1, 0); |
| 411 | } |
| 412 | return 0; |
| 413 | } |
| 414 | return 0; |
| 415 | case WM_CLOSE: |
| 416 | keylist = NULL; |
| 417 | DestroyWindow(hwnd); |
| 418 | return 0; |
| 419 | } |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static LRESULT CALLBACK WndProc (HWND hwnd, UINT message, |
| 424 | WPARAM wParam, LPARAM lParam) { |
| 425 | int ret; |
| 426 | static int menuinprogress; |
| 427 | |
| 428 | switch (message) { |
| 429 | case WM_SYSTRAY: |
| 430 | if (lParam == WM_RBUTTONUP) { |
| 431 | POINT cursorpos; |
| 432 | GetCursorPos(&cursorpos); |
| 433 | PostMessage(hwnd, WM_SYSTRAY2, cursorpos.x, cursorpos.y); |
| 434 | } |
| 435 | break; |
| 436 | case WM_SYSTRAY2: |
| 437 | if (!menuinprogress) { |
| 438 | menuinprogress = 1; |
| 439 | SetForegroundWindow(hwnd); |
| 440 | ret = TrackPopupMenu(systray_menu, |
| 441 | TPM_RIGHTALIGN | TPM_BOTTOMALIGN | |
| 442 | TPM_RIGHTBUTTON, |
| 443 | wParam, lParam, 0, hwnd, NULL); |
| 444 | menuinprogress = 0; |
| 445 | } |
| 446 | break; |
| 447 | case WM_COMMAND: |
| 448 | case WM_SYSCOMMAND: |
| 449 | switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */ |
| 450 | case IDM_CLOSE: |
| 451 | SendMessage(hwnd, WM_CLOSE, 0, 0); |
| 452 | break; |
| 453 | case IDM_VIEWKEYS: |
| 454 | if (!keylist) { |
| 455 | keylist = CreateDialog (instance, MAKEINTRESOURCE(211), |
| 456 | NULL, KeyListProc); |
| 457 | ShowWindow (keylist, SW_SHOWNORMAL); |
| 458 | } |
| 459 | break; |
| 460 | } |
| 461 | break; |
| 462 | case WM_DESTROY: |
| 463 | PostQuitMessage (0); |
| 464 | return 0; |
| 465 | case WM_COPYDATA: |
| 466 | { |
| 467 | COPYDATASTRUCT *cds; |
| 468 | void *in, *out, *ret; |
| 469 | int inlen, outlen; |
| 470 | HANDLE filemap; |
| 471 | char mapname[64]; |
| 472 | int id; |
| 473 | |
| 474 | cds = (COPYDATASTRUCT *)lParam; |
| 475 | /* |
| 476 | * FIXME: use dwData somehow. |
| 477 | */ |
| 478 | in = cds->lpData; |
| 479 | inlen = cds->cbData; |
| 480 | answer_msg(in, inlen, &out, &outlen); |
| 481 | if (out) { |
| 482 | id = 0; |
| 483 | do { |
| 484 | sprintf(mapname, "PageantReply%08x", ++id); |
| 485 | filemap = CreateFileMapping(INVALID_HANDLE_VALUE, |
| 486 | NULL, PAGE_READWRITE, |
| 487 | 0, outlen+sizeof(int), |
| 488 | mapname); |
| 489 | } while (filemap == INVALID_HANDLE_VALUE); |
| 490 | ret = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, |
| 491 | outlen+sizeof(int)); |
| 492 | if (ret) { |
| 493 | *((int *)ret) = outlen; |
| 494 | memcpy(((int *)ret)+1, out, outlen); |
| 495 | UnmapViewOfFile(ret); |
| 496 | return id; |
| 497 | } |
| 498 | } else |
| 499 | return 0; /* invalid request */ |
| 500 | } |
| 501 | break; |
| 502 | case WM_CLOSEMEM: |
| 503 | /* |
| 504 | * FIXME! |
| 505 | */ |
| 506 | break; |
| 507 | } |
| 508 | |
| 509 | return DefWindowProc (hwnd, message, wParam, lParam); |
| 510 | } |
| 511 | |
| 512 | int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) { |
| 513 | WNDCLASS wndclass; |
| 514 | MSG msg; |
| 515 | |
| 516 | instance = inst; |
| 517 | |
| 518 | if (!prev) { |
| 519 | wndclass.style = 0; |
| 520 | wndclass.lpfnWndProc = WndProc; |
| 521 | wndclass.cbClsExtra = 0; |
| 522 | wndclass.cbWndExtra = 0; |
| 523 | wndclass.hInstance = inst; |
| 524 | wndclass.hIcon = LoadIcon (inst, |
| 525 | MAKEINTRESOURCE(IDI_MAINICON)); |
| 526 | wndclass.hCursor = LoadCursor (NULL, IDC_IBEAM); |
| 527 | wndclass.hbrBackground = GetStockObject (BLACK_BRUSH); |
| 528 | wndclass.lpszMenuName = NULL; |
| 529 | wndclass.lpszClassName = APPNAME; |
| 530 | |
| 531 | RegisterClass (&wndclass); |
| 532 | } |
| 533 | |
| 534 | hwnd = keylist = NULL; |
| 535 | |
| 536 | hwnd = CreateWindow (APPNAME, APPNAME, |
| 537 | WS_OVERLAPPEDWINDOW | WS_VSCROLL, |
| 538 | CW_USEDEFAULT, CW_USEDEFAULT, |
| 539 | 100, 100, NULL, NULL, inst, NULL); |
| 540 | |
| 541 | /* Set up a system tray icon */ |
| 542 | { |
| 543 | BOOL res; |
| 544 | NOTIFYICONDATA tnid; |
| 545 | HICON hicon; |
| 546 | |
| 547 | #ifdef NIM_SETVERSION |
| 548 | tnid.uVersion = 0; |
| 549 | res = Shell_NotifyIcon(NIM_SETVERSION, &tnid); |
| 550 | #endif |
| 551 | |
| 552 | tnid.cbSize = sizeof(NOTIFYICONDATA); |
| 553 | tnid.hWnd = hwnd; |
| 554 | tnid.uID = 1; /* unique within this systray use */ |
| 555 | tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; |
| 556 | tnid.uCallbackMessage = WM_SYSTRAY; |
| 557 | tnid.hIcon = hicon = LoadIcon (instance, MAKEINTRESOURCE(201)); |
| 558 | strcpy(tnid.szTip, "Pageant (PuTTY authentication agent)"); |
| 559 | |
| 560 | res = Shell_NotifyIcon(NIM_ADD, &tnid); |
| 561 | |
| 562 | if (hicon) |
| 563 | DestroyIcon(hicon); |
| 564 | |
| 565 | systray_menu = CreatePopupMenu(); |
| 566 | AppendMenu (systray_menu, MF_ENABLED, IDM_VIEWKEYS, "View Keys"); |
| 567 | AppendMenu (systray_menu, MF_ENABLED, IDM_CLOSE, "Terminate"); |
| 568 | } |
| 569 | |
| 570 | ShowWindow (hwnd, SW_HIDE); |
| 571 | |
| 572 | /* |
| 573 | * Initialise storage for RSA keys. |
| 574 | */ |
| 575 | rsakeys = newtree234(cmpkeys); |
| 576 | |
| 577 | /* |
| 578 | * Process the command line and add RSA keys as listed on it. |
| 579 | * FIXME: we don't support spaces in filenames here. We should. |
| 580 | */ |
| 581 | { |
| 582 | char *p = cmdline; |
| 583 | while (*p) { |
| 584 | while (*p && isspace(*p)) p++; |
| 585 | if (*p && !isspace(*p)) { |
| 586 | char *q = p; |
| 587 | while (*p && !isspace(*p)) p++; |
| 588 | if (*p) *p++ = '\0'; |
| 589 | add_keyfile(q); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Main message loop. |
| 596 | */ |
| 597 | while (GetMessage(&msg, NULL, 0, 0) == 1) { |
| 598 | TranslateMessage(&msg); |
| 599 | DispatchMessage(&msg); |
| 600 | } |
| 601 | |
| 602 | /* Clean up the system tray icon */ |
| 603 | { |
| 604 | NOTIFYICONDATA tnid; |
| 605 | |
| 606 | tnid.cbSize = sizeof(NOTIFYICONDATA); |
| 607 | tnid.hWnd = hwnd; |
| 608 | tnid.uID = 1; |
| 609 | |
| 610 | Shell_NotifyIcon(NIM_DELETE, &tnid); |
| 611 | |
| 612 | DestroyMenu(systray_menu); |
| 613 | } |
| 614 | |
| 615 | exit(msg.wParam); |
| 616 | } |