]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winnpc.c
Move the dynamic loading of advapi into its own module.
[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 "winsecur.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     assert(strncmp(pipename, "\\\\.\\pipe\\", 9) == 0);
31     assert(strchr(pipename + 9, '\\') == NULL);
32
33     pipehandle = CreateFile(pipename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
34                             OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
35
36     if (pipehandle == INVALID_HANDLE_VALUE) {
37         err = dupprintf("Unable to open named pipe '%s': %s",
38                         pipename, win_strerror(GetLastError()));
39         ret = new_error_socket(err, plug);
40         sfree(err);
41         return ret;
42     }
43
44     if ((usersid = get_user_sid()) == NULL) {
45         CloseHandle(pipehandle);
46         err = dupprintf("Unable to get user SID");
47         ret = new_error_socket(err, plug);
48         sfree(err);
49         return ret;
50     }
51
52     if (GetSecurityInfo(pipehandle, SE_KERNEL_OBJECT,
53                         OWNER_SECURITY_INFORMATION,
54                         &pipeowner, NULL, NULL, NULL,
55                         &psd) != ERROR_SUCCESS) {
56         err = dupprintf("Unable to get named pipe security information: %s",
57                         win_strerror(GetLastError()));
58         ret = new_error_socket(err, plug);
59         sfree(err);
60         CloseHandle(pipehandle);
61         sfree(usersid);
62         return ret;
63     }
64
65     if (!EqualSid(pipeowner, usersid)) {
66         err = dupprintf("Owner of named pipe '%s' is not us", pipename);
67         ret = new_error_socket(err, plug);
68         sfree(err);
69         CloseHandle(pipehandle);
70         LocalFree(psd);
71         sfree(usersid);
72         return ret;
73     }
74
75     LocalFree(psd);
76     sfree(usersid);
77
78     return make_handle_socket(pipehandle, pipehandle, plug, TRUE);
79 }
80
81 #endif /* !defined NO_SECURITY */