2013-11-17 14:04:01 +00:00
|
|
|
/*
|
|
|
|
* Windows support module which deals with being a named-pipe client.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "tree234.h"
|
|
|
|
#include "putty.h"
|
|
|
|
#include "network.h"
|
|
|
|
#include "proxy.h"
|
|
|
|
#include "ssh.h"
|
|
|
|
|
|
|
|
#if !defined NO_SECURITY
|
|
|
|
|
2013-11-17 14:05:29 +00:00
|
|
|
#include "winsecur.h"
|
2013-11-17 14:04:01 +00:00
|
|
|
|
Get rid of lots of implicit pointer types.
All the main backend structures - Ssh, Telnet, Pty, Serial etc - now
describe structure types themselves rather than pointers to them. The
same goes for the codebase-wide trait types Socket and Plug, and the
supporting types SockAddr and Pinger.
All those things that were typedefed as pointers are older types; the
newer ones have the explicit * at the point of use, because that's
what I now seem to be preferring. But whichever one of those is
better, inconsistently using a mixture of the two styles is worse, so
let's make everything consistent.
A few types are still implicitly pointers, such as Bignum and some of
the GSSAPI types; generally this is either because they have to be
void *, or because they're typedefed differently on different
platforms and aren't always pointers at all. Can't be helped. But I've
got rid of the main ones, at least.
2018-10-04 18:10:23 +00:00
|
|
|
Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
|
|
|
|
Plug *plug, int overlapped);
|
2013-11-17 14:04:01 +00:00
|
|
|
|
Get rid of lots of implicit pointer types.
All the main backend structures - Ssh, Telnet, Pty, Serial etc - now
describe structure types themselves rather than pointers to them. The
same goes for the codebase-wide trait types Socket and Plug, and the
supporting types SockAddr and Pinger.
All those things that were typedefed as pointers are older types; the
newer ones have the explicit * at the point of use, because that's
what I now seem to be preferring. But whichever one of those is
better, inconsistently using a mixture of the two styles is worse, so
let's make everything consistent.
A few types are still implicitly pointers, such as Bignum and some of
the GSSAPI types; generally this is either because they have to be
void *, or because they're typedefed differently on different
platforms and aren't always pointers at all. Can't be helped. But I've
got rid of the main ones, at least.
2018-10-04 18:10:23 +00:00
|
|
|
Socket *new_named_pipe_client(const char *pipename, Plug *plug)
|
2013-11-17 14:04:01 +00:00
|
|
|
{
|
|
|
|
HANDLE pipehandle;
|
|
|
|
PSID usersid, pipeowner;
|
|
|
|
PSECURITY_DESCRIPTOR psd;
|
|
|
|
char *err;
|
Get rid of lots of implicit pointer types.
All the main backend structures - Ssh, Telnet, Pty, Serial etc - now
describe structure types themselves rather than pointers to them. The
same goes for the codebase-wide trait types Socket and Plug, and the
supporting types SockAddr and Pinger.
All those things that were typedefed as pointers are older types; the
newer ones have the explicit * at the point of use, because that's
what I now seem to be preferring. But whichever one of those is
better, inconsistently using a mixture of the two styles is worse, so
let's make everything consistent.
A few types are still implicitly pointers, such as Bignum and some of
the GSSAPI types; generally this is either because they have to be
void *, or because they're typedefed differently on different
platforms and aren't always pointers at all. Can't be helped. But I've
got rid of the main ones, at least.
2018-10-04 18:10:23 +00:00
|
|
|
Socket *ret;
|
2013-11-17 14:04:01 +00:00
|
|
|
|
|
|
|
assert(strncmp(pipename, "\\\\.\\pipe\\", 9) == 0);
|
|
|
|
assert(strchr(pipename + 9, '\\') == NULL);
|
|
|
|
|
2013-11-17 14:05:41 +00:00
|
|
|
while (1) {
|
|
|
|
pipehandle = CreateFile(pipename, GENERIC_READ | GENERIC_WRITE,
|
|
|
|
0, NULL, OPEN_EXISTING,
|
|
|
|
FILE_FLAG_OVERLAPPED, NULL);
|
2013-11-17 14:04:01 +00:00
|
|
|
|
2013-11-17 14:05:41 +00:00
|
|
|
if (pipehandle != INVALID_HANDLE_VALUE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (GetLastError() != ERROR_PIPE_BUSY) {
|
|
|
|
err = dupprintf("Unable to open named pipe '%s': %s",
|
|
|
|
pipename, win_strerror(GetLastError()));
|
|
|
|
ret = new_error_socket(err, plug);
|
|
|
|
sfree(err);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we got ERROR_PIPE_BUSY, wait for the server to
|
|
|
|
* create a new pipe instance. (Since the server is
|
|
|
|
* expected to be winnps.c, which will do that immediately
|
|
|
|
* after a previous connection is accepted, that shouldn't
|
|
|
|
* take excessively long.)
|
|
|
|
*/
|
|
|
|
if (!WaitNamedPipe(pipename, NMPWAIT_USE_DEFAULT_WAIT)) {
|
|
|
|
err = dupprintf("Error waiting for named pipe '%s': %s",
|
|
|
|
pipename, win_strerror(GetLastError()));
|
|
|
|
ret = new_error_socket(err, plug);
|
|
|
|
sfree(err);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-11-17 14:04:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((usersid = get_user_sid()) == NULL) {
|
|
|
|
CloseHandle(pipehandle);
|
|
|
|
err = dupprintf("Unable to get user SID");
|
|
|
|
ret = new_error_socket(err, plug);
|
|
|
|
sfree(err);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-11-17 14:05:41 +00:00
|
|
|
if (p_GetSecurityInfo(pipehandle, SE_KERNEL_OBJECT,
|
|
|
|
OWNER_SECURITY_INFORMATION,
|
|
|
|
&pipeowner, NULL, NULL, NULL,
|
|
|
|
&psd) != ERROR_SUCCESS) {
|
2013-11-17 14:04:01 +00:00
|
|
|
err = dupprintf("Unable to get named pipe security information: %s",
|
|
|
|
win_strerror(GetLastError()));
|
|
|
|
ret = new_error_socket(err, plug);
|
|
|
|
sfree(err);
|
|
|
|
CloseHandle(pipehandle);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EqualSid(pipeowner, usersid)) {
|
|
|
|
err = dupprintf("Owner of named pipe '%s' is not us", pipename);
|
|
|
|
ret = new_error_socket(err, plug);
|
|
|
|
sfree(err);
|
|
|
|
CloseHandle(pipehandle);
|
|
|
|
LocalFree(psd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalFree(psd);
|
|
|
|
|
2015-11-22 11:50:37 +00:00
|
|
|
return make_handle_socket(pipehandle, pipehandle, NULL, plug, TRUE);
|
2013-11-17 14:04:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !defined NO_SECURITY */
|