dcc.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173
  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 = -1;
  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 (vlocalhub == -1 || vbuildts == 0 || vcommit == NULL || vversion == NULL) {
  276. putlog(LOG_BOTS, "*", "Invalid link from %s [likely a hack].\n", dcc[idx].nick);
  277. dprintf(idx, "error badbot\n");
  278. dprintf(idx, "bye badbot\n");
  279. killsock(dcc[idx].sock);
  280. lostdcc(idx);
  281. return;
  282. }
  283. if (conf.bot->hub || (conf.bot->localhub && (dcc[idx].status & STAT_UNIXDOMAIN))) {
  284. putlog(LOG_BOTS, "*", "Linked to %s.\n", dcc[idx].nick);
  285. chatout("*** Linked to %s.\n", dcc[idx].nick);
  286. } else {
  287. putlog(LOG_BOTS, "*", "Linked to botnet.");
  288. chatout("*** Linked to botnet.\n");
  289. }
  290. if (conf.bot->hub || conf.bot->localhub) {
  291. if (bot_hublevel(dcc[idx].user) < 999) {
  292. if (!bot_aggressive_to(dcc[idx].user)) { //not aggressive, so they are technically my uplink.
  293. uplink_idx = idx;
  294. // This is now done in share_endstartup
  295. //have_linked_to_hub = 1;
  296. }
  297. dcc[idx].hub = 1;
  298. }
  299. botnet_send_nlinked(idx, dcc[idx].nick, conf.bot->nick, '!', vlocalhub, vbuildts, vcommit, vversion);
  300. } else {
  301. // This is now done in share_endstartup
  302. //have_linked_to_hub = 1;
  303. uplink_idx = idx;
  304. dcc[idx].hub = 1;
  305. }
  306. dump_links(idx);
  307. touch_laston(dcc[idx].user, "linked", now);
  308. dcc[idx].type = &DCC_BOT;
  309. addbot(dcc[idx].nick, dcc[idx].nick, conf.bot->nick, '-', vlocalhub, vbuildts, vcommit, vversion);
  310. simple_snprintf(x, sizeof x, "v 1001500");
  311. bot_share(idx, x);
  312. dprintf(idx, "el\n");
  313. }
  314. void
  315. failed_link(int idx)
  316. {
  317. char nick[NICKLEN] = "", s1[NICKLEN + 17 + 1] = "";
  318. if (dcc[idx].u.bot->linker[0]) {
  319. simple_snprintf(s1, sizeof s1, "Couldn't link to %s.", dcc[idx].nick);
  320. strlcpy(nick, dcc[idx].u.bot->linker, sizeof(s1));
  321. add_note(nick, conf.bot->nick, s1, -2, 0);
  322. }
  323. if (dcc[idx].u.bot->numver >= (-1))
  324. putlog(LOG_BOTS, "*", "Failed link to %s.", dcc[idx].nick);
  325. if (dcc[idx].sock != -1) {
  326. killsock(dcc[idx].sock);
  327. dcc[idx].sock = -1;
  328. }
  329. strlcpy(nick, dcc[idx].nick, sizeof(nick));
  330. lostdcc(idx);
  331. if (conf.bot->hub || conf.bot->localhub)
  332. strlcpy(autolink_failed, nick, HANDLEN + 1);
  333. }
  334. static void
  335. cont_link(int idx, char *buf, int ii)
  336. {
  337. /* If we're already connected somewhere, unlink and idle a sec */
  338. for (int i = 0; i < dcc_total; i++) {
  339. if (unlikely(dcc[i].type && (dcc[i].type == &DCC_BOT) && (!bot_aggressive_to(dcc[i].user)))) {
  340. putlog(LOG_BOTS, "*", "Unlinking %s - restructure", dcc[i].nick);
  341. botnet_send_unlinked(i, dcc[i].nick, "Restructure");
  342. killsock(dcc[i].sock);
  343. lostdcc(i);
  344. usleep(1000 * 500);
  345. break;
  346. }
  347. }
  348. dcc[idx].type = &DCC_BOT_NEW;
  349. dcc[idx].u.bot->numver = 0;
  350. if (ii == 3)
  351. dprintf(idx, STR("-%s\n"), conf.bot->nick);
  352. /* wait for "neg?" now */
  353. /* now we wait to negotiate an encryption */
  354. return;
  355. }
  356. static void
  357. dcc_bot_new(int idx, char *buf, int x)
  358. {
  359. /* struct userrec *u = get_user_by_handle(userlist, dcc[idx].nick); */
  360. char *code = NULL;
  361. strip_telnet(dcc[idx].sock, buf, &x);
  362. code = newsplit(&buf);
  363. if (!strcasecmp(code, "goodbye!")) {
  364. greet_new_bot(idx);
  365. } else if (!strcasecmp(code, "v")) {
  366. bot_version(idx, buf);
  367. } else if (!strcasecmp(code, STR("neg!"))) { /* something to parse in enclink.c */
  368. link_parse(idx, buf);
  369. } else if (!strcasecmp(code, STR("neg?"))) { /* we're connecting to THEM */
  370. link_challenge_to(idx, buf);
  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. in_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, sizeof(dcc[j].host));
  575. strlcpy(dcc[j].nick, "*", sizeof(dcc[j].nick));
  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, sizeof(dcc[idx].nick));
  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].encrypt == 1) {
  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. int snum = findanysnum(dcc[idx].sock);
  871. if (socklist[snum].enclink == -1) {
  872. putlog(LOG_WARN, "*", STR("%s attempted to negotiate an encryption out of order."), dcc[idx].nick);
  873. killsock(dcc[idx].sock);
  874. lostdcc(idx);
  875. return;
  876. }
  877. dcc[idx].encrypt = 2;
  878. if (dcc[idx].bot) {
  879. dcc[idx].type = &DCC_BOT_NEW;
  880. dcc[idx].u.bot = (struct bot_info *) my_calloc(1, sizeof(struct bot_info));
  881. if (dcc[idx].status & STAT_UNIXDOMAIN)
  882. dcc[idx].status = STAT_UNIXDOMAIN|STAT_CALLED;
  883. else
  884. dcc[idx].status = STAT_CALLED;
  885. dprintf(idx, "goodbye!\n");
  886. greet_new_bot(idx);
  887. if (conf.bot->hub || conf.bot->localhub)
  888. send_timesync(idx);
  889. } else {
  890. // User encrypted over relay
  891. /* Turn off remote telnet echo (send IAC WILL ECHO). */
  892. dprintf(idx, "\n%s" TLN_IAC_C TLN_WILL_C TLN_ECHO_C "\n", "Enter your password");
  893. }
  894. } else if (!strcasecmp(pass, STR("neg"))) {
  895. //link_challenge_from()
  896. int snum = findanysnum(dcc[idx].sock);
  897. if (snum >= 0) {
  898. char *hash = newsplit(&buf);
  899. int hash_n = strcmp(dcc[idx].shahash, hash);
  900. OPENSSL_cleanse(dcc[idx].shahash, sizeof(dcc[idx].shahash));
  901. OPENSSL_cleanse(hash, strlen(hash));
  902. if (hash_n) {
  903. putlog(LOG_WARN, "*", STR("%s attempted to negotiate an encryption with an invalid hash."), dcc[idx].nick);
  904. killsock(dcc[idx].sock);
  905. lostdcc(idx);
  906. return;
  907. }
  908. int type = atoi(newsplit(&buf)), i = -1;
  909. /* verify we have that type and then initiate it */
  910. if ((i = link_find_by_type(type)) == -1) {
  911. putlog(LOG_WARN, "*", STR("%s attempted to link with an invalid encryption. (%d)"), dcc[idx].nick, type);
  912. if (type == 0 && !link_cleartext) {
  913. putlog(LOG_WARN, "*", "This is likely due to %s needing to be upgraded. Enable 'link_cleartext' to allow linking.", dcc[idx].nick);
  914. putlog(LOG_WARN, "*", "Be sure to disable 'link_cleartext' after all bots are upgraded.");
  915. }
  916. killsock(dcc[idx].sock);
  917. lostdcc(idx);
  918. return;
  919. }
  920. sdprintf(STR("Using '%s' (%d/%d) for link with %s"), enclink[i].name, enclink[i].type, i, dcc[idx].nick);
  921. if (buf[0]) {
  922. const char *expected_nick = newsplit(&buf);
  923. if (strcasecmp(expected_nick, conf.bot->nick)) {
  924. putlog(LOG_WARN, "*", STR("%s failed encrypted link handshake (was expecting '%s' instead of me)"), dcc[idx].nick, expected_nick);
  925. killsock(dcc[idx].sock);
  926. lostdcc(idx);
  927. return;
  928. }
  929. }
  930. socklist[snum].enclink = i;
  931. link_link(idx, -1, i, FROM);
  932. }
  933. } else {
  934. /* Invalid password/digest on hub */
  935. putlog(LOG_WARN, "*", STR("%s failed encrypted link handshake."), dcc[idx].nick);
  936. killsock(dcc[idx].sock);
  937. lostdcc(idx);
  938. }
  939. return;
  940. }
  941. /* else !bot */
  942. int passok = u_pass_match(dcc[idx].user, pass);
  943. bool do_obscure = (!passok && auth_obscure) ? 1 : 0;
  944. if (passok || do_obscure) {
  945. if (do_obscure)
  946. dcc[idx].wrong_pass = 1;
  947. if (dccauth || do_obscure) {
  948. char randstr[51] = "";
  949. make_rand_str(randstr, 50);
  950. makehash(dcc[idx].user, randstr, dcc[idx].hash, MD5_HASH_LENGTH + 1);
  951. dcc[idx].type = &DCC_CHAT_SECPASS;
  952. dcc[idx].timeval = now;
  953. dprintf(-dcc[idx].sock, STR("-Auth %s %s\n"), randstr, conf.bot->nick);
  954. } else {
  955. dcc_chat_secpass(idx, pass, atr);
  956. }
  957. } else {
  958. dprintf(idx, "%s\n", response(RES_BADUSERPASS));
  959. putlog(LOG_MISC, "*", "Bad Password: [%s]%s/%d", dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  960. if (dcc[idx].u.chat->away) { /* su from a dumb user */
  961. /* Turn echo back on for telnet sessions (send IAC WON'T ECHO). */
  962. if (dcc[idx].status & STAT_TELNET)
  963. dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  964. dcc[idx].user = get_user_by_handle(userlist, dcc[idx].u.chat->away);
  965. strlcpy(dcc[idx].nick, dcc[idx].u.chat->away, sizeof(dcc[idx].nick));
  966. free(dcc[idx].u.chat->away);
  967. free(dcc[idx].u.chat->su_nick);
  968. dcc[idx].u.chat->away = NULL;
  969. dcc[idx].u.chat->su_nick = NULL;
  970. dcc[idx].type = &DCC_CHAT;
  971. dcc[idx].u.chat->channel = dcc[idx].u.chat->su_channel;
  972. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  973. botnet_send_join_idx(idx);
  974. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s has joined the party line.\n", dcc[idx].nick);
  975. } else {
  976. killsock(dcc[idx].sock);
  977. lostdcc(idx);
  978. }
  979. }
  980. }
  981. struct dcc_table DCC_CHAT_PASS = {
  982. "CHAT_PASS",
  983. 0,
  984. eof_dcc_general,
  985. dcc_chat_pass,
  986. &password_timeout,
  987. tout_dcc_chat_pass,
  988. display_dcc_chat_pass,
  989. kill_dcc_general,
  990. out_dcc_general,
  991. NULL
  992. };
  993. /* Make sure ansi code is just for color-changing
  994. */
  995. static int
  996. check_ansi(char *v)
  997. {
  998. int count = 2;
  999. if (*v++ != '\033')
  1000. return 1;
  1001. if (*v++ != '[')
  1002. return 1;
  1003. while (*v) {
  1004. if (*v == 'm')
  1005. return 0;
  1006. if ((*v != ';') && ((*v < '0') || (*v > '9')))
  1007. return count;
  1008. v++;
  1009. count++;
  1010. }
  1011. return count;
  1012. }
  1013. int ansi_len(char *s)
  1014. {
  1015. char *c = s;
  1016. int count = 0;
  1017. while (*c) {
  1018. if (*c == 27) {
  1019. c++;
  1020. count++;
  1021. if (*c == '[') {
  1022. c++;
  1023. count++;
  1024. while ((*c != 'm')) {
  1025. c++;
  1026. count++;
  1027. }
  1028. c++;
  1029. count++;
  1030. }
  1031. } else
  1032. c++;
  1033. }
  1034. return count;
  1035. }
  1036. static void
  1037. eof_dcc_chat(int idx)
  1038. {
  1039. putlog(LOG_MISC, "*", "Lost dcc connection to %s (%s/%d)", dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  1040. if (dcc[idx].u.chat->channel >= 0) {
  1041. chanout_but(idx, dcc[idx].u.chat->channel, "*** %s lost dcc link.\n", dcc[idx].nick);
  1042. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  1043. botnet_send_part_idx(idx, "lost dcc link");
  1044. }
  1045. check_bind_chof(dcc[idx].nick, idx);
  1046. killsock(dcc[idx].sock);
  1047. lostdcc(idx);
  1048. }
  1049. static void
  1050. dcc_chat(int idx, char *buf, int len)
  1051. {
  1052. int i = 0;
  1053. strip_telnet(dcc[idx].sock, buf, &len);
  1054. if (buf[0] && (buf[0] != settings.dcc_prefix[0]) && !(dcc[idx].user && (dcc[idx].user->flags & USER_NOFLOOD)) &&
  1055. detect_dcc_flood(&dcc[idx].timeval, dcc[idx].u.chat, idx))
  1056. return;
  1057. dcc[idx].timeval = now;
  1058. if (buf[0]) {
  1059. int nathan = 0, doron = 0, fixed = 0;
  1060. char *v = buf, *d = buf;
  1061. /* Check for beeps and cancel annoying ones */
  1062. while (*v)
  1063. switch (*v) {
  1064. case 1: /* CTCP ?! */
  1065. v++;
  1066. break;
  1067. case 7: /* Beep - no more than 3 */
  1068. nathan++;
  1069. if (nathan > 3)
  1070. v++;
  1071. else
  1072. *d++ = *v++;
  1073. break;
  1074. case 8: /* Backspace - for lame telnet's :) */
  1075. if (d > buf) {
  1076. d--;
  1077. }
  1078. v++;
  1079. break;
  1080. case 27: /* ESC - ansi code? */
  1081. doron = check_ansi(v);
  1082. /* If it's valid, append a return-to-normal code at the end */
  1083. if (!doron) {
  1084. *d++ = *v++;
  1085. fixed = 1;
  1086. } else
  1087. v += doron;
  1088. break;
  1089. case '\r': /* Weird pseudo-linefeed */
  1090. v++;
  1091. break;
  1092. default:
  1093. *d++ = *v++;
  1094. }
  1095. if (fixed)
  1096. strcpy(d, "\033[0m");
  1097. else
  1098. *d = 0;
  1099. if (unlikely(u_pass_match(dcc[idx].user, buf))) { /* user said their password :) */
  1100. dprintf(idx, "Sure you want that going to the partyline? ;) (msg to partyline halted.)\n");
  1101. } else if (unlikely(!strncmp(buf, STR("+Auth "), 6))) { /* ignore extra +Auth lines */
  1102. } else if ((!strncmp(buf, settings.dcc_prefix, strlen(settings.dcc_prefix))) || (dcc[idx].u.chat->channel < 0)) {
  1103. if (!strncmp(buf, settings.dcc_prefix, strlen(settings.dcc_prefix)) && (dcc[idx].u.chat->channel >= 0)) /* strip '.' out */
  1104. buf++;
  1105. v = newsplit(&buf);
  1106. rmspace(buf);
  1107. check_bind_dcc(v, idx, buf);
  1108. } else if (unlikely(buf[0] == ',')) {
  1109. int me = 0;
  1110. if ((buf[1] == 'm') && (buf[2] == 'e') && buf[3] == ' ')
  1111. me = 1;
  1112. for (i = 0; i < dcc_total; i++) {
  1113. if (dcc[i].type) {
  1114. int ok = 0;
  1115. if (dcc[i].type->flags & DCT_MASTER) {
  1116. if ((dcc[i].type != &DCC_CHAT) || (dcc[i].u.chat->channel >= 0))
  1117. if ((i != idx) || (dcc[idx].status & STAT_ECHO))
  1118. ok = 1;
  1119. }
  1120. if (ok) {
  1121. struct userrec *u = get_user_by_handle(userlist, dcc[i].nick);
  1122. if (u && (u->flags & USER_MASTER)) {
  1123. if (me)
  1124. dprintf(i, "-> %s%s\n", dcc[idx].nick, buf + 3);
  1125. else
  1126. dprintf(i, "-%s-> %s\n", dcc[idx].nick, buf + 1);
  1127. }
  1128. }
  1129. }
  1130. }
  1131. } else if (unlikely(buf[0] == '\'')) {
  1132. int me = 0;
  1133. if ((buf[1] == 'm') && (buf[2] == 'e') && ((buf[3] == ' ') || (buf[3] == '\'') || (buf[3] == ',')))
  1134. me = 1;
  1135. for (i = 0; i < dcc_total; i++) {
  1136. if (dcc[i].type && dcc[i].type->flags & DCT_CHAT) {
  1137. if (me)
  1138. dprintf(i, "=> %s%s\n", dcc[idx].nick, buf + 3);
  1139. else
  1140. dprintf(i, "=%s=> %s\n", dcc[idx].nick, buf + 1);
  1141. }
  1142. }
  1143. } else { /* partyline chat */
  1144. if (dcc[idx].u.chat->away != NULL)
  1145. not_away(idx);
  1146. /* Check for CTCP (/me) */
  1147. if (!strncmp(buf, "CTCP_MESSAGE ", 13)) /* irssi */
  1148. buf += 13;
  1149. if (!strncmp(buf, "ACTION ", 7)) {
  1150. buf += 7;
  1151. check_bind_dcc("me", idx, buf);
  1152. } else { /* regular text */
  1153. if (dcc[idx].status & STAT_ECHO)
  1154. chanout_but(-1, dcc[idx].u.chat->channel, "<%s> %s\n", dcc[idx].nick, buf);
  1155. else
  1156. chanout_but(idx, dcc[idx].u.chat->channel, "<%s> %s\n", dcc[idx].nick, buf);
  1157. botnet_send_chan(-1, conf.bot->nick, dcc[idx].nick, dcc[idx].u.chat->channel, buf);
  1158. }
  1159. }
  1160. }
  1161. if (dcc[idx].type == &DCC_CHAT) /* Could have change to files */
  1162. if (dcc[idx].status & STAT_PAGE)
  1163. flush_lines(idx, dcc[idx].u.chat);
  1164. }
  1165. static void
  1166. display_dcc_chat(int idx, char *buf, size_t bufsiz)
  1167. {
  1168. size_t i = simple_snprintf(buf, bufsiz, "chat flags: ");
  1169. int colori = 0;
  1170. buf[i++] = dcc[idx].status & STAT_CHAT ? 'C' : 'c';
  1171. buf[i++] = dcc[idx].status & STAT_PARTY ? 'P' : 'p';
  1172. buf[i++] = dcc[idx].status & STAT_TELNET ? 'T' : 't';
  1173. buf[i++] = dcc[idx].status & STAT_ECHO ? 'E' : 'e';
  1174. buf[i++] = dcc[idx].status & STAT_PAGE ? 'P' : 'p';
  1175. if ((colori = coloridx(idx)))
  1176. buf[i++] = colori == 1 ? 'A' : 'M';
  1177. #ifdef USE_IPV6
  1178. if (sockprotocol(dcc[idx].sock) == AF_INET6 && dcc[idx].host6[0])
  1179. buf[i++] = '6';
  1180. #endif /* USE_IPV6 */
  1181. simple_snprintf(buf + i, bufsiz - i, "/%d", dcc[idx].u.chat->channel);
  1182. }
  1183. struct dcc_table DCC_CHAT = {
  1184. "CHAT",
  1185. DCT_CHAT | DCT_MASTER | DCT_SHOWWHO | DCT_VALIDIDX | DCT_SIMUL | DCT_CANBOOT | DCT_REMOTEWHO,
  1186. eof_dcc_chat,
  1187. dcc_chat,
  1188. NULL,
  1189. NULL,
  1190. display_dcc_chat,
  1191. kill_dcc_general,
  1192. out_dcc_general,
  1193. NULL
  1194. };
  1195. static int lasttelnets;
  1196. static char lasttelnethost[81];
  1197. static time_t lasttelnettime;
  1198. /* A modified detect_flood for incoming telnet flood protection.
  1199. */
  1200. static bool
  1201. detect_telnet_flood(char *floodhost)
  1202. {
  1203. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  1204. get_user_flagrec(get_user_by_host(floodhost), &fr, NULL);
  1205. if (!flood_telnet_thr || glob_noflood(fr))
  1206. return 0; /* No flood protection */
  1207. if (strcasecmp(lasttelnethost, floodhost)) { /* New... */
  1208. strlcpy(lasttelnethost, floodhost, sizeof(lasttelnethost));
  1209. lasttelnettime = now;
  1210. lasttelnets = 0;
  1211. return 0;
  1212. }
  1213. if (lasttelnettime < now - flood_telnet_time) {
  1214. /* Flood timer expired, reset it */
  1215. lasttelnettime = now;
  1216. lasttelnets = 0;
  1217. return 0;
  1218. }
  1219. lasttelnets++;
  1220. if (lasttelnets >= flood_telnet_thr) { /* FLOOD! */
  1221. /* Reset counters */
  1222. lasttelnets = 0;
  1223. lasttelnettime = 0;
  1224. lasttelnethost[0] = 0;
  1225. putlog(LOG_MISC, "*", "Telnet connection flood from %s! Placing on ignore!", floodhost);
  1226. addignore(floodhost, conf.bot->nick, "Telnet connection flood", now + (60 * ignore_time));
  1227. return 1;
  1228. }
  1229. return 0;
  1230. }
  1231. static void dcc_telnet_dns_callback(int, void *, const char *, bd::Array<bd::String>);
  1232. static void dcc_telnet_dns_forward_callback(int, void *, const char *, bd::Array<bd::String>);
  1233. static void
  1234. dcc_telnet(int idx, char *buf, int ii)
  1235. {
  1236. in_addr_t ip;
  1237. in_port_t port;
  1238. char s[UHOSTLEN + 1] = "";
  1239. int i;
  1240. char x[1024] = "";
  1241. if (unlikely(dcc_total + 1 > max_dcc)) {
  1242. int j;
  1243. j = answer(dcc[idx].sock, s, &ip, &port, 0);
  1244. if (j != -1) {
  1245. dprintf(-j, "Sorry, too many connections already.\r\n");
  1246. killsock(j);
  1247. }
  1248. return;
  1249. }
  1250. int sock = answer(dcc[idx].sock, s, &ip, &port, 0);
  1251. while ((sock == -1) && (errno == EAGAIN))
  1252. sock = answer(dcc[idx].sock, s, &ip, &port, 0);
  1253. if (unlikely(sock < 0)) {
  1254. putlog(LOG_MISC, "*", "Failed TELNET incoming (%s)", strerror(errno));
  1255. // killsock(dcc[idx].sock);
  1256. return;
  1257. }
  1258. /* Buffer data received on this socket. */
  1259. sockoptions(sock, EGG_OPTION_SET, SOCK_BUFFER);
  1260. int af_type = sockprotocol(sock);
  1261. if (af_type == AF_UNIX) {
  1262. //i = new_dcc(&DCC_IDENT, 0);
  1263. i = new_dcc(&DCC_TELNET_ID, 0);
  1264. simple_snprintf(x, sizeof(x), "UNKNOWN@localhost");
  1265. dcc[i].sock = sock;
  1266. dcc[i].uint.ident_sock = dcc[idx].sock;
  1267. dcc[i].port = 0;
  1268. dcc[i].timeval = now;
  1269. strlcpy(dcc[i].nick, "*", sizeof(dcc[i].nick));
  1270. putlog(LOG_BOTS, "*", "Connection over local socket: %s", s);
  1271. dcc_telnet_got_ident(i, x);
  1272. return;
  1273. }
  1274. if (port < 1024 || port > 65534) {
  1275. putlog(LOG_BOTS, "*", "Refused %s/%d (bad src port)", s, port);
  1276. killsock(sock);
  1277. return;
  1278. }
  1279. putlog(LOG_DEBUG, "*", "Telnet connection: %s/%d", s, port);
  1280. // Are they ignored by IP?
  1281. simple_snprintf(x, sizeof(x), "-telnet!telnet@%s", iptostr(htonl(ip)));
  1282. if (unlikely(match_ignore(x) || detect_telnet_flood(x))) {
  1283. putlog(LOG_DEBUG, "*", "Ignored telnet connection from: %s", x);
  1284. killsock(sock);
  1285. return;
  1286. }
  1287. /* If a matching bot ip is found, it might still be on the ignore list as a host,
  1288. * so we'll just reverse the ip anyway and check the ignores before
  1289. * proceeding with user matching
  1290. */
  1291. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1292. dcc[i].addr = ip;
  1293. dcc[i].sock = sock;
  1294. dcc[i].user = get_user_by_host(x); /* check for matching -telnet!telnet@ip */
  1295. strlcpy(dcc[i].host, s, sizeof(dcc[i].host));
  1296. #ifdef USE_IPV6
  1297. if (af_type == AF_INET6)
  1298. strlcpy(dcc[i].host6, s, sizeof(dcc[i].host6));
  1299. #endif /* USE_IPV6 */
  1300. dcc[i].port = port;
  1301. dcc[i].timeval = now;
  1302. strlcpy(dcc[i].nick, "*", sizeof(dcc[i].nick));
  1303. dcc[i].u.dns->ibuf = idx;
  1304. int dns_id = egg_dns_reverse(s, 20, dcc_telnet_dns_callback, (void *) (long) i);
  1305. if (dns_id >= 0)
  1306. dcc[i].dns_id = dns_id;
  1307. }
  1308. static void dcc_telnet_dns_callback(int id, void *client_data, const char *ip, bd::Array<bd::String> hosts)
  1309. {
  1310. // 64bit hacks
  1311. long data = (long) client_data;
  1312. int i = (int) data;
  1313. Context;
  1314. if (!valid_dns_id(i, id))
  1315. return;
  1316. int idx = dcc[i].u.dns->ibuf;
  1317. if (!valid_idx(idx)) {
  1318. putlog(LOG_BOTS, "*", "Lost listening socket while resolving %s", dcc[i].host);
  1319. killsock(dcc[i].sock);
  1320. lostdcc(i);
  1321. return;
  1322. }
  1323. //Reset timer
  1324. dcc[i].timeval = now;
  1325. //Clear the ip (still saved in dcc[i].addr)
  1326. dcc[i].host[0] = 0;
  1327. if (hosts.size()) {
  1328. strlcpy(dcc[i].host, bd::String(hosts[0]).c_str(), sizeof(dcc[i].host));
  1329. //Check forward; only check the protocol of which this connection is.
  1330. //If they connected on V4, lookup A, if V6, lookup AAAA
  1331. int dns_type = DNS_LOOKUP_A;
  1332. if (is_dotted_ip(iptostr(htonl(dcc[i].addr))) == AF_INET6)//Is this even valid?
  1333. dns_type = DNS_LOOKUP_AAAA;
  1334. int dns_id = egg_dns_lookup(bd::String(hosts[0]).c_str(), 20, dcc_telnet_dns_forward_callback, (void *) (long) i, dns_type);
  1335. if (dns_id >= 0)
  1336. dcc[i].dns_id = dns_id;
  1337. } else {
  1338. bd::Array<bd::String> empty;
  1339. dcc_telnet_dns_forward_callback(id, client_data, ip, empty);
  1340. }
  1341. }
  1342. static void dcc_telnet_dns_forward_callback(int id, void *client_data, const char *host, bd::Array<bd::String> ips) {
  1343. // 64bit hacks
  1344. long data = (long) client_data;
  1345. int i = (int) data;
  1346. Context;
  1347. if (!valid_dns_id(i, id))
  1348. return;
  1349. int j = -1, sock, idx = -1;
  1350. char s2[UHOSTLEN + 20] = "";
  1351. if (valid_idx(i))
  1352. idx = dcc[i].u.dns->ibuf;
  1353. if (!valid_idx(idx)) {
  1354. putlog(LOG_BOTS, "*", "Lost listening socket while resolving %s", iptostr(htonl(dcc[i].addr)));
  1355. killsock(dcc[i].sock);
  1356. lostdcc(i);
  1357. return;
  1358. }
  1359. bool forward_matched = false;
  1360. bd::String look_for_ip(iptostr(htonl(dcc[i].addr)));
  1361. // Look for any match
  1362. for (size_t n = 0; n < ips.size(); ++n) {
  1363. if (ips[n] == look_for_ip) {
  1364. forward_matched = true;
  1365. break;
  1366. }
  1367. }
  1368. // If the forward did not match, replace the saved host with the ip
  1369. if (!forward_matched)
  1370. strlcpy(dcc[i].host, iptostr(htonl(dcc[i].addr)), sizeof(dcc[i].host));
  1371. if (forward_matched) {
  1372. // Are they ignored by host?
  1373. simple_snprintf(s2, sizeof(s2), "-telnet!telnet@%s", dcc[i].host);
  1374. if (match_ignore(s2)) {
  1375. putlog(LOG_DEBUG, "*", "Ignored telnet connection from: %s", s2);
  1376. killsock(dcc[i].sock);
  1377. lostdcc(i);
  1378. return;
  1379. }
  1380. }
  1381. if (dcc[idx].host[0] == '@') {
  1382. /* Restrict by hostname */
  1383. if (!wild_match(dcc[idx].host + 1, dcc[i].host)) {
  1384. putlog(LOG_BOTS, "*", "Refused %s (bad hostname)", dcc[i].host);
  1385. killsock(dcc[i].sock);
  1386. lostdcc(i);
  1387. return;
  1388. }
  1389. }
  1390. changeover_dcc(i, &DCC_IDENTWAIT, 0);
  1391. dcc[i].timeval = now;
  1392. dcc[i].uint.ident_sock = dcc[idx].sock;
  1393. if (!dcc[i].user)
  1394. dcc[i].user = get_user_by_host(s2); /* check for matching -telnet!telnet@host */
  1395. if (forward_matched)
  1396. putlog(LOG_MISC, "*", "Telnet connection: %s[%s]/%d", dcc[i].host, iptostr(htonl(dcc[i].addr)), dcc[i].port);
  1397. else
  1398. putlog(LOG_MISC, "*", "Telnet connection: %s/%d", iptostr(htonl(dcc[i].addr)), dcc[i].port);
  1399. sock = open_telnet((char *) iptostr(htonl(dcc[i].addr)), 113, 0);
  1400. char s[UHOSTLEN] = "";
  1401. if (sock < 0) {
  1402. if (sock == -2)
  1403. strlcpy(s, "DNS lookup failed for ident", sizeof(s));
  1404. else
  1405. strlcpy(s, strerror(errno), sizeof(s));
  1406. } else {
  1407. j = new_dcc(&DCC_IDENT, 0);
  1408. if (j < 0) {
  1409. killsock(sock);
  1410. strlcpy(s, "No Free DCC's", sizeof(s));
  1411. }
  1412. }
  1413. if (s[0]) {
  1414. putlog(LOG_MISC, "*", "Ident failed for %s: %s", dcc[i].host, s);
  1415. simple_snprintf(s, sizeof(s), "telnet@%s", dcc[i].host);
  1416. dcc_telnet_got_ident(i, s);
  1417. return;
  1418. }
  1419. dcc[j].sock = sock;
  1420. dcc[j].port = 113;
  1421. dcc[j].addr = dcc[i].addr;
  1422. strlcpy(dcc[j].host, dcc[i].host, sizeof(dcc[j].host));
  1423. strlcpy(dcc[j].nick, "*", sizeof(dcc[j].nick));
  1424. dcc[j].uint.ident_sock = dcc[i].sock;
  1425. dcc[j].user = dcc[i].user;
  1426. dcc[j].timeval = now;
  1427. dprintf(j, "%d, %d\n", dcc[i].port, dcc[idx].port);
  1428. }
  1429. static void
  1430. eof_dcc_telnet(int idx)
  1431. {
  1432. putlog(LOG_MISC, "*", "(!) Listening port %d abruptly died.", dcc[idx].port);
  1433. killsock(dcc[idx].sock);
  1434. lostdcc(idx);
  1435. }
  1436. static void
  1437. display_telnet(int idx, char *buf, size_t bufsiz)
  1438. {
  1439. simple_snprintf(buf, bufsiz, "lstn %d%s", dcc[idx].port, (dcc[idx].status & LSTN_PUBLIC) ? " pub" : "");
  1440. }
  1441. struct dcc_table DCC_TELNET = {
  1442. "TELNET",
  1443. DCT_LISTEN,
  1444. eof_dcc_telnet,
  1445. dcc_telnet,
  1446. NULL,
  1447. NULL,
  1448. display_telnet,
  1449. NULL,
  1450. NULL,
  1451. NULL
  1452. };
  1453. static void
  1454. eof_dcc_dupwait(int idx)
  1455. {
  1456. putlog(LOG_BOTS, "*", "Lost telnet connection from %s while checking for duplicate", dcc[idx].host);
  1457. killsock(dcc[idx].sock);
  1458. lostdcc(idx);
  1459. }
  1460. static void
  1461. dcc_dupwait(int idx, char *buf, int i)
  1462. {
  1463. /* We just ignore any data at this point. */
  1464. return;
  1465. }
  1466. /* We now check again. If the bot is still marked as duplicate, there is no
  1467. * botnet lag we could push it on, so we just drop the connection.
  1468. */
  1469. static void
  1470. timeout_dupwait(int idx)
  1471. {
  1472. /* Still duplicate? */
  1473. if (in_chain(dcc[idx].nick)) {
  1474. char x[UHOSTLEN] = "";
  1475. simple_snprintf(x, sizeof x, "%s!%s", dcc[idx].nick, dcc[idx].host);
  1476. putlog(LOG_BOTS, "*", "Refused telnet connection from %s (duplicate)", x);
  1477. killsock(dcc[idx].sock);
  1478. lostdcc(idx);
  1479. } else {
  1480. /* Ha! Now it's gone and we can grant this bot access. */
  1481. dcc_telnet_pass(idx, dcc[idx].u.dupwait->atr);
  1482. }
  1483. }
  1484. static void
  1485. display_dupwait(int idx, char *buf, size_t bufsiz)
  1486. {
  1487. simple_snprintf(buf, bufsiz, "wait duplicate?");
  1488. }
  1489. static void
  1490. kill_dupwait(int idx, void *x)
  1491. {
  1492. register struct dupwait_info *p = (struct dupwait_info *) x;
  1493. if (p)
  1494. free(p);
  1495. }
  1496. struct dcc_table DCC_DUPWAIT = {
  1497. "DUPWAIT",
  1498. DCT_VALIDIDX,
  1499. eof_dcc_dupwait,
  1500. dcc_dupwait,
  1501. &dupwait_timeout,
  1502. timeout_dupwait,
  1503. display_dupwait,
  1504. kill_dupwait,
  1505. NULL,
  1506. NULL
  1507. };
  1508. /* This function is called if a bot gets removed from the list. It checks
  1509. * wether we have a pending duplicate connection for that bot and continues
  1510. * with the login in that case.
  1511. */
  1512. void
  1513. dupwait_notify(const char *who)
  1514. {
  1515. for (register int idx = 0; idx < dcc_total; idx++)
  1516. if (dcc[idx].type && (dcc[idx].type == &DCC_DUPWAIT) && !strcasecmp(dcc[idx].nick, who)) {
  1517. dcc_telnet_pass(idx, dcc[idx].u.dupwait->atr);
  1518. break;
  1519. }
  1520. }
  1521. static void
  1522. dcc_telnet_id(int idx, char *buf, int atr)
  1523. {
  1524. char *nick = buf;
  1525. strip_telnet(dcc[idx].sock, nick, &atr);
  1526. if (nick[0] == '-') {
  1527. nick++;
  1528. dcc[idx].bot = 1;
  1529. dcc[idx].encrypt = 1;
  1530. } else if (nick[0] == '+') {
  1531. nick++;
  1532. dcc[idx].encrypt = 1;
  1533. }
  1534. nick[HANDLEN] = 0; // Trim to handle length before looking up user.
  1535. dcc[idx].user = get_user_by_handle(userlist, nick);
  1536. bool ok = 0;
  1537. if (dcc[idx].user) {
  1538. if (dcc[idx].bot != dcc[idx].user->bot) {
  1539. putlog(LOG_WARN, "*", "Refused %s (fake bot login for '%s')", dcc[idx].host, nick);
  1540. killsock(dcc[idx].sock);
  1541. lostdcc(idx);
  1542. return;
  1543. }
  1544. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  1545. get_user_flagrec(dcc[idx].user, &fr, NULL);
  1546. ok = 1;
  1547. if (conf.bot->hub && !glob_huba(fr))
  1548. ok = 0;
  1549. if (!conf.bot->hub) {
  1550. /* if I am a chanhub and they dont have +c then drop */
  1551. if (ischanhub() && !glob_chuba(fr))
  1552. ok = 0;
  1553. if (!ischanhub())
  1554. ok = 0;
  1555. }
  1556. if (!ok && glob_bot(fr))
  1557. ok = 1;
  1558. }
  1559. if (!ok) {
  1560. if (dcc[idx].user)
  1561. 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);
  1562. else if (dcc[idx].bot)
  1563. 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);
  1564. else
  1565. putlog(LOG_BOTS, "*", "Refused %s (invalid handle: %s)", dcc[idx].host, nick);
  1566. killsock(dcc[idx].sock);
  1567. lostdcc(idx);
  1568. return;
  1569. }
  1570. correct_handle(nick);
  1571. strlcpy(dcc[idx].nick, nick, sizeof(dcc[idx].nick));
  1572. if (!strcmp(dcc[idx].host, "UNKNOWN@localhost"))
  1573. simple_snprintf(dcc[idx].host, sizeof(dcc[idx].host), "%s@localhost", nick);
  1574. if (dcc[idx].user->bot) {
  1575. if (!strcasecmp(conf.bot->nick, dcc[idx].nick)) {
  1576. putlog(LOG_BOTS, "*", "Refused telnet connection from %s (tried using my botnetnick)", dcc[idx].host);
  1577. killsock(dcc[idx].sock);
  1578. lostdcc(idx);
  1579. return;
  1580. } else if (in_chain(dcc[idx].nick)) {
  1581. dcc[idx].type = &DCC_DUPWAIT;
  1582. dcc[idx].u.dupwait = (struct dupwait_info *) my_calloc(1, sizeof(struct dupwait_info));
  1583. dcc[idx].u.dupwait->atr = atr;
  1584. return;
  1585. }
  1586. // dcc[idx].u.enc = (struct enc_link_dcc *) my_calloc(1, sizeof(struct enc_link_dcc));
  1587. // dcc[idx].u.enc->method_number = 0;
  1588. // link_get_method(idx);
  1589. } else {
  1590. }
  1591. if (dcc[idx].bot && !(dcc[idx].status & STAT_UNIXDOMAIN)) {
  1592. char shost[UHOSTLEN + 20] = "", sip[UHOSTLEN + 20] = "", user[30] = "";
  1593. simple_snprintf(shost, sizeof(shost), "-telnet!%s", dcc[idx].host);
  1594. char *p = strchr(dcc[idx].host, '@');
  1595. strlcpy(user, dcc[idx].host, p - dcc[idx].host + 1);
  1596. simple_snprintf(sip, sizeof(sip), "-telnet!%s@%s", user, iptostr(htonl(dcc[idx].addr)));
  1597. struct userrec *u = get_user_by_handle(userlist, nick);
  1598. ok = 1;
  1599. // Require that the linking bot has a matching host
  1600. if (u) {
  1601. /* Check for -telnet!ident@ip or -telnet!ident@host */
  1602. if (!user_has_matching_host(nick, u, sip) && !user_has_matching_host(nick, u, shost)) {
  1603. ok = 0;
  1604. }
  1605. } else {
  1606. ok = 0;
  1607. }
  1608. if (!ok) {
  1609. putlog(LOG_BOTS, "*", "Denied link to '%s': Host not recognized: %s", nick, dcc[idx].host);
  1610. putlog(LOG_BOTS, "*", "If this host/bot is trusted: %s+host %s %s", settings.dcc_prefix, nick, shost);
  1611. killsock(dcc[idx].sock);
  1612. lostdcc(idx);
  1613. return;
  1614. }
  1615. }
  1616. dcc_telnet_pass(idx, atr);
  1617. }
  1618. static void
  1619. dcc_telnet_pass(int idx, int atr)
  1620. {
  1621. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  1622. get_user_flagrec(dcc[idx].user, &fr, NULL);
  1623. /* No password set? */
  1624. if (!dcc[idx].user->bot && (u_pass_match(dcc[idx].user, "-"))) {
  1625. dprintf(idx, "Can't telnet until you have a password set.\r\n");
  1626. putlog(LOG_MISC, "*", "Refused [%s]%s (no password)", dcc[idx].nick, dcc[idx].host);
  1627. killsock(dcc[idx].sock);
  1628. lostdcc(idx);
  1629. return;
  1630. }
  1631. if (dcc[idx].type == &DCC_DUPWAIT) {
  1632. free(dcc[idx].u.dupwait);
  1633. dcc[idx].u.other = NULL;
  1634. }
  1635. dcc[idx].type = &DCC_CHAT_PASS;
  1636. dcc[idx].timeval = now;
  1637. if ((conf.bot->hub && !glob_huba(fr)) || (!conf.bot->hub && ischanhub() && !glob_chuba(fr)))
  1638. dcc[idx].status |= STAT_PARTY;
  1639. if (!dcc[idx].bot) {
  1640. //bots dont need this
  1641. dcc[idx].u.chat = (struct chat_info *) my_calloc(1, sizeof(struct chat_info));
  1642. struct chat_info dummy;
  1643. strlcpy(dcc[idx].u.chat->con_chan, chanset ? chanset->dname : "*", sizeof(dummy.con_chan));
  1644. }
  1645. if (conf.bot->hub || (conf.bot->localhub && (dcc[idx].status & STAT_UNIXDOMAIN))) {
  1646. if (dcc[idx].encrypt) {
  1647. /* negotiate a new linking scheme */
  1648. int i = 0;
  1649. char buf[1024] = "", rand[51] = "";
  1650. make_rand_str(rand, 50);
  1651. link_hash(idx, rand);
  1652. for (i = 0; enclink[i].name; i++) {
  1653. if (enclink[i].type == LINK_CLEARTEXT && !link_cleartext) continue;
  1654. simple_snprintf(&buf[strlen(buf)], sizeof(buf) - strlen(buf), "%d ", enclink[i].type);
  1655. }
  1656. dprintf(-dcc[idx].sock, "neg? %s %s\n", rand, buf);
  1657. } else {
  1658. /* Turn off remote telnet echo (send IAC WILL ECHO). */
  1659. dprintf(idx, "\n%s" TLN_IAC_C TLN_WILL_C TLN_ECHO_C "\n", "Enter your password");
  1660. }
  1661. } else
  1662. dprintf(idx, "%s\n" TLN_IAC_C TLN_WILL_C TLN_ECHO_C, response(RES_PASSWORD));
  1663. }
  1664. static void
  1665. eof_dcc_telnet_id(int idx)
  1666. {
  1667. if (dcc[idx].port)
  1668. putlog(LOG_MISC, "*", "Lost telnet connection to %s/%d", dcc[idx].host, dcc[idx].port);
  1669. else
  1670. putlog(LOG_MISC, "*", "Lost local connection for: %s", dcc[idx].host);
  1671. killsock(dcc[idx].sock);
  1672. lostdcc(idx);
  1673. }
  1674. static void
  1675. timeout_dcc_telnet_id(int idx)
  1676. {
  1677. putlog(LOG_MISC, "*", "Ident timeout on telnet: %s", dcc[idx].host);
  1678. killsock(dcc[idx].sock);
  1679. lostdcc(idx);
  1680. }
  1681. static void
  1682. display_dcc_telnet_id(int idx, char *buf, size_t bufsiz)
  1683. {
  1684. simple_snprintf(buf, bufsiz, "t-in waited %ds", (int) (now - dcc[idx].timeval));
  1685. }
  1686. struct dcc_table DCC_TELNET_ID = {
  1687. "TELNET_ID",
  1688. 0,
  1689. eof_dcc_telnet_id,
  1690. dcc_telnet_id,
  1691. &password_timeout,
  1692. timeout_dcc_telnet_id,
  1693. display_dcc_telnet_id,
  1694. NULL,
  1695. out_dcc_general,
  1696. NULL
  1697. };
  1698. static void
  1699. dcc_socket(int idx, char *buf, int len)
  1700. {
  1701. }
  1702. static void
  1703. eof_dcc_socket(int idx)
  1704. {
  1705. killsock(dcc[idx].sock);
  1706. lostdcc(idx);
  1707. }
  1708. static void
  1709. display_dcc_socket(int idx, char *buf, size_t bufsiz)
  1710. {
  1711. strlcpy(buf, "sock (stranded)", bufsiz);
  1712. }
  1713. struct dcc_table DCC_SOCKET = {
  1714. "SOCKET",
  1715. DCT_VALIDIDX,
  1716. eof_dcc_socket,
  1717. dcc_socket,
  1718. NULL,
  1719. NULL,
  1720. display_dcc_socket,
  1721. NULL,
  1722. NULL,
  1723. NULL
  1724. };
  1725. void
  1726. dcc_identwait(int idx, char *buf, int len)
  1727. {
  1728. /* Ignore anything now */
  1729. }
  1730. void
  1731. eof_dcc_identwait(int idx)
  1732. {
  1733. putlog(LOG_MISC, "*", "Lost connection while identing [%s/%d]", dcc[idx].host, dcc[idx].port);
  1734. for (int i = 0; i < dcc_total; i++)
  1735. if (dcc[i].type && (dcc[i].type == &DCC_IDENT) && (dcc[i].uint.ident_sock == dcc[idx].sock)) {
  1736. killsock(dcc[i].sock); /* Cleanup ident socket */
  1737. dcc[i].u.other = 0;
  1738. lostdcc(i);
  1739. break;
  1740. }
  1741. killsock(dcc[idx].sock); /* Cleanup waiting socket */
  1742. dcc[idx].u.other = 0;
  1743. lostdcc(idx);
  1744. }
  1745. static void
  1746. display_dcc_identwait(int idx, char *buf, size_t bufsiz)
  1747. {
  1748. simple_snprintf(buf, bufsiz, "idtw waited %ds", (int) (now - dcc[idx].timeval));
  1749. }
  1750. struct dcc_table DCC_IDENTWAIT = {
  1751. "IDENTWAIT",
  1752. 0,
  1753. eof_dcc_identwait,
  1754. dcc_identwait,
  1755. NULL,
  1756. NULL,
  1757. display_dcc_identwait,
  1758. NULL,
  1759. NULL,
  1760. NULL
  1761. };
  1762. void
  1763. dcc_ident(int idx, char *buf, int len)
  1764. {
  1765. char ident_response[512] = "", uid[512] = "", buf1[UHOSTLEN] = "";
  1766. sscanf(buf, "%*[^:]:%[^:]:%*[^:]:%[^\n]\n", ident_response, uid);
  1767. rmspace(ident_response);
  1768. if (ident_response[0] != 'U') {
  1769. dcc[idx].timeval = now;
  1770. return;
  1771. }
  1772. rmspace(uid);
  1773. uid[20] = 0; /* 20 character ident max */
  1774. for (int i = 0; i < dcc_total; i++)
  1775. if (dcc[i].type && (dcc[i].type == &DCC_IDENTWAIT) && (dcc[i].sock == dcc[idx].uint.ident_sock)) {
  1776. simple_snprintf(buf1, sizeof(buf1), "%s@%s", uid, dcc[idx].host);
  1777. dcc_telnet_got_ident(i, buf1);
  1778. }
  1779. dcc[idx].u.other = 0;
  1780. killsock(dcc[idx].sock);
  1781. lostdcc(idx);
  1782. }
  1783. void
  1784. eof_dcc_ident(int idx)
  1785. {
  1786. char buf[UHOSTLEN] = "";
  1787. for (int i = 0; i < dcc_total; i++)
  1788. if (dcc[i].type && (dcc[i].type == &DCC_IDENTWAIT) && (dcc[i].sock == dcc[idx].uint.ident_sock)) {
  1789. putlog(LOG_MISC, "*", "Timeout/EOF ident connection");
  1790. simple_snprintf(buf, sizeof(buf), "telnet@%s", dcc[idx].host);
  1791. dcc_telnet_got_ident(i, buf);
  1792. }
  1793. killsock(dcc[idx].sock);
  1794. dcc[idx].u.other = 0;
  1795. lostdcc(idx);
  1796. }
  1797. static void
  1798. display_dcc_ident(int idx, char *buf, size_t bufsiz)
  1799. {
  1800. simple_snprintf(buf, bufsiz, "idnt (sock %d)", dcc[idx].uint.ident_sock);
  1801. }
  1802. struct dcc_table DCC_IDENT = {
  1803. "IDENT",
  1804. 0,
  1805. eof_dcc_ident,
  1806. dcc_ident,
  1807. &identtimeout,
  1808. eof_dcc_ident,
  1809. display_dcc_ident,
  1810. NULL,
  1811. NULL,
  1812. NULL
  1813. };
  1814. static void
  1815. dcc_telnet_got_ident(int i, char *host)
  1816. {
  1817. int idx;
  1818. for (idx = 0; idx < dcc_total; idx++)
  1819. if (dcc[i].type && (dcc[idx].type == &DCC_TELNET) && (dcc[idx].sock == dcc[i].uint.ident_sock))
  1820. break;
  1821. dcc[i].u.other = 0;
  1822. if (idx == dcc_total || !dcc[idx].type) {
  1823. putlog(LOG_MISC, "*", "Lost ident wait telnet socket!!");
  1824. killsock(dcc[i].sock);
  1825. lostdcc(i);
  1826. return;
  1827. }
  1828. strlcpy(dcc[i].host, host, sizeof(dcc[i].host));
  1829. bool unix_domain = 0;
  1830. if (!strcmp(host, "UNKNOWN@localhost"))
  1831. unix_domain = 1;
  1832. if (!unix_domain) {
  1833. char shost[UHOSTLEN + 20] = "", sip[UHOSTLEN + 20] = "";
  1834. char *p = strchr(host, '@');
  1835. *p = 0;
  1836. simple_snprintf(shost, sizeof(shost), "-telnet!%s", dcc[i].host);
  1837. simple_snprintf(sip, sizeof(sip), "-telnet!%s@%s", host, iptostr(htonl(dcc[i].addr)));
  1838. if (match_ignore(shost) || match_ignore(sip)) {
  1839. putlog(LOG_DEBUG, "*", "Ignored telnet connection from: %s[%s]",dcc[i].host, iptostr(htonl(dcc[i].addr)));
  1840. killsock(dcc[i].sock);
  1841. lostdcc(i);
  1842. return;
  1843. }
  1844. if (protect_telnet) {
  1845. struct userrec *u = NULL;
  1846. bool ok = 1;
  1847. u = dcc[i].user;
  1848. if (!u)
  1849. u = get_user_by_host(sip); /* Check for -telnet!ident@ip */
  1850. if (!u)
  1851. u = get_user_by_host(shost); /* Check for -telnet!ident@host */
  1852. if (!u)
  1853. ok = 0;
  1854. if (ok && u && conf.bot->hub && !(u->flags & USER_HUBA))
  1855. ok = 0;
  1856. /* if I am a chanhub and they dont have +c then drop */
  1857. if (ok && (!conf.bot->hub && ischanhub() && u && !(u->flags & USER_CHUBA)))
  1858. ok = 0;
  1859. /* else if (!(u->flags & USER_PARTY))
  1860. ok = 0; */
  1861. if (!ok && u && u->bot)
  1862. ok = 1;
  1863. if (!ok && (dcc[idx].status & LSTN_PUBLIC))
  1864. ok = 1;
  1865. if (!ok) {
  1866. putlog(LOG_MISC, "*", "Denied telnet: %s, No Access", dcc[i].host);
  1867. killsock(dcc[i].sock);
  1868. lostdcc(i);
  1869. return;
  1870. }
  1871. }
  1872. }
  1873. /* Do not buffer data anymore. All received and stored data is passed
  1874. * over to the dcc functions from now on. */
  1875. sockoptions(dcc[i].sock, EGG_OPTION_UNSET, SOCK_BUFFER);
  1876. dcc[i].type = &DCC_TELNET_ID;
  1877. /* Copy acceptable-nick/host mask */
  1878. dcc[i].status = (STAT_TELNET | STAT_ECHO | STAT_COLOR | STAT_BANNER | STAT_CHANNELS | STAT_BOTS | STAT_WHOM);
  1879. if (unix_domain)
  1880. dcc[i].status |= STAT_UNIXDOMAIN;
  1881. /* Copy acceptable-nick/host mask */
  1882. strlcpy(dcc[i].nick, dcc[idx].host, sizeof(dcc[i].nick));
  1883. dcc[i].timeval = now;
  1884. dcc[i].u.other = NULL;
  1885. /* This is so we dont tell someone doing a portscan anything
  1886. * about ourselves. <cybah>
  1887. */
  1888. if (conf.bot->hub || (conf.bot->localhub && unix_domain))
  1889. dprintf(i, " \n"); /* represents hub that support new linking scheme */
  1890. else
  1891. dprintf(i, "%s\n", response(RES_USERNAME));
  1892. }