}
}
+/*
+ * This is a can't-happen stub, since Pageant never makes
+ * asynchronous agent requests.
+ */
+void agent_schedule_callback(void (*callback)(void *, void *, int),
+ void *callback_ctx, void *data, int len)
+{
+ assert(!"We shouldn't get here");
+}
+
void cleanup_exit(int code) { exit(code); }
+int flags = FLAG_SYNCAGENT;
+
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
{
WNDCLASS wndclass;
#include <stdio.h>
#include <stdlib.h>
-#include "puttymem.h"
+#include "putty.h"
#define AGENT_COPYDATA_ID 0x804e50ba /* random goop */
#define AGENT_MAX_MSGLEN 8192
-#ifdef TESTMODE
-#define debug(x) (printf x)
-#else
-#define debug(x)
-#endif
-
#define GET_32BIT(cp) \
(((unsigned long)(unsigned char)(cp)[0] << 24) | \
((unsigned long)(unsigned char)(cp)[1] << 16) | \
return TRUE;
}
+struct agent_query_data {
+ COPYDATASTRUCT cds;
+ unsigned char *mapping;
+ HANDLE handle;
+ char *mapname;
+ HWND hwnd;
+ void (*callback)(void *, void *, int);
+ void *callback_ctx;
+};
+
+DWORD WINAPI agent_query_thread(LPVOID param)
+{
+ struct agent_query_data *data = (struct agent_query_data *)param;
+ unsigned char *ret;
+ int id, retlen;
+
+ id = SendMessage(data->hwnd, WM_COPYDATA, (WPARAM) NULL,
+ (LPARAM) &data->cds);
+ ret = NULL;
+ if (id > 0) {
+ retlen = 4 + GET_32BIT(data->mapping);
+ ret = snewn(retlen, unsigned char);
+ if (ret) {
+ memcpy(ret, data->mapping, retlen);
+ }
+ }
+ if (!ret)
+ retlen = 0;
+ UnmapViewOfFile(data->mapping);
+ CloseHandle(data->handle);
+ sfree(data->mapname);
+
+ agent_schedule_callback(data->callback, data->callback_ctx, ret, retlen);
+
+ return 0;
+}
+
int agent_query(void *in, int inlen, void **out, int *outlen,
void (*callback)(void *, void *, int), void *callback_ctx)
{
HWND hwnd;
- char mapname[64];
+ char *mapname;
HANDLE filemap;
unsigned char *p, *ret;
int id, retlen;
*outlen = 0;
hwnd = FindWindow("Pageant", "Pageant");
- debug(("hwnd is %p\n", hwnd));
if (!hwnd)
return 1; /* *out == NULL, so failure */
- sprintf(mapname, "PageantRequest%08x", (unsigned)GetCurrentThreadId());
+ mapname = dupprintf("PageantRequest%08x", (unsigned)GetCurrentThreadId());
filemap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
0, AGENT_MAX_MSGLEN, mapname);
if (!filemap)
cds.dwData = AGENT_COPYDATA_ID;
cds.cbData = 1 + strlen(mapname);
cds.lpData = mapname;
+ if (callback != NULL && !(flags & FLAG_SYNCAGENT)) {
+ /*
+ * We need an asynchronous Pageant request. Since I know of
+ * no way to stop SendMessage from blocking the thread it's
+ * called in, I see no option but to start a fresh thread.
+ * When we're done we'll PostMessage the result back to our
+ * main window, so that the callback is done in the primary
+ * thread to avoid concurrency.
+ */
+ struct agent_query_data *data = snew(struct agent_query_data);
+ DWORD threadid;
+ data->mapping = p;
+ data->handle = filemap;
+ data->mapname = mapname;
+ data->callback = callback;
+ data->callback_ctx = callback_ctx;
+ data->cds = cds; /* structure copy */
+ data->hwnd = hwnd;
+ if (CreateThread(NULL, 0, agent_query_thread, data, 0, &threadid))
+ return 0;
+ sfree(data);
+ }
+
+ /*
+ * The user either passed a null callback (indicating that the
+ * query is required to be synchronous) or CreateThread failed.
+ * Either way, we need a synchronous request.
+ */
id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) & cds);
- debug(("return is %d\n", id));
if (id > 0) {
retlen = 4 + GET_32BIT(p);
- debug(("len is %d\n", retlen));
ret = snewn(retlen, unsigned char);
if (ret) {
memcpy(ret, p, retlen);
}
UnmapViewOfFile(p);
CloseHandle(filemap);
-
return 1;
}
-
-#ifdef TESTMODE
-
-int main(void)
-{
- void *msg;
- int len;
- int i;
-
- agent_query("\0\0\0\1\1", 5, &msg, &len);
- debug(("%d:", len));
- for (i = 0; i < len; i++)
- debug((" %02x", ((unsigned char *) msg)[i]));
- debug(("\n"));
- return 0;
-}
-
-#endif
#include "storage.h"
#include "tree234.h"
+#define WM_AGENT_CALLBACK (WM_XUSER + 4)
+
#define MAX_STDIN_BACKLOG 4096
+struct agent_callback {
+ void (*callback)(void *, void *, int);
+ void *callback_ctx;
+ void *data;
+ int len;
+};
+
void fatalbox(char *p, ...)
{
va_list ap;
return osize + esize;
}
+static DWORD main_thread_id;
+
+void agent_schedule_callback(void (*callback)(void *, void *, int),
+ void *callback_ctx, void *data, int len)
+{
+ struct agent_callback *c = snew(struct agent_callback);
+ c->callback = callback;
+ c->callback_ctx = callback_ctx;
+ c->data = data;
+ c->len = len;
+ PostThreadMessage(main_thread_id, WM_AGENT_CALLBACK, 0, (LPARAM)c);
+}
+
/*
* Short description of parameters.
*/
GetConsoleMode(inhandle, &orig_console_mode);
SetConsoleMode(inhandle, ENABLE_PROCESSED_INPUT);
+ main_thread_id = GetCurrentThreadId();
+
/*
* Turn off ECHO and LINE input modes. We don't care if this
* call fails, because we know we aren't necessarily running in
sending = TRUE;
}
- n = WaitForMultipleObjects(4, handles, FALSE, INFINITE);
+ n = MsgWaitForMultipleObjects(4, handles, FALSE, INFINITE,
+ QS_POSTMESSAGE);
if (n == 0) {
WSANETWORKEVENTS things;
SOCKET socket;
back->unthrottle(backhandle, bufchain_size(&stdout_data) +
bufchain_size(&stderr_data));
}
+ } else if (n == 4) {
+ MSG msg;
+ while (PeekMessage(&msg, INVALID_HANDLE_VALUE,
+ WM_AGENT_CALLBACK, WM_AGENT_CALLBACK,
+ PM_REMOVE)) {
+ struct agent_callback *c = (struct agent_callback *)msg.lParam;
+ c->callback(c->callback_ctx, c->data, c->len);
+ sfree(c);
+ }
}
if (!reading && back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) {
SetEvent(idata.eventback);
extern int select_result(WPARAM, LPARAM);
/*
+ * In psftp, all agent requests should be synchronous, so this is a
+ * never-called stub.
+ */
+void agent_schedule_callback(void (*callback)(void *, void *, int),
+ void *callback_ctx, void *data, int len)
+{
+ assert(!"We shouldn't be here");
+}
+
+/*
* Receive a block of data from the SSH link. Block until all data
* is available.
*
char *batchfile = NULL;
int errors = 0;
- flags = FLAG_STDERR | FLAG_INTERACTIVE;
+ flags = FLAG_STDERR | FLAG_INTERACTIVE | FLAG_SYNCAGENT;
cmdline_tooltype = TOOLTYPE_FILETRANSFER;
ssh_get_line = &console_get_line;
init_winsock();
* These flags describe the type of _application_ - they wouldn't
* vary between individual sessions - and so it's OK to have this
* variable be GLOBAL.
+ *
+ * Note that additional flags may be defined in platform-specific
+ * headers. It's probably best if those ones start from 0x1000, to
+ * avoid collision.
*/
#define FLAG_VERBOSE 0x0001
#define FLAG_STDERR 0x0002
extern int select_result(WPARAM, LPARAM);
/*
+ * In pscp, all agent requests should be synchronous, so this is a
+ * never-called stub.
+ */
+void agent_schedule_callback(void (*callback)(void *, void *, int),
+ void *callback_ctx, void *data, int len)
+{
+ assert(!"We shouldn't be here");
+}
+
+/*
* Receive a block of data from the SSH link. Block until all data
* is available.
*
default_protocol = PROT_TELNET;
- flags = FLAG_STDERR;
+ flags = FLAG_STDERR | FLAG_SYNCAGENT;
cmdline_tooltype = TOOLTYPE_FILETRANSFER;
ssh_get_line = &console_get_line;
init_winsock();
#define WM_IGNORE_CLIP (WM_XUSER + 2)
#define WM_FULLSCR_ON_MAX (WM_XUSER + 3)
+#define WM_AGENT_CALLBACK (WM_XUSER + 4)
/* Needed for Chinese support and apparently not always defined. */
#ifndef VK_PROCESSKEY
extern struct sesslist sesslist; /* imported from windlg.c */
+struct agent_callback {
+ void (*callback)(void *, void *, int);
+ void *callback_ctx;
+ void *data;
+ int len;
+};
+
#define FONT_NORMAL 0
#define FONT_BOLD 1
#define FONT_UNDERLINE 2
SetCursor(LoadCursor(NULL, IDC_ARROW));
return TRUE;
}
+ break;
+ case WM_AGENT_CALLBACK:
+ {
+ struct agent_callback *c = (struct agent_callback *)lParam;
+ c->callback(c->callback_ctx, c->data, c->len);
+ sfree(c);
+ }
+ return 0;
default:
if (message == wm_mousewheel || message == WM_MOUSEWHEEL) {
int shift_pressed=0, control_pressed=0;
{
return term_data(term, is_stderr, data, len);
}
+
+void agent_schedule_callback(void (*callback)(void *, void *, int),
+ void *callback_ctx, void *data, int len)
+{
+ struct agent_callback *c = snew(struct agent_callback);
+ c->callback = callback;
+ c->callback_ctx = callback_ctx;
+ c->data = data;
+ c->len = len;
+ PostMessage(hwnd, WM_AGENT_CALLBACK, 0, (LPARAM)c);
+}
struct unicode_data;
void init_ucs(Config *, struct unicode_data *);
+/*
+ * pageantc.c needs to schedule callbacks for asynchronous agent
+ * requests. This has to be done differently in GUI and console, so
+ * there's an exported function used for the purpose.
+ *
+ * Also, we supply FLAG_SYNCAGENT to force agent requests to be
+ * synchronous in pscp and psftp.
+ */
+void agent_schedule_callback(void (*callback)(void *, void *, int),
+ void *callback_ctx, void *data, int len);
+#define FLAG_SYNCAGENT 0x1000
+
#endif