2 * Windows support module which deals with being a named-pipe server.
8 #define DEFINE_PLUG_METHOD_MACROS
15 #if !defined NO_SECURITY
19 Socket make_handle_socket(HANDLE send_H, HANDLE recv_H, Plug plug,
22 typedef struct Socket_named_pipe_server_tag *Named_Pipe_Server_Socket;
23 struct Socket_named_pipe_server_tag {
24 const struct socket_function_table *fn;
25 /* the above variable absolutely *must* be the first in this structure */
27 /* Parameters for (repeated) creation of named pipe objects */
28 PSECURITY_DESCRIPTOR psd;
33 /* The current named pipe object + attempt to connect to it */
35 OVERLAPPED connect_ovl;
37 /* PuTTY Socket machinery */
42 static Plug sk_namedpipeserver_plug(Socket s, Plug p)
44 Named_Pipe_Server_Socket ps = (Named_Pipe_Server_Socket) s;
51 static void sk_namedpipeserver_close(Socket s)
53 Named_Pipe_Server_Socket ps = (Named_Pipe_Server_Socket) s;
55 CloseHandle(ps->pipehandle);
56 CloseHandle(ps->connect_ovl.hEvent);
60 LocalFree(ps->networksid);
68 static const char *sk_namedpipeserver_socket_error(Socket s)
70 Named_Pipe_Server_Socket ps = (Named_Pipe_Server_Socket) s;
74 static int create_named_pipe(Named_Pipe_Server_Socket ps, int first_instance)
76 SECURITY_ATTRIBUTES sa;
78 memset(&sa, 0, sizeof(sa));
79 sa.nLength = sizeof(sa);
80 sa.lpSecurityDescriptor = ps->psd;
81 sa.bInheritHandle = FALSE;
83 ps->pipehandle = CreateNamedPipe
89 FILE_FLAG_OVERLAPPED |
90 (first_instance ? FILE_FLAG_FIRST_PIPE_INSTANCE : 0),
93 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT
94 #ifdef PIPE_REJECT_REMOTE_CLIENTS
95 | PIPE_REJECT_REMOTE_CLIENTS
100 PIPE_UNLIMITED_INSTANCES,
102 /* nOutBufferSize, nInBufferSize */
103 4096, 4096, /* FIXME: think harder about buffer sizes? */
105 /* nDefaultTimeOut */
106 0 /* default timeout */,
108 /* lpSecurityAttributes */
111 return ps->pipehandle != INVALID_HANDLE_VALUE;
114 static Socket named_pipe_accept(accept_ctx_t ctx, Plug plug)
116 HANDLE conn = (HANDLE)ctx.p;
118 return make_handle_socket(conn, conn, plug, TRUE);
121 static void named_pipe_accept_loop(Named_Pipe_Server_Socket ps,
128 if (got_one_already) {
129 /* If we were called with a connection already waiting,
131 got_one_already = FALSE;
135 * Call ConnectNamedPipe, which might succeed or might
136 * tell us that an overlapped operation is in progress and
137 * we should wait for our event object.
139 if (ConnectNamedPipe(ps->pipehandle, &ps->connect_ovl))
142 error = GetLastError();
144 if (error == ERROR_IO_PENDING)
148 if (error == 0 || error == ERROR_PIPE_CONNECTED) {
150 * We've successfully retrieved an incoming connection, so
151 * ps->pipehandle now refers to that connection. So
152 * convert that handle into a separate connection-type
153 * Socket, and create a fresh one to be the new listening
156 HANDLE conn = ps->pipehandle;
159 actx.p = (void *)conn;
160 if (plug_accepting(ps->plug, named_pipe_accept, actx)) {
162 * If the plug didn't want the connection, might as
163 * well close this handle.
168 if (!create_named_pipe(ps, FALSE)) {
169 error = GetLastError();
172 * Go round again to see if more connections can be
173 * got, or to begin waiting on the event object.
179 errmsg = dupprintf("Error while listening to named pipe: %s",
180 win_strerror(error));
181 plug_log(ps->plug, 1, NULL /* FIXME: appropriate kind of sockaddr */, 0,
188 static void named_pipe_connect_callback(void *vps)
190 Named_Pipe_Server_Socket ps = (Named_Pipe_Server_Socket)vps;
191 named_pipe_accept_loop(ps, TRUE);
194 Socket new_named_pipe_listener(const char *pipename, Plug plug)
197 * This socket type is only used for listening, so it should never
198 * be asked to write or flush or set_frozen.
200 static const struct socket_function_table socket_fn_table = {
201 sk_namedpipeserver_plug,
202 sk_namedpipeserver_close,
204 NULL /* write_oob */,
205 NULL /* write_eof */,
207 NULL /* set_frozen */,
208 sk_namedpipeserver_socket_error
211 Named_Pipe_Server_Socket ret;
212 SID_IDENTIFIER_AUTHORITY nt_auth = SECURITY_NT_AUTHORITY;
213 EXPLICIT_ACCESS ea[2];
215 ret = snew(struct Socket_named_pipe_server_tag);
216 ret->fn = &socket_fn_table;
220 ret->pipename = dupstr(pipename);
221 ret->networksid = NULL;
224 assert(strncmp(pipename, "\\\\.\\pipe\\", 9) == 0);
225 assert(strchr(pipename + 9, '\\') == NULL);
227 if (!AllocateAndInitializeSid(&nt_auth, 1, SECURITY_NETWORK_RID,
228 0, 0, 0, 0, 0, 0, 0, &ret->networksid)) {
229 ret->error = dupprintf("unable to construct SID for rejecting "
230 "remote pipe connections: %s",
231 win_strerror(GetLastError()));
235 memset(ea, 0, sizeof(ea));
236 ea[0].grfAccessPermissions = GENERIC_READ | GENERIC_WRITE;
237 ea[0].grfAccessMode = GRANT_ACCESS;
238 ea[0].grfInheritance = NO_INHERITANCE;
239 ea[0].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
240 ea[0].Trustee.ptstrName = "CURRENT_USER";
241 ea[1].grfAccessPermissions = GENERIC_READ | GENERIC_WRITE;
242 ea[1].grfAccessMode = REVOKE_ACCESS;
243 ea[1].grfInheritance = NO_INHERITANCE;
244 ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
245 ea[1].Trustee.ptstrName = (LPTSTR)ret->networksid;
247 if (SetEntriesInAcl(2, ea, NULL, &ret->acl) != ERROR_SUCCESS) {
248 ret->error = dupprintf("unable to construct ACL: %s",
249 win_strerror(GetLastError()));
253 ret->psd = (PSECURITY_DESCRIPTOR)
254 LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
256 ret->error = dupprintf("unable to allocate security descriptor: %s",
257 win_strerror(GetLastError()));
261 if (!InitializeSecurityDescriptor(ret->psd,SECURITY_DESCRIPTOR_REVISION)) {
262 ret->error = dupprintf("unable to initialise security descriptor: %s",
263 win_strerror(GetLastError()));
267 if (!SetSecurityDescriptorDacl(ret->psd, TRUE, ret->acl, FALSE)) {
268 ret->error = dupprintf("unable to set DACL in security descriptor: %s",
269 win_strerror(GetLastError()));
273 if (!create_named_pipe(ret, TRUE)) {
274 ret->error = dupprintf("unable to create named pipe '%s': %s",
275 pipename, win_strerror(GetLastError()));
279 memset(&ret->connect_ovl, 0, sizeof(ret->connect_ovl));
280 ret->connect_ovl.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
281 handle_add_foreign_event(ret->connect_ovl.hEvent,
282 named_pipe_connect_callback, ret);
283 named_pipe_accept_loop(ret, FALSE);
289 #endif /* !defined NO_SECURITY */