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