net.c 44 KB

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