1
0

proxy.c 42 KB

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