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