dcc.c 57 KB

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