net.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * net.c -- handles:
  22. * all raw network i/o
  23. *
  24. */
  25. #include <fcntl.h>
  26. #include "common.h"
  27. #include "net.h"
  28. #include "socket.h"
  29. #include "misc.h"
  30. #include "main.h"
  31. #include "debug.h"
  32. #include "dccutil.h"
  33. #include "enclink.h"
  34. #include "egg_timer.h"
  35. #include "traffic.h"
  36. #include "adns.h"
  37. #include <limits.h>
  38. #include <string.h>
  39. #include <netdb.h>
  40. #include <signal.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <setjmp.h>
  44. #if HAVE_SYS_SELECT_H
  45. # include <sys/select.h>
  46. #endif /* HAVE_SYS_SELECT_H */
  47. #include <netinet/in.h>
  48. #include <arpa/inet.h>
  49. #include <errno.h>
  50. #include <sys/stat.h>
  51. #if HAVE_UNISTD_H
  52. # include <unistd.h>
  53. #endif /* HAVE_UNITSTD_H */
  54. extern egg_traffic_t traffic;
  55. #ifdef HAVE_SSL
  56. SSL_CTX *ssl_c_ctx = NULL, *ssl_s_ctx = NULL;
  57. char *tls_rand_file = NULL;
  58. #endif /* HAVE_SSL */
  59. union sockaddr_union cached_myip4_so;
  60. #ifdef USE_IPV6
  61. union sockaddr_union cached_myip6_so;
  62. #endif /* USE_IPV6 */
  63. bool cached_ip = 0; /* Set to 1 after cache_my_ip is called */
  64. bool identd_hack = 0; /* identd_open() won't work on most servers, dont even bother warning. */
  65. char botuser[21] = ""; /* Username of the user running the bot */
  66. int socks_total = 0; /* total number of sockets */
  67. sock_list *socklist = NULL; /* Enough to be safe */
  68. int MAXSOCKS = 0;
  69. jmp_buf alarmret; /* Env buffer for alarm() returns */
  70. /* This *MUST* be an ip */
  71. char firewall[121] = ""; /* Socks server for firewall */
  72. port_t firewallport = 1080; /* Default port of Sock4/5 firewalls */
  73. /* Types of proxy */
  74. #define PROXY_SOCKS 1
  75. #define PROXY_SUN 2
  76. #define PROXY_HTTP 3
  77. /* I need an UNSIGNED long for dcc type stuff
  78. */
  79. unsigned long my_atoul(char *s)
  80. {
  81. unsigned long ret = 0;
  82. while ((*s >= '0') && (*s <= '9')) {
  83. ret *= 10;
  84. ret += ((*s) - '0');
  85. s++;
  86. }
  87. return ret;
  88. }
  89. /* get the protocol used on a socket */
  90. int sockprotocol(int sock)
  91. {
  92. struct sockaddr sa;
  93. socklen_t socklen = sizeof(sa);
  94. egg_bzero(&sa, socklen);
  95. if (getsockname(sock, &sa, &socklen))
  96. return -1;
  97. else
  98. return sa.sa_family;
  99. }
  100. /* AF_INET-independent resolving routine */
  101. static int get_ip(char *hostname, union sockaddr_union *so)
  102. {
  103. if (!hostname || (hostname && !hostname[0]))
  104. return 1;
  105. egg_memset(so, 0, sizeof(union sockaddr_union));
  106. debug1("get_ip(%s)", hostname);
  107. #ifdef USE_IPV6
  108. struct addrinfo hints, *ai = NULL, *res = NULL;
  109. int error = 0;
  110. egg_memset(&hints, 0, sizeof(struct addrinfo));
  111. hints.ai_socktype = SOCK_STREAM;
  112. if ((error = getaddrinfo(hostname, NULL, &hints, &res))) {
  113. if (res)
  114. freeaddrinfo(res);
  115. return error;
  116. }
  117. error = 1;
  118. for (ai = res; ai != NULL; ai = ai->ai_next) {
  119. if ((ai->ai_family == AF_INET6) || (ai->ai_family == AF_INET)) {
  120. memcpy(so, ai->ai_addr, ai->ai_addrlen);
  121. error = 0;
  122. break;
  123. }
  124. }
  125. if (res)
  126. freeaddrinfo(res);
  127. return error;
  128. #else
  129. struct hostent *hp = NULL;
  130. if (!(hp = gethostbyname(hostname)))
  131. return -1;
  132. memcpy(&so->sin.sin_addr, hp->h_addr, 4);
  133. so->sin.sin_family = AF_INET;
  134. return 0;
  135. #endif /* USE_IPV6 */
  136. }
  137. #ifdef HAVE_SSL
  138. int seed_PRNG(void)
  139. {
  140. char stackdata[1024] = "";
  141. static char rand_file[300] = "";
  142. FILE *fh = NULL;
  143. #if OPENSSL_VERSION_NUMBER >= 0x00905100
  144. if (RAND_status()) return 0;
  145. #endif /* OPENSSL_VERSION_NUMBER */
  146. if ((fh = fopen("/dev/urandom", "r"))) {
  147. fclose(fh);
  148. return 0;
  149. }
  150. if (RAND_file_name(rand_file, sizeof(rand_file)))
  151. tls_rand_file = rand_file;
  152. else
  153. return 1;
  154. if (!RAND_load_file(rand_file, 1024)) {
  155. unsigned int c;
  156. c = now;
  157. RAND_seed(&c, sizeof(c));
  158. c = getpid();
  159. RAND_seed(&c, sizeof(c));
  160. RAND_seed(stackdata, sizeof(stackdata));
  161. }
  162. #if OPENSSL_VERSION_NUMBER >= 0x00905100
  163. if (!RAND_status()) return 2;
  164. #endif /* OPENSSL_VERSION_NUMBER >= 0x00905100 */
  165. return 0;
  166. }
  167. #endif /* HAVE_SSL */
  168. /* Initialize the socklist
  169. */
  170. void init_net()
  171. {
  172. MAXSOCKS = max_dcc + 10;
  173. if (socklist)
  174. socklist = (sock_list *) my_realloc((void *) socklist, sizeof(sock_list) * MAXSOCKS);
  175. else
  176. socklist = (sock_list *) my_calloc(1, sizeof(sock_list) * MAXSOCKS);
  177. for (int i = 0; i < MAXSOCKS; i++) {
  178. egg_bzero(&socklist[i], sizeof(socklist[i]));
  179. #ifdef HAVE_SSL
  180. socklist[i].ssl = NULL;
  181. #endif /* HAVE_SSL */
  182. socklist[i].flags = SOCK_UNUSED;
  183. socklist[i].sock = -1;
  184. }
  185. #ifdef HAVE_SSL
  186. SSL_load_error_strings();
  187. OpenSSL_add_ssl_algorithms();
  188. ssl_c_ctx = SSL_CTX_new(SSLv23_client_method());
  189. ssl_s_ctx = SSL_CTX_new(SSLv23_server_method());
  190. if (!ssl_c_ctx || !ssl_s_ctx)
  191. fatal("SSL Inititlization failed", 0);
  192. if (seed_PRNG())
  193. fatal("SSL PRNG seeding failed!", 0);
  194. #endif /* HAVE_SSL */
  195. }
  196. #ifdef HAVE_SSL
  197. int ssl_cleanup() {
  198. if (ssl_c_ctx) {
  199. SSL_CTX_free(ssl_c_ctx);
  200. ssl_c_ctx = NULL;
  201. }
  202. if (ssl_s_ctx) {
  203. SSL_CTX_free(ssl_s_ctx);
  204. ssl_s_ctx = NULL;
  205. }
  206. if (tls_rand_file) RAND_write_file(tls_rand_file);
  207. return 0;
  208. }
  209. #endif /* HAVE_SSL */
  210. /* Get my ipv? ip
  211. */
  212. char *myipstr(int af_type)
  213. {
  214. if (cached_ip) {
  215. #ifdef USE_IPV6
  216. if (af_type == AF_INET6) {
  217. static char s[UHOSTLEN + 1] = "";
  218. egg_inet_ntop(AF_INET6, &cached_myip6_so.sin6.sin6_addr, s, 119);
  219. s[120] = 0;
  220. return s;
  221. } else
  222. #endif /* USE_IPV6 */
  223. if (af_type == AF_INET) {
  224. static char s[UHOSTLEN + 1] = "";
  225. egg_inet_ntop(AF_INET, &cached_myip4_so.sin.sin_addr, s, 119);
  226. s[120] = 0;
  227. return s;
  228. }
  229. }
  230. return "";
  231. }
  232. /* Get my ip number
  233. */
  234. in_addr_t getmyip() {
  235. return cached_myip4_so.sin.sin_addr.s_addr;
  236. }
  237. /* see if it's necessary to set inaddr_any... because if we can't resolve, we die anyway */
  238. void cache_my_ip()
  239. {
  240. #ifdef no
  241. cached_myip6_so.sin6.sin6_family = AF_INET6;
  242. cached_myip6_so.sin6.sin6_addr = in6addr_any;
  243. cached_myip4_so.sin.sin_family = AF_INET;
  244. cached_myip4_so.sin.sin_addr.s_addr = INADDR_ANY;
  245. #endif
  246. int error = 0;
  247. debug0("cache_my_ip()");
  248. egg_memset(&cached_myip4_so, 0, sizeof(union sockaddr_union));
  249. #ifdef USE_IPV6
  250. bool any = 0;
  251. egg_memset(&cached_myip6_so, 0, sizeof(union sockaddr_union));
  252. if (conf.bot->net.ip6) {
  253. if (get_ip(conf.bot->net.ip6, &cached_myip6_so))
  254. any = 1;
  255. } else if (conf.bot->net.host6) {
  256. if (get_ip(conf.bot->net.host6, &cached_myip6_so))
  257. any = 1;
  258. } else
  259. any = 1;
  260. if (any) {
  261. cached_myip6_so.sin6.sin6_family = AF_INET6;
  262. cached_myip6_so.sin6.sin6_addr = in6addr_any;
  263. }
  264. #endif /* USE_IPV6 */
  265. if (conf.bot->net.ip) {
  266. if (get_ip(conf.bot->net.ip, &cached_myip4_so))
  267. error = 1;
  268. } else if (conf.bot->net.host) {
  269. if (get_ip(conf.bot->net.host, &cached_myip4_so))
  270. error = 2;
  271. } else {
  272. /*
  273. char s[121] = "";
  274. gethostname(s, sizeof(s));
  275. if (get_ip(s, &cached_myip4_so)) {
  276. */
  277. /* error = 3; */
  278. cached_myip4_so.sin.sin_family = AF_INET;
  279. cached_myip4_so.sin.sin_addr.s_addr = INADDR_ANY;
  280. // }
  281. }
  282. if (error) {
  283. putlog(LOG_DEBUG, "*", "Hostname self-lookup error: %d", error);
  284. fatal("Hostname self-lookup failed.", 0);
  285. }
  286. cached_ip = 1;
  287. }
  288. /* Sets/Unsets options for a specific socket.
  289. *
  290. * Returns: 0 - on success
  291. * -1 - socket not found
  292. * -2 - illegal operation
  293. */
  294. int sockoptions(int sock, int operation, int sock_options)
  295. {
  296. for (int i = 0; i < MAXSOCKS; i++) {
  297. if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
  298. if (operation == EGG_OPTION_SET)
  299. socklist[i].flags |= sock_options;
  300. else if (operation == EGG_OPTION_UNSET)
  301. socklist[i].flags &= ~sock_options;
  302. else
  303. return -2;
  304. return 0;
  305. }
  306. }
  307. return -1;
  308. }
  309. int
  310. sock_read(FILE *f, bool enc)
  311. {
  312. char inbuf[1024] = "", *type = NULL, *buf = NULL, *buf_ptr = NULL;
  313. int fd = -1;
  314. const char salt1[] = SALT1;
  315. while (fgets(inbuf, sizeof(inbuf), f) != NULL) {
  316. remove_crlf(inbuf);
  317. if (enc)
  318. buf = buf_ptr = decrypt_string(salt1, inbuf);
  319. else
  320. buf = inbuf;
  321. if (!strcmp(buf, "+sock")) {
  322. if (enc)
  323. free(buf_ptr);
  324. return fd;
  325. }
  326. type = newsplit(&buf);
  327. if (!strcmp(type, "sock")) {
  328. int sock = atoi(newsplit(&buf)), options = atoi(newsplit(&buf));
  329. fd = allocsock(sock, options);
  330. }
  331. if (fd >= 0) {
  332. #ifdef USE_IPV6
  333. if (!strcmp(type, "af"))
  334. socklist[fd].af = atoi(buf);
  335. #endif
  336. if (!strcmp(type, "host"))
  337. socklist[fd].host = strdup(buf);
  338. if (!strcmp(type, "port"))
  339. socklist[fd].port = atoi(buf);
  340. }
  341. if (enc)
  342. free(buf_ptr);
  343. }
  344. return -1;
  345. }
  346. void
  347. sock_write(FILE *f, int fd)
  348. {
  349. if (socklist[fd].sock > 0) {
  350. lfprintf(f, "-sock\n");
  351. lfprintf(f, "sock %d %d\n", socklist[fd].sock, socklist[fd].flags);
  352. #ifdef USE_IPV6
  353. lfprintf(f, "af %u\n", socklist[fd].af);
  354. #endif
  355. if (socklist[fd].host)
  356. lfprintf(f, "host %s\n", socklist[fd].host);
  357. if (socklist[fd].port)
  358. lfprintf(f, "port %d\n", socklist[fd].port);
  359. lfprintf(f, "+sock\n");
  360. }
  361. }
  362. /* Return a free entry in the socket entry
  363. */
  364. int allocsock(int sock, int options)
  365. {
  366. for (int i = 0; i < MAXSOCKS; i++) {
  367. if (socklist[i].flags & SOCK_UNUSED) {
  368. /* yay! there is table space */
  369. socklist[i].inbuf = socklist[i].outbuf = NULL;
  370. socklist[i].inbuflen = socklist[i].outbuflen = 0;
  371. #ifdef HAVE_SSL
  372. socklist[i].ssl = NULL;
  373. #endif /* HAVE_SSL */
  374. socklist[i].flags = options;
  375. socklist[i].sock = sock;
  376. socklist[i].encstatus = 0;
  377. socklist[i].enclink = -1;
  378. socklist[i].gz = 0;
  379. egg_bzero(&(socklist[i].okey), ENC_KEY_LEN + 1);
  380. egg_bzero(&(socklist[i].ikey), ENC_KEY_LEN + 1);
  381. socks_total++;
  382. sdprintf("allocsock(%d) = %d", i, sock);
  383. return i;
  384. }
  385. }
  386. fatal("Socket table is full!", 0);
  387. return -1; /* Never reached */
  388. }
  389. /* Request a normal socket for i/o
  390. */
  391. void setsock(int sock, int options)
  392. {
  393. int i = allocsock(sock, options);
  394. bool parm;
  395. if (((sock != STDOUT) || backgrd) && !(socklist[i].flags & SOCK_NONSOCK)) {
  396. parm = 1;
  397. setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *) &parm, sizeof(int));
  398. parm = 0;
  399. setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &parm, sizeof(int));
  400. }
  401. if (options & SOCK_LISTEN) {
  402. /* Tris says this lets us grab the same port again next time */
  403. parm = 1;
  404. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &parm, sizeof(int));
  405. }
  406. /* Yay async i/o ! */
  407. fcntl(sock, F_SETFL, O_NONBLOCK);
  408. }
  409. #ifdef USE_IPV6
  410. int real_getsock(int options, int af_def, char *fname, int line)
  411. {
  412. #else
  413. int real_getsock(int options, char *fname, int line)
  414. {
  415. int af_def = AF_INET;
  416. #endif /* USE_IPV6 */
  417. int sock = -1;
  418. if (!af_def)
  419. af_def = AF_INET;
  420. sock = socket(af_def, SOCK_STREAM, 0);
  421. if (sock >= 0)
  422. setsock(sock, options);
  423. else if (!identd_hack)
  424. putlog(LOG_WARNING, "*", "Warning: Can't create new socket! (%s:%d): %s", fname, line, strerror(errno));
  425. else if (identd_hack)
  426. identd_hack = 0;
  427. return sock;
  428. }
  429. #ifdef HAVE_SSL
  430. void dropssl(register int sock)
  431. {
  432. if (sock < 0)
  433. return;
  434. int i;
  435. for (i = 0; (i < MAXSOCKS); i++)
  436. if (socklist[i].sock == sock) break;
  437. if (socklist[i].ssl) {
  438. SSL_set_quiet_shutdown(socklist[i].ssl, 1);
  439. SSL_shutdown(socklist[i].ssl);
  440. usleep(1000 * 500);
  441. SSL_free(socklist[i].ssl);
  442. usleep(1000 * 500);
  443. socklist[i].ssl = NULL;
  444. }
  445. }
  446. #endif /* HAVE_SSL */
  447. /* Done with a socket
  448. */
  449. void real_killsock(register int sock, const char *file, int line)
  450. {
  451. if (sock < 0) {
  452. putlog(LOG_ERRORS, "*", "Attempt to kill socket -1 %s:%d", file, line);
  453. return;
  454. }
  455. for (register int i = 0; i < MAXSOCKS; i++) {
  456. if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
  457. #ifdef HAVE_SSL
  458. dropssl(sock);
  459. #endif /* HAVE_SSL */
  460. close(socklist[i].sock);
  461. if (socklist[i].inbuf != NULL) {
  462. free(socklist[i].inbuf);
  463. socklist[i].inbuf = NULL;
  464. }
  465. if (socklist[i].outbuf != NULL) {
  466. free(socklist[i].outbuf);
  467. socklist[i].outbuf = NULL;
  468. socklist[i].outbuflen = 0;
  469. }
  470. if (socklist[i].host)
  471. free(socklist[i].host);
  472. egg_bzero(&socklist[i], sizeof(socklist[i]));
  473. socklist[i].flags = SOCK_UNUSED;
  474. socks_total--;
  475. sdprintf("killsock(%d, %s, %d) (socklist: %d)", sock, file, line, i);
  476. return;
  477. }
  478. }
  479. putlog(LOG_MISC, "*", "Attempt to kill un-allocated socket %d %s:%d !!", sock, file, line);
  480. }
  481. /* Send connection request to proxy
  482. */
  483. static int proxy_connect(int sock, const char *ip, port_t port, int proxy_type)
  484. {
  485. sdprintf("proxy_connect(%d, %s, %d, %d)", sock, ip, port, proxy_type);
  486. #ifdef USE_IPV6
  487. unsigned char x[32] = "";
  488. int af_ty = sockprotocol(sock);
  489. #else
  490. unsigned char x[10] = "";
  491. #endif /* USE_IPV6 */
  492. char s[256] = "";
  493. /* socks proxy */
  494. if (proxy_type == PROXY_SOCKS) {
  495. /* numeric IP? */
  496. if (is_dotted_ip(ip)) {
  497. in_addr_t ipaddr = ((in_addr_t) inet_addr(ip));
  498. egg_memcpy(x, &ipaddr, 4);
  499. } else { /* if not resolved, resolve it with blocking calls.. (shouldn't happen ever) */
  500. return -1;
  501. }
  502. for (int i = 0; i < MAXSOCKS; i++)
  503. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
  504. socklist[i].flags |= SOCK_PROXYWAIT;
  505. #ifdef USE_IPV6
  506. if (af_ty == AF_INET6)
  507. simple_snprintf(s, sizeof s,
  508. "\004\001%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%s",
  509. (port >> 8) % 256, (port % 256), x[0], x[1], x[2], x[3],
  510. x[4], x[5], x[6], x[7], x[9], x[9], x[10], x[11], x[12],
  511. x[13], x[14], x[15], botuser);
  512. else
  513. #endif /* USE_IPV6 */
  514. simple_snprintf(s, sizeof s, "\004\001%c%c%c%c%c%c%s", (port >> 8) % 256,
  515. (port % 256), x[0], x[1], x[2], x[3], botuser);
  516. tputs(sock, s, strlen(botuser) + 9);
  517. } else if (proxy_type == PROXY_SUN) {
  518. simple_snprintf(s, sizeof s, "%s %d\n", ip, port);
  519. tputs(sock, s, strlen(s));
  520. } else if (proxy_type == PROXY_HTTP) {
  521. simple_snprintf(s, sizeof s, "CONNECT %s:%d\n\n", ip, port);
  522. tputs(sock, s, strlen(s));
  523. }
  524. return sock;
  525. }
  526. /* FIXME: REPLACE WITH SOCK_NAME() */
  527. void initialize_sockaddr(int af_type, const char *host, port_t port, union sockaddr_union *so)
  528. {
  529. egg_bzero(so, sizeof(*so));
  530. so->sa.sa_family = af_type;
  531. if (af_type == AF_INET) {
  532. so->sin.sin_family = AF_INET;
  533. if (host) {
  534. inet_pton(AF_INET, host, &so->sin.sin_addr);
  535. so->sin.sin_port = htons(port);
  536. } else {
  537. so->sin.sin_addr.s_addr = getmyip();
  538. }
  539. #ifdef USE_IPV6
  540. } else {
  541. so->sin6.sin6_family = AF_INET6;
  542. if (host) {
  543. inet_pton(AF_INET6, host, &so->sin6.sin6_addr);
  544. so->sin6.sin6_port = htons(port);
  545. } else {
  546. memcpy(&so->sin6.sin6_addr, &cached_myip6_so.sin6.sin6_addr, 16);
  547. }
  548. #endif /* USE_IPV6 */
  549. }
  550. }
  551. /* Starts a connection attempt to a socket
  552. *
  553. * If given a normal hostname, this will be resolved to the corresponding
  554. * IP address first. PLEASE try to use the non-blocking dns functions
  555. * instead and then call this function with the IP address to avoid blocking.
  556. *
  557. * returns <0 if connection refused:
  558. * -1 strerror()/errno type error
  559. * -2 can't resolve hostname
  560. */
  561. int open_telnet_raw(int sock, const char *ipIn, port_t sport, bool proxy_on, bool identd)
  562. {
  563. static port_t port = 0;
  564. union sockaddr_union so;
  565. char ip[121] = "";
  566. int is_resolved = 0;
  567. volatile int proxy_type = 0, proxy = proxy_on;
  568. /* firewall? use socks */
  569. if (proxy) {
  570. switch (firewall[0]) {
  571. case '!':
  572. proxy_type = PROXY_SUN;
  573. strlcpy(ip, &firewall[1], sizeof(ip));
  574. break;
  575. case '@':
  576. proxy_type = PROXY_HTTP;
  577. strlcpy(ip, &firewall[1], sizeof(ip));
  578. break;
  579. default:
  580. proxy_type = PROXY_SOCKS;
  581. strlcpy(ip, firewall, sizeof(ip));
  582. break;
  583. }
  584. port = firewallport;
  585. } else {
  586. proxy_type = 0;
  587. strlcpy(ip, ipIn, sizeof(ip));
  588. port = sport;
  589. }
  590. /* figure out which ip to bind to locally (v4 or v6) based on what the host ip is .. */
  591. if ((is_resolved = is_dotted_ip(ip))) { /* already resolved */
  592. /* bind to our cached ip for v4/v6 depending on what the ip is */
  593. initialize_sockaddr(is_resolved, NULL, 0, &so);
  594. if (bind(sock, &so.sa, SIZEOF_SOCKADDR(so)) < 0) {
  595. putlog(LOG_DEBUG, "*", "Failed to bind to socket %d: %s", sock, strerror(errno));
  596. killsock(sock);
  597. return -1;
  598. }
  599. /* initialize so for connect using the host/port */
  600. initialize_sockaddr(is_resolved, ip, port, &so);
  601. } else { /* if not resolved, resolve it with blocking calls.. (shouldn't happen ever) */
  602. sdprintf("WARNING: open_telnet_raw(%s,%d) was passed an unresolved hostname.", ip, port);
  603. return -1;
  604. }
  605. for (int i = 0; i < MAXSOCKS; i++) {
  606. if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock)) {
  607. socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT;
  608. socklist[i].host = strdup(ipIn);
  609. socklist[i].port = port;
  610. break;
  611. }
  612. }
  613. if (identd)
  614. identd_open(myipstr(is_resolved), ipIn);
  615. int rc = -1;
  616. /* make the connect attempt */
  617. rc = connect(sock, &so.sa, SIZEOF_SOCKADDR(so));
  618. if (rc < 0) {
  619. if (errno == EINPROGRESS) {
  620. debug3("net: connect(%d, %s, %d)", sock, ipIn, sport);
  621. /* Firewall? announce connect attempt to proxy */
  622. if (proxy)
  623. return proxy_connect(sock, ipIn, sport, proxy_type);
  624. return sock; /* async success! */
  625. } else {
  626. sdprintf("connect(%d, %s, %d) failed: %s", sock, ip, sport, strerror(errno));
  627. killsock(sock);
  628. return -1;
  629. }
  630. }
  631. /* Synchronous? :/ */
  632. debug3("net: (BLOCKING) connect(%d, %s, %d)", sock, ipIn, sport);
  633. if (proxy)
  634. return proxy_connect(sock, ipIn, sport, proxy_type);
  635. return sock;
  636. }
  637. /* Ordinary non-binary connection attempt */
  638. int open_telnet(const char *ip, port_t port, bool proxy, bool identd)
  639. {
  640. int sock = -1;
  641. #ifdef USE_IPV6
  642. sock = getsock(0, is_dotted_ip(ip));
  643. #else
  644. sock = getsock(0);
  645. #endif /* USE_IPV6 */
  646. if (sock >= 0)
  647. return open_telnet_raw(sock, ip, port, proxy, identd);
  648. return -1;
  649. }
  650. /* Returns a socket number for a listening socket that will accept any
  651. * connection on a certain address -- port # is returned in port
  652. *
  653. * 'addr' is ignored if af_def is AF_INET6 -poptix (02/03/03)
  654. */
  655. #ifdef USE_IPV6
  656. int open_address_listen(in_addr_t addr, int af_def, port_t *port)
  657. #else
  658. int open_address_listen(in_addr_t addr, port_t *port)
  659. #endif /* USE_IPV6 */
  660. {
  661. // if (firewall[0]) {
  662. // /* FIXME: can't do listen port thru firewall yet */
  663. // putlog(LOG_MISC, "*", "!! Cant open a listen port (you are using a firewall)");
  664. // return -1;
  665. // }
  666. int sock = 0;
  667. socklen_t addrlen;
  668. struct sockaddr_in name;
  669. #ifdef USE_IPV6
  670. if (af_def == AF_INET6) {
  671. struct sockaddr_in6 name6;
  672. sock = getsock(SOCK_LISTEN, af_def);
  673. if (sock < 0)
  674. return -1;
  675. debug2("Opening listen socket on port %d with AF_INET6, sock: %d", *port, sock);
  676. egg_bzero((char *) &name6, sizeof(name6));
  677. name6.sin6_family = af_def;
  678. name6.sin6_port = htons(*port); /* 0 = just assign us a port */
  679. /* memcpy(&name6.sin6_addr, &in6addr_any, 16); */ /* this is the only way to get ipv6+ipv4 in 1 socket */
  680. memcpy(&name6.sin6_addr, &cached_myip6_so.sin6.sin6_addr, 16);
  681. if (bind(sock, (struct sockaddr *) &name6, sizeof(name6)) < 0) {
  682. if (!(identd_hack && *port == 113))
  683. putlog(LOG_DEBUG, "*", "Failed to bind to socket %d for listen on port %d: %s", sock, *port, strerror(errno));
  684. killsock(sock);
  685. return -1;
  686. }
  687. addrlen = sizeof(name6);
  688. if (getsockname(sock, (struct sockaddr *) &name6, &addrlen) < 0) {
  689. if (!(identd_hack && *port == 113))
  690. putlog(LOG_DEBUG, "*", "Failed to getsockname on socket %d for listen on port %d: %s", sock, *port, strerror(errno));
  691. killsock(sock);
  692. return -1;
  693. }
  694. *port = ntohs(name6.sin6_port);
  695. if (listen(sock, 1) < 0) {
  696. if (!(identd_hack && *port == 113))
  697. putlog(LOG_DEBUG, "*", "Failed to listen on socket %d for listen on port %d: %s", sock, *port, strerror(errno));
  698. killsock(sock);
  699. return -1;
  700. }
  701. } else {
  702. sock = getsock(SOCK_LISTEN, AF_INET);
  703. #else
  704. sock = getsock(SOCK_LISTEN);
  705. #endif /* USE_IPV6 */
  706. if (sock < 0)
  707. return -1;
  708. debug2("Opening listen socket on port %d with AF_INET, sock: %d", *port, sock);
  709. egg_bzero((char *) &name, sizeof(struct sockaddr_in));
  710. name.sin_family = AF_INET;
  711. name.sin_port = htons(*port); /* 0 = just assign us a port */
  712. name.sin_addr.s_addr = addr;
  713. if (bind(sock, (struct sockaddr *) &name, sizeof(name)) < 0) {
  714. if (!(identd_hack && *port == 113))
  715. putlog(LOG_DEBUG, "*", "Failed to bind to socket %d for listen on port %d: %s", sock, *port, strerror(errno));
  716. killsock(sock);
  717. return -1;
  718. }
  719. /* what port are we on? */
  720. addrlen = sizeof(name);
  721. if (getsockname(sock, (struct sockaddr *) &name, &addrlen) < 0) {
  722. if (!(identd_hack && *port == 113))
  723. putlog(LOG_DEBUG, "*", "Failed to getsockname on socket %d for listen on port %d: %s", sock, *port, strerror(errno));
  724. killsock(sock);
  725. return -1;
  726. }
  727. *port = ntohs(name.sin_port);
  728. if (listen(sock, 1) < 0) {
  729. if (!(identd_hack && *port == 113))
  730. putlog(LOG_DEBUG, "*", "Failed to listen on socket %d for on port %d: %s", sock, *port, strerror(errno));
  731. killsock(sock);
  732. return -1;
  733. }
  734. #ifdef USE_IPV6
  735. }
  736. #endif /* USE_IPV6 */
  737. debug2("Opened listen socket on port %d with AF_INET, sock: %d", *port, sock);
  738. return sock;
  739. }
  740. /* Returns a socket number for a listening socket that will accept any
  741. * connection -- port # is returned in port
  742. */
  743. int open_listen(port_t *port)
  744. {
  745. #ifdef USE_IPV6
  746. return open_address_listen(getmyip(), AF_INET, port);
  747. #else
  748. return open_address_listen(getmyip(), port);
  749. #endif /* USE_IPV6 */
  750. }
  751. /* Same as above, except this one can be called with an AF_ type
  752. * the above is being left in for compatibility, and should NOT LONGER BE USED IN THE CORE CODE.
  753. */
  754. int open_listen_by_af(port_t *port, int af_def)
  755. {
  756. #ifdef USE_IPV6
  757. return open_address_listen(getmyip(), af_def, port);
  758. #else
  759. return -1;
  760. #endif /* USE_IPV6 */
  761. }
  762. #ifdef HAVE_SSL
  763. int ssl_link(register int sock, int state)
  764. {
  765. int err = 0, i = 0, errs = 0;
  766. debug2("ssl_link(%d, %d)", sock, state);
  767. for (i = 0; (i < MAXSOCKS); i++) {
  768. if (socklist[i].sock == sock) break;
  769. }
  770. if (socklist[i].ssl) {
  771. putlog(LOG_ERROR, "*", "Switching to SSL (%d,%d) - already active", state, sock);
  772. return 0;
  773. }
  774. if (state == CONNECT_SSL) {
  775. socklist[i].ssl = SSL_new(ssl_c_ctx);
  776. } else if (state == ACCEPT_SSL) {
  777. socklist[i].ssl = SSL_new(ssl_s_ctx);
  778. }
  779. if (!socklist[i].ssl) {
  780. putlog(LOG_ERROR, "*", "Switching to SSL (%d) - SSL_new(%d) failed", sock, state);
  781. return 0;
  782. }
  783. if (!SSL_set_fd(socklist[i].ssl, socklist[i].sock)) {
  784. putlog(LOG_ERROR, "*", "SSL_set_fd(%d) (%d) failed", state, socklist[i].sock);
  785. return 0;
  786. }
  787. if (state == CONNECT_SSL) {
  788. SSL_set_connect_state(socklist[i].ssl);
  789. } else if (state == ACCEPT_SSL) {
  790. SSL_set_accept_state(socklist[i].ssl);
  791. } else {
  792. putlog(LOG_DEBUG, "*", "ssl_link(%d, 0?) NO STATE?", sock);
  793. return 0;
  794. }
  795. if (state == CONNECT_SSL) {
  796. err = SSL_connect(socklist[i].ssl);
  797. } else if (state == ACCEPT_SSL) {
  798. err = SSL_accept(socklist[i].ssl);
  799. }
  800. if (!setjmp(alarmret)) {
  801. alarm(5); /* this is plenty of time */
  802. while ((err < 1) && (errno == EAGAIN)) {
  803. if (state == CONNECT_SSL) {
  804. err = SSL_connect(socklist[i].ssl);
  805. } else if (state == ACCEPT_SSL) {
  806. err = SSL_accept(socklist[i].ssl);
  807. }
  808. /* if ((errs!=SSL_ERROR_WANT_READ)&&(errs!=SSL_ERROR_WANT_WRITE)&& (errs!=SSL_ERROR_WANT_X509_LOOKUP)) */
  809. /* break; anything not one of these is a sufficient condition to break out... */
  810. }
  811. alarm(0);
  812. }
  813. errs = SSL_get_error(socklist[i].ssl, err);
  814. putlog(LOG_DEBUG, "*", "SSL_link(%d, %d) = %d, errs: %d (%d), %s", sock, state, err, errs, errno, (char *)ERR_error_string(ERR_get_error(), NULL));
  815. if (errno) putlog(LOG_DEBUG, "*", "errno %d: %s", errno, strerror(errno));
  816. if (err == 1) {
  817. putlog(LOG_ERROR, "*", "SSL_link(%d, %d) was successfull", sock, state);
  818. return 1;
  819. } else {
  820. putlog(LOG_ERROR, "*", "SSL_link(%d, %d) failed", sock, state);
  821. dropssl(socklist[i].sock);
  822. }
  823. return 0;
  824. }
  825. #endif /* HAVE_SSL */
  826. /* Returns the given network byte order IP address in the
  827. * dotted format - "##.##.##.##"
  828. */
  829. char *iptostr(in_addr_t ip)
  830. {
  831. static char ipbuf[32];
  832. struct in_addr a;
  833. a.s_addr = ip;
  834. return (char *) egg_inet_ntop(AF_INET, &a, ipbuf, sizeof(ipbuf));
  835. }
  836. /* Short routine to answer a connect received on a socket made previously
  837. * by open_listen ... returns hostname of the caller & the new socket
  838. * does NOT dispose of old "public" socket!
  839. */
  840. int answer(int sock, char *caller, in_addr_t *ip, port_t *port, int binary)
  841. {
  842. int new_sock;
  843. socklen_t addrlen;
  844. struct sockaddr_in from;
  845. #ifdef USE_IPV6
  846. int af_ty = sockprotocol(sock);
  847. struct sockaddr_in6 from6;
  848. egg_bzero(&from6, sizeof(struct sockaddr_in6));
  849. if (af_ty == AF_INET6) {
  850. addrlen = sizeof(from6);
  851. new_sock = accept(sock, (struct sockaddr *) &from6, &addrlen);
  852. } else {
  853. #endif /* USE_IPV6 */
  854. addrlen = sizeof(struct sockaddr);
  855. new_sock = accept(sock, (struct sockaddr *) &from, &addrlen);
  856. #ifdef USE_IPV6
  857. }
  858. #endif /* USE_IPV6 */
  859. if (new_sock < 0)
  860. return -1;
  861. if (ip != NULL) {
  862. #ifdef USE_IPV6
  863. /* Detect IPv4 in IPv6 mapped address .... */
  864. if (af_ty == AF_INET6 && (!IN6_IS_ADDR_V4MAPPED(&from6.sin6_addr))) {
  865. egg_inet_ntop(AF_INET6, &from6.sin6_addr, caller, 119);
  866. caller[120] = 0;
  867. *ip = 0L;
  868. } else if (IN6_IS_ADDR_V4MAPPED(&from6.sin6_addr)) { /* ...and convert it to plain (AF_INET) IPv4 address (openssh) */
  869. struct sockaddr_in *from4 = (struct sockaddr_in *)&from6;
  870. struct in_addr addr;
  871. memcpy(&addr, ((char *)&from6.sin6_addr) + 12, sizeof(addr));
  872. egg_memset(&from, 0, sizeof(from));
  873. from4->sin_family = AF_INET;
  874. addrlen = sizeof(*from4);
  875. memcpy(&from4->sin_addr, &addr, sizeof(addr));
  876. *ip = from4->sin_addr.s_addr;
  877. strlcpy(caller, iptostr(*ip), 121);
  878. *ip = ntohl(*ip);
  879. } else {
  880. #endif /* USE_IPV6 */
  881. *ip = from.sin_addr.s_addr;
  882. strlcpy(caller, iptostr(*ip), 121);
  883. *ip = ntohl(*ip);
  884. #ifdef USE_IPV6
  885. }
  886. #endif /* USE_IPV6 */
  887. }
  888. if (port != NULL) {
  889. #ifdef USE_IPV6
  890. if (af_ty == AF_INET6)
  891. *port = ntohs(from6.sin6_port);
  892. else
  893. #endif /* USE_IPV6 */
  894. *port = ntohs(from.sin_port);
  895. }
  896. /* Set up all the normal socket crap */
  897. setsock(new_sock, (binary ? SOCK_BINARY : 0));
  898. sdprintf("Answered socket %d: %s", new_sock, caller);
  899. return new_sock;
  900. }
  901. /* Like open_telnet, but uses ip & port specifications of dcc
  902. Take a longip and make into dotted form
  903. */
  904. int open_telnet_dcc(int sock, char *ip, char *port)
  905. {
  906. port_t p;
  907. unsigned long addr;
  908. char sv[100] = "";
  909. unsigned char c[4] = "";
  910. #ifdef DEBUG_IPV6
  911. debug1("open_telnet_dcc %s", ip);
  912. #endif /* DEBUG_IPV6 */
  913. if (port != NULL)
  914. p = atoi(port);
  915. else
  916. p = 2000;
  917. #ifdef USE_IPV6
  918. if (sockprotocol(sock) == AF_INET6) {
  919. # ifdef DEBUG_IPV6
  920. debug0("open_telnet_dcc, af_inet6!");
  921. # endif /* DEBUG_IPV6 */
  922. strlcpy(sv, ip, sizeof(sv));
  923. } else {
  924. #endif /* USE_IPV6 */
  925. if (ip != NULL)
  926. addr = my_atoul(ip);
  927. else
  928. addr = 0L;
  929. if (addr < (1 << 24))
  930. return -3; /* fake address */
  931. c[0] = (addr >> 24) & 0xff;
  932. c[1] = (addr >> 16) & 0xff;
  933. c[2] = (addr >> 8) & 0xff;
  934. c[3] = addr & 0xff;
  935. simple_snprintf(sv, sizeof(sv), "%u.%u.%u.%u", c[0], c[1], c[2], c[3]);
  936. #ifdef USE_IPV6
  937. }
  938. /* strcpy(sv,hostnamefromip(addr)); */
  939. # ifdef DEBUG_IPV6
  940. debug3("open_telnet_raw %s %d %d", sv, sock, p);
  941. # endif /* DEBUG_IPV6 */
  942. #endif /* USE_IPV6 */
  943. return open_telnet_raw(sock, sv, p, 0);
  944. }
  945. /* Attempts to read from all the sockets in socklist
  946. * fills s with up to 511 bytes if available, and returns the array index
  947. *
  948. * on EOF: returns -1, with socket in len
  949. * on socket error: returns -2
  950. * if nothing is ready: returns -3
  951. */
  952. static int sockread(char *s, int *len)
  953. {
  954. fd_set fd;
  955. int fds = 0, i, fdtmp, x;
  956. struct timeval t;
  957. int grab = SGRAB + 1;
  958. egg_timeval_t howlong;
  959. if (timer_get_shortest(&howlong)) {
  960. /* No timer, default to 1 second. */
  961. t.tv_sec = 1;
  962. t.tv_usec = 0;
  963. }
  964. else {
  965. t.tv_sec = howlong.sec;
  966. t.tv_usec = howlong.usec;
  967. }
  968. FD_ZERO(&fd);
  969. for (i = 0; i < MAXSOCKS; i++) {
  970. if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_VIRTUAL))) {
  971. if ((socklist[i].sock == STDOUT) && !backgrd)
  972. fdtmp = STDIN;
  973. else
  974. fdtmp = socklist[i].sock;
  975. if (fdtmp > fds)
  976. fds = fdtmp;
  977. FD_SET(fdtmp, &fd);
  978. }
  979. }
  980. fds++;
  981. x = select(fds, &fd, NULL, NULL, &t);
  982. if (x > 0) {
  983. /* Something happened */
  984. for (i = 0; i < MAXSOCKS; i++) {
  985. if ((!(socklist[i].flags & SOCK_UNUSED)) && ((FD_ISSET(socklist[i].sock, &fd)) ||
  986. #ifdef HAVE_SSL
  987. ((socklist[i].ssl) && (SSL_pending(socklist[i].ssl))) ||
  988. #endif /* HAVE_SSL */
  989. ((socklist[i].sock == STDOUT) && (!backgrd) && (FD_ISSET(STDIN, &fd))))) {
  990. if (socklist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
  991. /* Listening socket -- don't read, just return activity */
  992. /* Same for connection attempt */
  993. /* (for strong connections, require a read to succeed first) */
  994. if (socklist[i].flags & SOCK_PROXYWAIT) { /* drummer */
  995. /* Hang around to get the return code from proxy */
  996. grab = 10;
  997. } else if (!(socklist[i].flags & SOCK_STRONGCONN)) {
  998. debug1("net: connect! sock %d", socklist[i].sock);
  999. s[0] = 0;
  1000. *len = 0;
  1001. #ifdef HAVE_SSL
  1002. /* debug0("CALLING SSL_LINK() FROM SOCKREAD");
  1003. if (!ssl_link(socklist[i].sock))
  1004. debug0("SSL_LINK FAILED");
  1005. debug0("BACK FROM SSL_LINK()"); */
  1006. #endif /* HAVE_SSL */
  1007. return i;
  1008. }
  1009. } else if (socklist[i].flags & SOCK_PASS) {
  1010. s[0] = 0;
  1011. *len = 0;
  1012. return i;
  1013. }
  1014. errno = 0;
  1015. if ((socklist[i].sock == STDOUT) && !backgrd)
  1016. x = read(STDIN, s, grab);
  1017. else {
  1018. #ifdef HAVE_SSL
  1019. if (socklist[i].ssl) {
  1020. x = SSL_read(socklist[i].ssl, s, grab);
  1021. if (x < 0) {
  1022. int err = SSL_get_error(socklist[i].ssl, x);
  1023. x = -1;
  1024. switch (err) {
  1025. case SSL_ERROR_WANT_READ:
  1026. errno = EAGAIN;
  1027. break;
  1028. case SSL_ERROR_WANT_WRITE:
  1029. errno = EAGAIN;
  1030. break;
  1031. case SSL_ERROR_WANT_X509_LOOKUP:
  1032. errno = EAGAIN;
  1033. break;
  1034. }
  1035. }
  1036. } else
  1037. #endif /* HAVE_SSL */
  1038. x = read(socklist[i].sock, s, grab);
  1039. }
  1040. if (x <= 0) { /* eof */
  1041. if (errno != EAGAIN) { /* EAGAIN happens when the operation would block
  1042. on a non-blocking socket, if the socket is going
  1043. to die, it will die later, otherwise it will connect */
  1044. *len = socklist[i].sock;
  1045. socklist[i].flags &= ~SOCK_CONNECT;
  1046. debug1("net: eof!(read) socket %d", socklist[i].sock);
  1047. return -1;
  1048. } else {
  1049. debug3("sockread EAGAIN: %d %d (%s)", socklist[i].sock, errno, strerror(errno));
  1050. continue; /* EAGAIN */
  1051. }
  1052. }
  1053. s[x] = 0;
  1054. *len = x;
  1055. if (socklist[i].flags & SOCK_PROXYWAIT) {
  1056. debug2("net: socket: %d proxy errno: %d", socklist[i].sock, s[1]);
  1057. socklist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
  1058. switch (s[1]) {
  1059. case 90: /* Success */
  1060. s[0] = 0;
  1061. *len = 0;
  1062. return i;
  1063. case 91: /* Failed */
  1064. errno = ECONNREFUSED;
  1065. break;
  1066. case 92: /* No identd */
  1067. case 93: /* Identd said wrong username */
  1068. /* A better error message would be "socks misconfigured"
  1069. * or "identd not working" but this is simplest.
  1070. */
  1071. errno = ENETUNREACH;
  1072. break;
  1073. }
  1074. *len = socklist[i].sock;
  1075. return -1;
  1076. }
  1077. return i;
  1078. }
  1079. }
  1080. } else if (x == -1)
  1081. return -2; /* socket error */
  1082. else {
  1083. s[0] = 0;
  1084. *len = 0;
  1085. }
  1086. return -3;
  1087. }
  1088. /* sockgets: buffer and read from sockets
  1089. *
  1090. * Attempts to read from all registered sockets for up to one second. if
  1091. * after one second, no complete data has been received from any of the
  1092. * sockets, 's' will be empty, 'len' will be 0, and sockgets will return -3.
  1093. * if there is returnable data received from a socket, the data will be
  1094. * in 's' (null-terminated if non-binary), the length will be returned
  1095. * in len, and the socket number will be returned.
  1096. * normal sockets have their input buffered, and each call to sockgets
  1097. * will return one line terminated with a '\n'. binary sockets are not
  1098. * buffered and return whatever coems in as soon as it arrives.
  1099. * listening sockets will return an empty string when a connection comes in.
  1100. * connecting sockets will return an empty string on a successful connect,
  1101. * or EOF on a failed connect.
  1102. * if an EOF is detected from any of the sockets, that socket number will be
  1103. * put in len, and -1 will be returned.
  1104. * the maximum length of the string returned is 512 (including null)
  1105. *
  1106. * Returns -4 if we handled something that shouldn't be handled by the
  1107. * dcc functions. Simply ignore it.
  1108. */
  1109. int sockgets(char *s, int *len)
  1110. {
  1111. char xx[SGRAB + 4] = "", *p = NULL, *px = NULL;
  1112. int ret;
  1113. for (int i = 0; i < MAXSOCKS; i++) {
  1114. /* Check for stored-up data waiting to be processed */
  1115. if (!(socklist[i].flags & SOCK_UNUSED) && !(socklist[i].flags & SOCK_BUFFER) && (socklist[i].inbuf != NULL)) {
  1116. if (!(socklist[i].flags & SOCK_BINARY)) {
  1117. /* look for \r too cos windows can't follow RFCs */
  1118. p = strchr(socklist[i].inbuf, '\n');
  1119. if (p == NULL)
  1120. p = strchr(socklist[i].inbuf, '\r');
  1121. if (p != NULL) {
  1122. *p = 0;
  1123. if (strlen(socklist[i].inbuf) > SGRAB)
  1124. socklist[i].inbuf[SGRAB] = 0;
  1125. strlcpy(s, socklist[i].inbuf, SGRAB + 10); //buf@main.c
  1126. size_t psiz = strlen(p + 1) + 1;
  1127. px = (char *) my_calloc(1, psiz);
  1128. strlcpy(px, p + 1, psiz);
  1129. free(socklist[i].inbuf);
  1130. if (px[0])
  1131. socklist[i].inbuf = px;
  1132. else {
  1133. free(px);
  1134. socklist[i].inbuf = NULL;
  1135. }
  1136. /* Strip CR if this was CR/LF combo */
  1137. if (s[strlen(s) - 1] == '\r')
  1138. s[strlen(s) - 1] = 0;
  1139. if (s[0] && socklist[i].encstatus)
  1140. link_read(i, s, (size_t *) len);
  1141. *len = strlen(s);
  1142. return socklist[i].sock;
  1143. }
  1144. } else {
  1145. /* i dont think any of this is *ever* called */
  1146. /* Handling buffered binary data (must have been SOCK_BUFFER before). */
  1147. if (socklist[i].inbuflen <= SGRAB) {
  1148. *len = socklist[i].inbuflen;
  1149. egg_memcpy(s, socklist[i].inbuf, socklist[i].inbuflen);
  1150. free(socklist[i].inbuf);
  1151. socklist[i].inbuf = NULL;
  1152. socklist[i].inbuflen = 0;
  1153. } else {
  1154. /* Split up into chunks of SGRAB bytes. */
  1155. *len = SGRAB;
  1156. egg_memcpy(s, socklist[i].inbuf, *len);
  1157. egg_memcpy(socklist[i].inbuf, socklist[i].inbuf + *len, *len);
  1158. socklist[i].inbuflen -= *len;
  1159. socklist[i].inbuf = (char *) my_realloc(socklist[i].inbuf, socklist[i].inbuflen);
  1160. }
  1161. return socklist[i].sock;
  1162. }
  1163. }
  1164. /* Also check any sockets that might have EOF'd during write */
  1165. if (!(socklist[i].flags & SOCK_UNUSED)
  1166. && (socklist[i].flags & SOCK_EOFD)) {
  1167. s[0] = 0;
  1168. *len = socklist[i].sock;
  1169. return -1;
  1170. }
  1171. }
  1172. /* No pent-up data of any worth -- down to business */
  1173. *len = 0;
  1174. ret = sockread(xx, len);
  1175. if (ret < 0) {
  1176. s[0] = 0;
  1177. return ret;
  1178. }
  1179. /* Binary, listening and passed on sockets don't get buffered. */
  1180. if (socklist[ret].flags & SOCK_CONNECT) {
  1181. if (socklist[ret].flags & SOCK_STRONGCONN) {
  1182. socklist[ret].flags &= ~SOCK_STRONGCONN;
  1183. /* Buffer any data that came in, for future read. */
  1184. socklist[ret].inbuflen = *len;
  1185. socklist[ret].inbuf = (char *) my_calloc(1, *len + 1);
  1186. /* It might be binary data. You never know. */
  1187. egg_memcpy(socklist[ret].inbuf, xx, *len);
  1188. socklist[ret].inbuf[*len] = 0;
  1189. }
  1190. socklist[ret].flags &= ~SOCK_CONNECT;
  1191. s[0] = 0;
  1192. return socklist[ret].sock;
  1193. }
  1194. if (socklist[ret].flags & SOCK_BINARY) {
  1195. egg_memcpy(s, xx, *len);
  1196. return socklist[ret].sock;
  1197. }
  1198. if ((socklist[ret].flags & SOCK_LISTEN) || (socklist[ret].flags & SOCK_PASS))
  1199. return socklist[ret].sock;
  1200. if (socklist[ret].flags & SOCK_BUFFER) {
  1201. socklist[ret].inbuf = (char *) my_realloc(socklist[ret].inbuf,
  1202. socklist[ret].inbuflen + *len + 1);
  1203. egg_memcpy(socklist[ret].inbuf + socklist[ret].inbuflen, xx, *len);
  1204. socklist[ret].inbuflen += *len;
  1205. /* We don't know whether it's binary data. Make sure normal strings
  1206. will be handled properly later on too. */
  1207. socklist[ret].inbuf[socklist[ret].inbuflen] = 0;
  1208. return -4; /* Ignore this one. */
  1209. }
  1210. /* Might be necessary to prepend stored-up data! */
  1211. if (socklist[ret].inbuf != NULL) {
  1212. p = socklist[ret].inbuf;
  1213. size_t bufsiz = strlen(p) + strlen(xx) + 1;
  1214. socklist[ret].inbuf = (char *) my_calloc(1, bufsiz);
  1215. strlcpy(socklist[ret].inbuf, p, bufsiz);
  1216. strlcat(socklist[ret].inbuf, xx, bufsiz);
  1217. free(p);
  1218. if (strlen(socklist[ret].inbuf) < (SGRAB + 2)) {
  1219. strlcpy(xx, socklist[ret].inbuf, sizeof(xx));
  1220. free(socklist[ret].inbuf);
  1221. socklist[ret].inbuf = NULL;
  1222. socklist[ret].inbuflen = 0;
  1223. } else {
  1224. p = socklist[ret].inbuf;
  1225. socklist[ret].inbuflen = strlen(p) - SGRAB;
  1226. socklist[ret].inbuf = (char *) my_calloc(1, socklist[ret].inbuflen + 1);
  1227. strlcpy(socklist[ret].inbuf, p + SGRAB, socklist[ret].inbuflen + 1);
  1228. *(p + SGRAB) = 0;
  1229. strlcpy(xx, p, sizeof(xx));
  1230. free(p);
  1231. /* (leave the rest to be post-pended later) */
  1232. }
  1233. }
  1234. bool data = 0;
  1235. /* Look for EOL marker; if it's there, i have something to show */
  1236. p = strchr(xx, '\n');
  1237. if (p == NULL)
  1238. p = strchr(xx, '\r');
  1239. if (p != NULL) {
  1240. *p = 0;
  1241. /* FIXME: overlapping here */
  1242. strlcpy(s, xx, SGRAB + 10); //buf@main.c
  1243. strlcpy(xx, p + 1, sizeof(xx));
  1244. /* if (s[0] && strlen(s) && (s[strlen(s) - 1] == '\r')) */
  1245. if (s[strlen(s) - 1] == '\r')
  1246. s[strlen(s) - 1] = 0;
  1247. data = 1; /* DCC_CHAT may now need to process a blank line */
  1248. /* NO! */
  1249. /* if (!s[0]) strcpy(s," "); */
  1250. } else {
  1251. s[0] = 0;
  1252. if (strlen(xx) >= SGRAB) {
  1253. /* String is too long, so just insert fake \n */
  1254. strlcpy(s, xx, SGRAB + 10); //buf@main.c
  1255. xx[0] = 0;
  1256. data = 1;
  1257. }
  1258. }
  1259. if (s[0] && socklist[ret].encstatus)
  1260. link_read(ret, s, (size_t *) len);
  1261. *len = strlen(s);
  1262. /* Anything left that needs to be saved? */
  1263. if (!xx[0]) {
  1264. if (data)
  1265. return socklist[ret].sock;
  1266. else
  1267. return -3;
  1268. }
  1269. /* Prepend old data back */
  1270. if (socklist[ret].inbuf != NULL) {
  1271. p = socklist[ret].inbuf;
  1272. socklist[ret].inbuflen = strlen(p) + strlen(xx);
  1273. socklist[ret].inbuf = (char *) my_calloc(1, socklist[ret].inbuflen + 1);
  1274. strlcpy(socklist[ret].inbuf, xx, socklist[ret].inbuflen + 1);
  1275. strlcat(socklist[ret].inbuf, p, socklist[ret].inbuflen + 1);
  1276. free(p);
  1277. } else {
  1278. socklist[ret].inbuflen = strlen(xx);
  1279. socklist[ret].inbuf = (char *) my_calloc(1, socklist[ret].inbuflen + 1);
  1280. strlcpy(socklist[ret].inbuf, xx, socklist[ret].inbuflen + 1);
  1281. }
  1282. if (data) {
  1283. return socklist[ret].sock;
  1284. } else {
  1285. return -3;
  1286. }
  1287. }
  1288. /* Dump something to a socket
  1289. *
  1290. * NOTE: Do NOT put Contexts in here if you want DEBUG to be meaningful!!
  1291. */
  1292. void tputs(register int z, char *s, size_t len)
  1293. {
  1294. if (z < 0) /* um... HELLO?! sanity check please! */
  1295. return;
  1296. if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
  1297. if (write(z, s, len) == -1) {
  1298. ;
  1299. }
  1300. return;
  1301. }
  1302. register int x, idx;
  1303. char *p = NULL;
  1304. for (register int i = 0; i < MAXSOCKS; i++) {
  1305. if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
  1306. for (idx = 0; idx < dcc_total; idx++) {
  1307. if (dcc[idx].type && (dcc[idx].sock == z) && dcc[idx].type->name) {
  1308. if (!strncmp(dcc[idx].type->name, "BOT", 3))
  1309. traffic.out_today.bn += len;
  1310. else if (!strcmp(dcc[idx].type->name, "SERVER"))
  1311. traffic.out_today.irc += len;
  1312. else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
  1313. traffic.out_today.dcc += len;
  1314. else if (!strncmp(dcc[idx].type->name, "FILES", 5))
  1315. traffic.out_today.filesys += len;
  1316. else if (!strcmp(dcc[idx].type->name, "SEND"))
  1317. traffic.out_today.trans += len;
  1318. else if (!strncmp(dcc[idx].type->name, "GET", 3))
  1319. traffic.out_today.trans += len;
  1320. else
  1321. traffic.out_today.unknown += len;
  1322. break;
  1323. }
  1324. }
  1325. if (len && socklist[i].encstatus)
  1326. s = link_write(i, s, &len);
  1327. if (socklist[i].outbuf != NULL) {
  1328. /* Already queueing: just add it */
  1329. p = (char *) my_realloc(socklist[i].outbuf, socklist[i].outbuflen + len);
  1330. egg_memcpy(p + socklist[i].outbuflen, s, len);
  1331. socklist[i].outbuf = p;
  1332. socklist[i].outbuflen += len;
  1333. if (socklist[i].encstatus && s)
  1334. free(s);
  1335. return;
  1336. }
  1337. /* Try. */
  1338. #ifdef HAVE_SSL
  1339. if (socklist[i].ssl) {
  1340. x = SSL_write(socklist[i].ssl, s, len);
  1341. if (x < 0) {
  1342. int err = SSL_get_error(socklist[i].ssl, x);
  1343. x = -1;
  1344. switch (err) {
  1345. case SSL_ERROR_WANT_READ:
  1346. errno = EAGAIN;
  1347. break;
  1348. case SSL_ERROR_WANT_WRITE:
  1349. errno = EAGAIN;
  1350. break;
  1351. case SSL_ERROR_WANT_X509_LOOKUP:
  1352. errno = EAGAIN;
  1353. break;
  1354. }
  1355. }
  1356. } else
  1357. #endif /* HAVE_SSL */
  1358. #ifdef HAVE_ZLIB_H
  1359. /*
  1360. if (socklist[i].gz) {
  1361. FILE *fp;
  1362. fp = gzdopen(z, "wb0");
  1363. x = gzwrite(fp, s, len);
  1364. } else
  1365. */
  1366. #endif /* HAVE_ZLIB_H */
  1367. x = write(z, s, len);
  1368. if (x == -1)
  1369. x = 0;
  1370. if ((size_t) x < len) {
  1371. /* Socket is full, queue it */
  1372. socklist[i].outbuf = (char *) my_calloc(1, len - x);
  1373. egg_memcpy(socklist[i].outbuf, &s[x], len - x);
  1374. socklist[i].outbuflen = len - x;
  1375. }
  1376. if (socklist[i].encstatus && s)
  1377. free(s);
  1378. return;
  1379. }
  1380. }
  1381. /* Make sure we don't cause a crash by looping here */
  1382. static int inhere = 0;
  1383. if (!inhere) {
  1384. inhere = 1;
  1385. putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
  1386. if (strlen(s)) {
  1387. s[strlen(s) - 1] = 0;
  1388. putlog(LOG_MISC, "*", "!-> '%s'", s);
  1389. }
  1390. inhere = 0;
  1391. }
  1392. /* if (socklist[i].encstatus > 0)
  1393. free(s);
  1394. */
  1395. }
  1396. int findanysnum(register int sock)
  1397. {
  1398. register int i = 0;
  1399. if (sock != -1)
  1400. for (i = 0; i < MAXSOCKS; i++)
  1401. if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED))
  1402. return i;
  1403. return -1;
  1404. }
  1405. static int findanyidx(register int sock)
  1406. {
  1407. register int j;
  1408. if (sock != -1)
  1409. for (j = 0; j < dcc_total; j++)
  1410. if (dcc[j].type && dcc[j].sock == sock)
  1411. return j;
  1412. return -1;
  1413. }
  1414. /* tputs might queue data for sockets, let's dump as much of it as
  1415. * possible.
  1416. */
  1417. void dequeue_sockets()
  1418. {
  1419. int i, x, z = 0, fds = 0;
  1420. fd_set wfds;
  1421. struct timeval tv;
  1422. /* ^-- start poptix test code, this should avoid writes to sockets not ready to be written to. */
  1423. FD_ZERO(&wfds);
  1424. tv.tv_sec = 0;
  1425. tv.tv_usec = 0; /* we only want to see if it's ready for writing, no need to actually wait.. */
  1426. for (i = 0; i < MAXSOCKS; i++) {
  1427. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].outbuf != NULL) {
  1428. FD_SET(socklist[i].sock, &wfds);
  1429. if (socklist[i].sock > fds)
  1430. fds = socklist[i].sock;
  1431. z = 1;
  1432. }
  1433. }
  1434. if (!z)
  1435. return; /* nothing to write */
  1436. fds++;
  1437. select(fds, NULL, &wfds, NULL, &tv);
  1438. /* end poptix */
  1439. for (i = 0; i < MAXSOCKS; i++) {
  1440. if (!(socklist[i].flags & SOCK_UNUSED) &&
  1441. (socklist[i].outbuf != NULL) && (FD_ISSET(socklist[i].sock, &wfds))) {
  1442. /* Trick tputs into doing the work */
  1443. errno = 0;
  1444. #ifdef HAVE_SSL
  1445. if (socklist[i].ssl) {
  1446. x = write(socklist[i].sock, socklist[i].outbuf, socklist[i].outbuflen);
  1447. if (x < 0) {
  1448. int err = SSL_get_error(socklist[i].ssl, x);
  1449. x = -1;
  1450. switch (err) {
  1451. case SSL_ERROR_WANT_READ:
  1452. errno = EAGAIN;
  1453. break;
  1454. case SSL_ERROR_WANT_WRITE:
  1455. errno = EAGAIN;
  1456. break;
  1457. case SSL_ERROR_WANT_X509_LOOKUP:
  1458. errno = EAGAIN;
  1459. break;
  1460. }
  1461. }
  1462. } else
  1463. #endif /* HAVE_SSL */
  1464. x = write(socklist[i].sock, socklist[i].outbuf, socklist[i].outbuflen);
  1465. if ((x < 0) && (errno != EAGAIN)
  1466. #ifdef EBADSLT
  1467. && (errno != EBADSLT)
  1468. #endif /* EBADSLT */
  1469. #ifdef ENOTCONN
  1470. && (errno != ENOTCONN)
  1471. #endif /* EBADSLT */
  1472. ) {
  1473. /* This detects an EOF during writing */
  1474. debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock, strerror(errno), errno);
  1475. socklist[i].flags |= SOCK_EOFD;
  1476. } else if ((size_t) x == socklist[i].outbuflen) {
  1477. /* If the whole buffer was sent, nuke it */
  1478. free(socklist[i].outbuf);
  1479. socklist[i].outbuf = NULL;
  1480. socklist[i].outbuflen = 0;
  1481. } else if (x > 0) {
  1482. char *p = socklist[i].outbuf;
  1483. /* This removes any sent bytes from the beginning of the buffer */
  1484. socklist[i].outbuf = (char *) my_calloc(1, socklist[i].outbuflen - x);
  1485. egg_memcpy(socklist[i].outbuf, p + x, socklist[i].outbuflen - x);
  1486. socklist[i].outbuflen -= x;
  1487. free(p);
  1488. } else {
  1489. debug3("dequeue_sockets(): errno = %d (%s) on %d", errno, strerror(errno), socklist[i].sock);
  1490. }
  1491. /* All queued data was sent. Call handler if one exists and the
  1492. * dcc entry wants it.
  1493. */
  1494. if (!socklist[i].outbuf) {
  1495. int idx = findanyidx(socklist[i].sock);
  1496. if (idx >= 0 && dcc[idx].type && dcc[idx].type->outdone)
  1497. dcc[idx].type->outdone(idx);
  1498. }
  1499. }
  1500. }
  1501. }
  1502. /*
  1503. * Debugging stuff
  1504. */
  1505. void tell_netdebug(int idx)
  1506. {
  1507. char s[80] = "";
  1508. dprintf(idx, "Open sockets:");
  1509. for (int i = 0; i < MAXSOCKS; i++) {
  1510. if (!(socklist[i].flags & SOCK_UNUSED)) {
  1511. simple_snprintf(s, sizeof(s), " %d", socklist[i].sock);
  1512. if (socklist[i].flags & SOCK_BINARY)
  1513. strlcat(s, " (binary)", sizeof(s));
  1514. if (socklist[i].flags & SOCK_LISTEN)
  1515. strlcat(s, " (listen)", sizeof(s));
  1516. if (socklist[i].flags & SOCK_PASS)
  1517. strlcat(s, " (passed on)", sizeof(s));
  1518. if (socklist[i].flags & SOCK_CONNECT)
  1519. strlcat(s, " (connecting)", sizeof(s));
  1520. if (socklist[i].flags & SOCK_STRONGCONN)
  1521. strlcat(s, " (strong)", sizeof(s));
  1522. if (socklist[i].flags & SOCK_NONSOCK)
  1523. strlcat(s, " (file)", sizeof(s));
  1524. if (socklist[i].inbuf != NULL)
  1525. simple_sprintf(&s[strlen(s)], " (inbuf: %zu)", strlen(socklist[i].inbuf));
  1526. if (socklist[i].outbuf != NULL)
  1527. simple_sprintf(&s[strlen(s)], " (outbuf: %zu)", socklist[i].outbuflen);
  1528. if (socklist[i].host)
  1529. simple_sprintf(&s[strlen(s)], " (%s:%d)", socklist[i].host, socklist[i].port);
  1530. strlcat(s, ",", sizeof(s));
  1531. dprintf(idx, "%s", s);
  1532. }
  1533. }
  1534. dprintf(idx, " done.\n");
  1535. }
  1536. /* Checks wether the referenced socket has data queued.
  1537. *
  1538. * Returns true if the incoming/outgoing (depending on 'type') queues
  1539. * contain data, otherwise false.
  1540. */
  1541. bool sock_has_data(int type, int sock)
  1542. {
  1543. bool ret = 0;
  1544. int i;
  1545. for (i = 0; i < MAXSOCKS; i++)
  1546. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
  1547. break;
  1548. if (i < MAXSOCKS) {
  1549. switch (type) {
  1550. case SOCK_DATA_OUTGOING:
  1551. ret = (socklist[i].outbuf != NULL);
  1552. break;
  1553. case SOCK_DATA_INCOMING:
  1554. ret = (socklist[i].inbuf != NULL);
  1555. break;
  1556. }
  1557. } else
  1558. debug1("sock_has_data: could not find socket #%d, returning false.", sock);
  1559. return ret;
  1560. }