The WinSock library is now loaded at run-time, which means we can
[u/mdw/putty] / pageantc.c
1 /*
2 * Pageant client code.
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include "putty.h"
9
10 #define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
11 #define AGENT_MAX_MSGLEN 8192
12
13 #define GET_32BIT(cp) \
14 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
15 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
16 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
17 ((unsigned long)(unsigned char)(cp)[3]))
18
19 int agent_exists(void)
20 {
21 HWND hwnd;
22 hwnd = FindWindow("Pageant", "Pageant");
23 if (!hwnd)
24 return FALSE;
25 else
26 return TRUE;
27 }
28
29 struct agent_query_data {
30 COPYDATASTRUCT cds;
31 unsigned char *mapping;
32 HANDLE handle;
33 char *mapname;
34 HWND hwnd;
35 void (*callback)(void *, void *, int);
36 void *callback_ctx;
37 };
38
39 DWORD WINAPI agent_query_thread(LPVOID param)
40 {
41 struct agent_query_data *data = (struct agent_query_data *)param;
42 unsigned char *ret;
43 int id, retlen;
44
45 id = SendMessage(data->hwnd, WM_COPYDATA, (WPARAM) NULL,
46 (LPARAM) &data->cds);
47 ret = NULL;
48 if (id > 0) {
49 retlen = 4 + GET_32BIT(data->mapping);
50 ret = snewn(retlen, unsigned char);
51 if (ret) {
52 memcpy(ret, data->mapping, retlen);
53 }
54 }
55 if (!ret)
56 retlen = 0;
57 UnmapViewOfFile(data->mapping);
58 CloseHandle(data->handle);
59 sfree(data->mapname);
60
61 agent_schedule_callback(data->callback, data->callback_ctx, ret, retlen);
62
63 return 0;
64 }
65
66 int agent_query(void *in, int inlen, void **out, int *outlen,
67 void (*callback)(void *, void *, int), void *callback_ctx)
68 {
69 HWND hwnd;
70 char *mapname;
71 HANDLE filemap;
72 unsigned char *p, *ret;
73 int id, retlen;
74 COPYDATASTRUCT cds;
75
76 *out = NULL;
77 *outlen = 0;
78
79 hwnd = FindWindow("Pageant", "Pageant");
80 if (!hwnd)
81 return 1; /* *out == NULL, so failure */
82 mapname = dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId());
83 filemap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
84 0, AGENT_MAX_MSGLEN, mapname);
85 if (!filemap)
86 return 1; /* *out == NULL, so failure */
87 p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
88 memcpy(p, in, inlen);
89 cds.dwData = AGENT_COPYDATA_ID;
90 cds.cbData = 1 + strlen(mapname);
91 cds.lpData = mapname;
92 if (callback != NULL && !(flags & FLAG_SYNCAGENT)) {
93 /*
94 * We need an asynchronous Pageant request. Since I know of
95 * no way to stop SendMessage from blocking the thread it's
96 * called in, I see no option but to start a fresh thread.
97 * When we're done we'll PostMessage the result back to our
98 * main window, so that the callback is done in the primary
99 * thread to avoid concurrency.
100 */
101 struct agent_query_data *data = snew(struct agent_query_data);
102 DWORD threadid;
103 data->mapping = p;
104 data->handle = filemap;
105 data->mapname = mapname;
106 data->callback = callback;
107 data->callback_ctx = callback_ctx;
108 data->cds = cds; /* structure copy */
109 data->hwnd = hwnd;
110 if (CreateThread(NULL, 0, agent_query_thread, data, 0, &threadid))
111 return 0;
112 sfree(data);
113 }
114
115 /*
116 * The user either passed a null callback (indicating that the
117 * query is required to be synchronous) or CreateThread failed.
118 * Either way, we need a synchronous request.
119 */
120 id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) & cds);
121 if (id > 0) {
122 retlen = 4 + GET_32BIT(p);
123 ret = snewn(retlen, unsigned char);
124 if (ret) {
125 memcpy(ret, p, retlen);
126 *out = ret;
127 *outlen = retlen;
128 }
129 }
130 UnmapViewOfFile(p);
131 CloseHandle(filemap);
132 return 1;
133 }