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