conf.c 28 KB

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