dcc.c 48 KB

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