dcc.cc 59 KB

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