proto_socks.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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_remove_from_front(buf, n_drain);
  123. } while (res == 0 && head && want_length < buf_datalen(buf) &&
  124. buf_datalen(buf) >= 2);
  125. return res;
  126. }
  127. /** The size of the header of an Extended ORPort message: 2 bytes for
  128. * COMMAND, 2 bytes for BODYLEN */
  129. #define EXT_OR_CMD_HEADER_SIZE 4
  130. /** Read <b>buf</b>, which should contain an Extended ORPort message
  131. * from a transport proxy. If well-formed, create and populate
  132. * <b>out</b> with the Extended ORport message. Return 0 if the
  133. * buffer was incomplete, 1 if it was well-formed and -1 if we
  134. * encountered an error while parsing it. */
  135. int
  136. fetch_ext_or_command_from_buf(buf_t *buf, ext_or_cmd_t **out)
  137. {
  138. char hdr[EXT_OR_CMD_HEADER_SIZE];
  139. uint16_t len;
  140. if (buf_datalen(buf) < EXT_OR_CMD_HEADER_SIZE)
  141. return 0;
  142. peek_from_buf(hdr, sizeof(hdr), buf);
  143. len = ntohs(get_uint16(hdr+2));
  144. if (buf_datalen(buf) < (unsigned)len + EXT_OR_CMD_HEADER_SIZE)
  145. return 0;
  146. *out = ext_or_cmd_new(len);
  147. (*out)->cmd = ntohs(get_uint16(hdr));
  148. (*out)->len = len;
  149. buf_remove_from_front(buf, EXT_OR_CMD_HEADER_SIZE);
  150. fetch_from_buf((*out)->body, len, buf);
  151. return 1;
  152. }
  153. /** Create a SOCKS5 reply message with <b>reason</b> in its REP field and
  154. * have Tor send it as error response to <b>req</b>.
  155. */
  156. static void
  157. socks_request_set_socks5_error(socks_request_t *req,
  158. socks5_reply_status_t reason)
  159. {
  160. req->replylen = 10;
  161. memset(req->reply,0,10);
  162. req->reply[0] = 0x05; // VER field.
  163. req->reply[1] = reason; // REP field.
  164. req->reply[3] = 0x01; // ATYP field.
  165. }
  166. static const char SOCKS_PROXY_IS_NOT_AN_HTTP_PROXY_MSG[] =
  167. "HTTP/1.0 501 Tor is not an HTTP Proxy\r\n"
  168. "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
  169. "<html>\n"
  170. "<head>\n"
  171. "<title>Tor is not an HTTP Proxy</title>\n"
  172. "</head>\n"
  173. "<body>\n"
  174. "<h1>Tor is not an HTTP Proxy</h1>\n"
  175. "<p>\n"
  176. "It appears you have configured your web browser to use Tor as "
  177. "an HTTP proxy.\n\n"
  178. "This is not correct: Tor is a SOCKS proxy, not an HTTP proxy.\n"
  179. "Please configure your client accordingly.\n"
  180. "</p>\n"
  181. "<p>\n"
  182. "See <a href=\"https://www.torproject.org/documentation.html\">"
  183. "https://www.torproject.org/documentation.html</a> for more "
  184. "information.\n"
  185. "<!-- Plus this comment, to make the body response more than 512 bytes, so "
  186. " IE will be willing to display it. Comment comment comment comment "
  187. " comment comment comment comment comment comment comment comment.-->\n"
  188. "</p>\n"
  189. "</body>\n"
  190. "</html>\n";
  191. /** Implementation helper to implement fetch_from_*_socks. Instead of looking
  192. * at a buffer's contents, we look at the <b>datalen</b> bytes of data in
  193. * <b>data</b>. Instead of removing data from the buffer, we set
  194. * <b>drain_out</b> to the amount of data that should be removed (or -1 if the
  195. * buffer should be cleared). Instead of pulling more data into the first
  196. * chunk of the buffer, we set *<b>want_length_out</b> to the number of bytes
  197. * we'd like to see in the input buffer, if they're available. */
  198. static int
  199. parse_socks(const char *data, size_t datalen, socks_request_t *req,
  200. int log_sockstype, int safe_socks, ssize_t *drain_out,
  201. size_t *want_length_out)
  202. {
  203. unsigned int len;
  204. char tmpbuf[TOR_ADDR_BUF_LEN+1];
  205. tor_addr_t destaddr;
  206. uint32_t destip;
  207. uint8_t socksver;
  208. char *next, *startaddr;
  209. unsigned char usernamelen, passlen;
  210. struct in_addr in;
  211. if (datalen < 2) {
  212. /* We always need at least 2 bytes. */
  213. *want_length_out = 2;
  214. return 0;
  215. }
  216. if (req->socks_version == 5 && !req->got_auth) {
  217. /* See if we have received authentication. Strictly speaking, we should
  218. also check whether we actually negotiated username/password
  219. authentication. But some broken clients will send us authentication
  220. even if we negotiated SOCKS_NO_AUTH. */
  221. if (*data == 1) { /* username/pass version 1 */
  222. /* Format is: authversion [1 byte] == 1
  223. usernamelen [1 byte]
  224. username [usernamelen bytes]
  225. passlen [1 byte]
  226. password [passlen bytes] */
  227. usernamelen = (unsigned char)*(data + 1);
  228. if (datalen < 2u + usernamelen + 1u) {
  229. *want_length_out = 2u + usernamelen + 1u;
  230. return 0;
  231. }
  232. passlen = (unsigned char)*(data + 2u + usernamelen);
  233. if (datalen < 2u + usernamelen + 1u + passlen) {
  234. *want_length_out = 2u + usernamelen + 1u + passlen;
  235. return 0;
  236. }
  237. req->replylen = 2; /* 2 bytes of response */
  238. req->reply[0] = 1; /* authversion == 1 */
  239. req->reply[1] = 0; /* authentication successful */
  240. log_debug(LD_APP,
  241. "socks5: Accepted username/password without checking.");
  242. if (usernamelen) {
  243. req->username = tor_memdup(data+2u, usernamelen);
  244. req->usernamelen = usernamelen;
  245. }
  246. if (passlen) {
  247. req->password = tor_memdup(data+3u+usernamelen, passlen);
  248. req->passwordlen = passlen;
  249. }
  250. *drain_out = 2u + usernamelen + 1u + passlen;
  251. req->got_auth = 1;
  252. *want_length_out = 7; /* Minimal socks5 command. */
  253. return 0;
  254. } else if (req->auth_type == SOCKS_USER_PASS) {
  255. /* unknown version byte */
  256. log_warn(LD_APP, "Socks5 username/password version %d not recognized; "
  257. "rejecting.", (int)*data);
  258. return -1;
  259. }
  260. }
  261. socksver = *data;
  262. switch (socksver) { /* which version of socks? */
  263. case 5: /* socks5 */
  264. if (req->socks_version != 5) { /* we need to negotiate a method */
  265. unsigned char nummethods = (unsigned char)*(data+1);
  266. int have_user_pass, have_no_auth;
  267. int r=0;
  268. tor_assert(!req->socks_version);
  269. if (datalen < 2u+nummethods) {
  270. *want_length_out = 2u+nummethods;
  271. return 0;
  272. }
  273. if (!nummethods)
  274. return -1;
  275. req->replylen = 2; /* 2 bytes of response */
  276. req->reply[0] = 5; /* socks5 reply */
  277. have_user_pass = (memchr(data+2, SOCKS_USER_PASS, nummethods) !=NULL);
  278. have_no_auth = (memchr(data+2, SOCKS_NO_AUTH, nummethods) !=NULL);
  279. if (have_user_pass && !(have_no_auth && req->socks_prefer_no_auth)) {
  280. req->auth_type = SOCKS_USER_PASS;
  281. req->reply[1] = SOCKS_USER_PASS; /* tell client to use "user/pass"
  282. auth method */
  283. req->socks_version = 5; /* remember we've already negotiated auth */
  284. log_debug(LD_APP,"socks5: accepted method 2 (username/password)");
  285. r=0;
  286. } else if (have_no_auth) {
  287. req->reply[1] = SOCKS_NO_AUTH; /* tell client to use "none" auth
  288. method */
  289. req->socks_version = 5; /* remember we've already negotiated auth */
  290. log_debug(LD_APP,"socks5: accepted method 0 (no authentication)");
  291. r=0;
  292. } else {
  293. log_warn(LD_APP,
  294. "socks5: offered methods don't include 'no auth' or "
  295. "username/password. Rejecting.");
  296. req->reply[1] = '\xFF'; /* reject all methods */
  297. r=-1;
  298. }
  299. /* Remove packet from buf. Some SOCKS clients will have sent extra
  300. * junk at this point; let's hope it's an authentication message. */
  301. *drain_out = 2u + nummethods;
  302. return r;
  303. }
  304. if (req->auth_type != SOCKS_NO_AUTH && !req->got_auth) {
  305. log_warn(LD_APP,
  306. "socks5: negotiated authentication, but none provided");
  307. return -1;
  308. }
  309. /* we know the method; read in the request */
  310. log_debug(LD_APP,"socks5: checking request");
  311. if (datalen < 7) {/* basic info plus >=1 for addr plus 2 for port */
  312. *want_length_out = 7;
  313. return 0; /* not yet */
  314. }
  315. req->command = (unsigned char) *(data+1);
  316. if (req->command != SOCKS_COMMAND_CONNECT &&
  317. req->command != SOCKS_COMMAND_RESOLVE &&
  318. req->command != SOCKS_COMMAND_RESOLVE_PTR) {
  319. /* not a connect or resolve or a resolve_ptr? we don't support it. */
  320. socks_request_set_socks5_error(req,SOCKS5_COMMAND_NOT_SUPPORTED);
  321. log_warn(LD_APP,"socks5: command %d not recognized. Rejecting.",
  322. req->command);
  323. return -1;
  324. }
  325. switch (*(data+3)) { /* address type */
  326. case 1: /* IPv4 address */
  327. case 4: /* IPv6 address */ {
  328. const int is_v6 = *(data+3) == 4;
  329. const unsigned addrlen = is_v6 ? 16 : 4;
  330. log_debug(LD_APP,"socks5: ipv4 address type");
  331. if (datalen < 6+addrlen) {/* ip/port there? */
  332. *want_length_out = 6+addrlen;
  333. return 0; /* not yet */
  334. }
  335. if (is_v6)
  336. tor_addr_from_ipv6_bytes(&destaddr, data+4);
  337. else
  338. tor_addr_from_ipv4n(&destaddr, get_uint32(data+4));
  339. tor_addr_to_str(tmpbuf, &destaddr, sizeof(tmpbuf), 1);
  340. if (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
  341. socks_request_set_socks5_error(req, SOCKS5_GENERAL_ERROR);
  342. log_warn(LD_APP,
  343. "socks5 IP takes %d bytes, which doesn't fit in %d. "
  344. "Rejecting.",
  345. (int)strlen(tmpbuf)+1,(int)MAX_SOCKS_ADDR_LEN);
  346. return -1;
  347. }
  348. strlcpy(req->address,tmpbuf,sizeof(req->address));
  349. req->port = ntohs(get_uint16(data+4+addrlen));
  350. *drain_out = 6+addrlen;
  351. if (req->command != SOCKS_COMMAND_RESOLVE_PTR &&
  352. !addressmap_have_mapping(req->address,0)) {
  353. log_unsafe_socks_warning(5, req->address, req->port, safe_socks);
  354. if (safe_socks) {
  355. socks_request_set_socks5_error(req, SOCKS5_NOT_ALLOWED);
  356. return -1;
  357. }
  358. }
  359. return 1;
  360. }
  361. case 3: /* fqdn */
  362. log_debug(LD_APP,"socks5: fqdn address type");
  363. if (req->command == SOCKS_COMMAND_RESOLVE_PTR) {
  364. socks_request_set_socks5_error(req,
  365. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED);
  366. log_warn(LD_APP, "socks5 received RESOLVE_PTR command with "
  367. "hostname type. Rejecting.");
  368. return -1;
  369. }
  370. len = (unsigned char)*(data+4);
  371. if (datalen < 7+len) { /* addr/port there? */
  372. *want_length_out = 7+len;
  373. return 0; /* not yet */
  374. }
  375. if (len+1 > MAX_SOCKS_ADDR_LEN) {
  376. socks_request_set_socks5_error(req, SOCKS5_GENERAL_ERROR);
  377. log_warn(LD_APP,
  378. "socks5 hostname is %d bytes, which doesn't fit in "
  379. "%d. Rejecting.", len+1,MAX_SOCKS_ADDR_LEN);
  380. return -1;
  381. }
  382. memcpy(req->address,data+5,len);
  383. req->address[len] = 0;
  384. req->port = ntohs(get_uint16(data+5+len));
  385. *drain_out = 5+len+2;
  386. if (!string_is_valid_hostname(req->address)) {
  387. socks_request_set_socks5_error(req, SOCKS5_GENERAL_ERROR);
  388. log_warn(LD_PROTOCOL,
  389. "Your application (using socks5 to port %d) gave Tor "
  390. "a malformed hostname: %s. Rejecting the connection.",
  391. req->port, escaped_safe_str_client(req->address));
  392. return -1;
  393. }
  394. if (log_sockstype)
  395. log_notice(LD_APP,
  396. "Your application (using socks5 to port %d) instructed "
  397. "Tor to take care of the DNS resolution itself if "
  398. "necessary. This is good.", req->port);
  399. return 1;
  400. default: /* unsupported */
  401. socks_request_set_socks5_error(req,
  402. SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED);
  403. log_warn(LD_APP,"socks5: unsupported address type %d. Rejecting.",
  404. (int) *(data+3));
  405. return -1;
  406. }
  407. tor_assert(0);
  408. break;
  409. case 4: { /* socks4 */
  410. enum {socks4, socks4a} socks4_prot = socks4a;
  411. const char *authstart, *authend;
  412. /* http://ss5.sourceforge.net/socks4.protocol.txt */
  413. /* http://ss5.sourceforge.net/socks4A.protocol.txt */
  414. req->socks_version = 4;
  415. if (datalen < SOCKS4_NETWORK_LEN) {/* basic info available? */
  416. *want_length_out = SOCKS4_NETWORK_LEN;
  417. return 0; /* not yet */
  418. }
  419. // buf_pullup(buf, 1280);
  420. req->command = (unsigned char) *(data+1);
  421. if (req->command != SOCKS_COMMAND_CONNECT &&
  422. req->command != SOCKS_COMMAND_RESOLVE) {
  423. /* not a connect or resolve? we don't support it. (No resolve_ptr with
  424. * socks4.) */
  425. log_warn(LD_APP,"socks4: command %d not recognized. Rejecting.",
  426. req->command);
  427. return -1;
  428. }
  429. req->port = ntohs(get_uint16(data+2));
  430. destip = ntohl(get_uint32(data+4));
  431. if ((!req->port && req->command!=SOCKS_COMMAND_RESOLVE) || !destip) {
  432. log_warn(LD_APP,"socks4: Port or DestIP is zero. Rejecting.");
  433. return -1;
  434. }
  435. if (destip >> 8) {
  436. log_debug(LD_APP,"socks4: destip not in form 0.0.0.x.");
  437. in.s_addr = htonl(destip);
  438. tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
  439. if (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
  440. log_debug(LD_APP,"socks4 addr (%d bytes) too long. Rejecting.",
  441. (int)strlen(tmpbuf));
  442. return -1;
  443. }
  444. log_debug(LD_APP,
  445. "socks4: successfully read destip (%s)",
  446. safe_str_client(tmpbuf));
  447. socks4_prot = socks4;
  448. }
  449. authstart = data + SOCKS4_NETWORK_LEN;
  450. next = memchr(authstart, 0,
  451. datalen-SOCKS4_NETWORK_LEN);
  452. if (!next) {
  453. if (datalen >= 1024) {
  454. log_debug(LD_APP, "Socks4 user name too long; rejecting.");
  455. return -1;
  456. }
  457. log_debug(LD_APP,"socks4: Username not here yet.");
  458. *want_length_out = datalen+1024; /* More than we need, but safe */
  459. return 0;
  460. }
  461. authend = next;
  462. tor_assert(next < data+datalen);
  463. startaddr = NULL;
  464. if (socks4_prot != socks4a &&
  465. !addressmap_have_mapping(tmpbuf,0)) {
  466. log_unsafe_socks_warning(4, tmpbuf, req->port, safe_socks);
  467. if (safe_socks)
  468. return -1;
  469. }
  470. if (socks4_prot == socks4a) {
  471. if (next+1 == data+datalen) {
  472. log_debug(LD_APP,"socks4: No part of destaddr here yet.");
  473. *want_length_out = datalen + 1024; /* More than we need, but safe */
  474. return 0;
  475. }
  476. startaddr = next+1;
  477. next = memchr(startaddr, 0, data + datalen - startaddr);
  478. if (!next) {
  479. if (datalen >= 1024) {
  480. log_debug(LD_APP,"socks4: Destaddr too long.");
  481. return -1;
  482. }
  483. log_debug(LD_APP,"socks4: Destaddr not all here yet.");
  484. *want_length_out = datalen + 1024; /* More than we need, but safe */
  485. return 0;
  486. }
  487. if (MAX_SOCKS_ADDR_LEN <= next-startaddr) {
  488. log_warn(LD_APP,"socks4: Destaddr too long. Rejecting.");
  489. return -1;
  490. }
  491. // tor_assert(next < buf->cur+buf_datalen(buf));
  492. if (log_sockstype)
  493. log_notice(LD_APP,
  494. "Your application (using socks4a to port %d) instructed "
  495. "Tor to take care of the DNS resolution itself if "
  496. "necessary. This is good.", req->port);
  497. }
  498. log_debug(LD_APP,"socks4: Everything is here. Success.");
  499. strlcpy(req->address, startaddr ? startaddr : tmpbuf,
  500. sizeof(req->address));
  501. if (!string_is_valid_hostname(req->address)) {
  502. log_warn(LD_PROTOCOL,
  503. "Your application (using socks4 to port %d) gave Tor "
  504. "a malformed hostname: %s. Rejecting the connection.",
  505. req->port, escaped_safe_str_client(req->address));
  506. return -1;
  507. }
  508. if (authend != authstart) {
  509. req->got_auth = 1;
  510. req->usernamelen = authend - authstart;
  511. req->username = tor_memdup(authstart, authend - authstart);
  512. }
  513. /* next points to the final \0 on inbuf */
  514. *drain_out = next - data + 1;
  515. return 1;
  516. }
  517. case 'G': /* get */
  518. case 'H': /* head */
  519. case 'P': /* put/post */
  520. case 'C': /* connect */
  521. strlcpy((char*)req->reply, SOCKS_PROXY_IS_NOT_AN_HTTP_PROXY_MSG,
  522. MAX_SOCKS_REPLY_LEN);
  523. req->replylen = strlen((char*)req->reply)+1;
  524. /* fall through */
  525. default: /* version is not socks4 or socks5 */
  526. log_warn(LD_APP,
  527. "Socks version %d not recognized. (Tor is not an http proxy.)",
  528. *(data));
  529. {
  530. /* Tell the controller the first 8 bytes. */
  531. char *tmp = tor_strndup(data, datalen < 8 ? datalen : 8);
  532. control_event_client_status(LOG_WARN,
  533. "SOCKS_UNKNOWN_PROTOCOL DATA=\"%s\"",
  534. escaped(tmp));
  535. tor_free(tmp);
  536. }
  537. return -1;
  538. }
  539. }
  540. /** Inspect a reply from SOCKS server stored in <b>buf</b> according
  541. * to <b>state</b>, removing the protocol data upon success. Return 0 on
  542. * incomplete response, 1 on success and -1 on error, in which case
  543. * <b>reason</b> is set to a descriptive message (free() when finished
  544. * with it).
  545. *
  546. * As a special case, 2 is returned when user/pass is required
  547. * during SOCKS5 handshake and user/pass is configured.
  548. */
  549. int
  550. fetch_from_buf_socks_client(buf_t *buf, int state, char **reason)
  551. {
  552. ssize_t drain = 0;
  553. int r;
  554. const char *head = NULL;
  555. size_t datalen = 0;
  556. if (buf_datalen(buf) < 2)
  557. return 0;
  558. buf_pullup(buf, MAX_SOCKS_MESSAGE_LEN, &head, &datalen);
  559. tor_assert(head && datalen >= 2);
  560. r = parse_socks_client((uint8_t*)head, datalen,
  561. state, reason, &drain);
  562. if (drain > 0)
  563. buf_remove_from_front(buf, drain);
  564. else if (drain < 0)
  565. buf_clear(buf);
  566. return r;
  567. }
  568. /** Implementation logic for fetch_from_*_socks_client. */
  569. static int
  570. parse_socks_client(const uint8_t *data, size_t datalen,
  571. int state, char **reason,
  572. ssize_t *drain_out)
  573. {
  574. unsigned int addrlen;
  575. *drain_out = 0;
  576. if (datalen < 2)
  577. return 0;
  578. switch (state) {
  579. case PROXY_SOCKS4_WANT_CONNECT_OK:
  580. /* Wait for the complete response */
  581. if (datalen < 8)
  582. return 0;
  583. if (data[1] != 0x5a) {
  584. *reason = tor_strdup(socks4_response_code_to_string(data[1]));
  585. return -1;
  586. }
  587. /* Success */
  588. *drain_out = 8;
  589. return 1;
  590. case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
  591. /* we don't have any credentials */
  592. if (data[1] != 0x00) {
  593. *reason = tor_strdup("server doesn't support any of our "
  594. "available authentication methods");
  595. return -1;
  596. }
  597. log_info(LD_NET, "SOCKS 5 client: continuing without authentication");
  598. *drain_out = -1;
  599. return 1;
  600. case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
  601. /* we have a username and password. return 1 if we can proceed without
  602. * providing authentication, or 2 otherwise. */
  603. switch (data[1]) {
  604. case 0x00:
  605. log_info(LD_NET, "SOCKS 5 client: we have auth details but server "
  606. "doesn't require authentication.");
  607. *drain_out = -1;
  608. return 1;
  609. case 0x02:
  610. log_info(LD_NET, "SOCKS 5 client: need authentication.");
  611. *drain_out = -1;
  612. return 2;
  613. /* fall through */
  614. }
  615. *reason = tor_strdup("server doesn't support any of our available "
  616. "authentication methods");
  617. return -1;
  618. case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
  619. /* handle server reply to rfc1929 authentication */
  620. if (data[1] != 0x00) {
  621. *reason = tor_strdup("authentication failed");
  622. return -1;
  623. }
  624. log_info(LD_NET, "SOCKS 5 client: authentication successful.");
  625. *drain_out = -1;
  626. return 1;
  627. case PROXY_SOCKS5_WANT_CONNECT_OK:
  628. /* response is variable length. BND.ADDR, etc, isn't needed
  629. * (don't bother with buf_pullup()), but make sure to eat all
  630. * the data used */
  631. /* wait for address type field to arrive */
  632. if (datalen < 4)
  633. return 0;
  634. switch (data[3]) {
  635. case 0x01: /* ip4 */
  636. addrlen = 4;
  637. break;
  638. case 0x04: /* ip6 */
  639. addrlen = 16;
  640. break;
  641. case 0x03: /* fqdn (can this happen here?) */
  642. if (datalen < 5)
  643. return 0;
  644. addrlen = 1 + data[4];
  645. break;
  646. default:
  647. *reason = tor_strdup("invalid response to connect request");
  648. return -1;
  649. }
  650. /* wait for address and port */
  651. if (datalen < 6 + addrlen)
  652. return 0;
  653. if (data[1] != 0x00) {
  654. *reason = tor_strdup(socks5_response_code_to_string(data[1]));
  655. return -1;
  656. }
  657. *drain_out = 6 + addrlen;
  658. return 1;
  659. }
  660. /* shouldn't get here... */
  661. tor_assert(0);
  662. return -1;
  663. }