dcc.c 47 KB

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