PageRenderTime 44ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/src/TortoisePlink/PROXY.C

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