dcc.c 58 KB

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