| 1 | /* |
| 2 | * Pageant client code. |
| 3 | */ |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | |
| 8 | #include "putty.h" |
| 9 | |
| 10 | #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */ |
| 11 | #define AGENT_MAX_MSGLEN 8192 |
| 12 | |
| 13 | int agent_exists(void) |
| 14 | { |
| 15 | HWND hwnd; |
| 16 | hwnd = FindWindow("Pageant", "Pageant"); |
| 17 | if (!hwnd) |
| 18 | return FALSE; |
| 19 | else |
| 20 | return TRUE; |
| 21 | } |
| 22 | |
| 23 | /* |
| 24 | * Unfortunately, this asynchronous agent request mechanism doesn't |
| 25 | * appear to work terribly well. I'm going to comment it out for |
| 26 | * the moment, and see if I can come up with a better one :-/ |
| 27 | */ |
| 28 | #ifdef WINDOWS_ASYNC_AGENT |
| 29 | |
| 30 | struct agent_query_data { |
| 31 | COPYDATASTRUCT cds; |
| 32 | unsigned char *mapping; |
| 33 | HANDLE handle; |
| 34 | char *mapname; |
| 35 | HWND hwnd; |
| 36 | void (*callback)(void *, void *, int); |
| 37 | void *callback_ctx; |
| 38 | }; |
| 39 | |
| 40 | DWORD WINAPI agent_query_thread(LPVOID param) |
| 41 | { |
| 42 | struct agent_query_data *data = (struct agent_query_data *)param; |
| 43 | unsigned char *ret; |
| 44 | int id, retlen; |
| 45 | |
| 46 | id = SendMessage(data->hwnd, WM_COPYDATA, (WPARAM) NULL, |
| 47 | (LPARAM) &data->cds); |
| 48 | ret = NULL; |
| 49 | if (id > 0) { |
| 50 | retlen = 4 + GET_32BIT(data->mapping); |
| 51 | ret = snewn(retlen, unsigned char); |
| 52 | if (ret) { |
| 53 | memcpy(ret, data->mapping, retlen); |
| 54 | } |
| 55 | } |
| 56 | if (!ret) |
| 57 | retlen = 0; |
| 58 | UnmapViewOfFile(data->mapping); |
| 59 | CloseHandle(data->handle); |
| 60 | sfree(data->mapname); |
| 61 | |
| 62 | agent_schedule_callback(data->callback, data->callback_ctx, ret, retlen); |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | #endif |
| 68 | |
| 69 | int agent_query(void *in, int inlen, void **out, int *outlen, |
| 70 | void (*callback)(void *, void *, int), void *callback_ctx) |
| 71 | { |
| 72 | HWND hwnd; |
| 73 | char *mapname; |
| 74 | HANDLE filemap; |
| 75 | unsigned char *p, *ret; |
| 76 | int id, retlen; |
| 77 | COPYDATASTRUCT cds; |
| 78 | |
| 79 | *out = NULL; |
| 80 | *outlen = 0; |
| 81 | |
| 82 | hwnd = FindWindow("Pageant", "Pageant"); |
| 83 | if (!hwnd) |
| 84 | return 1; /* *out == NULL, so failure */ |
| 85 | mapname = dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId()); |
| 86 | filemap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, |
| 87 | 0, AGENT_MAX_MSGLEN, mapname); |
| 88 | if (filemap == NULL || filemap == INVALID_HANDLE_VALUE) |
| 89 | return 1; /* *out == NULL, so failure */ |
| 90 | p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0); |
| 91 | memcpy(p, in, inlen); |
| 92 | cds.dwData = AGENT_COPYDATA_ID; |
| 93 | cds.cbData = 1 + strlen(mapname); |
| 94 | cds.lpData = mapname; |
| 95 | #ifdef WINDOWS_ASYNC_AGENT |
| 96 | if (callback != NULL && !(flags & FLAG_SYNCAGENT)) { |
| 97 | /* |
| 98 | * We need an asynchronous Pageant request. Since I know of |
| 99 | * no way to stop SendMessage from blocking the thread it's |
| 100 | * called in, I see no option but to start a fresh thread. |
| 101 | * When we're done we'll PostMessage the result back to our |
| 102 | * main window, so that the callback is done in the primary |
| 103 | * thread to avoid concurrency. |
| 104 | */ |
| 105 | struct agent_query_data *data = snew(struct agent_query_data); |
| 106 | DWORD threadid; |
| 107 | data->mapping = p; |
| 108 | data->handle = filemap; |
| 109 | data->mapname = mapname; |
| 110 | data->callback = callback; |
| 111 | data->callback_ctx = callback_ctx; |
| 112 | data->cds = cds; /* structure copy */ |
| 113 | data->hwnd = hwnd; |
| 114 | if (CreateThread(NULL, 0, agent_query_thread, data, 0, &threadid)) |
| 115 | return 0; |
| 116 | sfree(data); |
| 117 | } |
| 118 | #endif |
| 119 | |
| 120 | /* |
| 121 | * The user either passed a null callback (indicating that the |
| 122 | * query is required to be synchronous) or CreateThread failed. |
| 123 | * Either way, we need a synchronous request. |
| 124 | */ |
| 125 | id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) &cds); |
| 126 | if (id > 0) { |
| 127 | retlen = 4 + GET_32BIT(p); |
| 128 | ret = snewn(retlen, unsigned char); |
| 129 | if (ret) { |
| 130 | memcpy(ret, p, retlen); |
| 131 | *out = ret; |
| 132 | *outlen = retlen; |
| 133 | } |
| 134 | } |
| 135 | UnmapViewOfFile(p); |
| 136 | CloseHandle(filemap); |
| 137 | return 1; |
| 138 | } |