dcc.c 51 KB

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