dcc.cc 59 KB

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