proto_socks.c 30 KB

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