dcc.c 47 KB

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