dcc.c 47 KB

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