The WinSock library is now loaded at run-time, which means we can
[u/mdw/putty] / pageantc.c
CommitLineData
5c58ad2d 1/*
2 * Pageant client code.
3 */
4
5c58ad2d 5#include <stdio.h>
6#include <stdlib.h>
7
c44bf5bd 8#include "putty.h"
dcbde236 9
d70f60ae 10#define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
11#define AGENT_MAX_MSGLEN 8192
12
d70f60ae 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
32874aea 19int agent_exists(void)
20{
5c58ad2d 21 HWND hwnd;
22 hwnd = FindWindow("Pageant", "Pageant");
23 if (!hwnd)
32874aea 24 return FALSE;
5c58ad2d 25 else
32874aea 26 return TRUE;
5c58ad2d 27}
28
c44bf5bd 29struct 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
39DWORD 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
839f10db 66int agent_query(void *in, int inlen, void **out, int *outlen,
67 void (*callback)(void *, void *, int), void *callback_ctx)
32874aea 68{
5c58ad2d 69 HWND hwnd;
c44bf5bd 70 char *mapname;
5c58ad2d 71 HANDLE filemap;
d70f60ae 72 unsigned char *p, *ret;
5c58ad2d 73 int id, retlen;
74 COPYDATASTRUCT cds;
75
76 *out = NULL;
77 *outlen = 0;
78
79 hwnd = FindWindow("Pageant", "Pageant");
5c58ad2d 80 if (!hwnd)
839f10db 81 return 1; /* *out == NULL, so failure */
c44bf5bd 82 mapname = dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId());
d70f60ae 83 filemap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
32874aea 84 0, AGENT_MAX_MSGLEN, mapname);
d70f60ae 85 if (!filemap)
839f10db 86 return 1; /* *out == NULL, so failure */
d70f60ae 87 p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0);
88 memcpy(p, in, inlen);
89 cds.dwData = AGENT_COPYDATA_ID;
32874aea 90 cds.cbData = 1 + strlen(mapname);
d70f60ae 91 cds.lpData = mapname;
c44bf5bd 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 */
32874aea 120 id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) & cds);
5c58ad2d 121 if (id > 0) {
32874aea 122 retlen = 4 + GET_32BIT(p);
3d88e64d 123 ret = snewn(retlen, unsigned char);
32874aea 124 if (ret) {
125 memcpy(ret, p, retlen);
126 *out = ret;
127 *outlen = retlen;
128 }
5c58ad2d 129 }
d70f60ae 130 UnmapViewOfFile(p);
131 CloseHandle(filemap);
839f10db 132 return 1;
5c58ad2d 133}