dcc.c 48 KB

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