dcc.c 53 KB

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