dcc.c 52 KB

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