dcc.c 50 KB

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