dcc.cc 59 KB

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