conf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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 "misc.h"
  16. #include "users.h"
  17. #include "misc_file.h"
  18. #include "socket.h"
  19. #include <errno.h>
  20. #ifdef HAVE_PATHS_H
  21. # include <paths.h>
  22. #endif /* HAVE_PATHS_H */
  23. #include <sys/types.h>
  24. #include <sys/wait.h>
  25. #include <sys/stat.h>
  26. #include <signal.h>
  27. #ifdef CYGWIN_HACKS
  28. char cfile[DIRMAX] = "";
  29. #endif /* CYGWIN_HACKS */
  30. conf_t conf; /* global conf struct */
  31. static void
  32. tellconf()
  33. {
  34. conf_bot *bot;
  35. int i = 0;
  36. sdprintf("tempdir: %s\n", tempdir);
  37. sdprintf("uid: %d\n", conf.uid);
  38. sdprintf("uname: %s\n", conf.uname);
  39. sdprintf("homedir: %s\n", conf.homedir);
  40. sdprintf("binpath: %s\n", conf.binpath);
  41. sdprintf("binname: %s\n", conf.binname);
  42. sdprintf("portmin: %d\n", conf.portmin);
  43. sdprintf("portmax: %d\n", conf.portmax);
  44. sdprintf("pscloak: %d\n", conf.pscloak);
  45. sdprintf("autocron: %d\n", conf.autocron);
  46. sdprintf("autouname: %d\n", conf.autouname);
  47. sdprintf("watcher: %d\n", conf.watcher);
  48. sdprintf("bots:\n");
  49. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  50. i++;
  51. sdprintf("%d: %s IP: %s HOST: %s IP6: %s HOST6: %s v6: %d HUB: %d PID: %d\n", i,
  52. bot->nick,
  53. bot->net.ip ? bot->net.ip : "",
  54. bot->net.host ? bot->net.host : "", bot->net.ip6 ? bot->net.ip6 : "", bot->net.host6 ? bot->net.host6 : "",
  55. bot->net.v6,
  56. bot->hub,
  57. bot->pid);
  58. }
  59. if (conf.bot && ((bot = conf.bot))) {
  60. sdprintf("me:\n");
  61. sdprintf("%s IP: %s HOST: %s IP6: %s HOST6: %s v6: %d HUB: %d PID: %d\n",
  62. bot->nick,
  63. bot->net.ip ? bot->net.ip : "",
  64. bot->net.host ? bot->net.host : "", bot->net.ip6 ? bot->net.ip6 : "", bot->net.host6 ? bot->net.host6 : "",
  65. bot->net.v6,
  66. bot->hub,
  67. bot->pid);
  68. }
  69. }
  70. void spawnbot(const char *nick)
  71. {
  72. size_t size = strlen(nick) + strlen(binname) + 20;
  73. char *run = (char *) my_calloc(1, size);
  74. int status = 0;
  75. egg_snprintf(run, size, "%s -B %s", binname, nick);
  76. sdprintf("Spawning '%s': %s", nick, run);
  77. status = system(run);
  78. if (status == -1 || WEXITSTATUS(status))
  79. sdprintf("Failed to spawn '%s': %s", nick, strerror(errno));
  80. free(run);
  81. }
  82. /* spawn and kill bots accordingly
  83. * bots prefixxed with '/' will be killed auto if running.
  84. * if (updating) then we were called with -L and -P, we need to restart all running bots except our parent and localhub */
  85. void
  86. spawnbots()
  87. {
  88. conf_bot *bot = NULL;
  89. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  90. sdprintf("checking bot: %s", bot->nick);
  91. if (bot->nick[0] == '/') {
  92. /* kill it if running */
  93. if (bot->pid)
  94. kill(bot->pid, SIGKILL);
  95. else
  96. continue;
  97. /* if we're updating automatically, we were called with -L -P, and are only supposed to kill non-localhubs
  98. -if updating and we find our nick, skip
  99. -if pid exists and not updating, bot is running and we have nothing more to do, skip.
  100. */
  101. } else if ((!strcmp(bot->nick, conf.bot->nick) && updating == UPDATE_AUTO) || (bot->pid && !updating)) {
  102. sdprintf(" ... skipping. Updating: %d, pid: %d", updating, bot->pid);
  103. continue;
  104. } else {
  105. /* if we are updating with -L -P, then we need to restart ALL bots */
  106. if (updating == UPDATE_AUTO && bot->pid) {
  107. kill(bot->pid, SIGKILL);
  108. /* remove the pid incase we start the new bot before the old dies */
  109. unlink(bot->pid_file);
  110. }
  111. spawnbot(bot->nick);
  112. }
  113. }
  114. }
  115. int
  116. killbot(char *botnick, int signal)
  117. {
  118. conf_bot *bot = NULL;
  119. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  120. if (bot->nick[0] == '/')
  121. continue;
  122. else if (!egg_strcasecmp(botnick, bot->nick)) {
  123. if (bot->pid)
  124. return kill(bot->pid, signal);
  125. }
  126. }
  127. return -1;
  128. }
  129. #ifndef CYGWIN_HACKS
  130. static uid_t save_euid, save_egid;
  131. static int
  132. swap_uids()
  133. {
  134. save_euid = geteuid();
  135. save_egid = getegid();
  136. return (setegid(getgid()) || seteuid(getuid()))? -1 : 0;
  137. }
  138. static int
  139. swap_uids_back()
  140. {
  141. return (setegid(save_egid) || seteuid(save_euid)) ? -1 : 0;
  142. }
  143. void
  144. confedit()
  145. {
  146. Tempfile tmpconf = Tempfile("conf");
  147. char *editor = NULL;
  148. mode_t um;
  149. int waiter;
  150. pid_t pid, xpid, localhub_pid = 0;
  151. um = umask(077);
  152. writeconf(NULL, tmpconf.f, CONF_COMMENT);
  153. fclose(tmpconf.f);
  154. (void) umask(um);
  155. if (!can_stat(tmpconf.file))
  156. fatal("Cannot stat tempfile", 0);
  157. /* Okay, edit the file */
  158. if ((!((editor = getenv("VISUAL")) && strlen(editor)))
  159. && (!((editor = getenv("EDITOR")) && strlen(editor)))
  160. ) {
  161. editor = "vi";
  162. /*
  163. #if defined(DEBIAN)
  164. editor = "/usr/bin/editor";
  165. #elif defined(_PATH_VI)
  166. editor = _PATH_VI;
  167. #else
  168. editor = "/usr/ucb/vi";
  169. #endif
  170. */
  171. }
  172. signal(SIGINT, SIG_IGN);
  173. signal(SIGQUIT, SIG_IGN);
  174. signal(SIGCONT, SIG_DFL);
  175. swap_uids();
  176. switch (pid = fork()) {
  177. case -1:
  178. fatal("Cannot fork", 0);
  179. case 0:
  180. {
  181. char *run = NULL;
  182. size_t size = tmpconf.len + strlen(editor) + 5;
  183. setgid(getgid());
  184. setuid(getuid());
  185. run = (char *) my_calloc(1, size);
  186. /* child */
  187. egg_snprintf(run, size, "%s %s", editor, tmpconf.file);
  188. execlp("/bin/sh", "/bin/sh", "-c", run, NULL);
  189. perror(editor);
  190. exit(1);
  191. /*NOTREACHED*/}
  192. default:
  193. /* parent */
  194. break;
  195. }
  196. /* parent */
  197. while (1) {
  198. xpid = waitpid(pid, &waiter, WUNTRACED);
  199. if (xpid == -1) {
  200. fprintf(stderr, "waitpid() failed waiting for PID %d from \"%s\": %s\n", pid, editor, strerror(errno));
  201. } else if (xpid != pid) {
  202. fprintf(stderr, "wrong PID (%d != %d) from \"%s\"\n", xpid, pid, editor);
  203. goto fatal;
  204. } else if (WIFSTOPPED(waiter)) {
  205. /* raise(WSTOPSIG(waiter)); Not needed and breaks in job control shell */
  206. } else if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) {
  207. fprintf(stderr, "\"%s\" exited with status %d\n", editor, WEXITSTATUS(waiter));
  208. goto fatal;
  209. } else if (WIFSIGNALED(waiter)) {
  210. fprintf(stderr, "\"%s\" killed; signal %d (%score dumped)\n", editor, WTERMSIG(waiter),
  211. # ifdef CYGWIN_HACKS
  212. 0
  213. # else
  214. WCOREDUMP(waiter)
  215. # endif
  216. /* CYGWIN_HACKS */
  217. ? "" : "no ");
  218. goto fatal;
  219. } else {
  220. break;
  221. }
  222. }
  223. (void) signal(SIGINT, SIG_DFL);
  224. (void) signal(SIGQUIT, SIG_DFL);
  225. swap_uids_back();
  226. if (!can_stat(tmpconf.file))
  227. fatal("Error reading new config file", 0);
  228. if (conf.bots && conf.bots->pid)
  229. localhub_pid = conf.bots->pid;
  230. readconf((const char *) tmpconf.file, 0); /* read cleartext conf tmp into &settings */
  231. fix_tilde(&conf.binpath);
  232. unlink(tmpconf.file);
  233. conf_to_bin(&conf, 0, -1);
  234. if (localhub_pid)
  235. kill(localhub_pid, SIGUSR1);
  236. exit(0);
  237. fatal:
  238. unlink(tmpconf.file);
  239. exit(1);
  240. }
  241. #endif /* !CYGWIN_HACKS */
  242. void
  243. init_conf()
  244. {
  245. // conf.bots = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  246. // conf.bots->nick = NULL;
  247. // conf.bots->next = NULL;
  248. conf.bots = NULL;
  249. conf.bot = NULL;
  250. conf.watcher = 0;
  251. #ifdef CYGWIN_HACKS
  252. conf.autocron = 0;
  253. #else
  254. conf.autocron = 1;
  255. #endif /* !CYGWIN_HACKS */
  256. conf.autouname = 0;
  257. #ifdef CYGWIN_HACKS
  258. conf.binpath = strdup(homedir());
  259. #else /* !CYGWIN_HACKS */
  260. conf.binpath = strdup(dirname(binname));
  261. #endif /* CYGWIN_HACKS */
  262. char *p = strrchr(binname, '/');
  263. p++;
  264. conf.binname = strdup(p);
  265. conf.portmin = 0;
  266. conf.portmax = 0;
  267. conf.pscloak = 0;
  268. conf.uid = -1;
  269. conf.uname = NULL;
  270. conf.username = NULL;
  271. conf.homedir = NULL;
  272. }
  273. void conf_checkpids()
  274. {
  275. conf_bot *bot = NULL;
  276. for (bot = conf.bots; bot && bot->nick; bot = bot->next)
  277. bot->pid = checkpid(bot->nick, bot);
  278. }
  279. /*
  280. * Return the PID of a bot if it is running, otherwise return 0
  281. */
  282. pid_t
  283. checkpid(char *nick, conf_bot *bot)
  284. {
  285. FILE *f = NULL;
  286. char buf[DIRMAX] = "", s[11] = "", *tmpnick = NULL, *tmp_ptr = NULL;
  287. tmpnick = tmp_ptr = strdup(nick);
  288. if (tmpnick[0] == '/')
  289. tmpnick++;
  290. egg_snprintf(buf, sizeof buf, "%s.pid.%s", tempdir, tmpnick);
  291. free(tmp_ptr);
  292. if (bot && !(bot->pid_file))
  293. bot->pid_file = strdup(buf);
  294. else if (bot && strcmp(bot->pid_file, buf))
  295. str_redup(&bot->pid_file, buf);
  296. if ((f = fopen(buf, "r"))) {
  297. pid_t xx = 0;
  298. fgets(s, 10, f);
  299. remove_crlf(s);
  300. fclose(f);
  301. xx = atoi(s);
  302. // if (bot) {
  303. int x = 0;
  304. x = kill(xx, SIGCHLD);
  305. if (x == -1 && errno == ESRCH)
  306. return 0;
  307. else if (x == 0)
  308. return xx;
  309. // } else {
  310. // return xx ? xx : 0;
  311. // }
  312. }
  313. return 0;
  314. }
  315. void
  316. conf_addbot(char *nick, char *ip, char *host, char *ip6)
  317. {
  318. conf_bot *bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  319. bot->next = NULL;
  320. bot->pid_file = NULL;
  321. bot->nick = strdup(nick);
  322. bot->net.ip = NULL;
  323. bot->net.host = NULL;
  324. bot->net.ip6 = NULL;
  325. bot->net.host6 = NULL;
  326. if (host && host[0] == '+') {
  327. host++;
  328. bot->net.host6 = strdup(host);
  329. } else if (host && strcmp(host, ".")) {
  330. bot->net.host = strdup(host);
  331. }
  332. if (ip && strcmp(ip, ".") && is_dotted_ip(ip) == AF_INET)
  333. bot->net.ip = strdup(ip);
  334. if (ip6 && strcmp(ip6, ".") && is_dotted_ip(ip6) == AF_INET6)
  335. bot->net.ip6 = strdup(ip6);
  336. if (bot->net.ip6 || bot->net.host6)
  337. bot->net.v6 = 1;
  338. bot->u = NULL;
  339. // bot->pid = checkpid(nick, bot);
  340. if (settings.hubs) {
  341. char *p = settings.hubs, *p2 = NULL;
  342. size_t len = 0;
  343. while (p && *p) {
  344. if ((p2 = strchr(p, ' '))) {
  345. len = p2 - p;
  346. if (!egg_strncasecmp(p, bot->nick, len)) {
  347. bot->hub = 1;
  348. break;
  349. }
  350. }
  351. if ((p = strchr(p, ',')))
  352. p++;
  353. }
  354. }
  355. if (!bot->hub && !conf.bots) {
  356. bot->localhub = 1; /* first bot */
  357. conf.localhub = strdup(nick ? nick : origbotname);
  358. /* perhaps they did -B localhub-bot ? */
  359. if (origbotname[0] && !strcmp(origbotname, bot->nick))
  360. localhub = 1;
  361. }
  362. list_append((struct list_type **) &(conf.bots), (struct list_type *) bot);
  363. // list_append((struct list_type **) &(cache->cchan), (struct list_type *) cchan);
  364. }
  365. void
  366. free_bot(conf_bot *bot)
  367. {
  368. if (bot) {
  369. list_delete((struct list_type **) &(conf.bots), (struct list_type *) bot);
  370. free(bot->nick);
  371. free(bot->pid_file);
  372. if (bot->net.ip)
  373. free(bot->net.ip);
  374. if (bot->net.host)
  375. free(bot->net.host);
  376. if (bot->net.ip6)
  377. free(bot->net.ip6);
  378. if (bot->net.host6)
  379. free(bot->net.host6);
  380. free(bot);
  381. }
  382. }
  383. int
  384. conf_delbot(char *botn)
  385. {
  386. conf_bot *bot = NULL;
  387. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  388. if (!strcmp(bot->nick, botn)) { /* found it! */
  389. bot->pid = checkpid(bot->nick, bot);
  390. killbot(bot->nick, SIGKILL);
  391. free_bot(bot);
  392. return 0;
  393. }
  394. }
  395. return 1;
  396. }
  397. void
  398. free_conf()
  399. {
  400. free_conf_bots();
  401. free_bot(conf.bot);
  402. conf.bot = NULL;
  403. free(conf.localhub);
  404. free(conf.uname);
  405. free(conf.username);
  406. free(conf.homedir);
  407. free(conf.binname);
  408. free(conf.binpath);
  409. }
  410. void
  411. free_conf_bots(void)
  412. {
  413. conf_bot *bot = NULL, *bot_n = NULL;
  414. for (bot = conf.bots; bot; bot = bot_n) {
  415. bot_n = bot->next;
  416. free_bot(bot);
  417. }
  418. conf.bots = NULL;
  419. }
  420. int
  421. parseconf(bool error)
  422. {
  423. if (conf.username) {
  424. str_redup(&conf.username, my_username());
  425. } else {
  426. conf.username = strdup(my_username());
  427. }
  428. #ifndef CYGWIN_HACKS
  429. if (error && conf.uid != (signed) myuid) {
  430. sdprintf("wrong uid, conf: %d :: %d", conf.uid, myuid);
  431. werr(ERR_WRONGUID);
  432. } else if (!conf.uid)
  433. conf.uid = myuid;
  434. if (conf.uname && strcmp(conf.uname, my_uname()) && !conf.autouname) {
  435. baduname(conf.uname, my_uname()); /* its not auto, and its not RIGHT, bail out. */
  436. sdprintf("wrong uname, conf: %s :: %s", conf.uname, my_uname());
  437. if (error)
  438. werr(ERR_WRONGUNAME);
  439. } else if (conf.uname && conf.autouname) { /* if autouname, dont bother comparing, just set uname to output */
  440. str_redup(&conf.uname, my_uname());
  441. } else if (!conf.uname) { /* if not set, then just set it, wont happen again next time... */
  442. conf.uname = strdup(my_uname());
  443. }
  444. if (conf.homedir) {
  445. str_redup(&conf.homedir, homedir());
  446. } else {
  447. conf.homedir = strdup(homedir());
  448. }
  449. #endif /* !CYGWIN_HACKS */
  450. return 0;
  451. }
  452. int
  453. readconf(const char *fname, int bits)
  454. {
  455. FILE *f = NULL;
  456. int i = 0, enc = (bits & CONF_ENC) ? 1 : 0;
  457. char *inbuf = NULL;
  458. sdprintf("readconf(%s, %d)", fname, enc);
  459. Context;
  460. if (!(f = fopen(fname, "r")))
  461. fatal("Cannot read config", 0);
  462. free_conf_bots();
  463. inbuf = (char *) my_calloc(1, 201);
  464. while (fgets(inbuf, 201, f) != NULL) {
  465. char *line = NULL, *temp_ptr = NULL;
  466. remove_crlf(inbuf);
  467. if (enc)
  468. line = temp_ptr = decrypt_string(settings.salt1, inbuf);
  469. else
  470. line = inbuf;
  471. if ((line && !line[0]) || line[0] == '\n') {
  472. if (enc)
  473. free(line);
  474. continue;
  475. }
  476. i++;
  477. sdprintf("CONF LINE: %s", line);
  478. // !strchr("_`|}][{*/#-+!abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPWRSTUVWXYZ", line[0])) {
  479. if (enc && line[0] > '~') {
  480. sdprintf("line %d, char %c ", i, line[0]);
  481. fatal("Bad encryption", 0);
  482. } else { /* line is good to parse */
  483. /* - uid */
  484. if (line[0] == '-') {
  485. newsplit(&line);
  486. if (!conf.uid)
  487. conf.uid = atoi(line);
  488. /* + uname */
  489. } else if (line[0] == '+') {
  490. newsplit(&line);
  491. if (!conf.uname)
  492. conf.uname = strdup(line);
  493. /* ! is misc options */
  494. } else if (line[0] == '!') {
  495. char *option = NULL;
  496. newsplit(&line);
  497. if (line[0])
  498. option = newsplit(&line);
  499. if (!option)
  500. continue;
  501. if (!strcmp(option, "autocron")) { /* automatically check/create crontab? */
  502. if (egg_isdigit(line[0]))
  503. conf.autocron = atoi(line);
  504. } else if (!strcmp(option, "autouname")) { /* auto update uname contents? */
  505. if (egg_isdigit(line[0]))
  506. conf.autouname = atoi(line);
  507. } else if (!strcmp(option, "username")) { /* shell username */
  508. conf.username = strdup(line);
  509. } else if (!strcmp(option, "homedir")) { /* homedir */
  510. conf.homedir = strdup(line);
  511. } else if (!strcmp(option, "binpath")) { /* path that the binary should move to? */
  512. str_redup(&conf.binpath, line);
  513. } else if (!strcmp(option, "binname")) { /* filename of the binary? */
  514. str_redup(&conf.binname, line);
  515. } else if (!strcmp(option, "portmin")) {
  516. if (egg_isdigit(line[0]))
  517. conf.portmin = atoi(line);
  518. } else if (!strcmp(option, "portmax")) {
  519. if (egg_isdigit(line[0]))
  520. conf.portmax = atoi(line);
  521. } else if (!strcmp(option, "pscloak")) { /* should bots on this shell pscloak? */
  522. if (egg_isdigit(line[0]))
  523. conf.pscloak = atoi(line);
  524. } else if (!strcmp(option, "uid")) { /* new method uid */
  525. if (egg_isdigit(line[0]))
  526. conf.uid = atoi(line);
  527. } else if (!strcmp(option, "uname")) { /* new method uname */
  528. if (!conf.uname)
  529. conf.uname = strdup(line);
  530. else
  531. str_redup(&conf.uname, line);
  532. } else if (!strcmp(option, "watcher")) {
  533. if (egg_isdigit(line[0]))
  534. conf.watcher = atoi(line);
  535. } else {
  536. putlog(LOG_MISC, "*", "Unrecognized config option '%s'", option);
  537. }
  538. /* read in portmin */
  539. } else if (line[0] == '>') {
  540. newsplit(&line);
  541. conf.portmin = atoi(line);
  542. } else if (line[0] == '<') {
  543. newsplit(&line);
  544. conf.portmax = atoi(line);
  545. /* now to parse nick/hosts */
  546. } else if (line[0] != '#') {
  547. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  548. nick = newsplit(&line);
  549. if (!nick || (nick && !nick[0]))
  550. werr(ERR_BADCONF);
  551. if (line[0])
  552. ip = newsplit(&line);
  553. if (line[0])
  554. host = newsplit(&line);
  555. if (line[0])
  556. ipsix = newsplit(&line);
  557. conf_addbot(nick, ip, host, ipsix);
  558. }
  559. }
  560. inbuf[0] = 0;
  561. if (enc)
  562. free(temp_ptr);
  563. } /* while(fgets()) */
  564. fclose(f);
  565. free(inbuf);
  566. return 0;
  567. }
  568. int
  569. writeconf(char *filename, FILE * stream, int bits)
  570. {
  571. FILE *f = NULL;
  572. conf_bot *bot = NULL;
  573. int (*my_write) (FILE *, const char *, ... ) = NULL;
  574. char *p = NULL;
  575. if (bits & CONF_ENC)
  576. my_write = lfprintf;
  577. else if (!(bits & CONF_ENC))
  578. my_write = fprintf;
  579. #define comment(text) \
  580. if (bits & CONF_COMMENT) \
  581. my_write(f, "%s\n", text);
  582. if (stream) {
  583. f = stream;
  584. } else if (filename) {
  585. if (!(f = fopen(filename, "w")))
  586. return 1;
  587. }
  588. #ifndef CYGWIN_HACKS
  589. comment("# Lines beginning with # are what the preceeding line SHOULD be");
  590. comment("# They are simply comments and are not parsed at all.\n");
  591. my_write(f, "! uid %d\n", conf.uid);
  592. if ((bits & CONF_COMMENT) && conf.uid != (signed) myuid)
  593. my_write(f, "#! uid %d\n\n", myuid);
  594. if (!conf.uname || (conf.uname && conf.autouname && strcmp(conf.uname, my_uname()))) {
  595. comment("# Automatic");
  596. my_write(f, "! uname %s\n", my_uname());
  597. } else if (conf.uname && !conf.autouname && strcmp(conf.uname, my_uname())) {
  598. my_write(f, "! uname %s\n", conf.uname);
  599. comment("# autouname is OFF");
  600. my_write(f, "#! uname %s\n\n", my_uname());
  601. } else
  602. my_write(f, "! uname %s\n", conf.uname);
  603. comment("");
  604. my_write(f, "! username %s\n", conf.username ? conf.username : my_username());
  605. if (conf.username && strcmp(conf.username, my_username()))
  606. my_write(f, "#! username %s\n", my_username());
  607. my_write(f, "! homedir %s\n", conf.homedir ? conf.homedir : homedir());
  608. if (conf.homedir && strcmp(conf.homedir, homedir()))
  609. my_write(f, "#! homedir %s\n", homedir());
  610. comment("\n# binpath needs to be full path unless it begins with '~', which uses 'homedir', ie, '~/'");
  611. if (strstr(conf.binpath, homedir())) {
  612. p = replace(conf.binpath, homedir(), "~");
  613. my_write(f, "! binpath %s\n", p);
  614. } else
  615. my_write(f, "! binpath %s\n", conf.binpath);
  616. comment("# binname is relative to binpath, if you change this, you'll need to manually remove the old one from crontab.");
  617. my_write(f, "! binname %s\n", conf.binname);
  618. comment("");
  619. comment("# portmin/max are for incoming connections (DCC) [0 for any]");
  620. my_write(f, "! portmin %d\n", conf.portmin);
  621. my_write(f, "! portmax %d\n", conf.portmax);
  622. comment("");
  623. comment("# Attempt to \"cloak\" the process name in `ps` for Linux?");
  624. my_write(f, "! pscloak %d\n", conf.pscloak);
  625. comment("");
  626. comment("# Automatically add the bot to crontab?");
  627. my_write(f, "! autocron %d\n", conf.autocron);
  628. comment("");
  629. comment("# Automatically update 'uname' if it changes? (DANGEROUS)");
  630. my_write(f, "! autouname %d\n", conf.autouname);
  631. comment("");
  632. comment("# This will spawn a child process for EACH BOT that will block ALL process hijackers.");
  633. my_write(f, "! watcher %d\n", conf.watcher);
  634. comment("");
  635. comment("# '|' means OR, [] means the enclosed is optional");
  636. comment("# A '+' in front of HOST means the HOST is ipv6");
  637. comment("# A '/' in front of BOT will disable that bot.");
  638. comment("#[/]BOT IP|. [+]HOST|. [IPV6-IP]");
  639. #endif /* CYGWIN_HACKS */
  640. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  641. my_write(f, "%s %s %s%s %s\n", bot->nick,
  642. bot->net.ip ? bot->net.ip : ".", bot->net.host6 ? "+" : "",
  643. bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "."), bot->net.ip6 ? bot->net.ip6 : "");
  644. }
  645. fflush(f);
  646. if (!stream)
  647. fclose(f);
  648. return 0;
  649. }
  650. static void
  651. conf_bot_dup(conf_bot * dest, conf_bot * src)
  652. {
  653. dest->nick = src->nick ? strdup(src->nick) : NULL;
  654. dest->pid_file = src->pid_file ? strdup(src->pid_file) : NULL;
  655. dest->net.ip = src->net.ip ? strdup(src->net.ip) : NULL;
  656. dest->net.host = src->net.host ? strdup(src->net.host) : NULL;
  657. dest->net.ip6 = src->net.ip6 ? strdup(src->net.ip6) : NULL;
  658. dest->net.host6 = src->net.host6 ? strdup(src->net.host6) : NULL;
  659. dest->net.v6 = src->net.v6;
  660. dest->u = src->u ? src->u : NULL;
  661. dest->pid = src->pid;
  662. dest->hub = src->hub;
  663. dest->localhub = src->localhub;
  664. dest->next = NULL;
  665. }
  666. void
  667. fill_conf_bot()
  668. {
  669. conf_bot *me = NULL;
  670. char *mynick = NULL;
  671. if (!conf.bots || !conf.bots->nick)
  672. return;
  673. if (localhub && conf.bots && conf.bots->nick) {
  674. mynick = strdup(conf.bots->nick);
  675. strlcpy(origbotname, conf.bots->nick, NICKLEN + 1);
  676. } else
  677. mynick = strdup(origbotname);
  678. for (me = conf.bots; me && me->nick; me = me->next)
  679. if (!strcmp(me->nick, mynick))
  680. break;
  681. if (me->nick && me->nick[0] == '/')
  682. werr(ERR_BOTDISABLED);
  683. if (!me->nick || (me->nick && strcmp(me->nick, mynick)))
  684. werr(ERR_BADBOT);
  685. free(mynick);
  686. /* for future, we may just want to make this a pointer to ->bots if we do an emech style currentbot-> */
  687. conf.bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  688. conf_bot_dup(conf.bot, me);
  689. }
  690. void
  691. bin_to_conf(void)
  692. {
  693. /* printf("Converting binary data to conf struct\n"); */
  694. conf.uid = atol(settings.uid);
  695. conf.username = strdup(settings.username);
  696. conf.uname = strdup(settings.uname);
  697. conf.homedir = strdup(settings.homedir);
  698. conf.binpath = strdup(settings.binpath);
  699. fix_tilde(&conf.binpath);
  700. conf.binname = strdup(settings.binname);
  701. conf.portmin = atol(settings.portmin);
  702. conf.portmax = atol(settings.portmax);
  703. conf.autouname = atoi(settings.autouname);
  704. conf.autocron = atoi(settings.autocron);
  705. conf.watcher = atoi(settings.watcher);
  706. conf.pscloak = atoi(settings.pscloak);
  707. /* BOTS */
  708. {
  709. char *p = NULL, *tmp = NULL, *tmpp = NULL;
  710. tmp = tmpp = strdup(settings.bots);
  711. while ((p = strchr(tmp, ','))) {
  712. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  713. *p++ = 0;
  714. if (!tmp[0])
  715. break;
  716. nick = newsplit(&tmp);
  717. if (!nick || (nick && !nick[0]))
  718. werr(ERR_BADCONF);
  719. if (tmp[0])
  720. ip = newsplit(&tmp);
  721. if (tmp[0])
  722. host = newsplit(&tmp);
  723. if (tmp[0])
  724. ipsix = newsplit(&tmp);
  725. conf_addbot(nick, ip, host, ipsix);
  726. tmp = p++;
  727. }
  728. free(tmpp);
  729. }
  730. /* this is temporary until we make tmpdir customizable */
  731. if (conf.bots && conf.bots->nick && conf.bots->hub)
  732. egg_snprintf(tempdir, DIRMAX, "%s/tmp/", conf.binpath);
  733. else
  734. egg_snprintf(tempdir, DIRMAX, "%s/.ssh/.../", conf.homedir);
  735. #ifdef CYGWIN_HACKS
  736. egg_snprintf(tempdir, DIRMAX, "tmp/");
  737. #endif /* CYGWIN_HACKS */
  738. check_tempdir(); /* ensure we can access tmpdir if it changed */
  739. clear_tmp(); /* clear out the tmp dir, no matter if we are localhub or not */
  740. conf_checkpids();
  741. tellconf();
  742. }