dcc.c 49 KB

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