dcc.c 50 KB

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