botnet.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * botnet.c -- handles:
  22. * keeping track of which bot's connected where in the chain
  23. * dumping a list of bots or a bot tree to a user
  24. * channel name associations on the party line
  25. * rejecting a bot
  26. * linking, unlinking, and relaying to another bot
  27. * pinging the bots periodically and checking leaf status
  28. *
  29. */
  30. #include "common.h"
  31. #include "botnet.h"
  32. #include "color.h"
  33. #include "chanprog.h"
  34. #include "net.h"
  35. #include "socket.h"
  36. #include "adns.h"
  37. #include "match.h"
  38. #include "users.h"
  39. #include "misc.h"
  40. #include "userrec.h"
  41. #include "main.h"
  42. #include "dccutil.h"
  43. #include "dcc.h"
  44. #include "botmsg.h"
  45. #include "tandem.h"
  46. #include "core_binds.h"
  47. tand_t *tandbot = NULL; /* Keep track of tandem bots on the
  48. botnet */
  49. party_t *party = NULL; /* Keep track of people on the botnet */
  50. int tands = 0; /* Number of bots on the botnet */
  51. static int maxparty = 50; /* Maximum space for party line members
  52. currently */
  53. static int parties = 0; /* Number of people on the botnet */
  54. static int share_unlinks = 1; /* Allow remote unlinks of my
  55. sharebots? */
  56. void init_party()
  57. {
  58. party = (party_t *) my_calloc(1, maxparty * sizeof(party_t));
  59. }
  60. tand_t *findbot(const char *who)
  61. {
  62. for (tand_t* ptr = tandbot; ptr; ptr = ptr->next)
  63. if (!strcasecmp(ptr->bot, who))
  64. return ptr;
  65. return NULL;
  66. }
  67. extern void counter_clear(const char* botnick);
  68. /* Add a tandem bot to our chain list
  69. */
  70. void addbot(char *who, char *from, char *next, char flag, int vlocalhub, time_t vbuildts, char *vcommit, char *vversion)
  71. {
  72. tand_t **ptr = &tandbot, *ptr2 = NULL;
  73. while (*ptr) {
  74. if (!strcasecmp((*ptr)->bot, who))
  75. putlog(LOG_BOTS, "*", "!!! Duplicate botnet bot entry!!");
  76. ptr = &((*ptr)->next);
  77. }
  78. ptr2 = (tand_t *) my_calloc(1, sizeof(tand_t));
  79. strlcpy(ptr2->bot, who, HANDLEN + 1);
  80. ptr2->bot[HANDLEN] = 0;
  81. ptr2->share = flag;
  82. ptr2->localhub = vlocalhub;
  83. ptr2->buildts = vbuildts;
  84. strlcpy(ptr2->commit, vcommit, sizeof(ptr2->commit));
  85. if (vversion && vversion[0])
  86. strlcpy(ptr2->version, vversion, 121);
  87. ptr2->next = *ptr;
  88. *ptr = ptr2;
  89. /* May be via itself */
  90. ptr2->via = findbot(from);
  91. /* Look up the bot in internal users */
  92. ptr2->hub = is_hub(who);
  93. /* Cache user record */
  94. ptr2->u = userlist ? get_user_by_handle(userlist, who) : NULL;
  95. if (!strcasecmp(next, conf.bot->nick))
  96. ptr2->uplink = (tand_t *) 1;
  97. else
  98. ptr2->uplink = findbot(next);
  99. tands++;
  100. counter_clear(who);
  101. }
  102. #ifdef G_BACKUP
  103. void check_should_backup()
  104. {
  105. struct chanset_t *chan = NULL;
  106. for (chan = chanset; chan; chan = chan->next) {
  107. if (chan->channel.backup_time && (chan->channel.backup_time < now) && !channel_backup(chan)) {
  108. do_chanset(NULL, chan, STR("+backup"), DO_LOCAL | DO_NET);
  109. chan->channel.backup_time = 0;
  110. }
  111. }
  112. }
  113. #endif /* G_BACKUP */
  114. void updatebot(int idx, char *who, char share, int vlocalhub, time_t vbuildts, char *vcommit, char *vversion)
  115. {
  116. tand_t *ptr = findbot(who);
  117. if (ptr) {
  118. if (share)
  119. ptr->share = share;
  120. if (vlocalhub)
  121. ptr->localhub = vlocalhub;
  122. if (vbuildts)
  123. ptr->buildts = vbuildts;
  124. if (vcommit)
  125. strlcpy(ptr->commit, vcommit, sizeof(ptr->commit));
  126. if (vversion && vversion[0])
  127. strlcpy(ptr->version, vversion, 121);
  128. botnet_send_update(idx, ptr);
  129. }
  130. }
  131. /* New botnet member
  132. */
  133. int addparty(char *bot, char *nick, int chan, char flag, int sock, char *from, int *idx)
  134. {
  135. int i = 0;
  136. for (i = 0; i < parties; i++) {
  137. /* Just changing the channel of someone already on? */
  138. if (!strcasecmp(party[i].bot, bot) && (party[i].sock == sock)) {
  139. int oldchan = party[i].chan;
  140. party[i].chan = chan;
  141. party[i].timer = now;
  142. if (from[0]) {
  143. if (flag == ' ')
  144. flag = '-';
  145. party[i].flag = flag;
  146. if (party[i].from)
  147. free(party[i].from);
  148. party[i].from = strdup(from);
  149. }
  150. *idx = i;
  151. return oldchan;
  152. }
  153. }
  154. /* New member */
  155. if (parties == maxparty) {
  156. maxparty += 50;
  157. party = (party_t *) my_realloc((void *) party, maxparty * sizeof(party_t));
  158. }
  159. strlcpy(party[parties].nick, nick, sizeof(party[parties].nick));
  160. strlcpy(party[parties].bot, bot, sizeof(party[parties].bot));
  161. party[parties].chan = chan;
  162. party[parties].sock = sock;
  163. party[parties].status = 0;
  164. party[parties].away = 0;
  165. party[parties].timer = now; /* cope. */
  166. if (from[0]) {
  167. if (flag == ' ')
  168. flag = '-';
  169. party[parties].flag = flag;
  170. party[parties].from = strdup(from);
  171. } else {
  172. party[parties].flag = ' ';
  173. party[parties].from = strdup("(unknown)");
  174. }
  175. *idx = parties;
  176. parties++;
  177. return -1;
  178. }
  179. /* Alter status flags for remote party-line user.
  180. */
  181. void partystat(char *bot, int sock, int add, int rem)
  182. {
  183. for (int i = 0; i < parties; i++) {
  184. if ((!strcasecmp(party[i].bot, bot)) &&
  185. (party[i].sock == sock)) {
  186. party[i].status |= add;
  187. party[i].status &= ~rem;
  188. }
  189. }
  190. }
  191. /* Other bot is sharing idle info.
  192. */
  193. void partysetidle(char *bot, int sock, int secs)
  194. {
  195. for (int i = 0; i < parties; i++) {
  196. if ((!strcasecmp(party[i].bot, bot)) &&
  197. (party[i].sock == sock)) {
  198. party[i].timer = (now - (time_t) secs);
  199. }
  200. }
  201. }
  202. /* Return someone's chat channel.
  203. */
  204. int getparty(char *bot, int sock)
  205. {
  206. for (int i = 0; i < parties; i++) {
  207. if (!strcasecmp(party[i].bot, bot) &&
  208. (party[i].sock == sock)) {
  209. return i;
  210. }
  211. }
  212. return -1;
  213. }
  214. /* Un-idle someone
  215. */
  216. bool partyidle(char *bot, char *nick)
  217. {
  218. bool ok = 0;
  219. for (int i = 0; i < parties; i++) {
  220. if ((!strcasecmp(party[i].bot, bot)) && (!strcasecmp(party[i].nick, nick))) {
  221. party[i].timer = now;
  222. ok = 1;
  223. }
  224. }
  225. return ok;
  226. }
  227. /* Change someone's nick
  228. */
  229. int partynick(char *bot, int sock, char *nick)
  230. {
  231. char work[HANDLEN + 1] = "";
  232. for (int i = 0; i < parties; i++) {
  233. if (!strcasecmp(party[i].bot, bot) && (party[i].sock == sock)) {
  234. strlcpy(work, party[i].nick, sizeof(work));
  235. strlcpy(party[i].nick, nick, sizeof(party[i].nick));
  236. strlcpy(nick, work, HANDLEN + 1);
  237. return i;
  238. }
  239. }
  240. return -1;
  241. }
  242. /* Set away message
  243. */
  244. void partyaway(char *bot, int sock, char *msg)
  245. {
  246. for (int i = 0; i < parties; i++) {
  247. if ((!strcasecmp(party[i].bot, bot)) &&
  248. (party[i].sock == sock)) {
  249. if (party[i].away)
  250. free(party[i].away);
  251. if (msg[0]) {
  252. party[i].away = strdup(msg);
  253. } else
  254. party[i].away = 0;
  255. }
  256. }
  257. }
  258. /* Remove a tandem bot from the chain list
  259. */
  260. void rembot(const char *whoin)
  261. {
  262. tand_t **ptr = &tandbot, *ptr2 = NULL;
  263. char *who = strdup(whoin);
  264. while (*ptr) {
  265. if (!strcasecmp((*ptr)->bot, who))
  266. break;
  267. ptr = &((*ptr)->next);
  268. }
  269. if (!*ptr) {
  270. /* May have just .unlink *'d */
  271. free(who);
  272. return;
  273. }
  274. struct userrec *u = get_user_by_handle(userlist, (char *) who);
  275. if (u)
  276. touch_laston(u, "unlinked", now);
  277. ptr2 = *ptr;
  278. *ptr = ptr2->next;
  279. free(ptr2);
  280. tands--;
  281. dupwait_notify(who);
  282. free(who);
  283. }
  284. void remparty(char *bot, int sock)
  285. {
  286. for (int i = 0; i < parties; i++)
  287. if ((!strcasecmp(party[i].bot, bot)) &&
  288. (party[i].sock == sock)) {
  289. parties--;
  290. if (party[i].from)
  291. free(party[i].from);
  292. if (party[i].away)
  293. free(party[i].away);
  294. if (i < parties) {
  295. strlcpy(party[i].bot, party[parties].bot, sizeof(party[i].bot));
  296. strlcpy(party[i].nick, party[parties].nick, sizeof(party[i].nick));
  297. party[i].chan = party[parties].chan;
  298. party[i].sock = party[parties].sock;
  299. party[i].flag = party[parties].flag;
  300. party[i].status = party[parties].status;
  301. party[i].timer = party[parties].timer;
  302. party[i].from = party[parties].from;
  303. party[i].away = party[parties].away;
  304. }
  305. }
  306. }
  307. /* Cancel every user that was on a certain bot
  308. */
  309. void rempartybot(char *bot)
  310. {
  311. for (int i = 0; i < parties; i++)
  312. if (!strcasecmp(party[i].bot, bot)) {
  313. remparty(bot, party[i].sock);
  314. i--;
  315. }
  316. }
  317. /* Remove every bot linked 'via' bot <x>
  318. */
  319. void unvia(int idx, tand_t *who)
  320. {
  321. if (!who)
  322. return; /* Safety */
  323. tand_t *bot = NULL, *bot2 = NULL;
  324. rempartybot(who->bot);
  325. bot = tandbot;
  326. while (bot) {
  327. if (bot->uplink == who) {
  328. unvia(idx, bot);
  329. bot2 = bot->next;
  330. rembot(bot->bot);
  331. bot = bot2;
  332. } else
  333. bot = bot->next;
  334. }
  335. }
  336. /* Return index into dcc list of the bot that connects us to bot <x>
  337. */
  338. int nextbot(const char *who)
  339. {
  340. tand_t *bot = findbot(who);
  341. if (!bot)
  342. return -1;
  343. for (int j = 0; j < dcc_total; j++) {
  344. if (dcc[j].type && bot->via && !strcasecmp(bot->via->bot, dcc[j].nick) && (dcc[j].type == &DCC_BOT))
  345. return j;
  346. }
  347. return -1; /* We're not connected to 'via' */
  348. }
  349. /* Return name of the bot that is directly connected to bot X
  350. */
  351. char *lastbot(const char *who)
  352. {
  353. tand_t *bot = findbot(who);
  354. if (!bot)
  355. return "*";
  356. else if (bot->uplink == (tand_t *) 1)
  357. return conf.bot->nick;
  358. else
  359. return bot->uplink->bot;
  360. }
  361. /* Modern version of 'whom' (use local data)
  362. */
  363. void answer_local_whom(int idx, int chan)
  364. {
  365. char format[81] = "", c = 0, idle[40] = "";
  366. int i, t, nicklen, botnicklen, total = 0;
  367. if (chan == (-1))
  368. dprintf(idx, "%s (+: %s, *: %s)\n", "Users across the botnet", "Party line", "Local channel");
  369. else if (chan > 0)
  370. dprintf(idx, "%s %s%d:\n", "Users on channel", (chan < GLOBAL_CHANS) ? "" : "*", chan % GLOBAL_CHANS);
  371. /* Find longest nick and botnick */
  372. nicklen = botnicklen = 0;
  373. for (i = 0; i < dcc_total; i++) {
  374. if (dcc[i].type && dcc[i].simul == -1 && dcc[i].type == &DCC_CHAT) {
  375. if ((chan == (-1)) || ((chan >= 0) && (dcc[i].u.chat->channel == chan))) {
  376. t = strlen(dcc[i].nick); if(t > nicklen) nicklen = t;
  377. t = strlen(conf.bot->nick); if(t > botnicklen) botnicklen = t;
  378. }
  379. }
  380. }
  381. for (i = 0; i < parties; i++) {
  382. if ((chan == (-1)) || ((chan >= 0) && (party[i].chan == chan))) {
  383. t = strlen(party[i].nick); if(t > nicklen) nicklen = t;
  384. t = strlen(party[i].bot); if(t > botnicklen) botnicklen = t;
  385. }
  386. }
  387. if(nicklen < 9) nicklen = 9;
  388. if(botnicklen < 9) botnicklen = 9;
  389. if (conf.bot->hub) {
  390. simple_snprintf(format, sizeof format, "%%-%us %%-%us %%s\n", nicklen, botnicklen);
  391. dprintf(idx, format, " Nick", " Bot", " Host");
  392. dprintf(idx, format, "----------", "---------", "--------------------");
  393. simple_snprintf(format, sizeof format, "%%c%%-%us %%c %%-%us %%s%%s\n", nicklen, botnicklen);
  394. } else {
  395. simple_snprintf(format, sizeof format, "%%-%us\n", nicklen);
  396. dprintf(idx, format, " Nick");
  397. dprintf(idx, format, "----------");
  398. simple_snprintf(format, sizeof format, "%%c%%-%us %%c %%s\n", nicklen);
  399. }
  400. for (i = 0; i < dcc_total; i++) {
  401. if (dcc[i].type && dcc[i].simul == -1 && dcc[i].type == &DCC_CHAT) {
  402. if ((chan == (-1)) || ((chan >= 0) && (dcc[i].u.chat->channel == chan))) {
  403. c = geticon(i);
  404. if (c == '-')
  405. c = ' ';
  406. if (now - dcc[i].timeval > 300) {
  407. unsigned long mydays, hrs, mins;
  408. mydays = (now - dcc[i].timeval) / 86400;
  409. hrs = ((now - dcc[i].timeval) - (mydays * 86400)) / 3600;
  410. mins = ((now - dcc[i].timeval) - (hrs * 3600)) / 60;
  411. if (mydays > 0)
  412. simple_snprintf(idle, sizeof(idle), " [idle %lud%luh]", mydays, hrs);
  413. else if (hrs > 0)
  414. simple_snprintf(idle, sizeof(idle), " [idle %luh%lum]", hrs, mins);
  415. else
  416. simple_snprintf(idle, sizeof(idle), " [idle %lum]", mins);
  417. } else
  418. idle[0] = 0;
  419. total++;
  420. if (conf.bot->hub)
  421. dprintf(idx, format, c, dcc[i].nick,
  422. (dcc[i].u.chat->channel == 0) && (chan == (-1)) ? '+' :
  423. (dcc[i].u.chat->channel > GLOBAL_CHANS) &&
  424. (chan == (-1)) ? '*' : ' ', conf.bot->nick, dcc[i].host, idle);
  425. else
  426. dprintf(idx, format, c, dcc[i].nick,
  427. (dcc[i].u.chat->channel == 0) && (chan == (-1)) ? '+' :
  428. (dcc[i].u.chat->channel > GLOBAL_CHANS) &&
  429. (chan == (-1)) ? '*' : ' ', idle);
  430. if (dcc[i].u.chat->away != NULL)
  431. dprintf(idx, " AWAY: %s\n", dcc[i].u.chat->away);
  432. if (dcc[i].u.chat->su_nick && (dcc[idx].user->flags & USER_ADMIN))
  433. dprintf(idx, " SU FROM: %s\n", dcc[i].u.chat->su_nick);
  434. }
  435. }
  436. }
  437. for (i = 0; i < parties; i++) {
  438. if ((chan == (-1)) || ((chan >= 0) && (party[i].chan == chan))) {
  439. c = party[i].flag;
  440. if (c == '-')
  441. c = ' ';
  442. if (party[i].timer == 0L)
  443. strlcpy(idle, " [idle?]", sizeof(idle));
  444. else if (now - party[i].timer > 300) {
  445. unsigned long mydays, hrs, mins;
  446. mydays = (now - party[i].timer) / 86400;
  447. hrs = ((now - party[i].timer) - (mydays * 86400)) / 3600;
  448. mins = ((now - party[i].timer) - (hrs * 3600)) / 60;
  449. if (mydays > 0)
  450. simple_snprintf(idle, sizeof(idle), " [idle %lud%luh]", mydays, hrs);
  451. else if (hrs > 0)
  452. simple_snprintf(idle, sizeof(idle), " [idle %luh%lum]", hrs, mins);
  453. else
  454. simple_snprintf(idle, sizeof(idle), " [idle %lum]", mins);
  455. } else
  456. idle[0] = 0;
  457. total++;
  458. if (conf.bot->hub)
  459. dprintf(idx, format, c, party[i].nick, (party[i].chan == 0) && (chan == (-1)) ? '+' : ' ',
  460. party[i].bot, party[i].from, idle);
  461. else
  462. dprintf(idx, format, c, party[i].nick, (party[i].chan == 0) && (chan == (-1)) ? '+' : ' ', idle);
  463. if (party[i].status & PLSTAT_AWAY)
  464. dprintf(idx, " AWAY: %s\n", party[i].away ? party[i].away : "");
  465. }
  466. }
  467. dprintf(idx, "Total users: %d\n", total);
  468. }
  469. /* Show z a list of all bots connected
  470. */
  471. void
  472. tell_bots(int idx, int up, const char *nodename)
  473. {
  474. struct userrec *u = NULL;
  475. int cnt = 0, tot = 0, total = 1, mtot = 0;
  476. char work[151] = "", *node = NULL;
  477. if (up) {
  478. if (nodename)
  479. node = (char *) get_user(&USERENTRY_NODENAME, conf.bot->u);
  480. if (!nodename || wild_match(nodename, node)) {
  481. strlcat(work, conf.bot->nick, sizeof(work));
  482. strlcat(work, " ", sizeof(work));
  483. cnt++;
  484. tot++;
  485. if (nodename)
  486. mtot++;
  487. }
  488. }
  489. for (u = userlist; u; u = u->next) {
  490. if (u->bot) {
  491. if (strcasecmp(u->handle, conf.bot->nick)) {
  492. bool found = 0;
  493. if (findbot(u->handle))
  494. found = 1;
  495. total++;
  496. if (nodename || (!nodename && ((!up && !found) || (up && found)))) {
  497. if (nodename)
  498. node = (char *) get_user(&USERENTRY_NODENAME, u);
  499. if (!nodename || wild_match(nodename, node)) {
  500. if (nodename && !found)
  501. strlcat(work, "*", sizeof(work));
  502. strlcat(work, u->handle, sizeof(work));
  503. cnt++;
  504. if (nodename)
  505. mtot++;
  506. if (!nodename || (nodename && found))
  507. tot++;
  508. if (cnt == 11) {
  509. dprintf(idx, "%s bots: %s\n", nodename ? "Matching" : up ? "Up" : "Down", work);
  510. work[0] = 0;
  511. cnt = 0;
  512. } else {
  513. strlcat(work, " ", sizeof(work));
  514. }
  515. }
  516. }
  517. }
  518. }
  519. }
  520. if (work[0])
  521. dprintf(idx, "%s bot%s: %s\n", nodename ? "Matching" : up ? "Up" : "Down", cnt > 1 ? "s" : "", work);
  522. if (nodename)
  523. dprintf(idx, "(Total Matching: %d/%d)\n", mtot, total);
  524. dprintf(idx, "(Total %s: %d/%d)\n", nodename ? "up" : up ? "up" : "down", tot, nodename ? mtot : total);
  525. }
  526. /* Show a simpleton bot tree
  527. */
  528. void tell_bottree(int idx)
  529. {
  530. if (tands == 0) {
  531. dprintf(idx, "%s\n", "No bots linked.");
  532. return;
  533. }
  534. char s[161] = "", work[1024] = "", *color_str = NULL;
  535. tand_t *last[20] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  536. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  537. tand_t *thisbot = NULL, *bot = NULL, *bot2 = NULL;
  538. int lev = 0, more = 1, mark[20], ok, cnt, i = 0, imark, tothops = 0;
  539. struct userrec *botu = NULL;
  540. for (bot = tandbot; bot; bot = bot->next)
  541. if (!bot->uplink) {
  542. if (i) {
  543. s[i++] = ',';
  544. s[i++] = ' ';
  545. }
  546. strlcpy(s + i, bot->bot, sizeof(s) - i);
  547. i += strlen(bot->bot);
  548. }
  549. if (bot_hublevel(conf.bot->u) < 999)
  550. color_str = (char *) YELLOW(idx);
  551. else if (conf.bot->localhub)
  552. color_str = (char *) RED(idx);
  553. else
  554. color_str = (char *) NULL;
  555. if (s[0])
  556. dprintf(idx, "(%s %s)\n", "No trace info for:", s);
  557. dprintf(idx, "%s%s%s (%s)\n", color_str ? color_str : "",
  558. conf.bot->nick,
  559. color_str ? COLOR_END(idx) : "",
  560. egg_version);
  561. thisbot = (tand_t *) 1;
  562. work[0] = 0;
  563. while (more) {
  564. if (lev == 20) {
  565. dprintf(idx, "\n%s\n", "Tree too complex!");
  566. return;
  567. }
  568. cnt = 0;
  569. tothops += lev;
  570. for (bot = tandbot; bot; bot = bot->next)
  571. if (bot->uplink == thisbot)
  572. cnt++;
  573. if (cnt) {
  574. imark = 0;
  575. for (i = 0; i < lev; i++) {
  576. if (mark[i])
  577. strlcpy(work + imark, " | ", sizeof(work) - imark);
  578. else
  579. strlcpy(work + imark, " ", sizeof(work) - imark);
  580. imark += 5;
  581. }
  582. if (cnt > 1)
  583. strlcpy(work + imark, " |-", sizeof(work) - imark);
  584. else
  585. strlcpy(work + imark, " `-", sizeof(work) - imark);
  586. s[0] = 0;
  587. bot = tandbot;
  588. while (!s[0]) {
  589. if (bot->uplink == thisbot) {
  590. botu = get_user_by_handle(userlist, bot->bot);
  591. if (botu && bot_hublevel(botu) < 999)
  592. color_str = (char *) YELLOW(idx);
  593. else if (botu && bot->localhub)
  594. color_str = (char *) RED(idx);
  595. else
  596. color_str = (char *) NULL;
  597. if (bot->share)
  598. i = simple_snprintf(s, sizeof(s), "%c", bot->share);
  599. else
  600. i = simple_snprintf(s, sizeof(s), "-");
  601. i = simple_snprintf(s + 1, sizeof(s) - 1, "%s%s%s (%s)", color_str ? color_str : "",
  602. bot->bot,
  603. color_str ? COLOR_END(idx) : "",
  604. bot->version);
  605. } else
  606. bot = bot->next;
  607. }
  608. dprintf(idx, "%s%s\n", work, s);
  609. if (cnt > 1)
  610. mark[lev] = 1;
  611. else
  612. mark[lev] = 0;
  613. work[0] = 0;
  614. last[lev] = thisbot;
  615. thisbot = bot;
  616. lev++;
  617. more = 1;
  618. } else {
  619. while (cnt == 0) {
  620. /* No subtrees from here */
  621. if (lev == 0) {
  622. dprintf(idx, "(( tree error ))\n");
  623. return;
  624. }
  625. ok = 0;
  626. for (bot = tandbot; bot; bot = bot->next) {
  627. if (bot->uplink == last[lev - 1]) {
  628. if (thisbot == bot)
  629. ok = 1;
  630. else if (ok) {
  631. cnt++;
  632. if (cnt == 1) {
  633. botu = get_user_by_handle(userlist, bot->bot);
  634. if (botu && bot_hublevel(botu) < 999)
  635. color_str = (char *) YELLOW(idx);
  636. else if (botu && bot->localhub)
  637. color_str = (char *) RED(idx);
  638. else
  639. color_str = (char *) NULL;
  640. bot2 = bot;
  641. if (bot->share)
  642. i = simple_snprintf(s, sizeof(s), "%c", bot->share);
  643. else
  644. i = simple_snprintf(s, sizeof(s), "-");
  645. i = simple_snprintf(s + 1, sizeof(s) - 1, "%s%s%s (%s)", color_str ? color_str : "",
  646. bot->bot,
  647. color_str ? COLOR_END(idx) : "",
  648. bot->version);
  649. }
  650. }
  651. }
  652. }
  653. if (cnt) {
  654. imark = 0;
  655. for (i = 1; i < lev; i++) {
  656. if (mark[i - 1])
  657. strlcpy(work + imark, " | ", sizeof(work) - imark);
  658. else
  659. strlcpy(work + imark, " ", sizeof(work) - imark);
  660. imark += 5;
  661. }
  662. more = 1;
  663. if (cnt > 1)
  664. dprintf(idx, "%s |-%s\n", work, s);
  665. else
  666. dprintf(idx, "%s `-%s\n", work, s);
  667. thisbot = bot2;
  668. work[0] = 0;
  669. if (cnt > 1)
  670. mark[lev - 1] = 1;
  671. else
  672. mark[lev - 1] = 0;
  673. } else {
  674. /* This was the last child */
  675. lev--;
  676. if (lev == 0) {
  677. more = 0;
  678. cnt = 999;
  679. } else {
  680. more = 1;
  681. thisbot = last[lev];
  682. }
  683. }
  684. }
  685. }
  686. }
  687. /* Hop information: (9d) */
  688. dprintf(idx, "Average hops: %3.1f, total bots: %d\n", ((float) tothops) / ((float) tands), tands + 1);
  689. }
  690. /* Dump list of links to a new bot
  691. */
  692. void dump_links(int z)
  693. {
  694. register int i;
  695. register size_t l;
  696. char x[1024] = "";
  697. if (conf.bot->hub) {
  698. tand_t *bot = NULL;
  699. char *p = NULL;
  700. for (bot = tandbot; bot; bot = bot->next) {
  701. if (bot->uplink == (tand_t *) 1)
  702. p = conf.bot->nick;
  703. else
  704. p = bot->uplink->bot;
  705. l = simple_snprintf(x, sizeof(x), "n %s %s %cD0gc %d %d %s %s\n", bot->bot, p, bot->share, bot->localhub,
  706. (int) bot->buildts, bot->commit, bot->version ? bot->version : "");
  707. tputs(dcc[z].sock, x, l);
  708. }
  709. }
  710. /* Dump party line members */
  711. for (i = 0; i < dcc_total; i++) {
  712. if (dcc[i].type && dcc[i].type == &DCC_CHAT && dcc[i].simul == -1) {
  713. if ((dcc[i].u.chat->channel >= 0) && (dcc[i].u.chat->channel < GLOBAL_CHANS)) {
  714. l = simple_snprintf2(x, sizeof(x), "j !%s %s %D %c%D %s\n", conf.bot->nick, dcc[i].nick,
  715. dcc[i].u.chat->channel, geticon(i), dcc[i].sock, dcc[i].host);
  716. tputs(dcc[z].sock, x, l);
  717. l = simple_snprintf2(x, sizeof(x), "i %s %D %D %s\n", conf.bot->nick,
  718. dcc[i].sock, now - dcc[i].timeval,
  719. dcc[i].u.chat->away ? dcc[i].u.chat->away : "");
  720. tputs(dcc[z].sock, x, l);
  721. }
  722. }
  723. }
  724. for (i = 0; i < parties; i++) {
  725. l = simple_snprintf2(x, sizeof(x), "j %s %s %D %c%D %s\n", party[i].bot, party[i].nick,
  726. party[i].chan, party[i].flag, party[i].sock, party[i].from);
  727. tputs(dcc[z].sock, x, l);
  728. if ((party[i].status & PLSTAT_AWAY) || (party[i].timer != 0)) {
  729. l = simple_snprintf2(x, sizeof(x), "i %s %D %D %s\n", party[i].bot, party[i].sock, now - party[i].timer, party[i].away ? party[i].away : "");
  730. tputs(dcc[z].sock, x, l);
  731. }
  732. }
  733. }
  734. int in_chain(char *who)
  735. {
  736. if (!strcasecmp(who, conf.bot->nick))
  737. return 1;
  738. if (findbot(who))
  739. return 1;
  740. return 0;
  741. }
  742. int bots_in_subtree(tand_t *bot)
  743. {
  744. if (!bot)
  745. return 0;
  746. int nr = 1;
  747. tand_t *b = NULL;
  748. for (b = tandbot; b; b = b->next) {
  749. if (b->bot && (b->uplink == bot)) {
  750. nr += bots_in_subtree(b);
  751. }
  752. }
  753. return nr;
  754. }
  755. int users_in_subtree(tand_t *bot)
  756. {
  757. if (!bot)
  758. return 0;
  759. int i, nr = 0;
  760. tand_t *b = NULL;
  761. for (i = 0; i < parties; i++)
  762. if (!strcasecmp(party[i].bot, bot->bot))
  763. nr++;
  764. for (b = tandbot; b; b = b->next)
  765. if (b->bot && (b->uplink == bot))
  766. nr += users_in_subtree(b);
  767. return nr;
  768. }
  769. /* Break link with a tandembot
  770. */
  771. int botunlink(int idx, const char *nick, const char *reason)
  772. {
  773. register int i;
  774. int bots, users;
  775. tand_t *bot = NULL;
  776. if (nick[0] == '*')
  777. dprintf(idx, "%s\n", "Unlinking all bots...");
  778. for (i = 0; i < dcc_total; i++) {
  779. if (dcc[i].type && ((nick[0] == '*') || !strcasecmp(dcc[i].nick, nick))) {
  780. if (dcc[i].type == &DCC_FORK_BOT) {
  781. if (idx >= 0)
  782. dprintf(idx, "%s: %s -> %s.\n", "Killed link attempt to",
  783. dcc[i].nick, dcc[i].host);
  784. putlog(LOG_BOTS, "*", "%s: %s -> %s:%d",
  785. "Killed link attempt to", dcc[i].nick,
  786. dcc[i].host, dcc[i].port);
  787. killsock(dcc[i].sock);
  788. lostdcc(i);
  789. if (nick[0] != '*')
  790. return 1;
  791. } else if (dcc[i].type == &DCC_BOT_NEW) {
  792. if (idx >= 0)
  793. dprintf(idx, "%s %s.\n", "No longer trying to link:",
  794. dcc[i].nick);
  795. putlog(LOG_BOTS, "*", "%s %s @ %s:%d",
  796. "Stopped trying to link", dcc[i].nick,
  797. dcc[i].host, dcc[i].port);
  798. killsock(dcc[i].sock);
  799. lostdcc(i);
  800. if (nick[0] != '*')
  801. return 1;
  802. } else if (dcc[i].type == &DCC_BOT) {
  803. char s[1024] = "";
  804. if (idx >= 0)
  805. dprintf(idx, "%s %s.\n", "Breaking link with", dcc[i].nick);
  806. else if ((idx == -3) && (b_status(i) & STAT_SHARE) && !share_unlinks)
  807. return -1;
  808. bot = findbot(dcc[i].nick);
  809. bots = bots_in_subtree(bot);
  810. users = users_in_subtree(bot);
  811. if (reason && reason[0]) {
  812. simple_snprintf(s, sizeof(s), "%s %s (%s) (lost %d bot%s and %d user%s)",
  813. "Unlinked from:", dcc[i].nick, reason, bots,
  814. (bots != 1) ? "s" : "", users, (users != 1) ?
  815. "s" : "");
  816. dprintf(i, "bye %s\n", reason);
  817. } else {
  818. simple_snprintf(s, sizeof(s), "%s %s (lost %d bot%s and %d user%s)",
  819. "Unlinked from:", dcc[i].nick, bots, (bots != 1) ?
  820. "s" : "", users, (users != 1) ? "s" : "");
  821. dprintf(i, "bye No reason\n");
  822. }
  823. chatout("*** %s\n", s);
  824. botnet_send_unlinked(i, dcc[i].nick, s);
  825. killsock(dcc[i].sock);
  826. lostdcc(i);
  827. if (nick[0] != '*')
  828. return 1;
  829. }
  830. }
  831. }
  832. if (idx >= 0 && nick[0] != '*')
  833. dprintf(idx, "%s\n", "Not connected to that bot.");
  834. if (nick[0] != '*') {
  835. bot = findbot(nick);
  836. if (bot) {
  837. /* The internal bot list is desynched from the dcc list
  838. sometimes. While we still search for the bug, provide
  839. an easy way to clear out those `ghost'-bots.
  840. Fabian (2000-08-02) */
  841. char *ghost = "BUG!!: Found bot `%s' in internal bot list, but it\n"
  842. " shouldn't have been there! Removing.\n"
  843. " This is a known bug we haven't fixed yet. If this\n"
  844. " bot is the newest eggdrop version available and you\n"
  845. " know a *reliable* way to reproduce the bug, please\n"
  846. " contact us - we need your help!\n";
  847. if (idx >= 0)
  848. dprintf(idx, ghost, nick);
  849. else
  850. putlog(LOG_MISC, "*", ghost, nick);
  851. rembot(bot->bot);
  852. return 1;
  853. }
  854. }
  855. if (nick[0] == '*') {
  856. dprintf(idx, "%s\n", "Smooshing bot tables and assocs...");
  857. while (tandbot)
  858. rembot(tandbot->bot);
  859. while (parties) {
  860. parties--;
  861. }
  862. }
  863. return 0;
  864. }
  865. static void botlink_dns_callback(int, void *, const char *, char **);
  866. /* Link to another bot
  867. */
  868. int botlink(char *linker, int idx, char *nick)
  869. {
  870. struct userrec *u = get_user_by_handle(userlist, nick);
  871. if (!u || !u->bot) {
  872. if (idx >= 0)
  873. dprintf(idx, "%s %s\n", nick, "is not a known bot.");
  874. } else if (!strcasecmp(nick, conf.bot->nick)) {
  875. if (idx >= 0)
  876. dprintf(idx, "%s\n", "Link to myself? Oh boy, Freud would have a field day.");
  877. } else if (in_chain(nick) && (idx != -3)) {
  878. if (idx >= 0)
  879. dprintf(idx, "That bot is already connected up.\n");
  880. } else {
  881. register int i;
  882. for (i = 0; i < dcc_total; i++) {
  883. if (dcc[i].type && (dcc[i].user == u) &&
  884. ((dcc[i].type == &DCC_FORK_BOT) ||
  885. (dcc[i].type == &DCC_BOT_NEW))) {
  886. if (idx >= 0)
  887. dprintf(idx, "Already linking to that bot\n");
  888. return 0;
  889. }
  890. }
  891. /* Address to connect to is in 'info' */
  892. struct bot_addr *bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  893. if (!bi || !strlen(bi->address) || !bi->telnet_port || (bi->telnet_port <= 0)) {
  894. if (idx >= 0) {
  895. dprintf(idx, "Invalid telnet address:port stored for '%s'.\n", nick);
  896. dprintf(idx, "Use: %schaddr %s <address>:<port#>[/<relay-port#>]\n", (dcc[idx].u.chat->channel >= 0) ? settings.dcc_prefix : "", nick);
  897. }
  898. } else if (dcc_total == max_dcc) {
  899. if (idx >= 0)
  900. dprintf(idx, "%s\n", "Sorry, too many DCC connections.");
  901. } else {
  902. correct_handle(nick);
  903. if (idx > -2)
  904. putlog(LOG_BOTS, "*", "Linking to %s at %s:%d ...", nick,
  905. bi->address, bi->telnet_port);
  906. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  907. dcc[i].timeval = now;
  908. dcc[i].port = bi->telnet_port;
  909. dcc[i].user = u;
  910. strlcpy(dcc[i].nick, nick, NICKLEN);
  911. strlcpy(dcc[i].host, bi->address, UHOSTLEN);
  912. dcc[i].u.dns->cptr = strdup(linker);
  913. dcc[i].u.dns->ibuf = idx;
  914. dcc[i].bot = 1;
  915. int dns_id = egg_dns_lookup(bi->address, 20, botlink_dns_callback, (void *) (long)i);
  916. /* dns_id
  917. * -1 means it was cached and the callback already called
  918. * -2 means it's already being looked up.. try again later .. */
  919. if (dns_id >= 0)
  920. dcc[i].dns_id = dns_id;
  921. else if (dns_id == -2) {
  922. lostdcc(i);
  923. return 0;
  924. }
  925. return 1;
  926. /* wait for async reply */
  927. }
  928. }
  929. return 0;
  930. }
  931. static void botlink_dns_callback(int id, void *client_data, const char *host, char **ips)
  932. {
  933. long data = (long) client_data;
  934. int i = (int) data;
  935. if (!valid_dns_id(i, id))
  936. return;
  937. int idx = -1;
  938. char *linker = NULL;
  939. if (valid_idx(i)) {
  940. idx = dcc[i].u.dns->ibuf;
  941. linker = strdup(dcc[i].u.dns->cptr);
  942. free(dcc[i].u.dns->cptr);
  943. dcc[i].u.dns->cptr = NULL;
  944. }
  945. if (!ips) {
  946. lostdcc(i);
  947. return;
  948. }
  949. dcc[i].addr = inet_addr(ips[0]);
  950. changeover_dcc(i, &DCC_FORK_BOT, sizeof(struct bot_info));
  951. dcc[i].timeval = now;
  952. struct bot_info dummy;
  953. strlcpy(dcc[i].u.bot->version, "(primitive bot)", sizeof(dummy.version));
  954. strlcpy(dcc[i].u.bot->sysname, "*", 2);
  955. strlcpy(dcc[i].u.bot->linker, linker, sizeof(dummy.linker));
  956. dcc[i].u.bot->numver = idx;
  957. free(linker);
  958. dcc[i].u.bot->port = dcc[i].port; /* Remember where i started */
  959. #ifdef USE_IPV6
  960. dcc[i].sock = getsock(SOCK_STRONGCONN, is_dotted_ip(ips[0]));
  961. #else
  962. dcc[i].sock = getsock(SOCK_STRONGCONN);
  963. #endif /* USE_IPV6 */
  964. // if (dcc[i].sock > 0)
  965. // identd_open(); /* will be closed when an ident is replied. */
  966. if (dcc[i].sock < 0 || open_telnet_raw(dcc[i].sock, ips[0], dcc[i].port, 0, 1) < 0)
  967. failed_link(i);
  968. else { /* let's attempt to initiate SSL before ANYTHING else... */
  969. dcc[i].ssl = 0;
  970. }
  971. /* wait for async reply */
  972. }
  973. static void failed_tandem_relay(int idx)
  974. {
  975. int uidx = (-1), i;
  976. for (i = 0; i < dcc_total; i++) {
  977. if (dcc[i].type && (dcc[i].type == &DCC_PRE_RELAY) &&
  978. (dcc[i].u.relay->sock == dcc[idx].sock))
  979. uidx = i;
  980. }
  981. if (uidx < 0) {
  982. putlog(LOG_MISC, "*", "Can't find user for relay! %d -> %d", dcc[idx].sock, dcc[idx].u.relay->sock);
  983. if (dcc[idx].sock != -1) {
  984. killsock(dcc[idx].sock);
  985. dcc[idx].sock = -1;
  986. }
  987. lostdcc(idx);
  988. return;
  989. }
  990. struct chat_info *ci = dcc[uidx].u.relay->chat;
  991. dprintf(uidx, "Could not relay to %s.\n", dcc[idx].nick);
  992. dcc[uidx].status = dcc[uidx].u.relay->old_status;
  993. free(dcc[uidx].u.relay);
  994. dcc[uidx].u.chat = ci;
  995. dcc[uidx].type = &DCC_CHAT;
  996. if (dcc[idx].sock != -1) {
  997. killsock(dcc[idx].sock);
  998. dcc[idx].sock = -1;
  999. }
  1000. lostdcc(idx);
  1001. return;
  1002. }
  1003. static void tandem_relay_dns_callback(int, void *, const char *, char **);
  1004. /* Relay to another tandembot
  1005. */
  1006. void tandem_relay(int idx, char *nick, register int i)
  1007. {
  1008. struct userrec *u = get_user_by_handle(userlist, nick);
  1009. if (!u || !u->bot) {
  1010. dprintf(idx, "%s %s\n", nick, "is not a known bot.");
  1011. return;
  1012. }
  1013. if (!strcasecmp(nick, conf.bot->nick)) {
  1014. dprintf(idx, "%s\n", "Relay to myself? What on EARTH would be the point?!");
  1015. return;
  1016. }
  1017. /* Address to connect to is in 'info' */
  1018. struct bot_addr *bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  1019. if (!bi || !strlen(bi->address) || !bi->relay_port || (bi->relay_port <= 0)) {
  1020. dprintf(idx, "Invalid telnet address:port stored for '%s'.\n", nick);
  1021. dprintf(idx, "Use: %schaddr %s <address>:<port#>[/<relay-port#>]\n", (dcc[idx].u.chat->channel >= 0) ? settings.dcc_prefix : "", nick);
  1022. return;
  1023. }
  1024. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1025. if (i < 0) {
  1026. dprintf(idx, "%s\n", "Sorry, too many DCC connections.");
  1027. return;
  1028. }
  1029. dcc[i].port = bi->relay_port;
  1030. dcc[i].addr = 0L;
  1031. strlcpy(dcc[i].nick, nick, NICKLEN);
  1032. dcc[i].user = u;
  1033. strlcpy(dcc[i].host, bi->address, UHOSTLEN);
  1034. if (conf.bot->hub)
  1035. dprintf(idx, "%s %s @ %s:%d ...\n", "Connecting to", nick, bi->address, bi->relay_port);
  1036. dprintf(idx, "(Type *BYE* on a line by itself to abort.)\n");
  1037. dcc[idx].type = &DCC_PRE_RELAY;
  1038. struct chat_info *ci = dcc[idx].u.chat;
  1039. dcc[idx].u.relay = (struct relay_info *) my_calloc(1, sizeof(struct relay_info));
  1040. dcc[idx].u.relay->chat = ci;
  1041. dcc[idx].u.relay->old_status = dcc[idx].status;
  1042. dcc[idx].u.relay->idx = i;
  1043. dcc[idx].u.relay->sock = -1;
  1044. dcc[i].timeval = now;
  1045. dcc[i].u.dns->ibuf = idx;
  1046. int dns_id = egg_dns_lookup(bi->address, 20, tandem_relay_dns_callback, (void *) (long) i);
  1047. if (dns_id >= 0)
  1048. dcc[i].dns_id = dns_id;
  1049. return;
  1050. /* wait for async reply */
  1051. }
  1052. static void tandem_relay_dns_callback(int id, void *client_data, const char *host, char **ips)
  1053. {
  1054. //64bit hacks
  1055. long data = (long) client_data;
  1056. int i = (int) data, idx = -1;
  1057. if (!valid_dns_id(i, id))
  1058. return;
  1059. if (valid_idx(i))
  1060. idx = dcc[i].u.dns->ibuf;
  1061. if (idx < 0) {
  1062. putlog(LOG_MISC, "*", "Can't find user for relay! %d -> %d", i, idx);
  1063. lostdcc(i);
  1064. return;
  1065. }
  1066. if (!ips) {
  1067. struct chat_info *ci = dcc[idx].u.relay->chat;
  1068. dprintf(idx, "Could not relay to %s.\n", dcc[i].nick);
  1069. dcc[idx].status = dcc[idx].u.relay->old_status;
  1070. free(dcc[idx].u.relay);
  1071. dcc[idx].u.chat = ci;
  1072. dcc[idx].type = &DCC_CHAT;
  1073. lostdcc(i);
  1074. return;
  1075. }
  1076. #ifdef USE_IPV6
  1077. int af = is_dotted_ip(ips[0]);
  1078. dcc[i].sock = getsock(SOCK_STRONGCONN | SOCK_VIRTUAL, af);
  1079. #else
  1080. dcc[i].sock = getsock(SOCK_STRONGCONN | SOCK_VIRTUAL);
  1081. #endif /* USE_IPV6 */
  1082. if (dcc[i].sock < 0) {
  1083. lostdcc(i);
  1084. dprintf(idx, "No free sockets available.\n");
  1085. return;
  1086. }
  1087. dcc[idx].u.relay->sock = dcc[i].sock;
  1088. changeover_dcc(i, &DCC_FORK_RELAY, sizeof(struct relay_info));
  1089. dcc[i].addr = inet_addr(ips[0]);
  1090. dcc[i].u.relay->chat = (struct chat_info *) my_calloc(1, sizeof(struct chat_info));
  1091. dcc[i].u.relay->sock = dcc[idx].sock;
  1092. dcc[i].u.relay->port = dcc[i].port;
  1093. #ifdef USE_IPV6
  1094. dcc[i].u.relay->af = af;
  1095. #endif
  1096. dcc[i].u.relay->chat->away = NULL;
  1097. dcc[i].u.relay->chat->msgs_per_sec = 0;
  1098. dcc[i].u.relay->chat->con_flags = 0;
  1099. dcc[i].u.relay->chat->buffer = NULL;
  1100. dcc[i].u.relay->chat->max_line = 0;
  1101. dcc[i].u.relay->chat->line_count = 0;
  1102. dcc[i].u.relay->chat->current_lines = 0;
  1103. dcc[i].timeval = now;
  1104. if (open_telnet_raw(dcc[i].sock, ips[0], dcc[i].port, 0) < 0)
  1105. failed_tandem_relay(i);
  1106. }
  1107. /* Input from user before connect is ready
  1108. */
  1109. static void pre_relay(int idx, char *buf, register int len)
  1110. {
  1111. register int tidx = (-1), i;
  1112. for (i = 0; i < dcc_total; i++) {
  1113. if (dcc[i].type && dcc[i].type == &DCC_FORK_RELAY && (dcc[i].u.relay->sock == dcc[idx].sock)) {
  1114. tidx = i;
  1115. break;
  1116. }
  1117. }
  1118. if (tidx < 0) {
  1119. /* Now try to find it among the DNSWAIT sockets instead. */
  1120. for (i = 0; i < dcc_total; i++) {
  1121. if (dcc[i].type && (dcc[i].type == &DCC_DNSWAIT && (dcc[idx].u.relay->idx == i))) {
  1122. tidx = i;
  1123. break;
  1124. }
  1125. }
  1126. }
  1127. if (tidx < 0) {
  1128. putlog(LOG_MISC, "*", "Can't find user for relay! %d -> %d", dcc[idx].sock, dcc[idx].u.relay->sock);
  1129. killsock(dcc[idx].sock);
  1130. lostdcc(idx);
  1131. return;
  1132. }
  1133. if (!strcasecmp(buf, "*bye*")) {
  1134. /* Disconnect */
  1135. struct chat_info *ci = dcc[idx].u.relay->chat;
  1136. dprintf(idx, "Aborting relay attempt to %s.\n", dcc[tidx].nick);
  1137. dprintf(idx, "You are now back on %s.\n\n", conf.bot->nick);
  1138. putlog(LOG_MISC, "*", "Relay aborted: %s -> %s", dcc[idx].nick, dcc[tidx].nick);
  1139. dcc[idx].status = dcc[idx].u.relay->old_status;
  1140. free(dcc[idx].u.relay);
  1141. dcc[idx].u.chat = ci;
  1142. dcc[idx].type = &DCC_CHAT;
  1143. if (dcc[tidx].sock != -1)
  1144. killsock(dcc[tidx].sock);
  1145. lostdcc(tidx);
  1146. return;
  1147. }
  1148. }
  1149. /* User disconnected before her relay had finished connecting
  1150. */
  1151. static void failed_pre_relay(int idx)
  1152. {
  1153. register int tidx = (-1), i;
  1154. for (i = 0; i < dcc_total; i++) {
  1155. if (dcc[i].type && (dcc[i].type == &DCC_FORK_RELAY) &&
  1156. (dcc[i].u.relay->sock == dcc[idx].sock)) {
  1157. tidx = i;
  1158. break;
  1159. }
  1160. }
  1161. if (tidx < 0) {
  1162. /* Now try to find it among the DNSWAIT sockets instead. */
  1163. for (i = 0; i < dcc_total; i++) {
  1164. if (dcc[i].type && (dcc[i].type == &DCC_DNSWAIT && (dcc[idx].u.relay->idx == i))) {
  1165. tidx = i;
  1166. break;
  1167. }
  1168. }
  1169. }
  1170. if (tidx < 0) {
  1171. putlog(LOG_MISC, "*", "Can't find user for relay! %d -> %d", dcc[idx].sock, dcc[idx].u.relay->sock);
  1172. killsock(dcc[idx].sock);
  1173. lostdcc(idx);
  1174. return;
  1175. }
  1176. putlog(LOG_MISC, "*", "%s [%s]%s/%d", "Lost dcc connection to", dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  1177. putlog(LOG_MISC, "*", "(%s %s)", "Dropping relay attempt to", dcc[tidx].nick);
  1178. if ((dcc[tidx].sock != STDOUT) || backgrd) {
  1179. if (idx > tidx) {
  1180. int t = tidx;
  1181. tidx = idx;
  1182. idx = t;
  1183. }
  1184. if (dcc[tidx].sock != -1)
  1185. killsock(dcc[tidx].sock);
  1186. lostdcc(tidx);
  1187. } else {
  1188. fatal("Lost my terminal?!", 0);
  1189. }
  1190. if (dcc[idx].sock != -1)
  1191. killsock(dcc[idx].sock);
  1192. lostdcc(idx);
  1193. }
  1194. static void cont_tandem_relay(int idx, char *buf, register int len)
  1195. {
  1196. register int uidx = (-1), i;
  1197. struct relay_info *ri = NULL;
  1198. for (i = 0; i < dcc_total; i++) {
  1199. if (dcc[i].type && (dcc[i].type == &DCC_PRE_RELAY) && (dcc[i].u.relay->sock == dcc[idx].sock))
  1200. uidx = i;
  1201. }
  1202. if (uidx < 0) {
  1203. putlog(LOG_MISC, "*", "Can't find user for relay! %d -> %d", dcc[idx].sock, dcc[idx].u.relay->sock);
  1204. killsock(dcc[idx].sock);
  1205. lostdcc(idx);
  1206. return;
  1207. }
  1208. dcc[idx].type = &DCC_RELAY;
  1209. dcc[idx].u.relay->sock = dcc[uidx].sock;
  1210. dcc[uidx].u.relay->sock = dcc[idx].sock;
  1211. dprintf(uidx, "%s %s ...\n", "Success!\n\nNOW CONNECTED TO RELAY BOT", dcc[idx].nick);
  1212. dprintf(uidx, "(You can type *BYE* to prematurely close the connection.)\n\n");
  1213. putlog(LOG_MISC, "*", "%s %s -> %s", "Relay link:",
  1214. dcc[uidx].nick, dcc[idx].nick);
  1215. ri = dcc[uidx].u.relay;
  1216. dcc[uidx].type = &DCC_CHAT;
  1217. dcc[uidx].u.chat = ri->chat;
  1218. if (dcc[uidx].u.chat->channel >= 0) {
  1219. chanout_but(-1, dcc[uidx].u.chat->channel, "*** %s left the party line.\n", dcc[uidx].nick);
  1220. if (dcc[uidx].u.chat->channel < GLOBAL_CHANS)
  1221. botnet_send_part_idx(uidx, NULL);
  1222. }
  1223. check_bind_chof(dcc[uidx].nick, uidx);
  1224. dcc[uidx].type = &DCC_RELAYING;
  1225. dcc[uidx].u.relay = ri;
  1226. }
  1227. static void eof_dcc_relay(int idx)
  1228. {
  1229. register int j;
  1230. for (j = 0; j < dcc_total; j++)
  1231. if (dcc[j].type && dcc[j].sock == dcc[idx].u.relay->sock)
  1232. break;
  1233. if (j == dcc_total || !dcc[j].type) {
  1234. killsock(dcc[idx].sock);
  1235. lostdcc(idx);
  1236. return;
  1237. }
  1238. dcc[j].status = dcc[j].u.relay->old_status;
  1239. /* In case echo was off, turn it back on (send IAC WON'T ECHO): */
  1240. if (dcc[j].status & STAT_TELNET)
  1241. dprintf(j, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  1242. putlog(LOG_MISC, "*", "%s: %s -> %s", "Ended relay link", dcc[j].nick,
  1243. dcc[idx].nick);
  1244. dprintf(j, "\n\n*** %s %s\n", "RELAY CONNECTION DROPPED.\nYou are now back on", conf.bot->nick);
  1245. struct chat_info *ci = dcc[j].u.relay->chat;
  1246. free(dcc[j].u.relay);
  1247. dcc[j].u.chat = ci;
  1248. dcc[j].type = &DCC_CHAT;
  1249. if (dcc[j].u.chat->channel >= 0) {
  1250. chanout_but(-1, dcc[j].u.chat->channel, "*** %s %s.\n",
  1251. dcc[j].nick, "rejoined the party line.");
  1252. if (dcc[j].u.chat->channel < GLOBAL_CHANS)
  1253. botnet_send_join_idx(j);
  1254. }
  1255. check_bind_chon(dcc[j].nick, j);
  1256. killsock(dcc[idx].sock);
  1257. lostdcc(idx);
  1258. }
  1259. static void eof_dcc_relaying(int idx)
  1260. {
  1261. register int j, x = dcc[idx].u.relay->sock;
  1262. putlog(LOG_MISC, "*", "%s [%s]%s/%d", "Lost dcc connection to", dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  1263. killsock(dcc[idx].sock);
  1264. lostdcc(idx);
  1265. for (j = 0; j < dcc_total; j++)
  1266. if (dcc[j].type && dcc[j].sock == x && dcc[j].type != &DCC_FORK_RELAY)
  1267. break;
  1268. putlog(LOG_MISC, "*", "(%s %s)", "Dropping relay link to", dcc[j].nick);
  1269. killsock(dcc[j].sock);
  1270. lostdcc(j); /* Drop connection to the bot */
  1271. }
  1272. static void dcc_relay(int idx, char *buf, int j)
  1273. {
  1274. unsigned char *p = (unsigned char *) buf;
  1275. int mark;
  1276. for (j = 0; j < dcc_total; j++)
  1277. if (dcc[j].type && dcc[j].sock == dcc[idx].u.relay->sock && dcc[j].type == &DCC_RELAYING)
  1278. break;
  1279. /* If redirecting to a non-telnet user, swallow telnet codes and
  1280. escape sequences. */
  1281. if (!(dcc[j].status & STAT_TELNET)) {
  1282. while (*p != 0) {
  1283. while (*p && *p != 255 && *p != '\r')
  1284. p++; /* Search for IAC, escape sequences and CR. */
  1285. if (*p == 255) {
  1286. mark = 2;
  1287. if (!*(p + 1))
  1288. mark = 1; /* Bogus */
  1289. if ((*(p + 1) >= 251) || (*(p + 1) <= 254)) {
  1290. mark = 3;
  1291. if (!*(p + 2))
  1292. mark = 2; /* Bogus */
  1293. }
  1294. strcpy((char *) p, (char *) (p + mark));
  1295. } else if (*p == '\r')
  1296. strcpy((char *) p, (char *) (p + 1));
  1297. }
  1298. if (!buf[0])
  1299. dprintf(-dcc[idx].u.relay->sock, " \n");
  1300. else
  1301. dprintf(-dcc[idx].u.relay->sock, "%s\n", buf);
  1302. return;
  1303. }
  1304. /* Telnet user */
  1305. if (!buf[0])
  1306. dprintf(-dcc[idx].u.relay->sock, " \r\n");
  1307. else
  1308. dprintf(-dcc[idx].u.relay->sock, "%s\r\n", buf);
  1309. }
  1310. static void dcc_relaying(int idx, char *buf, int j)
  1311. {
  1312. if (strcasecmp(buf, "*bye*")) {
  1313. dprintf(-dcc[idx].u.relay->sock, "%s\n", buf);
  1314. return;
  1315. }
  1316. /* The user want's to abort, so return them to partyline */
  1317. for (j = 0; j < dcc_total; j++)
  1318. if (dcc[j].type && dcc[j].sock == dcc[idx].u.relay->sock && dcc[j].type == &DCC_RELAY)
  1319. break;
  1320. dcc[idx].status = dcc[idx].u.relay->old_status;
  1321. /* In case echo was off, turn it back on (send IAC WON'T ECHO): */
  1322. if (dcc[idx].status & STAT_TELNET)
  1323. dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  1324. dprintf(idx, "\n(%s %s.)\n", "Breaking connection to", dcc[j].nick);
  1325. dprintf(idx, "You are now back on %s.\n\n", conf.bot->nick);
  1326. putlog(LOG_MISC, "*", "%s: %s -> %s", "Relay broken", dcc[idx].nick, dcc[j].nick);
  1327. if (dcc[idx].u.relay->chat->channel >= 0) {
  1328. chanout_but(-1, dcc[idx].u.relay->chat->channel, "*** %s joined the party line.\n", dcc[idx].nick);
  1329. if (dcc[idx].u.relay->chat->channel < GLOBAL_CHANS)
  1330. botnet_send_join_idx(idx);
  1331. }
  1332. struct chat_info *ci = dcc[idx].u.relay->chat;
  1333. free(dcc[idx].u.relay);
  1334. dcc[idx].u.chat = ci;
  1335. dcc[idx].type = &DCC_CHAT;
  1336. check_bind_chon(dcc[idx].nick, idx);
  1337. if (dcc[j].sock != -1)
  1338. killsock(dcc[j].sock);
  1339. lostdcc(j);
  1340. }
  1341. static void display_relay(int i, char *other, size_t bufsiz)
  1342. {
  1343. simple_snprintf(other, bufsiz, "rela -> sock %d", dcc[i].u.relay->sock);
  1344. }
  1345. static void display_relaying(int i, char *other, size_t bufsiz)
  1346. {
  1347. simple_snprintf(other, bufsiz, ">rly -> sock %d", dcc[i].u.relay->sock);
  1348. }
  1349. static void display_tandem_relay(int i, char *other, size_t bufsiz)
  1350. {
  1351. strlcpy(other, "other rela", bufsiz);
  1352. }
  1353. static void display_pre_relay(int i, char *other, size_t bufsiz)
  1354. {
  1355. strlcpy(other, "other >rly", bufsiz);
  1356. }
  1357. static void kill_relay(int idx, void *x)
  1358. {
  1359. register struct relay_info *p = (struct relay_info *) x;
  1360. if (p->chat)
  1361. DCC_CHAT.kill(idx, p->chat);
  1362. free(p);
  1363. }
  1364. struct dcc_table DCC_RELAY =
  1365. {
  1366. "RELAY",
  1367. 0, /* Flags */
  1368. eof_dcc_relay,
  1369. dcc_relay,
  1370. NULL,
  1371. NULL,
  1372. display_relay,
  1373. kill_relay,
  1374. NULL,
  1375. NULL
  1376. };
  1377. static void out_relay(int idx, char *buf, void *x)
  1378. {
  1379. register struct relay_info *p = (struct relay_info *) x;
  1380. if (p && p->chat)
  1381. DCC_CHAT.output(idx, buf, p->chat);
  1382. else
  1383. tputs(dcc[idx].sock, buf, strlen(buf));
  1384. }
  1385. struct dcc_table DCC_RELAYING =
  1386. {
  1387. "RELAYING",
  1388. 0, /* Flags */
  1389. eof_dcc_relaying,
  1390. dcc_relaying,
  1391. NULL,
  1392. NULL,
  1393. display_relaying,
  1394. kill_relay,
  1395. out_relay,
  1396. NULL
  1397. };
  1398. struct dcc_table DCC_FORK_RELAY =
  1399. {
  1400. "FORK_RELAY",
  1401. 0, /* Flags */
  1402. failed_tandem_relay,
  1403. cont_tandem_relay,
  1404. &connect_timeout,
  1405. failed_tandem_relay,
  1406. display_tandem_relay,
  1407. kill_relay,
  1408. NULL,
  1409. NULL
  1410. };
  1411. struct dcc_table DCC_PRE_RELAY =
  1412. {
  1413. "PRE_RELAY",
  1414. 0, /* Flags */
  1415. failed_pre_relay,
  1416. pre_relay,
  1417. NULL,
  1418. NULL,
  1419. display_pre_relay,
  1420. kill_relay,
  1421. NULL,
  1422. NULL
  1423. };
  1424. /* Once a minute, send 'ping' to each bot -- no exceptions
  1425. */
  1426. void check_botnet_pings()
  1427. {
  1428. int i, bots, users, top_index = 0;
  1429. tand_t *bot = NULL;
  1430. for (i = 0; i < dcc_total; i++) {
  1431. if (dcc[i].type) {
  1432. top_index = i;
  1433. if (dcc[i].type == &DCC_BOT) {
  1434. if (dcc[i].status & STAT_LEAF) {
  1435. tand_t *via = findbot(dcc[i].nick);
  1436. for (bot = tandbot; bot; bot = bot->next) {
  1437. if ((via == bot->via) && (bot != via)) {
  1438. /* Not leaflike behavior */
  1439. if (dcc[i].status & STAT_WARNED) {
  1440. char s[1024] = "";
  1441. putlog(LOG_BOTS, "*", "%s %s (%s).", "Disconnected from:", dcc[i].nick, "unleaflike behavior");
  1442. dprintf(i, "bye %s\n", "unleaflike behavior");
  1443. bot = findbot(dcc[i].nick);
  1444. bots = bots_in_subtree(bot);
  1445. users = users_in_subtree(bot);
  1446. simple_snprintf(s, sizeof(s), "%s %s (%s) (lost %d bot%s and %d user%s)",
  1447. "Disconnected from:", dcc[i].nick, "unleaflike behavior",
  1448. bots, (bots != 1) ? "s" : "", users, (users != 1) ?
  1449. "s" : "");
  1450. chatout("*** %s\n", s);
  1451. botnet_send_unlinked(i, dcc[i].nick, s);
  1452. killsock(dcc[i].sock);
  1453. lostdcc(i);
  1454. } else {
  1455. botnet_send_reject(i, conf.bot->nick, NULL, bot->bot, NULL, NULL);
  1456. dcc[i].status |= STAT_WARNED;
  1457. }
  1458. } else
  1459. dcc[i].status &= ~STAT_WARNED;
  1460. }
  1461. }
  1462. if (dcc[i].status & STAT_PINGED) {
  1463. char s[1024] = "";
  1464. putlog(LOG_BOTS, "*", "%s: %s", "Ping timeout", dcc[i].nick);
  1465. bot = findbot(dcc[i].nick);
  1466. bots = bots_in_subtree(bot);
  1467. users = users_in_subtree(bot);
  1468. simple_snprintf(s, sizeof(s), "%s: %s (lost %d bot%s and %d user%s)", "Ping timeout",
  1469. dcc[i].nick, bots, (bots != 1) ? "s" : "",
  1470. users, (users != 1) ? "s" : "");
  1471. chatout("*** %s\n", s);
  1472. botnet_send_unlinked(i, dcc[i].nick, s);
  1473. killsock(dcc[i].sock);
  1474. lostdcc(i);
  1475. } else {
  1476. botnet_send_ping(i);
  1477. dcc[i].status |= STAT_PINGED;
  1478. }
  1479. }
  1480. }
  1481. }
  1482. if (top_index != (dcc_total - 1))
  1483. dcc_total = top_index + 1;
  1484. }
  1485. void zapfbot(int idx)
  1486. {
  1487. char s[1024] = "";
  1488. tand_t *bot = findbot(dcc[idx].nick);
  1489. int bots = bots_in_subtree(bot), users = users_in_subtree(bot);
  1490. simple_snprintf(s, sizeof(s), "%s: %s (lost %d bot%s and %d user%s)", "Dropped bot",
  1491. dcc[idx].nick, bots, (bots != 1) ? "s" : "", users,
  1492. (users != 1) ? "s" : "");
  1493. chatout("*** %s\n", s);
  1494. botnet_send_unlinked(idx, dcc[idx].nick, s);
  1495. killsock(dcc[idx].sock);
  1496. lostdcc(idx);
  1497. }
  1498. static int get_role(char *bot)
  1499. {
  1500. struct userrec *u2 = NULL;
  1501. if (!(u2 = get_user_by_handle(userlist, bot)))
  1502. return 1;
  1503. if (bot_hublevel(u2) != 999)
  1504. return 0;
  1505. int rl, i;
  1506. struct bot_addr *ba = NULL;
  1507. int r[5] = { 0, 0, 0, 0, 0 };
  1508. struct userrec *u = NULL;
  1509. for (u = userlist; u; u = u->next) {
  1510. if (u->bot && bot_hublevel(u) == 999) {
  1511. if (strcmp(u->handle, bot)) {
  1512. ba = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  1513. if ((nextbot(u->handle) >= 0) && (ba) && (ba->roleid > 0) && (ba->roleid < 5))
  1514. r[(ba->roleid - 1)]++;
  1515. }
  1516. }
  1517. }
  1518. rl = 0;
  1519. for (i = 1; i <= 4; i++)
  1520. if (r[i] < r[rl])
  1521. rl = i;
  1522. rl++;
  1523. ba = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u2);
  1524. if (ba)
  1525. ba->roleid = rl;
  1526. return rl;
  1527. }
  1528. void lower_bot_linked(int idx)
  1529. {
  1530. char tmp[6] = "";
  1531. simple_snprintf(tmp, sizeof(tmp), "rl %d", get_role(dcc[idx].nick));
  1532. putbot(dcc[idx].nick, tmp);
  1533. }