proto_socks.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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>Tor is not an HTTP Proxy</title>\n"
  146. "</head>\n"
  147. "<body>\n"
  148. "<h1>Tor is not an HTTP Proxy</h1>\n"
  149. "<p>\n"
  150. "It appears you have configured your web browser to use Tor as "
  151. "an HTTP proxy.\n\n"
  152. "This is not correct: Tor is a SOCKS proxy, not an HTTP proxy.\n"
  153. "Please configure your client accordingly.\n"
  154. "</p>\n"
  155. "<p>\n"
  156. "See <a href=\"https://www.torproject.org/documentation.html\">"
  157. "https://www.torproject.org/documentation.html</a> for more "
  158. "information.\n"
  159. "<!-- Plus this comment, to make the body response more than 512 bytes, so "
  160. " IE will be willing to display it. Comment comment comment comment "
  161. " comment comment comment comment comment comment comment comment.-->\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 (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
  315. socks_request_set_socks5_error(req, SOCKS5_GENERAL_ERROR);
  316. log_warn(LD_APP,
  317. "socks5 IP takes %d bytes, which doesn't fit in %d. "
  318. "Rejecting.",
  319. (int)strlen(tmpbuf)+1,(int)MAX_SOCKS_ADDR_LEN);
  320. return -1;
  321. }
  322. strlcpy(req->address,tmpbuf,sizeof(req->address));
  323. req->port = ntohs(get_uint16(data+4+addrlen));
  324. *drain_out = 6+addrlen;
  325. if (req->command != SOCKS_COMMAND_RESOLVE_PTR &&
  326. !addressmap_have_mapping(req->address,0)) {
  327. log_unsafe_socks_warning(5, req->address, req->port, safe_socks);
  328. if (safe_socks) {
  329. socks_request_set_socks5_error(req, SOCKS5_NOT_ALLOWED);
  330. return -1;
  331. }
  332. }
  333. return 1;
  334. }
  335. case 3: /* fqdn */
  336. log_debug(LD_APP,"socks5: fqdn address type");
  337. if (req->command == SOCKS_COMMAND_RESOLVE_PTR) {
  338. socks_request_set_socks5_error(req,
  339. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED);
  340. log_warn(LD_APP, "socks5 received RESOLVE_PTR command with "
  341. "hostname type. Rejecting.");
  342. return -1;
  343. }
  344. len = (unsigned char)*(data+4);
  345. if (datalen < 7+len) { /* addr/port there? */
  346. *want_length_out = 7+len;
  347. return 0; /* not yet */
  348. }
  349. if (len+1 > MAX_SOCKS_ADDR_LEN) {
  350. socks_request_set_socks5_error(req, SOCKS5_GENERAL_ERROR);
  351. log_warn(LD_APP,
  352. "socks5 hostname is %d bytes, which doesn't fit in "
  353. "%d. Rejecting.", len+1,MAX_SOCKS_ADDR_LEN);
  354. return -1;
  355. }
  356. memcpy(req->address,data+5,len);
  357. req->address[len] = 0;
  358. req->port = ntohs(get_uint16(data+5+len));
  359. *drain_out = 5+len+2;
  360. if (!string_is_valid_hostname(req->address)) {
  361. socks_request_set_socks5_error(req, SOCKS5_GENERAL_ERROR);
  362. log_warn(LD_PROTOCOL,
  363. "Your application (using socks5 to port %d) gave Tor "
  364. "a malformed hostname: %s. Rejecting the connection.",
  365. req->port, escaped_safe_str_client(req->address));
  366. return -1;
  367. }
  368. if (log_sockstype)
  369. log_notice(LD_APP,
  370. "Your application (using socks5 to port %d) instructed "
  371. "Tor to take care of the DNS resolution itself if "
  372. "necessary. This is good.", req->port);
  373. return 1;
  374. default: /* unsupported */
  375. socks_request_set_socks5_error(req,
  376. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED);
  377. log_warn(LD_APP,"socks5: unsupported address type %d. Rejecting.",
  378. (int) *(data+3));
  379. return -1;
  380. }
  381. tor_assert(0);
  382. break;
  383. case 4: { /* socks4 */
  384. enum {socks4, socks4a} socks4_prot = socks4a;
  385. const char *authstart, *authend;
  386. /* http://ss5.sourceforge.net/socks4.protocol.txt */
  387. /* http://ss5.sourceforge.net/socks4A.protocol.txt */
  388. req->socks_version = 4;
  389. if (datalen < SOCKS4_NETWORK_LEN) {/* basic info available? */
  390. *want_length_out = SOCKS4_NETWORK_LEN;
  391. return 0; /* not yet */
  392. }
  393. // buf_pullup(buf, 1280);
  394. req->command = (unsigned char) *(data+1);
  395. if (req->command != SOCKS_COMMAND_CONNECT &&
  396. req->command != SOCKS_COMMAND_RESOLVE) {
  397. /* not a connect or resolve? we don't support it. (No resolve_ptr with
  398. * socks4.) */
  399. log_warn(LD_APP,"socks4: command %d not recognized. Rejecting.",
  400. req->command);
  401. return -1;
  402. }
  403. req->port = ntohs(get_uint16(data+2));
  404. destip = ntohl(get_uint32(data+4));
  405. if ((!req->port && req->command!=SOCKS_COMMAND_RESOLVE) || !destip) {
  406. log_warn(LD_APP,"socks4: Port or DestIP is zero. Rejecting.");
  407. return -1;
  408. }
  409. if (destip >> 8) {
  410. log_debug(LD_APP,"socks4: destip not in form 0.0.0.x.");
  411. in.s_addr = htonl(destip);
  412. tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
  413. if (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
  414. log_debug(LD_APP,"socks4 addr (%d bytes) too long. Rejecting.",
  415. (int)strlen(tmpbuf));
  416. return -1;
  417. }
  418. log_debug(LD_APP,
  419. "socks4: successfully read destip (%s)",
  420. safe_str_client(tmpbuf));
  421. socks4_prot = socks4;
  422. }
  423. authstart = data + SOCKS4_NETWORK_LEN;
  424. next = memchr(authstart, 0,
  425. datalen-SOCKS4_NETWORK_LEN);
  426. if (!next) {
  427. if (datalen >= 1024) {
  428. log_debug(LD_APP, "Socks4 user name too long; rejecting.");
  429. return -1;
  430. }
  431. log_debug(LD_APP,"socks4: Username not here yet.");
  432. *want_length_out = datalen+1024; /* More than we need, but safe */
  433. return 0;
  434. }
  435. authend = next;
  436. tor_assert(next < data+datalen);
  437. startaddr = NULL;
  438. if (socks4_prot != socks4a &&
  439. !addressmap_have_mapping(tmpbuf,0)) {
  440. log_unsafe_socks_warning(4, tmpbuf, req->port, safe_socks);
  441. if (safe_socks)
  442. return -1;
  443. }
  444. if (socks4_prot == socks4a) {
  445. if (next+1 == data+datalen) {
  446. log_debug(LD_APP,"socks4: No part of destaddr here yet.");
  447. *want_length_out = datalen + 1024; /* More than we need, but safe */
  448. return 0;
  449. }
  450. startaddr = next+1;
  451. next = memchr(startaddr, 0, data + datalen - startaddr);
  452. if (!next) {
  453. if (datalen >= 1024) {
  454. log_debug(LD_APP,"socks4: Destaddr too long.");
  455. return -1;
  456. }
  457. log_debug(LD_APP,"socks4: Destaddr not all here yet.");
  458. *want_length_out = datalen + 1024; /* More than we need, but safe */
  459. return 0;
  460. }
  461. if (MAX_SOCKS_ADDR_LEN <= next-startaddr) {
  462. log_warn(LD_APP,"socks4: Destaddr too long. Rejecting.");
  463. return -1;
  464. }
  465. // tor_assert(next < buf->cur+buf_datalen(buf));
  466. if (log_sockstype)
  467. log_notice(LD_APP,
  468. "Your application (using socks4a to port %d) instructed "
  469. "Tor to take care of the DNS resolution itself if "
  470. "necessary. This is good.", req->port);
  471. }
  472. log_debug(LD_APP,"socks4: Everything is here. Success.");
  473. strlcpy(req->address, startaddr ? startaddr : tmpbuf,
  474. sizeof(req->address));
  475. if (!string_is_valid_hostname(req->address)) {
  476. log_warn(LD_PROTOCOL,
  477. "Your application (using socks4 to port %d) gave Tor "
  478. "a malformed hostname: %s. Rejecting the connection.",
  479. req->port, escaped_safe_str_client(req->address));
  480. return -1;
  481. }
  482. if (authend != authstart) {
  483. req->got_auth = 1;
  484. req->usernamelen = authend - authstart;
  485. req->username = tor_memdup(authstart, authend - authstart);
  486. }
  487. /* next points to the final \0 on inbuf */
  488. *drain_out = next - data + 1;
  489. return 1;
  490. }
  491. case 'G': /* get */
  492. case 'H': /* head */
  493. case 'P': /* put/post */
  494. case 'C': /* connect */
  495. strlcpy((char*)req->reply, SOCKS_PROXY_IS_NOT_AN_HTTP_PROXY_MSG,
  496. MAX_SOCKS_REPLY_LEN);
  497. req->replylen = strlen((char*)req->reply)+1;
  498. /* fall through */
  499. default: /* version is not socks4 or socks5 */
  500. log_warn(LD_APP,
  501. "Socks version %d not recognized. (Tor is not an http proxy.)",
  502. *(data));
  503. {
  504. /* Tell the controller the first 8 bytes. */
  505. char *tmp = tor_strndup(data, datalen < 8 ? datalen : 8);
  506. control_event_client_status(LOG_WARN,
  507. "SOCKS_UNKNOWN_PROTOCOL DATA=\"%s\"",
  508. escaped(tmp));
  509. tor_free(tmp);
  510. }
  511. return -1;
  512. }
  513. }
  514. /** Inspect a reply from SOCKS server stored in <b>buf</b> according
  515. * to <b>state</b>, removing the protocol data upon success. Return 0 on
  516. * incomplete response, 1 on success and -1 on error, in which case
  517. * <b>reason</b> is set to a descriptive message (free() when finished
  518. * with it).
  519. *
  520. * As a special case, 2 is returned when user/pass is required
  521. * during SOCKS5 handshake and user/pass is configured.
  522. */
  523. int
  524. fetch_from_buf_socks_client(buf_t *buf, int state, char **reason)
  525. {
  526. ssize_t drain = 0;
  527. int r;
  528. const char *head = NULL;
  529. size_t datalen = 0;
  530. if (buf_datalen(buf) < 2)
  531. return 0;
  532. buf_pullup(buf, MAX_SOCKS_MESSAGE_LEN, &head, &datalen);
  533. tor_assert(head && datalen >= 2);
  534. r = parse_socks_client((uint8_t*)head, datalen,
  535. state, reason, &drain);
  536. if (drain > 0)
  537. buf_drain(buf, drain);
  538. else if (drain < 0)
  539. buf_clear(buf);
  540. return r;
  541. }
  542. /** Implementation logic for fetch_from_*_socks_client. */
  543. static int
  544. parse_socks_client(const uint8_t *data, size_t datalen,
  545. int state, char **reason,
  546. ssize_t *drain_out)
  547. {
  548. unsigned int addrlen;
  549. *drain_out = 0;
  550. if (datalen < 2)
  551. return 0;
  552. switch (state) {
  553. case PROXY_SOCKS4_WANT_CONNECT_OK:
  554. /* Wait for the complete response */
  555. if (datalen < 8)
  556. return 0;
  557. if (data[1] != 0x5a) {
  558. *reason = tor_strdup(socks4_response_code_to_string(data[1]));
  559. return -1;
  560. }
  561. /* Success */
  562. *drain_out = 8;
  563. return 1;
  564. case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
  565. /* we don't have any credentials */
  566. if (data[1] != 0x00) {
  567. *reason = tor_strdup("server doesn't support any of our "
  568. "available authentication methods");
  569. return -1;
  570. }
  571. log_info(LD_NET, "SOCKS 5 client: continuing without authentication");
  572. *drain_out = -1;
  573. return 1;
  574. case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
  575. /* we have a username and password. return 1 if we can proceed without
  576. * providing authentication, or 2 otherwise. */
  577. switch (data[1]) {
  578. case 0x00:
  579. log_info(LD_NET, "SOCKS 5 client: we have auth details but server "
  580. "doesn't require authentication.");
  581. *drain_out = -1;
  582. return 1;
  583. case 0x02:
  584. log_info(LD_NET, "SOCKS 5 client: need authentication.");
  585. *drain_out = -1;
  586. return 2;
  587. /* fall through */
  588. }
  589. *reason = tor_strdup("server doesn't support any of our available "
  590. "authentication methods");
  591. return -1;
  592. case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
  593. /* handle server reply to rfc1929 authentication */
  594. if (data[1] != 0x00) {
  595. *reason = tor_strdup("authentication failed");
  596. return -1;
  597. }
  598. log_info(LD_NET, "SOCKS 5 client: authentication successful.");
  599. *drain_out = -1;
  600. return 1;
  601. case PROXY_SOCKS5_WANT_CONNECT_OK:
  602. /* response is variable length. BND.ADDR, etc, isn't needed
  603. * (don't bother with buf_pullup()), but make sure to eat all
  604. * the data used */
  605. /* wait for address type field to arrive */
  606. if (datalen < 4)
  607. return 0;
  608. switch (data[3]) {
  609. case 0x01: /* ip4 */
  610. addrlen = 4;
  611. break;
  612. case 0x04: /* ip6 */
  613. addrlen = 16;
  614. break;
  615. case 0x03: /* fqdn (can this happen here?) */
  616. if (datalen < 5)
  617. return 0;
  618. addrlen = 1 + data[4];
  619. break;
  620. default:
  621. *reason = tor_strdup("invalid response to connect request");
  622. return -1;
  623. }
  624. /* wait for address and port */
  625. if (datalen < 6 + addrlen)
  626. return 0;
  627. if (data[1] != 0x00) {
  628. *reason = tor_strdup(socks5_response_code_to_string(data[1]));
  629. return -1;
  630. }
  631. *drain_out = 6 + addrlen;
  632. return 1;
  633. }
  634. /* shouldn't get here... */
  635. tor_assert(0);
  636. return -1;
  637. }