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