chanprog.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2010 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. * chanprog.c -- handles:
  22. * rmspace()
  23. * maintaining the server list
  24. * timers, utimers
  25. * telling the current programmed settings
  26. * initializing a lot of stuff and loading the tcl scripts
  27. *
  28. */
  29. #include "common.h"
  30. #include "chanprog.h"
  31. #include "settings.h"
  32. #include "src/mod/irc.mod/irc.h"
  33. #include "src/mod/channels.mod/channels.h"
  34. #include "src/mod/server.mod/server.h"
  35. #include "src/mod/share.mod/share.h"
  36. #include "rfc1459.h"
  37. #include "net.h"
  38. #include "misc.h"
  39. #include "users.h"
  40. #include "botnet.h"
  41. #include "userrec.h"
  42. #include "main.h"
  43. #include "debug.h"
  44. #include "set.h"
  45. #include "dccutil.h"
  46. #include "botmsg.h"
  47. #if HAVE_GETRUSAGE
  48. #include <sys/resource.h>
  49. #if HAVE_SYS_RUSAGE_H
  50. #include <sys/rusage.h>
  51. #endif
  52. #endif
  53. #include <sys/utsname.h>
  54. #include <bdlib/src/Array.h>
  55. #include <bdlib/src/String.h>
  56. char *def_chanset = "+enforcebans +dynamicbans +userbans -bitch +cycle -inactive +userexempts -dynamicexempts +userinvites -dynamicinvites -nodesynch -closed -take -voice -private -fastop +meankicks ban-type 3 protect-backup 1 groups { main } revenge react";
  57. struct chanset_t *chanset = NULL; /* Channel list */
  58. struct chanset_t *chanset_default = NULL; /* Default channel list */
  59. char admin[121] = ""; /* Admin info */
  60. char origbotnick[HANDLEN + 1] = ""; /* from -B (placed into conf.bot->nick .. for backup when conf is cleared */
  61. char origbotname[NICKLEN] = ""; /* Nick to regain */
  62. char jupenick[NICKLEN] = "";
  63. char botname[NICKLEN] = ""; /* IRC nickname */
  64. in_port_t my_port = 0;
  65. int reset_chans = 0;
  66. bool cookies_disabled = 0;
  67. char s2_4[3] = "",s1_6[3] = "",s1_11[3] = "";
  68. /* Remove leading and trailing whitespaces.
  69. */
  70. void rmspace(char *s)
  71. {
  72. if (!s || !*s)
  73. return;
  74. register char *p = NULL, *q = NULL;
  75. /* Remove trailing whitespaces. */
  76. for (q = s + strlen(s) - 1; q >= s && egg_isspace(*q); q--)
  77. ;
  78. *(q + 1) = 0;
  79. /* Remove leading whitespaces. */
  80. for (p = s; egg_isspace(*p); p++)
  81. ;
  82. if (p != s)
  83. memmove(s, p, q - p + 2);
  84. }
  85. /* Returns memberfields if the nick is in the member list.
  86. */
  87. memberlist *ismember(const struct chanset_t *chan, const char *nick)
  88. {
  89. register memberlist *x = NULL;
  90. if (chan && nick && nick[0])
  91. for (x = chan->channel.member; x && x->nick[0]; x = x->next)
  92. if (!rfc_casecmp(x->nick, nick))
  93. return x;
  94. return NULL;
  95. }
  96. /* Find a chanset by channel name as the server knows it (ie !ABCDEchannel)
  97. */
  98. struct chanset_t *findchan(const char *name)
  99. {
  100. register struct chanset_t *chan = NULL;
  101. for (chan = chanset; chan; chan = chan->next)
  102. if (chan->name[0] && !rfc_casecmp(chan->name, name))
  103. return chan;
  104. return NULL;
  105. }
  106. /* Find a chanset by display name (ie !channel)
  107. */
  108. struct chanset_t *findchan_by_dname(const char *name)
  109. {
  110. register struct chanset_t *chan = NULL;
  111. for (chan = chanset; chan; chan = chan->next)
  112. if (!rfc_casecmp(chan->dname, name))
  113. return chan;
  114. return NULL;
  115. }
  116. /*
  117. * "caching" functions
  118. */
  119. /* Shortcut for get_user_by_host -- might have user record in one
  120. * of the channel caches.
  121. */
  122. struct userrec *check_chanlist(const char *host)
  123. {
  124. char *nick = NULL, *uhost = NULL, buf[UHOSTLEN] = "";
  125. register memberlist *m = NULL;
  126. register struct chanset_t *chan = NULL;
  127. strlcpy(buf, host, sizeof buf);
  128. uhost = buf;
  129. nick = splitnick(&uhost);
  130. for (chan = chanset; chan; chan = chan->next)
  131. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  132. if (!rfc_casecmp(nick, m->nick) && !strcasecmp(uhost, m->userhost))
  133. return m->user;
  134. return NULL;
  135. }
  136. /* Shortcut for get_user_by_handle -- might have user record in channels
  137. */
  138. struct userrec *check_chanlist_hand(const char *hand)
  139. {
  140. register struct chanset_t *chan = NULL;
  141. register memberlist *m = NULL;
  142. for (chan = chanset; chan; chan = chan->next)
  143. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  144. if (m->user && !strcasecmp(m->user->handle, hand))
  145. return m->user;
  146. return NULL;
  147. }
  148. /* Clear the user pointers in the chanlists.
  149. *
  150. * Necessary when a hostmask is added/removed, a user is added or a new
  151. * userfile is loaded.
  152. */
  153. void clear_chanlist(void)
  154. {
  155. register memberlist *m = NULL;
  156. register struct chanset_t *chan = NULL;
  157. for (chan = chanset; chan; chan = chan->next)
  158. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  159. m->user = NULL;
  160. m->tried_getuser = 0;
  161. }
  162. }
  163. /* Clear the user pointer of a specific nick in the chanlists.
  164. *
  165. * Necessary when a hostmask is added/removed, a nick changes, etc.
  166. * Does not completely invalidate the channel cache like clear_chanlist().
  167. */
  168. void clear_chanlist_member(const char *nick)
  169. {
  170. register memberlist *m = NULL;
  171. register struct chanset_t *chan = NULL;
  172. for (chan = chanset; chan; chan = chan->next)
  173. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  174. if (!rfc_casecmp(m->nick, nick)) {
  175. m->user = NULL;
  176. m->tried_getuser = 0;
  177. break;
  178. }
  179. }
  180. /* If this user@host is in a channel, set it (it was null)
  181. */
  182. void set_chanlist(const char *host, struct userrec *rec)
  183. {
  184. char *nick = NULL, *uhost = NULL, buf[UHOSTLEN] = "";
  185. register memberlist *m = NULL;
  186. register struct chanset_t *chan = NULL;
  187. strlcpy(buf, host, sizeof buf);
  188. uhost = buf;
  189. nick = splitnick(&uhost);
  190. for (chan = chanset; chan; chan = chan->next)
  191. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  192. if (!rfc_casecmp(nick, m->nick) && !strcasecmp(uhost, m->userhost))
  193. m->user = rec;
  194. }
  195. /* 0 marks all channels
  196. * 1 removes marked channels
  197. * 2 unmarks all channels
  198. */
  199. void checkchans(int which)
  200. {
  201. struct chanset_t *chan = NULL, *chan_next = NULL;
  202. if (which == 0 || which == 2) {
  203. for (chan = chanset; chan; chan = chan->next) {
  204. if (which == 0) {
  205. chan->status |= CHAN_FLAGGED;
  206. } else if (which == 2) {
  207. chan->status &= ~CHAN_FLAGGED;
  208. }
  209. }
  210. } else if (which == 1) {
  211. for (chan = chanset; chan; chan = chan_next) {
  212. chan_next = chan->next;
  213. if (chan->status & CHAN_FLAGGED) {
  214. putlog(LOG_MISC, "*", "No longer supporting channel %s", chan->dname);
  215. remove_channel(chan);
  216. }
  217. }
  218. }
  219. }
  220. void tell_verbose_uptime(int idx)
  221. {
  222. char s[256] = "", s1[121] = "", s2[81] = "", outbuf[501] = "";
  223. time_t total, hr, min;
  224. #if HAVE_GETRUSAGE
  225. struct rusage ru;
  226. #else
  227. # if HAVE_CLOCK
  228. clock_t cl;
  229. # endif
  230. #endif /* HAVE_GETRUSAGE */
  231. daysdur(now, online_since, s, sizeof(s), false);
  232. if (backgrd)
  233. strlcpy(s1, "background", sizeof(s1));
  234. else {
  235. if (term_z)
  236. strlcpy(s1, "terminal mode", sizeof(s1));
  237. else
  238. strlcpy(s1, "log dump mode", sizeof(s1));
  239. }
  240. simple_snprintf(outbuf, sizeof(outbuf), "Online for %s", s);
  241. if (restart_time) {
  242. daysdur(now, restart_time, s, sizeof(s), false);
  243. size_t olen = strlen(outbuf);
  244. simple_snprintf(&outbuf[olen], sizeof(outbuf) - olen, " (%s %s ago)", restart_was_update ? "updated" : "restarted", s);
  245. }
  246. #if HAVE_GETRUSAGE
  247. getrusage(RUSAGE_SELF, &ru);
  248. total = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec;
  249. hr = (int) (total / 60);
  250. min = (int) (total - (hr * 60));
  251. egg_snprintf(s2, sizeof(s2), "CPU %02d:%02d (load avg %3.1f%%)", (int) hr, (int) min, 100.0 * ((float) total / (float) (now - online_since)));
  252. #else
  253. # if HAVE_CLOCK
  254. cl = (clock() / CLOCKS_PER_SEC);
  255. hr = (int) (cl / 60);
  256. min = (int) (cl - (hr * 60));
  257. egg_snprintf(s2, sizeof(s2), "CPU %02d:%02d (load avg %3.1f%%)", (int) hr, (int) min, 100.0 * ((float) cl / (float) (now - online_since)));
  258. # else
  259. simple_snprintf(s2, sizeof(s2), "CPU ???");
  260. # endif
  261. #endif /* HAVE_GETRUSAGE */
  262. dprintf(idx, "%s (%s) %s cache hit %4.1f%%\n",
  263. outbuf, s1, s2,
  264. 100.0 * ((float) cache_hit) / ((float) (cache_hit + cache_miss)));
  265. }
  266. /* Dump status info out to dcc
  267. */
  268. void tell_verbose_status(int idx)
  269. {
  270. char *vers_t = NULL, *uni_t = NULL;
  271. int i;
  272. struct utsname un;
  273. if (!uname(&un) < 0) {
  274. vers_t = " ";
  275. uni_t = "*unknown*";
  276. } else {
  277. vers_t = un.release;
  278. uni_t = un.sysname;
  279. }
  280. i = count_users(userlist);
  281. dprintf(idx, "I am %s, running %s: %d user%s\n", conf.bot->nick, ver, i, i == 1 ? "" : "s");
  282. dprintf(idx, "my user: %s\n", conf.bot->u->handle);
  283. if (conf.bot->localhub)
  284. dprintf(idx, "I am a localhub.\n");
  285. if (conf.bot->hub && isupdatehub())
  286. dprintf(idx, "I am an update hub.\n");
  287. tell_verbose_uptime(idx);
  288. if (admin[0])
  289. dprintf(idx, "Admin: %s\n", admin);
  290. dprintf(idx, "OS: %s %s\n", uni_t, vers_t);
  291. dprintf(idx, "Running from: %s\n", binname);
  292. dprintf(idx, "uid: %s (%d) pid: %lu homedir: %s\n", conf.username, conf.uid, (unsigned long) mypid, conf.homedir);
  293. if (tempdir[0])
  294. dprintf(idx, "Tempdir : %s\n", tempdir);
  295. if (conf.datadir)
  296. dprintf(idx, "Datadir : %s\n", conf.datadir);
  297. }
  298. /* Show all internal state variables
  299. */
  300. void tell_settings(int idx)
  301. {
  302. char s[1024] = "";
  303. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  304. dprintf(idx, "Botnet Nickname: %s\n", conf.bot->nick);
  305. if (conf.bot->hub)
  306. dprintf(idx, "Userfile: %s \n", userfile);
  307. dprintf(idx, "Directories:\n");
  308. dprintf(idx, " Temp : %s\n", tempdir);
  309. fr.global = default_flags;
  310. build_flags(s, &fr, NULL);
  311. dprintf(idx, "New users get flags [%s]\n", s);
  312. if (conf.bot->hub && owner[0])
  313. dprintf(idx, "Permanent owner(s): %s\n", owner);
  314. dprintf(idx, "Ignores last %d mins\n", ignore_time);
  315. }
  316. void reaffirm_owners()
  317. {
  318. /* Make sure perm owners are +a */
  319. if (owner[0]) {
  320. char *q = owner, *p = strchr(q, ','), s[121] = "";
  321. struct userrec *u = NULL;
  322. while (p) {
  323. strlcpy(s, q, (p - q) + 1);
  324. rmspace(s);
  325. u = get_user_by_handle(userlist, s);
  326. if (u)
  327. u->flags = sanity_check(u->flags | USER_ADMIN, 0);
  328. q = p + 1;
  329. p = strchr(q, ',');
  330. }
  331. strlcpy(s, q, sizeof(s));
  332. rmspace(s);
  333. u = get_user_by_handle(userlist, s);
  334. if (u)
  335. u->flags = sanity_check(u->flags | USER_ADMIN, 0);
  336. }
  337. }
  338. bool is_hub(const char* nick) {
  339. for (size_t idx = 0; idx < conf.hubs.length(); ++idx) {
  340. bd::String hub(conf.hubs[idx]);
  341. if (!strncasecmp(nick, newsplit(hub).c_str(), HANDLEN)) {
  342. return true;
  343. }
  344. }
  345. return false;
  346. }
  347. void load_internal_users()
  348. {
  349. char *p = NULL, *ln = NULL, *hand = NULL, *pass = NULL, *q = NULL;
  350. char *hosts = NULL, buf[2048] = "", tmp[51] = "";
  351. int i;
  352. struct userrec *u = NULL;
  353. /* hubs */
  354. for (size_t idx = 0; idx < conf.hubs.length(); ++idx) {
  355. bd::Array<bd::String> params(static_cast<bd::String>(conf.hubs[idx]).split(' '));
  356. bd::String handle(params[0]);
  357. if (!get_user_by_handle(userlist, const_cast<char*>(handle.c_str()))) {
  358. bd::String address(params[1]);
  359. in_port_t port = atoi(static_cast<bd::String>(params[2]).c_str());
  360. unsigned short hublevel = params.length() == 4 ? atoi(static_cast<bd::String>(params[3]).c_str()) : (idx + 1);
  361. userlist = adduser(userlist, handle.c_str(), "none", "-", USER_OP, 1);
  362. u = get_user_by_handle(userlist, const_cast<char*>(handle.c_str()));
  363. simple_snprintf(tmp, sizeof(tmp), "%li [internal]", (long)now);
  364. set_user(&USERENTRY_ADDED, u, tmp);
  365. struct bot_addr *bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  366. bi->address = strdup(address.c_str());
  367. bi->telnet_port = bi->relay_port = port;
  368. bi->hublevel = hublevel;
  369. if (conf.bot->hub && (!bi->hublevel) && (!strcasecmp(handle.c_str(), conf.bot->nick))) {
  370. bi->hublevel = 99;
  371. }
  372. bi->uplink = (char *) my_calloc(1, 1);
  373. set_user(&USERENTRY_BOTADDR, u, bi);
  374. /* set_user(&USERENTRY_PASS, get_user_by_handle(userlist, handle.c_str()), SALT2); */
  375. }
  376. }
  377. /* perm owners */
  378. owner[0] = 0;
  379. strlcpy(buf, settings.owners, sizeof(buf));
  380. p = buf;
  381. while (p) {
  382. ln = p;
  383. p = strchr(p, ',');
  384. if (p)
  385. *p++ = 0;
  386. hand = ln;
  387. pass = NULL;
  388. hosts = NULL;
  389. for (i = 0; ln; i++) {
  390. switch (i) {
  391. case 0:
  392. hand = ln;
  393. break;
  394. case 1:
  395. pass = ln;
  396. if (ln && (ln = strchr(ln, ' ')))
  397. *ln++ = 0;
  398. hosts = ln;
  399. if (owner[0])
  400. strlcat(owner, ",", 121);
  401. strlcat(owner, hand, 121);
  402. if (!get_user_by_handle(userlist, hand)) {
  403. userlist = adduser(userlist, hand, "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_OP | USER_PARTY | USER_HUBA | USER_CHUBA, 0);
  404. u = get_user_by_handle(userlist, hand);
  405. set_user(&USERENTRY_PASS, u, pass);
  406. simple_snprintf(tmp, sizeof(tmp), "%li [internal]", (long)now);
  407. set_user(&USERENTRY_ADDED, u, tmp);
  408. while (hosts) {
  409. char x[1024] = "";
  410. if ((ln = strchr(ln, ' ')))
  411. *ln++ = 0;
  412. if ((q = strchr(hosts, '!'))) { /* skip over nick they provided ... */
  413. q++;
  414. if (*q == '*' || *q == '?') /* ... and any '*' or '?' */
  415. q++;
  416. hosts = q;
  417. }
  418. simple_snprintf(x, sizeof(x), "-telnet!%s", hosts);
  419. set_user(&USERENTRY_HOSTS, u, x);
  420. hosts = ln;
  421. }
  422. }
  423. break;
  424. default:
  425. break;
  426. }
  427. if (ln && (ln = strchr(ln, ' ')))
  428. *ln++ = 0;
  429. }
  430. }
  431. // Add HQ in if needed
  432. if (!backgrd && term_z) {
  433. strlcat(owner, ",HQ", sizeof(owner));
  434. }
  435. }
  436. static struct userrec* add_bot_userlist(char* bot) {
  437. struct userrec *u = NULL;
  438. if (!(u = get_user_by_handle(userlist, bot))) {
  439. /* I need to be on the userlist... doh. */
  440. userlist = adduser(userlist, bot, "none", "-", USER_OP, 1);
  441. u = get_user_by_handle(userlist, bot);
  442. struct bot_addr *bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  443. bi->uplink = (char *) my_calloc(1, 1);
  444. bi->address = (char *) my_calloc(1, 1);
  445. bi->telnet_port = 3333;
  446. bi->relay_port = 3333;
  447. bi->hublevel = 999;
  448. set_user(&USERENTRY_BOTADDR, u, bi);
  449. }
  450. return u;
  451. }
  452. void add_myself_to_userlist() {
  453. conf.bot->u = add_bot_userlist(conf.bot->nick);
  454. }
  455. void add_child_bots() {
  456. conf_bot* bot = conf.bots->next; //Skip myself
  457. if (bot && bot->nick) {
  458. for (; bot && bot->nick; bot = bot->next) {
  459. add_bot_userlist(bot->nick);
  460. }
  461. }
  462. }
  463. void add_localhub() {
  464. add_bot_userlist(conf.localhub);
  465. }
  466. void rehash_ip() {
  467. /* cache our ip on load instead of every 30 seconds */
  468. char *ip4 = NULL, *ip6 = NULL;
  469. if (cached_ip) {
  470. ip4 = strdup(myipstr(AF_INET));
  471. ip6 = strdup(myipstr(AF_INET6));
  472. }
  473. cache_my_ip();
  474. sdprintf("ip4: %s", myipstr(AF_INET));
  475. sdprintf("ip6: %s", myipstr(AF_INET6));
  476. /* Check if our ip changed during a rehash */
  477. if (ip4) {
  478. if (strcmp(ip4, myipstr(AF_INET)) || strcmp(ip6, myipstr(AF_INET6))) {
  479. if (tands > 0) {
  480. botnet_send_chat(-1, conf.bot->nick, "IP changed.");
  481. botnet_send_bye("IP changed.");
  482. }
  483. fatal("brb", 1);
  484. }
  485. free(ip4);
  486. free(ip6);
  487. }
  488. if (conf.bot->hub) {
  489. struct bot_addr *bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, conf.bot->u);
  490. listen_all(bi->telnet_port, 0, 1);
  491. my_port = bi->telnet_port;
  492. } else if (conf.bot->localhub) {
  493. // If not listening on the domain socket, open it up
  494. bool listening = 0;
  495. for (int i = 0; i < dcc_total; i++) {
  496. if (dcc[i].type && (dcc[i].type == &DCC_TELNET) && (!strcmp(dcc[i].host, conf.localhub_socket)) && (!strcmp(dcc[i].nick, "(unix_domain"))) {
  497. listening = 1;
  498. break;
  499. }
  500. }
  501. if (!listening) {
  502. // Listen on the unix domain socket
  503. in_port_t port;
  504. int i = open_listen_addr_by_af(conf.localhub_socket, &port, AF_UNIX);
  505. if (i < 0) {
  506. putlog(LOG_ERRORS, "*", "Can't listen on %s - %s", conf.localhub_socket, i == -1 ? "it's taken." : "couldn't assign file.");
  507. } else {
  508. /* now setup dcc entry */
  509. int idx = new_dcc(&DCC_TELNET, 0);
  510. dcc[idx].addr = 0L;
  511. strlcpy(dcc[idx].host, conf.localhub_socket, sizeof(dcc[idx].host));
  512. dcc[idx].port = 0;
  513. dcc[idx].sock = i;
  514. dcc[idx].timeval = now;
  515. strlcpy(dcc[idx].nick, "(unix_domain)", sizeof(dcc[idx].nick));
  516. putlog(LOG_DEBUG, "*", "Listening on telnet %s", conf.localhub_socket);
  517. }
  518. }
  519. }
  520. }
  521. void chanprog()
  522. {
  523. struct utsname un;
  524. sdprintf("I am: %s", conf.bot->nick);
  525. /* Add the 'default' virtual channel.
  526. The flags will be overwritten when the userfile is loaded,
  527. but if there is no userfile yet, or no 'default' channel,
  528. then it will take on the properties of 'def_chanset',
  529. and then later save this to the userfile.
  530. */
  531. channel_add(NULL, "default", def_chanset, 1);
  532. if (conf.bot->hub) {
  533. simple_snprintf(userfile, 121, "%s/.u", dirname(binname));
  534. loading = 1;
  535. checkchans(0);
  536. readuserfile(userfile, &userlist);
  537. checkchans(1);
  538. var_parse_my_botset();
  539. loading = 0;
  540. }
  541. load_internal_users();
  542. add_myself_to_userlist();
  543. if (conf.bot->localhub)
  544. add_child_bots();
  545. else if (!conf.bot->hub)
  546. add_localhub();
  547. rehash_ip();
  548. /* set our shell info */
  549. uname(&un);
  550. set_user(&USERENTRY_OS, conf.bot->u, un.sysname);
  551. set_user(&USERENTRY_USERNAME, conf.bot->u, conf.username);
  552. set_user(&USERENTRY_NODENAME, conf.bot->u, un.nodename);
  553. set_user(&USERENTRY_ARCH, conf.bot->u, un.machine);
  554. set_user(&USERENTRY_OSVER, conf.bot->u, un.release);
  555. var_parse_my_botset();
  556. /* We should be safe now */
  557. reaffirm_owners();
  558. }
  559. /* Reload the user file from disk
  560. */
  561. void reload()
  562. {
  563. FILE *f = fopen(userfile, "r");
  564. if (f == NULL) {
  565. putlog(LOG_MISC, "*", "Can't reload user file!");
  566. return;
  567. }
  568. fclose(f);
  569. noshare = 1;
  570. clear_userlist(userlist);
  571. noshare = 0;
  572. userlist = NULL;
  573. loading = 1;
  574. checkchans(0);
  575. if (!readuserfile(userfile, &userlist))
  576. fatal("User file is missing!", 0);
  577. /* ensure we did not lose our internal users */
  578. load_internal_users();
  579. /* make sure I am added and conf.bot->u is set */
  580. add_myself_to_userlist();
  581. if (conf.bot->localhub)
  582. add_child_bots();
  583. else if (!conf.bot->hub)
  584. add_localhub();
  585. /* Make sure no removed users/bots are still connected. */
  586. check_stale_dcc_users();
  587. for (tand_t* bot = tandbot; bot; bot = bot->next)
  588. bot->u = get_user_by_handle(userlist, bot->bot);
  589. /* I don't think these will ever be called anyway. */
  590. if (!conf.bot->hub) {
  591. Auth::FillUsers();
  592. check_hostmask();
  593. }
  594. checkchans(1);
  595. loading = 0;
  596. var_parse_my_botset();
  597. reaffirm_owners();
  598. hook_read_userfile();
  599. }
  600. void setup_HQ(int n) {
  601. dcc[n].addr = iptolong(getmyip());
  602. dcc[n].sock = STDOUT;
  603. dcc[n].timeval = now;
  604. dcc[n].u.chat->con_flags = conmask | LOG_ALL;
  605. dcc[n].u.chat->strip_flags = STRIP_ALL;
  606. dcc[n].status = STAT_ECHO;
  607. strlcpy(dcc[n].nick, STR("HQ"), sizeof(dcc[n].nick));
  608. strlcpy(dcc[n].host, STR("llama@console"), sizeof(dcc[n].host));
  609. dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
  610. /* Make sure there's an innocuous HQ user if needed */
  611. if (!dcc[n].user) {
  612. userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_VOICE | USER_OP | USER_PARTY | USER_CHUBA | USER_HUBA, 0);
  613. dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
  614. }
  615. }
  616. /* Oddly enough, written by proton (Emech's coder)
  617. */
  618. int isowner(char *name)
  619. {
  620. if (!owner[0])
  621. return (0);
  622. if (!name || !name[0])
  623. return (0);
  624. char *pa = owner, *pb = owner;
  625. size_t nl = strlen(name), pl;
  626. while (1) {
  627. while (1) {
  628. if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
  629. break;
  630. pb++;
  631. }
  632. pl = pb - pa;
  633. if (pl == nl && !strncasecmp(pa, name, nl))
  634. return (1);
  635. while (1) {
  636. if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
  637. break;
  638. pb++;
  639. }
  640. if (*pb == 0)
  641. return (0);
  642. pa = pb;
  643. }
  644. }
  645. bool bot_shouldjoin(struct userrec* u, struct flag_record* fr, const struct chanset_t* chan, bool ignore_inactive)
  646. {
  647. // If restarting, keep this channel.
  648. if (restarting && (reset_chans == 2) && (channel_active(chan) || channel_pending(chan))) return 1;
  649. /* If the bot is restarting (and hasn't finished getting the userfile for the first time) DO NOT JOIN channels - breaks +B/+backup */
  650. if (restarting || loading) return 0;
  651. // No user record, can't make any safe assumptions really
  652. if (!u) return 0;
  653. // Is this bot in the groups that this channel has?
  654. const char *botgroups = u == conf.bot->u ? groups : var_get_bot_data(u, "groups", true);
  655. bd::Array<bd::String> my_groupsArray(bd::String(botgroups).split(','));
  656. bool group_match = 0;
  657. if (chan->groups && chan->groups->length()) {
  658. for (size_t i = 0; i < my_groupsArray.length(); ++i) {
  659. if (chan->groups->find(my_groupsArray[i]) != chan->groups->npos) {
  660. group_match = 1;
  661. break;
  662. }
  663. }
  664. }
  665. // Ignore +inactive during cmd_slowjoin to ensure that +backup bots join
  666. return (!glob_kick(*fr) && !chan_kick(*fr) && // Not being kicked
  667. ((ignore_inactive || !channel_inactive(chan)) && // Not inactive
  668. ((channel_backup(chan) && (glob_backup(*fr) || chan_backup(*fr) || group_match)) || // Is +backup and I'm a backup bot or my group matches
  669. (!channel_backup(chan) && !glob_backup(*fr) && !chan_backup(*fr) && group_match))) // is -backup and I am not a backup bot and my group matches
  670. );
  671. }
  672. bool shouldjoin(const struct chanset_t *chan)
  673. {
  674. struct flag_record fr = { FR_CHAN|FR_GLOBAL|FR_BOT, 0, 0, 0 };
  675. get_user_flagrec(conf.bot->u, &fr, chan->dname, chan);
  676. return bot_shouldjoin(conf.bot->u, &fr, chan);
  677. }
  678. /* do_chanset() set (options) on (chan)
  679. * USES DO_LOCAL|DO_NET bits.
  680. */
  681. int do_chanset(char *result, struct chanset_t *chan, const char *options, int flags)
  682. {
  683. int ret = OK;
  684. if (flags & DO_NET) {
  685. size_t bufsiz = 0;
  686. /* malloc(options,chan,'cset ',' ',+ 1) */
  687. if (chan)
  688. bufsiz = strlen(options) + strlen(chan->dname) + 5 + 1 + 1;
  689. else
  690. bufsiz = strlen(options) + 1 + 5 + 1 + 1;
  691. char *buf = (char*) my_calloc(1, bufsiz);
  692. strlcat(buf, "cset ", bufsiz);
  693. if (chan)
  694. strlcat(buf, chan->dname, bufsiz);
  695. else
  696. strlcat(buf, "*", bufsiz);
  697. strlcat(buf, " ", bufsiz);
  698. strlcat(buf, options, bufsiz);
  699. putlog(LOG_DEBUG, "*", "sending out cset: %s", buf);
  700. putallbots(buf);
  701. free(buf);
  702. }
  703. if (flags & DO_LOCAL) {
  704. bool cmd = (flags & CMD);
  705. struct chanset_t *ch = NULL;
  706. int all = chan ? 0 : 1;
  707. if (chan)
  708. ch = chan;
  709. else
  710. ch = chanset_default; //First iteration changes default, then move on to all chans
  711. while (ch) {
  712. const char **item = NULL;
  713. int items = 0;
  714. if (SplitList(result, options, &items, &item) == OK) {
  715. ret = channel_modify(result, ch, items, (char **) item, cmd);
  716. } else
  717. ret = ERROR;
  718. free(item);
  719. if (all) {
  720. if (ret == ERROR) /* just bail if there was an error, no sense in trying more */
  721. return ret;
  722. if (ch == chanset_default)
  723. ch = chanset;
  724. else
  725. ch = ch->next;
  726. } else {
  727. ch = NULL;
  728. }
  729. }
  730. }
  731. return ret;
  732. }
  733. char *
  734. samechans(const char *nick, const char *delim)
  735. {
  736. static char ret[1024] = "";
  737. struct chanset_t *chan = NULL;
  738. ret[0] = 0; /* may be filled from last time */
  739. for (chan = chanset; chan; chan = chan->next) {
  740. if (ismember(chan, nick)) {
  741. strlcat(ret, chan->dname, sizeof(ret));
  742. strlcat(ret, delim, sizeof(ret));
  743. }
  744. }
  745. return ret;
  746. }
  747. struct chanset_t* find_common_opped_chan(bd::String nick) {
  748. for (struct chanset_t* chan = chanset; chan; chan = chan->next) {
  749. if (channel_active(chan) && (me_op(chan) || me_voice(chan))) {
  750. if (ismember(chan, nick.c_str()))
  751. return chan;
  752. }
  753. }
  754. return NULL;
  755. }
  756. void privmsg(bd::String target, bd::String msg, int idx) {
  757. struct chanset_t* chan = NULL;
  758. bool talking_to_chan = strchr(CHANMETA, target[0]);
  759. if (have_cprivmsg && !talking_to_chan)
  760. chan = find_common_opped_chan(target);
  761. bool cleartextPrefix = (msg(0, 3) == "+p ");
  762. // Encrypt with FiSH?
  763. if (!cleartextPrefix && FishKeys.contains(target) && FishKeys[target]->sharedKey.length()) {
  764. msg = "+OK " + egg_bf_encrypt(msg, FishKeys[target]->sharedKey);
  765. }
  766. if (cleartextPrefix) {
  767. msg += static_cast<size_t>(3);
  768. }
  769. if (chan)
  770. dprintf(idx, "CPRIVMSG %s %s :%s\n", target.c_str(), chan->name, msg.c_str());
  771. else
  772. dprintf(idx, "PRIVMSG %s :%s\n", target.c_str(), msg.c_str());
  773. }
  774. void notice(bd::String target, bd::String msg, int idx) {
  775. struct chanset_t* chan = NULL;
  776. bool talking_to_chan = strchr(CHANMETA, target[0]);
  777. if (have_cnotice && !talking_to_chan)
  778. chan = find_common_opped_chan(target);
  779. bool cleartextPrefix = (msg(0, 3) == "+p ");
  780. if (!cleartextPrefix && FishKeys.contains(target) && FishKeys[target]->sharedKey.length()) {
  781. msg = "+OK " + egg_bf_encrypt(msg, FishKeys[target]->sharedKey);
  782. }
  783. if (cleartextPrefix) {
  784. msg += static_cast<size_t>(3);
  785. }
  786. if (chan)
  787. dprintf(idx, "CNOTICE %s %s :%s\n", target.c_str(), chan->name, msg.c_str());
  788. else
  789. dprintf(idx, "NOTICE %s :%s\n", target.c_str(), msg.c_str());
  790. }
  791. void keyx(const bd::String &target) {
  792. bd::String myPublicKeyB64, myPrivateKey, sharedKey;
  793. DH1080_gen(myPrivateKey, myPublicKeyB64);
  794. fish_data_t* fishData = FishKeys.contains(target) ? FishKeys[target] : new fish_data_t;
  795. fishData->sharedKey.clear();
  796. putlog(LOG_MSGS, "*", "[FiSH] Initiating DH1080 key-exchange with %s - sending my public key", target.c_str());
  797. notice(target, "DH1080_INIT " + myPublicKeyB64, DP_HELP);
  798. fishData->myPublicKeyB64 = myPublicKeyB64;
  799. fishData->myPrivateKey = myPrivateKey;
  800. fishData->timestamp = now;
  801. FishKeys[target] = fishData;
  802. }
  803. void set_fish_key(char *target, bd::String key)
  804. {
  805. fish_data_t* fishData = FishKeys.contains(target) ? FishKeys[target] : NULL;
  806. if (!key.length()) { //remove key
  807. if (fishData) {
  808. FishKeys.remove(target);
  809. delete fishData;
  810. }
  811. } else { //set key
  812. fishData = new fish_data_t;
  813. if (key == "rand") {
  814. // Set a RANDOM key
  815. const size_t randomKeyLength = 32;
  816. char *rand_key = (char*)my_calloc(1, randomKeyLength+1);
  817. make_rand_str(rand_key, randomKeyLength);
  818. fishData->sharedKey = rand_key;
  819. free(rand_key);
  820. } else {
  821. fishData->sharedKey = key;
  822. }
  823. // Set the key
  824. fishData->timestamp = now;
  825. FishKeys[target] = fishData;
  826. }
  827. }