14 #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
15 #define AGENT_MAX_MSGLEN 8192
17 int agent_exists(void)
20 hwnd = FindWindow("Pageant", "Pageant");
27 int agent_query(void *in, int inlen, void **out, int *outlen,
28 void (*callback)(void *, void *, int), void *callback_ctx)
33 unsigned char *p, *ret;
36 SECURITY_ATTRIBUTES sa, *psa;
37 PSECURITY_DESCRIPTOR psd = NULL;
43 hwnd = FindWindow("Pageant", "Pageant");
45 return 1; /* *out == NULL, so failure */
46 mapname = dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId());
52 * Make the file mapping we create for communication with
53 * Pageant owned by the user SID rather than the default. This
54 * should make communication between processes with slightly
55 * different contexts more reliable: in particular, command
56 * prompts launched as administrator should still be able to
57 * run PSFTPs which refer back to the owning user's
58 * unprivileged Pageant.
60 usersid = get_user_sid();
63 psd = (PSECURITY_DESCRIPTOR)
64 LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
66 if (p_InitializeSecurityDescriptor
67 (psd, SECURITY_DESCRIPTOR_REVISION) &&
68 p_SetSecurityDescriptorOwner(psd, usersid, FALSE)) {
69 sa.nLength = sizeof(sa);
70 sa.bInheritHandle = TRUE;
71 sa.lpSecurityDescriptor = psd;
80 #endif /* NO_SECURITY */
82 filemap = CreateFileMapping(INVALID_HANDLE_VALUE, psa, PAGE_READWRITE,
83 0, AGENT_MAX_MSGLEN, mapname);
84 if (filemap == NULL || filemap == INVALID_HANDLE_VALUE) {
86 return 1; /* *out == NULL, so failure */
88 p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
90 cds.dwData = AGENT_COPYDATA_ID;
91 cds.cbData = 1 + strlen(mapname);
95 * The user either passed a null callback (indicating that the
96 * query is required to be synchronous) or CreateThread failed.
97 * Either way, we need a synchronous request.
99 id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) &cds);
101 retlen = 4 + GET_32BIT(p);
102 ret = snewn(retlen, unsigned char);
104 memcpy(ret, p, retlen);
110 CloseHandle(filemap);