net.c 48 KB

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