dcc.c 51 KB

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