proto_socks.c 28 KB

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