]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winpgntc.c
af2abb6fb5a667025e33ebd003bfa1de4b68a065
[PuTTY.git] / windows / winpgntc.c
1 /*
2  * Pageant client code.
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include "putty.h"
9
10 #ifndef NO_SECURITY
11 #include "winsecur.h"
12 #endif
13
14 #define AGENT_COPYDATA_ID 0x804e50ba   /* random goop */
15 #define AGENT_MAX_MSGLEN  8192
16
17 int agent_exists(void)
18 {
19     HWND hwnd;
20     hwnd = FindWindow("Pageant", "Pageant");
21     if (!hwnd)
22         return FALSE;
23     else
24         return TRUE;
25 }
26
27 void agent_cancel_query(agent_pending_query *q)
28 {
29     assert(0 && "Windows agent queries are never asynchronous!");
30 }
31
32 agent_pending_query *agent_query(
33     void *in, int inlen, void **out, int *outlen,
34     void (*callback)(void *, void *, int), void *callback_ctx)
35 {
36     HWND hwnd;
37     char *mapname;
38     HANDLE filemap;
39     unsigned char *p, *ret;
40     int id, retlen;
41     COPYDATASTRUCT cds;
42     SECURITY_ATTRIBUTES sa, *psa;
43     PSECURITY_DESCRIPTOR psd = NULL;
44     PSID usersid = NULL;
45
46     *out = NULL;
47     *outlen = 0;
48
49     hwnd = FindWindow("Pageant", "Pageant");
50     if (!hwnd)
51         return NULL;                   /* *out == NULL, so failure */
52     mapname = dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId());
53
54     psa = NULL;
55 #ifndef NO_SECURITY
56     if (got_advapi()) {
57         /*
58          * Make the file mapping we create for communication with
59          * Pageant owned by the user SID rather than the default. This
60          * should make communication between processes with slightly
61          * different contexts more reliable: in particular, command
62          * prompts launched as administrator should still be able to
63          * run PSFTPs which refer back to the owning user's
64          * unprivileged Pageant.
65          */
66         usersid = get_user_sid();
67
68         if (usersid) {
69             psd = (PSECURITY_DESCRIPTOR)
70                 LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
71             if (psd) {
72                 if (p_InitializeSecurityDescriptor
73                     (psd, SECURITY_DESCRIPTOR_REVISION) &&
74                     p_SetSecurityDescriptorOwner(psd, usersid, FALSE)) {
75                     sa.nLength = sizeof(sa);
76                     sa.bInheritHandle = TRUE;
77                     sa.lpSecurityDescriptor = psd;
78                     psa = &sa;
79                 } else {
80                     LocalFree(psd);
81                     psd = NULL;
82                 }
83             }
84         }
85     }
86 #endif /* NO_SECURITY */
87
88     filemap = CreateFileMapping(INVALID_HANDLE_VALUE, psa, PAGE_READWRITE,
89                                 0, AGENT_MAX_MSGLEN, mapname);
90     if (filemap == NULL || filemap == INVALID_HANDLE_VALUE) {
91         sfree(mapname);
92         return NULL;                   /* *out == NULL, so failure */
93     }
94     p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
95     memcpy(p, in, inlen);
96     cds.dwData = AGENT_COPYDATA_ID;
97     cds.cbData = 1 + strlen(mapname);
98     cds.lpData = mapname;
99
100     /*
101      * The user either passed a null callback (indicating that the
102      * query is required to be synchronous) or CreateThread failed.
103      * Either way, we need a synchronous request.
104      */
105     id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) &cds);
106     if (id > 0) {
107         retlen = 4 + GET_32BIT(p);
108         ret = snewn(retlen, unsigned char);
109         if (ret) {
110             memcpy(ret, p, retlen);
111             *out = ret;
112             *outlen = retlen;
113         }
114     }
115     UnmapViewOfFile(p);
116     CloseHandle(filemap);
117     sfree(mapname);
118     if (psd)
119         LocalFree(psd);
120     return NULL;
121 }