PROXY.C 41 KB

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