dcc.c 53 KB

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