1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
/*
* Windows support module which deals with being a named-pipe server.
*/
#include <stdio.h>
#include <assert.h>
#include "tree234.h"
#include "putty.h"
#include "network.h"
#include "proxy/proxy.h"
#include "ssh.h"
#include "security-api.h"
typedef struct NamedPipeServerSocket {
/* Parameters for (repeated) creation of named pipe objects */
PSECURITY_DESCRIPTOR psd;
PACL acl;
char *pipename;
/* The current named pipe object + attempt to connect to it */
HANDLE pipehandle;
OVERLAPPED connect_ovl;
HandleWait *callback_handle; /* handle-wait.c's reference */
/* PuTTY Socket machinery */
Plug *plug;
char *error;
Socket sock;
} NamedPipeServerSocket;
static Plug *sk_namedpipeserver_plug(Socket *s, Plug *p)
{
NamedPipeServerSocket *ps = container_of(s, NamedPipeServerSocket, sock);
Plug *ret = ps->plug;
if (p)
ps->plug = p;
return ret;
}
static void sk_namedpipeserver_close(Socket *s)
{
NamedPipeServerSocket *ps = container_of(s, NamedPipeServerSocket, sock);
if (ps->callback_handle)
delete_handle_wait(ps->callback_handle);
CloseHandle(ps->pipehandle);
CloseHandle(ps->connect_ovl.hEvent);
sfree(ps->error);
sfree(ps->pipename);
if (ps->acl)
LocalFree(ps->acl);
if (ps->psd)
LocalFree(ps->psd);
sfree(ps);
}
static const char *sk_namedpipeserver_socket_error(Socket *s)
{
NamedPipeServerSocket *ps = container_of(s, NamedPipeServerSocket, sock);
return ps->error;
}
static SocketPeerInfo *sk_namedpipeserver_peer_info(Socket *s)
{
return NULL;
}
static bool create_named_pipe(NamedPipeServerSocket *ps, bool first_instance)
{
SECURITY_ATTRIBUTES sa;
memset(&sa, 0, sizeof(sa));
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = ps->psd;
sa.bInheritHandle = false;
ps->pipehandle = CreateNamedPipe(
/* lpName */
ps->pipename,
/* dwOpenMode */
PIPE_ACCESS_DUPLEX |
FILE_FLAG_OVERLAPPED |
(first_instance ? FILE_FLAG_FIRST_PIPE_INSTANCE : 0),
/* dwPipeMode */
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT
#ifdef PIPE_REJECT_REMOTE_CLIENTS
| PIPE_REJECT_REMOTE_CLIENTS
#endif
,
/* nMaxInstances */
PIPE_UNLIMITED_INSTANCES,
/* nOutBufferSize, nInBufferSize */
4096, 4096, /* FIXME: think harder about buffer sizes? */
/* nDefaultTimeOut */
0 /* default timeout */,
/* lpSecurityAttributes */
&sa);
return ps->pipehandle != INVALID_HANDLE_VALUE;
}
static Socket *named_pipe_accept(accept_ctx_t ctx, Plug *plug)
{
HANDLE conn = (HANDLE)ctx.p;
return make_handle_socket(conn, conn, NULL, NULL, 0, plug, true);
}
static void named_pipe_accept_loop(NamedPipeServerSocket *ps,
bool got_one_already)
{
while (1) {
int error;
char *errmsg;
if (got_one_already) {
/* If we were called with a connection already waiting,
* skip this step. */
got_one_already = false;
error = 0;
} else {
/*
* Call ConnectNamedPipe, which might succeed or might
* tell us that an overlapped operation is in progress and
* we should wait for our event object.
*/
if (ConnectNamedPipe(ps->pipehandle, &ps->connect_ovl))
error = 0;
else
error = GetLastError();
if (error == ERROR_IO_PENDING)
return;
}
if (error == 0 || error == ERROR_PIPE_CONNECTED) {
/*
* We've successfully retrieved an incoming connection, so
* ps->pipehandle now refers to that connection. So
* convert that handle into a separate connection-type
* Socket, and create a fresh one to be the new listening
* pipe.
*/
HANDLE conn = ps->pipehandle;
accept_ctx_t actx;
actx.p = (void *)conn;
if (plug_accepting(ps->plug, named_pipe_accept, actx)) {
/*
* If the plug didn't want the connection, might as
* well close this handle.
*/
CloseHandle(conn);
}
if (!create_named_pipe(ps, false)) {
error = GetLastError();
} else {
/*
* Go round again to see if more connections can be
* got, or to begin waiting on the event object.
*/
continue;
}
}
errmsg = dupprintf("Error while listening to named pipe: %s",
win_strerror(error));
plug_log(ps->plug, 1, sk_namedpipe_addr(ps->pipename), 0,
errmsg, error);
sfree(errmsg);
break;
}
}
static void named_pipe_connect_callback(void *vps)
{
NamedPipeServerSocket *ps = (NamedPipeServerSocket *)vps;
named_pipe_accept_loop(ps, true);
}
/*
* This socket type is only used for listening, so it should never
* be asked to write or set_frozen.
*/
static const SocketVtable NamedPipeServerSocket_sockvt = {
.plug = sk_namedpipeserver_plug,
.close = sk_namedpipeserver_close,
.socket_error = sk_namedpipeserver_socket_error,
.peer_info = sk_namedpipeserver_peer_info,
};
Socket *new_named_pipe_listener(const char *pipename, Plug *plug)
{
NamedPipeServerSocket *ret = snew(NamedPipeServerSocket);
ret->sock.vt = &NamedPipeServerSocket_sockvt;
ret->plug = plug;
ret->error = NULL;
ret->psd = NULL;
ret->pipename = dupstr(pipename);
ret->acl = NULL;
ret->callback_handle = NULL;
assert(strncmp(pipename, "\\\\.\\pipe\\", 9) == 0);
assert(strchr(pipename + 9, '\\') == NULL);
if (!make_private_security_descriptor(GENERIC_READ | GENERIC_WRITE,
&ret->psd, &ret->acl, &ret->error)) {
goto cleanup;
}
if (!create_named_pipe(ret, true)) {
ret->error = dupprintf("unable to create named pipe '%s': %s",
pipename, win_strerror(GetLastError()));
goto cleanup;
}
memset(&ret->connect_ovl, 0, sizeof(ret->connect_ovl));
ret->connect_ovl.hEvent = CreateEvent(NULL, true, false, NULL);
ret->callback_handle = add_handle_wait(
ret->connect_ovl.hEvent, named_pipe_connect_callback, ret);
named_pipe_accept_loop(ret, false);
cleanup:
return &ret->sock;
}
|