dcc.c 47 KB

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