dcc.c 48 KB

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