conf.c 31 KB

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