dcc.c 48 KB

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