dcc.c 50 KB

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