dcc.c 48 KB

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