net.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724
  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 resolve_timeout = 10; /* hostname/address lookup timeout */
  67. int socks_total = 0; /* total number of sockets */
  68. sock_list *socklist = NULL; /* Enough to be safe */
  69. int MAXSOCKS = 0;
  70. jmp_buf alarmret; /* Env buffer for alarm() returns */
  71. /* This *MUST* be an ip */
  72. char firewall[121] = ""; /* Socks server for firewall */
  73. port_t firewallport = 1080; /* Default port of Sock4/5 firewalls */
  74. /* Types of proxy */
  75. #define PROXY_SOCKS 1
  76. #define PROXY_SUN 2
  77. #define PROXY_HTTP 3
  78. /* I need an UNSIGNED long for dcc type stuff
  79. */
  80. unsigned long my_atoul(char *s)
  81. {
  82. unsigned long ret = 0;
  83. while ((*s >= '0') && (*s <= '9')) {
  84. ret *= 10;
  85. ret += ((*s) - '0');
  86. s++;
  87. }
  88. return ret;
  89. }
  90. /* get the protocol used on a socket */
  91. int sockprotocol(int sock)
  92. {
  93. struct sockaddr sa;
  94. socklen_t socklen = sizeof(sa);
  95. egg_bzero(&sa, socklen);
  96. if (getsockname(sock, &sa, &socklen))
  97. return -1;
  98. else
  99. return sa.sa_family;
  100. }
  101. /* AF_INET-independent resolving routine */
  102. static int get_ip(char *hostname, union sockaddr_union *so)
  103. {
  104. if (!hostname || (hostname && !hostname[0]))
  105. return 1;
  106. egg_memset(so, 0, sizeof(union sockaddr_union));
  107. debug1("get_ip(%s)", hostname);
  108. #ifdef USE_IPV6
  109. struct addrinfo hints, *ai = NULL, *res = NULL;
  110. int error = 0;
  111. egg_memset(&hints, 0, sizeof(struct addrinfo));
  112. hints.ai_socktype = SOCK_STREAM;
  113. if ((error = getaddrinfo(hostname, NULL, &hints, &res))) {
  114. if (res)
  115. freeaddrinfo(res);
  116. return error;
  117. }
  118. error = 1;
  119. for (ai = res; ai != NULL; ai = ai->ai_next) {
  120. if ((ai->ai_family == AF_INET6) || (ai->ai_family == AF_INET)) {
  121. memcpy(so, ai->ai_addr, ai->ai_addrlen);
  122. error = 0;
  123. break;
  124. }
  125. }
  126. if (res)
  127. freeaddrinfo(res);
  128. return error;
  129. #else
  130. struct hostent *hp = NULL;
  131. if (!(hp = gethostbyname(hostname)))
  132. return -1;
  133. memcpy(&so->sin.sin_addr, hp->h_addr, 4);
  134. so->sin.sin_family = AF_INET;
  135. return 0;
  136. #endif /* USE_IPV6 */
  137. }
  138. #ifdef HAVE_SSL
  139. int seed_PRNG(void)
  140. {
  141. char stackdata[1024] = "";
  142. static char rand_file[300] = "";
  143. FILE *fh = NULL;
  144. #if OPENSSL_VERSION_NUMBER >= 0x00905100
  145. if (RAND_status()) return 0;
  146. #endif /* OPENSSL_VERSION_NUMBER */
  147. if ((fh = fopen("/dev/urandom", "r"))) {
  148. fclose(fh);
  149. return 0;
  150. }
  151. if (RAND_file_name(rand_file, sizeof(rand_file)))
  152. tls_rand_file = rand_file;
  153. else
  154. return 1;
  155. if (!RAND_load_file(rand_file, 1024)) {
  156. unsigned int c;
  157. c = now;
  158. RAND_seed(&c, sizeof(c));
  159. c = getpid();
  160. RAND_seed(&c, sizeof(c));
  161. RAND_seed(stackdata, sizeof(stackdata));
  162. }
  163. #if OPENSSL_VERSION_NUMBER >= 0x00905100
  164. if (!RAND_status()) return 2;
  165. #endif /* OPENSSL_VERSION_NUMBER >= 0x00905100 */
  166. return 0;
  167. }
  168. #endif /* HAVE_SSL */
  169. /* Initialize the socklist
  170. */
  171. void init_net()
  172. {
  173. MAXSOCKS = max_dcc + 10;
  174. if (socklist)
  175. socklist = (sock_list *) my_realloc((void *) socklist, sizeof(sock_list) * MAXSOCKS);
  176. else
  177. socklist = (sock_list *) my_calloc(1, sizeof(sock_list) * MAXSOCKS);
  178. for (int i = 0; i < MAXSOCKS; i++) {
  179. egg_bzero(&socklist[i], sizeof(socklist[i]));
  180. #ifdef HAVE_SSL
  181. socklist[i].ssl = NULL;
  182. #endif /* HAVE_SSL */
  183. socklist[i].flags = SOCK_UNUSED;
  184. socklist[i].sock = -1;
  185. }
  186. #ifdef HAVE_SSL
  187. SSL_load_error_strings();
  188. OpenSSL_add_ssl_algorithms();
  189. ssl_c_ctx = SSL_CTX_new(SSLv23_client_method());
  190. ssl_s_ctx = SSL_CTX_new(SSLv23_server_method());
  191. if (!ssl_c_ctx || !ssl_s_ctx)
  192. fatal("SSL Inititlization failed", 0);
  193. if (seed_PRNG())
  194. fatal("SSL PRNG seeding failed!", 0);
  195. #endif /* HAVE_SSL */
  196. }
  197. #ifdef HAVE_SSL
  198. int ssl_cleanup() {
  199. if (ssl_c_ctx) {
  200. SSL_CTX_free(ssl_c_ctx);
  201. ssl_c_ctx = NULL;
  202. }
  203. if (ssl_s_ctx) {
  204. SSL_CTX_free(ssl_s_ctx);
  205. ssl_s_ctx = NULL;
  206. }
  207. if (tls_rand_file) RAND_write_file(tls_rand_file);
  208. return 0;
  209. }
  210. #endif /* HAVE_SSL */
  211. /* Get my ipv? ip
  212. */
  213. char *myipstr(int af_type)
  214. {
  215. if (cached_ip) {
  216. #ifdef USE_IPV6
  217. if (af_type == AF_INET6) {
  218. static char s[UHOSTLEN + 1] = "";
  219. egg_inet_ntop(AF_INET6, &cached_myip6_so.sin6.sin6_addr, s, 119);
  220. s[120] = 0;
  221. return s;
  222. } else
  223. #endif /* USE_IPV6 */
  224. if (af_type == AF_INET) {
  225. static char s[UHOSTLEN + 1] = "";
  226. egg_inet_ntop(AF_INET, &cached_myip4_so.sin.sin_addr, s, 119);
  227. s[120] = 0;
  228. return s;
  229. }
  230. }
  231. return "";
  232. }
  233. /* Get my ip number
  234. */
  235. in_addr_t getmyip() {
  236. return cached_myip4_so.sin.sin_addr.s_addr;
  237. }
  238. /* see if it's necessary to set inaddr_any... because if we can't resolve, we die anyway */
  239. void cache_my_ip()
  240. {
  241. #ifdef no
  242. cached_myip6_so.sin6.sin6_family = AF_INET6;
  243. cached_myip6_so.sin6.sin6_addr = in6addr_any;
  244. cached_myip4_so.sin.sin_family = AF_INET;
  245. cached_myip4_so.sin.sin_addr.s_addr = INADDR_ANY;
  246. #endif
  247. int error = 0;
  248. debug0("cache_my_ip()");
  249. egg_memset(&cached_myip4_so, 0, sizeof(union sockaddr_union));
  250. #ifdef USE_IPV6
  251. bool any = 0;
  252. egg_memset(&cached_myip6_so, 0, sizeof(union sockaddr_union));
  253. if (conf.bot->net.ip6) {
  254. if (get_ip(conf.bot->net.ip6, &cached_myip6_so))
  255. any = 1;
  256. } else if (conf.bot->net.host6) {
  257. if (get_ip(conf.bot->net.host6, &cached_myip6_so))
  258. any = 1;
  259. } else
  260. any = 1;
  261. if (any) {
  262. cached_myip6_so.sin6.sin6_family = AF_INET6;
  263. cached_myip6_so.sin6.sin6_addr = in6addr_any;
  264. }
  265. #endif /* USE_IPV6 */
  266. if (conf.bot->net.ip) {
  267. if (get_ip(conf.bot->net.ip, &cached_myip4_so))
  268. error = 1;
  269. } else if (conf.bot->net.host) {
  270. if (get_ip(conf.bot->net.host, &cached_myip4_so))
  271. error = 2;
  272. } else {
  273. /*
  274. char s[121] = "";
  275. gethostname(s, sizeof(s));
  276. if (get_ip(s, &cached_myip4_so)) {
  277. */
  278. /* error = 3; */
  279. cached_myip4_so.sin.sin_family = AF_INET;
  280. cached_myip4_so.sin.sin_addr.s_addr = INADDR_ANY;
  281. // }
  282. }
  283. if (error) {
  284. putlog(LOG_DEBUG, "*", "Hostname self-lookup error: %d", error);
  285. fatal("Hostname self-lookup failed.", 0);
  286. }
  287. cached_ip = 1;
  288. }
  289. /* Sets/Unsets options for a specific socket.
  290. *
  291. * Returns: 0 - on success
  292. * -1 - socket not found
  293. * -2 - illegal operation
  294. */
  295. int sockoptions(int sock, int operation, int sock_options)
  296. {
  297. for (int i = 0; i < MAXSOCKS; i++) {
  298. if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
  299. if (operation == EGG_OPTION_SET)
  300. socklist[i].flags |= sock_options;
  301. else if (operation == EGG_OPTION_UNSET)
  302. socklist[i].flags &= ~sock_options;
  303. else
  304. return -2;
  305. return 0;
  306. }
  307. }
  308. return -1;
  309. }
  310. int
  311. sock_read(FILE *f, bool enc)
  312. {
  313. char inbuf[1024] = "", *type = NULL, *buf = NULL, *buf_ptr = NULL;
  314. int fd = -1;
  315. while (fgets(inbuf, sizeof(inbuf), f) != NULL) {
  316. remove_crlf(inbuf);
  317. if (enc)
  318. buf = buf_ptr = decrypt_string(settings.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. strcpy(s, socklist[i].inbuf);
  1126. px = (char *) my_calloc(1, strlen(p + 1) + 1);
  1127. strcpy(px, p + 1);
  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. socklist[ret].inbuf = (char *) my_calloc(1, strlen(p) + strlen(xx) + 1);
  1213. strcpy(socklist[ret].inbuf, p);
  1214. strcat(socklist[ret].inbuf, xx);
  1215. free(p);
  1216. if (strlen(socklist[ret].inbuf) < (SGRAB + 2)) {
  1217. strcpy(xx, socklist[ret].inbuf);
  1218. free(socklist[ret].inbuf);
  1219. socklist[ret].inbuf = NULL;
  1220. socklist[ret].inbuflen = 0;
  1221. } else {
  1222. p = socklist[ret].inbuf;
  1223. socklist[ret].inbuflen = strlen(p) - SGRAB;
  1224. socklist[ret].inbuf = (char *) my_calloc(1, socklist[ret].inbuflen + 1);
  1225. strcpy(socklist[ret].inbuf, p + SGRAB);
  1226. *(p + SGRAB) = 0;
  1227. strcpy(xx, p);
  1228. free(p);
  1229. /* (leave the rest to be post-pended later) */
  1230. }
  1231. }
  1232. bool data = 0;
  1233. /* Look for EOL marker; if it's there, i have something to show */
  1234. p = strchr(xx, '\n');
  1235. if (p == NULL)
  1236. p = strchr(xx, '\r');
  1237. if (p != NULL) {
  1238. *p = 0;
  1239. /* FIXME: overlapping here */
  1240. strcpy(s, xx);
  1241. strcpy(xx, p + 1);
  1242. /* if (s[0] && strlen(s) && (s[strlen(s) - 1] == '\r')) */
  1243. if (s[strlen(s) - 1] == '\r')
  1244. s[strlen(s) - 1] = 0;
  1245. data = 1; /* DCC_CHAT may now need to process a blank line */
  1246. /* NO! */
  1247. /* if (!s[0]) strcpy(s," "); */
  1248. } else {
  1249. s[0] = 0;
  1250. if (strlen(xx) >= SGRAB) {
  1251. /* String is too long, so just insert fake \n */
  1252. strcpy(s, xx);
  1253. xx[0] = 0;
  1254. data = 1;
  1255. }
  1256. }
  1257. if (s[0] && socklist[ret].encstatus)
  1258. link_read(ret, s, (size_t *) len);
  1259. *len = strlen(s);
  1260. /* Anything left that needs to be saved? */
  1261. if (!xx[0]) {
  1262. if (data)
  1263. return socklist[ret].sock;
  1264. else
  1265. return -3;
  1266. }
  1267. /* Prepend old data back */
  1268. if (socklist[ret].inbuf != NULL) {
  1269. p = socklist[ret].inbuf;
  1270. socklist[ret].inbuflen = strlen(p) + strlen(xx);
  1271. socklist[ret].inbuf = (char *) my_calloc(1, socklist[ret].inbuflen + 1);
  1272. strcpy(socklist[ret].inbuf, xx);
  1273. strcat(socklist[ret].inbuf, p);
  1274. free(p);
  1275. } else {
  1276. socklist[ret].inbuflen = strlen(xx);
  1277. socklist[ret].inbuf = (char *) my_calloc(1, socklist[ret].inbuflen + 1);
  1278. strcpy(socklist[ret].inbuf, xx);
  1279. }
  1280. if (data) {
  1281. return socklist[ret].sock;
  1282. } else {
  1283. return -3;
  1284. }
  1285. }
  1286. /* Dump something to a socket
  1287. *
  1288. * NOTE: Do NOT put Contexts in here if you want DEBUG to be meaningful!!
  1289. */
  1290. void tputs(register int z, char *s, size_t len)
  1291. {
  1292. if (z < 0) /* um... HELLO?! sanity check please! */
  1293. return;
  1294. if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
  1295. write(z, s, len);
  1296. return;
  1297. }
  1298. register int x, idx;
  1299. char *p = NULL;
  1300. for (register int i = 0; i < MAXSOCKS; i++) {
  1301. if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
  1302. for (idx = 0; idx < dcc_total; idx++) {
  1303. if (dcc[idx].type && (dcc[idx].sock == z) && dcc[idx].type->name) {
  1304. if (!strncmp(dcc[idx].type->name, "BOT", 3))
  1305. traffic.out_today.bn += len;
  1306. else if (!strcmp(dcc[idx].type->name, "SERVER"))
  1307. traffic.out_today.irc += len;
  1308. else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
  1309. traffic.out_today.dcc += len;
  1310. else if (!strncmp(dcc[idx].type->name, "FILES", 5))
  1311. traffic.out_today.filesys += len;
  1312. else if (!strcmp(dcc[idx].type->name, "SEND"))
  1313. traffic.out_today.trans += len;
  1314. else if (!strncmp(dcc[idx].type->name, "GET", 3))
  1315. traffic.out_today.trans += len;
  1316. else
  1317. traffic.out_today.unknown += len;
  1318. break;
  1319. }
  1320. }
  1321. if (len && socklist[i].encstatus)
  1322. s = link_write(i, s, &len);
  1323. if (socklist[i].outbuf != NULL) {
  1324. /* Already queueing: just add it */
  1325. p = (char *) my_realloc(socklist[i].outbuf, socklist[i].outbuflen + len);
  1326. egg_memcpy(p + socklist[i].outbuflen, s, len);
  1327. socklist[i].outbuf = p;
  1328. socklist[i].outbuflen += len;
  1329. if (socklist[i].encstatus && s)
  1330. free(s);
  1331. return;
  1332. }
  1333. /* Try. */
  1334. #ifdef HAVE_SSL
  1335. if (socklist[i].ssl) {
  1336. x = SSL_write(socklist[i].ssl, s, len);
  1337. if (x < 0) {
  1338. int err = SSL_get_error(socklist[i].ssl, x);
  1339. x = -1;
  1340. switch (err) {
  1341. case SSL_ERROR_WANT_READ:
  1342. errno = EAGAIN;
  1343. break;
  1344. case SSL_ERROR_WANT_WRITE:
  1345. errno = EAGAIN;
  1346. break;
  1347. case SSL_ERROR_WANT_X509_LOOKUP:
  1348. errno = EAGAIN;
  1349. break;
  1350. }
  1351. }
  1352. } else
  1353. #endif /* HAVE_SSL */
  1354. #ifdef HAVE_ZLIB_H
  1355. /*
  1356. if (socklist[i].gz) {
  1357. FILE *fp;
  1358. fp = gzdopen(z, "wb0");
  1359. x = gzwrite(fp, s, len);
  1360. } else
  1361. */
  1362. #endif /* HAVE_ZLIB_H */
  1363. x = write(z, s, len);
  1364. if (x == -1)
  1365. x = 0;
  1366. if ((size_t) x < len) {
  1367. /* Socket is full, queue it */
  1368. socklist[i].outbuf = (char *) my_calloc(1, len - x);
  1369. egg_memcpy(socklist[i].outbuf, &s[x], len - x);
  1370. socklist[i].outbuflen = len - x;
  1371. }
  1372. if (socklist[i].encstatus && s)
  1373. free(s);
  1374. return;
  1375. }
  1376. }
  1377. /* Make sure we don't cause a crash by looping here */
  1378. static int inhere = 0;
  1379. if (!inhere) {
  1380. inhere = 1;
  1381. putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
  1382. if (strlen(s)) {
  1383. s[strlen(s) - 1] = 0;
  1384. putlog(LOG_MISC, "*", "!-> '%s'", s);
  1385. }
  1386. inhere = 0;
  1387. }
  1388. /* if (socklist[i].encstatus > 0)
  1389. free(s);
  1390. */
  1391. }
  1392. int findanysnum(register int sock)
  1393. {
  1394. register int i = 0;
  1395. if (sock != -1)
  1396. for (i = 0; i < MAXSOCKS; i++)
  1397. if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED))
  1398. return i;
  1399. return -1;
  1400. }
  1401. static int findanyidx(register int sock)
  1402. {
  1403. register int j;
  1404. if (sock != -1)
  1405. for (j = 0; j < dcc_total; j++)
  1406. if (dcc[j].type && dcc[j].sock == sock)
  1407. return j;
  1408. return -1;
  1409. }
  1410. /* tputs might queue data for sockets, let's dump as much of it as
  1411. * possible.
  1412. */
  1413. void dequeue_sockets()
  1414. {
  1415. int i, x, z = 0, fds = 0;
  1416. fd_set wfds;
  1417. struct timeval tv;
  1418. /* ^-- start poptix test code, this should avoid writes to sockets not ready to be written to. */
  1419. FD_ZERO(&wfds);
  1420. tv.tv_sec = 0;
  1421. tv.tv_usec = 0; /* we only want to see if it's ready for writing, no need to actually wait.. */
  1422. for (i = 0; i < MAXSOCKS; i++) {
  1423. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].outbuf != NULL) {
  1424. FD_SET(socklist[i].sock, &wfds);
  1425. if (socklist[i].sock > fds)
  1426. fds = socklist[i].sock;
  1427. z = 1;
  1428. }
  1429. }
  1430. if (!z)
  1431. return; /* nothing to write */
  1432. fds++;
  1433. select(fds, NULL, &wfds, NULL, &tv);
  1434. /* end poptix */
  1435. for (i = 0; i < MAXSOCKS; i++) {
  1436. if (!(socklist[i].flags & SOCK_UNUSED) &&
  1437. (socklist[i].outbuf != NULL) && (FD_ISSET(socklist[i].sock, &wfds))) {
  1438. /* Trick tputs into doing the work */
  1439. errno = 0;
  1440. #ifdef HAVE_SSL
  1441. if (socklist[i].ssl) {
  1442. x = write(socklist[i].sock, socklist[i].outbuf, socklist[i].outbuflen);
  1443. if (x < 0) {
  1444. int err = SSL_get_error(socklist[i].ssl, x);
  1445. x = -1;
  1446. switch (err) {
  1447. case SSL_ERROR_WANT_READ:
  1448. errno = EAGAIN;
  1449. break;
  1450. case SSL_ERROR_WANT_WRITE:
  1451. errno = EAGAIN;
  1452. break;
  1453. case SSL_ERROR_WANT_X509_LOOKUP:
  1454. errno = EAGAIN;
  1455. break;
  1456. }
  1457. }
  1458. } else
  1459. #endif /* HAVE_SSL */
  1460. x = write(socklist[i].sock, socklist[i].outbuf, socklist[i].outbuflen);
  1461. if ((x < 0) && (errno != EAGAIN)
  1462. #ifdef EBADSLT
  1463. && (errno != EBADSLT)
  1464. #endif /* EBADSLT */
  1465. #ifdef ENOTCONN
  1466. && (errno != ENOTCONN)
  1467. #endif /* EBADSLT */
  1468. ) {
  1469. /* This detects an EOF during writing */
  1470. debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock, strerror(errno), errno);
  1471. socklist[i].flags |= SOCK_EOFD;
  1472. } else if ((size_t) x == socklist[i].outbuflen) {
  1473. /* If the whole buffer was sent, nuke it */
  1474. free(socklist[i].outbuf);
  1475. socklist[i].outbuf = NULL;
  1476. socklist[i].outbuflen = 0;
  1477. } else if (x > 0) {
  1478. char *p = socklist[i].outbuf;
  1479. /* This removes any sent bytes from the beginning of the buffer */
  1480. socklist[i].outbuf = (char *) my_calloc(1, socklist[i].outbuflen - x);
  1481. egg_memcpy(socklist[i].outbuf, p + x, socklist[i].outbuflen - x);
  1482. socklist[i].outbuflen -= x;
  1483. free(p);
  1484. } else {
  1485. debug3("dequeue_sockets(): errno = %d (%s) on %d", errno, strerror(errno), socklist[i].sock);
  1486. }
  1487. /* All queued data was sent. Call handler if one exists and the
  1488. * dcc entry wants it.
  1489. */
  1490. if (!socklist[i].outbuf) {
  1491. int idx = findanyidx(socklist[i].sock);
  1492. if (idx >= 0 && dcc[idx].type && dcc[idx].type->outdone)
  1493. dcc[idx].type->outdone(idx);
  1494. }
  1495. }
  1496. }
  1497. }
  1498. /*
  1499. * Debugging stuff
  1500. */
  1501. void tell_netdebug(int idx)
  1502. {
  1503. char s[80] = "";
  1504. dprintf(idx, "Open sockets:");
  1505. for (int i = 0; i < MAXSOCKS; i++) {
  1506. if (!(socklist[i].flags & SOCK_UNUSED)) {
  1507. simple_snprintf(s, sizeof(s), " %d", socklist[i].sock);
  1508. if (socklist[i].flags & SOCK_BINARY)
  1509. strcat(s, " (binary)");
  1510. if (socklist[i].flags & SOCK_LISTEN)
  1511. strcat(s, " (listen)");
  1512. if (socklist[i].flags & SOCK_PASS)
  1513. strcat(s, " (passed on)");
  1514. if (socklist[i].flags & SOCK_CONNECT)
  1515. strcat(s, " (connecting)");
  1516. if (socklist[i].flags & SOCK_STRONGCONN)
  1517. strcat(s, " (strong)");
  1518. if (socklist[i].flags & SOCK_NONSOCK)
  1519. strcat(s, " (file)");
  1520. if (socklist[i].inbuf != NULL)
  1521. sprintf(&s[strlen(s)], " (inbuf: %04X)", strlen(socklist[i].inbuf));
  1522. if (socklist[i].outbuf != NULL)
  1523. sprintf(&s[strlen(s)], " (outbuf: %06lX)", (unsigned long) socklist[i].outbuflen);
  1524. if (socklist[i].host)
  1525. sprintf(&s[strlen(s)], " (%s:%d)", socklist[i].host, socklist[i].port);
  1526. strcat(s, ",");
  1527. dprintf(idx, "%s", s);
  1528. }
  1529. }
  1530. dprintf(idx, " done.\n");
  1531. }
  1532. /* Checks wether the referenced socket has data queued.
  1533. *
  1534. * Returns true if the incoming/outgoing (depending on 'type') queues
  1535. * contain data, otherwise false.
  1536. */
  1537. bool sock_has_data(int type, int sock)
  1538. {
  1539. bool ret = 0;
  1540. int i;
  1541. for (i = 0; i < MAXSOCKS; i++)
  1542. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
  1543. break;
  1544. if (i < MAXSOCKS) {
  1545. switch (type) {
  1546. case SOCK_DATA_OUTGOING:
  1547. ret = (socklist[i].outbuf != NULL);
  1548. break;
  1549. case SOCK_DATA_INCOMING:
  1550. ret = (socklist[i].inbuf != NULL);
  1551. break;
  1552. }
  1553. } else
  1554. debug1("sock_has_data: could not find socket #%d, returning false.", sock);
  1555. return ret;
  1556. }