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