dcc.c 48 KB

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