dcc.c 49 KB

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