proto_socks.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #include "or.h"
  7. #include "addressmap.h"
  8. #include "buffers.h"
  9. #include "control.h"
  10. #include "config.h"
  11. #include "ext_orport.h"
  12. #include "proto_socks.h"
  13. #include "reasons.h"
  14. static void socks_request_set_socks5_error(socks_request_t *req,
  15. socks5_reply_status_t reason);
  16. static int parse_socks(const char *data, size_t datalen, socks_request_t *req,
  17. int log_sockstype, int safe_socks, ssize_t *drain_out,
  18. size_t *want_length_out);
  19. static int parse_socks_client(const uint8_t *data, size_t datalen,
  20. int state, char **reason,
  21. ssize_t *drain_out);
  22. /**
  23. * Wait this many seconds before warning the user about using SOCKS unsafely
  24. * again. */
  25. #define SOCKS_WARN_INTERVAL 5
  26. /** Warn that the user application has made an unsafe socks request using
  27. * protocol <b>socks_protocol</b> on port <b>port</b>. Don't warn more than
  28. * once per SOCKS_WARN_INTERVAL, unless <b>safe_socks</b> is set. */
  29. static void
  30. log_unsafe_socks_warning(int socks_protocol, const char *address,
  31. uint16_t port, int safe_socks)
  32. {
  33. static ratelim_t socks_ratelim = RATELIM_INIT(SOCKS_WARN_INTERVAL);
  34. if (safe_socks) {
  35. log_fn_ratelim(&socks_ratelim, LOG_WARN, LD_APP,
  36. "Your application (using socks%d to port %d) is giving "
  37. "Tor only an IP address. Applications that do DNS resolves "
  38. "themselves may leak information. Consider using Socks4A "
  39. "(e.g. via privoxy or socat) instead. For more information, "
  40. "please see https://wiki.torproject.org/TheOnionRouter/"
  41. "TorFAQ#SOCKSAndDNS.%s",
  42. socks_protocol,
  43. (int)port,
  44. safe_socks ? " Rejecting." : "");
  45. }
  46. control_event_client_status(LOG_WARN,
  47. "DANGEROUS_SOCKS PROTOCOL=SOCKS%d ADDRESS=%s:%d",
  48. socks_protocol, address, (int)port);
  49. }
  50. /** Do not attempt to parse socks messages longer than this. This value is
  51. * actually significantly higher than the longest possible socks message. */
  52. #define MAX_SOCKS_MESSAGE_LEN 512
  53. /** Return a new socks_request_t. */
  54. socks_request_t *
  55. socks_request_new(void)
  56. {
  57. return tor_malloc_zero(sizeof(socks_request_t));
  58. }
  59. /** Free all storage held in the socks_request_t <b>req</b>. */
  60. void
  61. socks_request_free_(socks_request_t *req)
  62. {
  63. if (!req)
  64. return;
  65. if (req->username) {
  66. memwipe(req->username, 0x10, req->usernamelen);
  67. tor_free(req->username);
  68. }
  69. if (req->password) {
  70. memwipe(req->password, 0x04, req->passwordlen);
  71. tor_free(req->password);
  72. }
  73. memwipe(req, 0xCC, sizeof(socks_request_t));
  74. tor_free(req);
  75. }
  76. /** There is a (possibly incomplete) socks handshake on <b>buf</b>, of one
  77. * of the forms
  78. * - socks4: "socksheader username\\0"
  79. * - socks4a: "socksheader username\\0 destaddr\\0"
  80. * - socks5 phase one: "version #methods methods"
  81. * - socks5 phase two: "version command 0 addresstype..."
  82. * If it's a complete and valid handshake, and destaddr fits in
  83. * MAX_SOCKS_ADDR_LEN bytes, then pull the handshake off the buf,
  84. * assign to <b>req</b>, and return 1.
  85. *
  86. * If it's invalid or too big, return -1.
  87. *
  88. * Else it's not all there yet, leave buf alone and return 0.
  89. *
  90. * If you want to specify the socks reply, write it into <b>req->reply</b>
  91. * and set <b>req->replylen</b>, else leave <b>req->replylen</b> alone.
  92. *
  93. * If <b>log_sockstype</b> is non-zero, then do a notice-level log of whether
  94. * the connection is possibly leaking DNS requests locally or not.
  95. *
  96. * If <b>safe_socks</b> is true, then reject unsafe socks protocols.
  97. *
  98. * If returning 0 or -1, <b>req->address</b> and <b>req->port</b> are
  99. * undefined.
  100. */
  101. int
  102. fetch_from_buf_socks(buf_t *buf, socks_request_t *req,
  103. int log_sockstype, int safe_socks)
  104. {
  105. int res;
  106. ssize_t n_drain;
  107. size_t want_length = 128;
  108. const char *head = NULL;
  109. size_t datalen = 0;
  110. if (buf_datalen(buf) < 2) /* version and another byte */
  111. return 0;
  112. do {
  113. n_drain = 0;
  114. buf_pullup(buf, want_length, &head, &datalen);
  115. tor_assert(head && datalen >= 2);
  116. want_length = 0;
  117. res = parse_socks(head, datalen, req, log_sockstype,
  118. safe_socks, &n_drain, &want_length);
  119. if (n_drain < 0)
  120. buf_clear(buf);
  121. else if (n_drain > 0)
  122. buf_drain(buf, n_drain);
  123. } while (res == 0 && head && want_length < buf_datalen(buf) &&
  124. buf_datalen(buf) >= 2);
  125. return res;
  126. }
  127. /** Create a SOCKS5 reply message with <b>reason</b> in its REP field and
  128. * have Tor send it as error response to <b>req</b>.
  129. */
  130. static void
  131. socks_request_set_socks5_error(socks_request_t *req,
  132. socks5_reply_status_t reason)
  133. {
  134. req->replylen = 10;
  135. memset(req->reply,0,10);
  136. req->reply[0] = 0x05; // VER field.
  137. req->reply[1] = reason; // REP field.
  138. req->reply[3] = 0x01; // ATYP field.
  139. }
  140. static const char SOCKS_PROXY_IS_NOT_AN_HTTP_PROXY_MSG[] =
  141. "HTTP/1.0 501 Tor is not an HTTP Proxy\r\n"
  142. "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
  143. "<html>\n"
  144. "<head>\n"
  145. "<title>This is a SOCKS Proxy, Not An HTTP Proxy</title>\n"
  146. "</head>\n"
  147. "<body>\n"
  148. "<h1>This is a SOCKs proxy, not an HTTP proxy.</h1>\n"
  149. "<p>\n"
  150. "It appears you have configured your web browser to use this Tor port as\n"
  151. "an HTTP proxy.\n"
  152. "</p><p>\n"
  153. "This is not correct: This port is configured as a SOCKS proxy, not\n"
  154. "an HTTP proxy. If you need an HTTP proxy tunnel, use the HTTPTunnelPort\n"
  155. "configuration option in place of, or in addition to, SOCKSPort.\n"
  156. "Please configure your client accordingly.\n"
  157. "</p>\n"
  158. "<p>\n"
  159. "See <a href=\"https://www.torproject.org/documentation.html\">"
  160. "https://www.torproject.org/documentation.html</a> for more "
  161. "information.\n"
  162. "</p>\n"
  163. "</body>\n"
  164. "</html>\n";
  165. /** Implementation helper to implement fetch_from_*_socks. Instead of looking
  166. * at a buffer's contents, we look at the <b>datalen</b> bytes of data in
  167. * <b>data</b>. Instead of removing data from the buffer, we set
  168. * <b>drain_out</b> to the amount of data that should be removed (or -1 if the
  169. * buffer should be cleared). Instead of pulling more data into the first
  170. * chunk of the buffer, we set *<b>want_length_out</b> to the number of bytes
  171. * we'd like to see in the input buffer, if they're available. */
  172. static int
  173. parse_socks(const char *data, size_t datalen, socks_request_t *req,
  174. int log_sockstype, int safe_socks, ssize_t *drain_out,
  175. size_t *want_length_out)
  176. {
  177. unsigned int len;
  178. char tmpbuf[TOR_ADDR_BUF_LEN+1];
  179. tor_addr_t destaddr;
  180. uint32_t destip;
  181. uint8_t socksver;
  182. char *next, *startaddr;
  183. unsigned char usernamelen, passlen;
  184. struct in_addr in;
  185. if (datalen < 2) {
  186. /* We always need at least 2 bytes. */
  187. *want_length_out = 2;
  188. return 0;
  189. }
  190. if (req->socks_version == 5 && !req->got_auth) {
  191. /* See if we have received authentication. Strictly speaking, we should
  192. also check whether we actually negotiated username/password
  193. authentication. But some broken clients will send us authentication
  194. even if we negotiated SOCKS_NO_AUTH. */
  195. if (*data == 1) { /* username/pass version 1 */
  196. /* Format is: authversion [1 byte] == 1
  197. usernamelen [1 byte]
  198. username [usernamelen bytes]
  199. passlen [1 byte]
  200. password [passlen bytes] */
  201. usernamelen = (unsigned char)*(data + 1);
  202. if (datalen < 2u + usernamelen + 1u) {
  203. *want_length_out = 2u + usernamelen + 1u;
  204. return 0;
  205. }
  206. passlen = (unsigned char)*(data + 2u + usernamelen);
  207. if (datalen < 2u + usernamelen + 1u + passlen) {
  208. *want_length_out = 2u + usernamelen + 1u + passlen;
  209. return 0;
  210. }
  211. req->replylen = 2; /* 2 bytes of response */
  212. req->reply[0] = 1; /* authversion == 1 */
  213. req->reply[1] = 0; /* authentication successful */
  214. log_debug(LD_APP,
  215. "socks5: Accepted username/password without checking.");
  216. if (usernamelen) {
  217. req->username = tor_memdup(data+2u, usernamelen);
  218. req->usernamelen = usernamelen;
  219. }
  220. if (passlen) {
  221. req->password = tor_memdup(data+3u+usernamelen, passlen);
  222. req->passwordlen = passlen;
  223. }
  224. *drain_out = 2u + usernamelen + 1u + passlen;
  225. req->got_auth = 1;
  226. *want_length_out = 7; /* Minimal socks5 command. */
  227. return 0;
  228. } else if (req->auth_type == SOCKS_USER_PASS) {
  229. /* unknown version byte */
  230. log_warn(LD_APP, "Socks5 username/password version %d not recognized; "
  231. "rejecting.", (int)*data);
  232. return -1;
  233. }
  234. }
  235. socksver = *data;
  236. switch (socksver) { /* which version of socks? */
  237. case 5: /* socks5 */
  238. if (req->socks_version != 5) { /* we need to negotiate a method */
  239. unsigned char nummethods = (unsigned char)*(data+1);
  240. int have_user_pass, have_no_auth;
  241. int r=0;
  242. tor_assert(!req->socks_version);
  243. if (datalen < 2u+nummethods) {
  244. *want_length_out = 2u+nummethods;
  245. return 0;
  246. }
  247. if (!nummethods)
  248. return -1;
  249. req->replylen = 2; /* 2 bytes of response */
  250. req->reply[0] = 5; /* socks5 reply */
  251. have_user_pass = (memchr(data+2, SOCKS_USER_PASS, nummethods) !=NULL);
  252. have_no_auth = (memchr(data+2, SOCKS_NO_AUTH, nummethods) !=NULL);
  253. if (have_user_pass && !(have_no_auth && req->socks_prefer_no_auth)) {
  254. req->auth_type = SOCKS_USER_PASS;
  255. req->reply[1] = SOCKS_USER_PASS; /* tell client to use "user/pass"
  256. auth method */
  257. req->socks_version = 5; /* remember we've already negotiated auth */
  258. log_debug(LD_APP,"socks5: accepted method 2 (username/password)");
  259. r=0;
  260. } else if (have_no_auth) {
  261. req->reply[1] = SOCKS_NO_AUTH; /* tell client to use "none" auth
  262. method */
  263. req->socks_version = 5; /* remember we've already negotiated auth */
  264. log_debug(LD_APP,"socks5: accepted method 0 (no authentication)");
  265. r=0;
  266. } else {
  267. log_warn(LD_APP,
  268. "socks5: offered methods don't include 'no auth' or "
  269. "username/password. Rejecting.");
  270. req->reply[1] = '\xFF'; /* reject all methods */
  271. r=-1;
  272. }
  273. /* Remove packet from buf. Some SOCKS clients will have sent extra
  274. * junk at this point; let's hope it's an authentication message. */
  275. *drain_out = 2u + nummethods;
  276. return r;
  277. }
  278. if (req->auth_type != SOCKS_NO_AUTH && !req->got_auth) {
  279. log_warn(LD_APP,
  280. "socks5: negotiated authentication, but none provided");
  281. return -1;
  282. }
  283. /* we know the method; read in the request */
  284. log_debug(LD_APP,"socks5: checking request");
  285. if (datalen < 7) {/* basic info plus >=1 for addr plus 2 for port */
  286. *want_length_out = 7;
  287. return 0; /* not yet */
  288. }
  289. req->command = (unsigned char) *(data+1);
  290. if (req->command != SOCKS_COMMAND_CONNECT &&
  291. req->command != SOCKS_COMMAND_RESOLVE &&
  292. req->command != SOCKS_COMMAND_RESOLVE_PTR) {
  293. /* not a connect or resolve or a resolve_ptr? we don't support it. */
  294. socks_request_set_socks5_error(req,SOCKS5_COMMAND_NOT_SUPPORTED);
  295. log_warn(LD_APP,"socks5: command %d not recognized. Rejecting.",
  296. req->command);
  297. return -1;
  298. }
  299. switch (*(data+3)) { /* address type */
  300. case 1: /* IPv4 address */
  301. case 4: /* IPv6 address */ {
  302. const int is_v6 = *(data+3) == 4;
  303. const unsigned addrlen = is_v6 ? 16 : 4;
  304. log_debug(LD_APP,"socks5: ipv4 address type");
  305. if (datalen < 6+addrlen) {/* ip/port there? */
  306. *want_length_out = 6+addrlen;
  307. return 0; /* not yet */
  308. }
  309. if (is_v6)
  310. tor_addr_from_ipv6_bytes(&destaddr, data+4);
  311. else
  312. tor_addr_from_ipv4n(&destaddr, get_uint32(data+4));
  313. tor_addr_to_str(tmpbuf, &destaddr, sizeof(tmpbuf), 1);
  314. if (BUG(strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN)) {
  315. /* LCOV_EXCL_START -- This branch is unreachable, given the
  316. * size of tmpbuf and the actual value of MAX_SOCKS_ADDR_LEN */
  317. socks_request_set_socks5_error(req, SOCKS5_GENERAL_ERROR);
  318. log_warn(LD_APP,
  319. "socks5 IP takes %d bytes, which doesn't fit in %d. "
  320. "Rejecting.",
  321. (int)strlen(tmpbuf)+1,(int)MAX_SOCKS_ADDR_LEN);
  322. return -1;
  323. /* LCOV_EXCL_STOP */
  324. }
  325. strlcpy(req->address,tmpbuf,sizeof(req->address));
  326. req->port = ntohs(get_uint16(data+4+addrlen));
  327. *drain_out = 6+addrlen;
  328. if (req->command != SOCKS_COMMAND_RESOLVE_PTR &&
  329. !addressmap_have_mapping(req->address,0)) {
  330. log_unsafe_socks_warning(5, req->address, req->port, safe_socks);
  331. if (safe_socks) {
  332. socks_request_set_socks5_error(req, SOCKS5_NOT_ALLOWED);
  333. return -1;
  334. }
  335. }
  336. return 1;
  337. }
  338. case 3: /* fqdn */
  339. log_debug(LD_APP,"socks5: fqdn address type");
  340. if (req->command == SOCKS_COMMAND_RESOLVE_PTR) {
  341. socks_request_set_socks5_error(req,
  342. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED);
  343. log_warn(LD_APP, "socks5 received RESOLVE_PTR command with "
  344. "hostname type. Rejecting.");
  345. return -1;
  346. }
  347. len = (unsigned char)*(data+4);
  348. if (datalen < 7+len) { /* addr/port there? */
  349. *want_length_out = 7+len;
  350. return 0; /* not yet */
  351. }
  352. if (BUG(len+1 > MAX_SOCKS_ADDR_LEN)) {
  353. /* LCOV_EXCL_START -- unreachable, since len is at most 255,
  354. * and MAX_SOCKS_ADDR_LEN is 256. */
  355. socks_request_set_socks5_error(req, SOCKS5_GENERAL_ERROR);
  356. log_warn(LD_APP,
  357. "socks5 hostname is %d bytes, which doesn't fit in "
  358. "%d. Rejecting.", len+1,MAX_SOCKS_ADDR_LEN);
  359. return -1;
  360. /* LCOV_EXCL_STOP */
  361. }
  362. memcpy(req->address,data+5,len);
  363. req->address[len] = 0;
  364. req->port = ntohs(get_uint16(data+5+len));
  365. *drain_out = 5+len+2;
  366. if (!string_is_valid_dest(req->address)) {
  367. socks_request_set_socks5_error(req, SOCKS5_GENERAL_ERROR);
  368. log_warn(LD_PROTOCOL,
  369. "Your application (using socks5 to port %d) gave Tor "
  370. "a malformed hostname: %s. Rejecting the connection.",
  371. req->port, escaped_safe_str_client(req->address));
  372. return -1;
  373. }
  374. if (log_sockstype)
  375. log_notice(LD_APP,
  376. "Your application (using socks5 to port %d) instructed "
  377. "Tor to take care of the DNS resolution itself if "
  378. "necessary. This is good.", req->port);
  379. return 1;
  380. default: /* unsupported */
  381. socks_request_set_socks5_error(req,
  382. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED);
  383. log_warn(LD_APP,"socks5: unsupported address type %d. Rejecting.",
  384. (int) *(data+3));
  385. return -1;
  386. }
  387. tor_assert(0);
  388. break;
  389. case 4: { /* socks4 */
  390. enum {socks4, socks4a} socks4_prot = socks4a;
  391. const char *authstart, *authend;
  392. /* http://ss5.sourceforge.net/socks4.protocol.txt */
  393. /* http://ss5.sourceforge.net/socks4A.protocol.txt */
  394. req->socks_version = 4;
  395. if (datalen < SOCKS4_NETWORK_LEN) {/* basic info available? */
  396. *want_length_out = SOCKS4_NETWORK_LEN;
  397. return 0; /* not yet */
  398. }
  399. // buf_pullup(buf, 1280);
  400. req->command = (unsigned char) *(data+1);
  401. if (req->command != SOCKS_COMMAND_CONNECT &&
  402. req->command != SOCKS_COMMAND_RESOLVE) {
  403. /* not a connect or resolve? we don't support it. (No resolve_ptr with
  404. * socks4.) */
  405. log_warn(LD_APP,"socks4: command %d not recognized. Rejecting.",
  406. req->command);
  407. return -1;
  408. }
  409. req->port = ntohs(get_uint16(data+2));
  410. destip = ntohl(get_uint32(data+4));
  411. if ((!req->port && req->command!=SOCKS_COMMAND_RESOLVE) || !destip) {
  412. log_warn(LD_APP,"socks4: Port or DestIP is zero. Rejecting.");
  413. return -1;
  414. }
  415. if (destip >> 8) {
  416. log_debug(LD_APP,"socks4: destip not in form 0.0.0.x.");
  417. in.s_addr = htonl(destip);
  418. tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
  419. if (BUG(strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN)) {
  420. /* LCOV_EXCL_START -- This branch is unreachable, given the
  421. * size of tmpbuf and the actual value of MAX_SOCKS_ADDR_LEN */
  422. log_debug(LD_APP,"socks4 addr (%d bytes) too long. Rejecting.",
  423. (int)strlen(tmpbuf));
  424. return -1;
  425. /* LCOV_EXCL_STOP */
  426. }
  427. log_debug(LD_APP,
  428. "socks4: successfully read destip (%s)",
  429. safe_str_client(tmpbuf));
  430. socks4_prot = socks4;
  431. }
  432. authstart = data + SOCKS4_NETWORK_LEN;
  433. next = memchr(authstart, 0,
  434. datalen-SOCKS4_NETWORK_LEN);
  435. if (!next) {
  436. if (datalen >= 1024) {
  437. log_debug(LD_APP, "Socks4 user name too long; rejecting.");
  438. return -1;
  439. }
  440. log_debug(LD_APP,"socks4: Username not here yet.");
  441. *want_length_out = datalen+1024; /* More than we need, but safe */
  442. return 0;
  443. }
  444. authend = next;
  445. tor_assert(next < data+datalen);
  446. startaddr = NULL;
  447. if (socks4_prot != socks4a &&
  448. !addressmap_have_mapping(tmpbuf,0)) {
  449. log_unsafe_socks_warning(4, tmpbuf, req->port, safe_socks);
  450. if (safe_socks)
  451. return -1;
  452. }
  453. if (socks4_prot == socks4a) {
  454. if (next+1 == data+datalen) {
  455. log_debug(LD_APP,"socks4: No part of destaddr here yet.");
  456. *want_length_out = datalen + 1024; /* More than we need, but safe */
  457. return 0;
  458. }
  459. startaddr = next+1;
  460. next = memchr(startaddr, 0, data + datalen - startaddr);
  461. if (!next) {
  462. if (datalen >= 1024) {
  463. log_debug(LD_APP,"socks4: Destaddr too long.");
  464. return -1;
  465. }
  466. log_debug(LD_APP,"socks4: Destaddr not all here yet.");
  467. *want_length_out = datalen + 1024; /* More than we need, but safe */
  468. return 0;
  469. }
  470. if (MAX_SOCKS_ADDR_LEN <= next-startaddr) {
  471. log_warn(LD_APP,"socks4: Destaddr too long. Rejecting.");
  472. return -1;
  473. }
  474. // tor_assert(next < buf->cur+buf_datalen(buf));
  475. if (log_sockstype)
  476. log_notice(LD_APP,
  477. "Your application (using socks4a to port %d) instructed "
  478. "Tor to take care of the DNS resolution itself if "
  479. "necessary. This is good.", req->port);
  480. }
  481. log_debug(LD_APP,"socks4: Everything is here. Success.");
  482. strlcpy(req->address, startaddr ? startaddr : tmpbuf,
  483. sizeof(req->address));
  484. if (!string_is_valid_dest(req->address)) {
  485. log_warn(LD_PROTOCOL,
  486. "Your application (using socks4 to port %d) gave Tor "
  487. "a malformed hostname: %s. Rejecting the connection.",
  488. req->port, escaped_safe_str_client(req->address));
  489. return -1;
  490. }
  491. if (authend != authstart) {
  492. req->got_auth = 1;
  493. req->usernamelen = authend - authstart;
  494. req->username = tor_memdup(authstart, authend - authstart);
  495. }
  496. /* next points to the final \0 on inbuf */
  497. *drain_out = next - data + 1;
  498. return 1;
  499. }
  500. case 'G': /* get */
  501. case 'H': /* head */
  502. case 'P': /* put/post */
  503. case 'C': /* connect */
  504. strlcpy((char*)req->reply, SOCKS_PROXY_IS_NOT_AN_HTTP_PROXY_MSG,
  505. MAX_SOCKS_REPLY_LEN);
  506. req->replylen = strlen((char*)req->reply)+1;
  507. /* fall through */
  508. default: /* version is not socks4 or socks5 */
  509. log_warn(LD_APP,
  510. "Socks version %d not recognized. (This port is not an "
  511. "HTTP proxy; did you want to use HTTPTunnelPort?)",
  512. *(data));
  513. {
  514. /* Tell the controller the first 8 bytes. */
  515. char *tmp = tor_strndup(data, datalen < 8 ? datalen : 8);
  516. control_event_client_status(LOG_WARN,
  517. "SOCKS_UNKNOWN_PROTOCOL DATA=\"%s\"",
  518. escaped(tmp));
  519. tor_free(tmp);
  520. }
  521. return -1;
  522. }
  523. }
  524. /** Inspect a reply from SOCKS server stored in <b>buf</b> according
  525. * to <b>state</b>, removing the protocol data upon success. Return 0 on
  526. * incomplete response, 1 on success and -1 on error, in which case
  527. * <b>reason</b> is set to a descriptive message (free() when finished
  528. * with it).
  529. *
  530. * As a special case, 2 is returned when user/pass is required
  531. * during SOCKS5 handshake and user/pass is configured.
  532. */
  533. int
  534. fetch_from_buf_socks_client(buf_t *buf, int state, char **reason)
  535. {
  536. ssize_t drain = 0;
  537. int r;
  538. const char *head = NULL;
  539. size_t datalen = 0;
  540. if (buf_datalen(buf) < 2)
  541. return 0;
  542. buf_pullup(buf, MAX_SOCKS_MESSAGE_LEN, &head, &datalen);
  543. tor_assert(head && datalen >= 2);
  544. r = parse_socks_client((uint8_t*)head, datalen,
  545. state, reason, &drain);
  546. if (drain > 0)
  547. buf_drain(buf, drain);
  548. else if (drain < 0)
  549. buf_clear(buf);
  550. return r;
  551. }
  552. /** Implementation logic for fetch_from_*_socks_client. */
  553. static int
  554. parse_socks_client(const uint8_t *data, size_t datalen,
  555. int state, char **reason,
  556. ssize_t *drain_out)
  557. {
  558. unsigned int addrlen;
  559. *drain_out = 0;
  560. if (datalen < 2)
  561. return 0;
  562. switch (state) {
  563. case PROXY_SOCKS4_WANT_CONNECT_OK:
  564. /* Wait for the complete response */
  565. if (datalen < 8)
  566. return 0;
  567. if (data[1] != 0x5a) {
  568. *reason = tor_strdup(socks4_response_code_to_string(data[1]));
  569. return -1;
  570. }
  571. /* Success */
  572. *drain_out = 8;
  573. return 1;
  574. case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
  575. /* we don't have any credentials */
  576. if (data[1] != 0x00) {
  577. *reason = tor_strdup("server doesn't support any of our "
  578. "available authentication methods");
  579. return -1;
  580. }
  581. log_info(LD_NET, "SOCKS 5 client: continuing without authentication");
  582. *drain_out = -1;
  583. return 1;
  584. case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
  585. /* we have a username and password. return 1 if we can proceed without
  586. * providing authentication, or 2 otherwise. */
  587. switch (data[1]) {
  588. case 0x00:
  589. log_info(LD_NET, "SOCKS 5 client: we have auth details but server "
  590. "doesn't require authentication.");
  591. *drain_out = -1;
  592. return 1;
  593. case 0x02:
  594. log_info(LD_NET, "SOCKS 5 client: need authentication.");
  595. *drain_out = -1;
  596. return 2;
  597. /* fall through */
  598. }
  599. *reason = tor_strdup("server doesn't support any of our available "
  600. "authentication methods");
  601. return -1;
  602. case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
  603. /* handle server reply to rfc1929 authentication */
  604. if (data[1] != 0x00) {
  605. *reason = tor_strdup("authentication failed");
  606. return -1;
  607. }
  608. log_info(LD_NET, "SOCKS 5 client: authentication successful.");
  609. *drain_out = -1;
  610. return 1;
  611. case PROXY_SOCKS5_WANT_CONNECT_OK:
  612. /* response is variable length. BND.ADDR, etc, isn't needed
  613. * (don't bother with buf_pullup()), but make sure to eat all
  614. * the data used */
  615. /* wait for address type field to arrive */
  616. if (datalen < 4)
  617. return 0;
  618. switch (data[3]) {
  619. case 0x01: /* ip4 */
  620. addrlen = 4;
  621. break;
  622. case 0x04: /* ip6 */
  623. addrlen = 16;
  624. break;
  625. case 0x03: /* fqdn (can this happen here?) */
  626. if (datalen < 5)
  627. return 0;
  628. addrlen = 1 + data[4];
  629. break;
  630. default:
  631. *reason = tor_strdup("invalid response to connect request");
  632. return -1;
  633. }
  634. /* wait for address and port */
  635. if (datalen < 6 + addrlen)
  636. return 0;
  637. if (data[1] != 0x00) {
  638. *reason = tor_strdup(socks5_response_code_to_string(data[1]));
  639. return -1;
  640. }
  641. *drain_out = 6 + addrlen;
  642. return 1;
  643. }
  644. /* LCOV_EXCL_START */
  645. /* shouldn't get here if the input state is one we know about... */
  646. tor_assert(0);
  647. return -1;
  648. /* LCOV_EXCL_STOP */
  649. }