proto_socks.c 25 KB

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