net.c 43 KB

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