dcc.cc 59 KB

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