uxproxy.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * uxproxy.c: Unix implementation of platform_new_connection(),
  3. * supporting an OpenSSH-like proxy command.
  4. */
  5. #include <stdio.h>
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #define DEFINE_PLUG_METHOD_MACROS
  11. #include "tree234.h"
  12. #include "putty.h"
  13. #include "network.h"
  14. #include "proxy.h"
  15. typedef struct Socket_localproxy_tag * Local_Proxy_Socket;
  16. struct Socket_localproxy_tag {
  17. const struct socket_function_table *fn;
  18. /* the above variable absolutely *must* be the first in this structure */
  19. int to_cmd, from_cmd, cmd_err; /* fds */
  20. char *error;
  21. Plug plug;
  22. bufchain pending_output_data;
  23. bufchain pending_input_data;
  24. bufchain pending_error_data;
  25. enum { EOF_NO, EOF_PENDING, EOF_SENT } outgoingeof;
  26. };
  27. static int localproxy_select_result(int fd, int event);
  28. /*
  29. * Trees to look up the pipe fds in.
  30. */
  31. static tree234 *localproxy_by_fromfd;
  32. static tree234 *localproxy_by_tofd;
  33. static tree234 *localproxy_by_errfd;
  34. static int localproxy_fromfd_cmp(void *av, void *bv)
  35. {
  36. Local_Proxy_Socket a = (Local_Proxy_Socket)av;
  37. Local_Proxy_Socket b = (Local_Proxy_Socket)bv;
  38. if (a->from_cmd < b->from_cmd)
  39. return -1;
  40. if (a->from_cmd > b->from_cmd)
  41. return +1;
  42. return 0;
  43. }
  44. static int localproxy_fromfd_find(void *av, void *bv)
  45. {
  46. int a = *(int *)av;
  47. Local_Proxy_Socket b = (Local_Proxy_Socket)bv;
  48. if (a < b->from_cmd)
  49. return -1;
  50. if (a > b->from_cmd)
  51. return +1;
  52. return 0;
  53. }
  54. static int localproxy_tofd_cmp(void *av, void *bv)
  55. {
  56. Local_Proxy_Socket a = (Local_Proxy_Socket)av;
  57. Local_Proxy_Socket b = (Local_Proxy_Socket)bv;
  58. if (a->to_cmd < b->to_cmd)
  59. return -1;
  60. if (a->to_cmd > b->to_cmd)
  61. return +1;
  62. return 0;
  63. }
  64. static int localproxy_tofd_find(void *av, void *bv)
  65. {
  66. int a = *(int *)av;
  67. Local_Proxy_Socket b = (Local_Proxy_Socket)bv;
  68. if (a < b->to_cmd)
  69. return -1;
  70. if (a > b->to_cmd)
  71. return +1;
  72. return 0;
  73. }
  74. static int localproxy_errfd_cmp(void *av, void *bv)
  75. {
  76. Local_Proxy_Socket a = (Local_Proxy_Socket)av;
  77. Local_Proxy_Socket b = (Local_Proxy_Socket)bv;
  78. if (a->cmd_err < b->cmd_err)
  79. return -1;
  80. if (a->cmd_err > b->cmd_err)
  81. return +1;
  82. return 0;
  83. }
  84. static int localproxy_errfd_find(void *av, void *bv)
  85. {
  86. int a = *(int *)av;
  87. Local_Proxy_Socket b = (Local_Proxy_Socket)bv;
  88. if (a < b->cmd_err)
  89. return -1;
  90. if (a > b->cmd_err)
  91. return +1;
  92. return 0;
  93. }
  94. /* basic proxy socket functions */
  95. static Plug sk_localproxy_plug (Socket s, Plug p)
  96. {
  97. Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
  98. Plug ret = ps->plug;
  99. if (p)
  100. ps->plug = p;
  101. return ret;
  102. }
  103. static void sk_localproxy_close (Socket s)
  104. {
  105. Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
  106. if (ps->to_cmd >= 0) {
  107. del234(localproxy_by_tofd, ps);
  108. uxsel_del(ps->to_cmd);
  109. close(ps->to_cmd);
  110. }
  111. del234(localproxy_by_fromfd, ps);
  112. uxsel_del(ps->from_cmd);
  113. close(ps->from_cmd);
  114. del234(localproxy_by_errfd, ps);
  115. uxsel_del(ps->cmd_err);
  116. close(ps->cmd_err);
  117. bufchain_clear(&ps->pending_input_data);
  118. bufchain_clear(&ps->pending_output_data);
  119. bufchain_clear(&ps->pending_error_data);
  120. sfree(ps);
  121. }
  122. static int localproxy_try_send(Local_Proxy_Socket ps)
  123. {
  124. int sent = 0;
  125. while (bufchain_size(&ps->pending_output_data) > 0) {
  126. void *data;
  127. int len, ret;
  128. bufchain_prefix(&ps->pending_output_data, &data, &len);
  129. ret = write(ps->to_cmd, data, len);
  130. if (ret < 0 && errno != EWOULDBLOCK) {
  131. /* We're inside the Unix frontend here, so we know
  132. * that the frontend handle is unnecessary. */
  133. logevent(NULL, strerror(errno));
  134. fatalbox("%s", strerror(errno));
  135. } else if (ret <= 0) {
  136. break;
  137. } else {
  138. bufchain_consume(&ps->pending_output_data, ret);
  139. sent += ret;
  140. }
  141. }
  142. if (ps->outgoingeof == EOF_PENDING) {
  143. del234(localproxy_by_tofd, ps);
  144. close(ps->to_cmd);
  145. uxsel_del(ps->to_cmd);
  146. ps->to_cmd = -1;
  147. ps->outgoingeof = EOF_SENT;
  148. }
  149. if (bufchain_size(&ps->pending_output_data) == 0)
  150. uxsel_del(ps->to_cmd);
  151. else
  152. uxsel_set(ps->to_cmd, 2, localproxy_select_result);
  153. return sent;
  154. }
  155. static int sk_localproxy_write (Socket s, const char *data, int len)
  156. {
  157. Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
  158. assert(ps->outgoingeof == EOF_NO);
  159. bufchain_add(&ps->pending_output_data, data, len);
  160. localproxy_try_send(ps);
  161. return bufchain_size(&ps->pending_output_data);
  162. }
  163. static int sk_localproxy_write_oob (Socket s, const char *data, int len)
  164. {
  165. /*
  166. * oob data is treated as inband; nasty, but nothing really
  167. * better we can do
  168. */
  169. return sk_localproxy_write(s, data, len);
  170. }
  171. static void sk_localproxy_write_eof (Socket s)
  172. {
  173. Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
  174. assert(ps->outgoingeof == EOF_NO);
  175. ps->outgoingeof = EOF_PENDING;
  176. localproxy_try_send(ps);
  177. }
  178. static void sk_localproxy_flush (Socket s)
  179. {
  180. /* Local_Proxy_Socket ps = (Local_Proxy_Socket) s; */
  181. /* do nothing */
  182. }
  183. static void sk_localproxy_set_frozen (Socket s, int is_frozen)
  184. {
  185. Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
  186. if (is_frozen)
  187. uxsel_del(ps->from_cmd);
  188. else
  189. uxsel_set(ps->from_cmd, 1, localproxy_select_result);
  190. }
  191. static const char * sk_localproxy_socket_error (Socket s)
  192. {
  193. Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
  194. return ps->error;
  195. }
  196. static int localproxy_select_result(int fd, int event)
  197. {
  198. Local_Proxy_Socket s;
  199. char buf[20480];
  200. int ret;
  201. if (!(s = find234(localproxy_by_fromfd, &fd, localproxy_fromfd_find)) &&
  202. !(s = find234(localproxy_by_fromfd, &fd, localproxy_errfd_find)) &&
  203. !(s = find234(localproxy_by_tofd, &fd, localproxy_tofd_find)) )
  204. return 1; /* boggle */
  205. if (event == 1) {
  206. if (fd == s->cmd_err) {
  207. ret = read(fd, buf, sizeof(buf));
  208. if (ret > 0)
  209. log_proxy_stderr(s->plug, &s->pending_error_data, buf, ret);
  210. } else {
  211. assert(fd == s->from_cmd);
  212. ret = read(fd, buf, sizeof(buf));
  213. if (ret < 0) {
  214. return plug_closing(s->plug, strerror(errno), errno, 0);
  215. } else if (ret == 0) {
  216. return plug_closing(s->plug, NULL, 0, 0);
  217. } else {
  218. return plug_receive(s->plug, 0, buf, ret);
  219. }
  220. }
  221. } else if (event == 2) {
  222. assert(fd == s->to_cmd);
  223. if (localproxy_try_send(s))
  224. plug_sent(s->plug, bufchain_size(&s->pending_output_data));
  225. return 1;
  226. }
  227. return 1;
  228. }
  229. Socket platform_new_connection(SockAddr addr, const char *hostname,
  230. int port, int privport,
  231. int oobinline, int nodelay, int keepalive,
  232. Plug plug, Conf *conf)
  233. {
  234. char *cmd;
  235. static const struct socket_function_table socket_fn_table = {
  236. sk_localproxy_plug,
  237. sk_localproxy_close,
  238. sk_localproxy_write,
  239. sk_localproxy_write_oob,
  240. sk_localproxy_write_eof,
  241. sk_localproxy_flush,
  242. sk_localproxy_set_frozen,
  243. sk_localproxy_socket_error,
  244. NULL, /* peer_info */
  245. };
  246. Local_Proxy_Socket ret;
  247. int to_cmd_pipe[2], from_cmd_pipe[2], cmd_err_pipe[2], pid, proxytype;
  248. proxytype = conf_get_int(conf, CONF_proxy_type);
  249. if (proxytype != PROXY_CMD && proxytype != PROXY_FUZZ)
  250. return NULL;
  251. ret = snew(struct Socket_localproxy_tag);
  252. ret->fn = &socket_fn_table;
  253. ret->plug = plug;
  254. ret->error = NULL;
  255. ret->outgoingeof = EOF_NO;
  256. bufchain_init(&ret->pending_input_data);
  257. bufchain_init(&ret->pending_output_data);
  258. bufchain_init(&ret->pending_error_data);
  259. if (proxytype == PROXY_CMD) {
  260. cmd = format_telnet_command(addr, port, conf);
  261. if (flags & FLAG_STDERR) {
  262. /* If we have a sensible stderr, the proxy command can
  263. * send its own standard error there, so we won't
  264. * interfere. */
  265. cmd_err_pipe[0] = cmd_err_pipe[1] = -1;
  266. } else {
  267. /* If we don't have a sensible stderr, we should catch the
  268. * proxy command's standard error to put in our event
  269. * log. */
  270. cmd_err_pipe[0] = cmd_err_pipe[1] = 0;
  271. }
  272. {
  273. char *logmsg = dupprintf("Starting local proxy command: %s", cmd);
  274. plug_log(plug, 2, NULL, 0, logmsg, 0);
  275. sfree(logmsg);
  276. }
  277. /*
  278. * Create the pipes to the proxy command, and spawn the proxy
  279. * command process.
  280. */
  281. if (pipe(to_cmd_pipe) < 0 ||
  282. pipe(from_cmd_pipe) < 0 ||
  283. (cmd_err_pipe[0] == 0 && pipe(cmd_err_pipe) < 0)) {
  284. ret->error = dupprintf("pipe: %s", strerror(errno));
  285. sfree(cmd);
  286. return (Socket)ret;
  287. }
  288. cloexec(to_cmd_pipe[1]);
  289. cloexec(from_cmd_pipe[0]);
  290. if (cmd_err_pipe[0] >= 0)
  291. cloexec(cmd_err_pipe[0]);
  292. pid = fork();
  293. if (pid < 0) {
  294. ret->error = dupprintf("fork: %s", strerror(errno));
  295. sfree(cmd);
  296. return (Socket)ret;
  297. } else if (pid == 0) {
  298. close(0);
  299. close(1);
  300. dup2(to_cmd_pipe[0], 0);
  301. dup2(from_cmd_pipe[1], 1);
  302. close(to_cmd_pipe[0]);
  303. close(from_cmd_pipe[1]);
  304. if (cmd_err_pipe[0] >= 0) {
  305. dup2(cmd_err_pipe[1], 2);
  306. close(cmd_err_pipe[1]);
  307. }
  308. noncloexec(0);
  309. noncloexec(1);
  310. execl("/bin/sh", "sh", "-c", cmd, (void *)NULL);
  311. _exit(255);
  312. }
  313. sfree(cmd);
  314. close(to_cmd_pipe[0]);
  315. close(from_cmd_pipe[1]);
  316. if (cmd_err_pipe[0] >= 0)
  317. close(cmd_err_pipe[1]);
  318. ret->to_cmd = to_cmd_pipe[1];
  319. ret->from_cmd = from_cmd_pipe[0];
  320. ret->cmd_err = cmd_err_pipe[0];
  321. } else {
  322. cmd = format_telnet_command(addr, port, conf);
  323. ret->to_cmd = open("/dev/null", O_WRONLY);
  324. if (ret->to_cmd == -1) {
  325. ret->error = dupprintf("/dev/null: %s", strerror(errno));
  326. sfree(cmd);
  327. return (Socket)ret;
  328. }
  329. ret->from_cmd = open(cmd, O_RDONLY);
  330. if (ret->from_cmd == -1) {
  331. ret->error = dupprintf("%s: %s", cmd, strerror(errno));
  332. sfree(cmd);
  333. return (Socket)ret;
  334. }
  335. sfree(cmd);
  336. ret->cmd_err = -1;
  337. }
  338. if (!localproxy_by_fromfd)
  339. localproxy_by_fromfd = newtree234(localproxy_fromfd_cmp);
  340. if (!localproxy_by_tofd)
  341. localproxy_by_tofd = newtree234(localproxy_tofd_cmp);
  342. if (!localproxy_by_errfd)
  343. localproxy_by_errfd = newtree234(localproxy_errfd_cmp);
  344. add234(localproxy_by_fromfd, ret);
  345. add234(localproxy_by_tofd, ret);
  346. if (ret->cmd_err >= 0)
  347. add234(localproxy_by_errfd, ret);
  348. uxsel_set(ret->from_cmd, 1, localproxy_select_result);
  349. if (ret->cmd_err >= 0)
  350. uxsel_set(ret->cmd_err, 1, localproxy_select_result);
  351. /* We are responsible for this and don't need it any more */
  352. sk_addr_free(addr);
  353. return (Socket) ret;
  354. }