conf.cc 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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. bool hubSort (bd::String hub1, bd::String hub2) {
  595. bd::Array<bd::String> hub1params(static_cast<bd::String>(hub1).split(' '));
  596. bd::Array<bd::String> hub2params(static_cast<bd::String>(hub2).split(' '));
  597. unsigned short hub1level = 99, hub2level = 99;
  598. if (hub1params.length() == 4) {
  599. hub1level = atoi(static_cast<bd::String>(hub1params[3]).c_str());
  600. }
  601. if (hub2params.length() == 4) {
  602. hub2level = atoi(static_cast<bd::String>(hub2params[3]).c_str());
  603. }
  604. return hub1level < hub2level;
  605. }
  606. int
  607. writeconf(char *filename, int fd, int bits)
  608. {
  609. conf_bot *bot = NULL;
  610. int autowrote = 0;
  611. bd::Stream* stream;
  612. if (bits & CONF_ENC) {
  613. const char salt1[] = SALT1;
  614. stream = new EncryptedStream(salt1);
  615. } else
  616. stream = new bd::Stream;
  617. #define comment(text) do { \
  618. if (bits & CONF_COMMENT) \
  619. *stream << bd::String::printf(STR("%s\n"), text); \
  620. } while(0)
  621. char *p = NULL;
  622. comment("");
  623. #define conf_com() do { \
  624. if (do_confedit == CONF_AUTO) { \
  625. comment("# Automatically updated with -C"); \
  626. autowrote = 1; \
  627. } else \
  628. comment("#The correct line follows. (Not auto due to -c)"); \
  629. } while(0)
  630. if ((bits & CONF_COMMENT) && conf.uid != (signed) myuid) {
  631. conf_com();
  632. *stream << bd::String::printf(STR("%s! uid %d\n"), do_confedit == CONF_AUTO ? "" : "#", myuid);
  633. *stream << bd::String::printf(STR("%s! uid %d\n"), do_confedit == CONF_STATIC ? "" : "#", conf.uid);
  634. } else
  635. *stream << bd::String::printf(STR("! uid %d\n"), conf.uid);
  636. comment("");
  637. if (conf.username && my_username() && strcmp(conf.username, my_username())) {
  638. conf_com();
  639. *stream << bd::String::printf(STR("%s! username %s\n"), do_confedit == CONF_AUTO ? "" : "#", my_username());
  640. *stream << bd::String::printf(STR("%s! username %s\n"), do_confedit == CONF_STATIC ? "" : "#", conf.username);
  641. } else
  642. *stream << bd::String::printf(STR("! username %s\n"), conf.username ? conf.username : my_username() ? my_username() : "");
  643. if (conf.homedir && homedir(0) && strcmp(conf.homedir, homedir(0))) {
  644. conf_com();
  645. *stream << bd::String::printf(STR("%s! homedir %s\n"), do_confedit == CONF_AUTO ? "" : "#", homedir(0));
  646. *stream << bd::String::printf(STR("%s! homedir %s\n"), do_confedit == CONF_STATIC ? "" : "#", conf.homedir);
  647. } else
  648. *stream << bd::String::printf(STR("! homedir %s\n"), conf.homedir ? conf.homedir : homedir(0) ? homedir(0) : "");
  649. comment("");
  650. if (conf.datadir && strcmp(conf.datadir, "./...")) {
  651. comment("# datadir should be set to a static directory that is writable");
  652. if (homedir() && strstr(conf.datadir, homedir())) {
  653. p = replace(conf.datadir, homedir(), "~");
  654. *stream << bd::String::printf(STR("! datadir %s\n"), p);
  655. } else if (conf.homedir && strstr(conf.datadir, conf.homedir)) { /* Could be an older homedir */
  656. p = replace(conf.datadir, conf.homedir, "~");
  657. *stream << bd::String::printf(STR("! datadir %s\n"), p);
  658. } else
  659. *stream << bd::String::printf(STR("! datadir %s\n"), conf.datadir);
  660. comment("");
  661. }
  662. if (conf.portmin || conf.portmax) {
  663. comment("# portmin/max are for incoming connections (DCC) [0 for any] (These only make sense for HUBS)");
  664. *stream << bd::String::printf(STR("! portmin %d\n"), conf.portmin);
  665. *stream << bd::String::printf(STR("! portmax %d\n"), conf.portmax);
  666. comment("");
  667. }
  668. if (conf.autocron == 0) {
  669. comment("# Automatically add the bot to crontab?");
  670. *stream << bd::String::printf(STR("! autocron %d\n"), conf.autocron);
  671. comment("");
  672. }
  673. if (conf.hubs.length()) {
  674. comment("# Hubs this bot will connect to");
  675. // Sort hub list by hublevel
  676. bd::Array<bd::String> sortedhubs(conf.hubs);
  677. std::sort(sortedhubs.begin(), sortedhubs.end(), hubSort);
  678. for (size_t idx = 0; idx < sortedhubs.length(); ++idx) {
  679. bd::Array<bd::String> hubparams(static_cast<bd::String>(sortedhubs[idx]).split(' '));
  680. bd::String hubnick(hubparams[0]), address(hubparams[1]);
  681. in_port_t port = atoi(static_cast<bd::String>(hubparams[2]).c_str());
  682. *stream << bd::String::printf(STR("! hub %s %s %d\n"), hubnick.c_str(), address.c_str(), port);
  683. }
  684. comment("");
  685. }
  686. comment("# '|' means OR, [] means the enclosed is optional");
  687. comment("# A '+' in front of HOST means the HOST is ipv6");
  688. comment("# A '/' in front of BOT will disable that bot.");
  689. comment("#[/]BOT IP|* [+]HOST|* [IPV6-IP]");
  690. comment("#bot ip vhost");
  691. comment("#bot2 * vhost");
  692. comment("#bot3 ip");
  693. comment("#bot4 * +ipv6.vhost.com");
  694. comment("#bot5 * * ip:v6:ip:goes:here::");
  695. comment("### Hubs should have their own binary ###");
  696. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  697. *stream << bd::String::printf(STR("%s%s %s %s%s"),
  698. bot->disabled ? "/" : "", bot->nick,
  699. bot->net.ip ? bot->net.ip : "*", bot->net.host6 ? "+" : "",
  700. bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "*"));
  701. if (bot->net.ip6)
  702. *stream << bd::String::printf(STR(" %s"), bot->net.ip6 ? bot->net.ip6 : "");
  703. *stream << "\n";
  704. }
  705. if (fd != -1)
  706. stream->writeFile(fd);
  707. else
  708. stream->writeFile(filename);
  709. delete stream;
  710. return autowrote;
  711. }
  712. void
  713. conf_bot_dup(conf_bot *dest, conf_bot *src)
  714. {
  715. if (dest && src) {
  716. dest->nick = src->nick ? strdup(src->nick) : NULL;
  717. dest->pid_file = src->pid_file ? strdup(src->pid_file) : NULL;
  718. dest->net.ip = src->net.ip ? strdup(src->net.ip) : NULL;
  719. dest->net.host = src->net.host ? strdup(src->net.host) : NULL;
  720. dest->net.ip6 = src->net.ip6 ? strdup(src->net.ip6) : NULL;
  721. dest->net.host6 = src->net.host6 ? strdup(src->net.host6) : NULL;
  722. dest->net.v6 = src->net.v6;
  723. dest->u = src->u ? src->u : NULL;
  724. dest->pid = src->pid;
  725. dest->hub = src->hub;
  726. dest->localhub = src->localhub;
  727. dest->disabled = src->disabled;
  728. dest->next = NULL;
  729. }
  730. }
  731. conf_bot *conf_bots_dup(conf_bot *src)
  732. {
  733. conf_bot *ret = NULL;
  734. if (src) {
  735. conf_bot *bot = NULL, *newbot = NULL;
  736. for (bot = src; bot && bot->nick; bot = bot->next) {
  737. newbot = (conf_bot *) calloc(1, sizeof(conf_bot));
  738. conf_bot_dup(newbot, bot);
  739. list_append((struct list_type **) &(ret), (struct list_type *) newbot);
  740. }
  741. }
  742. return ret;
  743. }
  744. void deluser_removed_bots(conf_bot *oldlist, conf_bot *newlist)
  745. {
  746. if (oldlist) {
  747. conf_bot *botold = NULL, *botnew = NULL;
  748. bool found = 0;
  749. struct userrec *u = NULL;
  750. for (botold = oldlist; botold && botold->nick; botold = botold->next) {
  751. found = 0;
  752. for (botnew = newlist; botnew && botnew->nick; botnew = botnew->next) {
  753. if (!strcasecmp(botold->nick, botnew->nick)) {
  754. found = 1;
  755. break;
  756. }
  757. }
  758. if (!found && strcasecmp(botold->nick, origbotnick)) { /* Never kill ME.. will handle it elsewhere */
  759. /* No need to kill -- they are signalled and they will die on their own now */
  760. //botold->pid = checkpid(botold->nick, botold);
  761. //conf_killbot(conf.bots, NULL, botold, SIGKILL);
  762. if ((u = get_user_by_handle(userlist, botold->nick))) {
  763. putlog(LOG_MISC, "*", STR("Removing '%s' as it has been removed from the binary config."), botold->nick);
  764. if (server_online)
  765. check_this_user(botold->nick, 1, NULL);
  766. if (deluser(botold->nick)) {
  767. /* there is likely NO conf[]
  768. if (conf.bot->hub)
  769. write_userfile(-1);
  770. */
  771. }
  772. }
  773. }
  774. }
  775. }
  776. }
  777. void
  778. fill_conf_bot(bool fatal)
  779. {
  780. if (!conf.bots || !conf.bots->nick)
  781. return;
  782. char *mynick = NULL;
  783. conf_bot *me = NULL;
  784. /* This first clause should actually be obsolete */
  785. if (!used_B && conf.bots && conf.bots->nick) {
  786. mynick = strdup(conf.bots->nick);
  787. strlcpy(origbotnick, conf.bots->nick, sizeof(origbotnick));
  788. } else
  789. mynick = strldup(origbotnick, HANDLEN);
  790. sdprintf(STR("mynick: %s"), mynick);
  791. for (me = conf.bots; me && me->nick; me = me->next)
  792. if (!strcasecmp(me->nick, mynick))
  793. break;
  794. if (fatal && (!me || (me->nick && strcasecmp(me->nick, mynick))))
  795. werr(ERR_BADBOT);
  796. free(mynick);
  797. if (me) {
  798. if (!me->hub && me->localhub)
  799. sdprintf(STR("I am localhub!"));
  800. /* for future, we may just want to make this a pointer to ->bots if we do an emech style currentbot-> */
  801. conf.bot = (conf_bot *) calloc(1, sizeof(conf_bot));
  802. conf_bot_dup(conf.bot, me);
  803. }
  804. }
  805. void
  806. bin_to_conf(bool error)
  807. {
  808. /* printf("Converting binary data to conf struct\n"); */
  809. conf.features = atol(settings.features);
  810. conf.uid = atol(settings.uid);
  811. if (settings.username[0])
  812. str_redup(&conf.username, settings.username);
  813. str_redup(&conf.datadir, settings.datadir);
  814. if (settings.homedir[0])
  815. str_redup(&conf.homedir, settings.homedir);
  816. conf.portmin = atol(settings.portmin);
  817. conf.portmax = atol(settings.portmax);
  818. conf.autocron = atoi(settings.autocron);
  819. prep_homedir(error);
  820. expand_tilde(&conf.datadir);
  821. /* PARSE/ADD BOTS */
  822. {
  823. char *p = NULL, *tmp = NULL, *tmpp = NULL;
  824. tmp = tmpp = strdup(settings.bots);
  825. while ((p = strchr(tmp, ','))) {
  826. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  827. *p++ = 0;
  828. if (!tmp[0])
  829. break;
  830. nick = newsplit(&tmp);
  831. if (!nick || (nick && !nick[0]))
  832. werr(ERR_BADCONF);
  833. if (tmp[0])
  834. ip = newsplit(&tmp);
  835. if (tmp[0])
  836. host = newsplit(&tmp);
  837. if (tmp[0])
  838. ipsix = newsplit(&tmp);
  839. conf_addbot(nick, ip, host, ipsix);
  840. tmp = p++;
  841. }
  842. free(tmpp);
  843. }
  844. char datadir[PATH_MAX] = "";
  845. if (!realpath(conf.datadir, datadir)) {
  846. ;
  847. }
  848. str_redup(&conf.datadir, datadir);
  849. if (!mkdir_p(conf.datadir) && error)
  850. werr(ERR_DATADIR);
  851. str_redup(&conf.datadir, replace(datadir, dirname(binname), "."));
  852. if (Tempfile::FindDir() == ERROR)
  853. werr(ERR_TMPSTAT);
  854. if (clear_tmpdir)
  855. clear_tmp(); /* clear out the tmp dir, no matter if we are localhub or not */
  856. conf_checkpids(conf.bots);
  857. tellconf();
  858. }
  859. void conf_update_hubs(struct userrec* list) {
  860. if (!conf.bot->hub && !conf.bot->localhub) {
  861. return;
  862. }
  863. bd::Array<bd::String> hubUsers, oldhubs(conf.hubs);
  864. // Count how many hubs there are
  865. for (struct userrec *u = list; u; u = u->next) {
  866. if (bot_hublevel(u) < 999) {
  867. hubUsers << u->handle;
  868. }
  869. }
  870. conf.hubs.clear();
  871. conf.hubs.Reserve(hubUsers.length());
  872. for (size_t idx = 0; idx < hubUsers.length(); ++idx) {
  873. struct userrec *u = get_user_by_handle(list, const_cast<char*>(static_cast<bd::String>(hubUsers[idx]).c_str()));
  874. struct bot_addr *bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  875. conf.hubs << bd::String::printf("%s %s %d %d", u->handle, bi->address, bi->telnet_port, bi->hublevel);
  876. }
  877. // Only rewrite binary / notify bots if the hubs changed
  878. if (conf.hubs == oldhubs) {
  879. return;
  880. }
  881. if (conf.bot->hub || conf.bot->localhub) {
  882. /* rewrite our binary */
  883. conf_to_bin(&conf, 0, -1);
  884. /* Now signal all of the old bots with SIGUSR1
  885. * They will auto die and determine new localhub, etc..
  886. */
  887. conf_checkpids(conf.bots);
  888. conf_killbot(conf.bots, conf.bot->nick, NULL, SIGUSR1, 1); /* Don't kill me. */
  889. }
  890. }
  891. void conf_add_userlist_bots()
  892. {
  893. conf_bot *bot = NULL;
  894. struct userrec *u = NULL;
  895. struct bot_addr *bi = NULL;
  896. char tmp[81] = "", uhost[UHOSTLEN] = "";
  897. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  898. /* Don't auto-add hubs. */
  899. if (!bot->hub && tands > 0 && !bot->disabled) {
  900. u = get_user_by_handle(userlist, bot->nick);
  901. if (!u) {
  902. putlog(LOG_MISC, "*", STR("Adding bot '%s' as it has been added to the binary config."), bot->nick);
  903. userlist = adduser(userlist, bot->nick, "none", "-", USER_OP, 1);
  904. u = get_user_by_handle(userlist, bot->nick);
  905. simple_snprintf(tmp, sizeof(tmp), "%li %s", (long)now, conf.bot->nick);
  906. set_user(&USERENTRY_ADDED, u, tmp);
  907. bi = (struct bot_addr *) calloc(1, sizeof(struct bot_addr));
  908. bi->address = (char *) calloc(1, 1);
  909. bi->uplink = (char *) calloc(1, 1);
  910. bi->telnet_port = bi->relay_port = 3333;
  911. bi->hublevel = 999;
  912. set_user(&USERENTRY_BOTADDR, u, bi);
  913. }
  914. if (bot->net.ip && strcmp(bot->net.ip, "0.0.0.0")) {
  915. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.ip);
  916. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  917. addhost_by_handle(bot->nick, uhost);
  918. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.ip);
  919. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  920. addhost_by_handle(bot->nick, uhost);
  921. }
  922. if (bot->net.host) {
  923. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.host);
  924. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  925. addhost_by_handle(bot->nick, uhost);
  926. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.host);
  927. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  928. addhost_by_handle(bot->nick, uhost);
  929. }
  930. if (bot->net.host6) {
  931. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.host6);
  932. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  933. addhost_by_handle(bot->nick, uhost);
  934. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.host6);
  935. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  936. addhost_by_handle(bot->nick, uhost);
  937. }
  938. if (bot->net.ip6 && strcmp(bot->net.ip6, "::")) {
  939. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.ip6);
  940. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  941. addhost_by_handle(bot->nick, uhost);
  942. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.ip6);
  943. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  944. addhost_by_handle(bot->nick, uhost);
  945. }
  946. }
  947. }
  948. }
  949. conf_bot *conf_getlocalhub(conf_bot *bots) {
  950. if (!bots)
  951. return NULL;
  952. conf_bot *localhub = bots;
  953. if (localhub->disabled)
  954. while (localhub && localhub->disabled)
  955. localhub = localhub->next;
  956. if (!localhub) return NULL;
  957. return !localhub->disabled ? localhub : NULL;
  958. }
  959. void conf_setmypid(pid_t pid) {
  960. conf.bot->pid = pid;
  961. conf_bot *bot = conf.bots;
  962. if (conf.bots) {
  963. for (; bot && strcasecmp(bot->nick, conf.bot->nick); bot = bot->next)
  964. ;
  965. if (bot)
  966. bot->pid = pid;
  967. }
  968. }
  969. /* vim: set sts=2 sw=2 ts=8 et: */