dcc.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  1. /*
  2. * dcc.c -- handles:
  3. * activity on a dcc socket
  4. * disconnect on a dcc socket
  5. * ...and that's it! (but it's a LOT)
  6. *
  7. */
  8. #include "common.h"
  9. #include "dcc.h"
  10. #include "settings.h"
  11. #include "tclhash.h"
  12. #include "adns.h"
  13. #include "main.h"
  14. #include "cmds.h"
  15. #include "color.h"
  16. #include "net.h"
  17. #include "response.h"
  18. #include "misc.h"
  19. #include "users.h"
  20. #include "userrec.h"
  21. #include "userent.h"
  22. #include "match.h"
  23. #include "auth.h"
  24. #include "dccutil.h"
  25. #include "crypt.h"
  26. #include "chanprog.h"
  27. #include "botmsg.h"
  28. #include "botcmd.h"
  29. #include "botnet.h"
  30. #include <ctype.h>
  31. #include <errno.h>
  32. #include <sys/types.h>
  33. #include <sys/socket.h>
  34. #include <netinet/in.h>
  35. #include <sys/utsname.h>
  36. #include <sys/stat.h>
  37. #include "tandem.h"
  38. #include "core_binds.h"
  39. struct dcc_t *dcc = NULL; /* DCC list */
  40. time_t timesync = 0;
  41. int dcc_total = 0; /* size of dcc table */
  42. int dccn = 0; /* actual number of dcc entries */
  43. static time_t password_timeout = 20; /* Time to wait for a password from a user */
  44. static time_t auth_timeout = 40;
  45. static time_t bot_timeout = 15; /* Bot timeout value */
  46. static time_t identtimeout = 15; /* Timeout value for ident lookups */
  47. static time_t dupwait_timeout = 5; /* Timeout for rejecting duplicate entries */
  48. #ifdef LEAF
  49. static bool protect_telnet = 0; /* Even bother with ident lookups :) */
  50. #else /* !LEAF */
  51. static bool protect_telnet = 1;
  52. #endif /* LEAF */
  53. static int flood_telnet_thr = 10; /* Number of telnet connections to be
  54. * considered a flood */
  55. static time_t flood_telnet_time = 5; /* In how many seconds? */
  56. static void dcc_telnet_got_ident(int, char *);
  57. static void dcc_telnet_pass(int, int);
  58. static void
  59. strip_telnet(int sock, char *buf, int *len)
  60. {
  61. unsigned char *p = (unsigned char *) buf, *o = (unsigned char *) buf;
  62. int mark;
  63. while (*p != 0) {
  64. while ((*p != TLN_IAC) && (*p != 0)) {
  65. if (*p == 0xA0) {
  66. *o++ = 32;
  67. p++;
  68. } else
  69. *o++ = *p++;
  70. }
  71. if (*p == TLN_IAC) {
  72. p++;
  73. mark = 2;
  74. if (!*p)
  75. mark = 1; /* bogus */
  76. if ((*p >= TLN_WILL) && (*p <= TLN_DONT)) {
  77. mark = 3;
  78. if (!*(p + 1))
  79. mark = 2; /* bogus */
  80. }
  81. if (*p == TLN_WILL) {
  82. /* WILL X -> response: DONT X */
  83. /* except WILL ECHO which we just smile and ignore */
  84. if (*(p + 1) != TLN_ECHO) {
  85. write(sock, TLN_IAC_C TLN_DONT_C, 2);
  86. write(sock, p + 1, 1);
  87. }
  88. }
  89. if (*p == TLN_DO) {
  90. /* DO X -> response: WONT X */
  91. /* except DO ECHO which we just smile and ignore */
  92. if (*(p + 1) != TLN_ECHO) {
  93. write(sock, TLN_IAC_C TLN_WONT_C, 2);
  94. write(sock, p + 1, 1);
  95. }
  96. }
  97. if (*p == TLN_AYT) {
  98. /* "are you there?" */
  99. /* response is: "hell yes!" */
  100. write(sock, "\r\nHell, yes!\r\n", 14);
  101. }
  102. /* Anything else can probably be ignored */
  103. p += mark - 1;
  104. *len = *len - mark;
  105. }
  106. }
  107. *o = *p;
  108. }
  109. #ifdef HUB
  110. void
  111. send_timesync(int idx)
  112. {
  113. /* Send timesync to idx, or all lower bots if idx<0 */
  114. if (idx >= 0)
  115. dprintf(idx, "ts %li\n", timesync + now);
  116. else {
  117. char s[15] = "";
  118. sprintf(s, "ts %li\n", timesync + now);
  119. for (int i = 0; i < dcc_total; i++) {
  120. if (dcc[i].type && (dcc[i].type == &DCC_BOT) && (bot_aggressive_to(dcc[i].user))) {
  121. dprintf(i, s);
  122. }
  123. }
  124. }
  125. }
  126. #endif /* HUB */
  127. static void
  128. greet_new_bot(int idx)
  129. {
  130. char *sysname = NULL;
  131. struct utsname un;
  132. dcc[idx].timeval = now;
  133. dcc[idx].u.bot->version[0] = 0;
  134. dcc[idx].u.bot->sysname[0] = 0;
  135. dcc[idx].u.bot->numver = 0;
  136. #ifdef HUB
  137. if (dcc[idx].user && (!(dcc[idx].user->flags & USER_OP))) {
  138. putlog(LOG_BOTS, "*", "Rejecting link from %s", dcc[idx].nick);
  139. dprintf(idx, "error You are being rejected.\n");
  140. dprintf(idx, "bye\n");
  141. killsock(dcc[idx].sock);
  142. lostdcc(idx);
  143. return;
  144. }
  145. #endif /* HUB */
  146. if (bot_hublevel(dcc[idx].user) == 999)
  147. dcc[idx].status |= STAT_LEAF;
  148. dcc[idx].status |= STAT_LINKING;
  149. if (uname(&un) < 0)
  150. sysname = "*";
  151. else
  152. sysname = un.sysname;
  153. dprintf(idx, "v 1001500 %d Wraith %s <%s> %d %li %s\n", HANDLEN, egg_version, "-", localhub, buildts, egg_version);
  154. dprintf(idx, "si %s %s %s", conf.username ? conf.username : "*", un.sysname ? un.sysname : "*",
  155. un.nodename ? un.nodename : "*");
  156. for (int i = 0; i < dcc_total; i++) {
  157. if (dcc[i].type && dcc[i].type == &DCC_FORK_BOT) {
  158. killsock(dcc[i].sock);
  159. lostdcc(i);
  160. }
  161. }
  162. }
  163. static void
  164. bot_version(int idx, char *par)
  165. {
  166. dcc[idx].timeval = now;
  167. if (in_chain(dcc[idx].nick)) {
  168. dprintf(idx, "error Sorry, already connected.\n");
  169. dprintf(idx, "bye\n");
  170. killsock(dcc[idx].sock);
  171. lostdcc(idx);
  172. return;
  173. }
  174. if ((par[0] >= '0') && (par[0] <= '9')) {
  175. char *work = NULL;
  176. work = newsplit(&par);
  177. dcc[idx].u.bot->numver = atoi(work);
  178. /* old numver crap */
  179. } else
  180. dcc[idx].u.bot->numver = 0;
  181. dprintf(idx, "tb %s\n", conf.bot->nick);
  182. size_t l = atol(newsplit(&par));
  183. if (l != HANDLEN) {
  184. putlog(LOG_BOTS, "*", "Non-matching handle lengths with %s, they use %d characters.", dcc[idx].nick, l);
  185. dprintf(idx, "error Non-matching handle length: mine %d, yours %d\n", HANDLEN, l);
  186. dprintf(idx, "bye %s\n", "bad handlen");
  187. killsock(dcc[idx].sock);
  188. lostdcc(idx);
  189. return;
  190. }
  191. char x[1024] = "", *vversion = NULL;
  192. int vlocalhub = 0;
  193. time_t vbuildts = 0;
  194. strncpyz(dcc[idx].u.bot->version, par, 120);
  195. newsplit(&par); /* 'ver' */
  196. newsplit(&par); /* handlen */
  197. newsplit(&par); /* network */
  198. if (par[0])
  199. vlocalhub = atoi(newsplit(&par));
  200. if (par[0])
  201. vbuildts = atol(newsplit(&par));
  202. if (par[0])
  203. vversion = newsplit(&par);
  204. #ifdef HUB
  205. putlog(LOG_BOTS, "*", DCC_LINKED, dcc[idx].nick);
  206. chatout("*** Linked to %s\n", dcc[idx].nick);
  207. #else /* !HUB */
  208. putlog(LOG_BOTS, "*", "Linked to botnet.");
  209. chatout("*** Linked to botnet.\n");
  210. #endif /* HUB */
  211. #ifdef HUB
  212. botnet_send_nlinked(idx, dcc[idx].nick, conf.bot->nick, '!', vlocalhub, vbuildts, vversion);
  213. #endif /* HUB */
  214. dump_links(idx);
  215. touch_laston(dcc[idx].user, "linked", now);
  216. dcc[idx].type = &DCC_BOT;
  217. addbot(dcc[idx].nick, dcc[idx].nick, conf.bot->nick, '-', vlocalhub, vbuildts, vversion);
  218. egg_snprintf(x, sizeof x, "v 1001500");
  219. bot_shareupdate(idx, x);
  220. bot_share(idx, x);
  221. dprintf(idx, "el\n");
  222. }
  223. void
  224. failed_link(int idx)
  225. {
  226. char s[81] = "", s1[512] = "";
  227. if (dcc[idx].u.bot->linker[0]) {
  228. egg_snprintf(s, sizeof s, "Couldn't link to %s.", dcc[idx].nick);
  229. strcpy(s1, dcc[idx].u.bot->linker);
  230. add_note(s1, conf.bot->nick, s, -2, 0);
  231. }
  232. if (dcc[idx].u.bot->numver >= (-1))
  233. putlog(LOG_BOTS, "*", DCC_LINKFAIL, dcc[idx].nick);
  234. if (dcc[idx].sock != -1) {
  235. for (int i = 0; i < MAXSOCKS; i++)
  236. if (dcc[idx].sock == socklist[i].sock) {
  237. killsock(dcc[idx].sock);
  238. dcc[idx].sock = -1;
  239. }
  240. }
  241. strcpy(s, dcc[idx].nick);
  242. lostdcc(idx);
  243. autolink_cycle(s); /* Check for more auto-connections */
  244. }
  245. static void
  246. cont_link(int idx, char *buf, int ii)
  247. {
  248. /* Now set the initial link key (incoming only, we're not sending more until we get an OK)... */
  249. int snum = findanysnum(dcc[idx].sock);
  250. if (snum >= 0) {
  251. struct sockaddr_in sa;
  252. socklen_t socklen = sizeof(sa);
  253. char tmp[301] = "";
  254. /* If we're already connected somewhere, unlink and idle a sec */
  255. for (int i = 0; i < dcc_total; i++) {
  256. if (dcc[i].type && (dcc[i].type == &DCC_BOT) && (!bot_aggressive_to(dcc[i].user))) {
  257. putlog(LOG_BOTS, "*", "Unlinking %s - restructure", dcc[i].nick);
  258. botnet_send_unlinked(i, dcc[i].nick, "Restructure");
  259. killsock(dcc[i].sock);
  260. lostdcc(i);
  261. usleep(1000 * 500);
  262. break;
  263. }
  264. }
  265. /*. ssl_link(dcc[idx].sock, CONNECT_SSL); */
  266. dcc[idx].type = &DCC_BOT_NEW;
  267. dcc[idx].u.bot->numver = 0;
  268. dprintf(idx, "%s\n", conf.bot->nick);
  269. /* initkey-gen leaf */
  270. /* bdhash myport hubnick mynick */
  271. egg_bzero(&sa, socklen);
  272. getsockname(socklist[snum].sock, (struct sockaddr *) &sa, &socklen);
  273. egg_snprintf(tmp, sizeof tmp, "%s@%4x@%s@%s", settings.bdhash, sa.sin_port, dcc[idx].nick, conf.bot->nick);
  274. strncpyz(socklist[snum].ikey, SHA1(tmp), sizeof(socklist[snum].ikey));
  275. putlog(LOG_DEBUG, "@", "Link hash for %s: %s", dcc[idx].nick, tmp);
  276. putlog(LOG_DEBUG, "@", "initkey (%d): %s", strlen(socklist[snum].ikey), socklist[snum].ikey);
  277. /* We've send our conf.bot->nick and set the key for the link on the sock, wait for 'elink' back to verify key */
  278. socklist[snum].encstatus = 1;
  279. socklist[snum].gz = 1;
  280. } else {
  281. /* FIXME: This seems unnecesary, we didnt find a socket yet we are killing one.. ?*/
  282. /* killsock(dcc[idx].sock); */
  283. lostdcc(idx);
  284. }
  285. return;
  286. }
  287. static void
  288. dcc_bot_new(int idx, char *buf, int x)
  289. {
  290. /* struct userrec *u = get_user_by_handle(userlist, dcc[idx].nick); */
  291. char *code = NULL;
  292. strip_telnet(dcc[idx].sock, buf, &x);
  293. code = newsplit(&buf);
  294. if (!egg_strcasecmp(code, "goodbye!")) {
  295. greet_new_bot(idx);
  296. } else if (!egg_strcasecmp(code, "v")) {
  297. bot_version(idx, buf);
  298. } else if (!egg_strcasecmp(code, "elink")) {
  299. int snum = findanysnum(dcc[idx].sock);
  300. /* putlog(LOG_DEBUG, "*", "Got elink: %s %s", code, buf); */
  301. /* Set the socket key and we're linked */
  302. if (snum >= 0) {
  303. char *tmp = decrypt_string(settings.salt2, newsplit(&buf));
  304. strncpyz(socklist[snum].okey, tmp, sizeof(socklist[snum].okey));
  305. strncpyz(socklist[snum].ikey, socklist[snum].okey, sizeof(socklist[snum].ikey));
  306. socklist[snum].iseed = atoi(buf);
  307. socklist[snum].oseed = atoi(buf);
  308. dprintf(idx, "elinkdone\n");
  309. putlog(LOG_BOTS, "*", "Handshake with %s succeeded, we're linked.", dcc[idx].nick);
  310. free(tmp);
  311. }
  312. } else if (!egg_strcasecmp(code, "error")) {
  313. putlog(LOG_MISC, "*", "ERROR linking %s: %s", dcc[idx].nick, buf);
  314. killsock(dcc[idx].sock);
  315. lostdcc(idx);
  316. } else if (strcmp(code, "")) {
  317. /* Invalid password/digest on leaf */
  318. putlog(LOG_WARN, "*", "%s failed encrypted link handshake", dcc[idx].nick);
  319. killsock(dcc[idx].sock);
  320. lostdcc(idx);
  321. }
  322. /* Ignore otherwise */
  323. }
  324. static void
  325. eof_dcc_bot_new(int idx)
  326. {
  327. putlog(LOG_BOTS, "*", DCC_LOSTBOT, dcc[idx].nick);
  328. killsock(dcc[idx].sock);
  329. lostdcc(idx);
  330. }
  331. static void
  332. timeout_dcc_bot_new(int idx)
  333. {
  334. #ifdef LEAF
  335. putlog(LOG_BOTS, "*", "Timeout: bot link to %s", dcc[idx].nick);
  336. #else /* !LEAF */
  337. putlog(LOG_BOTS, "*", DCC_TIMEOUT, dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  338. #endif /* LEAF */
  339. killsock(dcc[idx].sock);
  340. lostdcc(idx);
  341. }
  342. static void
  343. display_dcc_bot_new(int idx, char *buf)
  344. {
  345. sprintf(buf, "bot* waited %lis", now - dcc[idx].timeval);
  346. }
  347. static void
  348. free_dcc_bot_(int n, void *x)
  349. {
  350. if (dcc[n].type == &DCC_BOT) {
  351. unvia(n, findbot(dcc[n].nick));
  352. rembot(dcc[n].nick);
  353. }
  354. free(x);
  355. }
  356. struct dcc_table DCC_BOT_NEW = {
  357. "BOT_NEW",
  358. 0,
  359. eof_dcc_bot_new,
  360. dcc_bot_new,
  361. &bot_timeout,
  362. timeout_dcc_bot_new,
  363. display_dcc_bot_new,
  364. free_dcc_bot_,
  365. NULL,
  366. NULL
  367. };
  368. /* Hash function for tandem bot commands */
  369. extern botcmd_t C_bot[];
  370. static void
  371. dcc_bot(int idx, char *code, int i)
  372. {
  373. char *msg = NULL;
  374. int f;
  375. strip_telnet(dcc[idx].sock, code, &i);
  376. if (debug_output) {
  377. /* if (code[0] != 'z' && code[1] != 'b' && code[2] != ' ') { */
  378. if (code[0] == 's')
  379. putlog(LOG_BOTSHARE, "@", "{%s} %s", dcc[idx].nick, code);
  380. else
  381. putlog(LOG_BOTNET, "@", "<-[%s] %s", dcc[idx].nick, code);
  382. /* } */
  383. }
  384. msg = strchr(code, ' ');
  385. if (msg) {
  386. *msg = 0;
  387. msg++;
  388. } else
  389. msg = "";
  390. for (f = i = 0; C_bot[i].name && !f; i++) {
  391. int y = egg_strcasecmp(code, C_bot[i].name);
  392. if (!y) {
  393. /* Found a match */
  394. (C_bot[i].func) (idx, msg);
  395. f = 1;
  396. } else if (y < 0)
  397. return;
  398. }
  399. }
  400. static void
  401. eof_dcc_bot(int idx)
  402. {
  403. char x[1024] = "";
  404. int bots, users;
  405. bots = bots_in_subtree(findbot(dcc[idx].nick));
  406. users = users_in_subtree(findbot(dcc[idx].nick));
  407. egg_snprintf(x, sizeof x,
  408. "Lost bot: %s (lost %d bot%s and %d user%s)",
  409. dcc[idx].nick, bots, (bots != 1) ? "s" : "", users, (users != 1) ? "s" : "");
  410. putlog(LOG_BOTS, "*", "%s.", x);
  411. chatout("*** %s\n", x);
  412. botnet_send_unlinked(idx, dcc[idx].nick, x);
  413. killsock(dcc[idx].sock);
  414. lostdcc(idx);
  415. }
  416. static void
  417. display_dcc_bot(int idx, char *buf)
  418. {
  419. size_t i = simple_sprintf(buf, "bot flags: ");
  420. buf[i++] = b_status(idx) & STAT_PINGED ? 'P' : 'p';
  421. buf[i++] = b_status(idx) & STAT_SHARE ? 'U' : 'u';
  422. buf[i++] = b_status(idx) & STAT_CALLED ? 'C' : 'c';
  423. buf[i++] = b_status(idx) & STAT_OFFERED ? 'O' : 'o';
  424. buf[i++] = b_status(idx) & STAT_SENDING ? 'S' : 's';
  425. buf[i++] = b_status(idx) & STAT_GETTING ? 'G' : 'g';
  426. buf[i++] = b_status(idx) & STAT_WARNED ? 'W' : 'w';
  427. buf[i++] = b_status(idx) & STAT_LEAF ? 'L' : 'l';
  428. buf[i++] = b_status(idx) & STAT_LINKING ? 'I' : 'i';
  429. buf[i++] = b_status(idx) & STAT_AGGRESSIVE ? 'a' : 'A';
  430. buf[i++] = b_status(idx) & STAT_OFFEREDU ? 'B' : 'b';
  431. buf[i++] = b_status(idx) & STAT_SENDINGU ? 'D' : 'd';
  432. buf[i++] = b_status(idx) & STAT_GETTINGU ? 'E' : 'e';
  433. #ifdef USE_IPV6
  434. if (sockprotocol(dcc[idx].sock) == AF_INET6 && dcc[idx].host6[0])
  435. buf[i++] = '6';
  436. #endif /* USE_IPV6 */
  437. buf[i++] = 0;
  438. }
  439. static void
  440. display_dcc_fork_bot(int idx, char *buf)
  441. {
  442. sprintf(buf, "conn bot");
  443. }
  444. struct dcc_table DCC_BOT = {
  445. "BOT",
  446. DCT_BOT,
  447. eof_dcc_bot,
  448. dcc_bot,
  449. NULL,
  450. NULL,
  451. display_dcc_bot,
  452. free_dcc_bot_,
  453. NULL,
  454. NULL
  455. };
  456. struct dcc_table DCC_FORK_BOT = {
  457. "FORK_BOT",
  458. 0,
  459. failed_link,
  460. cont_link,
  461. &connect_timeout,
  462. failed_link,
  463. display_dcc_fork_bot,
  464. free_dcc_bot_,
  465. NULL,
  466. NULL
  467. };
  468. static void
  469. dcc_identd(int idx, char *buf, int atr)
  470. {
  471. char outbuf[1024] = "";
  472. egg_snprintf(outbuf, sizeof outbuf, "%s : USERID : UNIX : %s\n", buf, conf.bot->nick);
  473. tputs(dcc[idx].sock, outbuf, strlen(outbuf));
  474. /* just close it, functions neededing it will open it. */
  475. identd_close();
  476. }
  477. static void
  478. eof_dcc_identd(int idx)
  479. {
  480. /* dont bother logging it, who gives a fuck */
  481. killsock(dcc[idx].sock);
  482. lostdcc(idx);
  483. }
  484. static void
  485. display_dcc_identd(int idx, char *buf)
  486. {
  487. sprintf(buf, "idtd %d%s", dcc[idx].port, (dcc[idx].status & LSTN_PUBLIC) ? " pub" : "");
  488. }
  489. struct dcc_table DCC_IDENTD = {
  490. "IDENTD",
  491. DCT_LISTEN,
  492. eof_dcc_identd,
  493. dcc_identd,
  494. NULL,
  495. NULL,
  496. display_dcc_identd,
  497. NULL,
  498. NULL,
  499. NULL
  500. };
  501. static void
  502. dcc_identd_connect(int idx, char *buf, int atr)
  503. {
  504. in_addr_t ip;
  505. port_t port;
  506. int j, sock;
  507. char s[UHOSTLEN + 1] = "";
  508. if (dcc_total + 1 > max_dcc) {
  509. j = answer(dcc[idx].sock, s, &ip, &port, 0);
  510. if (j != -1)
  511. killsock(j);
  512. return;
  513. }
  514. sock = answer(dcc[idx].sock, s, &ip, &port, 0);
  515. while ((sock == -1) && (errno == EAGAIN))
  516. sock = answer(sock, s, &ip, &port, 0);
  517. if (sock < 0) {
  518. putlog(LOG_MISC, "*", DCC_FAILED, strerror(errno));
  519. return;
  520. }
  521. /* changeover_dcc(idx, &DCC_IDENTD, 0); */
  522. j = new_dcc(&DCC_IDENTD, 0);
  523. dcc[j].sock = sock;
  524. dcc[j].port = port;
  525. dcc[j].addr = dcc[idx].addr;
  526. strcpy(dcc[j].host, dcc[idx].host);
  527. strcpy(dcc[j].nick, "*");
  528. /* dcc[j].u.ident_sock = dcc[idx].sock; */
  529. dcc[j].timeval = now;
  530. }
  531. struct dcc_table DCC_IDENTD_CONNECT = {
  532. "IDENTD",
  533. DCT_LISTEN,
  534. eof_dcc_identd,
  535. dcc_identd_connect,
  536. NULL,
  537. NULL,
  538. display_dcc_identd,
  539. NULL,
  540. NULL,
  541. NULL
  542. };
  543. static void
  544. dcc_chat_secpass(int idx, char *buf, int atr)
  545. {
  546. int badauth = 0;
  547. if (dccauth) {
  548. if (!atr)
  549. return;
  550. char check[MD5_HASH_LENGTH + 7] = "";
  551. strip_telnet(dcc[idx].sock, buf, &atr);
  552. atr = dcc[idx].user ? dcc[idx].user->flags : 0;
  553. egg_snprintf(check, sizeof check, "+Auth %s", dcc[idx].hash);
  554. badauth = strcmp(check, buf);
  555. /* +secpass */
  556. }
  557. /* Correct pass or secpass! */
  558. if (!dccauth || (dccauth && !badauth)) {
  559. putlog(LOG_MISC, "*", DCC_LOGGEDIN, dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  560. if (dcc[idx].u.chat->away) {
  561. free(dcc[idx].u.chat->away);
  562. dcc[idx].u.chat->away = NULL;
  563. }
  564. dcc[idx].type = &DCC_CHAT;
  565. dcc[idx].status &= ~STAT_CHAT;
  566. dcc[idx].u.chat->channel = -2;
  567. /* Turn echo back on for telnet sessions (send IAC WON'T ECHO). */
  568. if (dcc[idx].status & STAT_TELNET)
  569. dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  570. stats_add(dcc[idx].user, 1, 0);
  571. if (!get_user(&USERENTRY_SECPASS, dcc[idx].user)) { /* this should check how many logins instead */
  572. char pass[17] = "";
  573. dprintf(idx, "********************************************************************\n \n \n");
  574. dprintf(idx, "%sWARNING: YOU DO NOT HAVE A SECPASS SET, NOW SETTING A RANDOM ONE....%s\n", FLASH(-1), FLASH_END(-1));
  575. make_rand_str(pass, 16);
  576. set_user(&USERENTRY_SECPASS, dcc[idx].user, pass);
  577. #ifdef HUB
  578. write_userfile(idx);
  579. #endif /* HUB */
  580. dprintf(idx, "Your secpass is now: %s%s%s\n", pass, BOLD(-1), BOLD_END(-1));
  581. dprintf(idx, "Make sure you do not lose this, as it is needed to login for now on.\n \n");
  582. dprintf(idx, "********************************************************************\n");
  583. }
  584. dcc_chatter(idx);
  585. } else if (dccauth && badauth) { /* bad auth */
  586. dprintf(idx, "%s\n", response(RES_BADUSERPASS));
  587. putlog(LOG_MISC, "*", DCC_BADAUTH, dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  588. if (dcc[idx].u.chat->away) { /* su from a dumb user */
  589. /* Turn echo back on for telnet sessions (send IAC WON'T ECHO). */
  590. if (dcc[idx].status & STAT_TELNET)
  591. dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  592. dcc[idx].user = get_user_by_handle(userlist, dcc[idx].u.chat->away);
  593. strcpy(dcc[idx].nick, dcc[idx].u.chat->away);
  594. free(dcc[idx].u.chat->away);
  595. free(dcc[idx].u.chat->su_nick);
  596. dcc[idx].u.chat->away = NULL;
  597. dcc[idx].u.chat->su_nick = NULL;
  598. dcc[idx].type = &DCC_CHAT;
  599. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  600. botnet_send_join_idx(idx);
  601. chanout_but(-1, dcc[idx].u.chat->channel, DCC_JOIN, dcc[idx].nick);
  602. } else {
  603. killsock(dcc[idx].sock);
  604. lostdcc(idx);
  605. }
  606. }
  607. }
  608. static void
  609. eof_dcc_general(int idx)
  610. {
  611. putlog(LOG_MISC, "*", DCC_LOSTDCC, dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  612. killsock(dcc[idx].sock);
  613. lostdcc(idx);
  614. }
  615. static void
  616. tout_dcc_chat_secpass(int idx)
  617. {
  618. putlog(LOG_MISC, "*", DCC_SPWDTIMEOUT, dcc[idx].nick, dcc[idx].host);
  619. killsock(dcc[idx].sock);
  620. lostdcc(idx);
  621. }
  622. static void
  623. display_dcc_chat_secpass(int idx, char *buf)
  624. {
  625. sprintf(buf, "secpass waited %lis", now - dcc[idx].timeval);
  626. }
  627. static void
  628. tout_dcc_chat_pass(int idx)
  629. {
  630. putlog(LOG_MISC, "*", DCC_PWDTIMEOUT, dcc[idx].nick, dcc[idx].host);
  631. killsock(dcc[idx].sock);
  632. lostdcc(idx);
  633. }
  634. static void
  635. display_dcc_chat_pass(int idx, char *buf)
  636. {
  637. sprintf(buf, "pass waited %lis", now - dcc[idx].timeval);
  638. }
  639. static void
  640. kill_dcc_general(int idx, void *x)
  641. {
  642. register struct chat_info *p = (struct chat_info *) x;
  643. if (p) {
  644. if (p->buffer) {
  645. struct msgq *r = NULL, *q = NULL;
  646. for (r = dcc[idx].u.chat->buffer; r; r = q) {
  647. q = r->next;
  648. free(r->msg);
  649. free(r);
  650. }
  651. }
  652. if (p->away) {
  653. free(p->away);
  654. }
  655. free(p);
  656. }
  657. }
  658. /* Remove the color control codes that mIRC,pIRCh etc use to make
  659. * their client seem so fecking cool! (Sorry, Khaled, you are a nice
  660. * guy, but when you added this feature you forced people to either
  661. * use your *SHAREWARE* client or face screenfulls of crap!)
  662. */
  663. static void
  664. strip_mirc_codes(int flags, char *text)
  665. {
  666. char *dd = text;
  667. while (*text) {
  668. switch (*text) {
  669. case 2: /* Bold text */
  670. if (flags & STRIP_BOLD) {
  671. text++;
  672. continue;
  673. }
  674. break;
  675. case 3: /* mIRC colors? */
  676. if (flags & STRIP_COLOR) {
  677. if (egg_isdigit(text[1])) { /* Is the first char a number? */
  678. text += 2; /* Skip over the ^C and the first digit */
  679. if (egg_isdigit(*text))
  680. text++; /* Is this a double digit number? */
  681. if (*text == ',') { /* Do we have a background color next? */
  682. if (egg_isdigit(text[1]))
  683. text += 2; /* Skip over the first background digit */
  684. if (egg_isdigit(*text))
  685. text++; /* Is it a double digit? */
  686. }
  687. } else
  688. text++;
  689. continue;
  690. }
  691. break;
  692. case 7:
  693. if (flags & STRIP_BELLS) {
  694. text++;
  695. continue;
  696. }
  697. break;
  698. case 0x16: /* Reverse video */
  699. if (flags & STRIP_REV) {
  700. text++;
  701. continue;
  702. }
  703. break;
  704. case 0x1f: /* Underlined text */
  705. if (flags & STRIP_UNDER) {
  706. text++;
  707. continue;
  708. }
  709. break;
  710. case 033:
  711. if (flags & STRIP_ANSI) {
  712. text++;
  713. if (*text == '[') {
  714. text++;
  715. while ((*text == ';') || egg_isdigit(*text))
  716. text++;
  717. if (*text)
  718. text++; /* also kill the following char */
  719. }
  720. continue;
  721. }
  722. break;
  723. }
  724. *dd++ = *text++; /* Move on to the next char */
  725. }
  726. *dd = 0;
  727. }
  728. static void
  729. append_line(int idx, char *line)
  730. {
  731. struct msgq *p = NULL, *q = NULL;
  732. struct chat_info *c = (dcc[idx].type == &DCC_CHAT) ? dcc[idx].u.chat : dcc[idx].u.file->chat;
  733. if (c->current_lines > 1000) {
  734. /* They're probably trying to fill up the bot nuke the sods :) */
  735. for (p = c->buffer; p; p = q) {
  736. q = p->next;
  737. free(p->msg);
  738. free(p);
  739. }
  740. c->buffer = 0;
  741. dcc[idx].status &= ~STAT_PAGE;
  742. do_boot(idx, conf.bot->nick, "too many pages - senq full");
  743. return;
  744. }
  745. size_t l = strlen(line);
  746. if ((c->line_count < c->max_line) && (c->buffer == NULL)) {
  747. c->line_count++;
  748. tputs(dcc[idx].sock, line, l);
  749. } else {
  750. c->current_lines++;
  751. if (c->buffer == NULL)
  752. q = NULL;
  753. else
  754. for (q = c->buffer; q->next; q = q->next) ;
  755. p = (struct msgq *) calloc(1, sizeof(struct msgq));
  756. p->len = l;
  757. p->msg = (char *) calloc(1, l + 1);
  758. p->next = NULL;
  759. strcpy(p->msg, line);
  760. if (q == NULL)
  761. c->buffer = p;
  762. else
  763. q->next = p;
  764. }
  765. }
  766. static void
  767. out_dcc_general(int idx, char *buf, void *x)
  768. {
  769. register struct chat_info *p = (struct chat_info *) x;
  770. char *y = buf;
  771. strip_mirc_codes(p->strip_flags, buf);
  772. if (dcc[idx].status & STAT_TELNET)
  773. y = add_cr(buf);
  774. if (dcc[idx].status & STAT_PAGE)
  775. append_line(idx, y);
  776. else
  777. tputs(dcc[idx].sock, y, strlen(y));
  778. }
  779. static struct dcc_table DCC_CHAT_SECPASS = {
  780. "CHAT_SECPASS",
  781. 0,
  782. eof_dcc_general,
  783. dcc_chat_secpass,
  784. &auth_timeout,
  785. tout_dcc_chat_secpass,
  786. display_dcc_chat_secpass,
  787. kill_dcc_general,
  788. out_dcc_general,
  789. NULL
  790. };
  791. static void
  792. dcc_chat_pass(int idx, char *buf, int atr)
  793. {
  794. if (!atr)
  795. return;
  796. strip_telnet(dcc[idx].sock, buf, &atr);
  797. atr = dcc[idx].user ? dcc[idx].user->flags : 0;
  798. if (dcc[idx].user->bot) {
  799. if (!egg_strcasecmp(buf, "elinkdone")) {
  800. free(dcc[idx].u.chat);
  801. dcc[idx].type = &DCC_BOT_NEW;
  802. dcc[idx].u.bot = (struct bot_info *) calloc(1, sizeof(struct bot_info));
  803. dcc[idx].status = STAT_CALLED;
  804. dprintf(idx, "goodbye!\n");
  805. greet_new_bot(idx);
  806. #ifdef HUB
  807. send_timesync(idx);
  808. #endif /* HUB */
  809. } else {
  810. /* Invalid password/digest on hub */
  811. putlog(LOG_WARN, "*", "%s failed encrypted link handshake.", dcc[idx].nick);
  812. killsock(dcc[idx].sock);
  813. lostdcc(idx);
  814. }
  815. return;
  816. }
  817. if (u_pass_match(dcc[idx].user, buf)) {
  818. if (dccauth) {
  819. char randstr[51] = "";
  820. make_rand_str(randstr, 50);
  821. strncpyz(dcc[idx].hash, makehash(dcc[idx].user, randstr), sizeof dcc[idx].hash);
  822. dcc[idx].type = &DCC_CHAT_SECPASS;
  823. dcc[idx].timeval = now;
  824. dprintf(idx, "-Auth %s %s\n", randstr, conf.bot->nick);
  825. } else {
  826. dcc_chat_secpass(idx, buf, atr);
  827. }
  828. } else {
  829. dprintf(idx, "%s\n", response(RES_BADUSERPASS));
  830. putlog(LOG_MISC, "*", DCC_BADLOGIN, dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  831. if (dcc[idx].u.chat->away) { /* su from a dumb user */
  832. /* Turn echo back on for telnet sessions (send IAC WON'T ECHO). */
  833. if (dcc[idx].status & STAT_TELNET)
  834. dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  835. dcc[idx].user = get_user_by_handle(userlist, dcc[idx].u.chat->away);
  836. strcpy(dcc[idx].nick, dcc[idx].u.chat->away);
  837. free(dcc[idx].u.chat->away);
  838. free(dcc[idx].u.chat->su_nick);
  839. dcc[idx].u.chat->away = NULL;
  840. dcc[idx].u.chat->su_nick = NULL;
  841. dcc[idx].type = &DCC_CHAT;
  842. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  843. botnet_send_join_idx(idx);
  844. chanout_but(-1, dcc[idx].u.chat->channel, DCC_JOIN, dcc[idx].nick);
  845. } else {
  846. killsock(dcc[idx].sock);
  847. lostdcc(idx);
  848. }
  849. }
  850. }
  851. struct dcc_table DCC_CHAT_PASS = {
  852. "CHAT_PASS",
  853. 0,
  854. eof_dcc_general,
  855. dcc_chat_pass,
  856. &password_timeout,
  857. tout_dcc_chat_pass,
  858. display_dcc_chat_pass,
  859. kill_dcc_general,
  860. out_dcc_general,
  861. NULL
  862. };
  863. /* Make sure ansi code is just for color-changing
  864. */
  865. static int
  866. check_ansi(char *v)
  867. {
  868. int count = 2;
  869. if (*v++ != '\033')
  870. return 1;
  871. if (*v++ != '[')
  872. return 1;
  873. while (*v) {
  874. if (*v == 'm')
  875. return 0;
  876. if ((*v != ';') && ((*v < '0') || (*v > '9')))
  877. return count;
  878. v++;
  879. count++;
  880. }
  881. return count;
  882. }
  883. static void
  884. eof_dcc_chat(int idx)
  885. {
  886. putlog(LOG_MISC, "*", DCC_LOSTDCC, dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  887. if (dcc[idx].u.chat->channel >= 0) {
  888. chanout_but(idx, dcc[idx].u.chat->channel, "*** %s lost dcc link.\n", dcc[idx].nick);
  889. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  890. botnet_send_part_idx(idx, "lost dcc link");
  891. }
  892. check_bind_chof(dcc[idx].nick, idx);
  893. killsock(dcc[idx].sock);
  894. lostdcc(idx);
  895. }
  896. static void
  897. dcc_chat(int idx, char *buf, int len)
  898. {
  899. int i = 0;
  900. strip_telnet(dcc[idx].sock, buf, &len);
  901. if (buf[0] && (buf[0] != settings.dcc_prefix[0]) && !(dcc[idx].user && (dcc[idx].user->flags & USER_NOFLOOD)) &&
  902. detect_dcc_flood(&dcc[idx].timeval, dcc[idx].u.chat, idx))
  903. return;
  904. dcc[idx].timeval = now;
  905. if (buf[0]) {
  906. int nathan = 0, doron = 0, fixed = 0;
  907. char *v = buf, *d = buf;
  908. /* Check for beeps and cancel annoying ones */
  909. while (*v)
  910. switch (*v) {
  911. case 1: /* CTCP ?! */
  912. v++;
  913. break;
  914. case 7: /* Beep - no more than 3 */
  915. nathan++;
  916. if (nathan > 3)
  917. v++;
  918. else
  919. *d++ = *v++;
  920. break;
  921. case 8: /* Backspace - for lame telnet's :) */
  922. if (d > buf) {
  923. d--;
  924. }
  925. v++;
  926. break;
  927. case 27: /* ESC - ansi code? */
  928. doron = check_ansi(v);
  929. /* If it's valid, append a return-to-normal code at the end */
  930. if (!doron) {
  931. *d++ = *v++;
  932. fixed = 1;
  933. } else
  934. v += doron;
  935. break;
  936. case '\r': /* Weird pseudo-linefeed */
  937. v++;
  938. break;
  939. default:
  940. *d++ = *v++;
  941. }
  942. if (fixed)
  943. strcpy(d, "\033[0m");
  944. else
  945. *d = 0;
  946. if (u_pass_match(dcc[idx].user, buf)) { /* user said their password :) */
  947. dprintf(idx, "Sure you want that going to the partyline? ;) (msg to partyline halted.)\n");
  948. } else if (!strncmp(buf, "+Auth ", 6)) { /* ignore extra +Auth lines */
  949. } else if ((!strncmp(buf, settings.dcc_prefix, strlen(settings.dcc_prefix))) || (dcc[idx].u.chat->channel < 0)) {
  950. if (!strncmp(buf, settings.dcc_prefix, strlen(settings.dcc_prefix))) /* strip '.' out */
  951. buf++;
  952. v = newsplit(&buf);
  953. rmspace(buf);
  954. check_bind_dcc(v, idx, buf);
  955. } else if (buf[0] == ',') {
  956. int me = 0;
  957. if ((buf[1] == 'm') && (buf[2] == 'e') && buf[3] == ' ')
  958. me = 1;
  959. for (i = 0; i < dcc_total; i++) {
  960. if (dcc[i].type) {
  961. int ok = 0;
  962. if (dcc[i].type->flags & DCT_MASTER) {
  963. if ((dcc[i].type != &DCC_CHAT) || (dcc[i].u.chat->channel >= 0))
  964. if ((i != idx) || (dcc[idx].status & STAT_ECHO))
  965. ok = 1;
  966. }
  967. if (ok) {
  968. struct userrec *u = get_user_by_handle(userlist, dcc[i].nick);
  969. if (u && (u->flags & USER_MASTER)) {
  970. if (me)
  971. dprintf(i, "-> %s%s\n", dcc[idx].nick, buf + 3);
  972. else
  973. dprintf(i, "-%s-> %s\n", dcc[idx].nick, buf + 1);
  974. }
  975. }
  976. }
  977. }
  978. } else if (buf[0] == '\'') {
  979. int me = 0;
  980. if ((buf[1] == 'm') && (buf[2] == 'e') && ((buf[3] == ' ') || (buf[3] == '\'') || (buf[3] == ',')))
  981. me = 1;
  982. for (i = 0; i < dcc_total; i++) {
  983. if (dcc[i].type && dcc[i].type->flags & DCT_CHAT) {
  984. if (me)
  985. dprintf(i, "=> %s%s\n", dcc[idx].nick, buf + 3);
  986. else
  987. dprintf(i, "=%s=> %s\n", dcc[idx].nick, buf + 1);
  988. }
  989. }
  990. } else {
  991. if (dcc[idx].u.chat->away != NULL)
  992. not_away(idx);
  993. if (!strncmp(buf, "CTCP_MESSAGE ", 13)) /* irssi */
  994. buf += 13;
  995. if (!strncmp(buf, "ACTION ", 7)) {
  996. buf += 7;
  997. check_bind_dcc("me", idx, buf);
  998. } else {
  999. if (dcc[idx].status & STAT_ECHO)
  1000. chanout_but(-1, dcc[idx].u.chat->channel, "<%s> %s\n", dcc[idx].nick, buf);
  1001. else
  1002. chanout_but(idx, dcc[idx].u.chat->channel, "<%s> %s\n", dcc[idx].nick, buf);
  1003. botnet_send_chan(-1, conf.bot->nick, dcc[idx].nick, dcc[idx].u.chat->channel, buf);
  1004. }
  1005. }
  1006. }
  1007. if (dcc[idx].type == &DCC_CHAT) /* Could have change to files */
  1008. if (dcc[idx].status & STAT_PAGE)
  1009. flush_lines(idx, dcc[idx].u.chat);
  1010. }
  1011. static void
  1012. display_dcc_chat(int idx, char *buf)
  1013. {
  1014. size_t i = simple_sprintf(buf, "chat flags: ");
  1015. int colori = 0;
  1016. buf[i++] = dcc[idx].status & STAT_CHAT ? 'C' : 'c';
  1017. buf[i++] = dcc[idx].status & STAT_PARTY ? 'P' : 'p';
  1018. buf[i++] = dcc[idx].status & STAT_TELNET ? 'T' : 't';
  1019. buf[i++] = dcc[idx].status & STAT_ECHO ? 'E' : 'e';
  1020. buf[i++] = dcc[idx].status & STAT_PAGE ? 'P' : 'p';
  1021. if ((colori = coloridx(idx)))
  1022. buf[i++] = colori == 1 ? 'A' : 'M';
  1023. #ifdef USE_IPV6
  1024. if (sockprotocol(dcc[idx].sock) == AF_INET6 && dcc[idx].host6[0])
  1025. buf[i++] = '6';
  1026. #endif /* USE_IPV6 */
  1027. simple_sprintf(buf + i, "/%d", dcc[idx].u.chat->channel);
  1028. }
  1029. struct dcc_table DCC_CHAT = {
  1030. "CHAT",
  1031. DCT_CHAT | DCT_MASTER | DCT_SHOWWHO | DCT_VALIDIDX | DCT_SIMUL | DCT_CANBOOT | DCT_REMOTEWHO,
  1032. eof_dcc_chat,
  1033. dcc_chat,
  1034. NULL,
  1035. NULL,
  1036. display_dcc_chat,
  1037. kill_dcc_general,
  1038. out_dcc_general,
  1039. NULL
  1040. };
  1041. static int lasttelnets;
  1042. static char lasttelnethost[81];
  1043. static time_t lasttelnettime;
  1044. /* A modified detect_flood for incoming telnet flood protection.
  1045. */
  1046. static bool
  1047. detect_telnet_flood(char *floodhost)
  1048. {
  1049. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  1050. get_user_flagrec(get_user_by_host(floodhost), &fr, NULL);
  1051. if (!flood_telnet_thr || glob_noflood(fr))
  1052. return 0; /* No flood protection */
  1053. if (egg_strcasecmp(lasttelnethost, floodhost)) { /* New... */
  1054. strcpy(lasttelnethost, floodhost);
  1055. lasttelnettime = now;
  1056. lasttelnets = 0;
  1057. return 0;
  1058. }
  1059. if (lasttelnettime < now - flood_telnet_time) {
  1060. /* Flood timer expired, reset it */
  1061. lasttelnettime = now;
  1062. lasttelnets = 0;
  1063. return 0;
  1064. }
  1065. lasttelnets++;
  1066. if (lasttelnets >= flood_telnet_thr) { /* FLOOD! */
  1067. /* Reset counters */
  1068. lasttelnets = 0;
  1069. lasttelnettime = 0;
  1070. lasttelnethost[0] = 0;
  1071. putlog(LOG_MISC, "*", IRC_TELNETFLOOD, floodhost);
  1072. addignore(floodhost, origbotname, "Telnet connection flood", now + (60 * ignore_time));
  1073. return 1;
  1074. }
  1075. return 0;
  1076. }
  1077. static void dcc_telnet_dns_callback(int id, void *, const char *, char **);
  1078. static void
  1079. dcc_telnet(int idx, char *buf, int ii)
  1080. {
  1081. in_addr_t ip;
  1082. port_t port;
  1083. char s[UHOSTLEN + 1] = "";
  1084. if (dcc_total + 1 > max_dcc) {
  1085. int j;
  1086. j = answer(dcc[idx].sock, s, &ip, &port, 0);
  1087. if (j != -1) {
  1088. dprintf(-j, "Sorry, too many connections already.\r\n");
  1089. killsock(j);
  1090. }
  1091. return;
  1092. }
  1093. int sock = answer(dcc[idx].sock, s, &ip, &port, 0);
  1094. while ((sock == -1) && (errno == EAGAIN))
  1095. sock = answer(sock, s, &ip, &port, 0);
  1096. /*. ssl_link ACCEPT_SSL should go here!!!! */
  1097. if (sock < 0) {
  1098. putlog(LOG_MISC, "*", DCC_FAILED, strerror(errno));
  1099. return;
  1100. }
  1101. /* Buffer data received on this socket. */
  1102. sockoptions(sock, EGG_OPTION_SET, SOCK_BUFFER);
  1103. #if SIZEOF_SHORT == 2
  1104. if (port < 1024) {
  1105. #else
  1106. if (port < 1024 || port > 65535) {
  1107. #endif
  1108. putlog(LOG_BOTS, "*", DCC_BADSRC, s, port);
  1109. killsock(sock);
  1110. return;
  1111. }
  1112. char x[1024] = "";
  1113. int i;
  1114. putlog(LOG_DEBUG, "*", "Telnet connection: %s/%d", s, port);
  1115. sprintf(x, "-telnet!telnet@%s", iptostr(htonl(ip)));
  1116. if (match_ignore(x) || detect_telnet_flood(x)) {
  1117. killsock(sock);
  1118. return;
  1119. }
  1120. i = new_dcc(&DCC_IDENTWAIT, 0);
  1121. dcc[i].sock = sock;
  1122. dcc[i].addr = ip;
  1123. strcpy(dcc[i].host, s);
  1124. #ifdef USE_IPV6
  1125. if (sockprotocol(sock) == AF_INET6)
  1126. strcpy(dcc[i].host6, s);
  1127. #endif /* USE_IPV6 */
  1128. dcc[i].port = port;
  1129. dcc[i].timeval = now;
  1130. strcpy(dcc[i].nick, "*");
  1131. dcc[i].u.other = (void *) idx;
  1132. dcc[i].dns_id = egg_dns_reverse(s, 20, dcc_telnet_dns_callback, (void *) i);
  1133. }
  1134. static void dcc_telnet_dns_callback(int id, void *client_data, const char *ip, char **hosts)
  1135. {
  1136. int idx = -1, i = (int) client_data;
  1137. if (!valid_dns_id(i, id))
  1138. return;
  1139. int j = 0, sock;
  1140. char s2[UHOSTLEN + 20] = "";
  1141. if (dcc[i].type)
  1142. idx = (int) dcc[i].u.other;
  1143. dcc[i].u.other = 0;
  1144. strncpyz(dcc[i].host, hosts ? hosts[0] : ip, UHOSTLEN);
  1145. sprintf(s2, "-telnet!telnet@%s", dcc[i].host);
  1146. if (match_ignore(s2) || detect_telnet_flood(s2)) {
  1147. killsock(dcc[i].sock);
  1148. lostdcc(i);
  1149. return;
  1150. }
  1151. if (!valid_idx(idx)) {
  1152. putlog(LOG_BOTS, "*", "Lost listening socket while resolving %s", dcc[i].host);
  1153. killsock(dcc[i].sock);
  1154. lostdcc(i);
  1155. return;
  1156. }
  1157. if (dcc[idx].host[0] == '@') {
  1158. /* Restrict by hostname */
  1159. if (!wild_match(dcc[idx].host + 1, dcc[i].host)) {
  1160. putlog(LOG_BOTS, "*", DCC_BADHOST, dcc[i].host);
  1161. killsock(dcc[i].sock);
  1162. lostdcc(i);
  1163. return;
  1164. }
  1165. }
  1166. /* . ssl_link(dcc[i].sock, ACCEPT_SSL); */
  1167. dcc[i].timeval = now;
  1168. dcc[i].u.ident_sock = dcc[idx].sock;
  1169. sock = open_telnet((char *) ip, 113);
  1170. if (hosts)
  1171. putlog(LOG_MISC, "*", "Telnet connection: %s[%s]/%d", dcc[i].host, ip, dcc[i].port);
  1172. else
  1173. putlog(LOG_MISC, "*", "Telnet connection: %s/%d", dcc[i].host, dcc[i].port);
  1174. char s[UHOSTLEN] = "";
  1175. if (sock < 0) {
  1176. if (sock == -2)
  1177. strcpy(s, "DNS lookup failed for ident");
  1178. else
  1179. strcpy(s, strerror(errno));
  1180. } else {
  1181. j = new_dcc(&DCC_IDENT, 0);
  1182. if (j < 0) {
  1183. killsock(sock);
  1184. strcpy(s, "No Free DCC's");
  1185. }
  1186. }
  1187. if (s[0]) {
  1188. putlog(LOG_MISC, "*", DCC_IDENTFAIL, dcc[i].host, s);
  1189. sprintf(s, "telnet@%s", dcc[i].host);
  1190. dcc_telnet_got_ident(i, s);
  1191. return;
  1192. }
  1193. dcc[j].sock = sock;
  1194. dcc[j].port = 113;
  1195. dcc[j].addr = dcc[i].addr;
  1196. strcpy(dcc[j].host, dcc[i].host);
  1197. strcpy(dcc[j].nick, "*");
  1198. dcc[j].u.ident_sock = dcc[i].sock;
  1199. dcc[j].timeval = now;
  1200. dprintf(j, "%d, %d\n", dcc[i].port, dcc[idx].port);
  1201. }
  1202. static void
  1203. eof_dcc_telnet(int idx)
  1204. {
  1205. putlog(LOG_MISC, "*", DCC_PORTDIE, dcc[idx].port);
  1206. killsock(dcc[idx].sock);
  1207. lostdcc(idx);
  1208. }
  1209. static void
  1210. display_telnet(int idx, char *buf)
  1211. {
  1212. sprintf(buf, "lstn %d%s", dcc[idx].port, (dcc[idx].status & LSTN_PUBLIC) ? " pub" : "");
  1213. }
  1214. struct dcc_table DCC_TELNET = {
  1215. "TELNET",
  1216. DCT_LISTEN,
  1217. eof_dcc_telnet,
  1218. dcc_telnet,
  1219. NULL,
  1220. NULL,
  1221. display_telnet,
  1222. NULL,
  1223. NULL,
  1224. NULL
  1225. };
  1226. static void
  1227. eof_dcc_dupwait(int idx)
  1228. {
  1229. putlog(LOG_BOTS, "*", DCC_LOSTDUP, dcc[idx].host);
  1230. killsock(dcc[idx].sock);
  1231. lostdcc(idx);
  1232. }
  1233. static void
  1234. dcc_dupwait(int idx, char *buf, int i)
  1235. {
  1236. /* We just ignore any data at this point. */
  1237. return;
  1238. }
  1239. /* We now check again. If the bot is still marked as duplicate, there is no
  1240. * botnet lag we could push it on, so we just drop the connection.
  1241. */
  1242. static void
  1243. timeout_dupwait(int idx)
  1244. {
  1245. /* Still duplicate? */
  1246. if (in_chain(dcc[idx].nick)) {
  1247. char x[UHOSTLEN] = "";
  1248. egg_snprintf(x, sizeof x, "%s!%s", dcc[idx].nick, dcc[idx].host);
  1249. putlog(LOG_BOTS, "*", DCC_DUPLICATE, x);
  1250. killsock(dcc[idx].sock);
  1251. lostdcc(idx);
  1252. } else {
  1253. /* Ha! Now it's gone and we can grant this bot access. */
  1254. dcc_telnet_pass(idx, dcc[idx].u.dupwait->atr);
  1255. }
  1256. }
  1257. static void
  1258. display_dupwait(int idx, char *buf)
  1259. {
  1260. sprintf(buf, "wait duplicate?");
  1261. }
  1262. static void
  1263. kill_dupwait(int idx, void *x)
  1264. {
  1265. register struct dupwait_info *p = (struct dupwait_info *) x;
  1266. if (p) {
  1267. if (p->chat && DCC_CHAT.kill)
  1268. DCC_CHAT.kill(idx, p->chat);
  1269. free(p);
  1270. }
  1271. }
  1272. struct dcc_table DCC_DUPWAIT = {
  1273. "DUPWAIT",
  1274. DCT_VALIDIDX,
  1275. eof_dcc_dupwait,
  1276. dcc_dupwait,
  1277. &dupwait_timeout,
  1278. timeout_dupwait,
  1279. display_dupwait,
  1280. kill_dupwait,
  1281. NULL,
  1282. NULL
  1283. };
  1284. /* This function is called if a bot gets removed from the list. It checks
  1285. * wether we have a pending duplicate connection for that bot and continues
  1286. * with the login in that case.
  1287. */
  1288. void
  1289. dupwait_notify(const char *who)
  1290. {
  1291. Assert(who);
  1292. for (register int idx = 0; idx < dcc_total; idx++)
  1293. if (dcc[idx].type && (dcc[idx].type == &DCC_DUPWAIT) && !egg_strcasecmp(dcc[idx].nick, who)) {
  1294. dcc_telnet_pass(idx, dcc[idx].u.dupwait->atr);
  1295. break;
  1296. }
  1297. }
  1298. static void
  1299. dcc_telnet_id(int idx, char *buf, int atr)
  1300. {
  1301. strip_telnet(dcc[idx].sock, buf, &atr);
  1302. buf[HANDLEN] = 0;
  1303. /* Toss out bad nicknames */
  1304. if ((dcc[idx].nick[0] != '@') && (!wild_match(dcc[idx].nick, buf))) {
  1305. /* FIXME: if a bot gets this, they will try to decrypt it ;/ */
  1306. dprintf(idx, "Sorry, that nickname format is invalid.\n");
  1307. putlog(LOG_BOTS, "*", DCC_BADNICK, dcc[idx].host);
  1308. killsock(dcc[idx].sock);
  1309. lostdcc(idx);
  1310. return;
  1311. }
  1312. bool ok = 0;
  1313. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  1314. dcc[idx].user = get_user_by_handle(userlist, buf);
  1315. get_user_flagrec(dcc[idx].user, &fr, NULL);
  1316. /* if (!ok && glob_party(fr))
  1317. ok = 1;*/
  1318. ok = 1;
  1319. #ifdef HUB
  1320. if (!glob_huba(fr))
  1321. ok = 0;
  1322. #else /* !HUB */
  1323. /* if I am a chanhub and they dont have +c then drop */
  1324. if (ischanhub() && !glob_chuba(fr))
  1325. ok = 0;
  1326. if (!ischanhub())
  1327. ok = 0;
  1328. #endif /* HUB */
  1329. if (!ok && glob_bot(fr))
  1330. ok = 1;
  1331. if (!ok) {
  1332. putlog(LOG_BOTS, "*", DCC_INVHANDLE, dcc[idx].host, buf);
  1333. killsock(dcc[idx].sock);
  1334. lostdcc(idx);
  1335. return;
  1336. }
  1337. correct_handle(buf);
  1338. strcpy(dcc[idx].nick, buf);
  1339. if (glob_bot(fr)) {
  1340. if (!egg_strcasecmp(conf.bot->nick, dcc[idx].nick)) {
  1341. putlog(LOG_BOTS, "*", DCC_MYBOTNETNICK, dcc[idx].host);
  1342. killsock(dcc[idx].sock);
  1343. lostdcc(idx);
  1344. return;
  1345. } else if (in_chain(dcc[idx].nick)) {
  1346. struct chat_info *ci = dcc[idx].u.chat;
  1347. dcc[idx].type = &DCC_DUPWAIT;
  1348. dcc[idx].u.dupwait = (struct dupwait_info *) calloc(1, sizeof(struct dupwait_info));
  1349. dcc[idx].u.dupwait->chat = ci;
  1350. dcc[idx].u.dupwait->atr = atr;
  1351. return;
  1352. }
  1353. }
  1354. dcc_telnet_pass(idx, atr);
  1355. }
  1356. static void
  1357. dcc_telnet_pass(int idx, int atr)
  1358. {
  1359. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  1360. get_user_flagrec(dcc[idx].user, &fr, NULL);
  1361. /* No password set? */
  1362. if (!glob_bot(fr) && (u_pass_match(dcc[idx].user, "-"))) {
  1363. dprintf(idx, "Can't telnet until you have a password set.\r\n");
  1364. putlog(LOG_MISC, "*", DCC_NOPASS, dcc[idx].nick, dcc[idx].host);
  1365. killsock(dcc[idx].sock);
  1366. lostdcc(idx);
  1367. return;
  1368. }
  1369. bool ok = 0, ok2 = 1;
  1370. if (dcc[idx].type == &DCC_DUPWAIT) {
  1371. struct chat_info *ci = dcc[idx].u.dupwait->chat;
  1372. free(dcc[idx].u.dupwait);
  1373. dcc[idx].u.chat = ci;
  1374. }
  1375. dcc[idx].type = &DCC_CHAT_PASS;
  1376. dcc[idx].timeval = now;
  1377. #ifdef HUB
  1378. if (!glob_huba(fr))
  1379. ok2 = 0;
  1380. #else /* !HUB */
  1381. if (ischanhub() && !glob_chuba(fr))
  1382. ok2 = 0;
  1383. #endif /* HUB */
  1384. if (ok2) {
  1385. ok = 1;
  1386. dcc[idx].status |= STAT_PARTY;
  1387. }
  1388. if (glob_bot(fr))
  1389. ok = 1;
  1390. if (!ok) {
  1391. struct chat_info *ci;
  1392. ci = dcc[idx].u.chat;
  1393. dcc[idx].u.file = (struct file_info *) calloc(1, sizeof(struct file_info));
  1394. dcc[idx].u.file->chat = ci;
  1395. }
  1396. if (glob_bot(fr)) {
  1397. int snum = findanysnum(dcc[idx].sock);
  1398. if (snum >= 0) {
  1399. char initkey[33] = "", *tmp2 = NULL;
  1400. char tmp[256] = "";
  1401. unsigned char buf[SHA_HASH_LENGTH + 1] = "";
  1402. SHA_CTX ctx;
  1403. /* initkey-gen hub */
  1404. /* bdhash port mynick conf.bot->nick */
  1405. sprintf(tmp, "%s@%4x@%s@%s", settings.bdhash, htons(dcc[idx].port), conf.bot->nick, dcc[idx].nick);
  1406. SHA1_Init(&ctx);
  1407. SHA1_Update(&ctx, tmp, strlen(tmp));
  1408. SHA1_Final(buf, &ctx);
  1409. strncpyz(socklist[snum].okey, btoh(buf, SHA_DIGEST_LENGTH), sizeof(socklist[snum].okey));
  1410. putlog(LOG_DEBUG, "@", "Link hash for %s: %s", dcc[idx].nick, tmp);
  1411. putlog(LOG_DEBUG, "@", "outkey (%d): %s", strlen(socklist[snum].okey), socklist[snum].okey);
  1412. make_rand_str(initkey, 32); /* set the initial out/in link key to random chars. */
  1413. socklist[snum].oseed = random();
  1414. socklist[snum].iseed = socklist[snum].oseed;
  1415. tmp2 = encrypt_string(settings.salt2, initkey);
  1416. putlog(LOG_BOTS, "*", "Sending encrypted link handshake to %s...", dcc[idx].nick);
  1417. /* the leaf bot set encstatus right after it sent it's nick, but we just set the key.... */
  1418. socklist[snum].encstatus = 1;
  1419. socklist[snum].gz = 1;
  1420. dprintf(idx, "elink %s %d\n", tmp2, socklist[snum].oseed);
  1421. free(tmp2);
  1422. strcpy(socklist[snum].okey, initkey);
  1423. strcpy(socklist[snum].ikey, initkey);
  1424. } else {
  1425. putlog(LOG_MISC, "*", "Couldn't find socket for %s connection?? Shouldn't happen :/", dcc[idx].nick);
  1426. killsock(dcc[idx].sock);
  1427. lostdcc(idx);
  1428. }
  1429. } else {
  1430. /* Turn off remote telnet echo (send IAC WILL ECHO). */
  1431. #ifdef HUB
  1432. dprintf(idx, "\n%s" TLN_IAC_C TLN_WILL_C TLN_ECHO_C "\n", DCC_ENTERPASS);
  1433. #else /* !HUB */
  1434. dprintf(idx, "%s\n" TLN_IAC_C TLN_WILL_C TLN_ECHO_C, response(RES_PASSWORD));
  1435. #endif /* HUB */
  1436. }
  1437. }
  1438. static void
  1439. eof_dcc_telnet_id(int idx)
  1440. {
  1441. putlog(LOG_MISC, "*", DCC_LOSTCON, dcc[idx].host, dcc[idx].port);
  1442. killsock(dcc[idx].sock);
  1443. lostdcc(idx);
  1444. }
  1445. static void
  1446. timeout_dcc_telnet_id(int idx)
  1447. {
  1448. putlog(LOG_MISC, "*", DCC_TTIMEOUT, dcc[idx].host);
  1449. killsock(dcc[idx].sock);
  1450. lostdcc(idx);
  1451. }
  1452. static void
  1453. display_dcc_telnet_id(int idx, char *buf)
  1454. {
  1455. sprintf(buf, "t-in waited %lis", now - dcc[idx].timeval);
  1456. }
  1457. struct dcc_table DCC_TELNET_ID = {
  1458. "TELNET_ID",
  1459. 0,
  1460. eof_dcc_telnet_id,
  1461. dcc_telnet_id,
  1462. &password_timeout,
  1463. timeout_dcc_telnet_id,
  1464. display_dcc_telnet_id,
  1465. kill_dcc_general,
  1466. out_dcc_general,
  1467. NULL
  1468. };
  1469. static void
  1470. dcc_socket(int idx, char *buf, int len)
  1471. {
  1472. }
  1473. static void
  1474. eof_dcc_socket(int idx)
  1475. {
  1476. killsock(dcc[idx].sock);
  1477. lostdcc(idx);
  1478. }
  1479. static void
  1480. display_dcc_socket(int idx, char *buf)
  1481. {
  1482. strcpy(buf, "sock (stranded)");
  1483. }
  1484. struct dcc_table DCC_SOCKET = {
  1485. "SOCKET",
  1486. DCT_VALIDIDX,
  1487. eof_dcc_socket,
  1488. dcc_socket,
  1489. NULL,
  1490. NULL,
  1491. display_dcc_socket,
  1492. NULL,
  1493. NULL,
  1494. NULL
  1495. };
  1496. void
  1497. dcc_identwait(int idx, char *buf, int len)
  1498. {
  1499. /* Ignore anything now */
  1500. }
  1501. void
  1502. eof_dcc_identwait(int idx)
  1503. {
  1504. putlog(LOG_MISC, "*", DCC_LOSTCONN, dcc[idx].host, dcc[idx].port);
  1505. for (int i = 0; i < dcc_total; i++)
  1506. if (dcc[i].type && (dcc[i].type == &DCC_IDENT) && (dcc[i].u.ident_sock == dcc[idx].sock)) {
  1507. killsock(dcc[i].sock); /* Cleanup ident socket */
  1508. dcc[i].u.other = 0;
  1509. lostdcc(i);
  1510. break;
  1511. }
  1512. killsock(dcc[idx].sock); /* Cleanup waiting socket */
  1513. dcc[idx].u.other = 0;
  1514. lostdcc(idx);
  1515. }
  1516. static void
  1517. display_dcc_identwait(int idx, char *buf)
  1518. {
  1519. sprintf(buf, "idtw waited %lis", now - dcc[idx].timeval);
  1520. }
  1521. struct dcc_table DCC_IDENTWAIT = {
  1522. "IDENTWAIT",
  1523. 0,
  1524. eof_dcc_identwait,
  1525. dcc_identwait,
  1526. NULL,
  1527. NULL,
  1528. display_dcc_identwait,
  1529. NULL,
  1530. NULL,
  1531. NULL
  1532. };
  1533. void
  1534. dcc_ident(int idx, char *buf, int len)
  1535. {
  1536. char ident_response[512] = "", uid[512] = "", buf1[UHOSTLEN] = "";
  1537. sscanf(buf, "%*[^:]:%[^:]:%*[^:]:%[^\n]\n", ident_response, uid);
  1538. rmspace(ident_response);
  1539. if (ident_response[0] != 'U') {
  1540. dcc[idx].timeval = now;
  1541. return;
  1542. }
  1543. rmspace(uid);
  1544. uid[20] = 0; /* 20 character ident max */
  1545. for (int i = 0; i < dcc_total; i++)
  1546. if (dcc[i].type && (dcc[i].type == &DCC_IDENTWAIT) && (dcc[i].sock == dcc[idx].u.ident_sock)) {
  1547. simple_sprintf(buf1, "%s@%s", uid, dcc[idx].host);
  1548. dcc_telnet_got_ident(i, buf1);
  1549. }
  1550. dcc[idx].u.other = 0;
  1551. killsock(dcc[idx].sock);
  1552. lostdcc(idx);
  1553. }
  1554. void
  1555. eof_dcc_ident(int idx)
  1556. {
  1557. char buf[UHOSTLEN] = "";
  1558. for (int i = 0; i < dcc_total; i++)
  1559. if (dcc[i].type && (dcc[i].type == &DCC_IDENTWAIT) && (dcc[i].sock == dcc[idx].u.ident_sock)) {
  1560. putlog(LOG_MISC, "*", DCC_EOFIDENT);
  1561. simple_sprintf(buf, "telnet@%s", dcc[idx].host);
  1562. dcc_telnet_got_ident(i, buf);
  1563. }
  1564. killsock(dcc[idx].sock);
  1565. dcc[idx].u.other = 0;
  1566. lostdcc(idx);
  1567. }
  1568. static void
  1569. display_dcc_ident(int idx, char *buf)
  1570. {
  1571. sprintf(buf, "idnt (sock %d)", dcc[idx].u.ident_sock);
  1572. }
  1573. struct dcc_table DCC_IDENT = {
  1574. "IDENT",
  1575. 0,
  1576. eof_dcc_ident,
  1577. dcc_ident,
  1578. &identtimeout,
  1579. eof_dcc_ident,
  1580. display_dcc_ident,
  1581. NULL,
  1582. NULL,
  1583. NULL
  1584. };
  1585. static void
  1586. dcc_telnet_got_ident(int i, char *host)
  1587. {
  1588. int idx;
  1589. for (idx = 0; idx < dcc_total; idx++)
  1590. if (dcc[i].type && (dcc[idx].type == &DCC_TELNET) && (dcc[idx].sock == dcc[i].u.ident_sock))
  1591. break;
  1592. dcc[i].u.other = 0;
  1593. if (!dcc[idx].type) {
  1594. putlog(LOG_MISC, "*", DCC_LOSTIDENT);
  1595. killsock(dcc[i].sock);
  1596. lostdcc(i);
  1597. return;
  1598. }
  1599. char x[1024] = "";
  1600. strncpyz(dcc[i].host, host, UHOSTLEN);
  1601. egg_snprintf(x, sizeof x, "-telnet!%s", dcc[i].host);
  1602. if (match_ignore(x)) {
  1603. killsock(dcc[i].sock);
  1604. lostdcc(i);
  1605. return;
  1606. }
  1607. if (protect_telnet) {
  1608. struct userrec *u = get_user_by_host(x);
  1609. bool ok = 1;
  1610. /* Not a user or +p & require p OR +o */
  1611. if (!u)
  1612. ok = 0;
  1613. #ifdef HUB
  1614. if (ok && !(u->flags & USER_HUBA))
  1615. ok = 0;
  1616. #else /* !HUB */
  1617. /* if I am a chanhub and they dont have +c then drop */
  1618. if (ok && (ischanhub() && !(u->flags & USER_CHUBA)))
  1619. ok = 0;
  1620. #endif /* HUB */
  1621. /* else if (!(u->flags & USER_PARTY))
  1622. ok = 0; */
  1623. if (!ok && u && u->bot)
  1624. ok = 1;
  1625. if (!ok && (dcc[idx].status & LSTN_PUBLIC))
  1626. ok = 1;
  1627. if (!ok) {
  1628. putlog(LOG_MISC, "*", DCC_NOACCESS, dcc[i].host);
  1629. killsock(dcc[i].sock);
  1630. lostdcc(i);
  1631. return;
  1632. }
  1633. }
  1634. /* Do not buffer data anymore. All received and stored data is passed
  1635. * over to the dcc functions from now on. */
  1636. sockoptions(dcc[i].sock, EGG_OPTION_UNSET, SOCK_BUFFER);
  1637. dcc[i].type = &DCC_TELNET_ID;
  1638. dcc[i].u.chat = (struct chat_info *) calloc(1, sizeof(struct chat_info));
  1639. egg_bzero(dcc[i].u.chat, sizeof(struct chat_info));
  1640. /* Copy acceptable-nick/host mask */
  1641. dcc[i].status = (STAT_TELNET | STAT_ECHO | STAT_COLOR | STAT_BANNER | STAT_CHANNELS | STAT_BOTS | STAT_WHOM);
  1642. /* Copy acceptable-nick/host mask */
  1643. strncpyz(dcc[i].nick, dcc[idx].host, HANDLEN);
  1644. dcc[i].timeval = now;
  1645. strcpy(dcc[i].u.chat->con_chan, chanset ? chanset->dname : "*");
  1646. /* This is so we dont tell someone doing a portscan anything
  1647. * about ourselves. <cybah>
  1648. */
  1649. /* n ssl_link(dcc[i].sock, ACCEPT_SSL); */
  1650. #ifdef HUB
  1651. dprintf(i, "\n");
  1652. #else /* !HUB */
  1653. dprintf(i, "%s\n", response(RES_USERNAME));
  1654. #endif /* HUB */
  1655. }