dcc.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  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 (Bot is not +o)", dcc[idx].nick);
  208. dprintf(idx, "error You are being rejected.\n");
  209. } else if (conf.bot->localhub && !have_linked_to_hub) {
  210. putlog(LOG_BOTS, "*", "Delaying link from %s", dcc[idx].nick);
  211. dprintf(idx, "error Delaying your link until I get userfile.\n");
  212. }
  213. dprintf(idx, "bye\n");
  214. killsock(dcc[idx].sock);
  215. lostdcc(idx);
  216. return;
  217. }
  218. if (bot_hublevel(dcc[idx].user) == 999)
  219. dcc[idx].status |= STAT_LEAF;
  220. dcc[idx].status |= STAT_LINKING;
  221. dprintf(idx, "v 1001500 9 Wraith %s <%s> %d %li %s %s\n", egg_version, "-", conf.bot->localhub, (long)buildts, commit, egg_version);
  222. for (int i = 0; i < dcc_total; i++) {
  223. if (dcc[i].type && dcc[i].type == &DCC_FORK_BOT) {
  224. killsock(dcc[i].sock);
  225. lostdcc(i);
  226. }
  227. }
  228. }
  229. static void
  230. bot_version(int idx, char *par)
  231. {
  232. dcc[idx].timeval = now;
  233. if (in_chain(dcc[idx].nick)) {
  234. dprintf(idx, "error Sorry, already connected.\n");
  235. dprintf(idx, "bye\n");
  236. killsock(dcc[idx].sock);
  237. lostdcc(idx);
  238. return;
  239. }
  240. if ((par[0] >= '0') && (par[0] <= '9')) {
  241. char *work = NULL;
  242. work = newsplit(&par);
  243. dcc[idx].u.bot->numver = atoi(work);
  244. /* old numver crap */
  245. } else
  246. dcc[idx].u.bot->numver = 0;
  247. dprintf(idx, "tb %s\n", conf.bot->nick);
  248. newsplit(&par); //Ignore handlen
  249. #ifdef no
  250. size_t l = atol(newsplit(&par));
  251. if (l != HANDLEN) {
  252. putlog(LOG_BOTS, "*", "Non-matching handle lengths with %s, they use %zu characters.", dcc[idx].nick, l);
  253. dprintf(idx, "error Non-matching handle length: mine %d, yours %zu\n", HANDLEN, l);
  254. dprintf(idx, "bye %s\n", "bad handlen");
  255. killsock(dcc[idx].sock);
  256. lostdcc(idx);
  257. return;
  258. }
  259. #endif
  260. char x[1024] = "", *vversion = NULL, *vcommit = NULL;
  261. int vlocalhub = 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_share(idx, x);
  304. dprintf(idx, "el\n");
  305. }
  306. void
  307. failed_link(int idx)
  308. {
  309. char nick[NICKLEN] = "", s1[NICKLEN + 17 + 1] = "";
  310. if (dcc[idx].u.bot->linker[0]) {
  311. simple_snprintf(s1, sizeof s1, "Couldn't link to %s.", dcc[idx].nick);
  312. strlcpy(nick, dcc[idx].u.bot->linker, sizeof(s1));
  313. add_note(nick, conf.bot->nick, s1, -2, 0);
  314. }
  315. if (dcc[idx].u.bot->numver >= (-1))
  316. putlog(LOG_BOTS, "*", "Failed link to %s.", dcc[idx].nick);
  317. if (dcc[idx].sock != -1) {
  318. killsock(dcc[idx].sock);
  319. dcc[idx].sock = -1;
  320. }
  321. strlcpy(nick, dcc[idx].nick, sizeof(nick));
  322. lostdcc(idx);
  323. if (conf.bot->hub || conf.bot->localhub)
  324. strlcpy(autolink_failed, nick, HANDLEN + 1);
  325. }
  326. static void
  327. cont_link(int idx, char *buf, int ii)
  328. {
  329. /* If we're already connected somewhere, unlink and idle a sec */
  330. for (int i = 0; i < dcc_total; i++) {
  331. if (unlikely(dcc[i].type && (dcc[i].type == &DCC_BOT) && (!bot_aggressive_to(dcc[i].user)))) {
  332. putlog(LOG_BOTS, "*", "Unlinking %s - restructure", dcc[i].nick);
  333. botnet_send_unlinked(i, dcc[i].nick, "Restructure");
  334. killsock(dcc[i].sock);
  335. lostdcc(i);
  336. usleep(1000 * 500);
  337. break;
  338. }
  339. }
  340. dcc[idx].type = &DCC_BOT_NEW;
  341. dcc[idx].u.bot->numver = 0;
  342. if (ii == 3)
  343. dprintf(idx, STR("-%s\n"), conf.bot->nick);
  344. /* wait for "neg?" now */
  345. /* now we wait to negotiate an encryption */
  346. return;
  347. }
  348. static void
  349. dcc_bot_new(int idx, char *buf, int x)
  350. {
  351. /* struct userrec *u = get_user_by_handle(userlist, dcc[idx].nick); */
  352. char *code = NULL;
  353. strip_telnet(dcc[idx].sock, buf, &x);
  354. code = newsplit(&buf);
  355. if (!strcasecmp(code, "goodbye!")) {
  356. greet_new_bot(idx);
  357. } else if (!strcasecmp(code, "v")) {
  358. bot_version(idx, buf);
  359. } else if (!strcasecmp(code, STR("neg!"))) { /* something to parse in enclink.c */
  360. link_parse(idx, buf);
  361. } else if (!strcasecmp(code, STR("neg?"))) { /* we're connecting to THEM */
  362. link_challenge_to(idx, buf);
  363. } else if (!strcasecmp(code, "error")) {
  364. putlog(LOG_MISC, "*", "ERROR linking %s: %s", dcc[idx].nick, buf);
  365. killsock(dcc[idx].sock);
  366. lostdcc(idx);
  367. } else if (strcmp(code, "")) {
  368. /* Invalid password/digest on leaf */
  369. putlog(LOG_WARN, "*", STR("%s failed encrypted link handshake"), dcc[idx].nick);
  370. killsock(dcc[idx].sock);
  371. lostdcc(idx);
  372. }
  373. /* Ignore otherwise */
  374. }
  375. static void
  376. eof_dcc_bot_new(int idx)
  377. {
  378. putlog(LOG_BOTS, "*", "Lost Bot: %s", dcc[idx].nick);
  379. killsock(dcc[idx].sock);
  380. lostdcc(idx);
  381. }
  382. static void
  383. timeout_dcc_bot_new(int idx)
  384. {
  385. if (conf.bot->hub)
  386. putlog(LOG_BOTS, "*", "Timeout: bot link to %s at %s:%d", dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  387. else
  388. putlog(LOG_BOTS, "*", "Timeout: bot link to %s", dcc[idx].nick);
  389. killsock(dcc[idx].sock);
  390. lostdcc(idx);
  391. }
  392. static void
  393. display_dcc_bot_new(int idx, char *buf, size_t bufsiz)
  394. {
  395. simple_snprintf(buf, bufsiz, "bot* waited %ds", (int) (now - dcc[idx].timeval));
  396. }
  397. static void
  398. free_dcc_bot_(int n, void *x)
  399. {
  400. if (dcc[n].type == &DCC_BOT) {
  401. unvia(n, findbot(dcc[n].nick));
  402. rembot(dcc[n].nick);
  403. }
  404. free(x);
  405. }
  406. struct dcc_table DCC_BOT_NEW = {
  407. "BOT_NEW",
  408. 0,
  409. eof_dcc_bot_new,
  410. dcc_bot_new,
  411. &bot_timeout,
  412. timeout_dcc_bot_new,
  413. display_dcc_bot_new,
  414. free_dcc_bot_,
  415. NULL,
  416. NULL
  417. };
  418. static void
  419. dcc_bot(int idx, char *code, int i)
  420. {
  421. char *msg = NULL;
  422. strip_telnet(dcc[idx].sock, code, &i);
  423. if (debug_output) {
  424. /* if (code[0] != 'z' && code[1] != 'b' && code[2] != ' ') { */
  425. if (code[0] == 's')
  426. putlog(LOG_BOTSHARE, "@", "{%s} %s", dcc[idx].nick, code);
  427. else
  428. putlog(LOG_BOTNET, "@", "<-[%s] %s", dcc[idx].nick, code);
  429. /* } */
  430. }
  431. msg = strchr(code, ' ');
  432. if (msg) {
  433. *msg = 0;
  434. msg++;
  435. } else
  436. msg = "";
  437. parse_botcmd(idx, code, msg);
  438. }
  439. static void
  440. eof_dcc_bot(int idx)
  441. {
  442. char x[1024] = "";
  443. int bots, users;
  444. bots = bots_in_subtree(findbot(dcc[idx].nick));
  445. users = users_in_subtree(findbot(dcc[idx].nick));
  446. simple_snprintf(x, sizeof x,
  447. "Lost bot: %s (lost %d bot%s and %d user%s)",
  448. dcc[idx].nick, bots, (bots != 1) ? "s" : "", users, (users != 1) ? "s" : "");
  449. putlog(LOG_BOTS, "*", "%s.", x);
  450. chatout("*** %s\n", x);
  451. botnet_send_unlinked(idx, dcc[idx].nick, x);
  452. killsock(dcc[idx].sock);
  453. lostdcc(idx);
  454. }
  455. static void
  456. display_dcc_bot(int idx, char *buf, size_t bufsiz)
  457. {
  458. size_t i = simple_snprintf(buf, bufsiz, "bot flags: ");
  459. buf[i++] = b_status(idx) & STAT_PINGED ? 'P' : 'p';
  460. buf[i++] = b_status(idx) & STAT_SHARE ? 'U' : 'u';
  461. buf[i++] = b_status(idx) & STAT_CALLED ? 'C' : 'c';
  462. buf[i++] = b_status(idx) & STAT_OFFERED ? 'O' : 'o';
  463. buf[i++] = b_status(idx) & STAT_SENDING ? 'S' : 's';
  464. buf[i++] = b_status(idx) & STAT_GETTING ? 'G' : 'g';
  465. buf[i++] = b_status(idx) & STAT_WARNED ? 'W' : 'w';
  466. buf[i++] = b_status(idx) & STAT_LEAF ? 'L' : 'l';
  467. buf[i++] = b_status(idx) & STAT_LINKING ? 'I' : 'i';
  468. buf[i++] = b_status(idx) & STAT_AGGRESSIVE ? 'a' : 'A';
  469. buf[i++] = b_status(idx) & STAT_OFFEREDU ? 'B' : 'b';
  470. buf[i++] = b_status(idx) & STAT_SENDINGU ? 'D' : 'd';
  471. buf[i++] = b_status(idx) & STAT_GETTINGU ? 'E' : 'e';
  472. buf[i++] = b_status(idx) & STAT_UNIXDOMAIN ? 'Z' : 'z';
  473. #ifdef USE_IPV6
  474. if (sockprotocol(dcc[idx].sock) == AF_INET6 && dcc[idx].host6[0])
  475. buf[i++] = '6';
  476. #endif /* USE_IPV6 */
  477. buf[i++] = 0;
  478. }
  479. static void
  480. display_dcc_fork_bot(int idx, char *buf, size_t bufsiz)
  481. {
  482. simple_snprintf(buf, bufsiz, "conn bot");
  483. }
  484. struct dcc_table DCC_BOT = {
  485. "BOT",
  486. DCT_BOT,
  487. eof_dcc_bot,
  488. dcc_bot,
  489. NULL,
  490. NULL,
  491. display_dcc_bot,
  492. free_dcc_bot_,
  493. NULL,
  494. NULL
  495. };
  496. struct dcc_table DCC_FORK_BOT = {
  497. "FORK_BOT",
  498. 0,
  499. failed_link,
  500. cont_link,
  501. &connect_timeout,
  502. failed_link,
  503. display_dcc_fork_bot,
  504. free_dcc_bot_,
  505. NULL,
  506. NULL
  507. };
  508. static void
  509. dcc_identd(int idx, char *buf, int atr)
  510. {
  511. char outbuf[1024] = "";
  512. size_t len = simple_snprintf(outbuf, sizeof outbuf, "%s : USERID : UNIX : %s\n", buf, botuser);
  513. tputs(dcc[idx].sock, outbuf, len);
  514. /* just close it, functions neededing it will open it. */
  515. identd_close();
  516. }
  517. static void
  518. eof_dcc_identd(int idx)
  519. {
  520. /* dont bother logging it, who gives a fuck */
  521. killsock(dcc[idx].sock);
  522. lostdcc(idx);
  523. }
  524. static void
  525. display_dcc_identd(int idx, char *buf, size_t bufsiz)
  526. {
  527. simple_snprintf(buf, bufsiz, "idtd %d%s", dcc[idx].port, (dcc[idx].status & LSTN_PUBLIC) ? " pub" : "");
  528. }
  529. struct dcc_table DCC_IDENTD = {
  530. "IDENTD",
  531. DCT_LISTEN,
  532. eof_dcc_identd,
  533. dcc_identd,
  534. NULL,
  535. NULL,
  536. display_dcc_identd,
  537. NULL,
  538. NULL,
  539. NULL
  540. };
  541. static void
  542. dcc_identd_connect(int idx, char *buf, int atr)
  543. {
  544. in_addr_t ip;
  545. in_port_t port;
  546. int j, sock;
  547. char s[UHOSTLEN + 1] = "";
  548. if (dcc_total + 1 > max_dcc) {
  549. j = answer(dcc[idx].sock, s, &ip, &port, 0);
  550. if (j != -1)
  551. killsock(j);
  552. return;
  553. }
  554. sock = answer(dcc[idx].sock, s, &ip, &port, 0);
  555. while ((sock == -1) && (errno == EAGAIN))
  556. sock = answer(sock, s, &ip, &port, 0);
  557. if (sock < 0) {
  558. putlog(LOG_MISC, "*", "Failed TELNET incoming (%s)", strerror(errno));
  559. return;
  560. }
  561. /* changeover_dcc(idx, &DCC_IDENTD, 0); */
  562. j = new_dcc(&DCC_IDENTD, 0);
  563. dcc[j].sock = sock;
  564. dcc[j].port = port;
  565. dcc[j].addr = dcc[idx].addr;
  566. strlcpy(dcc[j].host, dcc[idx].host, sizeof(dcc[j].host));
  567. strlcpy(dcc[j].nick, "*", sizeof(dcc[j].nick));
  568. /* dcc[j].uint.ident_sock = dcc[idx].sock; */
  569. dcc[j].timeval = now;
  570. }
  571. struct dcc_table DCC_IDENTD_CONNECT = {
  572. "IDENTD",
  573. DCT_LISTEN,
  574. eof_dcc_identd,
  575. dcc_identd_connect,
  576. NULL,
  577. NULL,
  578. display_dcc_identd,
  579. NULL,
  580. NULL,
  581. NULL
  582. };
  583. char s1_13[3] = "",s1_15[3] = "",s1_3[3] = "";
  584. static void
  585. dcc_chat_secpass(int idx, char *buf, int atr)
  586. {
  587. int badauth = 0;
  588. if (!atr)
  589. return;
  590. strip_telnet(dcc[idx].sock, buf, &atr);
  591. atr = dcc[idx].user ? dcc[idx].user->flags : 0;
  592. if (dccauth) {
  593. char check[MD5_HASH_LENGTH + 7] = "";
  594. simple_snprintf(check, sizeof check, STR("+Auth %s"), dcc[idx].hash);
  595. badauth = strcmp(check, buf);
  596. /* +secpass */
  597. }
  598. /* Correct pass or secpass! */
  599. if (!dcc[idx].wrong_pass && (!dccauth || (dccauth && !badauth))) {
  600. putlog(LOG_MISC, "*", "Logged in: %s (%s/%d)", dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  601. if (dcc[idx].u.chat->away) {
  602. free(dcc[idx].u.chat->away);
  603. dcc[idx].u.chat->away = NULL;
  604. }
  605. dcc[idx].type = &DCC_CHAT;
  606. dcc[idx].status &= ~STAT_CHAT;
  607. dcc[idx].u.chat->channel = -2;
  608. /* Turn echo back on for telnet sessions (send IAC WON'T ECHO). */
  609. if (dcc[idx].status & STAT_TELNET)
  610. dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  611. stats_add(dcc[idx].user, 1, 0);
  612. bool writeuserfile = 0;
  613. if (!get_user(&USERENTRY_SECPASS, dcc[idx].user)) { /* this should check how many logins instead */
  614. char pass[MAXPASSLEN + 1] = "";
  615. writeuserfile = 1;
  616. dprintf(idx, "********************************************************************\n \n \n");
  617. dprintf(idx, "%sWARNING: YOU DO NOT HAVE A SECPASS SET, NOW SETTING A RANDOM ONE....%s\n", FLASH(-1), FLASH_END(-1));
  618. make_rand_str(pass, MAXPASSLEN);
  619. set_user(&USERENTRY_SECPASS, dcc[idx].user, pass);
  620. dprintf(idx, "Your secpass is now: %s%s%s\n", pass, BOLD(-1), BOLD_END(-1));
  621. dprintf(idx, "Make sure you do not lose this, as it may be needed in the future.\n \n");
  622. dprintf(idx, "********************************************************************\n");
  623. }
  624. if (!get_user(&USERENTRY_CONSOLE, dcc[idx].user)) { /* No console, force them to have at least +mcobseuw */
  625. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  626. get_user_flagrec(dcc[idx].user, &fr, NULL);
  627. strlcpy(dcc[idx].u.chat->con_chan, "*", 2);
  628. dcc[idx].u.chat->con_flags = LOG_MSGS|LOG_SERV;
  629. if (glob_owner(fr))
  630. dcc[idx].u.chat->con_flags |= LOG_ERRORS|LOG_WARN;
  631. if (glob_master(fr))
  632. dcc[idx].u.chat->con_flags |= LOG_BOTS|LOG_CMDS|LOG_MISC|LOG_WALL;
  633. /* Avoid rewriting it later */
  634. writeuserfile = 0;
  635. console_dostore(idx);
  636. }
  637. if (writeuserfile && conf.bot->hub)
  638. write_userfile(idx);
  639. dcc_chatter(idx);
  640. } else if ((dccauth && badauth) || dcc[idx].wrong_pass) { /* bad auth */
  641. dprintf(idx, "%s\n", response(RES_BADUSERPASS));
  642. putlog(LOG_MISC, "*", "Bad Auth: [%s]%s/%d", dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  643. if (dcc[idx].u.chat->away) { /* su from a dumb user */
  644. /* Turn echo back on for telnet sessions (send IAC WON'T ECHO). */
  645. if (dcc[idx].status & STAT_TELNET)
  646. dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  647. dcc[idx].user = get_user_by_handle(userlist, dcc[idx].u.chat->away);
  648. strlcpy(dcc[idx].nick, dcc[idx].u.chat->away, sizeof(dcc[idx].nick));
  649. free(dcc[idx].u.chat->away);
  650. free(dcc[idx].u.chat->su_nick);
  651. dcc[idx].u.chat->away = NULL;
  652. dcc[idx].u.chat->su_nick = NULL;
  653. dcc[idx].type = &DCC_CHAT;
  654. dcc[idx].u.chat->channel = dcc[idx].u.chat->su_channel;
  655. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  656. botnet_send_join_idx(idx);
  657. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s has joined the party line.\n", dcc[idx].nick);
  658. } else {
  659. killsock(dcc[idx].sock);
  660. lostdcc(idx);
  661. }
  662. }
  663. }
  664. static void
  665. eof_dcc_general(int idx)
  666. {
  667. putlog(LOG_MISC, "*", "Lost dcc connection to %s (%s/%d)", dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  668. killsock(dcc[idx].sock);
  669. lostdcc(idx);
  670. }
  671. static void
  672. tout_dcc_chat_secpass(int idx)
  673. {
  674. putlog(LOG_MISC, "*", "Auth timeout on dcc chat: [%s]%s", dcc[idx].nick, dcc[idx].host);
  675. killsock(dcc[idx].sock);
  676. lostdcc(idx);
  677. }
  678. static void
  679. display_dcc_chat_secpass(int idx, char *buf, size_t bufsiz)
  680. {
  681. simple_snprintf(buf, bufsiz, "secpass waited %ds", (int) (now - dcc[idx].timeval));
  682. }
  683. static void
  684. tout_dcc_chat_pass(int idx)
  685. {
  686. putlog(LOG_MISC, "*", "Password timeout on dcc chat: [%s]%s", dcc[idx].nick, dcc[idx].host);
  687. killsock(dcc[idx].sock);
  688. lostdcc(idx);
  689. }
  690. static void
  691. display_dcc_chat_pass(int idx, char *buf, size_t bufsiz)
  692. {
  693. simple_snprintf(buf, bufsiz, "pass waited %ds", (int) (now - dcc[idx].timeval));
  694. }
  695. static void
  696. kill_dcc_general(int idx, void *x)
  697. {
  698. register struct chat_info *p = (struct chat_info *) x;
  699. if (p) {
  700. if (p->buffer) {
  701. struct msgq *r = NULL, *q = NULL;
  702. for (r = dcc[idx].u.chat->buffer; r; r = q) {
  703. q = r->next;
  704. free(r->msg);
  705. free(r);
  706. }
  707. }
  708. if (p->away) {
  709. free(p->away);
  710. }
  711. free(p);
  712. }
  713. }
  714. /* Remove the color control codes that mIRC,pIRCh etc use to make
  715. * their client seem so fecking cool! (Sorry, Khaled, you are a nice
  716. * guy, but when you added this feature you forced people to either
  717. * use your *SHAREWARE* client or face screenfulls of crap!)
  718. */
  719. static void
  720. strip_mirc_codes(int flags, char *text)
  721. {
  722. char *dd = text;
  723. while (*text) {
  724. switch (*text) {
  725. case 2: /* Bold text */
  726. if (flags & STRIP_BOLD) {
  727. text++;
  728. continue;
  729. }
  730. break;
  731. case 3: /* mIRC colors? */
  732. if (flags & STRIP_COLOR) {
  733. if (egg_isdigit(text[1])) { /* Is the first char a number? */
  734. text += 2; /* Skip over the ^C and the first digit */
  735. if (egg_isdigit(*text))
  736. text++; /* Is this a double digit number? */
  737. if (*text == ',') { /* Do we have a background color next? */
  738. if (egg_isdigit(text[1]))
  739. text += 2; /* Skip over the first background digit */
  740. if (egg_isdigit(*text))
  741. text++; /* Is it a double digit? */
  742. }
  743. } else
  744. text++;
  745. continue;
  746. }
  747. break;
  748. case 7:
  749. if (flags & STRIP_BELLS) {
  750. text++;
  751. continue;
  752. }
  753. break;
  754. case 0x16: /* Reverse video */
  755. if (flags & STRIP_REV) {
  756. text++;
  757. continue;
  758. }
  759. break;
  760. case 0x1f: /* Underlined text */
  761. if (flags & STRIP_UNDER) {
  762. text++;
  763. continue;
  764. }
  765. break;
  766. case 033:
  767. if (flags & STRIP_ANSI) {
  768. text++;
  769. if (*text == '[') {
  770. text++;
  771. while ((*text == ';') || egg_isdigit(*text))
  772. text++;
  773. if (*text)
  774. text++; /* also kill the following char */
  775. }
  776. continue;
  777. }
  778. break;
  779. }
  780. *dd++ = *text++; /* Move on to the next char */
  781. }
  782. *dd = 0;
  783. }
  784. static void
  785. append_line(int idx, char *line)
  786. {
  787. struct msgq *p = NULL, *q = NULL;
  788. struct chat_info *c = dcc[idx].u.chat;
  789. if (c->current_lines > 1000) {
  790. /* They're probably trying to fill up the bot nuke the sods :) */
  791. for (p = c->buffer; p; p = q) {
  792. q = p->next;
  793. free(p->msg);
  794. free(p);
  795. }
  796. c->buffer = NULL;
  797. c->current_lines = 0;
  798. dcc[idx].status &= ~STAT_PAGE;
  799. do_boot(idx, conf.bot->nick, "too many pages - senq full");
  800. return;
  801. }
  802. size_t l = strlen(line);
  803. if ((c->line_count < c->max_line) && (c->buffer == NULL)) {
  804. ++c->line_count;
  805. tputs(dcc[idx].sock, line, l);
  806. } else {
  807. ++c->current_lines;
  808. p = (struct msgq *) my_calloc(1, sizeof(struct msgq));
  809. p->len = l;
  810. p->msg = (char *) my_calloc(1, l + 1);
  811. p->next = NULL;
  812. strlcpy(p->msg, line, l + 1);
  813. if (c->buffer == NULL)
  814. c->buffer = p;
  815. else {
  816. for (q = c->buffer; q->next; q = q->next) ;
  817. q->next = p;
  818. }
  819. }
  820. }
  821. static void
  822. out_dcc_general(int idx, char *buf, void *x)
  823. {
  824. char *y = buf;
  825. if (dcc[idx].type == &DCC_CHAT) {
  826. register struct chat_info *p = (struct chat_info *) x;
  827. strip_mirc_codes(p->strip_flags, buf);
  828. }
  829. if (dcc[idx].status & STAT_TELNET)
  830. y = add_cr(buf);
  831. if (!dcc[idx].bot && dcc[idx].status & STAT_PAGE)
  832. append_line(idx, y);
  833. else
  834. tputs(dcc[idx].sock, y, strlen(y));
  835. }
  836. static struct dcc_table DCC_CHAT_SECPASS = {
  837. "CHAT_SECPASS",
  838. 0,
  839. eof_dcc_general,
  840. dcc_chat_secpass,
  841. &auth_timeout,
  842. tout_dcc_chat_secpass,
  843. display_dcc_chat_secpass,
  844. kill_dcc_general,
  845. out_dcc_general,
  846. NULL
  847. };
  848. //su drops us here
  849. static void
  850. dcc_chat_pass(int idx, char *buf, int atr)
  851. {
  852. if (!atr)
  853. return;
  854. char *pass = NULL;
  855. strip_telnet(dcc[idx].sock, buf, &atr);
  856. atr = dcc[idx].user ? dcc[idx].user->flags : 0;
  857. pass = newsplit(&buf);
  858. if (dcc[idx].encrypt == 1) {
  859. if (!strcasecmp(pass, STR("neg!"))) { /* we're the hub */
  860. link_parse(idx, buf);
  861. } else if (!strcasecmp(pass, STR("neg."))) { /* we're done, link up! */
  862. dcc[idx].encrypt = 2;
  863. if (dcc[idx].bot) {
  864. dcc[idx].type = &DCC_BOT_NEW;
  865. dcc[idx].u.bot = (struct bot_info *) my_calloc(1, sizeof(struct bot_info));
  866. if (dcc[idx].status & STAT_UNIXDOMAIN)
  867. dcc[idx].status = STAT_UNIXDOMAIN|STAT_CALLED;
  868. else
  869. dcc[idx].status = STAT_CALLED;
  870. dprintf(idx, "goodbye!\n");
  871. greet_new_bot(idx);
  872. if (conf.bot->hub || conf.bot->localhub)
  873. send_timesync(idx);
  874. } else {
  875. // User encrypted over relay
  876. /* Turn off remote telnet echo (send IAC WILL ECHO). */
  877. dprintf(idx, "\n%s" TLN_IAC_C TLN_WILL_C TLN_ECHO_C "\n", "Enter your password");
  878. }
  879. } else if (!strcasecmp(pass, STR("neg"))) {
  880. //link_challenge_from()
  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. sdprintf(STR("Using '%s' (%d/%d) for link with %s"), enclink[i].name, enclink[i].type, i, dcc[idx].nick);
  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. in_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 (port < 1024 || port > 65534) {
  1260. putlog(LOG_BOTS, "*", "Refused %s/%d (bad src port)", s, port);
  1261. killsock(sock);
  1262. return;
  1263. }
  1264. putlog(LOG_DEBUG, "*", "Telnet connection: %s/%d", s, port);
  1265. // Are they ignored by IP?
  1266. simple_snprintf(x, sizeof(x), "-telnet!telnet@%s", iptostr(htonl(ip)));
  1267. if (unlikely(match_ignore(x) || detect_telnet_flood(x))) {
  1268. putlog(LOG_DEBUG, "*", "Ignored telnet connection from: %s", x);
  1269. killsock(sock);
  1270. return;
  1271. }
  1272. /* If a matching bot ip is found, it might still be on the ignore list as a host,
  1273. * so we'll just reverse the ip anyway and check the ignores before
  1274. * proceeding with user matching
  1275. */
  1276. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1277. dcc[i].addr = ip;
  1278. dcc[i].sock = sock;
  1279. dcc[i].user = get_user_by_host(x); /* check for matching -telnet!telnet@ip */
  1280. strlcpy(dcc[i].host, s, sizeof(dcc[i].host));
  1281. #ifdef USE_IPV6
  1282. if (af_type == AF_INET6)
  1283. strlcpy(dcc[i].host6, s, sizeof(dcc[i].host6));
  1284. #endif /* USE_IPV6 */
  1285. dcc[i].port = port;
  1286. dcc[i].timeval = now;
  1287. strlcpy(dcc[i].nick, "*", sizeof(dcc[i].nick));
  1288. dcc[i].u.dns->ibuf = idx;
  1289. int dns_id = egg_dns_reverse(s, 20, dcc_telnet_dns_callback, (void *) (long) i);
  1290. if (dns_id >= 0)
  1291. dcc[i].dns_id = dns_id;
  1292. }
  1293. static void dcc_telnet_dns_callback(int id, void *client_data, const char *ip, bd::Array<bd::String> hosts)
  1294. {
  1295. // 64bit hacks
  1296. long data = (long) client_data;
  1297. int i = (int) data;
  1298. Context;
  1299. if (!valid_dns_id(i, id))
  1300. return;
  1301. int idx = dcc[i].u.dns->ibuf;
  1302. if (!valid_idx(idx)) {
  1303. putlog(LOG_BOTS, "*", "Lost listening socket while resolving %s", dcc[i].host);
  1304. killsock(dcc[i].sock);
  1305. lostdcc(i);
  1306. return;
  1307. }
  1308. //Reset timer
  1309. dcc[i].timeval = now;
  1310. //Clear the ip (still saved in dcc[i].addr)
  1311. dcc[i].host[0] = 0;
  1312. if (hosts.size()) {
  1313. strlcpy(dcc[i].host, bd::String(hosts[0]).c_str(), sizeof(dcc[i].host));
  1314. //Check forward; only check the protocol of which this connection is.
  1315. //If they connected on V4, lookup A, if V6, lookup AAAA
  1316. int dns_type = DNS_LOOKUP_A;
  1317. if (is_dotted_ip(iptostr(htonl(dcc[i].addr))) == AF_INET6)//Is this even valid?
  1318. dns_type = DNS_LOOKUP_AAAA;
  1319. int dns_id = egg_dns_lookup(bd::String(hosts[0]).c_str(), 20, dcc_telnet_dns_forward_callback, (void *) (long) i, dns_type);
  1320. if (dns_id >= 0)
  1321. dcc[i].dns_id = dns_id;
  1322. } else {
  1323. bd::Array<bd::String> empty;
  1324. dcc_telnet_dns_forward_callback(id, client_data, ip, empty);
  1325. }
  1326. }
  1327. static void dcc_telnet_dns_forward_callback(int id, void *client_data, const char *host, bd::Array<bd::String> ips) {
  1328. // 64bit hacks
  1329. long data = (long) client_data;
  1330. int i = (int) data;
  1331. Context;
  1332. if (!valid_dns_id(i, id))
  1333. return;
  1334. int j = -1, sock, idx = -1;
  1335. char s2[UHOSTLEN + 20] = "";
  1336. if (valid_idx(i))
  1337. idx = dcc[i].u.dns->ibuf;
  1338. if (!valid_idx(idx)) {
  1339. putlog(LOG_BOTS, "*", "Lost listening socket while resolving %s", iptostr(htonl(dcc[i].addr)));
  1340. killsock(dcc[i].sock);
  1341. lostdcc(i);
  1342. return;
  1343. }
  1344. bool forward_matched = false;
  1345. bd::String look_for_ip(iptostr(htonl(dcc[i].addr)));
  1346. // Look for any match
  1347. for (size_t n = 0; n < ips.size(); ++n) {
  1348. if (ips[n] == look_for_ip) {
  1349. forward_matched = true;
  1350. break;
  1351. }
  1352. }
  1353. // If the forward did not match, replace the saved host with the ip
  1354. if (!forward_matched)
  1355. strlcpy(dcc[i].host, iptostr(htonl(dcc[i].addr)), sizeof(dcc[i].host));
  1356. if (forward_matched) {
  1357. // Are they ignored by host?
  1358. simple_snprintf(s2, sizeof(s2), "-telnet!telnet@%s", dcc[i].host);
  1359. if (match_ignore(s2)) {
  1360. putlog(LOG_DEBUG, "*", "Ignored telnet connection from: %s", s2);
  1361. killsock(dcc[i].sock);
  1362. lostdcc(i);
  1363. return;
  1364. }
  1365. }
  1366. if (dcc[idx].host[0] == '@') {
  1367. /* Restrict by hostname */
  1368. if (!wild_match(dcc[idx].host + 1, dcc[i].host)) {
  1369. putlog(LOG_BOTS, "*", "Refused %s (bad hostname)", dcc[i].host);
  1370. killsock(dcc[i].sock);
  1371. lostdcc(i);
  1372. return;
  1373. }
  1374. }
  1375. changeover_dcc(i, &DCC_IDENTWAIT, 0);
  1376. dcc[i].timeval = now;
  1377. dcc[i].uint.ident_sock = dcc[idx].sock;
  1378. if (!dcc[i].user)
  1379. dcc[i].user = get_user_by_host(s2); /* check for matching -telnet!telnet@host */
  1380. if (forward_matched)
  1381. putlog(LOG_MISC, "*", "Telnet connection: %s[%s]/%d", dcc[i].host, iptostr(htonl(dcc[i].addr)), dcc[i].port);
  1382. else
  1383. putlog(LOG_MISC, "*", "Telnet connection: %s/%d", iptostr(htonl(dcc[i].addr)), dcc[i].port);
  1384. sock = open_telnet((char *) iptostr(htonl(dcc[i].addr)), 113, 0);
  1385. char s[UHOSTLEN] = "";
  1386. if (sock < 0) {
  1387. if (sock == -2)
  1388. strlcpy(s, "DNS lookup failed for ident", sizeof(s));
  1389. else
  1390. strlcpy(s, strerror(errno), sizeof(s));
  1391. } else {
  1392. j = new_dcc(&DCC_IDENT, 0);
  1393. if (j < 0) {
  1394. killsock(sock);
  1395. strlcpy(s, "No Free DCC's", sizeof(s));
  1396. }
  1397. }
  1398. if (s[0]) {
  1399. putlog(LOG_MISC, "*", "Ident failed for %s: %s", dcc[i].host, s);
  1400. simple_snprintf(s, sizeof(s), "telnet@%s", dcc[i].host);
  1401. dcc_telnet_got_ident(i, s);
  1402. return;
  1403. }
  1404. dcc[j].sock = sock;
  1405. dcc[j].port = 113;
  1406. dcc[j].addr = dcc[i].addr;
  1407. strlcpy(dcc[j].host, dcc[i].host, sizeof(dcc[j].host));
  1408. strlcpy(dcc[j].nick, "*", sizeof(dcc[j].nick));
  1409. dcc[j].uint.ident_sock = dcc[i].sock;
  1410. dcc[j].user = dcc[i].user;
  1411. dcc[j].timeval = now;
  1412. dprintf(j, "%d, %d\n", dcc[i].port, dcc[idx].port);
  1413. }
  1414. static void
  1415. eof_dcc_telnet(int idx)
  1416. {
  1417. putlog(LOG_MISC, "*", "(!) Listening port %d abruptly died.", dcc[idx].port);
  1418. killsock(dcc[idx].sock);
  1419. lostdcc(idx);
  1420. }
  1421. static void
  1422. display_telnet(int idx, char *buf, size_t bufsiz)
  1423. {
  1424. simple_snprintf(buf, bufsiz, "lstn %d%s", dcc[idx].port, (dcc[idx].status & LSTN_PUBLIC) ? " pub" : "");
  1425. }
  1426. struct dcc_table DCC_TELNET = {
  1427. "TELNET",
  1428. DCT_LISTEN,
  1429. eof_dcc_telnet,
  1430. dcc_telnet,
  1431. NULL,
  1432. NULL,
  1433. display_telnet,
  1434. NULL,
  1435. NULL,
  1436. NULL
  1437. };
  1438. static void
  1439. eof_dcc_dupwait(int idx)
  1440. {
  1441. putlog(LOG_BOTS, "*", "Lost telnet connection from %s while checking for duplicate", dcc[idx].host);
  1442. killsock(dcc[idx].sock);
  1443. lostdcc(idx);
  1444. }
  1445. static void
  1446. dcc_dupwait(int idx, char *buf, int i)
  1447. {
  1448. /* We just ignore any data at this point. */
  1449. return;
  1450. }
  1451. /* We now check again. If the bot is still marked as duplicate, there is no
  1452. * botnet lag we could push it on, so we just drop the connection.
  1453. */
  1454. static void
  1455. timeout_dupwait(int idx)
  1456. {
  1457. /* Still duplicate? */
  1458. if (in_chain(dcc[idx].nick)) {
  1459. char x[UHOSTLEN] = "";
  1460. simple_snprintf(x, sizeof x, "%s!%s", dcc[idx].nick, dcc[idx].host);
  1461. putlog(LOG_BOTS, "*", "Refused telnet connection from %s (duplicate)", x);
  1462. killsock(dcc[idx].sock);
  1463. lostdcc(idx);
  1464. } else {
  1465. /* Ha! Now it's gone and we can grant this bot access. */
  1466. dcc_telnet_pass(idx, dcc[idx].u.dupwait->atr);
  1467. }
  1468. }
  1469. static void
  1470. display_dupwait(int idx, char *buf, size_t bufsiz)
  1471. {
  1472. simple_snprintf(buf, bufsiz, "wait duplicate?");
  1473. }
  1474. static void
  1475. kill_dupwait(int idx, void *x)
  1476. {
  1477. register struct dupwait_info *p = (struct dupwait_info *) x;
  1478. if (p)
  1479. free(p);
  1480. }
  1481. struct dcc_table DCC_DUPWAIT = {
  1482. "DUPWAIT",
  1483. DCT_VALIDIDX,
  1484. eof_dcc_dupwait,
  1485. dcc_dupwait,
  1486. &dupwait_timeout,
  1487. timeout_dupwait,
  1488. display_dupwait,
  1489. kill_dupwait,
  1490. NULL,
  1491. NULL
  1492. };
  1493. /* This function is called if a bot gets removed from the list. It checks
  1494. * wether we have a pending duplicate connection for that bot and continues
  1495. * with the login in that case.
  1496. */
  1497. void
  1498. dupwait_notify(const char *who)
  1499. {
  1500. for (register int idx = 0; idx < dcc_total; idx++)
  1501. if (dcc[idx].type && (dcc[idx].type == &DCC_DUPWAIT) && !strcasecmp(dcc[idx].nick, who)) {
  1502. dcc_telnet_pass(idx, dcc[idx].u.dupwait->atr);
  1503. break;
  1504. }
  1505. }
  1506. static void
  1507. dcc_telnet_id(int idx, char *buf, int atr)
  1508. {
  1509. char *nick = buf;
  1510. strip_telnet(dcc[idx].sock, nick, &atr);
  1511. if (nick[0] == '-') {
  1512. nick++;
  1513. dcc[idx].bot = 1;
  1514. dcc[idx].encrypt = 1;
  1515. } else if (nick[0] == '+') {
  1516. nick++;
  1517. dcc[idx].encrypt = 1;
  1518. }
  1519. nick[HANDLEN] = 0; // Trim to handle length before looking up user.
  1520. dcc[idx].user = get_user_by_handle(userlist, nick);
  1521. bool ok = 0;
  1522. if (dcc[idx].user) {
  1523. if (dcc[idx].bot != dcc[idx].user->bot) {
  1524. putlog(LOG_WARN, "*", "Refused %s (fake bot login for '%s')", dcc[idx].host, nick);
  1525. killsock(dcc[idx].sock);
  1526. lostdcc(idx);
  1527. return;
  1528. }
  1529. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  1530. get_user_flagrec(dcc[idx].user, &fr, NULL);
  1531. ok = 1;
  1532. if (conf.bot->hub && !glob_huba(fr))
  1533. ok = 0;
  1534. if (!conf.bot->hub) {
  1535. /* if I am a chanhub and they dont have +c then drop */
  1536. if (ischanhub() && !glob_chuba(fr))
  1537. ok = 0;
  1538. if (!ischanhub())
  1539. ok = 0;
  1540. }
  1541. if (!ok && glob_bot(fr))
  1542. ok = 1;
  1543. }
  1544. if (!ok) {
  1545. if (dcc[idx].user)
  1546. 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);
  1547. else if (dcc[idx].bot)
  1548. 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);
  1549. else
  1550. putlog(LOG_BOTS, "*", "Refused %s (invalid handle: %s)", dcc[idx].host, nick);
  1551. killsock(dcc[idx].sock);
  1552. lostdcc(idx);
  1553. return;
  1554. }
  1555. correct_handle(nick);
  1556. strlcpy(dcc[idx].nick, nick, sizeof(dcc[idx].nick));
  1557. if (!strcmp(dcc[idx].host, "UNKNOWN@localhost"))
  1558. simple_snprintf(dcc[idx].host, sizeof(dcc[idx].host), "%s@localhost", nick);
  1559. if (dcc[idx].user->bot) {
  1560. if (!strcasecmp(conf.bot->nick, dcc[idx].nick)) {
  1561. putlog(LOG_BOTS, "*", "Refused telnet connection from %s (tried using my botnetnick)", dcc[idx].host);
  1562. killsock(dcc[idx].sock);
  1563. lostdcc(idx);
  1564. return;
  1565. } else if (in_chain(dcc[idx].nick)) {
  1566. dcc[idx].type = &DCC_DUPWAIT;
  1567. dcc[idx].u.dupwait = (struct dupwait_info *) my_calloc(1, sizeof(struct dupwait_info));
  1568. dcc[idx].u.dupwait->atr = atr;
  1569. return;
  1570. }
  1571. // dcc[idx].u.enc = (struct enc_link_dcc *) my_calloc(1, sizeof(struct enc_link_dcc));
  1572. // dcc[idx].u.enc->method_number = 0;
  1573. // link_get_method(idx);
  1574. } else {
  1575. }
  1576. if (dcc[idx].bot && !(dcc[idx].status & STAT_UNIXDOMAIN)) {
  1577. char shost[UHOSTLEN + 20] = "", sip[UHOSTLEN + 20] = "", user[30] = "";
  1578. simple_snprintf(shost, sizeof(shost), "-telnet!%s", dcc[idx].host);
  1579. char *p = strchr(dcc[idx].host, '@');
  1580. strlcpy(user, dcc[idx].host, p - dcc[idx].host + 1);
  1581. simple_snprintf(sip, sizeof(sip), "-telnet!%s@%s", user, iptostr(htonl(dcc[idx].addr)));
  1582. struct userrec *u = get_user_by_handle(userlist, nick);
  1583. ok = 1;
  1584. // Require that the linking bot has a matching host
  1585. if (u) {
  1586. /* Check for -telnet!ident@ip or -telnet!ident@host */
  1587. if (!user_has_matching_host(nick, u, sip) && !user_has_matching_host(nick, u, shost)) {
  1588. ok = 0;
  1589. }
  1590. } else {
  1591. ok = 0;
  1592. }
  1593. if (!ok) {
  1594. putlog(LOG_BOTS, "*", "Denied link to '%s': Host not recognized: %s", nick, dcc[idx].host);
  1595. putlog(LOG_BOTS, "*", "If this host/bot is trusted: %s+host %s %s", settings.dcc_prefix, nick, shost);
  1596. killsock(dcc[idx].sock);
  1597. lostdcc(idx);
  1598. return;
  1599. }
  1600. }
  1601. dcc_telnet_pass(idx, atr);
  1602. }
  1603. static void
  1604. dcc_telnet_pass(int idx, int atr)
  1605. {
  1606. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  1607. get_user_flagrec(dcc[idx].user, &fr, NULL);
  1608. /* No password set? */
  1609. if (!dcc[idx].user->bot && (u_pass_match(dcc[idx].user, "-"))) {
  1610. dprintf(idx, "Can't telnet until you have a password set.\r\n");
  1611. putlog(LOG_MISC, "*", "Refused [%s]%s (no password)", dcc[idx].nick, dcc[idx].host);
  1612. killsock(dcc[idx].sock);
  1613. lostdcc(idx);
  1614. return;
  1615. }
  1616. if (dcc[idx].type == &DCC_DUPWAIT) {
  1617. free(dcc[idx].u.dupwait);
  1618. dcc[idx].u.other = NULL;
  1619. }
  1620. dcc[idx].type = &DCC_CHAT_PASS;
  1621. dcc[idx].timeval = now;
  1622. if ((conf.bot->hub && !glob_huba(fr)) || (!conf.bot->hub && ischanhub() && !glob_chuba(fr)))
  1623. dcc[idx].status |= STAT_PARTY;
  1624. if (!dcc[idx].bot) {
  1625. //bots dont need this
  1626. dcc[idx].u.chat = (struct chat_info *) my_calloc(1, sizeof(struct chat_info));
  1627. struct chat_info dummy;
  1628. strlcpy(dcc[idx].u.chat->con_chan, chanset ? chanset->dname : "*", sizeof(dummy.con_chan));
  1629. }
  1630. if (conf.bot->hub || (conf.bot->localhub && (dcc[idx].status & STAT_UNIXDOMAIN))) {
  1631. if (dcc[idx].encrypt) {
  1632. /* negotiate a new linking scheme */
  1633. int i = 0;
  1634. char buf[1024] = "", rand[51] = "";
  1635. make_rand_str(rand, 50);
  1636. link_hash(idx, rand);
  1637. for (i = 0; enclink[i].name; i++) {
  1638. if (enclink[i].type == LINK_CLEARTEXT && !link_cleartext) continue;
  1639. simple_snprintf(&buf[strlen(buf)], sizeof(buf) - strlen(buf), "%d ", enclink[i].type);
  1640. }
  1641. dprintf(-dcc[idx].sock, "neg? %s %s\n", rand, buf);
  1642. } else {
  1643. /* Turn off remote telnet echo (send IAC WILL ECHO). */
  1644. dprintf(idx, "\n%s" TLN_IAC_C TLN_WILL_C TLN_ECHO_C "\n", "Enter your password");
  1645. }
  1646. } else
  1647. dprintf(idx, "%s\n" TLN_IAC_C TLN_WILL_C TLN_ECHO_C, response(RES_PASSWORD));
  1648. }
  1649. static void
  1650. eof_dcc_telnet_id(int idx)
  1651. {
  1652. if (dcc[idx].port)
  1653. putlog(LOG_MISC, "*", "Lost telnet connection to %s/%d", dcc[idx].host, dcc[idx].port);
  1654. else
  1655. putlog(LOG_MISC, "*", "Lost local connection for: %s", dcc[idx].host);
  1656. killsock(dcc[idx].sock);
  1657. lostdcc(idx);
  1658. }
  1659. static void
  1660. timeout_dcc_telnet_id(int idx)
  1661. {
  1662. putlog(LOG_MISC, "*", "Ident timeout on telnet: %s", dcc[idx].host);
  1663. killsock(dcc[idx].sock);
  1664. lostdcc(idx);
  1665. }
  1666. static void
  1667. display_dcc_telnet_id(int idx, char *buf, size_t bufsiz)
  1668. {
  1669. simple_snprintf(buf, bufsiz, "t-in waited %ds", (int) (now - dcc[idx].timeval));
  1670. }
  1671. struct dcc_table DCC_TELNET_ID = {
  1672. "TELNET_ID",
  1673. 0,
  1674. eof_dcc_telnet_id,
  1675. dcc_telnet_id,
  1676. &password_timeout,
  1677. timeout_dcc_telnet_id,
  1678. display_dcc_telnet_id,
  1679. NULL,
  1680. out_dcc_general,
  1681. NULL
  1682. };
  1683. static void
  1684. dcc_socket(int idx, char *buf, int len)
  1685. {
  1686. }
  1687. static void
  1688. eof_dcc_socket(int idx)
  1689. {
  1690. killsock(dcc[idx].sock);
  1691. lostdcc(idx);
  1692. }
  1693. static void
  1694. display_dcc_socket(int idx, char *buf, size_t bufsiz)
  1695. {
  1696. strlcpy(buf, "sock (stranded)", bufsiz);
  1697. }
  1698. struct dcc_table DCC_SOCKET = {
  1699. "SOCKET",
  1700. DCT_VALIDIDX,
  1701. eof_dcc_socket,
  1702. dcc_socket,
  1703. NULL,
  1704. NULL,
  1705. display_dcc_socket,
  1706. NULL,
  1707. NULL,
  1708. NULL
  1709. };
  1710. void
  1711. dcc_identwait(int idx, char *buf, int len)
  1712. {
  1713. /* Ignore anything now */
  1714. }
  1715. void
  1716. eof_dcc_identwait(int idx)
  1717. {
  1718. putlog(LOG_MISC, "*", "Lost connection while identing [%s/%d]", dcc[idx].host, dcc[idx].port);
  1719. for (int i = 0; i < dcc_total; i++)
  1720. if (dcc[i].type && (dcc[i].type == &DCC_IDENT) && (dcc[i].uint.ident_sock == dcc[idx].sock)) {
  1721. killsock(dcc[i].sock); /* Cleanup ident socket */
  1722. dcc[i].u.other = 0;
  1723. lostdcc(i);
  1724. break;
  1725. }
  1726. killsock(dcc[idx].sock); /* Cleanup waiting socket */
  1727. dcc[idx].u.other = 0;
  1728. lostdcc(idx);
  1729. }
  1730. static void
  1731. display_dcc_identwait(int idx, char *buf, size_t bufsiz)
  1732. {
  1733. simple_snprintf(buf, bufsiz, "idtw waited %ds", (int) (now - dcc[idx].timeval));
  1734. }
  1735. struct dcc_table DCC_IDENTWAIT = {
  1736. "IDENTWAIT",
  1737. 0,
  1738. eof_dcc_identwait,
  1739. dcc_identwait,
  1740. NULL,
  1741. NULL,
  1742. display_dcc_identwait,
  1743. NULL,
  1744. NULL,
  1745. NULL
  1746. };
  1747. void
  1748. dcc_ident(int idx, char *buf, int len)
  1749. {
  1750. char ident_response[512] = "", uid[512] = "", buf1[UHOSTLEN] = "";
  1751. sscanf(buf, "%*[^:]:%[^:]:%*[^:]:%[^\n]\n", ident_response, uid);
  1752. rmspace(ident_response);
  1753. if (ident_response[0] != 'U') {
  1754. dcc[idx].timeval = now;
  1755. return;
  1756. }
  1757. rmspace(uid);
  1758. uid[20] = 0; /* 20 character ident max */
  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. simple_snprintf(buf1, sizeof(buf1), "%s@%s", uid, dcc[idx].host);
  1762. dcc_telnet_got_ident(i, buf1);
  1763. }
  1764. dcc[idx].u.other = 0;
  1765. killsock(dcc[idx].sock);
  1766. lostdcc(idx);
  1767. }
  1768. void
  1769. eof_dcc_ident(int idx)
  1770. {
  1771. char buf[UHOSTLEN] = "";
  1772. for (int i = 0; i < dcc_total; i++)
  1773. if (dcc[i].type && (dcc[i].type == &DCC_IDENTWAIT) && (dcc[i].sock == dcc[idx].uint.ident_sock)) {
  1774. putlog(LOG_MISC, "*", "Timeout/EOF ident connection");
  1775. simple_snprintf(buf, sizeof(buf), "telnet@%s", dcc[idx].host);
  1776. dcc_telnet_got_ident(i, buf);
  1777. }
  1778. killsock(dcc[idx].sock);
  1779. dcc[idx].u.other = 0;
  1780. lostdcc(idx);
  1781. }
  1782. static void
  1783. display_dcc_ident(int idx, char *buf, size_t bufsiz)
  1784. {
  1785. simple_snprintf(buf, bufsiz, "idnt (sock %d)", dcc[idx].uint.ident_sock);
  1786. }
  1787. struct dcc_table DCC_IDENT = {
  1788. "IDENT",
  1789. 0,
  1790. eof_dcc_ident,
  1791. dcc_ident,
  1792. &identtimeout,
  1793. eof_dcc_ident,
  1794. display_dcc_ident,
  1795. NULL,
  1796. NULL,
  1797. NULL
  1798. };
  1799. static void
  1800. dcc_telnet_got_ident(int i, char *host)
  1801. {
  1802. int idx;
  1803. for (idx = 0; idx < dcc_total; idx++)
  1804. if (dcc[i].type && (dcc[idx].type == &DCC_TELNET) && (dcc[idx].sock == dcc[i].uint.ident_sock))
  1805. break;
  1806. dcc[i].u.other = 0;
  1807. if (idx == dcc_total || !dcc[idx].type) {
  1808. putlog(LOG_MISC, "*", "Lost ident wait telnet socket!!");
  1809. killsock(dcc[i].sock);
  1810. lostdcc(i);
  1811. return;
  1812. }
  1813. strlcpy(dcc[i].host, host, sizeof(dcc[i].host));
  1814. bool unix_domain = 0;
  1815. if (!strcmp(host, "UNKNOWN@localhost"))
  1816. unix_domain = 1;
  1817. if (!unix_domain) {
  1818. char shost[UHOSTLEN + 20] = "", sip[UHOSTLEN + 20] = "";
  1819. char *p = strchr(host, '@');
  1820. *p = 0;
  1821. simple_snprintf(shost, sizeof(shost), "-telnet!%s", dcc[i].host);
  1822. simple_snprintf(sip, sizeof(sip), "-telnet!%s@%s", host, iptostr(htonl(dcc[i].addr)));
  1823. if (match_ignore(shost) || match_ignore(sip)) {
  1824. putlog(LOG_DEBUG, "*", "Ignored telnet connection from: %s[%s]",dcc[i].host, iptostr(htonl(dcc[i].addr)));
  1825. killsock(dcc[i].sock);
  1826. lostdcc(i);
  1827. return;
  1828. }
  1829. if (protect_telnet) {
  1830. struct userrec *u = NULL;
  1831. bool ok = 1;
  1832. u = dcc[i].user;
  1833. if (!u)
  1834. u = get_user_by_host(sip); /* Check for -telnet!ident@ip */
  1835. if (!u)
  1836. u = get_user_by_host(shost); /* Check for -telnet!ident@host */
  1837. if (!u)
  1838. ok = 0;
  1839. if (ok && u && conf.bot->hub && !(u->flags & USER_HUBA))
  1840. ok = 0;
  1841. /* if I am a chanhub and they dont have +c then drop */
  1842. if (ok && (!conf.bot->hub && ischanhub() && u && !(u->flags & USER_CHUBA)))
  1843. ok = 0;
  1844. /* else if (!(u->flags & USER_PARTY))
  1845. ok = 0; */
  1846. if (!ok && u && u->bot)
  1847. ok = 1;
  1848. if (!ok && (dcc[idx].status & LSTN_PUBLIC))
  1849. ok = 1;
  1850. if (!ok) {
  1851. putlog(LOG_MISC, "*", "Denied telnet: %s, No Access", dcc[i].host);
  1852. killsock(dcc[i].sock);
  1853. lostdcc(i);
  1854. return;
  1855. }
  1856. }
  1857. }
  1858. /* Do not buffer data anymore. All received and stored data is passed
  1859. * over to the dcc functions from now on. */
  1860. sockoptions(dcc[i].sock, EGG_OPTION_UNSET, SOCK_BUFFER);
  1861. dcc[i].type = &DCC_TELNET_ID;
  1862. /* Copy acceptable-nick/host mask */
  1863. dcc[i].status = (STAT_TELNET | STAT_ECHO | STAT_COLOR | STAT_BANNER | STAT_CHANNELS | STAT_BOTS | STAT_WHOM);
  1864. if (unix_domain)
  1865. dcc[i].status |= STAT_UNIXDOMAIN;
  1866. /* Copy acceptable-nick/host mask */
  1867. strlcpy(dcc[i].nick, dcc[idx].host, sizeof(dcc[i].nick));
  1868. dcc[i].timeval = now;
  1869. dcc[i].u.other = NULL;
  1870. /* This is so we dont tell someone doing a portscan anything
  1871. * about ourselves. <cybah>
  1872. */
  1873. if (conf.bot->hub || (conf.bot->localhub && unix_domain))
  1874. dprintf(i, " \n"); /* represents hub that support new linking scheme */
  1875. else
  1876. dprintf(i, "%s\n", response(RES_USERNAME));
  1877. }