net.c 46 KB

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