dcc.c 52 KB

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