dcc.c 49 KB

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