]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winnpc.c
9b347dd3c22120bbd40bb2a64d6b59de19d2451f
[PuTTY.git] / windows / winnpc.c
1 /*
2  * Windows support module which deals with being a named-pipe client.
3  */
4
5 #include <stdio.h>
6 #include <assert.h>
7
8 #define DEFINE_PLUG_METHOD_MACROS
9 #include "tree234.h"
10 #include "putty.h"
11 #include "network.h"
12 #include "proxy.h"
13 #include "ssh.h"
14
15 #if !defined NO_SECURITY
16
17 #include <aclapi.h>
18
19 Socket make_handle_socket(HANDLE send_H, HANDLE recv_H, Plug plug,
20                           int overlapped);
21
22 Socket new_named_pipe_client(const char *pipename, Plug plug)
23 {
24     HANDLE pipehandle;
25     PSID usersid, pipeowner;
26     PSECURITY_DESCRIPTOR psd;
27     char *err;
28     Socket ret;
29
30     extern int advapi_initialised;
31     init_advapi();           /* for get_user_sid. FIXME: do better. */
32
33     assert(strncmp(pipename, "\\\\.\\pipe\\", 9) == 0);
34     assert(strchr(pipename + 9, '\\') == NULL);
35
36     pipehandle = CreateFile(pipename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
37                             OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
38
39     if (pipehandle == INVALID_HANDLE_VALUE) {
40         err = dupprintf("Unable to open named pipe '%s': %s",
41                         pipename, win_strerror(GetLastError()));
42         ret = new_error_socket(err, plug);
43         sfree(err);
44         return ret;
45     }
46
47     if ((usersid = get_user_sid()) == NULL) {
48         CloseHandle(pipehandle);
49         err = dupprintf("Unable to get user SID");
50         ret = new_error_socket(err, plug);
51         sfree(err);
52         return ret;
53     }
54
55     if (GetSecurityInfo(pipehandle, SE_KERNEL_OBJECT,
56                         OWNER_SECURITY_INFORMATION,
57                         &pipeowner, NULL, NULL, NULL,
58                         &psd) != ERROR_SUCCESS) {
59         err = dupprintf("Unable to get named pipe security information: %s",
60                         win_strerror(GetLastError()));
61         ret = new_error_socket(err, plug);
62         sfree(err);
63         CloseHandle(pipehandle);
64         sfree(usersid);
65         return ret;
66     }
67
68     if (!EqualSid(pipeowner, usersid)) {
69         err = dupprintf("Owner of named pipe '%s' is not us", pipename);
70         ret = new_error_socket(err, plug);
71         sfree(err);
72         CloseHandle(pipehandle);
73         LocalFree(psd);
74         sfree(usersid);
75         return ret;
76     }
77
78     LocalFree(psd);
79     sfree(usersid);
80
81     return make_handle_socket(pipehandle, pipehandle, plug, TRUE);
82 }
83
84 #endif /* !defined NO_SECURITY */