dcc.c 48 KB

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