net.c 51 KB

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