net.c 47 KB

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