net.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  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 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. int hostprotocol(const 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, const char *host, port_t port, int proxy_type)
  430. {
  431. sdprintf("proxy_connect(%d, %s, %d, %d)", sock, host, port, proxy_type);
  432. #ifdef USE_IPV6
  433. unsigned char x[32] = "";
  434. int af_ty = sockprotocol(sock);
  435. #else
  436. unsigned char x[10] = "";
  437. #endif /* USE_IPV6 */
  438. char s[256] = "";
  439. /* socks proxy */
  440. if (proxy_type == PROXY_SOCKS) {
  441. /* numeric IP? */
  442. #ifdef USE_IPV6
  443. if ((host[strlen(host) - 1] >= '0' && host[strlen(host) - 1] <= '9') && af_ty != AF_INET6) {
  444. #else
  445. if (host[strlen(host) - 1] >= '0' && host[strlen(host) - 1] <= '9') {
  446. #endif /* USE_IPV6 */
  447. in_addr_t ip = ((in_addr_t) inet_addr(host));
  448. egg_memcpy(x, &ip, 4);
  449. } else {
  450. struct hostent *hp = NULL;
  451. /* no, must be host.domain */
  452. if (!setjmp(alarmret)) {
  453. #ifdef USE_IPV6
  454. alarm(resolve_timeout);
  455. if (af_ty == AF_INET6)
  456. hp = gethostbyname(host);
  457. else
  458. #endif /* USE_IPV6 */
  459. hp = gethostbyname(host);
  460. #ifdef USE_IPV6
  461. alarm(0);
  462. #endif /* USE_IPV6 */
  463. } else
  464. hp = NULL;
  465. if (hp == NULL) {
  466. killsock(sock);
  467. return -2;
  468. }
  469. egg_memcpy(x, hp->h_addr, hp->h_length);
  470. }
  471. for (int i = 0; i < MAXSOCKS; i++)
  472. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
  473. socklist[i].flags |= SOCK_PROXYWAIT; /* drummer */
  474. #ifdef USE_IPV6
  475. if (af_ty == AF_INET6)
  476. simple_snprintf(s, sizeof s,
  477. "\004\001%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%s",
  478. (port >> 8) % 256, (port % 256), x[0], x[1], x[2], x[3],
  479. x[4], x[5], x[6], x[7], x[9], x[9], x[10], x[11], x[12],
  480. x[13], x[14], x[15], botuser);
  481. else
  482. #endif /* USE_IPV6 */
  483. simple_snprintf(s, sizeof s, "\004\001%c%c%c%c%c%c%s", (port >> 8) % 256,
  484. (port % 256), x[0], x[1], x[2], x[3], botuser);
  485. tputs(sock, s, strlen(botuser) + 9); /* drummer */
  486. } else if (proxy_type == PROXY_SUN) {
  487. simple_snprintf(s, sizeof s, "%s %d\n", host, port);
  488. tputs(sock, s, strlen(s)); /* drummer */
  489. } else if (proxy_type == PROXY_HTTP) {
  490. simple_snprintf(s, sizeof s, "CONNECT %s:%d\n\n", host, port);
  491. tputs(sock, s, strlen(s));
  492. }
  493. return sock;
  494. }
  495. /* FIXME: REPLACE WITH SOCK_NAME() */
  496. void initialize_sockaddr(int af_type, const char *host, port_t port, union sockaddr_union *so)
  497. {
  498. egg_bzero(so, sizeof(*so));
  499. so->sa.sa_family = af_type;
  500. if (af_type == AF_INET) {
  501. so->sin.sin_family = AF_INET;
  502. if (host) {
  503. inet_pton(AF_INET, host, &so->sin.sin_addr);
  504. so->sin.sin_port = htons(port);
  505. } else {
  506. so->sin.sin_addr.s_addr = getmyip();
  507. }
  508. #ifdef USE_IPV6
  509. } else {
  510. so->sin6.sin6_family = AF_INET6;
  511. if (host) {
  512. inet_pton(AF_INET6, host, &so->sin6.sin6_addr);
  513. so->sin6.sin6_port = htons(port);
  514. } else {
  515. memcpy(&so->sin6.sin6_addr, &cached_myip6_so.sin6.sin6_addr, 16);
  516. }
  517. #endif /* USE_IPV6 */
  518. }
  519. }
  520. /* Starts a connection attempt to a socket
  521. *
  522. * If given a normal hostname, this will be resolved to the corresponding
  523. * IP address first. PLEASE try to use the non-blocking dns functions
  524. * instead and then call this function with the IP address to avoid blocking.
  525. *
  526. * returns <0 if connection refused:
  527. * -1 strerror()/errno type error
  528. * -2 can't resolve hostname
  529. */
  530. int open_telnet_raw(int sock, const char *server, port_t sport, bool proxy_on)
  531. {
  532. static port_t port = 0;
  533. union sockaddr_union so;
  534. char host[121] = "";
  535. int is_resolved = 0;
  536. volatile int proxy_type = 0, proxy = proxy_on;
  537. /* firewall? use socks */
  538. if (proxy) {
  539. switch (firewall[0]) {
  540. case '!':
  541. proxy_type = PROXY_SUN;
  542. strlcpy(host, &firewall[1], sizeof(host));
  543. break;
  544. case '@':
  545. proxy_type = PROXY_HTTP;
  546. strlcpy(host, &firewall[1], sizeof(host));
  547. break;
  548. default:
  549. proxy_type = PROXY_SOCKS;
  550. strlcpy(host, firewall, sizeof(host));
  551. break;
  552. }
  553. port = firewallport;
  554. } else {
  555. proxy_type = 0;
  556. strlcpy(host, server, sizeof host);
  557. port = sport;
  558. }
  559. /* figure out which ip to bind to locally (v4 or v6) based on what the host ip is .. */
  560. if ((is_resolved = is_dotted_ip(server))) { /* already resolved */
  561. /* bind to our cached ip for v4/v6 depending on what the ip is */
  562. initialize_sockaddr(is_resolved, NULL, 0, &so);
  563. if (bind(sock, &so.sa, SIZEOF_SOCKADDR(so)) < 0) {
  564. putlog(LOG_ERROR, "*", "Failed to bind to socket %d: %s", sock, strerror(errno));
  565. killsock(sock);
  566. return -1;
  567. }
  568. /* initialize so for connect using the host/port */
  569. initialize_sockaddr(is_resolved, host, port, &so);
  570. } else { /* if not resolved, resolve it with blocking calls.. (shouldn't happen ever) */
  571. sdprintf("WARNING: open_telnet_raw() is about to block in get_ip()!");
  572. if (!setjmp(alarmret)) {
  573. alarm(resolve_timeout);
  574. if (!get_ip(host, &so)) {
  575. alarm(0);
  576. /* ok, we resolved it, bind an appropriate ip */
  577. #ifdef USE_IPV6
  578. if (so.sa.sa_family == AF_INET6) {
  579. if (bind(sock, &cached_myip6_so.sa, SIZEOF_SOCKADDR(cached_myip6_so)) < 0) {
  580. putlog(LOG_ERROR, "*", "Failed to bind to v6 socket %d: %s", sock, strerror(errno));
  581. killsock(sock);
  582. return -1;
  583. }
  584. } else {
  585. #endif /* USE_IPV6 */
  586. if (bind(sock, &cached_myip4_so.sa, SIZEOF_SOCKADDR(cached_myip4_so)) < 0) {
  587. putlog(LOG_ERROR, "*", "Failed to bind to socket %d: %s", sock, strerror(errno));
  588. killsock(sock);
  589. return -3;
  590. }
  591. #ifdef USE_IPV6
  592. }
  593. if (so.sa.sa_family == AF_INET6)
  594. so.sin6.sin6_port = htons(port);
  595. else
  596. #endif /* USE_IPV6 */
  597. so.sin.sin_port = htons(port);
  598. } else {
  599. alarm(0);
  600. killsock(sock);
  601. return -2;
  602. }
  603. }
  604. }
  605. for (int i = 0; i < MAXSOCKS; i++) {
  606. if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock)) {
  607. socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT;
  608. socklist[i].host = strdup(server);
  609. socklist[i].port = port;
  610. }
  611. }
  612. int rc = -1;
  613. /* make the connect attempt */
  614. rc = connect(sock, &so.sa, SIZEOF_SOCKADDR(so));
  615. if (rc < 0) {
  616. if (errno == EINPROGRESS) {
  617. debug3("net: connect(%d, %s, %d)", sock, server, sport);
  618. /* Firewall? announce connect attempt to proxy */
  619. if (proxy)
  620. return proxy_connect(sock, server, sport, proxy_type);
  621. return sock; /* async success! */
  622. } else {
  623. sdprintf("connect(%d, %s, %d) failed: %s", sock, server, sport, strerror(errno));
  624. killsock(sock);
  625. return -1;
  626. }
  627. }
  628. /* Synchronous? :/ */
  629. debug3("net: (BLOCKING) connect(%d, %s, %d)", sock, server, sport);
  630. if (proxy)
  631. return proxy_connect(sock, server, sport, proxy_type);
  632. return sock;
  633. }
  634. /* Ordinary non-binary connection attempt */
  635. int open_telnet(const char *server, port_t port, bool proxy)
  636. {
  637. int sock = -1;
  638. #ifdef USE_IPV6
  639. sock = getsock(0, hostprotocol(server));
  640. #else
  641. sock = getsock(0);
  642. #endif /* USE_IPV6 */
  643. if (sock >= 0)
  644. return open_telnet_raw(sock, server, port, proxy);
  645. return -1;
  646. }
  647. /* Returns a socket number for a listening socket that will accept any
  648. * connection on a certain address -- port # is returned in port
  649. *
  650. * 'addr' is ignored if af_def is AF_INET6 -poptix (02/03/03)
  651. */
  652. #ifdef USE_IPV6
  653. int open_address_listen(in_addr_t addr, int af_def, port_t *port)
  654. #else
  655. intt open_address_listen(in_addr_t addr, port_t *port)
  656. #endif /* USE_IPV6 */
  657. {
  658. // if (firewall[0]) {
  659. // /* FIXME: can't do listen port thru firewall yet */
  660. // putlog(LOG_MISC, "*", "!! Cant open a listen port (you are using a firewall)");
  661. // return -1;
  662. // }
  663. int sock = 0;
  664. socklen_t addrlen;
  665. struct sockaddr_in name;
  666. #ifdef USE_IPV6
  667. if (af_def == AF_INET6) {
  668. struct sockaddr_in6 name6;
  669. sock = getsock(SOCK_LISTEN, af_def);
  670. if (sock < 0)
  671. return -1;
  672. debug2("Opening listen socket on port %d with AF_INET6, sock: %d", *port, sock);
  673. egg_bzero((char *) &name6, sizeof(name6));
  674. name6.sin6_family = af_def;
  675. name6.sin6_port = htons(*port); /* 0 = just assign us a port */
  676. /* memcpy(&name6.sin6_addr, &in6addr_any, 16); */ /* this is the only way to get ipv6+ipv4 in 1 socket */
  677. memcpy(&name6.sin6_addr, &cached_myip6_so.sin6.sin6_addr, 16);
  678. if (bind(sock, (struct sockaddr *) &name6, sizeof(name6)) < 0) {
  679. if (!(identd_hack && *port == 113))
  680. putlog(LOG_ERROR, "*", "Failed to bind to socket %d for listen on port '%d': %s", sock, *port, strerror(errno));
  681. killsock(sock);
  682. return -1;
  683. }
  684. addrlen = sizeof(name6);
  685. if (getsockname(sock, (struct sockaddr *) &name6, &addrlen) < 0) {
  686. if (!(identd_hack && *port == 113))
  687. putlog(LOG_ERROR, "*", "Failed to getsockname on socket '%d' for listen on port '%d': %s", sock, *port, strerror(errno));
  688. killsock(sock);
  689. return -1;
  690. }
  691. *port = ntohs(name6.sin6_port);
  692. if (listen(sock, 1) < 0) {
  693. if (!(identd_hack && *port == 113))
  694. putlog(LOG_ERROR, "*", "Failed to listen on socket '%d' for listen on port '%d': %s", sock, *port, strerror(errno));
  695. killsock(sock);
  696. return -1;
  697. }
  698. } else {
  699. sock = getsock(SOCK_LISTEN, AF_INET);
  700. #else
  701. sock = getsock(SOCK_LISTEN);
  702. #endif /* USE_IPV6 */
  703. if (sock < 0)
  704. return -1;
  705. debug2("Opening listen socket on port %d with AF_INET, sock: %d", *port, sock);
  706. egg_bzero((char *) &name, sizeof(struct sockaddr_in));
  707. name.sin_family = AF_INET;
  708. name.sin_port = htons(*port); /* 0 = just assign us a port */
  709. name.sin_addr.s_addr = addr;
  710. if (bind(sock, (struct sockaddr *) &name, sizeof(name)) < 0) {
  711. if (!(identd_hack && *port == 113))
  712. putlog(LOG_ERROR, "*", "Failed to bind to socket '%d' for listen on port '%d': %s", sock, *port, strerror(errno));
  713. killsock(sock);
  714. return -1;
  715. }
  716. /* what port are we on? */
  717. addrlen = sizeof(name);
  718. if (getsockname(sock, (struct sockaddr *) &name, &addrlen) < 0) {
  719. if (!(identd_hack && *port == 113))
  720. putlog(LOG_ERROR, "*", "Failed to getsockname on socket '%d' for listen on port '%d': %s", sock, *port, strerror(errno));
  721. killsock(sock);
  722. return -1;
  723. }
  724. *port = ntohs(name.sin_port);
  725. if (listen(sock, 1) < 0) {
  726. if (!(identd_hack && *port == 113))
  727. putlog(LOG_ERROR, "*", "Failed to listen on socket '%d' for on port '%d': %s", sock, *port, strerror(errno));
  728. killsock(sock);
  729. return -1;
  730. }
  731. #ifdef USE_IPV6
  732. }
  733. #endif /* USE_IPV6 */
  734. debug2("Opened listen socket on port %d with AF_INET, sock: %d", *port, sock);
  735. return sock;
  736. }
  737. /* Returns a socket number for a listening socket that will accept any
  738. * connection -- port # is returned in port
  739. */
  740. int open_listen(port_t *port)
  741. {
  742. #ifdef USE_IPV6
  743. return open_address_listen(getmyip(), AF_INET, port);
  744. #else
  745. return open_address_listen(getmyip(), port);
  746. #endif /* USE_IPV6 */
  747. }
  748. /* Same as above, except this one can be called with an AF_ type
  749. * the above is being left in for compatibility, and should NOT LONGER BE USED IN THE CORE CODE.
  750. */
  751. int open_listen_by_af(port_t *port, int af_def)
  752. {
  753. #ifdef USE_IPV6
  754. return open_address_listen(getmyip(), af_def, port);
  755. #else
  756. return -1;
  757. #endif /* USE_IPV6 */
  758. }
  759. #ifdef HAVE_SSL
  760. int ssl_link(register int sock, int state)
  761. {
  762. int err = 0, i = 0, errs = 0;
  763. debug2("ssl_link(%d, %d)", sock, state);
  764. for (i = 0; (i < MAXSOCKS); i++) {
  765. if (socklist[i].sock == sock) break;
  766. }
  767. if (socklist[i].ssl) {
  768. putlog(LOG_ERROR, "*", "Switching to SSL (%d,%d) - already active", state, sock);
  769. return 0;
  770. }
  771. if (state == CONNECT_SSL) {
  772. socklist[i].ssl = SSL_new(ssl_c_ctx);
  773. } else if (state == ACCEPT_SSL) {
  774. socklist[i].ssl = SSL_new(ssl_s_ctx);
  775. }
  776. if (!socklist[i].ssl) {
  777. putlog(LOG_ERROR, "*", "Switching to SSL (%d) - SSL_new(%d) failed", sock, state);
  778. return 0;
  779. }
  780. if (!SSL_set_fd(socklist[i].ssl, socklist[i].sock)) {
  781. putlog(LOG_ERROR, "*", "SSL_set_fd(%d) (%d) failed", state, socklist[i].sock);
  782. return 0;
  783. }
  784. if (state == CONNECT_SSL) {
  785. SSL_set_connect_state(socklist[i].ssl);
  786. } else if (state == ACCEPT_SSL) {
  787. SSL_set_accept_state(socklist[i].ssl);
  788. } else {
  789. putlog(LOG_DEBUG, "*", "ssl_link(%d, 0?) NO STATE?", sock);
  790. return 0;
  791. }
  792. if (state == CONNECT_SSL) {
  793. err = SSL_connect(socklist[i].ssl);
  794. } else if (state == ACCEPT_SSL) {
  795. err = SSL_accept(socklist[i].ssl);
  796. }
  797. if (!setjmp(alarmret)) {
  798. alarm(5); /* this is plenty of time */
  799. while ((err < 1) && (errno == EAGAIN)) {
  800. if (state == CONNECT_SSL) {
  801. err = SSL_connect(socklist[i].ssl);
  802. } else if (state == ACCEPT_SSL) {
  803. err = SSL_accept(socklist[i].ssl);
  804. }
  805. /* if ((errs!=SSL_ERROR_WANT_READ)&&(errs!=SSL_ERROR_WANT_WRITE)&& (errs!=SSL_ERROR_WANT_X509_LOOKUP)) */
  806. /* break; anything not one of these is a sufficient condition to break out... */
  807. }
  808. alarm(0);
  809. }
  810. errs = SSL_get_error(socklist[i].ssl, err);
  811. 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));
  812. if (errno) putlog(LOG_DEBUG, "*", "errno %d: %s", errno, strerror(errno));
  813. if (err == 1) {
  814. putlog(LOG_ERROR, "*", "SSL_link(%d, %d) was successfull", sock, state);
  815. return 1;
  816. } else {
  817. putlog(LOG_ERROR, "*", "SSL_link(%d, %d) failed", sock, state);
  818. dropssl(socklist[i].sock);
  819. }
  820. return 0;
  821. }
  822. #endif /* HAVE_SSL */
  823. /* Returns the given network byte order IP address in the
  824. * dotted format - "##.##.##.##"
  825. */
  826. char *iptostr(in_addr_t ip)
  827. {
  828. static char ipbuf[32];
  829. struct in_addr a;
  830. a.s_addr = ip;
  831. return (char *) egg_inet_ntop(AF_INET, &a, ipbuf, sizeof(ipbuf));
  832. }
  833. /* Short routine to answer a connect received on a socket made previously
  834. * by open_listen ... returns hostname of the caller & the new socket
  835. * does NOT dispose of old "public" socket!
  836. */
  837. int answer(int sock, char *caller, in_addr_t *ip, port_t *port, int binary)
  838. {
  839. int new_sock;
  840. socklen_t addrlen;
  841. struct sockaddr_in from;
  842. #ifdef USE_IPV6
  843. int af_ty = sockprotocol(sock);
  844. struct sockaddr_in6 from6;
  845. egg_bzero(&from6, sizeof(struct sockaddr_in6));
  846. if (af_ty == AF_INET6) {
  847. addrlen = sizeof(from6);
  848. new_sock = accept(sock, (struct sockaddr *) &from6, &addrlen);
  849. } else {
  850. #endif /* USE_IPV6 */
  851. addrlen = sizeof(struct sockaddr);
  852. new_sock = accept(sock, (struct sockaddr *) &from, &addrlen);
  853. #ifdef USE_IPV6
  854. }
  855. #endif /* USE_IPV6 */
  856. if (new_sock < 0)
  857. return -1;
  858. if (ip != NULL) {
  859. #ifdef USE_IPV6
  860. /* Detect IPv4 in IPv6 mapped address .... */
  861. if (af_ty == AF_INET6 && (!IN6_IS_ADDR_V4MAPPED(&from6.sin6_addr))) {
  862. egg_inet_ntop(AF_INET6, &from6.sin6_addr, caller, 119);
  863. caller[120] = 0;
  864. *ip = 0L;
  865. } else if (IN6_IS_ADDR_V4MAPPED(&from6.sin6_addr)) { /* ...and convert it to plain (AF_INET) IPv4 address (openssh) */
  866. struct sockaddr_in *from4 = (struct sockaddr_in *)&from6;
  867. struct in_addr addr;
  868. memcpy(&addr, ((char *)&from6.sin6_addr) + 12, sizeof(addr));
  869. egg_memset(&from, 0, sizeof(from));
  870. from4->sin_family = AF_INET;
  871. addrlen = sizeof(*from4);
  872. memcpy(&from4->sin_addr, &addr, sizeof(addr));
  873. *ip = from4->sin_addr.s_addr;
  874. strlcpy(caller, iptostr(*ip), 121);
  875. *ip = ntohl(*ip);
  876. } else {
  877. #endif /* USE_IPV6 */
  878. *ip = from.sin_addr.s_addr;
  879. strlcpy(caller, iptostr(*ip), 121);
  880. *ip = ntohl(*ip);
  881. #ifdef USE_IPV6
  882. }
  883. #endif /* USE_IPV6 */
  884. }
  885. if (port != NULL) {
  886. #ifdef USE_IPV6
  887. if (af_ty == AF_INET6)
  888. *port = ntohs(from6.sin6_port);
  889. else
  890. #endif /* USE_IPV6 */
  891. *port = ntohs(from.sin_port);
  892. }
  893. /* Set up all the normal socket crap */
  894. setsock(new_sock, (binary ? SOCK_BINARY : 0));
  895. sdprintf("Answered socket %d: %s", new_sock, caller);
  896. return new_sock;
  897. }
  898. /* Like open_telnet, but uses server & port specifications of dcc
  899. */
  900. int open_telnet_dcc(int sock, char *server, char *port)
  901. {
  902. port_t p;
  903. unsigned long addr;
  904. char sv[500] = "";
  905. unsigned char c[4] = "";
  906. #ifdef DEBUG_IPV6
  907. debug1("open_telnet_dcc %s", server);
  908. #endif /* DEBUG_IPV6 */
  909. if (port != NULL)
  910. p = atoi(port);
  911. else
  912. p = 2000;
  913. #ifdef USE_IPV6
  914. if (sockprotocol(sock) == AF_INET6) {
  915. # ifdef DEBUG_IPV6
  916. debug0("open_telnet_dcc, af_inet6!");
  917. # endif /* DEBUG_IPV6 */
  918. strlcpy(sv, server, sizeof sv);
  919. debug2("%s should be %s",sv,server);
  920. } else {
  921. #endif /* USE_IPV6 */
  922. if (server != NULL)
  923. addr = my_atoul(server);
  924. else
  925. addr = 0L;
  926. if (addr < (1 << 24))
  927. return -3; /* fake address */
  928. c[0] = (addr >> 24) & 0xff;
  929. c[1] = (addr >> 16) & 0xff;
  930. c[2] = (addr >> 8) & 0xff;
  931. c[3] = addr & 0xff;
  932. simple_sprintf(sv, "%u.%u.%u.%u", c[0], c[1], c[2], c[3]);
  933. #ifdef USE_IPV6
  934. }
  935. /* strcpy(sv,hostnamefromip(addr)); */
  936. # ifdef DEBUG_IPV6
  937. debug3("open_telnet_raw %s %d %d", sv, sock, p);
  938. # endif /* DEBUG_IPV6 */
  939. #endif /* USE_IPV6 */
  940. return open_telnet_raw(sock, sv, p, 0);
  941. }
  942. /* Attempts to read from all the sockets in socklist
  943. * fills s with up to 511 bytes if available, and returns the array index
  944. *
  945. * on EOF: returns -1, with socket in len
  946. * on socket error: returns -2
  947. * if nothing is ready: returns -3
  948. */
  949. static int sockread(char *s, int *len)
  950. {
  951. fd_set fd;
  952. int fds = 0, i, fdtmp, x;
  953. struct timeval t;
  954. int grab = SGRAB + 1;
  955. egg_timeval_t howlong;
  956. if (timer_get_shortest(&howlong)) {
  957. /* No timer, default to 1 second. */
  958. t.tv_sec = 1;
  959. t.tv_usec = 0;
  960. }
  961. else {
  962. t.tv_sec = howlong.sec;
  963. t.tv_usec = howlong.usec;
  964. }
  965. FD_ZERO(&fd);
  966. for (i = 0; i < MAXSOCKS; i++) {
  967. if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_VIRTUAL))) {
  968. if ((socklist[i].sock == STDOUT) && !backgrd)
  969. fdtmp = STDIN;
  970. else
  971. fdtmp = socklist[i].sock;
  972. if (fdtmp > fds)
  973. fds = fdtmp;
  974. FD_SET(fdtmp, &fd);
  975. }
  976. }
  977. fds++;
  978. x = select(fds, &fd, NULL, NULL, &t);
  979. if (x > 0) {
  980. /* Something happened */
  981. for (i = 0; i < MAXSOCKS; i++) {
  982. if ((!(socklist[i].flags & SOCK_UNUSED)) && ((FD_ISSET(socklist[i].sock, &fd)) ||
  983. #ifdef HAVE_SSL
  984. ((socklist[i].ssl) && (SSL_pending(socklist[i].ssl))) ||
  985. #endif /* HAVE_SSL */
  986. ((socklist[i].sock == STDOUT) && (!backgrd) && (FD_ISSET(STDIN, &fd))))) {
  987. if (socklist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
  988. /* Listening socket -- don't read, just return activity */
  989. /* Same for connection attempt */
  990. /* (for strong connections, require a read to succeed first) */
  991. if (socklist[i].flags & SOCK_PROXYWAIT) { /* drummer */
  992. /* Hang around to get the return code from proxy */
  993. grab = 10;
  994. } else if (!(socklist[i].flags & SOCK_STRONGCONN)) {
  995. debug1("net: connect! sock %d", socklist[i].sock);
  996. s[0] = 0;
  997. *len = 0;
  998. #ifdef HAVE_SSL
  999. /* debug0("CALLING SSL_LINK() FROM SOCKREAD");
  1000. if (!ssl_link(socklist[i].sock))
  1001. debug0("SSL_LINK FAILED");
  1002. debug0("BACK FROM SSL_LINK()"); */
  1003. #endif /* HAVE_SSL */
  1004. return i;
  1005. }
  1006. } else if (socklist[i].flags & SOCK_PASS) {
  1007. s[0] = 0;
  1008. *len = 0;
  1009. return i;
  1010. }
  1011. errno = 0;
  1012. if ((socklist[i].sock == STDOUT) && !backgrd)
  1013. x = read(STDIN, s, grab);
  1014. else {
  1015. #ifdef HAVE_SSL
  1016. if (socklist[i].ssl) {
  1017. x = SSL_read(socklist[i].ssl, s, grab);
  1018. if (x < 0) {
  1019. int err = SSL_get_error(socklist[i].ssl, x);
  1020. x = -1;
  1021. switch (err) {
  1022. case SSL_ERROR_WANT_READ:
  1023. errno = EAGAIN;
  1024. break;
  1025. case SSL_ERROR_WANT_WRITE:
  1026. errno = EAGAIN;
  1027. break;
  1028. case SSL_ERROR_WANT_X509_LOOKUP:
  1029. errno = EAGAIN;
  1030. break;
  1031. }
  1032. }
  1033. } else
  1034. #endif /* HAVE_SSL */
  1035. x = read(socklist[i].sock, s, grab);
  1036. }
  1037. if (x <= 0) { /* eof */
  1038. if (errno != EAGAIN) { /* EAGAIN happens when the operation would block
  1039. on a non-blocking socket, if the socket is going
  1040. to die, it will die later, otherwise it will connect */
  1041. *len = socklist[i].sock;
  1042. socklist[i].flags &= ~SOCK_CONNECT;
  1043. debug1("net: eof!(read) socket %d", socklist[i].sock);
  1044. return -1;
  1045. } else {
  1046. debug3("sockread EAGAIN: %d %d (%s)", socklist[i].sock, errno, strerror(errno));
  1047. continue; /* EAGAIN */
  1048. }
  1049. }
  1050. s[x] = 0;
  1051. *len = x;
  1052. if (socklist[i].flags & SOCK_PROXYWAIT) {
  1053. debug2("net: socket: %d proxy errno: %d", socklist[i].sock, s[1]);
  1054. socklist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
  1055. switch (s[1]) {
  1056. case 90: /* Success */
  1057. s[0] = 0;
  1058. *len = 0;
  1059. return i;
  1060. case 91: /* Failed */
  1061. errno = ECONNREFUSED;
  1062. break;
  1063. case 92: /* No identd */
  1064. case 93: /* Identd said wrong username */
  1065. /* A better error message would be "socks misconfigured"
  1066. * or "identd not working" but this is simplest.
  1067. */
  1068. errno = ENETUNREACH;
  1069. break;
  1070. }
  1071. *len = socklist[i].sock;
  1072. return -1;
  1073. }
  1074. return i;
  1075. }
  1076. }
  1077. } else if (x == -1)
  1078. return -2; /* socket error */
  1079. else {
  1080. s[0] = 0;
  1081. *len = 0;
  1082. }
  1083. return -3;
  1084. }
  1085. /* sockgets: buffer and read from sockets
  1086. *
  1087. * Attempts to read from all registered sockets for up to one second. if
  1088. * after one second, no complete data has been received from any of the
  1089. * sockets, 's' will be empty, 'len' will be 0, and sockgets will return -3.
  1090. * if there is returnable data received from a socket, the data will be
  1091. * in 's' (null-terminated if non-binary), the length will be returned
  1092. * in len, and the socket number will be returned.
  1093. * normal sockets have their input buffered, and each call to sockgets
  1094. * will return one line terminated with a '\n'. binary sockets are not
  1095. * buffered and return whatever coems in as soon as it arrives.
  1096. * listening sockets will return an empty string when a connection comes in.
  1097. * connecting sockets will return an empty string on a successful connect,
  1098. * or EOF on a failed connect.
  1099. * if an EOF is detected from any of the sockets, that socket number will be
  1100. * put in len, and -1 will be returned.
  1101. * the maximum length of the string returned is 512 (including null)
  1102. *
  1103. * Returns -4 if we handled something that shouldn't be handled by the
  1104. * dcc functions. Simply ignore it.
  1105. */
  1106. int sockgets(char *s, int *len)
  1107. {
  1108. char xx[SGRAB + 4] = "", *p = NULL, *px = NULL;
  1109. int ret;
  1110. for (int i = 0; i < MAXSOCKS; i++) {
  1111. /* Check for stored-up data waiting to be processed */
  1112. if (!(socklist[i].flags & SOCK_UNUSED) && !(socklist[i].flags & SOCK_BUFFER) && (socklist[i].inbuf != NULL)) {
  1113. if (!(socklist[i].flags & SOCK_BINARY)) {
  1114. /* look for \r too cos windows can't follow RFCs */
  1115. p = strchr(socklist[i].inbuf, '\n');
  1116. if (p == NULL)
  1117. p = strchr(socklist[i].inbuf, '\r');
  1118. if (p != NULL) {
  1119. *p = 0;
  1120. if (strlen(socklist[i].inbuf) > SGRAB)
  1121. socklist[i].inbuf[SGRAB] = 0;
  1122. strcpy(s, socklist[i].inbuf);
  1123. px = (char *) my_calloc(1, strlen(p + 1) + 1);
  1124. strcpy(px, p + 1);
  1125. free(socklist[i].inbuf);
  1126. if (px[0])
  1127. socklist[i].inbuf = px;
  1128. else {
  1129. free(px);
  1130. socklist[i].inbuf = NULL;
  1131. }
  1132. /* Strip CR if this was CR/LF combo */
  1133. if (s[strlen(s) - 1] == '\r')
  1134. s[strlen(s) - 1] = 0;
  1135. if (s[0] && socklist[i].encstatus)
  1136. link_read(i, s, (size_t *) len);
  1137. *len = strlen(s);
  1138. return socklist[i].sock;
  1139. }
  1140. } else {
  1141. /* i dont think any of this is *ever* called */
  1142. /* Handling buffered binary data (must have been SOCK_BUFFER before). */
  1143. if (socklist[i].inbuflen <= SGRAB) {
  1144. *len = socklist[i].inbuflen;
  1145. egg_memcpy(s, socklist[i].inbuf, socklist[i].inbuflen);
  1146. free(socklist[i].inbuf);
  1147. socklist[i].inbuf = NULL;
  1148. socklist[i].inbuflen = 0;
  1149. } else {
  1150. /* Split up into chunks of SGRAB bytes. */
  1151. *len = SGRAB;
  1152. egg_memcpy(s, socklist[i].inbuf, *len);
  1153. egg_memcpy(socklist[i].inbuf, socklist[i].inbuf + *len, *len);
  1154. socklist[i].inbuflen -= *len;
  1155. socklist[i].inbuf = (char *) my_realloc(socklist[i].inbuf, socklist[i].inbuflen);
  1156. }
  1157. return socklist[i].sock;
  1158. }
  1159. }
  1160. /* Also check any sockets that might have EOF'd during write */
  1161. if (!(socklist[i].flags & SOCK_UNUSED)
  1162. && (socklist[i].flags & SOCK_EOFD)) {
  1163. s[0] = 0;
  1164. *len = socklist[i].sock;
  1165. return -1;
  1166. }
  1167. }
  1168. /* No pent-up data of any worth -- down to business */
  1169. *len = 0;
  1170. ret = sockread(xx, len);
  1171. if (ret < 0) {
  1172. s[0] = 0;
  1173. return ret;
  1174. }
  1175. /* Binary, listening and passed on sockets don't get buffered. */
  1176. if (socklist[ret].flags & SOCK_CONNECT) {
  1177. if (socklist[ret].flags & SOCK_STRONGCONN) {
  1178. socklist[ret].flags &= ~SOCK_STRONGCONN;
  1179. /* Buffer any data that came in, for future read. */
  1180. socklist[ret].inbuflen = *len;
  1181. socklist[ret].inbuf = (char *) my_calloc(1, *len + 1);
  1182. /* It might be binary data. You never know. */
  1183. egg_memcpy(socklist[ret].inbuf, xx, *len);
  1184. socklist[ret].inbuf[*len] = 0;
  1185. }
  1186. socklist[ret].flags &= ~SOCK_CONNECT;
  1187. s[0] = 0;
  1188. return socklist[ret].sock;
  1189. }
  1190. if (socklist[ret].flags & SOCK_BINARY) {
  1191. egg_memcpy(s, xx, *len);
  1192. return socklist[ret].sock;
  1193. }
  1194. if ((socklist[ret].flags & SOCK_LISTEN) || (socklist[ret].flags & SOCK_PASS))
  1195. return socklist[ret].sock;
  1196. if (socklist[ret].flags & SOCK_BUFFER) {
  1197. socklist[ret].inbuf = (char *) my_realloc(socklist[ret].inbuf,
  1198. socklist[ret].inbuflen + *len + 1);
  1199. egg_memcpy(socklist[ret].inbuf + socklist[ret].inbuflen, xx, *len);
  1200. socklist[ret].inbuflen += *len;
  1201. /* We don't know whether it's binary data. Make sure normal strings
  1202. will be handled properly later on too. */
  1203. socklist[ret].inbuf[socklist[ret].inbuflen] = 0;
  1204. return -4; /* Ignore this one. */
  1205. }
  1206. /* Might be necessary to prepend stored-up data! */
  1207. if (socklist[ret].inbuf != NULL) {
  1208. p = socklist[ret].inbuf;
  1209. socklist[ret].inbuf = (char *) my_calloc(1, strlen(p) + strlen(xx) + 1);
  1210. strcpy(socklist[ret].inbuf, p);
  1211. strcat(socklist[ret].inbuf, xx);
  1212. free(p);
  1213. if (strlen(socklist[ret].inbuf) < (SGRAB + 2)) {
  1214. strcpy(xx, socklist[ret].inbuf);
  1215. free(socklist[ret].inbuf);
  1216. socklist[ret].inbuf = NULL;
  1217. socklist[ret].inbuflen = 0;
  1218. } else {
  1219. p = socklist[ret].inbuf;
  1220. socklist[ret].inbuflen = strlen(p) - SGRAB;
  1221. socklist[ret].inbuf = (char *) my_calloc(1, socklist[ret].inbuflen + 1);
  1222. strcpy(socklist[ret].inbuf, p + SGRAB);
  1223. *(p + SGRAB) = 0;
  1224. strcpy(xx, p);
  1225. free(p);
  1226. /* (leave the rest to be post-pended later) */
  1227. }
  1228. }
  1229. bool data = 0;
  1230. /* Look for EOL marker; if it's there, i have something to show */
  1231. p = strchr(xx, '\n');
  1232. if (p == NULL)
  1233. p = strchr(xx, '\r');
  1234. if (p != NULL) {
  1235. *p = 0;
  1236. /* FIXME: overlapping here */
  1237. strcpy(s, xx);
  1238. strcpy(xx, p + 1);
  1239. /* if (s[0] && strlen(s) && (s[strlen(s) - 1] == '\r')) */
  1240. if (s[strlen(s) - 1] == '\r')
  1241. s[strlen(s) - 1] = 0;
  1242. data = 1; /* DCC_CHAT may now need to process a blank line */
  1243. /* NO! */
  1244. /* if (!s[0]) strcpy(s," "); */
  1245. } else {
  1246. s[0] = 0;
  1247. if (strlen(xx) >= SGRAB) {
  1248. /* String is too long, so just insert fake \n */
  1249. strcpy(s, xx);
  1250. xx[0] = 0;
  1251. data = 1;
  1252. }
  1253. }
  1254. if (s[0] && socklist[ret].encstatus)
  1255. link_read(ret, s, (size_t *) len);
  1256. *len = strlen(s);
  1257. /* Anything left that needs to be saved? */
  1258. if (!xx[0]) {
  1259. if (data)
  1260. return socklist[ret].sock;
  1261. else
  1262. return -3;
  1263. }
  1264. /* Prepend old data back */
  1265. if (socklist[ret].inbuf != NULL) {
  1266. p = socklist[ret].inbuf;
  1267. socklist[ret].inbuflen = strlen(p) + strlen(xx);
  1268. socklist[ret].inbuf = (char *) my_calloc(1, socklist[ret].inbuflen + 1);
  1269. strcpy(socklist[ret].inbuf, xx);
  1270. strcat(socklist[ret].inbuf, p);
  1271. free(p);
  1272. } else {
  1273. socklist[ret].inbuflen = strlen(xx);
  1274. socklist[ret].inbuf = (char *) my_calloc(1, socklist[ret].inbuflen + 1);
  1275. strcpy(socklist[ret].inbuf, xx);
  1276. }
  1277. if (data) {
  1278. return socklist[ret].sock;
  1279. } else {
  1280. return -3;
  1281. }
  1282. }
  1283. /* Dump something to a socket
  1284. *
  1285. * NOTE: Do NOT put Contexts in here if you want DEBUG to be meaningful!!
  1286. */
  1287. void tputs(register int z, char *s, size_t len)
  1288. {
  1289. if (z < 0) /* um... HELLO?! sanity check please! */
  1290. return;
  1291. if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
  1292. write(z, s, len);
  1293. return;
  1294. }
  1295. register int x, idx;
  1296. char *p = NULL;
  1297. for (register int i = 0; i < MAXSOCKS; i++) {
  1298. if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
  1299. for (idx = 0; idx < dcc_total; idx++) {
  1300. if (dcc[idx].type && (dcc[idx].sock == z) && dcc[idx].type->name) {
  1301. if (!strncmp(dcc[idx].type->name, "BOT", 3))
  1302. traffic.out_today.bn += len;
  1303. else if (!strcmp(dcc[idx].type->name, "SERVER"))
  1304. traffic.out_today.irc += len;
  1305. else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
  1306. traffic.out_today.dcc += len;
  1307. else if (!strncmp(dcc[idx].type->name, "FILES", 5))
  1308. traffic.out_today.filesys += len;
  1309. else if (!strcmp(dcc[idx].type->name, "SEND"))
  1310. traffic.out_today.trans += len;
  1311. else if (!strncmp(dcc[idx].type->name, "GET", 3))
  1312. traffic.out_today.trans += len;
  1313. else
  1314. traffic.out_today.unknown += len;
  1315. break;
  1316. }
  1317. }
  1318. if (len && socklist[i].encstatus)
  1319. s = link_write(i, s, &len);
  1320. if (socklist[i].outbuf != NULL) {
  1321. /* Already queueing: just add it */
  1322. p = (char *) my_realloc(socklist[i].outbuf, socklist[i].outbuflen + len);
  1323. egg_memcpy(p + socklist[i].outbuflen, s, len);
  1324. socklist[i].outbuf = p;
  1325. socklist[i].outbuflen += len;
  1326. if (socklist[i].encstatus && s)
  1327. free(s);
  1328. return;
  1329. }
  1330. /* Try. */
  1331. #ifdef HAVE_SSL
  1332. if (socklist[i].ssl) {
  1333. x = SSL_write(socklist[i].ssl, s, len);
  1334. if (x < 0) {
  1335. int err = SSL_get_error(socklist[i].ssl, x);
  1336. x = -1;
  1337. switch (err) {
  1338. case SSL_ERROR_WANT_READ:
  1339. errno = EAGAIN;
  1340. break;
  1341. case SSL_ERROR_WANT_WRITE:
  1342. errno = EAGAIN;
  1343. break;
  1344. case SSL_ERROR_WANT_X509_LOOKUP:
  1345. errno = EAGAIN;
  1346. break;
  1347. }
  1348. }
  1349. } else
  1350. #endif /* HAVE_SSL */
  1351. #ifdef HAVE_ZLIB_H
  1352. /*
  1353. if (socklist[i].gz) {
  1354. FILE *fp;
  1355. fp = gzdopen(z, "wb0");
  1356. x = gzwrite(fp, s, len);
  1357. } else
  1358. */
  1359. #endif /* HAVE_ZLIB_H */
  1360. x = write(z, s, len);
  1361. if (x == -1)
  1362. x = 0;
  1363. if ((size_t) x < len) {
  1364. /* Socket is full, queue it */
  1365. socklist[i].outbuf = (char *) my_calloc(1, len - x);
  1366. egg_memcpy(socklist[i].outbuf, &s[x], len - x);
  1367. socklist[i].outbuflen = len - x;
  1368. }
  1369. if (socklist[i].encstatus && s)
  1370. free(s);
  1371. return;
  1372. }
  1373. }
  1374. /* Make sure we don't cause a crash by looping here */
  1375. static int inhere = 0;
  1376. if (!inhere) {
  1377. inhere = 1;
  1378. putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
  1379. s[strlen(s) - 1] = 0;
  1380. putlog(LOG_MISC, "*", "!-> '%s'", s);
  1381. inhere = 0;
  1382. }
  1383. /* if (socklist[i].encstatus > 0)
  1384. free(s);
  1385. */
  1386. }
  1387. int findanysnum(register int sock)
  1388. {
  1389. register int i = 0;
  1390. if (sock != -1)
  1391. for (i = 0; i < MAXSOCKS; i++)
  1392. if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED))
  1393. return i;
  1394. return -1;
  1395. }
  1396. static int findanyidx(register int sock)
  1397. {
  1398. register int j;
  1399. if (sock != -1)
  1400. for (j = 0; j < dcc_total; j++)
  1401. if (dcc[j].type && dcc[j].sock == sock)
  1402. return j;
  1403. return -1;
  1404. }
  1405. /* tputs might queue data for sockets, let's dump as much of it as
  1406. * possible.
  1407. */
  1408. void dequeue_sockets()
  1409. {
  1410. int i, x, z = 0, fds = 0;
  1411. fd_set wfds;
  1412. struct timeval tv;
  1413. /* ^-- start poptix test code, this should avoid writes to sockets not ready to be written to. */
  1414. FD_ZERO(&wfds);
  1415. tv.tv_sec = 0;
  1416. tv.tv_usec = 0; /* we only want to see if it's ready for writing, no need to actually wait.. */
  1417. for (i = 0; i < MAXSOCKS; i++) {
  1418. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].outbuf != NULL) {
  1419. FD_SET(socklist[i].sock, &wfds);
  1420. if (socklist[i].sock > fds)
  1421. fds = socklist[i].sock;
  1422. z = 1;
  1423. }
  1424. }
  1425. if (!z)
  1426. return; /* nothing to write */
  1427. fds++;
  1428. select(fds, NULL, &wfds, NULL, &tv);
  1429. /* end poptix */
  1430. for (i = 0; i < MAXSOCKS; i++) {
  1431. if (!(socklist[i].flags & SOCK_UNUSED) &&
  1432. (socklist[i].outbuf != NULL) && (FD_ISSET(socklist[i].sock, &wfds))) {
  1433. /* Trick tputs into doing the work */
  1434. errno = 0;
  1435. #ifdef HAVE_SSL
  1436. if (socklist[i].ssl) {
  1437. x = write(socklist[i].sock, socklist[i].outbuf, socklist[i].outbuflen);
  1438. if (x < 0) {
  1439. int err = SSL_get_error(socklist[i].ssl, x);
  1440. x = -1;
  1441. switch (err) {
  1442. case SSL_ERROR_WANT_READ:
  1443. errno = EAGAIN;
  1444. break;
  1445. case SSL_ERROR_WANT_WRITE:
  1446. errno = EAGAIN;
  1447. break;
  1448. case SSL_ERROR_WANT_X509_LOOKUP:
  1449. errno = EAGAIN;
  1450. break;
  1451. }
  1452. }
  1453. } else
  1454. #endif /* HAVE_SSL */
  1455. x = write(socklist[i].sock, socklist[i].outbuf, socklist[i].outbuflen);
  1456. if ((x < 0) && (errno != EAGAIN)
  1457. #ifdef EBADSLT
  1458. && (errno != EBADSLT)
  1459. #endif /* EBADSLT */
  1460. #ifdef ENOTCONN
  1461. && (errno != ENOTCONN)
  1462. #endif /* EBADSLT */
  1463. ) {
  1464. /* This detects an EOF during writing */
  1465. debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock, strerror(errno), errno);
  1466. socklist[i].flags |= SOCK_EOFD;
  1467. } else if ((size_t) x == socklist[i].outbuflen) {
  1468. /* If the whole buffer was sent, nuke it */
  1469. free(socklist[i].outbuf);
  1470. socklist[i].outbuf = NULL;
  1471. socklist[i].outbuflen = 0;
  1472. } else if (x > 0) {
  1473. char *p = socklist[i].outbuf;
  1474. /* This removes any sent bytes from the beginning of the buffer */
  1475. socklist[i].outbuf = (char *) my_calloc(1, socklist[i].outbuflen - x);
  1476. egg_memcpy(socklist[i].outbuf, p + x, socklist[i].outbuflen - x);
  1477. socklist[i].outbuflen -= x;
  1478. free(p);
  1479. } else {
  1480. debug3("dequeue_sockets(): errno = %d (%s) on %d", errno, strerror(errno), socklist[i].sock);
  1481. }
  1482. /* All queued data was sent. Call handler if one exists and the
  1483. * dcc entry wants it.
  1484. */
  1485. if (!socklist[i].outbuf) {
  1486. int idx = findanyidx(socklist[i].sock);
  1487. if (idx >= 0 && dcc[idx].type && dcc[idx].type->outdone)
  1488. dcc[idx].type->outdone(idx);
  1489. }
  1490. }
  1491. }
  1492. }
  1493. /*
  1494. * Debugging stuff
  1495. */
  1496. void tell_netdebug(int idx)
  1497. {
  1498. char s[80] = "";
  1499. dprintf(idx, "Open sockets:");
  1500. for (int i = 0; i < MAXSOCKS; i++) {
  1501. if (!(socklist[i].flags & SOCK_UNUSED)) {
  1502. simple_sprintf(s, " %d", socklist[i].sock);
  1503. if (socklist[i].flags & SOCK_BINARY)
  1504. strcat(s, " (binary)");
  1505. if (socklist[i].flags & SOCK_LISTEN)
  1506. strcat(s, " (listen)");
  1507. if (socklist[i].flags & SOCK_PASS)
  1508. strcat(s, " (passed on)");
  1509. if (socklist[i].flags & SOCK_CONNECT)
  1510. strcat(s, " (connecting)");
  1511. if (socklist[i].flags & SOCK_STRONGCONN)
  1512. strcat(s, " (strong)");
  1513. if (socklist[i].flags & SOCK_NONSOCK)
  1514. strcat(s, " (file)");
  1515. if (socklist[i].inbuf != NULL)
  1516. sprintf(&s[strlen(s)], " (inbuf: %04X)", strlen(socklist[i].inbuf));
  1517. if (socklist[i].outbuf != NULL)
  1518. sprintf(&s[strlen(s)], " (outbuf: %06lX)", (unsigned long) socklist[i].outbuflen);
  1519. if (socklist[i].host)
  1520. sprintf(&s[strlen(s)], " (%s:%d)", socklist[i].host, socklist[i].port);
  1521. strcat(s, ",");
  1522. dprintf(idx, "%s", s);
  1523. }
  1524. }
  1525. dprintf(idx, " done.\n");
  1526. }
  1527. /* Checks wether the referenced socket has data queued.
  1528. *
  1529. * Returns true if the incoming/outgoing (depending on 'type') queues
  1530. * contain data, otherwise false.
  1531. */
  1532. bool sock_has_data(int type, int sock)
  1533. {
  1534. bool ret = 0;
  1535. int i;
  1536. for (i = 0; i < MAXSOCKS; i++)
  1537. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
  1538. break;
  1539. if (i < MAXSOCKS) {
  1540. switch (type) {
  1541. case SOCK_DATA_OUTGOING:
  1542. ret = (socklist[i].outbuf != NULL);
  1543. break;
  1544. case SOCK_DATA_INCOMING:
  1545. ret = (socklist[i].inbuf != NULL);
  1546. break;
  1547. }
  1548. } else
  1549. debug1("sock_has_data: could not find socket #%d, returning false.", sock);
  1550. return ret;
  1551. }