]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winpgntc.c
6e085e424471e62c398fad515a09f6879a17b0f2
[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 int agent_query(void *in, int inlen, void **out, int *outlen,
28                 void (*callback)(void *, void *, int), void *callback_ctx)
29 {
30     HWND hwnd;
31     char *mapname;
32     HANDLE filemap;
33     unsigned char *p, *ret;
34     int id, retlen;
35     COPYDATASTRUCT cds;
36     SECURITY_ATTRIBUTES sa, *psa;
37     PSECURITY_DESCRIPTOR psd = NULL;
38     PSID usersid = NULL;
39
40     *out = NULL;
41     *outlen = 0;
42
43     hwnd = FindWindow("Pageant", "Pageant");
44     if (!hwnd)
45         return 1;                      /* *out == NULL, so failure */
46     mapname = dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId());
47
48     psa = NULL;
49 #ifndef NO_SECURITY
50     if (got_advapi()) {
51         /*
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.
59          */
60         usersid = get_user_sid();
61
62         if (usersid) {
63             psd = (PSECURITY_DESCRIPTOR)
64                 LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
65             if (psd) {
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;
72                     psa = &sa;
73                 } else {
74                     LocalFree(psd);
75                     psd = NULL;
76                 }
77             }
78         }
79     }
80 #endif /* NO_SECURITY */
81
82     filemap = CreateFileMapping(INVALID_HANDLE_VALUE, psa, PAGE_READWRITE,
83                                 0, AGENT_MAX_MSGLEN, mapname);
84     if (filemap == NULL || filemap == INVALID_HANDLE_VALUE) {
85         sfree(mapname);
86         return 1;                      /* *out == NULL, so failure */
87     }
88     p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
89     memcpy(p, in, inlen);
90     cds.dwData = AGENT_COPYDATA_ID;
91     cds.cbData = 1 + strlen(mapname);
92     cds.lpData = mapname;
93
94     /*
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.
98      */
99     id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) &cds);
100     if (id > 0) {
101         retlen = 4 + GET_32BIT(p);
102         ret = snewn(retlen, unsigned char);
103         if (ret) {
104             memcpy(ret, p, retlen);
105             *out = ret;
106             *outlen = retlen;
107         }
108     }
109     UnmapViewOfFile(p);
110     CloseHandle(filemap);
111     sfree(mapname);
112     if (psd)
113         LocalFree(psd);
114     return 1;
115 }