botnet.c 47 KB

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