conf.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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. /*
  320. * Return the PID of a bot if it is running, otherwise return 0
  321. */
  322. pid_t
  323. checkpid(const char *nick, conf_bot *bot)
  324. {
  325. FILE *f = NULL;
  326. char buf[DIRMAX] = "", *tmpnick = NULL, *tmp_ptr = NULL;
  327. pid_t pid = 0;
  328. tmpnick = tmp_ptr = strdup(nick);
  329. strtolower(tmpnick);
  330. simple_snprintf(buf, sizeof buf, STR("%s/.pid.%s"), conf.datadir, tmpnick);
  331. free(tmp_ptr);
  332. if (bot && !(bot->pid_file))
  333. bot->pid_file = strdup(buf);
  334. else if (bot && strcasecmp(bot->pid_file, buf))
  335. str_redup(&bot->pid_file, buf);
  336. if ((f = fopen(buf, "r"))) {
  337. char *bufp = NULL, *pids = NULL;
  338. if (!fgets(buf, sizeof(buf), f)) {
  339. fclose(f);
  340. return 0;
  341. }
  342. fclose(f);
  343. remove_crlf(buf);
  344. if (!buf[0])
  345. return 0;
  346. bufp = buf;
  347. pids = newsplit(&bufp);
  348. if (str_isdigit(pids)) {
  349. pid = atoi(pids);
  350. if (kill(pid, SIGCHLD)) //Problem killing, most likely it's just not running.
  351. pid = 0;
  352. }
  353. if (bufp[0] && pid && can_stat(bufp) && (getpid() == pid) &&
  354. !strncasecmp(nick, origbotnick, HANDLEN)) {
  355. socksfile = strdup(bufp);
  356. return 0;
  357. }
  358. }
  359. return pid;
  360. }
  361. void
  362. conf_addbot(const char *nick, const char *ip, const char *host, const char *ip6)
  363. {
  364. conf_bot *bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  365. bot->next = NULL;
  366. bot->pid_file = NULL;
  367. if (nick[0] == '/') {
  368. bot->disabled = 1;
  369. ++nick;
  370. sdprintf(STR("%s is disabled."), nick);
  371. }
  372. bot->nick = strldup(nick, HANDLEN);
  373. bot->net.ip = NULL;
  374. bot->net.host = NULL;
  375. bot->net.ip6 = NULL;
  376. bot->net.host6 = NULL;
  377. if (host && host[0] == '+') {
  378. ++host;
  379. bot->net.host6 = strdup(host);
  380. } else if (host && !strchr(".*", host[0])) {
  381. bot->net.host = strdup(host);
  382. }
  383. if (ip && !strchr(".*", ip[0])) {
  384. int aftype = is_dotted_ip(ip);
  385. if (aftype == AF_INET)
  386. bot->net.ip = strdup(ip);
  387. #ifdef USE_IPV6
  388. else if (aftype == AF_INET6)
  389. bot->net.ip6 = strdup(ip);
  390. #endif /* USE_IPV6 */
  391. else if (aftype == 0) { /* The idiot used a hostname in place of ip */
  392. if (bot->net.host)
  393. free(bot->net.host);
  394. bot->net.host = strdup(ip);
  395. }
  396. }
  397. #ifdef USE_IPV6
  398. if (ip6 && !strchr(".*", ip6[0]) && is_dotted_ip(ip6) == AF_INET6)
  399. bot->net.ip6 = strdup(ip6);
  400. if (bot->net.ip6 || bot->net.host6)
  401. bot->net.v6 = 1;
  402. #endif /* USE_IPV6 */
  403. if (userlist)
  404. bot->u = get_user_by_handle(userlist, (char*)nick);
  405. else
  406. bot->u = NULL;
  407. // bot->pid = checkpid(nick, bot);
  408. if (settings.hubs && is_hub(bot->nick))
  409. bot->hub = 1;
  410. /* not a hub
  411. AND
  412. * no bots added yet (first bot) yet, not disabled.
  413. OR
  414. * bots already listed but we dont have a localhub yet, so we're it!
  415. */
  416. if (!conf.localhub && !bot->hub && !bot->disabled) {
  417. bot->localhub = 1; /* first bot */
  418. conf.localhub = strdup(bot->nick);
  419. }
  420. //Add this bot to the userlist
  421. if (!bot->u)
  422. userlist = adduser(userlist, bot->nick, "none", "-", USER_OP, 1);
  423. list_append((struct list_type **) &(conf.bots), (struct list_type *) bot);
  424. }
  425. void
  426. free_bot(conf_bot *bot)
  427. {
  428. if (bot) {
  429. list_delete((struct list_type **) &(conf.bots), (struct list_type *) bot);
  430. free(bot->nick);
  431. free(bot->pid_file);
  432. if (bot->net.ip)
  433. free(bot->net.ip);
  434. if (bot->net.host)
  435. free(bot->net.host);
  436. if (bot->net.ip6)
  437. free(bot->net.ip6);
  438. if (bot->net.host6)
  439. free(bot->net.host6);
  440. free(bot);
  441. }
  442. }
  443. int
  444. conf_delbot(char *botn, bool kill)
  445. {
  446. conf_bot *bot = NULL;
  447. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  448. if (!strcasecmp(bot->nick, botn)) { /* found it! */
  449. if (kill) {
  450. bot->pid = checkpid(bot->nick, bot);
  451. conf_killbot(conf.bots, NULL, bot, SIGKILL);
  452. }
  453. free_bot(bot);
  454. return 0;
  455. }
  456. }
  457. return 1;
  458. }
  459. void
  460. free_conf()
  461. {
  462. free_conf_bots(conf.bots);
  463. free_bot(conf.bot);
  464. conf.bot = NULL;
  465. if (conf.localhub)
  466. free(conf.localhub);
  467. if (conf.username)
  468. free(conf.username);
  469. if (conf.datadir)
  470. free(conf.datadir);
  471. if (conf.homedir)
  472. free(conf.homedir);
  473. init_conf();
  474. }
  475. void
  476. free_conf_bots(conf_bot *list)
  477. {
  478. if (list) {
  479. conf_bot *bot = NULL, *bot_n = NULL;
  480. for (bot = list; bot; bot = bot_n) {
  481. bot_n = bot->next;
  482. free_bot(bot);
  483. }
  484. list = NULL;
  485. }
  486. }
  487. void prep_homedir(bool error)
  488. {
  489. if (!conf.homedir)
  490. str_redup(&conf.homedir, homedir());
  491. if (error && (!conf.homedir || !conf.homedir[0]))
  492. werr(ERR_NOHOMEDIR);
  493. }
  494. int
  495. parseconf(bool error)
  496. {
  497. if (error && conf.uid == -1 && !conf.homedir)
  498. werr(ERR_NOTINIT);
  499. if (!conf.username)
  500. str_redup(&conf.username, my_username());
  501. if (error && (!conf.username || !conf.username[0]))
  502. werr(ERR_NOUSERNAME);
  503. #ifndef CYGWIN_HACKS
  504. if (error && conf.uid != (signed) myuid) {
  505. sdprintf(STR("wrong uid, conf: %d :: %d"), conf.uid, myuid);
  506. werr(ERR_WRONGUID);
  507. } else if (!conf.uid)
  508. conf.uid = myuid;
  509. #endif /* !CYGWIN_HACKS */
  510. return 0;
  511. }
  512. int
  513. readconf(const char *fname, int bits)
  514. {
  515. int enc = (bits & CONF_ENC) ? 1 : 0;
  516. bd::Stream* stream;
  517. if (enc) {
  518. const char salt1[] = SALT1;
  519. stream = new EncryptedStream(salt1);
  520. } else
  521. stream = new bd::Stream;
  522. sdprintf(STR("readconf(%s, %d)"), fname, enc);
  523. if (stream->loadFile(fname)) {
  524. delete stream;
  525. fatal(STR("Cannot read config"), 0);
  526. }
  527. free_conf_bots(conf.bots);
  528. bd::String line, option;
  529. while (stream->tell() < stream->length()) {
  530. line = stream->getline().chomp().trim();
  531. // Skip blank lines
  532. if (!line)
  533. continue;
  534. sdprintf(STR("CONF LINE: %s"), line.c_str());
  535. // !strchr("_`|}][{*/#-+!abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPWRSTUVWXYZ", line[0])) {
  536. /* - uid */
  537. if (line[0] == '!') {
  538. ++line;
  539. line.trim();
  540. option.clear();
  541. if (line.length())
  542. option = newsplit(line);
  543. if (!option || !line)
  544. continue;
  545. // option.toLower();
  546. if (option == STR("autocron")) { /* automatically check/create crontab? */
  547. if (egg_isdigit(line[0]))
  548. conf.autocron = atoi(line.c_str());
  549. } else if (option == STR("username")) { /* shell username */
  550. str_redup(&conf.username, line.c_str());
  551. } else if (option == STR("homedir")) { /* homedir */
  552. str_redup(&conf.homedir, line.c_str());
  553. } else if (option == STR("datadir")) { /* datadir */
  554. str_redup(&conf.datadir, line.c_str());
  555. } else if (option == STR("portmin")) {
  556. if (egg_isdigit(line[0]))
  557. conf.portmin = atoi(line.c_str());
  558. } else if (option == STR("portmax")) {
  559. if (egg_isdigit(line[0]))
  560. conf.portmax = atoi(line.c_str());
  561. } else if (option == STR("uid")) { /* new method uid */
  562. if (str_isdigit(line.c_str()))
  563. conf.uid = atoi(line.c_str());
  564. } else {
  565. putlog(LOG_MISC, "*", STR("Unrecognized config option '%s'"), option.c_str());
  566. }
  567. /* read in portmin */
  568. } else if (line[0] == '>') {
  569. conf.portmin = atoi(newsplit(line).c_str());
  570. } else if (line[0] == '<') {
  571. conf.portmax = atoi(newsplit(line).c_str());
  572. /* now to parse nick/hosts */
  573. } else if (line[0] != '#') {
  574. bd::String nick, host, ip, ipsix;
  575. nick = newsplit(line);
  576. if (!nick)
  577. werr(ERR_BADCONF);
  578. ip = newsplit(line);
  579. host = newsplit(line);
  580. ipsix = newsplit(line);
  581. conf_addbot(nick.c_str(), ip.c_str(), host.c_str(), ipsix.c_str());
  582. }
  583. } /* while(fgets()) */
  584. delete stream;
  585. return 0;
  586. }
  587. char s1_9[3] = "",s1_5[3] = "",s1_1[3] = "";
  588. int
  589. writeconf(char *filename, int fd, int bits)
  590. {
  591. conf_bot *bot = NULL;
  592. int autowrote = 0;
  593. bd::Stream* stream;
  594. bd::String buf;
  595. if (bits & CONF_ENC) {
  596. const char salt1[] = SALT1;
  597. stream = new EncryptedStream(salt1);
  598. } else
  599. stream = new bd::Stream;
  600. #define comment(text) do { \
  601. if (bits & CONF_COMMENT) \
  602. *stream << buf.printf(STR("%s\n"), text); \
  603. } while(0)
  604. #ifndef CYGWIN_HACKS
  605. char *p = NULL;
  606. comment("");
  607. #define conf_com() do { \
  608. if (do_confedit == CONF_AUTO) { \
  609. comment("# Automatically updated with -C"); \
  610. autowrote = 1; \
  611. } else \
  612. comment("#The correct line follows. (Not auto due to -c)"); \
  613. } while(0)
  614. if ((bits & CONF_COMMENT) && conf.uid != (signed) myuid) {
  615. conf_com();
  616. *stream << buf.printf(STR("%s! uid %d\n"), do_confedit == CONF_AUTO ? "" : "#", myuid);
  617. *stream << buf.printf(STR("%s! uid %d\n"), do_confedit == CONF_STATIC ? "" : "#", conf.uid);
  618. } else
  619. *stream << buf.printf(STR("! uid %d\n"), conf.uid);
  620. comment("");
  621. if (conf.username && my_username() && strcmp(conf.username, my_username())) {
  622. conf_com();
  623. *stream << buf.printf(STR("%s! username %s\n"), do_confedit == CONF_AUTO ? "" : "#", my_username());
  624. *stream << buf.printf(STR("%s! username %s\n"), do_confedit == CONF_STATIC ? "" : "#", conf.username);
  625. } else
  626. *stream << buf.printf(STR("! username %s\n"), conf.username ? conf.username : my_username() ? my_username() : "");
  627. if (conf.homedir && homedir(0) && strcmp(conf.homedir, homedir(0))) {
  628. conf_com();
  629. *stream << buf.printf(STR("%s! homedir %s\n"), do_confedit == CONF_AUTO ? "" : "#", homedir(0));
  630. *stream << buf.printf(STR("%s! homedir %s\n"), do_confedit == CONF_STATIC ? "" : "#", conf.homedir);
  631. } else
  632. *stream << buf.printf(STR("! homedir %s\n"), conf.homedir ? conf.homedir : homedir(0) ? homedir(0) : "");
  633. comment("");
  634. if (conf.datadir && strcmp(conf.datadir, "./...")) {
  635. comment("# datadir should be set to a static directory that is writable");
  636. if (homedir() && strstr(conf.datadir, homedir())) {
  637. p = replace(conf.datadir, homedir(), "~");
  638. *stream << buf.printf(STR("! datadir %s\n"), p);
  639. } else if (conf.homedir && strstr(conf.datadir, conf.homedir)) { /* Could be an older homedir */
  640. p = replace(conf.datadir, conf.homedir, "~");
  641. *stream << buf.printf(STR("! datadir %s\n"), p);
  642. } else
  643. *stream << buf.printf(STR("! datadir %s\n"), conf.datadir);
  644. comment("");
  645. }
  646. if (conf.portmin || conf.portmax) {
  647. comment("# portmin/max are for incoming connections (DCC) [0 for any] (These only make sense for HUBS)");
  648. *stream << buf.printf(STR("! portmin %d\n"), conf.portmin);
  649. *stream << buf.printf(STR("! portmax %d\n"), conf.portmax);
  650. comment("");
  651. }
  652. if (conf.autocron == 0) {
  653. comment("# Automatically add the bot to crontab?");
  654. *stream << buf.printf(STR("! autocron %d\n"), conf.autocron);
  655. comment("");
  656. }
  657. comment("# '|' means OR, [] means the enclosed is optional");
  658. comment("# A '+' in front of HOST means the HOST is ipv6");
  659. comment("# A '/' in front of BOT will disable that bot.");
  660. comment("#[/]BOT IP|* [+]HOST|* [IPV6-IP]");
  661. comment("#bot ip vhost");
  662. comment("#bot2 * vhost");
  663. comment("#bot3 ip");
  664. comment("#bot4 * +ipv6.vhost.com");
  665. comment("#bot5 * * ip:v6:ip:goes:here::");
  666. comment("### Hubs should have their own binary ###");
  667. #endif /* CYGWIN_HACKS */
  668. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  669. *stream << buf.printf(STR("%s%s %s %s%s %s\n"),
  670. bot->disabled ? "/" : "", bot->nick,
  671. bot->net.ip ? bot->net.ip : "*", bot->net.host6 ? "+" : "",
  672. bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "*"), bot->net.ip6 ? bot->net.ip6 : "");
  673. }
  674. if (fd != -1)
  675. stream->writeFile(fd);
  676. else
  677. stream->writeFile(filename);
  678. delete stream;
  679. return autowrote;
  680. }
  681. void
  682. conf_bot_dup(conf_bot *dest, conf_bot *src)
  683. {
  684. if (dest && src) {
  685. dest->nick = src->nick ? strdup(src->nick) : NULL;
  686. dest->pid_file = src->pid_file ? strdup(src->pid_file) : NULL;
  687. dest->net.ip = src->net.ip ? strdup(src->net.ip) : NULL;
  688. dest->net.host = src->net.host ? strdup(src->net.host) : NULL;
  689. dest->net.ip6 = src->net.ip6 ? strdup(src->net.ip6) : NULL;
  690. dest->net.host6 = src->net.host6 ? strdup(src->net.host6) : NULL;
  691. dest->net.v6 = src->net.v6;
  692. dest->u = src->u ? src->u : NULL;
  693. dest->pid = src->pid;
  694. dest->hub = src->hub;
  695. dest->localhub = src->localhub;
  696. dest->disabled = src->disabled;
  697. dest->next = NULL;
  698. }
  699. }
  700. conf_bot *conf_bots_dup(conf_bot *src)
  701. {
  702. conf_bot *ret = NULL;
  703. if (src) {
  704. conf_bot *bot = NULL, *newbot = NULL;
  705. for (bot = src; bot && bot->nick; bot = bot->next) {
  706. newbot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  707. conf_bot_dup(newbot, bot);
  708. list_append((struct list_type **) &(ret), (struct list_type *) newbot);
  709. }
  710. }
  711. return ret;
  712. }
  713. void deluser_removed_bots(conf_bot *oldlist, conf_bot *newlist)
  714. {
  715. if (oldlist) {
  716. conf_bot *botold = NULL, *botnew = NULL;
  717. bool found = 0;
  718. struct userrec *u = NULL;
  719. for (botold = oldlist; botold && botold->nick; botold = botold->next) {
  720. found = 0;
  721. for (botnew = newlist; botnew && botnew->nick; botnew = botnew->next) {
  722. if (!strcasecmp(botold->nick, botnew->nick)) {
  723. found = 1;
  724. break;
  725. }
  726. }
  727. if (!found && strcasecmp(botold->nick, origbotnick)) { /* Never kill ME.. will handle it elsewhere */
  728. /* No need to kill -- they are signalled and they will die on their own now */
  729. //botold->pid = checkpid(botold->nick, botold);
  730. //conf_killbot(conf.bots, NULL, botold, SIGKILL);
  731. if ((u = get_user_by_handle(userlist, botold->nick))) {
  732. putlog(LOG_MISC, "*", STR("Removing '%s' as it has been removed from the binary config."), botold->nick);
  733. if (server_online)
  734. check_this_user(botold->nick, 1, NULL);
  735. if (deluser(botold->nick)) {
  736. /* there is likely NO conf[]
  737. if (conf.bot->hub)
  738. write_userfile(-1);
  739. */
  740. }
  741. }
  742. }
  743. }
  744. }
  745. }
  746. void
  747. fill_conf_bot(bool fatal)
  748. {
  749. if (!conf.bots || !conf.bots->nick)
  750. return;
  751. char *mynick = NULL;
  752. conf_bot *me = NULL;
  753. /* This first clause should actually be obsolete */
  754. if (!used_B && conf.bots && conf.bots->nick) {
  755. mynick = strdup(conf.bots->nick);
  756. strlcpy(origbotnick, conf.bots->nick, HANDLEN + 1);
  757. } else
  758. mynick = strldup(origbotnick, HANDLEN);
  759. sdprintf(STR("mynick: %s"), mynick);
  760. for (me = conf.bots; me && me->nick; me = me->next)
  761. if (!strcasecmp(me->nick, mynick))
  762. break;
  763. if (fatal && (!me || (me->nick && strcasecmp(me->nick, mynick))))
  764. werr(ERR_BADBOT);
  765. free(mynick);
  766. if (me) {
  767. if (!me->hub && me->localhub)
  768. sdprintf(STR("I am localhub!"));
  769. /* for future, we may just want to make this a pointer to ->bots if we do an emech style currentbot-> */
  770. conf.bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  771. conf_bot_dup(conf.bot, me);
  772. }
  773. }
  774. void
  775. bin_to_conf(bool error)
  776. {
  777. /* printf("Converting binary data to conf struct\n"); */
  778. conf.features = atol(settings.features);
  779. conf.uid = atol(settings.uid);
  780. if (settings.username[0])
  781. str_redup(&conf.username, settings.username);
  782. str_redup(&conf.datadir, settings.datadir);
  783. if (settings.homedir[0])
  784. str_redup(&conf.homedir, settings.homedir);
  785. conf.portmin = atol(settings.portmin);
  786. conf.portmax = atol(settings.portmax);
  787. conf.autocron = atoi(settings.autocron);
  788. prep_homedir(error);
  789. expand_tilde(&conf.datadir);
  790. /* PARSE/ADD BOTS */
  791. {
  792. char *p = NULL, *tmp = NULL, *tmpp = NULL;
  793. tmp = tmpp = strdup(settings.bots);
  794. while ((p = strchr(tmp, ','))) {
  795. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  796. *p++ = 0;
  797. if (!tmp[0])
  798. break;
  799. nick = newsplit(&tmp);
  800. if (!nick || (nick && !nick[0]))
  801. werr(ERR_BADCONF);
  802. if (tmp[0])
  803. ip = newsplit(&tmp);
  804. if (tmp[0])
  805. host = newsplit(&tmp);
  806. if (tmp[0])
  807. ipsix = newsplit(&tmp);
  808. conf_addbot(nick, ip, host, ipsix);
  809. tmp = p++;
  810. }
  811. free(tmpp);
  812. }
  813. char datadir[PATH_MAX] = "";
  814. if (!realpath(conf.datadir, datadir)) {
  815. ;
  816. }
  817. str_redup(&conf.datadir, datadir);
  818. if (!mkdir_p(conf.datadir) && error)
  819. werr(ERR_DATADIR);
  820. str_redup(&conf.datadir, replace(datadir, dirname(binname), "."));
  821. if (Tempfile::FindDir() == ERROR)
  822. werr(ERR_TMPSTAT);
  823. if (clear_tmpdir)
  824. clear_tmp(); /* clear out the tmp dir, no matter if we are localhub or not */
  825. conf_checkpids(conf.bots);
  826. tellconf();
  827. }
  828. void conf_add_userlist_bots()
  829. {
  830. conf_bot *bot = NULL;
  831. struct userrec *u = NULL;
  832. struct bot_addr *bi = NULL;
  833. char tmp[81] = "", uhost[UHOSTLEN] = "";
  834. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  835. /* Don't auto-add hubs. */
  836. if (!bot->hub && tands > 0 && !bot->disabled) {
  837. u = get_user_by_handle(userlist, bot->nick);
  838. if (!u) {
  839. putlog(LOG_MISC, "*", STR("Adding bot '%s' as it has been added to the binary config."), bot->nick);
  840. userlist = adduser(userlist, bot->nick, "none", "-", USER_OP, 1);
  841. u = get_user_by_handle(userlist, bot->nick);
  842. simple_snprintf(tmp, sizeof(tmp), "%li %s", (long)now, conf.bot->nick);
  843. set_user(&USERENTRY_ADDED, u, tmp);
  844. bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  845. bi->address = (char *) my_calloc(1, 1);
  846. bi->uplink = (char *) my_calloc(1, 1);
  847. bi->telnet_port = bi->relay_port = 3333;
  848. bi->hublevel = 999;
  849. set_user(&USERENTRY_BOTADDR, u, bi);
  850. }
  851. if (bot->net.ip) {
  852. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.ip);
  853. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  854. addhost_by_handle(bot->nick, uhost);
  855. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.ip);
  856. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  857. addhost_by_handle(bot->nick, uhost);
  858. }
  859. if (bot->net.host) {
  860. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.host);
  861. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  862. addhost_by_handle(bot->nick, uhost);
  863. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.host);
  864. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  865. addhost_by_handle(bot->nick, uhost);
  866. }
  867. if (bot->net.host6) {
  868. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.host6);
  869. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  870. addhost_by_handle(bot->nick, uhost);
  871. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.host6);
  872. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  873. addhost_by_handle(bot->nick, uhost);
  874. }
  875. if (bot->net.ip6) {
  876. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.ip6);
  877. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  878. addhost_by_handle(bot->nick, uhost);
  879. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.ip6);
  880. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  881. addhost_by_handle(bot->nick, uhost);
  882. }
  883. }
  884. }
  885. }
  886. conf_bot *conf_getlocalhub(conf_bot *bots) {
  887. if (!bots)
  888. return NULL;
  889. conf_bot *localhub = bots;
  890. if (localhub->disabled)
  891. while (localhub && localhub->disabled)
  892. localhub = localhub->next;
  893. if (!localhub) return NULL;
  894. return !localhub->disabled ? localhub : NULL;
  895. }
  896. void conf_setmypid(pid_t pid) {
  897. conf.bot->pid = pid;
  898. conf_bot *bot = conf.bots;
  899. if (conf.bots) {
  900. for (; bot && strcasecmp(bot->nick, conf.bot->nick); bot = bot->next)
  901. ;
  902. if (bot)
  903. bot->pid = pid;
  904. }
  905. }