dcc.c 58 KB

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