dcc.c 47 KB

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