dcc.c 48 KB

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