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