conf.cc 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. /*
  2. * conf.c -- handles:
  3. *
  4. * all of the conf handling
  5. */
  6. #include "common.h"
  7. #include "conf.h"
  8. #include "shell.h"
  9. #include "binary.h"
  10. #include "debug.h"
  11. #include "chanprog.h"
  12. #include "crypt.h"
  13. #include "main.h"
  14. #include "settings.h"
  15. #include "src/mod/irc.mod/irc.h"
  16. #include "misc.h"
  17. #include "users.h"
  18. #include "misc_file.h"
  19. #include "socket.h"
  20. #include "botnet.h"
  21. #include "userrec.h"
  22. #include <errno.h>
  23. #include "EncryptedStream.h"
  24. #include <bdlib/src/String.h>
  25. #ifdef HAVE_PATHS_H
  26. # include <paths.h>
  27. #endif /* HAVE_PATHS_H */
  28. #include <sys/types.h>
  29. #include <sys/wait.h>
  30. #include <sys/stat.h>
  31. #include <time.h>
  32. #include <sys/time.h>
  33. #include <signal.h>
  34. #ifdef HAVE_LIMITS_H
  35. # include <limits.h>
  36. #endif
  37. conf_t conf; /* global conf struct */
  38. static void
  39. tellconf()
  40. {
  41. conf_bot *bot = NULL;
  42. int i = 0;
  43. sdprintf(STR("tempdir: %s\n"), replace(tempdir, conf.homedir, "~"));
  44. sdprintf(STR("features: %d\n"), conf.features);
  45. sdprintf(STR("uid: %d\n"), conf.uid);
  46. sdprintf(STR("homedir: %s\n"), conf.homedir);
  47. sdprintf(STR("username: %s\n"), conf.username);
  48. sdprintf(STR("datadir: %s\n"), replace(conf.datadir, conf.homedir, "~"));
  49. sdprintf(STR("portmin: %d\n"), conf.portmin);
  50. sdprintf(STR("portmax: %d\n"), conf.portmax);
  51. sdprintf(STR("autocron: %d\n"), conf.autocron);
  52. sdprintf(STR("bots:\n"));
  53. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  54. i++;
  55. sdprintf(STR("%d: %s%s IP: %s HOST: %s IP6: %s HOST6: %s v6: %d HUB: %d PID: %d\n"), i,
  56. bot->disabled ? "/" : "",
  57. bot->nick,
  58. bot->net.ip ? bot->net.ip : "",
  59. bot->net.host ? bot->net.host : "", bot->net.ip6 ? bot->net.ip6 : "", bot->net.host6 ? bot->net.host6 : "",
  60. bot->net.v6,
  61. bot->hub,
  62. (int)bot->pid);
  63. }
  64. if (conf.bot && ((bot = conf.bot))) {
  65. sdprintf(STR("me:\n"));
  66. sdprintf(STR("%s%s IP: %s HOST: %s IP6: %s HOST6: %s v6: %d HUB: %d PID: %d\n"),
  67. bot->disabled ? "/" : "",
  68. bot->nick,
  69. bot->net.ip ? bot->net.ip : "",
  70. bot->net.host ? bot->net.host : "", bot->net.ip6 ? bot->net.ip6 : "", bot->net.host6 ? bot->net.host6 : "",
  71. bot->net.v6,
  72. bot->hub,
  73. (int)bot->pid);
  74. }
  75. }
  76. void spawnbot(const char *nick)
  77. {
  78. int status = 0;
  79. sdprintf("Spawning '%s'", nick);
  80. const char* argv[] = {binname, nick, 0};
  81. status = simple_exec(argv);
  82. if (status == -1 || WEXITSTATUS(status))
  83. sdprintf("Failed to spawn '%s': %s", nick, strerror(errno));
  84. }
  85. /* spawn and kill bots accordingly
  86. * bots prefixxed with '/' will be killed auto if running.
  87. * if (updating) then we were called with -U or -u */
  88. void
  89. spawnbots(conf_bot *bots, bool rehashed)
  90. {
  91. conf_bot *bot = NULL;
  92. for (bot = bots; bot && bot->nick; bot = bot->next) {
  93. sdprintf("checking bot: %s", bot->nick);
  94. if (bot->disabled) {
  95. /* kill it if running */
  96. if (bot->pid) {
  97. kill(bot->pid, SIGKILL);
  98. bot->pid = 0;
  99. } else
  100. continue;
  101. /* if we're updating automatically, we were called with -u and are only supposed to kill non-localhubs
  102. -if updating and we find our nick, skip
  103. -if pid exists and not updating, bot is running and we have nothing more to do, skip.
  104. */
  105. } else if ((conf.bot && !strcasecmp(bot->nick, conf.bot->nick) &&
  106. (updating == UPDATE_AUTO || rehashed)) || (bot->pid && !updating)) {
  107. sdprintf(STR(" ... skipping. Updating: %d, pid: %d"), updating, (int)bot->pid);
  108. continue;
  109. } else {
  110. /* if we are updating with -u then we need to restart ALL bots */
  111. if (updating == UPDATE_AUTO && bot->pid) {
  112. kill(bot->pid, SIGHUP);
  113. continue;
  114. }
  115. spawnbot(bot->nick);
  116. }
  117. }
  118. }
  119. int
  120. conf_killbot(conf_bot *bots, const char *botnick, conf_bot *bot, int signal, bool notbotnick)
  121. {
  122. int ret = -1;
  123. if (bot) {
  124. if (bot->pid)
  125. ret = kill(bot->pid, signal);
  126. } else {
  127. for (bot = bots; bot && bot->nick; bot = bot->next) {
  128. /* kill all bots but myself if botnick==NULL, otherwise just kill botnick */
  129. if ((!conf.bot) ||
  130. (!botnick &&
  131. (conf.bot->nick && strcasecmp(conf.bot->nick, bot->nick))) ||
  132. (botnick &&
  133. ((notbotnick == 0 && !strcasecmp(botnick, bot->nick)) ||
  134. (notbotnick == 1 && strcasecmp(botnick, bot->nick))
  135. )
  136. )
  137. ) {
  138. if (bot->pid)
  139. ret = kill(bot->pid, signal);
  140. if (botnick && !notbotnick)
  141. break;
  142. }
  143. }
  144. }
  145. return ret;
  146. }
  147. static int
  148. my_gettime(struct timespec *ts)
  149. {
  150. int rval;
  151. #if defined(HAVE_GETTIMEOFDAY) && (defined(HAVE_ST_MTIM) || defined(HAVE_ST_MTIMESPEC))
  152. struct timeval tv;
  153. rval = gettimeofday(&tv, NULL);
  154. ts->tv_sec = tv.tv_sec;
  155. ts->tv_nsec = tv.tv_usec * 1000;
  156. #else
  157. rval = (int)time(&ts->tv_sec);
  158. ts->tv_nsec = 0;
  159. #endif
  160. return (rval);
  161. }
  162. void
  163. confedit()
  164. {
  165. Tempfile tmpconf = Tempfile("conf");
  166. const char *editor = NULL;
  167. mode_t um;
  168. int waiter;
  169. pid_t pid, xpid;
  170. struct stat st, sn;
  171. struct timespec ts1, ts2; /* time before and after edit */
  172. bool autowrote = 0;
  173. conf_bot *oldbots = NULL;
  174. um = umask(077);
  175. autowrote = writeconf(NULL, tmpconf.fd, CONF_COMMENT);
  176. fsync(tmpconf.fd);
  177. fstat(tmpconf.fd, &st); /* for file modification compares */
  178. // tmpconf.my_close();
  179. umask(um);
  180. if (!can_stat(tmpconf.file))
  181. fatal(STR("Cannot stat tempfile"), 0);
  182. /* Okay, edit the file */
  183. if ((!((editor = getenv(STR("EDITOR"))) && strlen(editor)))
  184. && (!((editor = getenv(STR("VISUAL"))) && strlen(editor)))
  185. ) {
  186. editor = STR("vi");
  187. /*
  188. #if defined(DEBIAN)
  189. editor = "/usr/bin/editor";
  190. #elif defined(_PATH_VI)
  191. editor = _PATH_VI;
  192. #else
  193. editor = "/usr/ucb/vi";
  194. #endif
  195. */
  196. }
  197. signal(SIGINT, SIG_IGN);
  198. signal(SIGQUIT, SIG_IGN);
  199. signal(SIGCONT, SIG_DFL);
  200. my_gettime(&ts1);
  201. switch (pid = vfork()) {
  202. case -1:
  203. fatal(STR("Cannot fork"), 0);
  204. case 0:
  205. {
  206. /* child */
  207. execlp(editor, editor, tmpconf.file, (char*)NULL);
  208. perror(editor);
  209. _exit(127);
  210. }
  211. default:
  212. /* parent */
  213. break;
  214. }
  215. /* parent */
  216. while (1) {
  217. xpid = waitpid(pid, &waiter, WUNTRACED);
  218. my_gettime(&ts2);
  219. if (xpid == -1) {
  220. fprintf(stderr, STR("waitpid() failed waiting for PID %d from \"%s\": %s\n"), pid, editor, strerror(errno));
  221. } else if (xpid != pid) {
  222. fprintf(stderr, STR("wrong PID (%d != %d) from \"%s\"\n"), xpid, pid, editor);
  223. goto fatal;
  224. } else if (WIFSTOPPED(waiter)) {
  225. /* raise(WSTOPSIG(waiter)); Not needed and breaks in job control shell */
  226. } else if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) {
  227. fprintf(stderr, STR("\"%s\" exited with status %d\n"), editor, WEXITSTATUS(waiter));
  228. goto fatal;
  229. } else if (WIFSIGNALED(waiter)) {
  230. fprintf(stderr, STR("\"%s\" killed; signal %d (%score dumped)\n"), editor, WTERMSIG(waiter),
  231. WCOREDUMP(waiter)
  232. ? "" : "no ");
  233. goto fatal;
  234. } else {
  235. break;
  236. }
  237. }
  238. signal(SIGINT, SIG_DFL);
  239. signal(SIGQUIT, SIG_DFL);
  240. if (fstat(tmpconf.fd, &sn))
  241. fatal(STR("Error reading new config file"), 0);
  242. if (!autowrote && st.st_size == sn.st_size &&
  243. mtim_getsec(st) == mtim_getsec(sn) &&
  244. mtim_getnsec(st) == mtim_getnsec(sn)) {
  245. /*
  246. * If mtime and size match but the user spent no measurable
  247. * time in the editor we can't tell if the file was changed.
  248. */
  249. #ifdef HAVE_TIMESPECSUB2
  250. timespecsub(&ts1, &ts2);
  251. #else
  252. timespecsub(&ts1, &ts2, &ts2);
  253. #endif
  254. if (timespecisset(&ts2)) {
  255. printf(STR("* Config unchanged.\n"));
  256. tmpconf.my_close();
  257. unlink(tmpconf.file);
  258. exit(0);
  259. }
  260. }
  261. tmpconf.my_close();
  262. oldbots = conf_bots_dup(conf.bots);
  263. free_conf();
  264. readconf((const char *) tmpconf.file, 0); /* read cleartext conf tmp into &settings */
  265. expand_tilde(&conf.datadir);
  266. unlink(tmpconf.file);
  267. conf_to_bin(&conf, 0, -1);
  268. /* Now signal all of the old bots with SIGUSR1
  269. * They will auto die and determine new localhub, etc..
  270. */
  271. conf_checkpids(oldbots);
  272. conf_killbot(oldbots, NULL, NULL, SIGUSR1);
  273. /* Now spawn new bots */
  274. // spawnbots(conf.bots);
  275. exit(0);
  276. fatal:
  277. unlink(tmpconf.file);
  278. exit(1);
  279. }
  280. void
  281. init_conf()
  282. {
  283. // conf.bots = (conf_bot *) calloc(1, sizeof(conf_bot));
  284. // conf.bots->nick = NULL;
  285. // conf.bots->next = NULL;
  286. conf.bots = NULL;
  287. conf.bot = NULL;
  288. // If conf_hubs is blank, revert to pack hubs
  289. if (strlen(settings.conf_hubs)) {
  290. conf.hubs = bd::String(settings.conf_hubs).split(',');
  291. } else {
  292. conf.hubs = bd::String(settings.hubs).split(',');
  293. }
  294. conf.localhub = NULL;
  295. conf.autocron = 1;
  296. conf.features = 0;
  297. conf.portmin = 0;
  298. conf.portmax = 0;
  299. conf.uid = -1;
  300. conf.username = NULL;
  301. conf.homedir = NULL;
  302. conf.datadir = strdup(STR("./..."));
  303. expand_tilde(&conf.datadir);
  304. }
  305. void conf_checkpids(conf_bot *bots, bool all)
  306. {
  307. conf_bot *bot = NULL;
  308. for (bot = bots; bot && bot->nick; bot = bot->next)
  309. if (all || (!all && bot->pid == 0))
  310. bot->pid = checkpid(bot->nick, bot);
  311. }
  312. static char* datafile(const char* prefix, const char* nick) {
  313. static char buf[DIRMAX] = "";
  314. char *tmpnick = NULL, *tmp_ptr = NULL;
  315. tmpnick = tmp_ptr = strdup(nick);
  316. strtolower(tmpnick);
  317. simple_snprintf(buf, sizeof buf, STR("%s/.%s.%s"), conf.datadir, prefix, tmpnick);
  318. free(tmp_ptr);
  319. return buf;
  320. }
  321. /*
  322. * Return the PID of a bot if it is running, otherwise return 0
  323. */
  324. pid_t
  325. checkpid(const char *nick, conf_bot *bot)
  326. {
  327. FILE *f = NULL;
  328. pid_t pid = 0;
  329. char buf[DIRMAX] = "";
  330. char *bufp = datafile("pid", nick);
  331. if (bot && !(bot->pid_file))
  332. bot->pid_file = strdup(bufp);
  333. else if (bot && strcasecmp(bot->pid_file, bufp))
  334. str_redup(&bot->pid_file, bufp);
  335. if ((f = fopen(bufp, "r"))) {
  336. char *pids = NULL;
  337. if (!fgets(buf, sizeof(buf), f)) {
  338. fclose(f);
  339. return 0;
  340. }
  341. fclose(f);
  342. remove_crlf(buf);
  343. if (!buf[0])
  344. return 0;
  345. bufp = buf;
  346. pids = newsplit(&bufp);
  347. if (str_isdigit(pids)) {
  348. pid = atoi(pids);
  349. if (kill(pid, SIGCHLD)) //Problem killing, most likely it's just not running.
  350. pid = 0;
  351. }
  352. if (bufp[0] && pid && can_stat(bufp) && (mypid == pid) &&
  353. !strncasecmp(nick, origbotnick, HANDLEN)) {
  354. socksfile = strdup(bufp);
  355. return 0;
  356. }
  357. }
  358. return pid;
  359. }
  360. void
  361. conf_addbot(const char *nick, const char *ip, const char *host, const char *ip6)
  362. {
  363. conf_bot *bot = (conf_bot *) calloc(1, sizeof(conf_bot));
  364. bot->next = NULL;
  365. bot->pid_file = NULL;
  366. if (nick[0] == '/') {
  367. bot->disabled = 1;
  368. ++nick;
  369. sdprintf(STR("%s is disabled."), nick);
  370. }
  371. bot->nick = strldup(nick, HANDLEN);
  372. bot->net.ip = NULL;
  373. bot->net.host = NULL;
  374. bot->net.ip6 = NULL;
  375. bot->net.host6 = NULL;
  376. if (host && host[0] == '+') {
  377. ++host;
  378. bot->net.host6 = strdup(host);
  379. } else if (host && !strchr(".*", host[0])) {
  380. bot->net.host = strdup(host);
  381. }
  382. if (ip && !strchr(".*", ip[0])) {
  383. int aftype = is_dotted_ip(ip);
  384. if (aftype == AF_INET)
  385. bot->net.ip = strdup(ip);
  386. #ifdef USE_IPV6
  387. else if (aftype == AF_INET6)
  388. bot->net.ip6 = strdup(ip);
  389. #endif /* USE_IPV6 */
  390. else if (aftype == 0) { /* The idiot used a hostname in place of ip */
  391. if (bot->net.host)
  392. free(bot->net.host);
  393. bot->net.host = strdup(ip);
  394. }
  395. }
  396. #ifdef USE_IPV6
  397. if (ip6 && !strchr(".*", ip6[0]) && is_dotted_ip(ip6) == AF_INET6)
  398. bot->net.ip6 = strdup(ip6);
  399. if (bot->net.ip6 || bot->net.host6)
  400. bot->net.v6 = 1;
  401. #endif /* USE_IPV6 */
  402. if (userlist)
  403. bot->u = get_user_by_handle(userlist, (char*)nick);
  404. else
  405. bot->u = NULL;
  406. // bot->pid = checkpid(nick, bot);
  407. if (is_hub(bot->nick))
  408. bot->hub = 1;
  409. /* not a hub
  410. AND
  411. * no bots added yet (first bot) yet, not disabled.
  412. OR
  413. * bots already listed but we dont have a localhub yet, so we're it!
  414. */
  415. if (!conf.localhub && !bot->hub && !bot->disabled) {
  416. bot->localhub = 1; /* first bot */
  417. conf.localhub = strdup(bot->nick);
  418. conf.localhub_socket = strdup(datafile("sock", conf.localhub));
  419. }
  420. list_append((struct list_type **) &(conf.bots), (struct list_type *) bot);
  421. }
  422. void
  423. free_bot(conf_bot *bot)
  424. {
  425. if (bot) {
  426. list_delete((struct list_type **) &(conf.bots), (struct list_type *) bot);
  427. free(bot->nick);
  428. free(bot->pid_file);
  429. if (bot->net.ip)
  430. free(bot->net.ip);
  431. if (bot->net.host)
  432. free(bot->net.host);
  433. if (bot->net.ip6)
  434. free(bot->net.ip6);
  435. if (bot->net.host6)
  436. free(bot->net.host6);
  437. free(bot);
  438. }
  439. }
  440. int
  441. conf_delbot(char *botn, bool kill)
  442. {
  443. conf_bot *bot = NULL;
  444. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  445. if (!strcasecmp(bot->nick, botn)) { /* found it! */
  446. if (kill) {
  447. bot->pid = checkpid(bot->nick, bot);
  448. conf_killbot(conf.bots, NULL, bot, SIGKILL);
  449. }
  450. free_bot(bot);
  451. return 0;
  452. }
  453. }
  454. return 1;
  455. }
  456. void
  457. free_conf()
  458. {
  459. free_conf_bots(conf.bots);
  460. free_bot(conf.bot);
  461. conf.bot = NULL;
  462. if (conf.localhub)
  463. free(conf.localhub);
  464. if (conf.username)
  465. free(conf.username);
  466. if (conf.datadir)
  467. free(conf.datadir);
  468. if (conf.homedir)
  469. free(conf.homedir);
  470. conf.hubs.clear();
  471. init_conf();
  472. }
  473. void
  474. free_conf_bots(conf_bot *list)
  475. {
  476. if (list) {
  477. conf_bot *bot = NULL, *bot_n = NULL;
  478. for (bot = list; bot; bot = bot_n) {
  479. bot_n = bot->next;
  480. free_bot(bot);
  481. }
  482. list = NULL;
  483. }
  484. }
  485. void prep_homedir(bool error)
  486. {
  487. if (!conf.homedir)
  488. str_redup(&conf.homedir, homedir());
  489. if (error && (!conf.homedir || !conf.homedir[0]))
  490. werr(ERR_NOHOMEDIR);
  491. }
  492. int
  493. parseconf(bool error)
  494. {
  495. if (error && conf.uid == -1 && !conf.homedir)
  496. werr(ERR_NOTINIT);
  497. if (!conf.username)
  498. str_redup(&conf.username, my_username());
  499. if (error && (!conf.username || !conf.username[0]))
  500. werr(ERR_NOUSERNAME);
  501. if (error && conf.uid != (signed) myuid) {
  502. sdprintf(STR("wrong uid, conf: %d :: %d"), conf.uid, myuid);
  503. werr(ERR_WRONGUID);
  504. } else if (!conf.uid)
  505. conf.uid = myuid;
  506. return 0;
  507. }
  508. int
  509. readconf(const char *fname, int bits)
  510. {
  511. int enc = (bits & CONF_ENC) ? 1 : 0;
  512. bd::Stream* stream;
  513. size_t bots = 0;
  514. if (enc) {
  515. const char salt1[] = SALT1;
  516. stream = new EncryptedStream(salt1);
  517. } else
  518. stream = new bd::Stream;
  519. sdprintf(STR("readconf(%s, %d)"), fname, enc);
  520. if (stream->loadFile(fname)) {
  521. delete stream;
  522. fatal(STR("Cannot read config"), 0);
  523. }
  524. conf.hubs.clear();
  525. free_conf_bots(conf.bots);
  526. bd::String line, option;
  527. unsigned short hublevel = 0;
  528. while (stream->tell() < stream->length()) {
  529. line = stream->getline().chomp().trim();
  530. // Skip blank lines
  531. if (!line)
  532. continue;
  533. sdprintf(STR("CONF LINE: %s"), line.c_str());
  534. // !strchr("_`|}][{*/#-+!abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPWRSTUVWXYZ", line[0])) {
  535. /* - uid */
  536. if (line[0] == '!') {
  537. ++line;
  538. line.trim();
  539. option.clear();
  540. if (line.length())
  541. option = newsplit(line);
  542. if (!option || !line)
  543. continue;
  544. // option.toLower();
  545. if (option == STR("autocron")) { /* automatically check/create crontab? */
  546. if (egg_isdigit(line[0]))
  547. conf.autocron = atoi(line.c_str());
  548. } else if (option == STR("username")) { /* shell username */
  549. str_redup(&conf.username, line.c_str());
  550. } else if (option == STR("homedir")) { /* homedir */
  551. str_redup(&conf.homedir, line.c_str());
  552. } else if (option == STR("datadir")) { /* datadir */
  553. str_redup(&conf.datadir, line.c_str());
  554. } else if (option == STR("portmin")) {
  555. if (egg_isdigit(line[0]))
  556. conf.portmin = atoi(line.c_str());
  557. } else if (option == STR("portmax")) {
  558. if (egg_isdigit(line[0]))
  559. conf.portmax = atoi(line.c_str());
  560. } else if (option == STR("uid")) { /* new method uid */
  561. if (str_isdigit(line.c_str()))
  562. conf.uid = atoi(line.c_str());
  563. } else if (option == STR("hub")) {
  564. if (line.split(' ').length() == 3) {
  565. conf.hubs << bd::String::printf("%s %d", line.c_str(), ++hublevel);
  566. }
  567. } else {
  568. putlog(LOG_MISC, "*", STR("Unrecognized config option '%s'"), option.c_str());
  569. }
  570. /* read in portmin */
  571. } else if (line[0] == '>') {
  572. conf.portmin = atoi(newsplit(line).c_str());
  573. } else if (line[0] == '<') {
  574. conf.portmax = atoi(newsplit(line).c_str());
  575. /* now to parse nick/hosts */
  576. } else if (line[0] != '#') {
  577. bd::String nick, host, ip, ipsix;
  578. nick = newsplit(line);
  579. if (!nick)
  580. werr(ERR_BADCONF);
  581. ip = newsplit(line);
  582. host = newsplit(line);
  583. ipsix = newsplit(line);
  584. conf_addbot(nick.c_str(), ip.c_str(), host.c_str(), ipsix.c_str());
  585. ++bots;
  586. }
  587. } /* while(fgets()) */
  588. if (bots > 5)
  589. werr(ERR_TOOMANYBOTS);
  590. delete stream;
  591. return 0;
  592. }
  593. char s1_9[3] = "",s1_5[3] = "",s1_1[3] = "";
  594. static bool
  595. __attribute__((pure))
  596. hubSort (const bd::String& hub1, const bd::String& hub2) {
  597. const auto& hub1params(hub1.split(' '));
  598. const auto& hub2params(hub2.split(' '));
  599. unsigned short hub1level = 99, hub2level = 99;
  600. if (hub1params.length() == 4) {
  601. hub1level = atoi(hub1params[3].c_str());
  602. }
  603. if (hub2params.length() == 4) {
  604. hub2level = atoi(hub2params[3].c_str());
  605. }
  606. return hub1level < hub2level;
  607. }
  608. int
  609. writeconf(char *filename, int fd, int bits)
  610. {
  611. conf_bot *bot = NULL;
  612. int autowrote = 0;
  613. bd::Stream* stream;
  614. if (bits & CONF_ENC) {
  615. const char salt1[] = SALT1;
  616. stream = new EncryptedStream(salt1);
  617. } else
  618. stream = new bd::Stream;
  619. #define comment(text) do { \
  620. if (bits & CONF_COMMENT) \
  621. *stream << bd::String::printf(STR("%s\n"), text); \
  622. } while(0)
  623. char *p = NULL;
  624. comment("");
  625. #define conf_com() do { \
  626. if (do_confedit == CONF_AUTO) { \
  627. comment("# Automatically updated with -C"); \
  628. autowrote = 1; \
  629. } else \
  630. comment("#The correct line follows. (Not auto due to -c)"); \
  631. } while(0)
  632. if ((bits & CONF_COMMENT) && conf.uid != (signed) myuid) {
  633. conf_com();
  634. *stream << bd::String::printf(STR("%s! uid %d\n"), do_confedit == CONF_AUTO ? "" : "#", myuid);
  635. *stream << bd::String::printf(STR("%s! uid %d\n"), do_confedit == CONF_STATIC ? "" : "#", conf.uid);
  636. } else
  637. *stream << bd::String::printf(STR("! uid %d\n"), conf.uid);
  638. comment("");
  639. if (conf.username && my_username() && strcmp(conf.username, my_username())) {
  640. conf_com();
  641. *stream << bd::String::printf(STR("%s! username %s\n"), do_confedit == CONF_AUTO ? "" : "#", my_username());
  642. *stream << bd::String::printf(STR("%s! username %s\n"), do_confedit == CONF_STATIC ? "" : "#", conf.username);
  643. } else
  644. *stream << bd::String::printf(STR("! username %s\n"), conf.username ? conf.username : my_username() ? my_username() : "");
  645. if (conf.homedir && homedir(0) && strcmp(conf.homedir, homedir(0))) {
  646. conf_com();
  647. *stream << bd::String::printf(STR("%s! homedir %s\n"), do_confedit == CONF_AUTO ? "" : "#", homedir(0));
  648. *stream << bd::String::printf(STR("%s! homedir %s\n"), do_confedit == CONF_STATIC ? "" : "#", conf.homedir);
  649. } else
  650. *stream << bd::String::printf(STR("! homedir %s\n"), conf.homedir ? conf.homedir : homedir(0) ? homedir(0) : "");
  651. comment("");
  652. if (conf.datadir && strcmp(conf.datadir, "./...")) {
  653. comment("# datadir should be set to a static directory that is writable");
  654. if (homedir() && strstr(conf.datadir, homedir())) {
  655. p = replace(conf.datadir, homedir(), "~");
  656. *stream << bd::String::printf(STR("! datadir %s\n"), p);
  657. } else if (conf.homedir && strstr(conf.datadir, conf.homedir)) { /* Could be an older homedir */
  658. p = replace(conf.datadir, conf.homedir, "~");
  659. *stream << bd::String::printf(STR("! datadir %s\n"), p);
  660. } else
  661. *stream << bd::String::printf(STR("! datadir %s\n"), conf.datadir);
  662. comment("");
  663. }
  664. if (conf.portmin || conf.portmax) {
  665. comment("# portmin/max are for incoming connections (DCC) [0 for any] (These only make sense for HUBS)");
  666. *stream << bd::String::printf(STR("! portmin %d\n"), conf.portmin);
  667. *stream << bd::String::printf(STR("! portmax %d\n"), conf.portmax);
  668. comment("");
  669. }
  670. if (conf.autocron == 0) {
  671. comment("# Automatically add the bot to crontab?");
  672. *stream << bd::String::printf(STR("! autocron %d\n"), conf.autocron);
  673. comment("");
  674. }
  675. if (conf.hubs.length()) {
  676. comment("# Hubs this bot will connect to");
  677. // Sort hub list by hublevel
  678. bd::Array<bd::String> sortedhubs(conf.hubs);
  679. std::sort(sortedhubs.begin(), sortedhubs.end(), hubSort);
  680. for (const auto& hubLine : sortedhubs) {
  681. const auto& hubparams(hubLine.split(' '));
  682. const auto& hubnick(hubparams[0]), address(hubparams[1]);
  683. const in_port_t port = atoi(hubparams[2].c_str());
  684. *stream << bd::String::printf(STR("! hub %s %s %d\n"), hubnick.c_str(), address.c_str(), port);
  685. }
  686. comment("");
  687. }
  688. comment("# '|' means OR, [] means the enclosed is optional");
  689. comment("# A '+' in front of HOST means the HOST is ipv6");
  690. comment("# A '/' in front of BOT will disable that bot.");
  691. comment("#[/]BOT IP|* [+]HOST|* [IPV6-IP]");
  692. comment("#bot ip vhost");
  693. comment("#bot2 * vhost");
  694. comment("#bot3 ip");
  695. comment("#bot4 * +ipv6.vhost.com");
  696. comment("#bot5 * * ip:v6:ip:goes:here::");
  697. comment("### Hubs should have their own binary ###");
  698. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  699. *stream << bd::String::printf(STR("%s%s %s %s%s"),
  700. bot->disabled ? "/" : "", bot->nick,
  701. bot->net.ip ? bot->net.ip : "*", bot->net.host6 ? "+" : "",
  702. bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "*"));
  703. if (bot->net.ip6)
  704. *stream << bd::String::printf(STR(" %s"), bot->net.ip6 ? bot->net.ip6 : "");
  705. *stream << "\n";
  706. }
  707. if (fd != -1)
  708. stream->writeFile(fd);
  709. else
  710. stream->writeFile(filename);
  711. delete stream;
  712. return autowrote;
  713. }
  714. void
  715. conf_bot_dup(conf_bot *dest, conf_bot *src)
  716. {
  717. if (dest && src) {
  718. dest->nick = src->nick ? strdup(src->nick) : NULL;
  719. dest->pid_file = src->pid_file ? strdup(src->pid_file) : NULL;
  720. dest->net.ip = src->net.ip ? strdup(src->net.ip) : NULL;
  721. dest->net.host = src->net.host ? strdup(src->net.host) : NULL;
  722. dest->net.ip6 = src->net.ip6 ? strdup(src->net.ip6) : NULL;
  723. dest->net.host6 = src->net.host6 ? strdup(src->net.host6) : NULL;
  724. dest->net.v6 = src->net.v6;
  725. dest->u = src->u ? src->u : NULL;
  726. dest->pid = src->pid;
  727. dest->hub = src->hub;
  728. dest->localhub = src->localhub;
  729. dest->disabled = src->disabled;
  730. dest->next = NULL;
  731. }
  732. }
  733. conf_bot *conf_bots_dup(conf_bot *src)
  734. {
  735. conf_bot *ret = NULL;
  736. if (src) {
  737. conf_bot *bot = NULL, *newbot = NULL;
  738. for (bot = src; bot && bot->nick; bot = bot->next) {
  739. newbot = (conf_bot *) calloc(1, sizeof(conf_bot));
  740. conf_bot_dup(newbot, bot);
  741. list_append((struct list_type **) &(ret), (struct list_type *) newbot);
  742. }
  743. }
  744. return ret;
  745. }
  746. void deluser_removed_bots(conf_bot *oldlist, conf_bot *newlist)
  747. {
  748. if (oldlist) {
  749. conf_bot *botold = NULL, *botnew = NULL;
  750. bool found = 0;
  751. struct userrec *u = NULL;
  752. for (botold = oldlist; botold && botold->nick; botold = botold->next) {
  753. found = 0;
  754. for (botnew = newlist; botnew && botnew->nick; botnew = botnew->next) {
  755. if (!strcasecmp(botold->nick, botnew->nick)) {
  756. found = 1;
  757. break;
  758. }
  759. }
  760. if (!found && strcasecmp(botold->nick, origbotnick)) { /* Never kill ME.. will handle it elsewhere */
  761. /* No need to kill -- they are signalled and they will die on their own now */
  762. //botold->pid = checkpid(botold->nick, botold);
  763. //conf_killbot(conf.bots, NULL, botold, SIGKILL);
  764. if ((u = get_user_by_handle(userlist, botold->nick))) {
  765. putlog(LOG_MISC, "*", STR("Removing '%s' as it has been removed from the binary config."), botold->nick);
  766. if (server_online)
  767. check_this_user(botold->nick, 1, NULL);
  768. if (deluser(botold->nick)) {
  769. /* there is likely NO conf[]
  770. if (conf.bot->hub)
  771. write_userfile(-1);
  772. */
  773. }
  774. }
  775. }
  776. }
  777. }
  778. }
  779. void
  780. fill_conf_bot(bool fatal)
  781. {
  782. if (!conf.bots || !conf.bots->nick)
  783. return;
  784. char *mynick = NULL;
  785. conf_bot *me = NULL;
  786. /* This first clause should actually be obsolete */
  787. if (!used_B && conf.bots && conf.bots->nick) {
  788. mynick = strdup(conf.bots->nick);
  789. strlcpy(origbotnick, conf.bots->nick, sizeof(origbotnick));
  790. } else
  791. mynick = strldup(origbotnick, HANDLEN);
  792. sdprintf(STR("mynick: %s"), mynick);
  793. for (me = conf.bots; me && me->nick; me = me->next)
  794. if (!strcasecmp(me->nick, mynick))
  795. break;
  796. if (fatal && (!me || (me->nick && strcasecmp(me->nick, mynick))))
  797. werr(ERR_BADBOT);
  798. free(mynick);
  799. if (me) {
  800. if (!me->hub && me->localhub)
  801. sdprintf(STR("I am localhub!"));
  802. /* for future, we may just want to make this a pointer to ->bots if we do an emech style currentbot-> */
  803. conf.bot = (conf_bot *) calloc(1, sizeof(conf_bot));
  804. conf_bot_dup(conf.bot, me);
  805. }
  806. }
  807. void
  808. bin_to_conf(bool error)
  809. {
  810. /* printf("Converting binary data to conf struct\n"); */
  811. conf.features = atol(settings.features);
  812. conf.uid = atol(settings.uid);
  813. if (settings.username[0])
  814. str_redup(&conf.username, settings.username);
  815. str_redup(&conf.datadir, settings.datadir);
  816. if (settings.homedir[0])
  817. str_redup(&conf.homedir, settings.homedir);
  818. conf.portmin = atol(settings.portmin);
  819. conf.portmax = atol(settings.portmax);
  820. conf.autocron = atoi(settings.autocron);
  821. prep_homedir(error);
  822. expand_tilde(&conf.datadir);
  823. /* PARSE/ADD BOTS */
  824. {
  825. char *p = NULL, *tmp = NULL, *tmpp = NULL;
  826. tmp = tmpp = strdup(settings.bots);
  827. while ((p = strchr(tmp, ','))) {
  828. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  829. *p++ = 0;
  830. if (!tmp[0])
  831. break;
  832. nick = newsplit(&tmp);
  833. if (!nick || (nick && !nick[0]))
  834. werr(ERR_BADCONF);
  835. if (tmp[0])
  836. ip = newsplit(&tmp);
  837. if (tmp[0])
  838. host = newsplit(&tmp);
  839. if (tmp[0])
  840. ipsix = newsplit(&tmp);
  841. conf_addbot(nick, ip, host, ipsix);
  842. tmp = p++;
  843. }
  844. free(tmpp);
  845. }
  846. char datadir[PATH_MAX] = "";
  847. if (!realpath(conf.datadir, datadir)) {
  848. ;
  849. }
  850. str_redup(&conf.datadir, datadir);
  851. if (!mkdir_p(conf.datadir) && error)
  852. werr(ERR_DATADIR);
  853. str_redup(&conf.datadir, replace(datadir, bindir, "."));
  854. if (Tempfile::FindDir() == ERROR)
  855. werr(ERR_TMPSTAT);
  856. if (clear_tmpdir)
  857. clear_tmp(); /* clear out the tmp dir, no matter if we are localhub or not */
  858. conf_checkpids(conf.bots);
  859. tellconf();
  860. }
  861. void conf_update_hubs(struct userrec* list) {
  862. if (!conf.bot->hub && !conf.bot->localhub) {
  863. return;
  864. }
  865. bd::Array<bd::String> hubUsers, oldhubs(conf.hubs);
  866. // Count how many hubs there are
  867. for (struct userrec *u = list; u; u = u->next) {
  868. if (bot_hublevel(u) < 999) {
  869. hubUsers << u->handle;
  870. }
  871. }
  872. conf.hubs.clear();
  873. conf.hubs.reserve(hubUsers.length());
  874. for (const auto& hubUser : hubUsers) {
  875. struct userrec *u = get_user_by_handle(list, hubUser.c_str());
  876. struct bot_addr *bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  877. conf.hubs << bd::String::printf("%s %s %d %d", u->handle, bi->address, bi->telnet_port, bi->hublevel);
  878. }
  879. // Only rewrite binary / notify bots if the hubs changed
  880. if (conf.hubs == oldhubs) {
  881. return;
  882. }
  883. if (conf.bot->hub || conf.bot->localhub) {
  884. /* rewrite our binary */
  885. conf_to_bin(&conf, 0, -1);
  886. /* Now signal all of the old bots with SIGUSR1
  887. * They will auto die and determine new localhub, etc..
  888. */
  889. conf_checkpids(conf.bots);
  890. conf_killbot(conf.bots, conf.bot->nick, NULL, SIGUSR1, 1); /* Don't kill me. */
  891. }
  892. }
  893. void conf_add_userlist_bots()
  894. {
  895. conf_bot *bot = NULL;
  896. struct userrec *u = NULL;
  897. struct bot_addr *bi = NULL;
  898. char tmp[81] = "", uhost[UHOSTLEN] = "";
  899. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  900. /* Don't auto-add hubs. */
  901. if (!bot->hub && tands > 0 && !bot->disabled) {
  902. u = get_user_by_handle(userlist, bot->nick);
  903. if (!u) {
  904. putlog(LOG_MISC, "*", STR("Adding bot '%s' as it has been added to the binary config."), bot->nick);
  905. userlist = adduser(userlist, bot->nick, "none", "-", USER_OP, 1);
  906. u = get_user_by_handle(userlist, bot->nick);
  907. simple_snprintf(tmp, sizeof(tmp), "%li %s", (long)now, conf.bot->nick);
  908. set_user(&USERENTRY_ADDED, u, tmp);
  909. bi = (struct bot_addr *) calloc(1, sizeof(struct bot_addr));
  910. bi->address = (char *) calloc(1, 1);
  911. bi->uplink = (char *) calloc(1, 1);
  912. bi->telnet_port = bi->relay_port = 3333;
  913. bi->hublevel = 999;
  914. set_user(&USERENTRY_BOTADDR, u, bi);
  915. }
  916. if (bot->net.ip && strcmp(bot->net.ip, "0.0.0.0")) {
  917. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.ip);
  918. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  919. addhost_by_handle(bot->nick, uhost);
  920. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.ip);
  921. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  922. addhost_by_handle(bot->nick, uhost);
  923. }
  924. if (bot->net.host) {
  925. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.host);
  926. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  927. addhost_by_handle(bot->nick, uhost);
  928. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.host);
  929. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  930. addhost_by_handle(bot->nick, uhost);
  931. }
  932. if (bot->net.host6) {
  933. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.host6);
  934. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  935. addhost_by_handle(bot->nick, uhost);
  936. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.host6);
  937. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  938. addhost_by_handle(bot->nick, uhost);
  939. }
  940. if (bot->net.ip6 && strcmp(bot->net.ip6, "::")) {
  941. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.ip6);
  942. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  943. addhost_by_handle(bot->nick, uhost);
  944. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.ip6);
  945. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  946. addhost_by_handle(bot->nick, uhost);
  947. }
  948. }
  949. }
  950. }
  951. void conf_setmypid(pid_t pid) {
  952. conf.bot->pid = pid;
  953. conf_bot *bot = conf.bots;
  954. if (conf.bots) {
  955. for (; bot && strcasecmp(bot->nick, conf.bot->nick); bot = bot->next)
  956. ;
  957. if (bot)
  958. bot->pid = pid;
  959. }
  960. }
  961. /* vim: set sts=2 sw=2 ts=8 et: */