dcc.c 59 KB

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