net.c 51 KB

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