dcc.c 48 KB

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