dcc.c 49 KB

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