dcc.c 58 KB

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