PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/TortoisePlink/PROXY.C

https://gitlab.com/info29/tortoisegit
C | 1510 lines | 954 code | 230 blank | 326 comment | 295 complexity | 625185a291fe80ae1374350bd469571b MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MPL-2.0-no-copyleft-exception
  1. /*
  2. * Network proxy abstraction in PuTTY
  3. *
  4. * A proxy layer, if necessary, wedges itself between the network
  5. * code and the higher level backend.
  6. */
  7. #include <assert.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10. #define DEFINE_PLUG_METHOD_MACROS
  11. #include "putty.h"
  12. #include "network.h"
  13. #include "proxy.h"
  14. #define do_proxy_dns(conf) \
  15. (conf_get_int(conf, CONF_proxy_dns) == FORCE_ON || \
  16. (conf_get_int(conf, CONF_proxy_dns) == AUTO && \
  17. conf_get_int(conf, CONF_proxy_type) != PROXY_SOCKS4))
  18. /*
  19. * Call this when proxy negotiation is complete, so that this
  20. * socket can begin working normally.
  21. */
  22. void proxy_activate (Proxy_Socket p)
  23. {
  24. void *data;
  25. int len;
  26. long output_before, output_after;
  27. p->state = PROXY_STATE_ACTIVE;
  28. /* we want to ignore new receive events until we have sent
  29. * all of our buffered receive data.
  30. */
  31. sk_set_frozen(p->sub_socket, 1);
  32. /* how many bytes of output have we buffered? */
  33. output_before = bufchain_size(&p->pending_oob_output_data) +
  34. bufchain_size(&p->pending_output_data);
  35. /* and keep track of how many bytes do not get sent. */
  36. output_after = 0;
  37. /* send buffered OOB writes */
  38. while (bufchain_size(&p->pending_oob_output_data) > 0) {
  39. bufchain_prefix(&p->pending_oob_output_data, &data, &len);
  40. output_after += sk_write_oob(p->sub_socket, data, len);
  41. bufchain_consume(&p->pending_oob_output_data, len);
  42. }
  43. /* send buffered normal writes */
  44. while (bufchain_size(&p->pending_output_data) > 0) {
  45. bufchain_prefix(&p->pending_output_data, &data, &len);
  46. output_after += sk_write(p->sub_socket, data, len);
  47. bufchain_consume(&p->pending_output_data, len);
  48. }
  49. /* if we managed to send any data, let the higher levels know. */
  50. if (output_after < output_before)
  51. plug_sent(p->plug, output_after);
  52. /* if we were asked to flush the output during
  53. * the proxy negotiation process, do so now.
  54. */
  55. if (p->pending_flush) sk_flush(p->sub_socket);
  56. /* if we have a pending EOF to send, send it */
  57. if (p->pending_eof) sk_write_eof(p->sub_socket);
  58. /* if the backend wanted the socket unfrozen, try to unfreeze.
  59. * our set_frozen handler will flush buffered receive data before
  60. * unfreezing the actual underlying socket.
  61. */
  62. if (!p->freeze)
  63. sk_set_frozen((Socket)p, 0);
  64. }
  65. /* basic proxy socket functions */
  66. static Plug sk_proxy_plug (Socket s, Plug p)
  67. {
  68. Proxy_Socket ps = (Proxy_Socket) s;
  69. Plug ret = ps->plug;
  70. if (p)
  71. ps->plug = p;
  72. return ret;
  73. }
  74. static void sk_proxy_close (Socket s)
  75. {
  76. Proxy_Socket ps = (Proxy_Socket) s;
  77. sk_close(ps->sub_socket);
  78. sk_addr_free(ps->remote_addr);
  79. sfree(ps);
  80. }
  81. static int sk_proxy_write (Socket s, const char *data, int len)
  82. {
  83. Proxy_Socket ps = (Proxy_Socket) s;
  84. if (ps->state != PROXY_STATE_ACTIVE) {
  85. bufchain_add(&ps->pending_output_data, data, len);
  86. return bufchain_size(&ps->pending_output_data);
  87. }
  88. return sk_write(ps->sub_socket, data, len);
  89. }
  90. static int sk_proxy_write_oob (Socket s, const char *data, int len)
  91. {
  92. Proxy_Socket ps = (Proxy_Socket) s;
  93. if (ps->state != PROXY_STATE_ACTIVE) {
  94. bufchain_clear(&ps->pending_output_data);
  95. bufchain_clear(&ps->pending_oob_output_data);
  96. bufchain_add(&ps->pending_oob_output_data, data, len);
  97. return len;
  98. }
  99. return sk_write_oob(ps->sub_socket, data, len);
  100. }
  101. static void sk_proxy_write_eof (Socket s)
  102. {
  103. Proxy_Socket ps = (Proxy_Socket) s;
  104. if (ps->state != PROXY_STATE_ACTIVE) {
  105. ps->pending_eof = 1;
  106. return;
  107. }
  108. sk_write_eof(ps->sub_socket);
  109. }
  110. static void sk_proxy_flush (Socket s)
  111. {
  112. Proxy_Socket ps = (Proxy_Socket) s;
  113. if (ps->state != PROXY_STATE_ACTIVE) {
  114. ps->pending_flush = 1;
  115. return;
  116. }
  117. sk_flush(ps->sub_socket);
  118. }
  119. static void sk_proxy_set_frozen (Socket s, int is_frozen)
  120. {
  121. Proxy_Socket ps = (Proxy_Socket) s;
  122. if (ps->state != PROXY_STATE_ACTIVE) {
  123. ps->freeze = is_frozen;
  124. return;
  125. }
  126. /* handle any remaining buffered recv data first */
  127. if (bufchain_size(&ps->pending_input_data) > 0) {
  128. ps->freeze = is_frozen;
  129. /* loop while we still have buffered data, and while we are
  130. * unfrozen. the plug_receive call in the loop could result
  131. * in a call back into this function refreezing the socket,
  132. * so we have to check each time.
  133. */
  134. while (!ps->freeze && bufchain_size(&ps->pending_input_data) > 0) {
  135. void *data;
  136. char databuf[512];
  137. int len;
  138. bufchain_prefix(&ps->pending_input_data, &data, &len);
  139. if (len > lenof(databuf))
  140. len = lenof(databuf);
  141. memcpy(databuf, data, len);
  142. bufchain_consume(&ps->pending_input_data, len);
  143. plug_receive(ps->plug, 0, databuf, len);
  144. }
  145. /* if we're still frozen, we'll have to wait for another
  146. * call from the backend to finish unbuffering the data.
  147. */
  148. if (ps->freeze) return;
  149. }
  150. sk_set_frozen(ps->sub_socket, is_frozen);
  151. }
  152. static const char * sk_proxy_socket_error (Socket s)
  153. {
  154. Proxy_Socket ps = (Proxy_Socket) s;
  155. if (ps->error != NULL || ps->sub_socket == NULL) {
  156. return ps->error;
  157. }
  158. return sk_socket_error(ps->sub_socket);
  159. }
  160. /* basic proxy plug functions */
  161. static void plug_proxy_log(Plug plug, int type, SockAddr addr, int port,
  162. const char *error_msg, int error_code)
  163. {
  164. Proxy_Plug pp = (Proxy_Plug) plug;
  165. Proxy_Socket ps = pp->proxy_socket;
  166. plug_log(ps->plug, type, addr, port, error_msg, error_code);
  167. }
  168. static int plug_proxy_closing (Plug p, const char *error_msg,
  169. int error_code, int calling_back)
  170. {
  171. Proxy_Plug pp = (Proxy_Plug) p;
  172. Proxy_Socket ps = pp->proxy_socket;
  173. if (ps->state != PROXY_STATE_ACTIVE) {
  174. ps->closing_error_msg = error_msg;
  175. ps->closing_error_code = error_code;
  176. ps->closing_calling_back = calling_back;
  177. return ps->negotiate(ps, PROXY_CHANGE_CLOSING);
  178. }
  179. return plug_closing(ps->plug, error_msg,
  180. error_code, calling_back);
  181. }
  182. static int plug_proxy_receive (Plug p, int urgent, char *data, int len)
  183. {
  184. Proxy_Plug pp = (Proxy_Plug) p;
  185. Proxy_Socket ps = pp->proxy_socket;
  186. if (ps->state != PROXY_STATE_ACTIVE) {
  187. /* we will lose the urgentness of this data, but since most,
  188. * if not all, of this data will be consumed by the negotiation
  189. * process, hopefully it won't affect the protocol above us
  190. */
  191. bufchain_add(&ps->pending_input_data, data, len);
  192. ps->receive_urgent = urgent;
  193. ps->receive_data = data;
  194. ps->receive_len = len;
  195. return ps->negotiate(ps, PROXY_CHANGE_RECEIVE);
  196. }
  197. return plug_receive(ps->plug, urgent, data, len);
  198. }
  199. static void plug_proxy_sent (Plug p, int bufsize)
  200. {
  201. Proxy_Plug pp = (Proxy_Plug) p;
  202. Proxy_Socket ps = pp->proxy_socket;
  203. if (ps->state != PROXY_STATE_ACTIVE) {
  204. ps->sent_bufsize = bufsize;
  205. ps->negotiate(ps, PROXY_CHANGE_SENT);
  206. return;
  207. }
  208. plug_sent(ps->plug, bufsize);
  209. }
  210. static int plug_proxy_accepting(Plug p,
  211. accept_fn_t constructor, accept_ctx_t ctx)
  212. {
  213. Proxy_Plug pp = (Proxy_Plug) p;
  214. Proxy_Socket ps = pp->proxy_socket;
  215. if (ps->state != PROXY_STATE_ACTIVE) {
  216. ps->accepting_constructor = constructor;
  217. ps->accepting_ctx = ctx;
  218. return ps->negotiate(ps, PROXY_CHANGE_ACCEPTING);
  219. }
  220. return plug_accepting(ps->plug, constructor, ctx);
  221. }
  222. /*
  223. * This function can accept a NULL pointer as `addr', in which case
  224. * it will only check the host name.
  225. */
  226. int proxy_for_destination (SockAddr addr, const char *hostname,
  227. int port, Conf *conf)
  228. {
  229. int s = 0, e = 0;
  230. char hostip[64];
  231. int hostip_len, hostname_len;
  232. const char *exclude_list;
  233. /*
  234. * Special local connections such as Unix-domain sockets
  235. * unconditionally cannot be proxied, even in proxy-localhost
  236. * mode. There just isn't any way to ask any known proxy type for
  237. * them.
  238. */
  239. if (addr && sk_address_is_special_local(addr))
  240. return 0; /* do not proxy */
  241. /*
  242. * Check the host name and IP against the hard-coded
  243. * representations of `localhost'.
  244. */
  245. if (!conf_get_int(conf, CONF_even_proxy_localhost) &&
  246. (sk_hostname_is_local(hostname) ||
  247. (addr && sk_address_is_local(addr))))
  248. return 0; /* do not proxy */
  249. /* we want a string representation of the IP address for comparisons */
  250. if (addr) {
  251. sk_getaddr(addr, hostip, 64);
  252. hostip_len = strlen(hostip);
  253. } else
  254. hostip_len = 0; /* placate gcc; shouldn't be required */
  255. hostname_len = strlen(hostname);
  256. exclude_list = conf_get_str(conf, CONF_proxy_exclude_list);
  257. /* now parse the exclude list, and see if either our IP
  258. * or hostname matches anything in it.
  259. */
  260. while (exclude_list[s]) {
  261. while (exclude_list[s] &&
  262. (isspace((unsigned char)exclude_list[s]) ||
  263. exclude_list[s] == ',')) s++;
  264. if (!exclude_list[s]) break;
  265. e = s;
  266. while (exclude_list[e] &&
  267. (isalnum((unsigned char)exclude_list[e]) ||
  268. exclude_list[e] == '-' ||
  269. exclude_list[e] == '.' ||
  270. exclude_list[e] == '*')) e++;
  271. if (exclude_list[s] == '*') {
  272. /* wildcard at beginning of entry */
  273. if ((addr && strnicmp(hostip + hostip_len - (e - s - 1),
  274. exclude_list + s + 1, e - s - 1) == 0) ||
  275. strnicmp(hostname + hostname_len - (e - s - 1),
  276. exclude_list + s + 1, e - s - 1) == 0)
  277. return 0; /* IP/hostname range excluded. do not use proxy. */
  278. } else if (exclude_list[e-1] == '*') {
  279. /* wildcard at end of entry */
  280. if ((addr && strnicmp(hostip, exclude_list + s, e - s - 1) == 0) ||
  281. strnicmp(hostname, exclude_list + s, e - s - 1) == 0)
  282. return 0; /* IP/hostname range excluded. do not use proxy. */
  283. } else {
  284. /* no wildcard at either end, so let's try an absolute
  285. * match (ie. a specific IP)
  286. */
  287. if (addr && strnicmp(hostip, exclude_list + s, e - s) == 0)
  288. return 0; /* IP/hostname excluded. do not use proxy. */
  289. if (strnicmp(hostname, exclude_list + s, e - s) == 0)
  290. return 0; /* IP/hostname excluded. do not use proxy. */
  291. }
  292. s = e;
  293. /* Make sure we really have reached the next comma or end-of-string */
  294. while (exclude_list[s] &&
  295. !isspace((unsigned char)exclude_list[s]) &&
  296. exclude_list[s] != ',') s++;
  297. }
  298. /* no matches in the exclude list, so use the proxy */
  299. return 1;
  300. }
  301. SockAddr name_lookup(char *host, int port, char **canonicalname,
  302. Conf *conf, int addressfamily)
  303. {
  304. if (conf_get_int(conf, CONF_proxy_type) != PROXY_NONE &&
  305. do_proxy_dns(conf) &&
  306. proxy_for_destination(NULL, host, port, conf)) {
  307. *canonicalname = dupstr(host);
  308. return sk_nonamelookup(host);
  309. }
  310. return sk_namelookup(host, canonicalname, addressfamily);
  311. }
  312. Socket new_connection(SockAddr addr, char *hostname,
  313. int port, int privport,
  314. int oobinline, int nodelay, int keepalive,
  315. Plug plug, Conf *conf)
  316. {
  317. static const struct socket_function_table socket_fn_table = {
  318. sk_proxy_plug,
  319. sk_proxy_close,
  320. sk_proxy_write,
  321. sk_proxy_write_oob,
  322. sk_proxy_write_eof,
  323. sk_proxy_flush,
  324. sk_proxy_set_frozen,
  325. sk_proxy_socket_error,
  326. NULL, /* peer_info */
  327. };
  328. static const struct plug_function_table plug_fn_table = {
  329. plug_proxy_log,
  330. plug_proxy_closing,
  331. plug_proxy_receive,
  332. plug_proxy_sent,
  333. plug_proxy_accepting
  334. };
  335. if (conf_get_int(conf, CONF_proxy_type) != PROXY_NONE &&
  336. proxy_for_destination(addr, hostname, port, conf))
  337. {
  338. Proxy_Socket ret;
  339. Proxy_Plug pplug;
  340. SockAddr proxy_addr;
  341. char *proxy_canonical_name;
  342. Socket sret;
  343. int type;
  344. if ((sret = platform_new_connection(addr, hostname, port, privport,
  345. oobinline, nodelay, keepalive,
  346. plug, conf)) !=
  347. NULL)
  348. return sret;
  349. ret = snew(struct Socket_proxy_tag);
  350. ret->fn = &socket_fn_table;
  351. ret->conf = conf_copy(conf);
  352. ret->plug = plug;
  353. ret->remote_addr = addr; /* will need to be freed on close */
  354. ret->remote_port = port;
  355. ret->error = NULL;
  356. ret->pending_flush = 0;
  357. ret->pending_eof = 0;
  358. ret->freeze = 0;
  359. bufchain_init(&ret->pending_input_data);
  360. bufchain_init(&ret->pending_output_data);
  361. bufchain_init(&ret->pending_oob_output_data);
  362. ret->sub_socket = NULL;
  363. ret->state = PROXY_STATE_NEW;
  364. ret->negotiate = NULL;
  365. type = conf_get_int(conf, CONF_proxy_type);
  366. if (type == PROXY_HTTP) {
  367. ret->negotiate = proxy_http_negotiate;
  368. } else if (type == PROXY_SOCKS4) {
  369. ret->negotiate = proxy_socks4_negotiate;
  370. } else if (type == PROXY_SOCKS5) {
  371. ret->negotiate = proxy_socks5_negotiate;
  372. } else if (type == PROXY_TELNET) {
  373. ret->negotiate = proxy_telnet_negotiate;
  374. } else {
  375. ret->error = "Proxy error: Unknown proxy method";
  376. return (Socket) ret;
  377. }
  378. /* create the proxy plug to map calls from the actual
  379. * socket into our proxy socket layer */
  380. pplug = snew(struct Plug_proxy_tag);
  381. pplug->fn = &plug_fn_table;
  382. pplug->proxy_socket = ret;
  383. /* look-up proxy */
  384. proxy_addr = sk_namelookup(conf_get_str(conf, CONF_proxy_host),
  385. &proxy_canonical_name,
  386. conf_get_int(conf, CONF_addressfamily));
  387. if (sk_addr_error(proxy_addr) != NULL) {
  388. ret->error = "Proxy error: Unable to resolve proxy host name";
  389. sfree(pplug);
  390. sk_addr_free(proxy_addr);
  391. return (Socket)ret;
  392. }
  393. sfree(proxy_canonical_name);
  394. /* create the actual socket we will be using,
  395. * connected to our proxy server and port.
  396. */
  397. ret->sub_socket = sk_new(proxy_addr,
  398. conf_get_int(conf, CONF_proxy_port),
  399. privport, oobinline,
  400. nodelay, keepalive, (Plug) pplug);
  401. if (sk_socket_error(ret->sub_socket) != NULL)
  402. return (Socket) ret;
  403. /* start the proxy negotiation process... */
  404. sk_set_frozen(ret->sub_socket, 0);
  405. ret->negotiate(ret, PROXY_CHANGE_NEW);
  406. return (Socket) ret;
  407. }
  408. /* no proxy, so just return the direct socket */
  409. return sk_new(addr, port, privport, oobinline, nodelay, keepalive, plug);
  410. }
  411. Socket new_listener(char *srcaddr, int port, Plug plug, int local_host_only,
  412. Conf *conf, int addressfamily)
  413. {
  414. /* TODO: SOCKS (and potentially others) support inbound
  415. * TODO: connections via the proxy. support them.
  416. */
  417. return sk_newlistener(srcaddr, port, plug, local_host_only, addressfamily);
  418. }
  419. /* ----------------------------------------------------------------------
  420. * HTTP CONNECT proxy type.
  421. */
  422. static int get_line_end (char * data, int len)
  423. {
  424. int off = 0;
  425. while (off < len)
  426. {
  427. if (data[off] == '\n') {
  428. /* we have a newline */
  429. off++;
  430. /* is that the only thing on this line? */
  431. if (off <= 2) return off;
  432. /* if not, then there is the possibility that this header
  433. * continues onto the next line, if it starts with a space
  434. * or a tab.
  435. */
  436. if (off + 1 < len &&
  437. data[off+1] != ' ' &&
  438. data[off+1] != '\t') return off;
  439. /* the line does continue, so we have to keep going
  440. * until we see an the header's "real" end of line.
  441. */
  442. off++;
  443. }
  444. off++;
  445. }
  446. return -1;
  447. }
  448. int proxy_http_negotiate (Proxy_Socket p, int change)
  449. {
  450. if (p->state == PROXY_STATE_NEW) {
  451. /* we are just beginning the proxy negotiate process,
  452. * so we'll send off the initial bits of the request.
  453. * for this proxy method, it's just a simple HTTP
  454. * request
  455. */
  456. char *buf, dest[512];
  457. char *username, *password;
  458. sk_getaddr(p->remote_addr, dest, lenof(dest));
  459. buf = dupprintf("CONNECT %s:%i HTTP/1.1\r\nHost: %s:%i\r\n",
  460. dest, p->remote_port, dest, p->remote_port);
  461. sk_write(p->sub_socket, buf, strlen(buf));
  462. sfree(buf);
  463. username = conf_get_str(p->conf, CONF_proxy_username);
  464. password = conf_get_str(p->conf, CONF_proxy_password);
  465. if (username[0] || password[0]) {
  466. char *buf, *buf2;
  467. int i, j, len;
  468. buf = dupprintf("%s:%s", username, password);
  469. len = strlen(buf);
  470. buf2 = snewn(len * 4 / 3 + 100, char);
  471. sprintf(buf2, "Proxy-Authorization: Basic ");
  472. for (i = 0, j = strlen(buf2); i < len; i += 3, j += 4)
  473. base64_encode_atom((unsigned char *)(buf+i),
  474. (len-i > 3 ? 3 : len-i), buf2+j);
  475. strcpy(buf2+j, "\r\n");
  476. sk_write(p->sub_socket, buf2, strlen(buf2));
  477. sfree(buf);
  478. sfree(buf2);
  479. }
  480. sk_write(p->sub_socket, "\r\n", 2);
  481. p->state = 1;
  482. return 0;
  483. }
  484. if (change == PROXY_CHANGE_CLOSING) {
  485. /* if our proxy negotiation process involves closing and opening
  486. * new sockets, then we would want to intercept this closing
  487. * callback when we were expecting it. if we aren't anticipating
  488. * a socket close, then some error must have occurred. we'll
  489. * just pass those errors up to the backend.
  490. */
  491. return plug_closing(p->plug, p->closing_error_msg,
  492. p->closing_error_code,
  493. p->closing_calling_back);
  494. }
  495. if (change == PROXY_CHANGE_SENT) {
  496. /* some (or all) of what we wrote to the proxy was sent.
  497. * we don't do anything new, however, until we receive the
  498. * proxy's response. we might want to set a timer so we can
  499. * timeout the proxy negotiation after a while...
  500. */
  501. return 0;
  502. }
  503. if (change == PROXY_CHANGE_ACCEPTING) {
  504. /* we should _never_ see this, as we are using our socket to
  505. * connect to a proxy, not accepting inbound connections.
  506. * what should we do? close the socket with an appropriate
  507. * error message?
  508. */
  509. return plug_accepting(p->plug,
  510. p->accepting_constructor, p->accepting_ctx);
  511. }
  512. if (change == PROXY_CHANGE_RECEIVE) {
  513. /* we have received data from the underlying socket, which
  514. * we'll need to parse, process, and respond to appropriately.
  515. */
  516. char *data, *datap;
  517. int len;
  518. int eol;
  519. if (p->state == 1) {
  520. int min_ver, maj_ver, status;
  521. /* get the status line */
  522. len = bufchain_size(&p->pending_input_data);
  523. assert(len > 0); /* or we wouldn't be here */
  524. data = snewn(len+1, char);
  525. bufchain_fetch(&p->pending_input_data, data, len);
  526. /*
  527. * We must NUL-terminate this data, because Windows
  528. * sscanf appears to require a NUL at the end of the
  529. * string because it strlens it _first_. Sigh.
  530. */
  531. data[len] = '\0';
  532. eol = get_line_end(data, len);
  533. if (eol < 0) {
  534. sfree(data);
  535. return 1;
  536. }
  537. status = -1;
  538. /* We can't rely on whether the %n incremented the sscanf return */
  539. if (sscanf((char *)data, "HTTP/%i.%i %n",
  540. &maj_ver, &min_ver, &status) < 2 || status == -1) {
  541. plug_closing(p->plug, "Proxy error: HTTP response was absent",
  542. PROXY_ERROR_GENERAL, 0);
  543. sfree(data);
  544. return 1;
  545. }
  546. /* remove the status line from the input buffer. */
  547. bufchain_consume(&p->pending_input_data, eol);
  548. if (data[status] != '2') {
  549. /* error */
  550. char *buf;
  551. data[eol] = '\0';
  552. while (eol > status &&
  553. (data[eol-1] == '\r' || data[eol-1] == '\n'))
  554. data[--eol] = '\0';
  555. buf = dupprintf("Proxy error: %s", data+status);
  556. plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
  557. sfree(buf);
  558. sfree(data);
  559. return 1;
  560. }
  561. sfree(data);
  562. p->state = 2;
  563. }
  564. if (p->state == 2) {
  565. /* get headers. we're done when we get a
  566. * header of length 2, (ie. just "\r\n")
  567. */
  568. len = bufchain_size(&p->pending_input_data);
  569. assert(len > 0); /* or we wouldn't be here */
  570. data = snewn(len, char);
  571. datap = data;
  572. bufchain_fetch(&p->pending_input_data, data, len);
  573. eol = get_line_end(datap, len);
  574. if (eol < 0) {
  575. sfree(data);
  576. return 1;
  577. }
  578. while (eol > 2)
  579. {
  580. bufchain_consume(&p->pending_input_data, eol);
  581. datap += eol;
  582. len -= eol;
  583. eol = get_line_end(datap, len);
  584. }
  585. if (eol == 2) {
  586. /* we're done */
  587. bufchain_consume(&p->pending_input_data, 2);
  588. proxy_activate(p);
  589. /* proxy activate will have dealt with
  590. * whatever is left of the buffer */
  591. sfree(data);
  592. return 1;
  593. }
  594. sfree(data);
  595. return 1;
  596. }
  597. }
  598. plug_closing(p->plug, "Proxy error: unexpected proxy error",
  599. PROXY_ERROR_UNEXPECTED, 0);
  600. return 1;
  601. }
  602. /* ----------------------------------------------------------------------
  603. * SOCKS proxy type.
  604. */
  605. /* SOCKS version 4 */
  606. int proxy_socks4_negotiate (Proxy_Socket p, int change)
  607. {
  608. if (p->state == PROXY_CHANGE_NEW) {
  609. /* request format:
  610. * version number (1 byte) = 4
  611. * command code (1 byte)
  612. * 1 = CONNECT
  613. * 2 = BIND
  614. * dest. port (2 bytes) [network order]
  615. * dest. address (4 bytes)
  616. * user ID (variable length, null terminated string)
  617. */
  618. int length, type, namelen;
  619. char *command, addr[4], hostname[512];
  620. char *username;
  621. type = sk_addrtype(p->remote_addr);
  622. if (type == ADDRTYPE_IPV6) {
  623. p->error = "Proxy error: SOCKS version 4 does not support IPv6";
  624. return 1;
  625. } else if (type == ADDRTYPE_IPV4) {
  626. namelen = 0;
  627. sk_addrcopy(p->remote_addr, addr);
  628. } else { /* type == ADDRTYPE_NAME */
  629. assert(type == ADDRTYPE_NAME);
  630. sk_getaddr(p->remote_addr, hostname, lenof(hostname));
  631. namelen = strlen(hostname) + 1; /* include the NUL */
  632. addr[0] = addr[1] = addr[2] = 0;
  633. addr[3] = 1;
  634. }
  635. username = conf_get_str(p->conf, CONF_proxy_username);
  636. length = strlen(username) + namelen + 9;
  637. command = snewn(length, char);
  638. strcpy(command + 8, username);
  639. command[0] = 4; /* version 4 */
  640. command[1] = 1; /* CONNECT command */
  641. /* port */
  642. command[2] = (char) (p->remote_port >> 8) & 0xff;
  643. command[3] = (char) p->remote_port & 0xff;
  644. /* address */
  645. memcpy(command + 4, addr, 4);
  646. /* hostname */
  647. memcpy(command + 8 + strlen(username) + 1,
  648. hostname, namelen);
  649. sk_write(p->sub_socket, command, length);
  650. sfree(username);
  651. sfree(command);
  652. p->state = 1;
  653. return 0;
  654. }
  655. if (change == PROXY_CHANGE_CLOSING) {
  656. /* if our proxy negotiation process involves closing and opening
  657. * new sockets, then we would want to intercept this closing
  658. * callback when we were expecting it. if we aren't anticipating
  659. * a socket close, then some error must have occurred. we'll
  660. * just pass those errors up to the backend.
  661. */
  662. return plug_closing(p->plug, p->closing_error_msg,
  663. p->closing_error_code,
  664. p->closing_calling_back);
  665. }
  666. if (change == PROXY_CHANGE_SENT) {
  667. /* some (or all) of what we wrote to the proxy was sent.
  668. * we don't do anything new, however, until we receive the
  669. * proxy's response. we might want to set a timer so we can
  670. * timeout the proxy negotiation after a while...
  671. */
  672. return 0;
  673. }
  674. if (change == PROXY_CHANGE_ACCEPTING) {
  675. /* we should _never_ see this, as we are using our socket to
  676. * connect to a proxy, not accepting inbound connections.
  677. * what should we do? close the socket with an appropriate
  678. * error message?
  679. */
  680. return plug_accepting(p->plug,
  681. p->accepting_constructor, p->accepting_ctx);
  682. }
  683. if (change == PROXY_CHANGE_RECEIVE) {
  684. /* we have received data from the underlying socket, which
  685. * we'll need to parse, process, and respond to appropriately.
  686. */
  687. if (p->state == 1) {
  688. /* response format:
  689. * version number (1 byte) = 4
  690. * reply code (1 byte)
  691. * 90 = request granted
  692. * 91 = request rejected or failed
  693. * 92 = request rejected due to lack of IDENTD on client
  694. * 93 = request rejected due to difference in user ID
  695. * (what we sent vs. what IDENTD said)
  696. * dest. port (2 bytes)
  697. * dest. address (4 bytes)
  698. */
  699. char data[8];
  700. if (bufchain_size(&p->pending_input_data) < 8)
  701. return 1; /* not got anything yet */
  702. /* get the response */
  703. bufchain_fetch(&p->pending_input_data, data, 8);
  704. if (data[0] != 0) {
  705. plug_closing(p->plug, "Proxy error: SOCKS proxy responded with "
  706. "unexpected reply code version",
  707. PROXY_ERROR_GENERAL, 0);
  708. return 1;
  709. }
  710. if (data[1] != 90) {
  711. switch (data[1]) {
  712. case 92:
  713. plug_closing(p->plug, "Proxy error: SOCKS server wanted IDENTD on client",
  714. PROXY_ERROR_GENERAL, 0);
  715. break;
  716. case 93:
  717. plug_closing(p->plug, "Proxy error: Username and IDENTD on client don't agree",
  718. PROXY_ERROR_GENERAL, 0);
  719. break;
  720. case 91:
  721. default:
  722. plug_closing(p->plug, "Proxy error: Error while communicating with proxy",
  723. PROXY_ERROR_GENERAL, 0);
  724. break;
  725. }
  726. return 1;
  727. }
  728. bufchain_consume(&p->pending_input_data, 8);
  729. /* we're done */
  730. proxy_activate(p);
  731. /* proxy activate will have dealt with
  732. * whatever is left of the buffer */
  733. return 1;
  734. }
  735. }
  736. plug_closing(p->plug, "Proxy error: unexpected proxy error",
  737. PROXY_ERROR_UNEXPECTED, 0);
  738. return 1;
  739. }
  740. /* SOCKS version 5 */
  741. int proxy_socks5_negotiate (Proxy_Socket p, int change)
  742. {
  743. if (p->state == PROXY_CHANGE_NEW) {
  744. /* initial command:
  745. * version number (1 byte) = 5
  746. * number of available authentication methods (1 byte)
  747. * available authentication methods (1 byte * previous value)
  748. * authentication methods:
  749. * 0x00 = no authentication
  750. * 0x01 = GSSAPI
  751. * 0x02 = username/password
  752. * 0x03 = CHAP
  753. */
  754. char command[5];
  755. char *username, *password;
  756. int len;
  757. command[0] = 5; /* version 5 */
  758. username = conf_get_str(p->conf, CONF_proxy_username);
  759. password = conf_get_str(p->conf, CONF_proxy_password);
  760. if (username[0] || password[0]) {
  761. command[2] = 0x00; /* no authentication */
  762. len = 3;
  763. proxy_socks5_offerencryptedauth (command, &len);
  764. command[len++] = 0x02; /* username/password */
  765. command[1] = len - 2; /* Number of methods supported */
  766. } else {
  767. command[1] = 1; /* one methods supported: */
  768. command[2] = 0x00; /* no authentication */
  769. len = 3;
  770. }
  771. sk_write(p->sub_socket, command, len);
  772. p->state = 1;
  773. return 0;
  774. }
  775. if (change == PROXY_CHANGE_CLOSING) {
  776. /* if our proxy negotiation process involves closing and opening
  777. * new sockets, then we would want to intercept this closing
  778. * callback when we were expecting it. if we aren't anticipating
  779. * a socket close, then some error must have occurred. we'll
  780. * just pass those errors up to the backend.
  781. */
  782. return plug_closing(p->plug, p->closing_error_msg,
  783. p->closing_error_code,
  784. p->closing_calling_back);
  785. }
  786. if (change == PROXY_CHANGE_SENT) {
  787. /* some (or all) of what we wrote to the proxy was sent.
  788. * we don't do anything new, however, until we receive the
  789. * proxy's response. we might want to set a timer so we can
  790. * timeout the proxy negotiation after a while...
  791. */
  792. return 0;
  793. }
  794. if (change == PROXY_CHANGE_ACCEPTING) {
  795. /* we should _never_ see this, as we are using our socket to
  796. * connect to a proxy, not accepting inbound connections.
  797. * what should we do? close the socket with an appropriate
  798. * error message?
  799. */
  800. return plug_accepting(p->plug,
  801. p->accepting_constructor, p->accepting_ctx);
  802. }
  803. if (change == PROXY_CHANGE_RECEIVE) {
  804. /* we have received data from the underlying socket, which
  805. * we'll need to parse, process, and respond to appropriately.
  806. */
  807. if (p->state == 1) {
  808. /* initial response:
  809. * version number (1 byte) = 5
  810. * authentication method (1 byte)
  811. * authentication methods:
  812. * 0x00 = no authentication
  813. * 0x01 = GSSAPI
  814. * 0x02 = username/password
  815. * 0x03 = CHAP
  816. * 0xff = no acceptable methods
  817. */
  818. char data[2];
  819. if (bufchain_size(&p->pending_input_data) < 2)
  820. return 1; /* not got anything yet */
  821. /* get the response */
  822. bufchain_fetch(&p->pending_input_data, data, 2);
  823. if (data[0] != 5) {
  824. plug_closing(p->plug, "Proxy error: SOCKS proxy returned unexpected version",
  825. PROXY_ERROR_GENERAL, 0);
  826. return 1;
  827. }
  828. if (data[1] == 0x00) p->state = 2; /* no authentication needed */
  829. else if (data[1] == 0x01) p->state = 4; /* GSSAPI authentication */
  830. else if (data[1] == 0x02) p->state = 5; /* username/password authentication */
  831. else if (data[1] == 0x03) p->state = 6; /* CHAP authentication */
  832. else {
  833. plug_closing(p->plug, "Proxy error: SOCKS proxy did not accept our authentication",
  834. PROXY_ERROR_GENERAL, 0);
  835. return 1;
  836. }
  837. bufchain_consume(&p->pending_input_data, 2);
  838. }
  839. if (p->state == 7) {
  840. /* password authentication reply format:
  841. * version number (1 bytes) = 1
  842. * reply code (1 byte)
  843. * 0 = succeeded
  844. * >0 = failed
  845. */
  846. char data[2];
  847. if (bufchain_size(&p->pending_input_data) < 2)
  848. return 1; /* not got anything yet */
  849. /* get the response */
  850. bufchain_fetch(&p->pending_input_data, data, 2);
  851. if (data[0] != 1) {
  852. plug_closing(p->plug, "Proxy error: SOCKS password "
  853. "subnegotiation contained wrong version number",
  854. PROXY_ERROR_GENERAL, 0);
  855. return 1;
  856. }
  857. if (data[1] != 0) {
  858. plug_closing(p->plug, "Proxy error: SOCKS proxy refused"
  859. " password authentication",
  860. PROXY_ERROR_GENERAL, 0);
  861. return 1;
  862. }
  863. bufchain_consume(&p->pending_input_data, 2);
  864. p->state = 2; /* now proceed as authenticated */
  865. }
  866. if (p->state == 8) {
  867. int ret;
  868. ret = proxy_socks5_handlechap(p);
  869. if (ret) return ret;
  870. }
  871. if (p->state == 2) {
  872. /* request format:
  873. * version number (1 byte) = 5
  874. * command code (1 byte)
  875. * 1 = CONNECT
  876. * 2 = BIND
  877. * 3 = UDP ASSOCIATE
  878. * reserved (1 byte) = 0x00
  879. * address type (1 byte)
  880. * 1 = IPv4
  881. * 3 = domainname (first byte has length, no terminating null)
  882. * 4 = IPv6
  883. * dest. address (variable)
  884. * dest. port (2 bytes) [network order]
  885. */
  886. char command[512];
  887. int len;
  888. int type;
  889. type = sk_addrtype(p->remote_addr);
  890. if (type == ADDRTYPE_IPV4) {
  891. len = 10; /* 4 hdr + 4 addr + 2 trailer */
  892. command[3] = 1; /* IPv4 */
  893. sk_addrcopy(p->remote_addr, command+4);
  894. } else if (type == ADDRTYPE_IPV6) {
  895. len = 22; /* 4 hdr + 16 addr + 2 trailer */
  896. command[3] = 4; /* IPv6 */
  897. sk_addrcopy(p->remote_addr, command+4);
  898. } else {
  899. assert(type == ADDRTYPE_NAME);
  900. command[3] = 3;
  901. sk_getaddr(p->remote_addr, command+5, 256);
  902. command[4] = strlen(command+5);
  903. len = 7 + command[4]; /* 4 hdr, 1 len, N addr, 2 trailer */
  904. }
  905. command[0] = 5; /* version 5 */
  906. command[1] = 1; /* CONNECT command */
  907. command[2] = 0x00;
  908. /* port */
  909. command[len-2] = (char) (p->remote_port >> 8) & 0xff;
  910. command[len-1] = (char) p->remote_port & 0xff;
  911. sk_write(p->sub_socket, command, len);
  912. p->state = 3;
  913. return 1;
  914. }
  915. if (p->state == 3) {
  916. /* reply format:
  917. * version number (1 bytes) = 5
  918. * reply code (1 byte)
  919. * 0 = succeeded
  920. * 1 = general SOCKS server failure
  921. * 2 = connection not allowed by ruleset
  922. * 3 = network unreachable
  923. * 4 = host unreachable
  924. * 5 = connection refused
  925. * 6 = TTL expired
  926. * 7 = command not supported
  927. * 8 = address type not supported
  928. * reserved (1 byte) = x00
  929. * address type (1 byte)
  930. * 1 = IPv4
  931. * 3 = domainname (first byte has length, no terminating null)
  932. * 4 = IPv6
  933. * server bound address (variable)
  934. * server bound port (2 bytes) [network order]
  935. */
  936. char data[5];
  937. int len;
  938. /* First 5 bytes of packet are enough to tell its length. */
  939. if (bufchain_size(&p->pending_input_data) < 5)
  940. return 1; /* not got anything yet */
  941. /* get the response */
  942. bufchain_fetch(&p->pending_input_data, data, 5);
  943. if (data[0] != 5) {
  944. plug_closing(p->plug, "Proxy error: SOCKS proxy returned wrong version number",
  945. PROXY_ERROR_GENERAL, 0);
  946. return 1;
  947. }
  948. if (data[1] != 0) {
  949. char buf[256];
  950. strcpy(buf, "Proxy error: ");
  951. switch (data[1]) {
  952. case 1: strcat(buf, "General SOCKS server failure"); break;
  953. case 2: strcat(buf, "Connection not allowed by ruleset"); break;
  954. case 3: strcat(buf, "Network unreachable"); break;
  955. case 4: strcat(buf, "Host unreachable"); break;
  956. case 5: strcat(buf, "Connection refused"); break;
  957. case 6: strcat(buf, "TTL expired"); break;
  958. case 7: strcat(buf, "Command not supported"); break;
  959. case 8: strcat(buf, "Address type not supported"); break;
  960. default: sprintf(buf+strlen(buf),
  961. "Unrecognised SOCKS error code %d",
  962. data[1]);
  963. break;
  964. }
  965. plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);
  966. return 1;
  967. }
  968. /*
  969. * Eat the rest of the reply packet.
  970. */
  971. len = 6; /* first 4 bytes, last 2 */
  972. switch (data[3]) {
  973. case 1: len += 4; break; /* IPv4 address */
  974. case 4: len += 16; break;/* IPv6 address */
  975. case 3: len += (unsigned char)data[4]; break; /* domain name */
  976. default:
  977. plug_closing(p->plug, "Proxy error: SOCKS proxy returned "
  978. "unrecognised address format",
  979. PROXY_ERROR_GENERAL, 0);
  980. return 1;
  981. }
  982. if (bufchain_size(&p->pending_input_data) < len)
  983. return 1; /* not got whole reply yet */
  984. bufchain_consume(&p->pending_input_data, len);
  985. /* we're done */
  986. proxy_activate(p);
  987. return 1;
  988. }
  989. if (p->state == 4) {
  990. /* TODO: Handle GSSAPI authentication */
  991. plug_closing(p->plug, "Proxy error: We don't support GSSAPI authentication",
  992. PROXY_ERROR_GENERAL, 0);
  993. return 1;
  994. }
  995. if (p->state == 5) {
  996. char *username = conf_get_str(p->conf, CONF_proxy_username);
  997. char *password = conf_get_str(p->conf, CONF_proxy_password);
  998. if (username[0] || password[0]) {
  999. char userpwbuf[255 + 255 + 3];
  1000. int ulen, plen;
  1001. ulen = strlen(username);
  1002. if (ulen > 255) ulen = 255; if (ulen < 1) ulen = 1;
  1003. plen = strlen(password);
  1004. if (plen > 255) plen = 255; if (plen < 1) plen = 1;
  1005. userpwbuf[0] = 1; /* version number of subnegotiation */
  1006. userpwbuf[1] = ulen;
  1007. memcpy(userpwbuf+2, username, ulen);
  1008. userpwbuf[ulen+2] = plen;
  1009. memcpy(userpwbuf+ulen+3, password, plen);
  1010. sk_write(p->sub_socket, userpwbuf, ulen + plen + 3);
  1011. p->state = 7;
  1012. } else
  1013. plug_closing(p->plug, "Proxy error: Server chose "
  1014. "username/password authentication but we "
  1015. "didn't offer it!",
  1016. PROXY_ERROR_GENERAL, 0);
  1017. return 1;
  1018. }
  1019. if (p->state == 6) {
  1020. int ret;
  1021. ret = proxy_socks5_selectchap(p);
  1022. if (ret) return ret;
  1023. }
  1024. }
  1025. plug_closing(p->plug, "Proxy error: Unexpected proxy error",
  1026. PROXY_ERROR_UNEXPECTED, 0);
  1027. return 1;
  1028. }
  1029. /* ----------------------------------------------------------------------
  1030. * `Telnet' proxy type.
  1031. *
  1032. * (This is for ad-hoc proxies where you connect to the proxy's
  1033. * telnet port and send a command such as `connect host port'. The
  1034. * command is configurable, since this proxy type is typically not
  1035. * standardised or at all well-defined.)
  1036. */
  1037. char *format_telnet_command(SockAddr addr, int port, Conf *conf)
  1038. {
  1039. char *fmt = conf_get_str(conf, CONF_proxy_telnet_command);
  1040. char *ret = NULL;
  1041. int retlen = 0, retsize = 0;
  1042. int so = 0, eo = 0;
  1043. #define ENSURE(n) do { \
  1044. if (retsize < retlen + n) { \
  1045. retsize = retlen + n + 512; \
  1046. ret = sresize(ret, retsize, char); \
  1047. } \
  1048. } while (0)
  1049. /* we need to escape \\, \%, \r, \n, \t, \x??, \0???,
  1050. * %%, %host, %port, %user, and %pass
  1051. */
  1052. while (fmt[eo] != 0) {
  1053. /* scan forward until we hit end-of-line,
  1054. * or an escape character (\ or %) */
  1055. while (fmt[eo] != 0 && fmt[eo] != '%' && fmt[eo] != '\\')
  1056. eo++;
  1057. /* if we hit eol, break out of our escaping loop */
  1058. if (fmt[eo] == 0) break;
  1059. /* if there was any unescaped text before the escape
  1060. * character, send that now */
  1061. if (eo != so) {
  1062. ENSURE(eo - so);
  1063. memcpy(ret + retlen, fmt + so, eo - so);
  1064. retlen += eo - so;
  1065. }
  1066. so = eo++;
  1067. /* if the escape character was the last character of
  1068. * the line, we'll just stop and send it. */
  1069. if (fmt[eo] == 0) break;
  1070. if (fmt[so] == '\\') {
  1071. /* we recognize \\, \%, \r, \n, \t, \x??.
  1072. * anything else, we just send unescaped (including the \).
  1073. */
  1074. switch (fmt[eo]) {
  1075. case '\\':
  1076. ENSURE(1);
  1077. ret[retlen++] = '\\';
  1078. eo++;
  1079. break;
  1080. case '%':
  1081. ENSURE(1);
  1082. ret[retlen++] = '%';
  1083. eo++;
  1084. break;
  1085. case 'r':
  1086. ENSURE(1);
  1087. ret[retlen++] = '\r';
  1088. eo++;
  1089. break;
  1090. case 'n':
  1091. ENSURE(1);
  1092. ret[retlen++] = '\n';
  1093. eo++;
  1094. break;
  1095. case 't':
  1096. ENSURE(1);
  1097. ret[retlen++] = '\t';
  1098. eo++;
  1099. break;
  1100. case 'x':
  1101. case 'X':
  1102. {
  1103. /* escaped hexadecimal value (ie. \xff) */
  1104. unsigned char v = 0;
  1105. int i = 0;
  1106. for (;;) {
  1107. eo++;
  1108. if (fmt[eo] >= '0' && fmt[eo] <= '9')
  1109. v += fmt[eo] - '0';
  1110. else if (fmt[eo] >= 'a' && fmt[eo] <= 'f')
  1111. v += fmt[eo] - 'a' + 10;
  1112. else if (fmt[eo] >= 'A' && fmt[eo] <= 'F')
  1113. v += fmt[eo] - 'A' + 10;
  1114. else {
  1115. /* non hex character, so we abort and just
  1116. * send the whole thing unescaped (including \x)
  1117. */
  1118. ENSURE(1);
  1119. ret[retlen++] = '\\';
  1120. eo = so + 1;
  1121. break;
  1122. }
  1123. /* we only extract two hex characters */
  1124. if (i == 1) {
  1125. ENSURE(1);
  1126. ret[retlen++] = v;
  1127. eo++;
  1128. break;
  1129. }
  1130. i++;
  1131. v <<= 4;
  1132. }
  1133. }
  1134. break;
  1135. default:
  1136. ENSURE(2);
  1137. memcpy(ret+retlen, fmt + so, 2);
  1138. retlen += 2;
  1139. eo++;
  1140. break;
  1141. }
  1142. } else {
  1143. /* % escape. we recognize %%, %host, %port, %user, %pass.
  1144. * %proxyhost, %proxyport. Anything else we just send
  1145. * unescaped (including the %).
  1146. */
  1147. if (fmt[eo] == '%') {
  1148. ENSURE(1);
  1149. ret[retlen++] = '%';
  1150. eo++;
  1151. }
  1152. else if (strnicmp(fmt + eo, "host", 4) == 0) {
  1153. char dest[512];
  1154. int destlen;
  1155. sk_getaddr(addr, dest, lenof(dest));
  1156. destlen = strlen(dest);
  1157. ENSURE(destlen);
  1158. memcpy(ret+retlen, dest, destlen);
  1159. retlen += destlen;
  1160. eo += 4;
  1161. }
  1162. else if (strnicmp(fmt + eo, "port", 4) == 0) {
  1163. char portstr[8], portlen;
  1164. portlen = sprintf(portstr, "%i", port);
  1165. ENSURE(portlen);
  1166. memcpy(ret + retlen, portstr, portlen);
  1167. retlen += portlen;
  1168. eo += 4;
  1169. }
  1170. else if (strnicmp(fmt + eo, "user", 4) == 0) {
  1171. char *username = conf_get_str(conf, CONF_proxy_username);
  1172. int userlen = strlen(username);
  1173. ENSURE(userlen);
  1174. memcpy(ret+retlen, username, userlen);
  1175. retlen += userlen;
  1176. eo += 4;
  1177. }
  1178. else if (strnicmp(fmt + eo, "pass", 4) == 0) {
  1179. char *password = conf_get_str(conf, CONF_proxy_password);
  1180. int passlen = strlen(password);
  1181. ENSURE(passlen);
  1182. memcpy(ret+retlen, password, passlen);
  1183. retlen += passlen;
  1184. eo += 4;
  1185. }
  1186. else if (strnicmp(fmt + eo, "proxyhost", 9) == 0) {
  1187. char *host = conf_get_str(conf, CONF_proxy_host);
  1188. int phlen = strlen(host);
  1189. ENSURE(phlen);
  1190. memcpy(ret+retlen, host, phlen);
  1191. retlen += phlen;
  1192. eo += 9;
  1193. }
  1194. else if (strnicmp(fmt + eo, "proxyport", 9) == 0) {
  1195. int port = conf_get_int(conf, CONF_proxy_port);
  1196. char pport[50];
  1197. int pplen;
  1198. sprintf(pport, "%d", port);
  1199. pplen = strlen(pport);
  1200. ENSURE(pplen);
  1201. memcpy(ret+retlen, pport, pplen);
  1202. retlen += pplen;
  1203. eo += 9;
  1204. }
  1205. else {
  1206. /* we don't escape this, so send the % now, and
  1207. * don't advance eo, so that we'll consider the
  1208. * text immediately following the % as unescaped.
  1209. */
  1210. ENSURE(1);
  1211. ret[retlen++] = '%';
  1212. }
  1213. }
  1214. /* resume scanning for additional escapes after this one. */
  1215. so = eo;
  1216. }
  1217. /* if there is any unescaped text at the end of the line, send it */
  1218. if (eo != so) {
  1219. ENSURE(eo - so);
  1220. memcpy(ret + retlen, fmt + so, eo - so);
  1221. retlen += eo - so;
  1222. }
  1223. ENSURE(1);
  1224. ret[retlen] = '\0';
  1225. return ret;
  1226. #undef ENSURE
  1227. }
  1228. int proxy_telnet_negotiate (Proxy_Socket p, int change)
  1229. {
  1230. if (p->state == PROXY_CHANGE_NEW) {
  1231. char *formatted_cmd;
  1232. formatted_cmd = format_telnet_command(p->remote_addr, p->remote_port,
  1233. p->conf);
  1234. sk_write(p->sub_socket, formatted_cmd, strlen(formatted_cmd));
  1235. sfree(formatted_cmd);
  1236. p->state = 1;
  1237. return 0;
  1238. }
  1239. if (change == PROXY_CHANGE_CLOSING) {
  1240. /* if our proxy negotiation process involves closing and opening
  1241. * new sockets, then we would want to intercept this closing
  1242. * callback when we were expecting it. if we aren't anticipating
  1243. * a socket close, then some error must have occurred. we'll
  1244. * just pass those errors up to the backend.
  1245. */
  1246. return plug_closing(p->plug, p->closing_error_msg,
  1247. p->closing_error_code,
  1248. p->closing_calling_back);
  1249. }
  1250. if (change == PROXY_CHANGE_SENT) {
  1251. /* some (or all) of what we wrote to the proxy was sent.
  1252. * we don't do anything new, however, until we receive the
  1253. * proxy's response. we might want to set a timer so we can
  1254. * timeout the proxy negotiation after a while...
  1255. */
  1256. return 0;
  1257. }
  1258. if (change == PROXY_CHANGE_ACCEPTING) {
  1259. /* we should _never_ see this, as we are using our socket to
  1260. * connect to a proxy, not accepting inbound connections.
  1261. * what should we do? close the socket with an appropriate
  1262. * error message?
  1263. */
  1264. return plug_accepting(p->plug,
  1265. p->accepting_constructor, p->accepting_ctx);
  1266. }
  1267. if (change == PROXY_CHANGE_RECEIVE) {
  1268. /* we have received data from the underlying socket, which
  1269. * we'll need to parse, process, and respond to appropriately.
  1270. */
  1271. /* we're done */
  1272. proxy_activate(p);
  1273. /* proxy activate will have dealt with
  1274. * whatever is left of the buffer */
  1275. return 1;
  1276. }
  1277. plug_closing(p->plug, "Proxy error: Unexpected proxy error",
  1278. PROXY_ERROR_UNEXPECTED, 0);
  1279. return 1;
  1280. }