| 1 | /* |
| 2 | * Pageant client code. |
| 3 | */ |
| 4 | |
| 5 | #include <windows.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | |
| 9 | #include "putty.h" |
| 10 | |
| 11 | #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */ |
| 12 | #define AGENT_MAX_MSGLEN 8192 |
| 13 | |
| 14 | #define GET_32BIT(cp) \ |
| 15 | (((unsigned long)(unsigned char)(cp)[0] << 24) | \ |
| 16 | ((unsigned long)(unsigned char)(cp)[1] << 16) | \ |
| 17 | ((unsigned long)(unsigned char)(cp)[2] << 8) | \ |
| 18 | ((unsigned long)(unsigned char)(cp)[3])) |
| 19 | |
| 20 | int agent_exists(void) |
| 21 | { |
| 22 | HWND hwnd; |
| 23 | hwnd = FindWindow("Pageant", "Pageant"); |
| 24 | if (!hwnd) |
| 25 | return FALSE; |
| 26 | else |
| 27 | return TRUE; |
| 28 | } |
| 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 | int agent_query(void *in, int inlen, void **out, int *outlen, |
| 68 | void (*callback)(void *, void *, int), void *callback_ctx) |
| 69 | { |
| 70 | HWND hwnd; |
| 71 | char *mapname; |
| 72 | HANDLE filemap; |
| 73 | unsigned char *p, *ret; |
| 74 | int id, retlen; |
| 75 | COPYDATASTRUCT cds; |
| 76 | |
| 77 | *out = NULL; |
| 78 | *outlen = 0; |
| 79 | |
| 80 | hwnd = FindWindow("Pageant", "Pageant"); |
| 81 | if (!hwnd) |
| 82 | return 1; /* *out == NULL, so failure */ |
| 83 | mapname = dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId()); |
| 84 | filemap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, |
| 85 | 0, AGENT_MAX_MSGLEN, mapname); |
| 86 | if (!filemap) |
| 87 | return 1; /* *out == NULL, so failure */ |
| 88 | p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0); |
| 89 | memcpy(p, in, inlen); |
| 90 | cds.dwData = AGENT_COPYDATA_ID; |
| 91 | cds.cbData = 1 + strlen(mapname); |
| 92 | cds.lpData = mapname; |
| 93 | if (callback != NULL && !(flags & FLAG_SYNCAGENT)) { |
| 94 | /* |
| 95 | * We need an asynchronous Pageant request. Since I know of |
| 96 | * no way to stop SendMessage from blocking the thread it's |
| 97 | * called in, I see no option but to start a fresh thread. |
| 98 | * When we're done we'll PostMessage the result back to our |
| 99 | * main window, so that the callback is done in the primary |
| 100 | * thread to avoid concurrency. |
| 101 | */ |
| 102 | struct agent_query_data *data = snew(struct agent_query_data); |
| 103 | DWORD threadid; |
| 104 | data->mapping = p; |
| 105 | data->handle = filemap; |
| 106 | data->mapname = mapname; |
| 107 | data->callback = callback; |
| 108 | data->callback_ctx = callback_ctx; |
| 109 | data->cds = cds; /* structure copy */ |
| 110 | data->hwnd = hwnd; |
| 111 | if (CreateThread(NULL, 0, agent_query_thread, data, 0, &threadid)) |
| 112 | return 0; |
| 113 | sfree(data); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * The user either passed a null callback (indicating that the |
| 118 | * query is required to be synchronous) or CreateThread failed. |
| 119 | * Either way, we need a synchronous request. |
| 120 | */ |
| 121 | id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) & cds); |
| 122 | if (id > 0) { |
| 123 | retlen = 4 + GET_32BIT(p); |
| 124 | ret = snewn(retlen, unsigned char); |
| 125 | if (ret) { |
| 126 | memcpy(ret, p, retlen); |
| 127 | *out = ret; |
| 128 | *outlen = retlen; |
| 129 | } |
| 130 | } |
| 131 | UnmapViewOfFile(p); |
| 132 | CloseHandle(filemap); |
| 133 | return 1; |
| 134 | } |