dcc.c 49 KB

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