dcc.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  1. /*
  2. * dcc.c -- handles:
  3. * activity on a dcc socket
  4. * disconnect on a dcc socket
  5. * ...and that's it! (but it's a LOT)
  6. *
  7. */
  8. #include "common.h"
  9. #include "dcc.h"
  10. #include "settings.h"
  11. #include "tclhash.h"
  12. #include "main.h"
  13. #include "cmds.h"
  14. #include "color.h"
  15. #include "net.h"
  16. #include "response.h"
  17. #include "misc.h"
  18. #include "users.h"
  19. #include "userrec.h"
  20. #include "userent.h"
  21. #include "match.h"
  22. #include "dns.h"
  23. #include "auth.h"
  24. #include "dccutil.h"
  25. #include "crypt.h"
  26. #include "chanprog.h"
  27. #include "botmsg.h"
  28. #include "botcmd.h"
  29. #include "botnet.h"
  30. #include <ctype.h>
  31. #include <errno.h>
  32. #include <sys/types.h>
  33. #include <sys/socket.h>
  34. #include <netinet/in.h>
  35. #include <sys/utsname.h>
  36. #include <sys/stat.h>
  37. #include "tandem.h"
  38. #include "core_binds.h"
  39. struct dcc_t *dcc = NULL; /* DCC list */
  40. time_t timesync = 0;
  41. int dcc_total = 0; /* Total dcc's */
  42. static int password_timeout = 20; /* Time to wait for a password from a user */
  43. static int auth_timeout = 40;
  44. static int bot_timeout = 15; /* Bot timeout value */
  45. static int identtimeout = 15; /* Timeout value for ident lookups */
  46. static int dupwait_timeout = 5; /* Timeout for rejecting duplicate entries */
  47. #ifdef LEAF
  48. static int protect_telnet = 0; /* Even bother with ident lookups :) */
  49. #else /* !LEAF */
  50. static int protect_telnet = 1;
  51. #endif /* LEAF */
  52. static int flood_telnet_thr = 10; /* Number of telnet connections to be
  53. * considered a flood */
  54. static int flood_telnet_time = 5; /* In how many seconds? */
  55. static void dcc_telnet_hostresolved(int);
  56. static void dcc_telnet_got_ident(int, char *);
  57. static void dcc_telnet_pass(int, int);
  58. static void
  59. strip_telnet(int sock, char *buf, int *len)
  60. {
  61. unsigned char *p = (unsigned char *) buf, *o = (unsigned char *) buf;
  62. int mark;
  63. while (*p != 0) {
  64. while ((*p != TLN_IAC) && (*p != 0)) {
  65. if (*p == 0xA0) {
  66. *o++ = 32;
  67. p++;
  68. } else
  69. *o++ = *p++;
  70. }
  71. if (*p == TLN_IAC) {
  72. p++;
  73. mark = 2;
  74. if (!*p)
  75. mark = 1; /* bogus */
  76. if ((*p >= TLN_WILL) && (*p <= TLN_DONT)) {
  77. mark = 3;
  78. if (!*(p + 1))
  79. mark = 2; /* bogus */
  80. }
  81. if (*p == TLN_WILL) {
  82. /* WILL X -> response: DONT X */
  83. /* except WILL ECHO which we just smile and ignore */
  84. if (*(p + 1) != TLN_ECHO) {
  85. write(sock, TLN_IAC_C TLN_DONT_C, 2);
  86. write(sock, p + 1, 1);
  87. }
  88. }
  89. if (*p == TLN_DO) {
  90. /* DO X -> response: WONT X */
  91. /* except DO ECHO which we just smile and ignore */
  92. if (*(p + 1) != TLN_ECHO) {
  93. write(sock, TLN_IAC_C TLN_WONT_C, 2);
  94. write(sock, p + 1, 1);
  95. }
  96. }
  97. if (*p == TLN_AYT) {
  98. /* "are you there?" */
  99. /* response is: "hell yes!" */
  100. write(sock, "\r\nHell, yes!\r\n", 14);
  101. }
  102. /* Anything else can probably be ignored */
  103. p += mark - 1;
  104. *len = *len - mark;
  105. }
  106. }
  107. *o = *p;
  108. }
  109. #ifdef HUB
  110. void
  111. send_timesync(int idx)
  112. {
  113. /* Send timesync to idx, or all lower bots if idx<0 */
  114. if (idx >= 0)
  115. dprintf(idx, "ts %li\n", timesync + now);
  116. else {
  117. char s[15] = "";
  118. int i;
  119. sprintf(s, "ts %li\n", timesync + now);
  120. for (i = 0; i < dcc_total; i++) {
  121. if ((dcc[i].type == &DCC_BOT) && (bot_aggressive_to(dcc[i].user))) {
  122. dprintf(i, s);
  123. }
  124. }
  125. }
  126. }
  127. #endif /* HUB */
  128. static void
  129. greet_new_bot(int idx)
  130. {
  131. int i;
  132. char *sysname = NULL;
  133. struct utsname un;
  134. dcc[idx].timeval = now;
  135. dcc[idx].u.bot->version[0] = 0;
  136. dcc[idx].u.bot->sysname[0] = 0;
  137. dcc[idx].u.bot->numver = 0;
  138. #ifdef HUB
  139. if (dcc[idx].user && (!(dcc[idx].user->flags & USER_OP))) {
  140. putlog(LOG_BOTS, "*", "Rejecting link from %s", dcc[idx].nick);
  141. dprintf(idx, "error You are being rejected.\n");
  142. dprintf(idx, "bye\n");
  143. killsock(dcc[idx].sock);
  144. lostdcc(idx);
  145. return;
  146. }
  147. #endif /* HUB */
  148. if (bot_hublevel(dcc[idx].user) == 999)
  149. dcc[idx].status |= STAT_LEAF;
  150. dcc[idx].status |= STAT_LINKING;
  151. if (uname(&un) < 0)
  152. sysname = "*";
  153. else
  154. sysname = un.sysname;
  155. dprintf(idx, "v 1001500 %d Wraith %s <%s> %d %li %s\n", HANDLEN, egg_version, "-", localhub, buildts, egg_version);
  156. dprintf(idx, "si %s %s %s", conf.username ? conf.username : "*", un.sysname ? un.sysname : "*",
  157. un.nodename ? un.nodename : "*");
  158. for (i = 0; i < dcc_total; i++) {
  159. if (dcc[i].type == &DCC_FORK_BOT) {
  160. killsock(dcc[i].sock);
  161. lostdcc(i);
  162. }
  163. }
  164. }
  165. static void
  166. bot_version(int idx, char *par)
  167. {
  168. char x[1024] = "", *vversion = NULL;
  169. int l, vlocalhub = 0;
  170. time_t vbuildts = 0;
  171. dcc[idx].timeval = now;
  172. if (in_chain(dcc[idx].nick)) {
  173. dprintf(idx, "error Sorry, already connected.\n");
  174. dprintf(idx, "bye\n");
  175. killsock(dcc[idx].sock);
  176. lostdcc(idx);
  177. return;
  178. }
  179. if ((par[0] >= '0') && (par[0] <= '9')) {
  180. char *work = NULL;
  181. work = newsplit(&par);
  182. dcc[idx].u.bot->numver = atoi(work);
  183. /* old numver crap */
  184. } else
  185. dcc[idx].u.bot->numver = 0;
  186. dprintf(idx, "tb %s\n", conf.bot->nick);
  187. l = atoi(newsplit(&par));
  188. if (l != HANDLEN) {
  189. putlog(LOG_BOTS, "*", "Non-matching handle lengths with %s, they use %d characters.", dcc[idx].nick, l);
  190. dprintf(idx, "error Non-matching handle length: mine %d, yours %d\n", HANDLEN, l);
  191. dprintf(idx, "bye %s\n", "bad handlen");
  192. killsock(dcc[idx].sock);
  193. lostdcc(idx);
  194. return;
  195. }
  196. strncpyz(dcc[idx].u.bot->version, par, 120);
  197. newsplit(&par); /* 'ver' */
  198. newsplit(&par); /* handlen */
  199. newsplit(&par); /* network */
  200. if (par[0])
  201. vlocalhub = atoi(newsplit(&par));
  202. if (par[0])
  203. vbuildts = atol(newsplit(&par));
  204. if (par[0])
  205. vversion = newsplit(&par);
  206. #ifdef HUB
  207. putlog(LOG_BOTS, "*", DCC_LINKED, dcc[idx].nick);
  208. chatout("*** Linked to %s\n", dcc[idx].nick);
  209. #else /* !HUB */
  210. putlog(LOG_BOTS, "*", "Linked to botnet.");
  211. chatout("*** Linked to botnet.\n");
  212. #endif /* HUB */
  213. #ifdef HUB
  214. botnet_send_nlinked(idx, dcc[idx].nick, conf.bot->nick, '!', vlocalhub, vbuildts, vversion);
  215. #endif /* HUB */
  216. dump_links(idx);
  217. touch_laston(dcc[idx].user, "linked", now);
  218. dcc[idx].type = &DCC_BOT;
  219. addbot(dcc[idx].nick, dcc[idx].nick, conf.bot->nick, '-', vlocalhub, vbuildts, vversion);
  220. egg_snprintf(x, sizeof x, "v 1001500");
  221. bot_shareupdate(idx, x);
  222. bot_share(idx, x);
  223. dprintf(idx, "el\n");
  224. }
  225. void
  226. failed_link(int idx)
  227. {
  228. char s[81] = "", s1[512] = "";
  229. if (dcc[idx].u.bot->linker[0]) {
  230. egg_snprintf(s, sizeof s, "Couldn't link to %s.", dcc[idx].nick);
  231. strcpy(s1, dcc[idx].u.bot->linker);
  232. add_note(s1, conf.bot->nick, s, -2, 0);
  233. }
  234. if (dcc[idx].u.bot->numver >= (-1))
  235. putlog(LOG_BOTS, "*", DCC_LINKFAIL, dcc[idx].nick);
  236. if (dcc[idx].sock != -1) {
  237. int i = 0;
  238. for (i = 0; i < MAXSOCKS; i++)
  239. if (dcc[idx].sock == socklist[i].sock) {
  240. killsock(dcc[idx].sock);
  241. dcc[idx].sock = -1;
  242. }
  243. }
  244. strcpy(s, dcc[idx].nick);
  245. lostdcc(idx);
  246. autolink_cycle(s); /* Check for more auto-connections */
  247. }
  248. static void
  249. cont_link(int idx, char *buf, int ii)
  250. {
  251. /* Now set the initial link key (incoming only, we're not sending more until we get an OK)... */
  252. struct sockaddr_in sa;
  253. char tmp[301] = "";
  254. int i, snum = -1;
  255. socklen_t socklen;
  256. for (i = 0; i < MAXSOCKS; i++) {
  257. if ((socklist[i].sock == dcc[idx].sock) && !(socklist[i].flags & SOCK_UNUSED)) {
  258. snum = i;
  259. }
  260. }
  261. if (snum >= 0) {
  262. /* If we're already connected somewhere, unlink and idle a sec */
  263. for (i = 0; i < dcc_total; i++) {
  264. if ((dcc[i].type == &DCC_BOT) && (!bot_aggressive_to(dcc[i].user))) {
  265. putlog(LOG_BOTS, "*", "Unlinking %s - restructure", dcc[i].nick);
  266. botnet_send_unlinked(i, dcc[i].nick, "Restructure");
  267. killsock(dcc[i].sock);
  268. lostdcc(i);
  269. usleep(1000 * 500);
  270. break;
  271. }
  272. }
  273. /*. ssl_link(dcc[idx].sock, CONNECT_SSL); */
  274. dcc[idx].type = &DCC_BOT_NEW;
  275. dcc[idx].u.bot->numver = 0;
  276. dprintf(idx, "%s\n", conf.bot->nick);
  277. socklen = sizeof(sa);
  278. /* initkey-gen leaf */
  279. /* bdhash myport hubnick mynick */
  280. getsockname(socklist[snum].sock, (struct sockaddr *) &sa, &socklen);
  281. egg_snprintf(tmp, sizeof tmp, "%s@%4x@%s@%s", settings.bdhash, sa.sin_port, dcc[idx].nick, conf.bot->nick);
  282. strncpyz(socklist[snum].ikey, SHA1(tmp), sizeof(socklist[snum].ikey));
  283. /*
  284. * putlog(LOG_DEBUG, "@", "Link hash for %s: %s", dcc[idx].nick, tmp);
  285. * putlog(LOG_DEBUG, "@", "initkey (%d): %s", strlen(socklist[snum].ikey), socklist[snum].ikey);
  286. */
  287. /* We've send our conf.bot->nick and set the key for the link on the sock, wait for 'elink' back to verify key */
  288. socklist[snum].encstatus = 1;
  289. socklist[snum].gz = 1;
  290. } else {
  291. /* FIXME: This seems unnecesary, we didnt find a socket yet we are killing one.. ?*/
  292. /* killsock(dcc[idx].sock); */
  293. lostdcc(idx);
  294. }
  295. return;
  296. }
  297. static void
  298. dcc_bot_new(int idx, char *buf, int x)
  299. {
  300. /* struct userrec *u = get_user_by_handle(userlist, dcc[idx].nick); */
  301. char *code = NULL;
  302. int i;
  303. strip_telnet(dcc[idx].sock, buf, &x);
  304. code = newsplit(&buf);
  305. if (!egg_strcasecmp(code, "goodbye!")) {
  306. greet_new_bot(idx);
  307. } else if (!egg_strcasecmp(code, "v")) {
  308. bot_version(idx, buf);
  309. } else if (!egg_strcasecmp(code, "elink")) {
  310. int snum = -1;
  311. /* putlog(LOG_DEBUG, "*", "Got elink: %s %s", code, buf); */
  312. /* Set the socket key and we're linked */
  313. for (i = 0; i < MAXSOCKS; i++) {
  314. if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == dcc[idx].sock)) {
  315. snum = i;
  316. break;
  317. }
  318. }
  319. if (snum >= 0) {
  320. char *tmp = NULL, *p = NULL;
  321. p = newsplit(&buf);
  322. tmp = decrypt_string(settings.salt2, p);
  323. strncpyz(socklist[snum].okey, tmp, sizeof(socklist[snum].okey));
  324. strncpyz(socklist[snum].ikey, socklist[snum].okey, sizeof(socklist[snum].ikey));
  325. socklist[snum].iseed = atoi(buf);
  326. socklist[snum].oseed = atoi(buf);
  327. dprintf(idx, "elinkdone\n");
  328. putlog(LOG_BOTS, "*", "Handshake with %s succeeded, we're linked.", dcc[idx].nick);
  329. free(tmp);
  330. }
  331. } else if (!egg_strcasecmp(code, "error")) {
  332. putlog(LOG_MISC, "*", "ERROR linking %s: %s", dcc[idx].nick, buf);
  333. killsock(dcc[idx].sock);
  334. lostdcc(idx);
  335. } else if (strcmp(code, "")) {
  336. /* Invalid password/digest on leaf */
  337. putlog(LOG_WARN, "*", "%s failed encrypted link handshake", dcc[idx].nick);
  338. killsock(dcc[idx].sock);
  339. lostdcc(idx);
  340. }
  341. /* Ignore otherwise */
  342. }
  343. static void
  344. eof_dcc_bot_new(int idx)
  345. {
  346. putlog(LOG_BOTS, "*", DCC_LOSTBOT, dcc[idx].nick);
  347. killsock(dcc[idx].sock);
  348. lostdcc(idx);
  349. }
  350. static void
  351. timeout_dcc_bot_new(int idx)
  352. {
  353. #ifdef LEAF
  354. putlog(LOG_BOTS, "*", "Timeout: bot link to %s", dcc[idx].nick);
  355. #else /* !LEAF */
  356. putlog(LOG_BOTS, "*", DCC_TIMEOUT, dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  357. #endif /* LEAF */
  358. killsock(dcc[idx].sock);
  359. lostdcc(idx);
  360. }
  361. static void
  362. display_dcc_bot_new(int idx, char *buf)
  363. {
  364. sprintf(buf, "bot* waited %lis", now - dcc[idx].timeval);
  365. }
  366. static void
  367. free_dcc_bot_(int n, void *x)
  368. {
  369. if (dcc[n].type == &DCC_BOT) {
  370. unvia(n, findbot(dcc[n].nick));
  371. rembot(dcc[n].nick);
  372. }
  373. free(x);
  374. }
  375. struct dcc_table DCC_BOT_NEW = {
  376. "BOT_NEW",
  377. 0,
  378. eof_dcc_bot_new,
  379. dcc_bot_new,
  380. &bot_timeout,
  381. timeout_dcc_bot_new,
  382. display_dcc_bot_new,
  383. free_dcc_bot_,
  384. NULL,
  385. NULL
  386. };
  387. /* Hash function for tandem bot commands */
  388. extern botcmd_t C_bot[];
  389. static void
  390. dcc_bot(int idx, char *code, int i)
  391. {
  392. char *msg = NULL;
  393. int f;
  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 (f = i = 0; C_bot[i].name && !f; i++) {
  410. int y = egg_strcasecmp(code, C_bot[i].name);
  411. if (!y) {
  412. /* Found a match */
  413. (C_bot[i].func) (idx, msg);
  414. f = 1;
  415. } else if (y < 0)
  416. return;
  417. }
  418. }
  419. static void
  420. eof_dcc_bot(int idx)
  421. {
  422. char x[1024] = "";
  423. int bots, users;
  424. bots = bots_in_subtree(findbot(dcc[idx].nick));
  425. users = users_in_subtree(findbot(dcc[idx].nick));
  426. egg_snprintf(x, sizeof x,
  427. "Lost bot: %s (lost %d bot%s and %d user%s)",
  428. dcc[idx].nick, bots, (bots != 1) ? "s" : "", users, (users != 1) ? "s" : "");
  429. putlog(LOG_BOTS, "*", "%s.", x);
  430. chatout("*** %s\n", x);
  431. botnet_send_unlinked(idx, dcc[idx].nick, x);
  432. killsock(dcc[idx].sock);
  433. lostdcc(idx);
  434. }
  435. static void
  436. display_dcc_bot(int idx, char *buf)
  437. {
  438. size_t i;
  439. 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].addr6[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. 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. egg_snprintf(outbuf, sizeof outbuf, "%s : USERID : UNIX : %s\n", buf, conf.bot->nick);
  493. tputs(dcc[idx].sock, outbuf, strlen(outbuf));
  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. 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. IP ip;
  525. port_t port;
  526. int j = 0, 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 (dccauth) {
  568. char check[MD5_HASH_LENGTH + 7] = "";
  569. if (!atr)
  570. return;
  571. strip_telnet(dcc[idx].sock, buf, &atr);
  572. atr = dcc[idx].user ? dcc[idx].user->flags : 0;
  573. egg_snprintf(check, sizeof check, "+Auth %s", dcc[idx].hash);
  574. badauth = strcmp(check, buf);
  575. /* +secpass */
  576. }
  577. /* Correct pass or secpass! */
  578. if (!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[17] = "";
  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, 16);
  596. set_user(&USERENTRY_SECPASS, dcc[idx].user, pass);
  597. #ifdef HUB
  598. write_userfile(idx);
  599. #endif /* HUB */
  600. dprintf(idx, "Your secpass is now: %s%s%s\n", pass, BOLD(-1), BOLD_END(-1));
  601. dprintf(idx, "Make sure you do not lose this, as it is needed to login for now on.\n \n");
  602. dprintf(idx, "********************************************************************\n");
  603. }
  604. dcc_chatter(idx);
  605. } else if (dccauth && badauth) { /* bad auth */
  606. dprintf(idx, "%s\n", response(RES_BADUSERPASS));
  607. putlog(LOG_MISC, "*", DCC_BADAUTH, dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  608. if (dcc[idx].u.chat->away) { /* su from a dumb user */
  609. /* Turn echo back on for telnet sessions (send IAC WON'T ECHO). */
  610. if (dcc[idx].status & STAT_TELNET)
  611. dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  612. dcc[idx].user = get_user_by_handle(userlist, dcc[idx].u.chat->away);
  613. strcpy(dcc[idx].nick, dcc[idx].u.chat->away);
  614. free(dcc[idx].u.chat->away);
  615. free(dcc[idx].u.chat->su_nick);
  616. dcc[idx].u.chat->away = NULL;
  617. dcc[idx].u.chat->su_nick = NULL;
  618. dcc[idx].type = &DCC_CHAT;
  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, *q;
  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. int l = strlen(line);
  752. struct msgq *p = NULL, *q = NULL;
  753. struct chat_info *c = (dcc[idx].type == &DCC_CHAT) ? dcc[idx].u.chat : dcc[idx].u.file->chat;
  754. if (c->current_lines > 1000) {
  755. /* They're probably trying to fill up the bot nuke the sods :) */
  756. for (p = c->buffer; p; p = q) {
  757. q = p->next;
  758. free(p->msg);
  759. free(p);
  760. }
  761. c->buffer = 0;
  762. dcc[idx].status &= ~STAT_PAGE;
  763. do_boot(idx, conf.bot->nick, "too many pages - senq full");
  764. return;
  765. }
  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 *) calloc(1, sizeof(struct msgq));
  776. p->len = l;
  777. p->msg = (char *) 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. register struct chat_info *p = (struct chat_info *) x;
  790. char *y = buf;
  791. strip_mirc_codes(p->strip_flags, buf);
  792. if (dcc[idx].status & STAT_TELNET)
  793. y = add_cr(buf);
  794. if (dcc[idx].status & STAT_PAGE)
  795. append_line(idx, y);
  796. else
  797. tputs(dcc[idx].sock, y, strlen(y));
  798. }
  799. static struct dcc_table DCC_CHAT_SECPASS = {
  800. "CHAT_SECPASS",
  801. 0,
  802. eof_dcc_general,
  803. dcc_chat_secpass,
  804. &auth_timeout,
  805. tout_dcc_chat_secpass,
  806. display_dcc_chat_secpass,
  807. kill_dcc_general,
  808. out_dcc_general,
  809. NULL
  810. };
  811. static void
  812. dcc_chat_pass(int idx, char *buf, int atr)
  813. {
  814. if (!atr)
  815. return;
  816. strip_telnet(dcc[idx].sock, buf, &atr);
  817. atr = dcc[idx].user ? dcc[idx].user->flags : 0;
  818. if (dcc[idx].user->bot) {
  819. if (!egg_strcasecmp(buf, "elinkdone")) {
  820. free(dcc[idx].u.chat);
  821. dcc[idx].type = &DCC_BOT_NEW;
  822. dcc[idx].u.bot = (struct bot_info *) calloc(1, sizeof(struct bot_info));
  823. dcc[idx].status = STAT_CALLED;
  824. dprintf(idx, "goodbye!\n");
  825. greet_new_bot(idx);
  826. #ifdef HUB
  827. send_timesync(idx);
  828. #endif /* HUB */
  829. } else {
  830. /* Invalid password/digest on hub */
  831. putlog(LOG_WARN, "*", "%s failed encrypted link handshake.", dcc[idx].nick);
  832. killsock(dcc[idx].sock);
  833. lostdcc(idx);
  834. }
  835. return;
  836. }
  837. if (u_pass_match(dcc[idx].user, buf)) {
  838. if (dccauth) {
  839. char randstr[51] = "";
  840. make_rand_str(randstr, 50);
  841. strncpyz(dcc[idx].hash, makehash(dcc[idx].user, randstr), sizeof dcc[idx].hash);
  842. dcc[idx].type = &DCC_CHAT_SECPASS;
  843. dcc[idx].timeval = now;
  844. dprintf(idx, "-Auth %s %s\n", randstr, conf.bot->nick);
  845. } else {
  846. dcc_chat_secpass(idx, buf, atr);
  847. }
  848. } else {
  849. dprintf(idx, "%s\n", response(RES_BADUSERPASS));
  850. putlog(LOG_MISC, "*", DCC_BADLOGIN, dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  851. if (dcc[idx].u.chat->away) { /* su from a dumb user */
  852. /* Turn echo back on for telnet sessions (send IAC WON'T ECHO). */
  853. if (dcc[idx].status & STAT_TELNET)
  854. dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  855. dcc[idx].user = get_user_by_handle(userlist, dcc[idx].u.chat->away);
  856. strcpy(dcc[idx].nick, dcc[idx].u.chat->away);
  857. free(dcc[idx].u.chat->away);
  858. free(dcc[idx].u.chat->su_nick);
  859. dcc[idx].u.chat->away = NULL;
  860. dcc[idx].u.chat->su_nick = NULL;
  861. dcc[idx].type = &DCC_CHAT;
  862. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  863. botnet_send_join_idx(idx);
  864. chanout_but(-1, dcc[idx].u.chat->channel, DCC_JOIN, dcc[idx].nick);
  865. } else {
  866. killsock(dcc[idx].sock);
  867. lostdcc(idx);
  868. }
  869. }
  870. }
  871. struct dcc_table DCC_CHAT_PASS = {
  872. "CHAT_PASS",
  873. 0,
  874. eof_dcc_general,
  875. dcc_chat_pass,
  876. &password_timeout,
  877. tout_dcc_chat_pass,
  878. display_dcc_chat_pass,
  879. kill_dcc_general,
  880. out_dcc_general,
  881. NULL
  882. };
  883. /* Make sure ansi code is just for color-changing
  884. */
  885. static int
  886. check_ansi(char *v)
  887. {
  888. int count = 2;
  889. if (*v++ != '\033')
  890. return 1;
  891. if (*v++ != '[')
  892. return 1;
  893. while (*v) {
  894. if (*v == 'm')
  895. return 0;
  896. if ((*v != ';') && ((*v < '0') || (*v > '9')))
  897. return count;
  898. v++;
  899. count++;
  900. }
  901. return count;
  902. }
  903. static void
  904. eof_dcc_chat(int idx)
  905. {
  906. putlog(LOG_MISC, "*", DCC_LOSTDCC, dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  907. if (dcc[idx].u.chat->channel >= 0) {
  908. chanout_but(idx, dcc[idx].u.chat->channel, "*** %s lost dcc link.\n", dcc[idx].nick);
  909. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  910. botnet_send_part_idx(idx, "lost dcc link");
  911. }
  912. check_bind_chof(dcc[idx].nick, idx);
  913. killsock(dcc[idx].sock);
  914. lostdcc(idx);
  915. }
  916. static void
  917. dcc_chat(int idx, char *buf, int i)
  918. {
  919. int nathan = 0, doron = 0, fixed = 0;
  920. char *v = NULL, *d = NULL;
  921. strip_telnet(dcc[idx].sock, buf, &i);
  922. if (buf[0] && (buf[0] != settings.dcc_prefix[0]) && !(dcc[idx].user && (dcc[idx].user->flags & USER_NOFLOOD)) &&
  923. detect_dcc_flood(&dcc[idx].timeval, dcc[idx].u.chat, idx))
  924. return;
  925. dcc[idx].timeval = now;
  926. if (buf[0]) {
  927. /* Check for beeps and cancel annoying ones */
  928. v = buf;
  929. d = buf;
  930. while (*v)
  931. switch (*v) {
  932. case 1: /* CTCP ?! */
  933. v++;
  934. break;
  935. case 7: /* Beep - no more than 3 */
  936. nathan++;
  937. if (nathan > 3)
  938. v++;
  939. else
  940. *d++ = *v++;
  941. break;
  942. case 8: /* Backspace - for lame telnet's :) */
  943. if (d > buf) {
  944. d--;
  945. }
  946. v++;
  947. break;
  948. case 27: /* ESC - ansi code? */
  949. doron = check_ansi(v);
  950. /* If it's valid, append a return-to-normal code at the end */
  951. if (!doron) {
  952. *d++ = *v++;
  953. fixed = 1;
  954. } else
  955. v += doron;
  956. break;
  957. case '\r': /* Weird pseudo-linefeed */
  958. v++;
  959. break;
  960. default:
  961. *d++ = *v++;
  962. }
  963. if (fixed)
  964. strcpy(d, "\033[0m");
  965. else
  966. *d = 0;
  967. if (u_pass_match(dcc[idx].user, buf)) { /* user said their password :) */
  968. dprintf(idx, "Sure you want that going to the partyline? ;) (msg to partyline halted.)\n");
  969. } else if (!strncmp(buf, "+Auth ", 6)) { /* ignore extra +Auth lines */
  970. } else if ((!strncmp(buf, settings.dcc_prefix, strlen(settings.dcc_prefix))) || (dcc[idx].u.chat->channel < 0)) {
  971. if (!strncmp(buf, settings.dcc_prefix, strlen(settings.dcc_prefix))) /* strip '.' out */
  972. buf++;
  973. v = newsplit(&buf);
  974. rmspace(buf);
  975. check_bind_dcc(v, idx, buf);
  976. } else if (buf[0] == ',') {
  977. int me = 0;
  978. if ((buf[1] == 'm') && (buf[2] == 'e') && buf[3] == ' ')
  979. me = 1;
  980. for (i = 0; i < dcc_total; i++) {
  981. int ok = 0;
  982. if (dcc[i].type->flags & DCT_MASTER) {
  983. if ((dcc[i].type != &DCC_CHAT) || (dcc[i].u.chat->channel >= 0))
  984. if ((i != idx) || (dcc[idx].status & STAT_ECHO))
  985. ok = 1;
  986. }
  987. if (ok) {
  988. struct userrec *u = get_user_by_handle(userlist, dcc[i].nick);
  989. if (u && (u->flags & USER_MASTER)) {
  990. if (me)
  991. dprintf(i, "-> %s%s\n", dcc[idx].nick, buf + 3);
  992. else
  993. dprintf(i, "-%s-> %s\n", dcc[idx].nick, buf + 1);
  994. }
  995. }
  996. }
  997. } else if (buf[0] == '\'') {
  998. int me = 0;
  999. if ((buf[1] == 'm') && (buf[2] == 'e') && ((buf[3] == ' ') || (buf[3] == '\'') || (buf[3] == ',')))
  1000. me = 1;
  1001. for (i = 0; i < dcc_total; i++) {
  1002. if (dcc[i].type->flags & DCT_CHAT) {
  1003. if (me)
  1004. dprintf(i, "=> %s%s\n", dcc[idx].nick, buf + 3);
  1005. else
  1006. dprintf(i, "=%s=> %s\n", dcc[idx].nick, buf + 1);
  1007. }
  1008. }
  1009. } else {
  1010. if (dcc[idx].u.chat->away != NULL)
  1011. not_away(idx);
  1012. if (!strncmp(buf, "CTCP_MESSAGE ", 13)) /* irssi */
  1013. buf += 13;
  1014. if (!strncmp(buf, "ACTION ", 7)) {
  1015. buf += 7;
  1016. check_bind_dcc("me", idx, buf);
  1017. } else {
  1018. if (dcc[idx].status & STAT_ECHO)
  1019. chanout_but(-1, dcc[idx].u.chat->channel, "<%s> %s\n", dcc[idx].nick, buf);
  1020. else
  1021. chanout_but(idx, dcc[idx].u.chat->channel, "<%s> %s\n", dcc[idx].nick, buf);
  1022. botnet_send_chan(-1, conf.bot->nick, dcc[idx].nick, dcc[idx].u.chat->channel, buf);
  1023. }
  1024. }
  1025. }
  1026. if (dcc[idx].type == &DCC_CHAT) /* Could have change to files */
  1027. if (dcc[idx].status & STAT_PAGE)
  1028. flush_lines(idx, dcc[idx].u.chat);
  1029. }
  1030. static void
  1031. display_dcc_chat(int idx, char *buf)
  1032. {
  1033. size_t i;
  1034. int colori = 0;
  1035. i = simple_sprintf(buf, "chat flags: ");
  1036. buf[i++] = dcc[idx].status & STAT_CHAT ? 'C' : 'c';
  1037. buf[i++] = dcc[idx].status & STAT_PARTY ? 'P' : 'p';
  1038. buf[i++] = dcc[idx].status & STAT_TELNET ? 'T' : 't';
  1039. buf[i++] = dcc[idx].status & STAT_ECHO ? 'E' : 'e';
  1040. buf[i++] = dcc[idx].status & STAT_PAGE ? 'P' : 'p';
  1041. if ((colori = coloridx(idx)))
  1042. buf[i++] = colori == 1 ? 'A' : 'M';
  1043. #ifdef USE_IPV6
  1044. if (sockprotocol(dcc[idx].sock) == AF_INET6 && dcc[idx].addr6[0])
  1045. buf[i++] = '6';
  1046. #endif /* USE_IPV6 */
  1047. simple_sprintf(buf + i, "/%d", dcc[idx].u.chat->channel);
  1048. }
  1049. struct dcc_table DCC_CHAT = {
  1050. "CHAT",
  1051. DCT_CHAT | DCT_MASTER | DCT_SHOWWHO | DCT_VALIDIDX | DCT_SIMUL | DCT_CANBOOT | DCT_REMOTEWHO,
  1052. eof_dcc_chat,
  1053. dcc_chat,
  1054. NULL,
  1055. NULL,
  1056. display_dcc_chat,
  1057. kill_dcc_general,
  1058. out_dcc_general,
  1059. NULL
  1060. };
  1061. static int lasttelnets;
  1062. static char lasttelnethost[81];
  1063. static time_t lasttelnettime;
  1064. /* A modified detect_flood for incoming telnet flood protection.
  1065. */
  1066. static int
  1067. detect_telnet_flood(char *floodhost)
  1068. {
  1069. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  1070. get_user_flagrec(get_user_by_host(floodhost), &fr, NULL);
  1071. if (!flood_telnet_thr || glob_noflood(fr))
  1072. return 0; /* No flood protection */
  1073. if (egg_strcasecmp(lasttelnethost, floodhost)) { /* New... */
  1074. strcpy(lasttelnethost, floodhost);
  1075. lasttelnettime = now;
  1076. lasttelnets = 0;
  1077. return 0;
  1078. }
  1079. if (lasttelnettime < now - flood_telnet_time) {
  1080. /* Flood timer expired, reset it */
  1081. lasttelnettime = now;
  1082. lasttelnets = 0;
  1083. return 0;
  1084. }
  1085. lasttelnets++;
  1086. if (lasttelnets >= flood_telnet_thr) { /* FLOOD! */
  1087. /* Reset counters */
  1088. lasttelnets = 0;
  1089. lasttelnettime = 0;
  1090. lasttelnethost[0] = 0;
  1091. putlog(LOG_MISC, "*", IRC_TELNETFLOOD, floodhost);
  1092. addignore(floodhost, origbotname, "Telnet connection flood", now + (60 * ignore_time));
  1093. return 1;
  1094. }
  1095. return 0;
  1096. }
  1097. static void
  1098. dcc_telnet(int idx, char *buf, int i)
  1099. {
  1100. IP ip;
  1101. port_t port;
  1102. int j = 0, sock;
  1103. char s[UHOSTLEN + 1] = "", x[1024] = "";
  1104. if (dcc_total + 1 > max_dcc) {
  1105. j = answer(dcc[idx].sock, s, &ip, &port, 0);
  1106. if (j != -1) {
  1107. dprintf(-j, "Sorry, too many connections already.\r\n");
  1108. killsock(j);
  1109. }
  1110. return;
  1111. }
  1112. sock = answer(dcc[idx].sock, s, &ip, &port, 0);
  1113. while ((sock == -1) && (errno == EAGAIN))
  1114. sock = answer(sock, s, &ip, &port, 0);
  1115. /*. ssl_link ACCEPT_SSL should go here!!!! */
  1116. if (sock < 0) {
  1117. putlog(LOG_MISC, "*", DCC_FAILED, strerror(errno));
  1118. return;
  1119. }
  1120. /* Buffer data received on this socket. */
  1121. sockoptions(sock, EGG_OPTION_SET, SOCK_BUFFER);
  1122. #if SIZEOF_SHORT == 2
  1123. if (port < 1024) {
  1124. #else
  1125. if (port < 1024 || port > 65535) {
  1126. #endif
  1127. putlog(LOG_BOTS, "*", DCC_BADSRC, s, port);
  1128. killsock(sock);
  1129. return;
  1130. }
  1131. sprintf(x, "-telnet!telnet@%s", iptostr(htonl(ip)));
  1132. if (match_ignore(x) || detect_telnet_flood(x)) {
  1133. killsock(sock);
  1134. return;
  1135. }
  1136. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1137. dcc[i].sock = sock;
  1138. dcc[i].addr = ip;
  1139. #ifdef USE_IPV6
  1140. if (sockprotocol(sock) == AF_INET6)
  1141. strcpy(dcc[i].addr6, s);
  1142. #endif /* USE_IPV6 */
  1143. dcc[i].port = port;
  1144. dcc[i].timeval = now;
  1145. strcpy(dcc[i].nick, "*");
  1146. dcc[i].u.dns->ip = ip;
  1147. dcc[i].u.dns->dns_success = dcc_telnet_hostresolved;
  1148. dcc[i].u.dns->dns_failure = dcc_telnet_hostresolved;
  1149. dcc[i].u.dns->dns_type = RES_HOSTBYIP;
  1150. dcc[i].u.dns->ibuf = dcc[idx].sock;
  1151. dcc[i].u.dns->type = &DCC_IDENTWAIT;
  1152. #ifdef USE_IPV6
  1153. if (sockprotocol(sock) == AF_INET6)
  1154. dcc_telnet_hostresolved(i);
  1155. else
  1156. #endif /* USE_IPV6 */
  1157. dcc_dnshostbyip(ip);
  1158. }
  1159. static void
  1160. dcc_telnet_hostresolved(int i)
  1161. {
  1162. int idx;
  1163. int j = 0, sock;
  1164. char s[UHOSTLEN] = "", s2[UHOSTLEN + 20] = "";
  1165. #ifdef USE_IPV6
  1166. if (sockprotocol(dcc[i].sock) == AF_INET6 && dcc[i].addr6[0])
  1167. strncpyz(dcc[i].host, dcc[i].addr6, UHOSTLEN);
  1168. else
  1169. #endif /* USE_IPV6 */
  1170. strncpyz(dcc[i].host, dcc[i].u.dns->host, UHOSTLEN);
  1171. for (idx = 0; idx < dcc_total; idx++)
  1172. if ((dcc[idx].type == &DCC_TELNET) && (dcc[idx].sock == dcc[i].u.dns->ibuf)) {
  1173. break;
  1174. }
  1175. sprintf(s2, "-telnet!telnet@%s", dcc[i].host);
  1176. if (match_ignore(s2) || detect_telnet_flood(s2)) {
  1177. killsock(dcc[i].sock);
  1178. lostdcc(i);
  1179. return;
  1180. }
  1181. if (dcc_total == idx) {
  1182. putlog(LOG_BOTS, "*", "Lost listening socket while resolving %s", dcc[i].host);
  1183. killsock(dcc[i].sock);
  1184. lostdcc(i);
  1185. return;
  1186. }
  1187. if (dcc[idx].host[0] == '@') {
  1188. /* Restrict by hostname */
  1189. if (!wild_match(dcc[idx].host + 1, dcc[i].host)) {
  1190. putlog(LOG_BOTS, "*", DCC_BADHOST, s);
  1191. killsock(dcc[i].sock);
  1192. lostdcc(i);
  1193. return;
  1194. }
  1195. }
  1196. /* . ssl_link(dcc[i].sock, ACCEPT_SSL); */
  1197. changeover_dcc(i, &DCC_IDENTWAIT, 0);
  1198. dcc[i].timeval = now;
  1199. dcc[i].u.ident_sock = dcc[idx].sock;
  1200. #ifdef USE_IPV6
  1201. if (sockprotocol(dcc[idx].sock) == AF_INET6)
  1202. sock = open_telnet(dcc[i].host, 113);
  1203. else
  1204. #endif /* USE_IPV6 */
  1205. sock = open_telnet(iptostr(htonl(dcc[i].addr)), 113);
  1206. putlog(LOG_MISC, "*", DCC_TELCONN, dcc[i].host, dcc[i].port);
  1207. s[0] = 0;
  1208. if (sock < 0) {
  1209. if (sock == -2)
  1210. strcpy(s, "DNS lookup failed for ident");
  1211. else
  1212. strcpy(s, strerror(errno));
  1213. } else {
  1214. j = new_dcc(&DCC_IDENT, 0);
  1215. if (j < 0) {
  1216. killsock(sock);
  1217. strcpy(s, "No Free DCC's");
  1218. }
  1219. }
  1220. if (s[0]) {
  1221. putlog(LOG_MISC, "*", DCC_IDENTFAIL, dcc[i].host, s);
  1222. sprintf(s, "telnet@%s", dcc[i].host);
  1223. dcc_telnet_got_ident(i, s);
  1224. return;
  1225. }
  1226. dcc[j].sock = sock;
  1227. dcc[j].port = 113;
  1228. dcc[j].addr = dcc[i].addr;
  1229. strcpy(dcc[j].host, dcc[i].host);
  1230. strcpy(dcc[j].nick, "*");
  1231. dcc[j].u.ident_sock = dcc[i].sock;
  1232. dcc[j].timeval = now;
  1233. dprintf(j, "%d, %d\n", dcc[i].port, dcc[idx].port);
  1234. }
  1235. static void
  1236. eof_dcc_telnet(int idx)
  1237. {
  1238. putlog(LOG_MISC, "*", DCC_PORTDIE, dcc[idx].port);
  1239. killsock(dcc[idx].sock);
  1240. lostdcc(idx);
  1241. }
  1242. static void
  1243. display_telnet(int idx, char *buf)
  1244. {
  1245. sprintf(buf, "lstn %d%s", dcc[idx].port, (dcc[idx].status & LSTN_PUBLIC) ? " pub" : "");
  1246. }
  1247. struct dcc_table DCC_TELNET = {
  1248. "TELNET",
  1249. DCT_LISTEN,
  1250. eof_dcc_telnet,
  1251. dcc_telnet,
  1252. NULL,
  1253. NULL,
  1254. display_telnet,
  1255. NULL,
  1256. NULL,
  1257. NULL
  1258. };
  1259. static void
  1260. eof_dcc_dupwait(int idx)
  1261. {
  1262. putlog(LOG_BOTS, "*", DCC_LOSTDUP, dcc[idx].host);
  1263. killsock(dcc[idx].sock);
  1264. lostdcc(idx);
  1265. }
  1266. static void
  1267. dcc_dupwait(int idx, char *buf, int i)
  1268. {
  1269. /* We just ignore any data at this point. */
  1270. return;
  1271. }
  1272. /* We now check again. If the bot is still marked as duplicate, there is no
  1273. * botnet lag we could push it on, so we just drop the connection.
  1274. */
  1275. static void
  1276. timeout_dupwait(int idx)
  1277. {
  1278. char x[100] = "";
  1279. /* Still duplicate? */
  1280. if (in_chain(dcc[idx].nick)) {
  1281. egg_snprintf(x, sizeof x, "%s!%s", dcc[idx].nick, dcc[idx].host);
  1282. putlog(LOG_BOTS, "*", DCC_DUPLICATE, x);
  1283. killsock(dcc[idx].sock);
  1284. lostdcc(idx);
  1285. } else {
  1286. /* Ha! Now it's gone and we can grant this bot access. */
  1287. dcc_telnet_pass(idx, dcc[idx].u.dupwait->atr);
  1288. }
  1289. }
  1290. static void
  1291. display_dupwait(int idx, char *buf)
  1292. {
  1293. sprintf(buf, "wait duplicate?");
  1294. }
  1295. static void
  1296. kill_dupwait(int idx, void *x)
  1297. {
  1298. register struct dupwait_info *p = (struct dupwait_info *) x;
  1299. if (p) {
  1300. if (p->chat && DCC_CHAT.kill)
  1301. DCC_CHAT.kill(idx, p->chat);
  1302. free(p);
  1303. }
  1304. }
  1305. struct dcc_table DCC_DUPWAIT = {
  1306. "DUPWAIT",
  1307. DCT_VALIDIDX,
  1308. eof_dcc_dupwait,
  1309. dcc_dupwait,
  1310. &dupwait_timeout,
  1311. timeout_dupwait,
  1312. display_dupwait,
  1313. kill_dupwait,
  1314. NULL,
  1315. NULL
  1316. };
  1317. /* This function is called if a bot gets removed from the list. It checks
  1318. * wether we have a pending duplicate connection for that bot and continues
  1319. * with the login in that case.
  1320. */
  1321. void
  1322. dupwait_notify(const char *who)
  1323. {
  1324. register int idx;
  1325. Assert(who);
  1326. for (idx = 0; idx < dcc_total; idx++)
  1327. if ((dcc[idx].type == &DCC_DUPWAIT) && !egg_strcasecmp(dcc[idx].nick, who)) {
  1328. dcc_telnet_pass(idx, dcc[idx].u.dupwait->atr);
  1329. break;
  1330. }
  1331. }
  1332. static void
  1333. dcc_telnet_id(int idx, char *buf, int atr)
  1334. {
  1335. int ok = 0;
  1336. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  1337. strip_telnet(dcc[idx].sock, buf, &atr);
  1338. buf[HANDLEN] = 0;
  1339. /* Toss out bad nicknames */
  1340. if ((dcc[idx].nick[0] != '@') && (!wild_match(dcc[idx].nick, buf))) {
  1341. /* FIXME: if a bot gets this, they will try to decrypt it ;/ */
  1342. dprintf(idx, "Sorry, that nickname format is invalid.\n");
  1343. putlog(LOG_BOTS, "*", DCC_BADNICK, dcc[idx].host);
  1344. killsock(dcc[idx].sock);
  1345. lostdcc(idx);
  1346. return;
  1347. }
  1348. dcc[idx].user = get_user_by_handle(userlist, buf);
  1349. get_user_flagrec(dcc[idx].user, &fr, NULL);
  1350. /* if (!ok && glob_party(fr))
  1351. ok = 1;*/
  1352. ok = 1;
  1353. #ifdef HUB
  1354. if (!glob_huba(fr))
  1355. ok = 0;
  1356. #else /* !HUB */
  1357. /* if I am a chanhub and they dont have +c then drop */
  1358. if (ischanhub() && !glob_chuba(fr))
  1359. ok = 0;
  1360. if (!ischanhub())
  1361. ok = 0;
  1362. #endif /* HUB */
  1363. if (!ok && glob_bot(fr))
  1364. ok = 1;
  1365. if (!ok) {
  1366. putlog(LOG_BOTS, "*", DCC_INVHANDLE, dcc[idx].host, buf);
  1367. killsock(dcc[idx].sock);
  1368. lostdcc(idx);
  1369. return;
  1370. }
  1371. correct_handle(buf);
  1372. strcpy(dcc[idx].nick, buf);
  1373. if (glob_bot(fr)) {
  1374. if (!egg_strcasecmp(conf.bot->nick, dcc[idx].nick)) {
  1375. putlog(LOG_BOTS, "*", DCC_MYBOTNETNICK, dcc[idx].host);
  1376. killsock(dcc[idx].sock);
  1377. lostdcc(idx);
  1378. return;
  1379. } else if (in_chain(dcc[idx].nick)) {
  1380. struct chat_info *ci;
  1381. ci = dcc[idx].u.chat;
  1382. dcc[idx].type = &DCC_DUPWAIT;
  1383. dcc[idx].u.dupwait = (struct dupwait_info *) calloc(1, sizeof(struct dupwait_info));
  1384. dcc[idx].u.dupwait->chat = ci;
  1385. dcc[idx].u.dupwait->atr = atr;
  1386. return;
  1387. }
  1388. }
  1389. dcc_telnet_pass(idx, atr);
  1390. }
  1391. static void
  1392. dcc_telnet_pass(int idx, int atr)
  1393. {
  1394. int ok = 0, i, ok2;
  1395. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  1396. get_user_flagrec(dcc[idx].user, &fr, NULL);
  1397. /* No password set? */
  1398. if (!glob_bot(fr) && (u_pass_match(dcc[idx].user, "-"))) {
  1399. dprintf(idx, "Can't telnet until you have a password set.\r\n");
  1400. putlog(LOG_MISC, "*", DCC_NOPASS, dcc[idx].nick, dcc[idx].host);
  1401. killsock(dcc[idx].sock);
  1402. lostdcc(idx);
  1403. return;
  1404. }
  1405. ok = 0;
  1406. if (dcc[idx].type == &DCC_DUPWAIT) {
  1407. struct chat_info *ci;
  1408. ci = dcc[idx].u.dupwait->chat;
  1409. free(dcc[idx].u.dupwait);
  1410. dcc[idx].u.chat = ci;
  1411. }
  1412. dcc[idx].type = &DCC_CHAT_PASS;
  1413. dcc[idx].timeval = now;
  1414. ok2 = 1;
  1415. #ifdef HUB
  1416. if (!glob_huba(fr))
  1417. ok2 = 0;
  1418. #else /* !HUB */
  1419. if (ischanhub() && !glob_chuba(fr))
  1420. ok2 = 0;
  1421. #endif /* HUB */
  1422. if (ok2) {
  1423. ok = 1;
  1424. dcc[idx].status |= STAT_PARTY;
  1425. }
  1426. if (glob_bot(fr))
  1427. ok = 1;
  1428. if (!ok) {
  1429. struct chat_info *ci;
  1430. ci = dcc[idx].u.chat;
  1431. dcc[idx].u.file = (struct file_info *) calloc(1, sizeof(struct file_info));
  1432. dcc[idx].u.file->chat = ci;
  1433. }
  1434. if (glob_bot(fr)) {
  1435. int snum = -1;
  1436. for (i = 0; i < MAXSOCKS; i++) {
  1437. if (!(socklist[i].flags & SOCK_UNUSED)
  1438. && (socklist[i].sock == dcc[idx].sock)) {
  1439. snum = i;
  1440. break;
  1441. }
  1442. }
  1443. if (snum >= 0) {
  1444. char initkey[33] = "", *tmp2 = NULL;
  1445. char tmp[256] = "";
  1446. unsigned char buf[SHA_HASH_LENGTH + 1] = "";
  1447. SHA_CTX ctx;
  1448. /* initkey-gen hub */
  1449. /* bdhash port mynick conf.bot->nick */
  1450. sprintf(tmp, "%s@%4x@%s@%s", settings.bdhash, htons(dcc[idx].port), conf.bot->nick, dcc[idx].nick);
  1451. SHA1_Init(&ctx);
  1452. SHA1_Update(&ctx, tmp, strlen(tmp));
  1453. SHA1_Final(buf, &ctx);
  1454. strncpyz(socklist[snum].okey, btoh(buf, SHA_DIGEST_LENGTH), sizeof(socklist[snum].okey));
  1455. putlog(LOG_DEBUG, "@", "Link hash for %s: %s", dcc[idx].nick, tmp);
  1456. putlog(LOG_DEBUG, "@", "outkey (%d): %s", strlen(socklist[snum].okey), socklist[snum].okey);
  1457. make_rand_str(initkey, 32); /* set the initial out/in link key to random chars. */
  1458. socklist[snum].oseed = random();
  1459. socklist[snum].iseed = socklist[snum].oseed;
  1460. tmp2 = encrypt_string(settings.salt2, initkey);
  1461. putlog(LOG_BOTS, "*", "Sending encrypted link handshake to %s...", dcc[idx].nick);
  1462. /* the leaf bot set encstatus right after it sent it's nick, but we just set the key.... */
  1463. socklist[snum].encstatus = 1;
  1464. socklist[snum].gz = 1;
  1465. dprintf(idx, "elink %s %d\n", tmp2, socklist[snum].oseed);
  1466. free(tmp2);
  1467. strcpy(socklist[snum].okey, initkey);
  1468. strcpy(socklist[snum].ikey, initkey);
  1469. } else {
  1470. putlog(LOG_MISC, "*", "Couldn't find socket for %s connection?? Shouldn't happen :/", dcc[idx].nick);
  1471. killsock(dcc[idx].sock);
  1472. lostdcc(idx);
  1473. }
  1474. } else {
  1475. /* Turn off remote telnet echo (send IAC WILL ECHO). */
  1476. #ifdef HUB
  1477. dprintf(idx, "\n%s" TLN_IAC_C TLN_WILL_C TLN_ECHO_C "\n", DCC_ENTERPASS);
  1478. #else /* !HUB */
  1479. dprintf(idx, "%s\n" TLN_IAC_C TLN_WILL_C TLN_ECHO_C, response(RES_PASSWORD));
  1480. #endif /* HUB */
  1481. }
  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. kill_dcc_general,
  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. static void
  1542. display_dcc_lost(int idx, char *buf)
  1543. {
  1544. strcpy(buf, "lost");
  1545. }
  1546. struct dcc_table DCC_LOST = {
  1547. "LOST",
  1548. 0,
  1549. NULL,
  1550. dcc_socket,
  1551. NULL,
  1552. NULL,
  1553. display_dcc_lost,
  1554. NULL,
  1555. NULL,
  1556. NULL
  1557. };
  1558. void
  1559. dcc_identwait(int idx, char *buf, int len)
  1560. {
  1561. /* Ignore anything now */
  1562. }
  1563. void
  1564. eof_dcc_identwait(int idx)
  1565. {
  1566. int i;
  1567. putlog(LOG_MISC, "*", DCC_LOSTCONN, dcc[idx].host, dcc[idx].port);
  1568. for (i = 0; i < dcc_total; i++)
  1569. if ((dcc[i].type == &DCC_IDENT) && (dcc[i].u.ident_sock == dcc[idx].sock)) {
  1570. killsock(dcc[i].sock); /* Cleanup ident socket */
  1571. dcc[i].u.other = 0;
  1572. lostdcc(i);
  1573. break;
  1574. }
  1575. killsock(dcc[idx].sock); /* Cleanup waiting socket */
  1576. dcc[idx].u.other = 0;
  1577. lostdcc(idx);
  1578. }
  1579. static void
  1580. display_dcc_identwait(int idx, char *buf)
  1581. {
  1582. sprintf(buf, "idtw waited %lis", now - dcc[idx].timeval);
  1583. }
  1584. struct dcc_table DCC_IDENTWAIT = {
  1585. "IDENTWAIT",
  1586. 0,
  1587. eof_dcc_identwait,
  1588. dcc_identwait,
  1589. NULL,
  1590. NULL,
  1591. display_dcc_identwait,
  1592. NULL,
  1593. NULL,
  1594. NULL
  1595. };
  1596. void
  1597. dcc_ident(int idx, char *buf, int len)
  1598. {
  1599. char ident_response[512] = "", uid[512] = "", buf1[UHOSTLEN] = "";
  1600. int i;
  1601. sscanf(buf, "%*[^:]:%[^:]:%*[^:]:%[^\n]\n", ident_response, uid);
  1602. rmspace(ident_response);
  1603. if (ident_response[0] != 'U') {
  1604. dcc[idx].timeval = now;
  1605. return;
  1606. }
  1607. rmspace(uid);
  1608. uid[20] = 0; /* 20 character ident max */
  1609. for (i = 0; i < dcc_total; i++)
  1610. if ((dcc[i].type == &DCC_IDENTWAIT) && (dcc[i].sock == dcc[idx].u.ident_sock)) {
  1611. simple_sprintf(buf1, "%s@%s", uid, dcc[idx].host);
  1612. dcc_telnet_got_ident(i, buf1);
  1613. }
  1614. dcc[idx].u.other = 0;
  1615. killsock(dcc[idx].sock);
  1616. lostdcc(idx);
  1617. }
  1618. void
  1619. eof_dcc_ident(int idx)
  1620. {
  1621. char buf[UHOSTLEN] = "";
  1622. int i;
  1623. for (i = 0; i < dcc_total; i++)
  1624. if ((dcc[i].type == &DCC_IDENTWAIT) && (dcc[i].sock == dcc[idx].u.ident_sock)) {
  1625. putlog(LOG_MISC, "*", DCC_EOFIDENT);
  1626. simple_sprintf(buf, "telnet@%s", dcc[idx].host);
  1627. dcc_telnet_got_ident(i, buf);
  1628. }
  1629. killsock(dcc[idx].sock);
  1630. dcc[idx].u.other = 0;
  1631. lostdcc(idx);
  1632. }
  1633. static void
  1634. display_dcc_ident(int idx, char *buf)
  1635. {
  1636. sprintf(buf, "idnt (sock %d)", dcc[idx].u.ident_sock);
  1637. }
  1638. struct dcc_table DCC_IDENT = {
  1639. "IDENT",
  1640. 0,
  1641. eof_dcc_ident,
  1642. dcc_ident,
  1643. &identtimeout,
  1644. eof_dcc_ident,
  1645. display_dcc_ident,
  1646. NULL,
  1647. NULL,
  1648. NULL
  1649. };
  1650. static void
  1651. dcc_telnet_got_ident(int i, char *host)
  1652. {
  1653. int idx;
  1654. char x[1024] = "";
  1655. for (idx = 0; idx < dcc_total; idx++)
  1656. if ((dcc[idx].type == &DCC_TELNET) && (dcc[idx].sock == dcc[i].u.ident_sock))
  1657. break;
  1658. dcc[i].u.other = 0;
  1659. if (dcc_total == idx) {
  1660. putlog(LOG_MISC, "*", DCC_LOSTIDENT);
  1661. killsock(dcc[i].sock);
  1662. lostdcc(i);
  1663. return;
  1664. }
  1665. strncpyz(dcc[i].host, host, UHOSTLEN);
  1666. egg_snprintf(x, sizeof x, "-telnet!%s", dcc[i].host);
  1667. if (match_ignore(x)) {
  1668. killsock(dcc[i].sock);
  1669. lostdcc(i);
  1670. return;
  1671. }
  1672. if (protect_telnet) {
  1673. struct userrec *u;
  1674. int ok = 1;
  1675. u = get_user_by_host(x);
  1676. /* Not a user or +p & require p OR +o */
  1677. if (!u)
  1678. ok = 0;
  1679. #ifdef HUB
  1680. if (ok && !(u->flags & USER_HUBA))
  1681. ok = 0;
  1682. #else /* !HUB */
  1683. /* if I am a chanhub and they dont have +c then drop */
  1684. if (ok && (ischanhub() && !(u->flags & USER_CHUBA)))
  1685. ok = 0;
  1686. #endif /* HUB */
  1687. /* else if (!(u->flags & USER_PARTY))
  1688. ok = 0; */
  1689. if (!ok && u && u->bot)
  1690. ok = 1;
  1691. if (!ok && (dcc[idx].status & LSTN_PUBLIC))
  1692. ok = 1;
  1693. if (!ok) {
  1694. putlog(LOG_MISC, "*", DCC_NOACCESS, dcc[i].host);
  1695. killsock(dcc[i].sock);
  1696. lostdcc(i);
  1697. return;
  1698. }
  1699. }
  1700. /* Do not buffer data anymore. All received and stored data is passed
  1701. * over to the dcc functions from now on. */
  1702. sockoptions(dcc[i].sock, EGG_OPTION_UNSET, SOCK_BUFFER);
  1703. dcc[i].type = &DCC_TELNET_ID;
  1704. dcc[i].u.chat = (struct chat_info *) calloc(1, sizeof(struct chat_info));
  1705. egg_bzero(dcc[i].u.chat, sizeof(struct chat_info));
  1706. /* Copy acceptable-nick/host mask */
  1707. dcc[i].status =
  1708. (STAT_TELNET | STAT_ECHO | STAT_COLOR | STAT_BANNER | STAT_CHANNELS | STAT_BOTS | STAT_WHOM);
  1709. /* Copy acceptable-nick/host mask */
  1710. strncpyz(dcc[i].nick, dcc[idx].host, HANDLEN);
  1711. dcc[i].timeval = now;
  1712. strcpy(dcc[i].u.chat->con_chan, chanset ? chanset->dname : "*");
  1713. /* This is so we dont tell someone doing a portscan anything
  1714. * about ourselves. <cybah>
  1715. */
  1716. /* n ssl_link(dcc[i].sock, ACCEPT_SSL); */
  1717. #ifdef HUB
  1718. dprintf(i, "\n");
  1719. #else /* !HUB */
  1720. dprintf(i, "%s\n", response(RES_USERNAME));
  1721. #endif /* HUB */
  1722. }