5c58ad2d |
1 | /* |
2 | * Pageant client code. |
3 | */ |
4 | |
5 | #include <windows.h> |
6 | #include <stdio.h> |
7 | #include <stdlib.h> |
8 | |
c44bf5bd |
9 | #include "putty.h" |
dcbde236 |
10 | |
d70f60ae |
11 | #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */ |
12 | #define AGENT_MAX_MSGLEN 8192 |
13 | |
d70f60ae |
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 | |
32874aea |
20 | int agent_exists(void) |
21 | { |
5c58ad2d |
22 | HWND hwnd; |
23 | hwnd = FindWindow("Pageant", "Pageant"); |
24 | if (!hwnd) |
32874aea |
25 | return FALSE; |
5c58ad2d |
26 | else |
32874aea |
27 | return TRUE; |
5c58ad2d |
28 | } |
29 | |
c44bf5bd |
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 | |
839f10db |
67 | int agent_query(void *in, int inlen, void **out, int *outlen, |
68 | void (*callback)(void *, void *, int), void *callback_ctx) |
32874aea |
69 | { |
5c58ad2d |
70 | HWND hwnd; |
c44bf5bd |
71 | char *mapname; |
5c58ad2d |
72 | HANDLE filemap; |
d70f60ae |
73 | unsigned char *p, *ret; |
5c58ad2d |
74 | int id, retlen; |
75 | COPYDATASTRUCT cds; |
76 | |
77 | *out = NULL; |
78 | *outlen = 0; |
79 | |
80 | hwnd = FindWindow("Pageant", "Pageant"); |
5c58ad2d |
81 | if (!hwnd) |
839f10db |
82 | return 1; /* *out == NULL, so failure */ |
c44bf5bd |
83 | mapname = dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId()); |
d70f60ae |
84 | filemap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, |
32874aea |
85 | 0, AGENT_MAX_MSGLEN, mapname); |
d70f60ae |
86 | if (!filemap) |
839f10db |
87 | return 1; /* *out == NULL, so failure */ |
d70f60ae |
88 | p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0); |
89 | memcpy(p, in, inlen); |
90 | cds.dwData = AGENT_COPYDATA_ID; |
32874aea |
91 | cds.cbData = 1 + strlen(mapname); |
d70f60ae |
92 | cds.lpData = mapname; |
c44bf5bd |
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 | */ |
32874aea |
121 | id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) & cds); |
5c58ad2d |
122 | if (id > 0) { |
32874aea |
123 | retlen = 4 + GET_32BIT(p); |
3d88e64d |
124 | ret = snewn(retlen, unsigned char); |
32874aea |
125 | if (ret) { |
126 | memcpy(ret, p, retlen); |
127 | *out = ret; |
128 | *outlen = retlen; |
129 | } |
5c58ad2d |
130 | } |
d70f60ae |
131 | UnmapViewOfFile(p); |
132 | CloseHandle(filemap); |
839f10db |
133 | return 1; |
5c58ad2d |
134 | } |