dcc.c 51 KB

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