dcc.c 47 KB

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