14 #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
15 #define AGENT_MAX_MSGLEN 8192
17 int agent_exists(void)
20 hwnd = FindWindow("Pageant", "Pageant");
28 * Unfortunately, this asynchronous agent request mechanism doesn't
29 * appear to work terribly well. I'm going to comment it out for
30 * the moment, and see if I can come up with a better one :-/
32 #ifdef WINDOWS_ASYNC_AGENT
34 struct agent_query_data {
36 unsigned char *mapping;
40 void (*callback)(void *, void *, int);
44 DWORD WINAPI agent_query_thread(LPVOID param)
46 struct agent_query_data *data = (struct agent_query_data *)param;
50 id = SendMessage(data->hwnd, WM_COPYDATA, (WPARAM) NULL,
54 retlen = 4 + GET_32BIT(data->mapping);
55 ret = snewn(retlen, unsigned char);
57 memcpy(ret, data->mapping, retlen);
62 UnmapViewOfFile(data->mapping);
63 CloseHandle(data->handle);
66 agent_schedule_callback(data->callback, data->callback_ctx, ret, retlen);
73 int agent_query(void *in, int inlen, void **out, int *outlen,
74 void (*callback)(void *, void *, int), void *callback_ctx)
79 unsigned char *p, *ret;
82 SECURITY_ATTRIBUTES sa, *psa;
83 PSECURITY_DESCRIPTOR psd = NULL;
89 hwnd = FindWindow("Pageant", "Pageant");
91 return 1; /* *out == NULL, so failure */
92 mapname = dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId());
98 * Make the file mapping we create for communication with
99 * Pageant owned by the user SID rather than the default. This
100 * should make communication between processes with slightly
101 * different contexts more reliable: in particular, command
102 * prompts launched as administrator should still be able to
103 * run PSFTPs which refer back to the owning user's
104 * unprivileged Pageant.
106 usersid = get_user_sid();
109 psd = (PSECURITY_DESCRIPTOR)
110 LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
112 if (p_InitializeSecurityDescriptor
113 (psd, SECURITY_DESCRIPTOR_REVISION) &&
114 p_SetSecurityDescriptorOwner(psd, usersid, FALSE)) {
115 sa.nLength = sizeof(sa);
116 sa.bInheritHandle = TRUE;
117 sa.lpSecurityDescriptor = psd;
126 #endif /* NO_SECURITY */
128 filemap = CreateFileMapping(INVALID_HANDLE_VALUE, psa, PAGE_READWRITE,
129 0, AGENT_MAX_MSGLEN, mapname);
130 if (filemap == NULL || filemap == INVALID_HANDLE_VALUE) {
132 return 1; /* *out == NULL, so failure */
134 p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
135 memcpy(p, in, inlen);
136 cds.dwData = AGENT_COPYDATA_ID;
137 cds.cbData = 1 + strlen(mapname);
138 cds.lpData = mapname;
139 #ifdef WINDOWS_ASYNC_AGENT
140 if (callback != NULL && !(flags & FLAG_SYNCAGENT)) {
142 * We need an asynchronous Pageant request. Since I know of
143 * no way to stop SendMessage from blocking the thread it's
144 * called in, I see no option but to start a fresh thread.
145 * When we're done we'll PostMessage the result back to our
146 * main window, so that the callback is done in the primary
147 * thread to avoid concurrency.
149 struct agent_query_data *data = snew(struct agent_query_data);
152 data->handle = filemap;
153 data->mapname = mapname;
154 data->callback = callback;
155 data->callback_ctx = callback_ctx;
156 data->cds = cds; /* structure copy */
158 if (CreateThread(NULL, 0, agent_query_thread, data, 0, &threadid))
166 * The user either passed a null callback (indicating that the
167 * query is required to be synchronous) or CreateThread failed.
168 * Either way, we need a synchronous request.
170 id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) &cds);
172 retlen = 4 + GET_32BIT(p);
173 ret = snewn(retlen, unsigned char);
175 memcpy(ret, p, retlen);
181 CloseHandle(filemap);