socksd.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "server_setup.h"
  25. #include <stdlib.h>
  26. /* Function
  27. *
  28. * Accepts a TCP connection on a custom port (IPv4 or IPv6). Connects to a
  29. * given addr + port backend (that is NOT extracted form the client's
  30. * request). The backend server default to connect to can be set with
  31. * --backend and --backendport.
  32. *
  33. * Read commands from FILE (set with --config). The commands control how to
  34. * act and is reset to defaults each client TCP connect.
  35. *
  36. * Config file keywords:
  37. *
  38. * "version [number: 5]" - requires the communication to use this version.
  39. * "nmethods_min [number: 1]" - the minimum numberf NMETHODS the client must
  40. * state
  41. * "nmethods_max [number: 3]" - the minimum numberf NMETHODS the client must
  42. * state
  43. * "user [string]" - the user name that must match (if method is 2)
  44. * "password [string]" - the password that must match (if method is 2)
  45. * "backend [IPv4]" - numerical IPv4 address of backend to connect to
  46. * "backendport [number:0]" - TCP port of backend to connect to. 0 means use
  47. the client's specified port number.
  48. * "method [number: 0]" - connect method to respond with:
  49. * 0 - no auth
  50. * 1 - GSSAPI (not supported)
  51. * 2 - user + password
  52. * "response [number]" - the decimal number to respond to a connect
  53. * SOCKS5: 0 is OK, SOCKS4: 90 is ok
  54. *
  55. */
  56. /* based on sockfilt.c */
  57. #include <signal.h>
  58. #ifdef HAVE_NETINET_IN_H
  59. #include <netinet/in.h>
  60. #endif
  61. #ifdef HAVE_NETINET_IN6_H
  62. #include <netinet/in6.h>
  63. #endif
  64. #ifdef HAVE_ARPA_INET_H
  65. #include <arpa/inet.h>
  66. #endif
  67. #ifdef HAVE_NETDB_H
  68. #include <netdb.h>
  69. #endif
  70. #include "curlx.h" /* from the private lib dir */
  71. #include "getpart.h"
  72. #include "inet_pton.h"
  73. #include "util.h"
  74. #include "server_sockaddr.h"
  75. #include "warnless.h"
  76. #include "tool_binmode.h"
  77. /* include memdebug.h last */
  78. #include "memdebug.h"
  79. #ifdef USE_WINSOCK
  80. #undef EINTR
  81. #define EINTR 4 /* errno.h value */
  82. #undef EAGAIN
  83. #define EAGAIN 11 /* errno.h value */
  84. #undef ENOMEM
  85. #define ENOMEM 12 /* errno.h value */
  86. #undef EINVAL
  87. #define EINVAL 22 /* errno.h value */
  88. #endif
  89. #define DEFAULT_PORT 8905
  90. #ifndef DEFAULT_LOGFILE
  91. #define DEFAULT_LOGFILE "log/socksd.log"
  92. #endif
  93. #ifndef DEFAULT_REQFILE
  94. #define DEFAULT_REQFILE "log/socksd-request.log"
  95. #endif
  96. #ifndef DEFAULT_CONFIG
  97. #define DEFAULT_CONFIG "socksd.config"
  98. #endif
  99. static const char *backendaddr = "127.0.0.1";
  100. static unsigned short backendport = 0; /* default is use client's */
  101. struct configurable {
  102. unsigned char version; /* initial version byte in the request must match
  103. this */
  104. unsigned char nmethods_min; /* minimum number of nmethods to expect */
  105. unsigned char nmethods_max; /* maximum number of nmethods to expect */
  106. unsigned char responseversion;
  107. unsigned char responsemethod;
  108. unsigned char reqcmd;
  109. unsigned char connectrep;
  110. unsigned short port; /* backend port */
  111. char addr[32]; /* backend IPv4 numerical */
  112. char user[256];
  113. char password[256];
  114. };
  115. #define CONFIG_VERSION 5
  116. #define CONFIG_NMETHODS_MIN 1 /* unauth, gssapi, auth */
  117. #define CONFIG_NMETHODS_MAX 3
  118. #define CONFIG_RESPONSEVERSION CONFIG_VERSION
  119. #define CONFIG_RESPONSEMETHOD 0 /* no auth */
  120. #define CONFIG_REQCMD 1 /* CONNECT */
  121. #define CONFIG_PORT backendport
  122. #define CONFIG_ADDR backendaddr
  123. #define CONFIG_CONNECTREP 0
  124. static struct configurable config;
  125. const char *serverlogfile = DEFAULT_LOGFILE;
  126. static const char *reqlogfile = DEFAULT_REQFILE;
  127. static const char *configfile = DEFAULT_CONFIG;
  128. static const char *socket_type = "IPv4";
  129. static unsigned short port = DEFAULT_PORT;
  130. static void resetdefaults(void)
  131. {
  132. logmsg("Reset to defaults");
  133. config.version = CONFIG_VERSION;
  134. config.nmethods_min = CONFIG_NMETHODS_MIN;
  135. config.nmethods_max = CONFIG_NMETHODS_MAX;
  136. config.responseversion = CONFIG_RESPONSEVERSION;
  137. config.responsemethod = CONFIG_RESPONSEMETHOD;
  138. config.reqcmd = CONFIG_REQCMD;
  139. config.connectrep = CONFIG_CONNECTREP;
  140. config.port = CONFIG_PORT;
  141. strcpy(config.addr, CONFIG_ADDR);
  142. strcpy(config.user, "user");
  143. strcpy(config.password, "password");
  144. }
  145. static unsigned char byteval(char *value)
  146. {
  147. unsigned long num = strtoul(value, NULL, 10);
  148. return num & 0xff;
  149. }
  150. static unsigned short shortval(char *value)
  151. {
  152. unsigned long num = strtoul(value, NULL, 10);
  153. return num & 0xffff;
  154. }
  155. static enum {
  156. socket_domain_inet = AF_INET
  157. #ifdef USE_IPV6
  158. , socket_domain_inet6 = AF_INET6
  159. #endif
  160. #ifdef USE_UNIX_SOCKETS
  161. , socket_domain_unix = AF_UNIX
  162. #endif
  163. } socket_domain = AF_INET;
  164. static void getconfig(void)
  165. {
  166. FILE *fp = fopen(configfile, FOPEN_READTEXT);
  167. resetdefaults();
  168. if(fp) {
  169. char buffer[512];
  170. logmsg("parse config file");
  171. while(fgets(buffer, sizeof(buffer), fp)) {
  172. char key[32];
  173. char value[260];
  174. if(2 == sscanf(buffer, "%31s %259s", key, value)) {
  175. if(!strcmp(key, "version")) {
  176. config.version = byteval(value);
  177. logmsg("version [%d] set", config.version);
  178. }
  179. else if(!strcmp(key, "nmethods_min")) {
  180. config.nmethods_min = byteval(value);
  181. logmsg("nmethods_min [%d] set", config.nmethods_min);
  182. }
  183. else if(!strcmp(key, "nmethods_max")) {
  184. config.nmethods_max = byteval(value);
  185. logmsg("nmethods_max [%d] set", config.nmethods_max);
  186. }
  187. else if(!strcmp(key, "backend")) {
  188. strcpy(config.addr, value);
  189. logmsg("backend [%s] set", config.addr);
  190. }
  191. else if(!strcmp(key, "backendport")) {
  192. config.port = shortval(value);
  193. logmsg("backendport [%d] set", config.port);
  194. }
  195. else if(!strcmp(key, "user")) {
  196. strcpy(config.user, value);
  197. logmsg("user [%s] set", config.user);
  198. }
  199. else if(!strcmp(key, "password")) {
  200. strcpy(config.password, value);
  201. logmsg("password [%s] set", config.password);
  202. }
  203. /* Methods:
  204. o X'00' NO AUTHENTICATION REQUIRED
  205. o X'01' GSSAPI
  206. o X'02' USERNAME/PASSWORD
  207. */
  208. else if(!strcmp(key, "method")) {
  209. config.responsemethod = byteval(value);
  210. logmsg("method [%d] set", config.responsemethod);
  211. }
  212. else if(!strcmp(key, "response")) {
  213. config.connectrep = byteval(value);
  214. logmsg("response [%d] set", config.connectrep);
  215. }
  216. }
  217. }
  218. fclose(fp);
  219. }
  220. }
  221. static void loghex(unsigned char *buffer, ssize_t len)
  222. {
  223. char data[1200];
  224. ssize_t i;
  225. unsigned char *ptr = buffer;
  226. char *optr = data;
  227. ssize_t width = 0;
  228. int left = sizeof(data);
  229. for(i = 0; i < len && (left >= 0); i++) {
  230. msnprintf(optr, left, "%02x", ptr[i]);
  231. width += 2;
  232. optr += 2;
  233. left -= 2;
  234. }
  235. if(width)
  236. logmsg("'%s'", data);
  237. }
  238. /* RFC 1928, SOCKS5 byte index */
  239. #define SOCKS5_VERSION 0
  240. #define SOCKS5_NMETHODS 1 /* number of methods that is listed */
  241. /* in the request: */
  242. #define SOCKS5_REQCMD 1
  243. #define SOCKS5_RESERVED 2
  244. #define SOCKS5_ATYP 3
  245. #define SOCKS5_DSTADDR 4
  246. /* connect response */
  247. #define SOCKS5_REP 1
  248. #define SOCKS5_BNDADDR 4
  249. /* auth request */
  250. #define SOCKS5_ULEN 1
  251. #define SOCKS5_UNAME 2
  252. #define SOCKS4_CD 1
  253. #define SOCKS4_DSTPORT 2
  254. /* connect to a given IPv4 address, not the one asked for */
  255. static curl_socket_t socksconnect(unsigned short connectport,
  256. const char *connectaddr)
  257. {
  258. int rc;
  259. srvr_sockaddr_union_t me;
  260. curl_socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
  261. if(sock == CURL_SOCKET_BAD)
  262. return CURL_SOCKET_BAD;
  263. memset(&me.sa4, 0, sizeof(me.sa4));
  264. me.sa4.sin_family = AF_INET;
  265. me.sa4.sin_port = htons(connectport);
  266. me.sa4.sin_addr.s_addr = INADDR_ANY;
  267. Curl_inet_pton(AF_INET, connectaddr, &me.sa4.sin_addr);
  268. rc = connect(sock, &me.sa, sizeof(me.sa4));
  269. if(rc) {
  270. int error = SOCKERRNO;
  271. logmsg("Error connecting to %s:%hu: (%d) %s",
  272. connectaddr, connectport, error, sstrerror(error));
  273. return CURL_SOCKET_BAD;
  274. }
  275. logmsg("Connected fine to %s:%d", connectaddr, connectport);
  276. return sock;
  277. }
  278. static curl_socket_t socks4(curl_socket_t fd,
  279. unsigned char *buffer,
  280. ssize_t rc)
  281. {
  282. unsigned char response[256 + 16];
  283. curl_socket_t connfd;
  284. unsigned char cd;
  285. unsigned short s4port;
  286. if(buffer[SOCKS4_CD] != 1) {
  287. logmsg("SOCKS4 CD is not 1: %d", buffer[SOCKS4_CD]);
  288. return CURL_SOCKET_BAD;
  289. }
  290. if(rc < 9) {
  291. logmsg("SOCKS4 connect message too short: %zd", rc);
  292. return CURL_SOCKET_BAD;
  293. }
  294. if(!config.port)
  295. s4port = (unsigned short)((buffer[SOCKS4_DSTPORT] << 8) |
  296. (buffer[SOCKS4_DSTPORT + 1]));
  297. else
  298. s4port = config.port;
  299. connfd = socksconnect(s4port, config.addr);
  300. if(connfd == CURL_SOCKET_BAD) {
  301. /* failed */
  302. cd = 91;
  303. }
  304. else {
  305. /* success */
  306. cd = 90;
  307. }
  308. response[0] = 0; /* reply version 0 */
  309. response[1] = cd; /* result */
  310. /* copy port and address from connect request */
  311. memcpy(&response[2], &buffer[SOCKS4_DSTPORT], 6);
  312. rc = (send)(fd, (char *)response, 8, 0);
  313. if(rc != 8) {
  314. logmsg("Sending SOCKS4 response failed!");
  315. return CURL_SOCKET_BAD;
  316. }
  317. logmsg("Sent %zd bytes", rc);
  318. loghex(response, rc);
  319. if(cd == 90)
  320. /* now do the transfer */
  321. return connfd;
  322. if(connfd != CURL_SOCKET_BAD)
  323. sclose(connfd);
  324. return CURL_SOCKET_BAD;
  325. }
  326. static curl_socket_t sockit(curl_socket_t fd)
  327. {
  328. unsigned char buffer[2*256 + 16];
  329. unsigned char response[2*256 + 16];
  330. ssize_t rc;
  331. unsigned char len;
  332. unsigned char type;
  333. unsigned char rep = 0;
  334. unsigned char *address;
  335. unsigned short socksport;
  336. curl_socket_t connfd = CURL_SOCKET_BAD;
  337. unsigned short s5port;
  338. getconfig();
  339. rc = recv(fd, (char *)buffer, sizeof(buffer), 0);
  340. if(rc <= 0) {
  341. logmsg("SOCKS identifier message missing, recv returned %zd", rc);
  342. return CURL_SOCKET_BAD;
  343. }
  344. logmsg("READ %zd bytes", rc);
  345. loghex(buffer, rc);
  346. if(buffer[SOCKS5_VERSION] == 4)
  347. return socks4(fd, buffer, rc);
  348. if(rc < 3) {
  349. logmsg("SOCKS5 identifier message too short: %zd", rc);
  350. return CURL_SOCKET_BAD;
  351. }
  352. if(buffer[SOCKS5_VERSION] != config.version) {
  353. logmsg("VERSION byte not %d", config.version);
  354. return CURL_SOCKET_BAD;
  355. }
  356. if((buffer[SOCKS5_NMETHODS] < config.nmethods_min) ||
  357. (buffer[SOCKS5_NMETHODS] > config.nmethods_max)) {
  358. logmsg("NMETHODS byte not within %d - %d ",
  359. config.nmethods_min, config.nmethods_max);
  360. return CURL_SOCKET_BAD;
  361. }
  362. /* after NMETHODS follows that many bytes listing the methods the client
  363. says it supports */
  364. if(rc != (buffer[SOCKS5_NMETHODS] + 2)) {
  365. logmsg("Expected %d bytes, got %zd", buffer[SOCKS5_NMETHODS] + 2, rc);
  366. return CURL_SOCKET_BAD;
  367. }
  368. logmsg("Incoming request deemed fine!");
  369. /* respond with two bytes: VERSION + METHOD */
  370. response[0] = config.responseversion;
  371. response[1] = config.responsemethod;
  372. rc = (send)(fd, (char *)response, 2, 0);
  373. if(rc != 2) {
  374. logmsg("Sending response failed!");
  375. return CURL_SOCKET_BAD;
  376. }
  377. logmsg("Sent %zd bytes", rc);
  378. loghex(response, rc);
  379. /* expect the request or auth */
  380. rc = recv(fd, (char *)buffer, sizeof(buffer), 0);
  381. if(rc <= 0) {
  382. logmsg("SOCKS5 request or auth message missing, recv returned %zd", rc);
  383. return CURL_SOCKET_BAD;
  384. }
  385. logmsg("READ %zd bytes", rc);
  386. loghex(buffer, rc);
  387. if(config.responsemethod == 2) {
  388. /* RFC 1929 authentication
  389. +----+------+----------+------+----------+
  390. |VER | ULEN | UNAME | PLEN | PASSWD |
  391. +----+------+----------+------+----------+
  392. | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
  393. +----+------+----------+------+----------+
  394. */
  395. unsigned char ulen;
  396. unsigned char plen;
  397. bool login = TRUE;
  398. if(rc < 5) {
  399. logmsg("Too short auth input: %zd", rc);
  400. return CURL_SOCKET_BAD;
  401. }
  402. if(buffer[SOCKS5_VERSION] != 1) {
  403. logmsg("Auth VERSION byte not 1, got %d", buffer[SOCKS5_VERSION]);
  404. return CURL_SOCKET_BAD;
  405. }
  406. ulen = buffer[SOCKS5_ULEN];
  407. if(rc < 4 + ulen) {
  408. logmsg("Too short packet for username: %zd", rc);
  409. return CURL_SOCKET_BAD;
  410. }
  411. plen = buffer[SOCKS5_ULEN + ulen + 1];
  412. if(rc < 3 + ulen + plen) {
  413. logmsg("Too short packet for ulen %d plen %d: %zd", ulen, plen, rc);
  414. return CURL_SOCKET_BAD;
  415. }
  416. if((ulen != strlen(config.user)) ||
  417. (plen != strlen(config.password)) ||
  418. memcmp(&buffer[SOCKS5_UNAME], config.user, ulen) ||
  419. memcmp(&buffer[SOCKS5_UNAME + ulen + 1], config.password, plen)) {
  420. /* no match! */
  421. logmsg("mismatched credentials!");
  422. login = FALSE;
  423. }
  424. response[0] = 1;
  425. response[1] = login ? 0 : 1;
  426. rc = (send)(fd, (char *)response, 2, 0);
  427. if(rc != 2) {
  428. logmsg("Sending auth response failed!");
  429. return CURL_SOCKET_BAD;
  430. }
  431. logmsg("Sent %zd bytes", rc);
  432. loghex(response, rc);
  433. if(!login)
  434. return CURL_SOCKET_BAD;
  435. /* expect the request */
  436. rc = recv(fd, (char *)buffer, sizeof(buffer), 0);
  437. if(rc <= 0) {
  438. logmsg("SOCKS5 request message missing, recv returned %zd", rc);
  439. return CURL_SOCKET_BAD;
  440. }
  441. logmsg("READ %zd bytes", rc);
  442. loghex(buffer, rc);
  443. }
  444. if(rc < 6) {
  445. logmsg("Too short for request: %zd", rc);
  446. return CURL_SOCKET_BAD;
  447. }
  448. if(buffer[SOCKS5_VERSION] != config.version) {
  449. logmsg("Request VERSION byte not %d", config.version);
  450. return CURL_SOCKET_BAD;
  451. }
  452. /* 1 == CONNECT */
  453. if(buffer[SOCKS5_REQCMD] != config.reqcmd) {
  454. logmsg("Request COMMAND byte not %d", config.reqcmd);
  455. return CURL_SOCKET_BAD;
  456. }
  457. /* reserved, should be zero */
  458. if(buffer[SOCKS5_RESERVED]) {
  459. logmsg("Request COMMAND byte not %d", config.reqcmd);
  460. return CURL_SOCKET_BAD;
  461. }
  462. /* ATYP:
  463. o IP V4 address: X'01'
  464. o DOMAINNAME: X'03'
  465. o IP V6 address: X'04'
  466. */
  467. type = buffer[SOCKS5_ATYP];
  468. address = &buffer[SOCKS5_DSTADDR];
  469. switch(type) {
  470. case 1:
  471. /* 4 bytes IPv4 address */
  472. len = 4;
  473. break;
  474. case 3:
  475. /* The first octet of the address field contains the number of octets of
  476. name that follow */
  477. len = buffer[SOCKS5_DSTADDR];
  478. len++;
  479. break;
  480. case 4:
  481. /* 16 bytes IPv6 address */
  482. len = 16;
  483. break;
  484. default:
  485. logmsg("Unknown ATYP %d", type);
  486. return CURL_SOCKET_BAD;
  487. }
  488. if(rc < (4 + len + 2)) {
  489. logmsg("Request too short: %zd, expected %d", rc, 4 + len + 2);
  490. return CURL_SOCKET_BAD;
  491. }
  492. logmsg("Received ATYP %d", type);
  493. {
  494. FILE *dump;
  495. dump = fopen(reqlogfile, "ab");
  496. if(dump) {
  497. int i;
  498. fprintf(dump, "atyp %u =>", type);
  499. switch(type) {
  500. case 1:
  501. /* 4 bytes IPv4 address */
  502. fprintf(dump, " %u.%u.%u.%u\n",
  503. address[0], address[1], address[2], address[3]);
  504. break;
  505. case 3:
  506. /* The first octet of the address field contains the number of octets
  507. of name that follow */
  508. fprintf(dump, " %.*s\n", len-1, &address[1]);
  509. break;
  510. case 4:
  511. /* 16 bytes IPv6 address */
  512. for(i = 0; i < 16; i++) {
  513. fprintf(dump, " %02x", address[i]);
  514. }
  515. fprintf(dump, "\n");
  516. break;
  517. }
  518. fclose(dump);
  519. }
  520. }
  521. if(!config.port) {
  522. unsigned char *portp = &buffer[SOCKS5_DSTADDR + len];
  523. s5port = (unsigned short)((portp[0] << 8) | (portp[1]));
  524. }
  525. else
  526. s5port = config.port;
  527. if(!config.connectrep)
  528. connfd = socksconnect(s5port, config.addr);
  529. if(connfd == CURL_SOCKET_BAD) {
  530. /* failed */
  531. rep = 1;
  532. }
  533. else {
  534. rep = config.connectrep;
  535. }
  536. /* */
  537. response[SOCKS5_VERSION] = config.responseversion;
  538. /*
  539. o REP Reply field:
  540. o X'00' succeeded
  541. o X'01' general SOCKS server failure
  542. o X'02' connection not allowed by ruleset
  543. o X'03' Network unreachable
  544. o X'04' Host unreachable
  545. o X'05' Connection refused
  546. o X'06' TTL expired
  547. o X'07' Command not supported
  548. o X'08' Address type not supported
  549. o X'09' to X'FF' unassigned
  550. */
  551. response[SOCKS5_REP] = rep;
  552. response[SOCKS5_RESERVED] = 0; /* must be zero */
  553. response[SOCKS5_ATYP] = type; /* address type */
  554. /* mirror back the original addr + port */
  555. /* address or hostname */
  556. memcpy(&response[SOCKS5_BNDADDR], address, len);
  557. /* port number */
  558. memcpy(&response[SOCKS5_BNDADDR + len],
  559. &buffer[SOCKS5_DSTADDR + len], sizeof(socksport));
  560. rc = (send)(fd, (char *)response, (SEND_TYPE_ARG3)(len + 6), 0);
  561. if(rc != (len + 6)) {
  562. logmsg("Sending connect response failed!");
  563. return CURL_SOCKET_BAD;
  564. }
  565. logmsg("Sent %zd bytes", rc);
  566. loghex(response, rc);
  567. if(!rep)
  568. return connfd;
  569. if(connfd != CURL_SOCKET_BAD)
  570. sclose(connfd);
  571. return CURL_SOCKET_BAD;
  572. }
  573. struct perclient {
  574. size_t fromremote;
  575. size_t fromclient;
  576. curl_socket_t remotefd;
  577. curl_socket_t clientfd;
  578. bool used;
  579. };
  580. /* return non-zero when transfer is done */
  581. static int tunnel(struct perclient *cp, fd_set *fds)
  582. {
  583. ssize_t nread;
  584. ssize_t nwrite;
  585. char buffer[512];
  586. if(FD_ISSET(cp->clientfd, fds)) {
  587. /* read from client, send to remote */
  588. nread = recv(cp->clientfd, buffer, sizeof(buffer), 0);
  589. if(nread > 0) {
  590. nwrite = send(cp->remotefd, (char *)buffer,
  591. (SEND_TYPE_ARG3)nread, 0);
  592. if(nwrite != nread)
  593. return 1;
  594. cp->fromclient += nwrite;
  595. }
  596. else
  597. return 1;
  598. }
  599. if(FD_ISSET(cp->remotefd, fds)) {
  600. /* read from remote, send to client */
  601. nread = recv(cp->remotefd, buffer, sizeof(buffer), 0);
  602. if(nread > 0) {
  603. nwrite = send(cp->clientfd, (char *)buffer,
  604. (SEND_TYPE_ARG3)nread, 0);
  605. if(nwrite != nread)
  606. return 1;
  607. cp->fromremote += nwrite;
  608. }
  609. else
  610. return 1;
  611. }
  612. return 0;
  613. }
  614. /*
  615. sockfdp is a pointer to an established stream or CURL_SOCKET_BAD
  616. if sockfd is CURL_SOCKET_BAD, listendfd is a listening socket we must
  617. accept()
  618. */
  619. static bool incoming(curl_socket_t listenfd)
  620. {
  621. fd_set fds_read;
  622. fd_set fds_write;
  623. fd_set fds_err;
  624. int clients = 0; /* connected clients */
  625. struct perclient c[2];
  626. memset(c, 0, sizeof(c));
  627. if(got_exit_signal) {
  628. logmsg("signalled to die, exiting...");
  629. return FALSE;
  630. }
  631. #ifdef HAVE_GETPPID
  632. /* As a last resort, quit if socks5 process becomes orphan. */
  633. if(getppid() <= 1) {
  634. logmsg("process becomes orphan, exiting");
  635. return FALSE;
  636. }
  637. #endif
  638. do {
  639. int i;
  640. ssize_t rc;
  641. int error = 0;
  642. curl_socket_t sockfd = listenfd;
  643. int maxfd = (int)sockfd;
  644. FD_ZERO(&fds_read);
  645. FD_ZERO(&fds_write);
  646. FD_ZERO(&fds_err);
  647. /* there's always a socket to wait for */
  648. #if defined(__DJGPP__)
  649. #pragma GCC diagnostic push
  650. #pragma GCC diagnostic ignored "-Warith-conversion"
  651. #endif
  652. FD_SET(sockfd, &fds_read);
  653. #if defined(__DJGPP__)
  654. #pragma GCC diagnostic pop
  655. #endif
  656. for(i = 0; i < 2; i++) {
  657. if(c[i].used) {
  658. curl_socket_t fd = c[i].clientfd;
  659. #if defined(__DJGPP__)
  660. #pragma GCC diagnostic push
  661. #pragma GCC diagnostic ignored "-Warith-conversion"
  662. #endif
  663. FD_SET(fd, &fds_read);
  664. #if defined(__DJGPP__)
  665. #pragma GCC diagnostic pop
  666. #endif
  667. if((int)fd > maxfd)
  668. maxfd = (int)fd;
  669. fd = c[i].remotefd;
  670. #if defined(__DJGPP__)
  671. #pragma GCC diagnostic push
  672. #pragma GCC diagnostic ignored "-Warith-conversion"
  673. #endif
  674. FD_SET(fd, &fds_read);
  675. #if defined(__DJGPP__)
  676. #pragma GCC diagnostic pop
  677. #endif
  678. if((int)fd > maxfd)
  679. maxfd = (int)fd;
  680. }
  681. }
  682. do {
  683. /* select() blocking behavior call on blocking descriptors please */
  684. rc = select(maxfd + 1, &fds_read, &fds_write, &fds_err, NULL);
  685. if(got_exit_signal) {
  686. logmsg("signalled to die, exiting...");
  687. return FALSE;
  688. }
  689. } while((rc == -1) && ((error = errno) == EINTR));
  690. if(rc < 0) {
  691. logmsg("select() failed with error: (%d) %s",
  692. error, strerror(error));
  693. return FALSE;
  694. }
  695. if((clients < 2) && FD_ISSET(sockfd, &fds_read)) {
  696. curl_socket_t newfd = accept(sockfd, NULL, NULL);
  697. if(CURL_SOCKET_BAD == newfd) {
  698. error = SOCKERRNO;
  699. logmsg("accept(%" FMT_SOCKET_T ", NULL, NULL) "
  700. "failed with error: (%d) %s",
  701. sockfd, error, sstrerror(error));
  702. }
  703. else {
  704. curl_socket_t remotefd;
  705. logmsg("====> Client connect, fd %" FMT_SOCKET_T ". "
  706. "Read config from %s", newfd, configfile);
  707. remotefd = sockit(newfd); /* SOCKS until done */
  708. if(remotefd == CURL_SOCKET_BAD) {
  709. logmsg("====> Client disconnect");
  710. sclose(newfd);
  711. }
  712. else {
  713. struct perclient *cp = &c[0];
  714. logmsg("====> Tunnel transfer");
  715. if(c[0].used)
  716. cp = &c[1];
  717. cp->fromremote = 0;
  718. cp->fromclient = 0;
  719. cp->clientfd = newfd;
  720. cp->remotefd = remotefd;
  721. cp->used = TRUE;
  722. clients++;
  723. }
  724. }
  725. }
  726. for(i = 0; i < 2; i++) {
  727. struct perclient *cp = &c[i];
  728. if(cp->used) {
  729. if(tunnel(cp, &fds_read)) {
  730. logmsg("SOCKS transfer completed. Bytes: < %zu > %zu",
  731. cp->fromremote, cp->fromclient);
  732. sclose(cp->clientfd);
  733. sclose(cp->remotefd);
  734. cp->used = FALSE;
  735. clients--;
  736. }
  737. }
  738. }
  739. } while(clients);
  740. return TRUE;
  741. }
  742. static curl_socket_t sockdaemon(curl_socket_t sock,
  743. unsigned short *listenport
  744. #ifdef USE_UNIX_SOCKETS
  745. , const char *unix_socket
  746. #endif
  747. )
  748. {
  749. /* passive daemon style */
  750. srvr_sockaddr_union_t listener;
  751. int flag;
  752. int rc;
  753. int totdelay = 0;
  754. int maxretr = 10;
  755. int delay = 20;
  756. int attempt = 0;
  757. int error = 0;
  758. do {
  759. attempt++;
  760. flag = 1;
  761. rc = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  762. (void *)&flag, sizeof(flag));
  763. if(rc) {
  764. error = SOCKERRNO;
  765. logmsg("setsockopt(SO_REUSEADDR) failed with error: (%d) %s",
  766. error, sstrerror(error));
  767. if(maxretr) {
  768. rc = wait_ms(delay);
  769. if(rc) {
  770. /* should not happen */
  771. error = errno;
  772. logmsg("wait_ms() failed with error: (%d) %s",
  773. error, strerror(error));
  774. sclose(sock);
  775. return CURL_SOCKET_BAD;
  776. }
  777. if(got_exit_signal) {
  778. logmsg("signalled to die, exiting...");
  779. sclose(sock);
  780. return CURL_SOCKET_BAD;
  781. }
  782. totdelay += delay;
  783. delay *= 2; /* double the sleep for next attempt */
  784. }
  785. }
  786. } while(rc && maxretr--);
  787. if(rc) {
  788. logmsg("setsockopt(SO_REUSEADDR) failed %d times in %d ms. Error: (%d) %s",
  789. attempt, totdelay, error, strerror(error));
  790. logmsg("Continuing anyway...");
  791. }
  792. /* When the specified listener port is zero, it is actually a
  793. request to let the system choose a non-zero available port. */
  794. switch(socket_domain) {
  795. case AF_INET:
  796. memset(&listener.sa4, 0, sizeof(listener.sa4));
  797. listener.sa4.sin_family = AF_INET;
  798. listener.sa4.sin_addr.s_addr = INADDR_ANY;
  799. listener.sa4.sin_port = htons(*listenport);
  800. rc = bind(sock, &listener.sa, sizeof(listener.sa4));
  801. break;
  802. #ifdef USE_IPV6
  803. case AF_INET6:
  804. memset(&listener.sa6, 0, sizeof(listener.sa6));
  805. listener.sa6.sin6_family = AF_INET6;
  806. listener.sa6.sin6_addr = in6addr_any;
  807. listener.sa6.sin6_port = htons(*listenport);
  808. rc = bind(sock, &listener.sa, sizeof(listener.sa6));
  809. break;
  810. #endif /* USE_IPV6 */
  811. #ifdef USE_UNIX_SOCKETS
  812. case AF_UNIX:
  813. rc = bind_unix_socket(sock, unix_socket, &listener.sau);
  814. #endif
  815. }
  816. if(rc) {
  817. error = SOCKERRNO;
  818. #ifdef USE_UNIX_SOCKETS
  819. if(socket_domain == AF_UNIX)
  820. logmsg("Error binding socket on path %s: (%d) %s",
  821. unix_socket, error, sstrerror(error));
  822. else
  823. #endif
  824. logmsg("Error binding socket on port %hu: (%d) %s",
  825. *listenport, error, sstrerror(error));
  826. sclose(sock);
  827. return CURL_SOCKET_BAD;
  828. }
  829. if(!*listenport
  830. #ifdef USE_UNIX_SOCKETS
  831. && !unix_socket
  832. #endif
  833. ) {
  834. /* The system was supposed to choose a port number, figure out which
  835. port we actually got and update the listener port value with it. */
  836. curl_socklen_t la_size;
  837. srvr_sockaddr_union_t localaddr;
  838. #ifdef USE_IPV6
  839. if(socket_domain == AF_INET6)
  840. la_size = sizeof(localaddr.sa6);
  841. else
  842. #endif
  843. la_size = sizeof(localaddr.sa4);
  844. memset(&localaddr.sa, 0, (size_t)la_size);
  845. if(getsockname(sock, &localaddr.sa, &la_size) < 0) {
  846. error = SOCKERRNO;
  847. logmsg("getsockname() failed with error: (%d) %s",
  848. error, sstrerror(error));
  849. sclose(sock);
  850. return CURL_SOCKET_BAD;
  851. }
  852. switch(localaddr.sa.sa_family) {
  853. case AF_INET:
  854. *listenport = ntohs(localaddr.sa4.sin_port);
  855. break;
  856. #ifdef USE_IPV6
  857. case AF_INET6:
  858. *listenport = ntohs(localaddr.sa6.sin6_port);
  859. break;
  860. #endif
  861. default:
  862. break;
  863. }
  864. if(!*listenport) {
  865. /* Real failure, listener port shall not be zero beyond this point. */
  866. logmsg("Apparently getsockname() succeeded, with listener port zero.");
  867. logmsg("A valid reason for this failure is a binary built without");
  868. logmsg("proper network library linkage. This might not be the only");
  869. logmsg("reason, but double check it before anything else.");
  870. sclose(sock);
  871. return CURL_SOCKET_BAD;
  872. }
  873. }
  874. /* start accepting connections */
  875. rc = listen(sock, 5);
  876. if(0 != rc) {
  877. error = SOCKERRNO;
  878. logmsg("listen(%" FMT_SOCKET_T ", 5) failed with error: (%d) %s",
  879. sock, error, sstrerror(error));
  880. sclose(sock);
  881. return CURL_SOCKET_BAD;
  882. }
  883. return sock;
  884. }
  885. int main(int argc, char *argv[])
  886. {
  887. curl_socket_t sock = CURL_SOCKET_BAD;
  888. curl_socket_t msgsock = CURL_SOCKET_BAD;
  889. int wrotepidfile = 0;
  890. int wroteportfile = 0;
  891. const char *pidname = ".socksd.pid";
  892. const char *portname = NULL; /* none by default */
  893. bool juggle_again;
  894. int error;
  895. int arg = 1;
  896. #ifdef USE_UNIX_SOCKETS
  897. const char *unix_socket = NULL;
  898. bool unlink_socket = false;
  899. #endif
  900. while(argc > arg) {
  901. if(!strcmp("--version", argv[arg])) {
  902. printf("socksd IPv4%s\n",
  903. #ifdef USE_IPV6
  904. "/IPv6"
  905. #else
  906. ""
  907. #endif
  908. );
  909. return 0;
  910. }
  911. else if(!strcmp("--pidfile", argv[arg])) {
  912. arg++;
  913. if(argc > arg)
  914. pidname = argv[arg++];
  915. }
  916. else if(!strcmp("--portfile", argv[arg])) {
  917. arg++;
  918. if(argc > arg)
  919. portname = argv[arg++];
  920. }
  921. else if(!strcmp("--config", argv[arg])) {
  922. arg++;
  923. if(argc > arg)
  924. configfile = argv[arg++];
  925. }
  926. else if(!strcmp("--backend", argv[arg])) {
  927. arg++;
  928. if(argc > arg)
  929. backendaddr = argv[arg++];
  930. }
  931. else if(!strcmp("--backendport", argv[arg])) {
  932. arg++;
  933. if(argc > arg)
  934. backendport = (unsigned short)atoi(argv[arg++]);
  935. }
  936. else if(!strcmp("--logfile", argv[arg])) {
  937. arg++;
  938. if(argc > arg)
  939. serverlogfile = argv[arg++];
  940. }
  941. else if(!strcmp("--reqfile", argv[arg])) {
  942. arg++;
  943. if(argc > arg)
  944. reqlogfile = argv[arg++];
  945. }
  946. else if(!strcmp("--ipv6", argv[arg])) {
  947. #ifdef USE_IPV6
  948. socket_domain = AF_INET6;
  949. socket_type = "IPv6";
  950. #endif
  951. arg++;
  952. }
  953. else if(!strcmp("--ipv4", argv[arg])) {
  954. /* for completeness, we support this option as well */
  955. #ifdef USE_IPV6
  956. socket_type = "IPv4";
  957. #endif
  958. arg++;
  959. }
  960. else if(!strcmp("--unix-socket", argv[arg])) {
  961. arg++;
  962. if(argc > arg) {
  963. #ifdef USE_UNIX_SOCKETS
  964. struct sockaddr_un sau;
  965. unix_socket = argv[arg];
  966. if(strlen(unix_socket) >= sizeof(sau.sun_path)) {
  967. fprintf(stderr,
  968. "socksd: socket path must be shorter than %zu chars: %s\n",
  969. sizeof(sau.sun_path), unix_socket);
  970. return 0;
  971. }
  972. socket_domain = AF_UNIX;
  973. socket_type = "unix";
  974. #endif
  975. arg++;
  976. }
  977. }
  978. else if(!strcmp("--port", argv[arg])) {
  979. arg++;
  980. if(argc > arg) {
  981. char *endptr;
  982. unsigned long ulnum = strtoul(argv[arg], &endptr, 10);
  983. port = curlx_ultous(ulnum);
  984. arg++;
  985. }
  986. }
  987. else {
  988. puts("Usage: socksd [option]\n"
  989. " --backend [ipv4 addr]\n"
  990. " --backendport [TCP port]\n"
  991. " --config [file]\n"
  992. " --version\n"
  993. " --logfile [file]\n"
  994. " --pidfile [file]\n"
  995. " --portfile [file]\n"
  996. " --reqfile [file]\n"
  997. " --ipv4\n"
  998. " --ipv6\n"
  999. " --unix-socket [file]\n"
  1000. " --bindonly\n"
  1001. " --port [port]\n");
  1002. return 0;
  1003. }
  1004. }
  1005. #ifdef _WIN32
  1006. win32_init();
  1007. atexit(win32_cleanup);
  1008. #endif
  1009. CURL_SET_BINMODE(stdin);
  1010. CURL_SET_BINMODE(stdout);
  1011. CURL_SET_BINMODE(stderr);
  1012. install_signal_handlers(false);
  1013. sock = socket(socket_domain, SOCK_STREAM, 0);
  1014. if(CURL_SOCKET_BAD == sock) {
  1015. error = SOCKERRNO;
  1016. logmsg("Error creating socket: (%d) %s",
  1017. error, sstrerror(error));
  1018. goto socks5_cleanup;
  1019. }
  1020. {
  1021. /* passive daemon style */
  1022. sock = sockdaemon(sock, &port
  1023. #ifdef USE_UNIX_SOCKETS
  1024. , unix_socket
  1025. #endif
  1026. );
  1027. if(CURL_SOCKET_BAD == sock) {
  1028. goto socks5_cleanup;
  1029. }
  1030. #ifdef USE_UNIX_SOCKETS
  1031. unlink_socket = true;
  1032. #endif
  1033. msgsock = CURL_SOCKET_BAD; /* no stream socket yet */
  1034. }
  1035. logmsg("Running %s version", socket_type);
  1036. #ifdef USE_UNIX_SOCKETS
  1037. if(socket_domain == AF_UNIX)
  1038. logmsg("Listening on Unix socket %s", unix_socket);
  1039. else
  1040. #endif
  1041. logmsg("Listening on port %hu", port);
  1042. wrotepidfile = write_pidfile(pidname);
  1043. if(!wrotepidfile) {
  1044. goto socks5_cleanup;
  1045. }
  1046. if(portname) {
  1047. wroteportfile = write_portfile(portname, port);
  1048. if(!wroteportfile) {
  1049. goto socks5_cleanup;
  1050. }
  1051. }
  1052. do {
  1053. juggle_again = incoming(sock);
  1054. } while(juggle_again);
  1055. socks5_cleanup:
  1056. if((msgsock != sock) && (msgsock != CURL_SOCKET_BAD))
  1057. sclose(msgsock);
  1058. if(sock != CURL_SOCKET_BAD)
  1059. sclose(sock);
  1060. #ifdef USE_UNIX_SOCKETS
  1061. if(unlink_socket && socket_domain == AF_UNIX) {
  1062. error = unlink(unix_socket);
  1063. logmsg("unlink(%s) = %d (%s)", unix_socket, error, strerror(error));
  1064. }
  1065. #endif
  1066. if(wrotepidfile)
  1067. unlink(pidname);
  1068. if(wroteportfile)
  1069. unlink(portname);
  1070. restore_signal_handlers(false);
  1071. if(got_exit_signal) {
  1072. logmsg("============> socksd exits with signal (%d)", exit_signal);
  1073. /*
  1074. * To properly set the return status of the process we
  1075. * must raise the same signal SIGINT or SIGTERM that we
  1076. * caught and let the old handler take care of it.
  1077. */
  1078. raise(exit_signal);
  1079. }
  1080. logmsg("============> socksd quits");
  1081. return 0;
  1082. }