dcc.c 47 KB

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