conf.c 29 KB

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