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 "salt.h"
  16. #include "net.h"
  17. #include "response.h"
  18. #include "misc.h"
  19. #include "users.h"
  20. #include "userrec.h"
  21. #include "userent.h"
  22. #include "match.h"
  23. #include "dns.h"
  24. #include "auth.h"
  25. #include "dccutil.h"
  26. #include "crypt.h"
  27. #include "chanprog.h"
  28. #include "botmsg.h"
  29. #include "botcmd.h"
  30. #include "botnet.h"
  31. #include <ctype.h>
  32. #include <errno.h>
  33. #include <sys/types.h>
  34. #include <sys/socket.h>
  35. #include <netinet/in.h>
  36. #include <sys/utsname.h>
  37. #include <sys/stat.h>
  38. #include "tandem.h"
  39. #include "core_binds.h"
  40. struct dcc_t *dcc = NULL; /* DCC list */
  41. time_t timesync = 0;
  42. int dcc_total = 0; /* Total dcc's */
  43. static int password_timeout = 20; /* Time to wait for a password from a user */
  44. static int auth_timeout = 40;
  45. static int bot_timeout = 15; /* Bot timeout value */
  46. static int identtimeout = 15; /* Timeout value for ident lookups */
  47. static int dupwait_timeout = 5; /* Timeout for rejecting duplicate entries */
  48. #ifdef LEAF
  49. static int protect_telnet = 0; /* Even bother with ident lookups :) */
  50. #else /* !LEAF */
  51. static int protect_telnet = 1;
  52. #endif /* LEAF */
  53. static int flood_telnet_thr = 10; /* Number of telnet connections to be
  54. * considered a flood */
  55. static int flood_telnet_time = 5; /* In how many seconds? */
  56. static void dcc_telnet_hostresolved(int);
  57. static void dcc_telnet_got_ident(int, char *);
  58. static void dcc_telnet_pass(int, int);
  59. static void
  60. strip_telnet(int sock, char *buf, int *len)
  61. {
  62. unsigned char *p = (unsigned char *) buf, *o = (unsigned char *) buf;
  63. int mark;
  64. while (*p != 0) {
  65. while ((*p != TLN_IAC) && (*p != 0)) {
  66. if (*p == 0xA0) {
  67. *o++ = 32;
  68. p++;
  69. } else
  70. *o++ = *p++;
  71. }
  72. if (*p == TLN_IAC) {
  73. p++;
  74. mark = 2;
  75. if (!*p)
  76. mark = 1; /* bogus */
  77. if ((*p >= TLN_WILL) && (*p <= TLN_DONT)) {
  78. mark = 3;
  79. if (!*(p + 1))
  80. mark = 2; /* bogus */
  81. }
  82. if (*p == TLN_WILL) {
  83. /* WILL X -> response: DONT X */
  84. /* except WILL ECHO which we just smile and ignore */
  85. if (*(p + 1) != TLN_ECHO) {
  86. write(sock, TLN_IAC_C TLN_DONT_C, 2);
  87. write(sock, p + 1, 1);
  88. }
  89. }
  90. if (*p == TLN_DO) {
  91. /* DO X -> response: WONT X */
  92. /* except DO ECHO which we just smile and ignore */
  93. if (*(p + 1) != TLN_ECHO) {
  94. write(sock, TLN_IAC_C TLN_WONT_C, 2);
  95. write(sock, p + 1, 1);
  96. }
  97. }
  98. if (*p == TLN_AYT) {
  99. /* "are you there?" */
  100. /* response is: "hell yes!" */
  101. write(sock, "\r\nHell, yes!\r\n", 14);
  102. }
  103. /* Anything else can probably be ignored */
  104. p += mark - 1;
  105. *len = *len - mark;
  106. }
  107. }
  108. *o = *p;
  109. }
  110. #ifdef HUB
  111. void
  112. send_timesync(int idx)
  113. {
  114. /* Send timesync to idx, or all lower bots if idx<0 */
  115. if (idx >= 0)
  116. dprintf(idx, "ts %li\n", timesync + now);
  117. else {
  118. char s[15] = "";
  119. int i;
  120. sprintf(s, "ts %li\n", timesync + now);
  121. for (i = 0; i < dcc_total; i++) {
  122. if ((dcc[i].type == &DCC_BOT) && (bot_aggressive_to(dcc[i].user))) {
  123. dprintf(i, s);
  124. }
  125. }
  126. }
  127. }
  128. #endif /* HUB */
  129. static void
  130. greet_new_bot(int idx)
  131. {
  132. int i;
  133. char *sysname = NULL;
  134. struct utsname un;
  135. dcc[idx].timeval = now;
  136. dcc[idx].u.bot->version[0] = 0;
  137. dcc[idx].u.bot->sysname[0] = 0;
  138. dcc[idx].u.bot->numver = 0;
  139. #ifdef HUB
  140. if (dcc[idx].user && (!(dcc[idx].user->flags & USER_OP))) {
  141. putlog(LOG_BOTS, "*", "Rejecting link from %s", dcc[idx].nick);
  142. dprintf(idx, "error You are being rejected.\n");
  143. dprintf(idx, "bye\n");
  144. killsock(dcc[idx].sock);
  145. lostdcc(idx);
  146. return;
  147. }
  148. #endif /* HUB */
  149. if (bot_hublevel(dcc[idx].user) == 999)
  150. dcc[idx].status |= STAT_LEAF;
  151. dcc[idx].status |= STAT_LINKING;
  152. if (uname(&un) < 0)
  153. sysname = "*";
  154. else
  155. sysname = un.sysname;
  156. dprintf(idx, "v 1001500 %d Wraith %s <%s> %d %li %s\n", HANDLEN, egg_version, "-", localhub, buildts, egg_version);
  157. dprintf(idx, "si %s %s %s", conf.username ? conf.username : "*", un.sysname ? un.sysname : "*",
  158. un.nodename ? un.nodename : "*");
  159. for (i = 0; i < dcc_total; i++) {
  160. if (dcc[i].type == &DCC_FORK_BOT) {
  161. killsock(dcc[i].sock);
  162. lostdcc(i);
  163. }
  164. }
  165. }
  166. static void
  167. bot_version(int idx, char *par)
  168. {
  169. char x[1024] = "", *vversion = NULL;
  170. int l, vlocalhub = 0;
  171. time_t vbuildts = 0;
  172. dcc[idx].timeval = now;
  173. if (in_chain(dcc[idx].nick)) {
  174. dprintf(idx, "error Sorry, already connected.\n");
  175. dprintf(idx, "bye\n");
  176. killsock(dcc[idx].sock);
  177. lostdcc(idx);
  178. return;
  179. }
  180. if ((par[0] >= '0') && (par[0] <= '9')) {
  181. char *work = NULL;
  182. work = newsplit(&par);
  183. dcc[idx].u.bot->numver = atoi(work);
  184. /* old numver crap */
  185. } else
  186. dcc[idx].u.bot->numver = 0;
  187. dprintf(idx, "tb %s\n", conf.bot->nick);
  188. l = atoi(newsplit(&par));
  189. if (l != HANDLEN) {
  190. putlog(LOG_BOTS, "*", "Non-matching handle lengths with %s, they use %d characters.", dcc[idx].nick, l);
  191. dprintf(idx, "error Non-matching handle length: mine %d, yours %d\n", HANDLEN, l);
  192. dprintf(idx, "bye %s\n", "bad handlen");
  193. killsock(dcc[idx].sock);
  194. lostdcc(idx);
  195. return;
  196. }
  197. strncpyz(dcc[idx].u.bot->version, par, 120);
  198. newsplit(&par); /* 'ver' */
  199. newsplit(&par); /* handlen */
  200. newsplit(&par); /* network */
  201. if (par[0])
  202. vlocalhub = atoi(newsplit(&par));
  203. if (par[0])
  204. vbuildts = atol(newsplit(&par));
  205. if (par[0])
  206. vversion = newsplit(&par);
  207. #ifdef HUB
  208. putlog(LOG_BOTS, "*", DCC_LINKED, dcc[idx].nick);
  209. chatout("*** Linked to %s\n", dcc[idx].nick);
  210. #else /* !HUB */
  211. putlog(LOG_BOTS, "*", "Linked to botnet.");
  212. chatout("*** Linked to botnet.\n");
  213. #endif /* HUB */
  214. #ifdef HUB
  215. botnet_send_nlinked(idx, dcc[idx].nick, conf.bot->nick, '!', vlocalhub, vbuildts, vversion);
  216. #endif /* HUB */
  217. dump_links(idx);
  218. touch_laston(dcc[idx].user, "linked", now);
  219. dcc[idx].type = &DCC_BOT;
  220. addbot(dcc[idx].nick, dcc[idx].nick, conf.bot->nick, '-', vlocalhub, vbuildts, vversion);
  221. egg_snprintf(x, sizeof x, "v 1001500");
  222. bot_shareupdate(idx, x);
  223. bot_share(idx, x);
  224. dprintf(idx, "el\n");
  225. }
  226. void
  227. failed_link(int idx)
  228. {
  229. char s[81] = "", s1[512] = "";
  230. if (dcc[idx].u.bot->linker[0]) {
  231. egg_snprintf(s, sizeof s, "Couldn't link to %s.", dcc[idx].nick);
  232. strcpy(s1, dcc[idx].u.bot->linker);
  233. add_note(s1, conf.bot->nick, s, -2, 0);
  234. }
  235. if (dcc[idx].u.bot->numver >= (-1))
  236. putlog(LOG_BOTS, "*", DCC_LINKFAIL, dcc[idx].nick);
  237. if (dcc[idx].sock != -1) {
  238. int i = 0;
  239. for (i = 0; i < MAXSOCKS; i++)
  240. if (dcc[idx].sock == socklist[i].sock) {
  241. killsock(dcc[idx].sock);
  242. dcc[idx].sock = -1;
  243. }
  244. }
  245. strcpy(s, dcc[idx].nick);
  246. lostdcc(idx);
  247. autolink_cycle(s); /* Check for more auto-connections */
  248. }
  249. static void
  250. cont_link(int idx, char *buf, int ii)
  251. {
  252. /* Now set the initial link key (incoming only, we're not sending more until we get an OK)... */
  253. struct sockaddr_in sa;
  254. char tmp[301] = "";
  255. int i, snum = -1;
  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. i = sizeof(sa);
  278. /* initkey-gen leaf */
  279. /* bdhash myport hubnick mynick */
  280. getsockname(socklist[snum].sock, (struct sockaddr *) &sa, &i);
  281. egg_snprintf(tmp, sizeof tmp, "%s@%4x@%s@%s", 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(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. struct dcc_table DCC_CHAT_SECPASS;
  629. static void
  630. dcc_chat_pass(int idx, char *buf, int atr)
  631. {
  632. if (!atr)
  633. return;
  634. strip_telnet(dcc[idx].sock, buf, &atr);
  635. atr = dcc[idx].user ? dcc[idx].user->flags : 0;
  636. if (dcc[idx].user->bot) {
  637. if (!egg_strcasecmp(buf, "elinkdone")) {
  638. free(dcc[idx].u.chat);
  639. dcc[idx].type = &DCC_BOT_NEW;
  640. dcc[idx].u.bot = calloc(1, sizeof(struct bot_info));
  641. dcc[idx].status = STAT_CALLED;
  642. dprintf(idx, "goodbye!\n");
  643. greet_new_bot(idx);
  644. #ifdef HUB
  645. send_timesync(idx);
  646. #endif /* HUB */
  647. } else {
  648. /* Invalid password/digest on hub */
  649. putlog(LOG_WARN, "*", "%s failed encrypted link handshake.", dcc[idx].nick);
  650. killsock(dcc[idx].sock);
  651. lostdcc(idx);
  652. }
  653. return;
  654. }
  655. if (u_pass_match(dcc[idx].user, buf)) {
  656. if (dccauth) {
  657. char randstr[51] = "";
  658. make_rand_str(randstr, 50);
  659. strncpyz(dcc[idx].hash, makehash(dcc[idx].user, randstr), sizeof dcc[idx].hash);
  660. dcc[idx].type = &DCC_CHAT_SECPASS;
  661. dcc[idx].timeval = now;
  662. dprintf(idx, "-Auth %s %s\n", randstr, conf.bot->nick);
  663. } else {
  664. dcc_chat_secpass(idx, buf, atr);
  665. }
  666. } else {
  667. dprintf(idx, "%s\n", response(RES_BADUSERPASS));
  668. putlog(LOG_MISC, "*", DCC_BADLOGIN, dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  669. if (dcc[idx].u.chat->away) { /* su from a dumb user */
  670. /* Turn echo back on for telnet sessions (send IAC WON'T ECHO). */
  671. if (dcc[idx].status & STAT_TELNET)
  672. dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  673. dcc[idx].user = get_user_by_handle(userlist, dcc[idx].u.chat->away);
  674. strcpy(dcc[idx].nick, dcc[idx].u.chat->away);
  675. free(dcc[idx].u.chat->away);
  676. free(dcc[idx].u.chat->su_nick);
  677. dcc[idx].u.chat->away = NULL;
  678. dcc[idx].u.chat->su_nick = NULL;
  679. dcc[idx].type = &DCC_CHAT;
  680. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  681. botnet_send_join_idx(idx);
  682. chanout_but(-1, dcc[idx].u.chat->channel, DCC_JOIN, dcc[idx].nick);
  683. } else {
  684. killsock(dcc[idx].sock);
  685. lostdcc(idx);
  686. }
  687. }
  688. }
  689. static void
  690. eof_dcc_general(int idx)
  691. {
  692. putlog(LOG_MISC, "*", DCC_LOSTDCC, dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  693. killsock(dcc[idx].sock);
  694. lostdcc(idx);
  695. }
  696. static void
  697. tout_dcc_chat_secpass(int idx)
  698. {
  699. putlog(LOG_MISC, "*", DCC_SPWDTIMEOUT, dcc[idx].nick, dcc[idx].host);
  700. killsock(dcc[idx].sock);
  701. lostdcc(idx);
  702. }
  703. static void
  704. display_dcc_chat_secpass(int idx, char *buf)
  705. {
  706. sprintf(buf, "secpass waited %lis", now - dcc[idx].timeval);
  707. }
  708. static void
  709. tout_dcc_chat_pass(int idx)
  710. {
  711. putlog(LOG_MISC, "*", DCC_PWDTIMEOUT, dcc[idx].nick, dcc[idx].host);
  712. killsock(dcc[idx].sock);
  713. lostdcc(idx);
  714. }
  715. static void
  716. display_dcc_chat_pass(int idx, char *buf)
  717. {
  718. sprintf(buf, "pass waited %lis", now - dcc[idx].timeval);
  719. }
  720. static void
  721. kill_dcc_general(int idx, void *x)
  722. {
  723. register struct chat_info *p = (struct chat_info *) x;
  724. if (p) {
  725. if (p->buffer) {
  726. struct msgq *r, *q;
  727. for (r = dcc[idx].u.chat->buffer; r; r = q) {
  728. q = r->next;
  729. free(r->msg);
  730. free(r);
  731. }
  732. }
  733. if (p->away) {
  734. free(p->away);
  735. }
  736. free(p);
  737. }
  738. }
  739. /* Remove the color control codes that mIRC,pIRCh etc use to make
  740. * their client seem so fecking cool! (Sorry, Khaled, you are a nice
  741. * guy, but when you added this feature you forced people to either
  742. * use your *SHAREWARE* client or face screenfulls of crap!)
  743. */
  744. static void
  745. strip_mirc_codes(int flags, char *text)
  746. {
  747. char *dd = text;
  748. while (*text) {
  749. switch (*text) {
  750. case 2: /* Bold text */
  751. if (flags & STRIP_BOLD) {
  752. text++;
  753. continue;
  754. }
  755. break;
  756. case 3: /* mIRC colors? */
  757. if (flags & STRIP_COLOR) {
  758. if (egg_isdigit(text[1])) { /* Is the first char a number? */
  759. text += 2; /* Skip over the ^C and the first digit */
  760. if (egg_isdigit(*text))
  761. text++; /* Is this a double digit number? */
  762. if (*text == ',') { /* Do we have a background color next? */
  763. if (egg_isdigit(text[1]))
  764. text += 2; /* Skip over the first background digit */
  765. if (egg_isdigit(*text))
  766. text++; /* Is it a double digit? */
  767. }
  768. } else
  769. text++;
  770. continue;
  771. }
  772. break;
  773. case 7:
  774. if (flags & STRIP_BELLS) {
  775. text++;
  776. continue;
  777. }
  778. break;
  779. case 0x16: /* Reverse video */
  780. if (flags & STRIP_REV) {
  781. text++;
  782. continue;
  783. }
  784. break;
  785. case 0x1f: /* Underlined text */
  786. if (flags & STRIP_UNDER) {
  787. text++;
  788. continue;
  789. }
  790. break;
  791. case 033:
  792. if (flags & STRIP_ANSI) {
  793. text++;
  794. if (*text == '[') {
  795. text++;
  796. while ((*text == ';') || egg_isdigit(*text))
  797. text++;
  798. if (*text)
  799. text++; /* also kill the following char */
  800. }
  801. continue;
  802. }
  803. break;
  804. }
  805. *dd++ = *text++; /* Move on to the next char */
  806. }
  807. *dd = 0;
  808. }
  809. static void
  810. append_line(int idx, char *line)
  811. {
  812. int l = strlen(line);
  813. struct msgq *p = NULL, *q = NULL;
  814. struct chat_info *c = (dcc[idx].type == &DCC_CHAT) ? dcc[idx].u.chat : dcc[idx].u.file->chat;
  815. if (c->current_lines > 1000) {
  816. /* They're probably trying to fill up the bot nuke the sods :) */
  817. for (p = c->buffer; p; p = q) {
  818. q = p->next;
  819. free(p->msg);
  820. free(p);
  821. }
  822. c->buffer = 0;
  823. dcc[idx].status &= ~STAT_PAGE;
  824. do_boot(idx, conf.bot->nick, "too many pages - senq full");
  825. return;
  826. }
  827. if ((c->line_count < c->max_line) && (c->buffer == NULL)) {
  828. c->line_count++;
  829. tputs(dcc[idx].sock, line, l);
  830. } else {
  831. c->current_lines++;
  832. if (c->buffer == NULL)
  833. q = NULL;
  834. else
  835. for (q = c->buffer; q->next; q = q->next) ;
  836. p = calloc(1, sizeof(struct msgq));
  837. p->len = l;
  838. p->msg = calloc(1, l + 1);
  839. p->next = NULL;
  840. strcpy(p->msg, line);
  841. if (q == NULL)
  842. c->buffer = p;
  843. else
  844. q->next = p;
  845. }
  846. }
  847. static void
  848. out_dcc_general(int idx, char *buf, void *x)
  849. {
  850. register struct chat_info *p = (struct chat_info *) x;
  851. char *y = buf;
  852. strip_mirc_codes(p->strip_flags, buf);
  853. if (dcc[idx].status & STAT_TELNET)
  854. y = add_cr(buf);
  855. if (dcc[idx].status & STAT_PAGE)
  856. append_line(idx, y);
  857. else
  858. tputs(dcc[idx].sock, y, strlen(y));
  859. }
  860. struct dcc_table DCC_CHAT_SECPASS = {
  861. "CHAT_SECPASS",
  862. 0,
  863. eof_dcc_general,
  864. dcc_chat_secpass,
  865. &auth_timeout,
  866. tout_dcc_chat_secpass,
  867. display_dcc_chat_secpass,
  868. kill_dcc_general,
  869. out_dcc_general,
  870. NULL
  871. };
  872. struct dcc_table DCC_CHAT_PASS = {
  873. "CHAT_PASS",
  874. 0,
  875. eof_dcc_general,
  876. dcc_chat_pass,
  877. &password_timeout,
  878. tout_dcc_chat_pass,
  879. display_dcc_chat_pass,
  880. kill_dcc_general,
  881. out_dcc_general,
  882. NULL
  883. };
  884. /* Make sure ansi code is just for color-changing
  885. */
  886. static int
  887. check_ansi(char *v)
  888. {
  889. int count = 2;
  890. if (*v++ != '\033')
  891. return 1;
  892. if (*v++ != '[')
  893. return 1;
  894. while (*v) {
  895. if (*v == 'm')
  896. return 0;
  897. if ((*v != ';') && ((*v < '0') || (*v > '9')))
  898. return count;
  899. v++;
  900. count++;
  901. }
  902. return count;
  903. }
  904. static void
  905. eof_dcc_chat(int idx)
  906. {
  907. putlog(LOG_MISC, "*", DCC_LOSTDCC, dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  908. if (dcc[idx].u.chat->channel >= 0) {
  909. chanout_but(idx, dcc[idx].u.chat->channel, "*** %s lost dcc link.\n", dcc[idx].nick);
  910. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  911. botnet_send_part_idx(idx, "lost dcc link");
  912. }
  913. check_bind_chof(dcc[idx].nick, idx);
  914. killsock(dcc[idx].sock);
  915. lostdcc(idx);
  916. }
  917. static void
  918. dcc_chat(int idx, char *buf, int i)
  919. {
  920. int nathan = 0, doron = 0, fixed = 0;
  921. char *v = NULL, *d = NULL;
  922. strip_telnet(dcc[idx].sock, buf, &i);
  923. if (buf[0] && (buf[0] != dcc_prefix[0]) && !(dcc[idx].user && (dcc[idx].user->flags & USER_NOFLOOD)) &&
  924. detect_dcc_flood(&dcc[idx].timeval, dcc[idx].u.chat, idx))
  925. return;
  926. dcc[idx].timeval = now;
  927. if (buf[0]) {
  928. /* Check for beeps and cancel annoying ones */
  929. v = buf;
  930. d = buf;
  931. while (*v)
  932. switch (*v) {
  933. case 1: /* CTCP ?! */
  934. v++;
  935. break;
  936. case 7: /* Beep - no more than 3 */
  937. nathan++;
  938. if (nathan > 3)
  939. v++;
  940. else
  941. *d++ = *v++;
  942. break;
  943. case 8: /* Backspace - for lame telnet's :) */
  944. if (d > buf) {
  945. d--;
  946. }
  947. v++;
  948. break;
  949. case 27: /* ESC - ansi code? */
  950. doron = check_ansi(v);
  951. /* If it's valid, append a return-to-normal code at the end */
  952. if (!doron) {
  953. *d++ = *v++;
  954. fixed = 1;
  955. } else
  956. v += doron;
  957. break;
  958. case '\r': /* Weird pseudo-linefeed */
  959. v++;
  960. break;
  961. default:
  962. *d++ = *v++;
  963. }
  964. if (fixed)
  965. strcpy(d, "\033[0m");
  966. else
  967. *d = 0;
  968. if (u_pass_match(dcc[idx].user, buf)) { /* user said their password :) */
  969. dprintf(idx, "Sure you want that going to the partyline? ;) (msg to partyline halted.)\n");
  970. } else if (!strncmp(buf, "+Auth ", 6)) { /* ignore extra +Auth lines */
  971. } else if ((!strncmp(buf, dcc_prefix, strlen(dcc_prefix))) || (dcc[idx].u.chat->channel < 0)) {
  972. if (!strncmp(buf, dcc_prefix, strlen(dcc_prefix))) /* strip '.' out */
  973. buf++;
  974. v = newsplit(&buf);
  975. rmspace(buf);
  976. check_bind_dcc(v, idx, buf);
  977. } else if (buf[0] == ',') {
  978. int me = 0;
  979. if ((buf[1] == 'm') && (buf[2] == 'e') && buf[3] == ' ')
  980. me = 1;
  981. for (i = 0; i < dcc_total; i++) {
  982. int ok = 0;
  983. if (dcc[i].type->flags & DCT_MASTER) {
  984. if ((dcc[i].type != &DCC_CHAT) || (dcc[i].u.chat->channel >= 0))
  985. if ((i != idx) || (dcc[idx].status & STAT_ECHO))
  986. ok = 1;
  987. }
  988. if (ok) {
  989. struct userrec *u = get_user_by_handle(userlist, dcc[i].nick);
  990. if (u && (u->flags & USER_MASTER)) {
  991. if (me)
  992. dprintf(i, "-> %s%s\n", dcc[idx].nick, buf + 3);
  993. else
  994. dprintf(i, "-%s-> %s\n", dcc[idx].nick, buf + 1);
  995. }
  996. }
  997. }
  998. } else if (buf[0] == '\'') {
  999. int me = 0;
  1000. if ((buf[1] == 'm') && (buf[2] == 'e') && ((buf[3] == ' ') || (buf[3] == '\'') || (buf[3] == ',')))
  1001. me = 1;
  1002. for (i = 0; i < dcc_total; i++) {
  1003. if (dcc[i].type->flags & DCT_CHAT) {
  1004. if (me)
  1005. dprintf(i, "=> %s%s\n", dcc[idx].nick, buf + 3);
  1006. else
  1007. dprintf(i, "=%s=> %s\n", dcc[idx].nick, buf + 1);
  1008. }
  1009. }
  1010. } else {
  1011. if (dcc[idx].u.chat->away != NULL)
  1012. not_away(idx);
  1013. if (!strncmp(buf, "CTCP_MESSAGE ", 13)) /* irssi */
  1014. buf += 13;
  1015. if (!strncmp(buf, "ACTION ", 7)) {
  1016. buf += 7;
  1017. check_bind_dcc("me", idx, buf);
  1018. } else {
  1019. if (dcc[idx].status & STAT_ECHO)
  1020. chanout_but(-1, dcc[idx].u.chat->channel, "<%s> %s\n", dcc[idx].nick, buf);
  1021. else
  1022. chanout_but(idx, dcc[idx].u.chat->channel, "<%s> %s\n", dcc[idx].nick, buf);
  1023. botnet_send_chan(-1, conf.bot->nick, dcc[idx].nick, dcc[idx].u.chat->channel, buf);
  1024. }
  1025. }
  1026. }
  1027. if (dcc[idx].type == &DCC_CHAT) /* Could have change to files */
  1028. if (dcc[idx].status & STAT_PAGE)
  1029. flush_lines(idx, dcc[idx].u.chat);
  1030. }
  1031. static void
  1032. display_dcc_chat(int idx, char *buf)
  1033. {
  1034. size_t i;
  1035. int colori = 0;
  1036. i = simple_sprintf(buf, "chat flags: ");
  1037. buf[i++] = dcc[idx].status & STAT_CHAT ? 'C' : 'c';
  1038. buf[i++] = dcc[idx].status & STAT_PARTY ? 'P' : 'p';
  1039. buf[i++] = dcc[idx].status & STAT_TELNET ? 'T' : 't';
  1040. buf[i++] = dcc[idx].status & STAT_ECHO ? 'E' : 'e';
  1041. buf[i++] = dcc[idx].status & STAT_PAGE ? 'P' : 'p';
  1042. if ((colori = coloridx(idx)))
  1043. buf[i++] = colori == 1 ? 'A' : 'M';
  1044. #ifdef USE_IPV6
  1045. if (sockprotocol(dcc[idx].sock) == AF_INET6 && dcc[idx].addr6[0])
  1046. buf[i++] = '6';
  1047. #endif /* USE_IPV6 */
  1048. simple_sprintf(buf + i, "/%d", dcc[idx].u.chat->channel);
  1049. }
  1050. struct dcc_table DCC_CHAT = {
  1051. "CHAT",
  1052. DCT_CHAT | DCT_MASTER | DCT_SHOWWHO | DCT_VALIDIDX | DCT_SIMUL | DCT_CANBOOT | DCT_REMOTEWHO,
  1053. eof_dcc_chat,
  1054. dcc_chat,
  1055. NULL,
  1056. NULL,
  1057. display_dcc_chat,
  1058. kill_dcc_general,
  1059. out_dcc_general,
  1060. NULL
  1061. };
  1062. static int lasttelnets;
  1063. static char lasttelnethost[81];
  1064. static time_t lasttelnettime;
  1065. /* A modified detect_flood for incoming telnet flood protection.
  1066. */
  1067. static int
  1068. detect_telnet_flood(char *floodhost)
  1069. {
  1070. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  1071. get_user_flagrec(get_user_by_host(floodhost), &fr, NULL);
  1072. if (!flood_telnet_thr || glob_noflood(fr))
  1073. return 0; /* No flood protection */
  1074. if (egg_strcasecmp(lasttelnethost, floodhost)) { /* New... */
  1075. strcpy(lasttelnethost, floodhost);
  1076. lasttelnettime = now;
  1077. lasttelnets = 0;
  1078. return 0;
  1079. }
  1080. if (lasttelnettime < now - flood_telnet_time) {
  1081. /* Flood timer expired, reset it */
  1082. lasttelnettime = now;
  1083. lasttelnets = 0;
  1084. return 0;
  1085. }
  1086. lasttelnets++;
  1087. if (lasttelnets >= flood_telnet_thr) { /* FLOOD! */
  1088. /* Reset counters */
  1089. lasttelnets = 0;
  1090. lasttelnettime = 0;
  1091. lasttelnethost[0] = 0;
  1092. putlog(LOG_MISC, "*", IRC_TELNETFLOOD, floodhost);
  1093. addignore(floodhost, origbotname, "Telnet connection flood", now + (60 * ignore_time));
  1094. return 1;
  1095. }
  1096. return 0;
  1097. }
  1098. static void
  1099. dcc_telnet(int idx, char *buf, int i)
  1100. {
  1101. IP ip;
  1102. port_t port;
  1103. int j = 0, sock;
  1104. char s[UHOSTLEN + 1] = "", x[1024] = "";
  1105. if (dcc_total + 1 > max_dcc) {
  1106. j = answer(dcc[idx].sock, s, &ip, &port, 0);
  1107. if (j != -1) {
  1108. dprintf(-j, "Sorry, too many connections already.\r\n");
  1109. killsock(j);
  1110. }
  1111. return;
  1112. }
  1113. sock = answer(dcc[idx].sock, s, &ip, &port, 0);
  1114. while ((sock == -1) && (errno == EAGAIN))
  1115. sock = answer(sock, s, &ip, &port, 0);
  1116. /*. ssl_link ACCEPT_SSL should go here!!!! */
  1117. if (sock < 0) {
  1118. putlog(LOG_MISC, "*", DCC_FAILED, strerror(errno));
  1119. return;
  1120. }
  1121. /* Buffer data received on this socket. */
  1122. sockoptions(sock, EGG_OPTION_SET, SOCK_BUFFER);
  1123. #if SIZEOF_SHORT == 2
  1124. if (port < 1024) {
  1125. #else
  1126. if (port < 1024 || port > 65535) {
  1127. #endif
  1128. putlog(LOG_BOTS, "*", DCC_BADSRC, s, port);
  1129. killsock(sock);
  1130. return;
  1131. }
  1132. sprintf(x, "-telnet!telnet@%s", iptostr(htonl(ip)));
  1133. if (match_ignore(x) || detect_telnet_flood(x)) {
  1134. killsock(sock);
  1135. return;
  1136. }
  1137. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1138. dcc[i].sock = sock;
  1139. dcc[i].addr = ip;
  1140. #ifdef USE_IPV6
  1141. if (sockprotocol(sock) == AF_INET6)
  1142. strcpy(dcc[i].addr6, s);
  1143. #endif /* USE_IPV6 */
  1144. dcc[i].port = port;
  1145. dcc[i].timeval = now;
  1146. strcpy(dcc[i].nick, "*");
  1147. dcc[i].u.dns->ip = ip;
  1148. dcc[i].u.dns->dns_success = dcc_telnet_hostresolved;
  1149. dcc[i].u.dns->dns_failure = dcc_telnet_hostresolved;
  1150. dcc[i].u.dns->dns_type = RES_HOSTBYIP;
  1151. dcc[i].u.dns->ibuf = dcc[idx].sock;
  1152. dcc[i].u.dns->type = &DCC_IDENTWAIT;
  1153. #ifdef USE_IPV6
  1154. if (sockprotocol(sock) == AF_INET6)
  1155. dcc_telnet_hostresolved(i);
  1156. else
  1157. #endif /* USE_IPV6 */
  1158. dcc_dnshostbyip(ip);
  1159. }
  1160. static void
  1161. dcc_telnet_hostresolved(int i)
  1162. {
  1163. int idx;
  1164. int j = 0, sock;
  1165. char s[UHOSTLEN] = "", s2[UHOSTLEN + 20] = "";
  1166. #ifdef USE_IPV6
  1167. if (sockprotocol(dcc[i].sock) == AF_INET6 && dcc[i].addr6[0])
  1168. strncpyz(dcc[i].host, dcc[i].addr6, UHOSTLEN);
  1169. else
  1170. #endif /* USE_IPV6 */
  1171. strncpyz(dcc[i].host, dcc[i].u.dns->host, UHOSTLEN);
  1172. for (idx = 0; idx < dcc_total; idx++)
  1173. if ((dcc[idx].type == &DCC_TELNET) && (dcc[idx].sock == dcc[i].u.dns->ibuf)) {
  1174. break;
  1175. }
  1176. sprintf(s2, "-telnet!telnet@%s", dcc[i].host);
  1177. if (match_ignore(s2) || detect_telnet_flood(s2)) {
  1178. killsock(dcc[i].sock);
  1179. lostdcc(i);
  1180. return;
  1181. }
  1182. if (dcc_total == idx) {
  1183. putlog(LOG_BOTS, "*", "Lost listening socket while resolving %s", dcc[i].host);
  1184. killsock(dcc[i].sock);
  1185. lostdcc(i);
  1186. return;
  1187. }
  1188. if (dcc[idx].host[0] == '@') {
  1189. /* Restrict by hostname */
  1190. if (!wild_match(dcc[idx].host + 1, dcc[i].host)) {
  1191. putlog(LOG_BOTS, "*", DCC_BADHOST, s);
  1192. killsock(dcc[i].sock);
  1193. lostdcc(i);
  1194. return;
  1195. }
  1196. }
  1197. /* . ssl_link(dcc[i].sock, ACCEPT_SSL); */
  1198. changeover_dcc(i, &DCC_IDENTWAIT, 0);
  1199. dcc[i].timeval = now;
  1200. dcc[i].u.ident_sock = dcc[idx].sock;
  1201. #ifdef USE_IPV6
  1202. if (sockprotocol(dcc[idx].sock) == AF_INET6)
  1203. sock = open_telnet(dcc[i].host, 113);
  1204. else
  1205. #endif /* USE_IPV6 */
  1206. sock = open_telnet(iptostr(htonl(dcc[i].addr)), 113);
  1207. putlog(LOG_MISC, "*", DCC_TELCONN, dcc[i].host, dcc[i].port);
  1208. s[0] = 0;
  1209. if (sock < 0) {
  1210. if (sock == -2)
  1211. strcpy(s, "DNS lookup failed for ident");
  1212. else
  1213. strcpy(s, strerror(errno));
  1214. } else {
  1215. j = new_dcc(&DCC_IDENT, 0);
  1216. if (j < 0) {
  1217. killsock(sock);
  1218. strcpy(s, "No Free DCC's");
  1219. }
  1220. }
  1221. if (s[0]) {
  1222. putlog(LOG_MISC, "*", DCC_IDENTFAIL, dcc[i].host, s);
  1223. sprintf(s, "telnet@%s", dcc[i].host);
  1224. dcc_telnet_got_ident(i, s);
  1225. return;
  1226. }
  1227. dcc[j].sock = sock;
  1228. dcc[j].port = 113;
  1229. dcc[j].addr = dcc[i].addr;
  1230. strcpy(dcc[j].host, dcc[i].host);
  1231. strcpy(dcc[j].nick, "*");
  1232. dcc[j].u.ident_sock = dcc[i].sock;
  1233. dcc[j].timeval = now;
  1234. dprintf(j, "%d, %d\n", dcc[i].port, dcc[idx].port);
  1235. }
  1236. static void
  1237. eof_dcc_telnet(int idx)
  1238. {
  1239. putlog(LOG_MISC, "*", DCC_PORTDIE, dcc[idx].port);
  1240. killsock(dcc[idx].sock);
  1241. lostdcc(idx);
  1242. }
  1243. static void
  1244. display_telnet(int idx, char *buf)
  1245. {
  1246. sprintf(buf, "lstn %d%s", dcc[idx].port, (dcc[idx].status & LSTN_PUBLIC) ? " pub" : "");
  1247. }
  1248. struct dcc_table DCC_TELNET = {
  1249. "TELNET",
  1250. DCT_LISTEN,
  1251. eof_dcc_telnet,
  1252. dcc_telnet,
  1253. NULL,
  1254. NULL,
  1255. display_telnet,
  1256. NULL,
  1257. NULL,
  1258. NULL
  1259. };
  1260. static void
  1261. eof_dcc_dupwait(int idx)
  1262. {
  1263. putlog(LOG_BOTS, "*", DCC_LOSTDUP, dcc[idx].host);
  1264. killsock(dcc[idx].sock);
  1265. lostdcc(idx);
  1266. }
  1267. static void
  1268. dcc_dupwait(int idx, char *buf, int i)
  1269. {
  1270. /* We just ignore any data at this point. */
  1271. return;
  1272. }
  1273. /* We now check again. If the bot is still marked as duplicate, there is no
  1274. * botnet lag we could push it on, so we just drop the connection.
  1275. */
  1276. static void
  1277. timeout_dupwait(int idx)
  1278. {
  1279. char x[100] = "";
  1280. /* Still duplicate? */
  1281. if (in_chain(dcc[idx].nick)) {
  1282. egg_snprintf(x, sizeof x, "%s!%s", dcc[idx].nick, dcc[idx].host);
  1283. putlog(LOG_BOTS, "*", DCC_DUPLICATE, x);
  1284. killsock(dcc[idx].sock);
  1285. lostdcc(idx);
  1286. } else {
  1287. /* Ha! Now it's gone and we can grant this bot access. */
  1288. dcc_telnet_pass(idx, dcc[idx].u.dupwait->atr);
  1289. }
  1290. }
  1291. static void
  1292. display_dupwait(int idx, char *buf)
  1293. {
  1294. sprintf(buf, "wait duplicate?");
  1295. }
  1296. static void
  1297. kill_dupwait(int idx, void *x)
  1298. {
  1299. register struct dupwait_info *p = (struct dupwait_info *) x;
  1300. if (p) {
  1301. if (p->chat && DCC_CHAT.kill)
  1302. DCC_CHAT.kill(idx, p->chat);
  1303. free(p);
  1304. }
  1305. }
  1306. struct dcc_table DCC_DUPWAIT = {
  1307. "DUPWAIT",
  1308. DCT_VALIDIDX,
  1309. eof_dcc_dupwait,
  1310. dcc_dupwait,
  1311. &dupwait_timeout,
  1312. timeout_dupwait,
  1313. display_dupwait,
  1314. kill_dupwait,
  1315. NULL,
  1316. NULL
  1317. };
  1318. /* This function is called if a bot gets removed from the list. It checks
  1319. * wether we have a pending duplicate connection for that bot and continues
  1320. * with the login in that case.
  1321. */
  1322. void
  1323. dupwait_notify(const char *who)
  1324. {
  1325. register int idx;
  1326. Assert(who);
  1327. for (idx = 0; idx < dcc_total; idx++)
  1328. if ((dcc[idx].type == &DCC_DUPWAIT) && !egg_strcasecmp(dcc[idx].nick, who)) {
  1329. dcc_telnet_pass(idx, dcc[idx].u.dupwait->atr);
  1330. break;
  1331. }
  1332. }
  1333. static void
  1334. dcc_telnet_id(int idx, char *buf, int atr)
  1335. {
  1336. int ok = 0;
  1337. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  1338. strip_telnet(dcc[idx].sock, buf, &atr);
  1339. buf[HANDLEN] = 0;
  1340. /* Toss out bad nicknames */
  1341. if ((dcc[idx].nick[0] != '@') && (!wild_match(dcc[idx].nick, buf))) {
  1342. /* FIXME: if a bot gets this, they will try to decrypt it ;/ */
  1343. dprintf(idx, "Sorry, that nickname format is invalid.\n");
  1344. putlog(LOG_BOTS, "*", DCC_BADNICK, dcc[idx].host);
  1345. killsock(dcc[idx].sock);
  1346. lostdcc(idx);
  1347. return;
  1348. }
  1349. dcc[idx].user = get_user_by_handle(userlist, buf);
  1350. get_user_flagrec(dcc[idx].user, &fr, NULL);
  1351. /* if (!ok && glob_party(fr))
  1352. ok = 1;*/
  1353. ok = 1;
  1354. #ifdef HUB
  1355. if (!glob_huba(fr))
  1356. ok = 0;
  1357. #else /* !HUB */
  1358. /* if I am a chanhub and they dont have +c then drop */
  1359. if (ischanhub() && !glob_chuba(fr))
  1360. ok = 0;
  1361. if (!ischanhub())
  1362. ok = 0;
  1363. #endif /* HUB */
  1364. if (!ok && glob_bot(fr))
  1365. ok = 1;
  1366. if (!ok) {
  1367. putlog(LOG_BOTS, "*", DCC_INVHANDLE, dcc[idx].host, buf);
  1368. killsock(dcc[idx].sock);
  1369. lostdcc(idx);
  1370. return;
  1371. }
  1372. correct_handle(buf);
  1373. strcpy(dcc[idx].nick, buf);
  1374. if (glob_bot(fr)) {
  1375. if (!egg_strcasecmp(conf.bot->nick, dcc[idx].nick)) {
  1376. putlog(LOG_BOTS, "*", DCC_MYBOTNETNICK, dcc[idx].host);
  1377. killsock(dcc[idx].sock);
  1378. lostdcc(idx);
  1379. return;
  1380. } else if (in_chain(dcc[idx].nick)) {
  1381. struct chat_info *ci;
  1382. ci = dcc[idx].u.chat;
  1383. dcc[idx].type = &DCC_DUPWAIT;
  1384. dcc[idx].u.dupwait = calloc(1, sizeof(struct dupwait_info));
  1385. dcc[idx].u.dupwait->chat = ci;
  1386. dcc[idx].u.dupwait->atr = atr;
  1387. return;
  1388. }
  1389. }
  1390. dcc_telnet_pass(idx, atr);
  1391. }
  1392. static void
  1393. dcc_telnet_pass(int idx, int atr)
  1394. {
  1395. int ok = 0, i, ok2;
  1396. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  1397. get_user_flagrec(dcc[idx].user, &fr, NULL);
  1398. /* No password set? */
  1399. if (!glob_bot(fr) && (u_pass_match(dcc[idx].user, "-"))) {
  1400. dprintf(idx, "Can't telnet until you have a password set.\r\n");
  1401. putlog(LOG_MISC, "*", DCC_NOPASS, dcc[idx].nick, dcc[idx].host);
  1402. killsock(dcc[idx].sock);
  1403. lostdcc(idx);
  1404. return;
  1405. }
  1406. ok = 0;
  1407. if (dcc[idx].type == &DCC_DUPWAIT) {
  1408. struct chat_info *ci;
  1409. ci = dcc[idx].u.dupwait->chat;
  1410. free(dcc[idx].u.dupwait);
  1411. dcc[idx].u.chat = ci;
  1412. }
  1413. dcc[idx].type = &DCC_CHAT_PASS;
  1414. dcc[idx].timeval = now;
  1415. ok2 = 1;
  1416. #ifdef HUB
  1417. if (!glob_huba(fr))
  1418. ok2 = 0;
  1419. #else /* !HUB */
  1420. if (ischanhub() && !glob_chuba(fr))
  1421. ok2 = 0;
  1422. #endif /* HUB */
  1423. if (ok2) {
  1424. ok = 1;
  1425. dcc[idx].status |= STAT_PARTY;
  1426. }
  1427. if (glob_bot(fr))
  1428. ok = 1;
  1429. if (!ok) {
  1430. struct chat_info *ci;
  1431. ci = dcc[idx].u.chat;
  1432. dcc[idx].u.file = calloc(1, sizeof(struct file_info));
  1433. dcc[idx].u.file->chat = ci;
  1434. }
  1435. if (glob_bot(fr)) {
  1436. int snum = -1;
  1437. for (i = 0; i < MAXSOCKS; i++) {
  1438. if (!(socklist[i].flags & SOCK_UNUSED)
  1439. && (socklist[i].sock == dcc[idx].sock)) {
  1440. snum = i;
  1441. break;
  1442. }
  1443. }
  1444. if (snum >= 0) {
  1445. char initkey[33] = "", *tmp2 = NULL;
  1446. char tmp[256] = "", 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", 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(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 = 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. }