dcc.c 49 KB

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