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