dcc.c 58 KB

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