net.c 47 KB

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