]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winproxy.c
Make get_user_sid() return the cached copy if one already exists.
[PuTTY.git] / windows / winproxy.c
1 /*
2  * winproxy.c: Windows implementation of platform_new_connection(),
3  * supporting an OpenSSH-like proxy command via the winhandl.c
4  * mechanism.
5  */
6
7 #include <stdio.h>
8 #include <assert.h>
9
10 #define DEFINE_PLUG_METHOD_MACROS
11 #include "tree234.h"
12 #include "putty.h"
13 #include "network.h"
14 #include "proxy.h"
15
16 Socket make_handle_socket(HANDLE send_H, HANDLE recv_H, Plug plug,
17                           int overlapped);
18
19 Socket platform_new_connection(SockAddr addr, char *hostname,
20                                int port, int privport,
21                                int oobinline, int nodelay, int keepalive,
22                                Plug plug, Conf *conf)
23 {
24     char *cmd;
25     HANDLE us_to_cmd, us_from_cmd, cmd_to_us, cmd_from_us;
26     SECURITY_ATTRIBUTES sa;
27     STARTUPINFO si;
28     PROCESS_INFORMATION pi;
29
30     if (conf_get_int(conf, CONF_proxy_type) != PROXY_CMD)
31         return NULL;
32
33     cmd = format_telnet_command(addr, port, conf);
34
35     /* We are responsible for this and don't need it any more */
36     sk_addr_free(addr);
37
38     {
39         char *msg = dupprintf("Starting local proxy command: %s", cmd);
40         /* We're allowed to pass NULL here, because we're part of the Windows
41          * front end so we know logevent doesn't expect any data. */
42         logevent(NULL, msg);
43         sfree(msg);
44     }
45
46     /*
47      * Create the pipes to the proxy command, and spawn the proxy
48      * command process.
49      */
50     sa.nLength = sizeof(sa);
51     sa.lpSecurityDescriptor = NULL;    /* default */
52     sa.bInheritHandle = TRUE;
53     if (!CreatePipe(&us_from_cmd, &cmd_to_us, &sa, 0)) {
54         Socket ret =
55             new_error_socket("Unable to create pipes for proxy command", plug);
56         sfree(cmd);
57         return ret;
58     }
59
60     if (!CreatePipe(&cmd_from_us, &us_to_cmd, &sa, 0)) {
61         Socket ret =
62             new_error_socket("Unable to create pipes for proxy command", plug);
63         sfree(cmd);
64         CloseHandle(us_from_cmd);
65         CloseHandle(cmd_to_us);
66         return ret;
67     }
68
69     SetHandleInformation(us_to_cmd, HANDLE_FLAG_INHERIT, 0);
70     SetHandleInformation(us_from_cmd, HANDLE_FLAG_INHERIT, 0);
71
72     si.cb = sizeof(si);
73     si.lpReserved = NULL;
74     si.lpDesktop = NULL;
75     si.lpTitle = NULL;
76     si.dwFlags = STARTF_USESTDHANDLES;
77     si.cbReserved2 = 0;
78     si.lpReserved2 = NULL;
79     si.hStdInput = cmd_from_us;
80     si.hStdOutput = cmd_to_us;
81     si.hStdError = NULL;
82     CreateProcess(NULL, cmd, NULL, NULL, TRUE,
83                   CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS,
84                   NULL, NULL, &si, &pi);
85     CloseHandle(pi.hProcess);
86     CloseHandle(pi.hThread);
87
88     sfree(cmd);
89
90     CloseHandle(cmd_from_us);
91     CloseHandle(cmd_to_us);
92
93     return make_handle_socket(us_to_cmd, us_from_cmd, plug, FALSE);
94 }