dcc.c 47 KB

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