dcc.c 49 KB

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