net.c 50 KB

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