conf.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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;
  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. readconf((const char *) tmpconf.file, 0); /* read cleartext conf tmp into &settings */
  229. fix_tilde(&conf.binpath);
  230. unlink(tmpconf.file);
  231. conf_to_bin(&conf, 0, 1, 1); /* will exit */
  232. exit(0); /* never reached */
  233. fatal:
  234. unlink(tmpconf.file);
  235. exit(1);
  236. }
  237. #endif /* !CYGWIN_HACKS */
  238. void
  239. init_conf()
  240. {
  241. // conf.bots = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  242. // conf.bots->nick = NULL;
  243. // conf.bots->next = NULL;
  244. conf.bots = NULL;
  245. conf.bot = NULL;
  246. conf.watcher = 0;
  247. #ifdef CYGWIN_HACKS
  248. conf.autocron = 0;
  249. #else
  250. conf.autocron = 1;
  251. #endif /* !CYGWIN_HACKS */
  252. conf.autouname = 0;
  253. #ifdef CYGWIN_HACKS
  254. conf.binpath = strdup(homedir());
  255. #else /* !CYGWIN_HACKS */
  256. conf.binpath = strdup(dirname(binname));
  257. #endif /* CYGWIN_HACKS */
  258. char *p = strrchr(binname, '/');
  259. p++;
  260. conf.binname = strdup(p);
  261. conf.portmin = 0;
  262. conf.portmax = 0;
  263. conf.pscloak = 0;
  264. conf.uid = 0;
  265. conf.uname = NULL;
  266. conf.username = NULL;
  267. conf.homedir = NULL;
  268. }
  269. /*
  270. * Return the PID of a bot if it is running, otherwise return 0
  271. */
  272. pid_t
  273. checkpid(char *nick, conf_bot *bot)
  274. {
  275. FILE *f = NULL;
  276. char buf[DIRMAX] = "", s[11] = "", *tmpnick = NULL, *tmp_ptr = NULL;
  277. tmpnick = tmp_ptr = strdup(nick);
  278. if (tmpnick[0] == '/')
  279. tmpnick++;
  280. egg_snprintf(buf, sizeof buf, "%s.pid.%s", tempdir, tmpnick);
  281. free(tmp_ptr);
  282. if (bot && !(bot->pid_file))
  283. bot->pid_file = strdup(buf);
  284. else if (bot && strcmp(bot->pid_file, buf))
  285. str_redup(&bot->pid_file, buf);
  286. if ((f = fopen(buf, "r"))) {
  287. pid_t xx = 0;
  288. fgets(s, 10, f);
  289. remove_crlf(s);
  290. fclose(f);
  291. xx = atoi(s);
  292. // if (bot) {
  293. int x = 0;
  294. x = kill(xx, SIGCHLD);
  295. if (x == -1 && errno == ESRCH)
  296. return 0;
  297. else if (x == 0)
  298. return xx;
  299. // } else {
  300. // return xx ? xx : 0;
  301. // }
  302. }
  303. return 0;
  304. }
  305. void
  306. conf_addbot(char *nick, char *ip, char *host, char *ip6)
  307. {
  308. conf_bot *bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  309. bot->next = NULL;
  310. bot->pid_file = NULL;
  311. bot->nick = strdup(nick);
  312. bot->net.ip = NULL;
  313. bot->net.host = NULL;
  314. bot->net.ip6 = NULL;
  315. bot->net.host6 = NULL;
  316. if (host && host[0] == '+') {
  317. host++;
  318. bot->net.host6 = strdup(host);
  319. } else if (host && strcmp(host, ".")) {
  320. bot->net.host = strdup(host);
  321. }
  322. if (ip && strcmp(ip, ".") && is_dotted_ip(ip) == AF_INET)
  323. bot->net.ip = strdup(ip);
  324. if (ip6 && strcmp(ip6, ".") && is_dotted_ip(ip6) == AF_INET6)
  325. bot->net.ip6 = strdup(ip6);
  326. if (bot->net.ip6 || bot->net.host6)
  327. bot->net.v6 = 1;
  328. bot->u = NULL;
  329. bot->pid = checkpid(nick, bot);
  330. if (settings.hubs) {
  331. char *p = settings.hubs, *p2 = NULL;
  332. size_t len = 0;
  333. while (p && *p) {
  334. if ((p2 = strchr(p, ' '))) {
  335. len = p2 - p;
  336. if (!egg_strncasecmp(p, bot->nick, len)) {
  337. bot->hub = 1;
  338. break;
  339. }
  340. }
  341. if ((p = strchr(p, ',')))
  342. p++;
  343. }
  344. }
  345. if (!bot->hub && !conf.bots) {
  346. bot->localhub = 1; /* first bot */
  347. conf.localhub = strdup(nick ? nick : origbotname);
  348. /* perhaps they did -B localhub-bot ? */
  349. if (origbotname[0] && !strcmp(origbotname, bot->nick))
  350. localhub = 1;
  351. }
  352. list_append((struct list_type **) &(conf.bots), (struct list_type *) bot);
  353. // list_append((struct list_type **) &(cache->cchan), (struct list_type *) cchan);
  354. }
  355. void
  356. free_bot(conf_bot *bot)
  357. {
  358. if (bot) {
  359. list_delete((struct list_type **) &(conf.bots), (struct list_type *) bot);
  360. free(bot->nick);
  361. free(bot->pid_file);
  362. if (bot->net.ip)
  363. free(bot->net.ip);
  364. if (bot->net.host)
  365. free(bot->net.host);
  366. if (bot->net.ip6)
  367. free(bot->net.ip6);
  368. if (bot->net.host6)
  369. free(bot->net.host6);
  370. free(bot);
  371. }
  372. }
  373. int
  374. conf_delbot(char *botn)
  375. {
  376. conf_bot *bot = NULL;
  377. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  378. if (!strcmp(bot->nick, botn)) { /* found it! */
  379. bot->pid = checkpid(bot->nick, bot);
  380. killbot(bot->nick, SIGKILL);
  381. free_bot(bot);
  382. return 0;
  383. }
  384. }
  385. return 1;
  386. }
  387. void
  388. free_conf()
  389. {
  390. free_conf_bots();
  391. free_bot(conf.bot);
  392. conf.bot = NULL;
  393. free(conf.localhub);
  394. free(conf.uname);
  395. free(conf.username);
  396. free(conf.homedir);
  397. free(conf.binname);
  398. free(conf.binpath);
  399. }
  400. void
  401. free_conf_bots(void)
  402. {
  403. conf_bot *bot = NULL, *bot_n = NULL;
  404. for (bot = conf.bots; bot; bot = bot_n) {
  405. bot_n = bot->next;
  406. free_bot(bot);
  407. }
  408. conf.bots = NULL;
  409. }
  410. int
  411. parseconf(bool error)
  412. {
  413. if (conf.username) {
  414. str_redup(&conf.username, my_username());
  415. } else {
  416. conf.username = strdup(my_username());
  417. }
  418. #ifndef CYGWIN_HACKS
  419. if (error && conf.uid && conf.uid != myuid) {
  420. sdprintf("wrong uid, conf: %d :: %d", conf.uid, myuid);
  421. werr(ERR_WRONGUID);
  422. } else if (!conf.uid)
  423. conf.uid = myuid;
  424. if (conf.uname && strcmp(conf.uname, my_uname()) && !conf.autouname) {
  425. baduname(conf.uname, my_uname()); /* its not auto, and its not RIGHT, bail out. */
  426. sdprintf("wrong uname, conf: %s :: %s", conf.uname, my_uname());
  427. if (error)
  428. werr(ERR_WRONGUNAME);
  429. } else if (conf.uname && conf.autouname) { /* if autouname, dont bother comparing, just set uname to output */
  430. str_redup(&conf.uname, my_uname());
  431. } else if (!conf.uname) { /* if not set, then just set it, wont happen again next time... */
  432. conf.uname = strdup(my_uname());
  433. }
  434. if (conf.homedir) {
  435. str_redup(&conf.homedir, homedir());
  436. } else {
  437. conf.homedir = strdup(homedir());
  438. }
  439. #endif /* !CYGWIN_HACKS */
  440. return 0;
  441. }
  442. int
  443. readconf(const char *fname, int bits)
  444. {
  445. FILE *f = NULL;
  446. int i = 0, enc = (bits & CONF_ENC) ? 1 : 0;
  447. char *inbuf = NULL;
  448. sdprintf("readconf(%s, %d)", fname, enc);
  449. Context;
  450. if (!(f = fopen(fname, "r")))
  451. fatal("Cannot read config", 0);
  452. free_conf_bots();
  453. inbuf = (char *) my_calloc(1, 201);
  454. while (fgets(inbuf, 201, f) != NULL) {
  455. char *line = NULL, *temp_ptr = NULL;
  456. remove_crlf(inbuf);
  457. if (enc)
  458. line = temp_ptr = decrypt_string(settings.salt1, inbuf);
  459. else
  460. line = inbuf;
  461. if ((line && !line[0]) || line[0] == '\n') {
  462. if (enc)
  463. free(line);
  464. continue;
  465. }
  466. i++;
  467. sdprintf("CONF LINE: %s", line);
  468. // !strchr("_`|}][{*/#-+!abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPWRSTUVWXYZ", line[0])) {
  469. if (enc && line[0] > '~') {
  470. sdprintf("line %d, char %c ", i, line[0]);
  471. fatal("Bad encryption", 0);
  472. } else { /* line is good to parse */
  473. /* - uid */
  474. if (line[0] == '-') {
  475. newsplit(&line);
  476. if (!conf.uid)
  477. conf.uid = atoi(line);
  478. /* + uname */
  479. } else if (line[0] == '+') {
  480. newsplit(&line);
  481. if (!conf.uname)
  482. conf.uname = strdup(line);
  483. /* ! is misc options */
  484. } else if (line[0] == '!') {
  485. char *option = NULL;
  486. newsplit(&line);
  487. if (line[0])
  488. option = newsplit(&line);
  489. if (!option)
  490. continue;
  491. if (!strcmp(option, "autocron")) { /* automatically check/create crontab? */
  492. if (egg_isdigit(line[0]))
  493. conf.autocron = atoi(line);
  494. } else if (!strcmp(option, "autouname")) { /* auto update uname contents? */
  495. if (egg_isdigit(line[0]))
  496. conf.autouname = atoi(line);
  497. } else if (!strcmp(option, "username")) { /* shell username */
  498. conf.username = strdup(line);
  499. } else if (!strcmp(option, "homedir")) { /* homedir */
  500. conf.homedir = strdup(line);
  501. } else if (!strcmp(option, "binpath")) { /* path that the binary should move to? */
  502. str_redup(&conf.binpath, line);
  503. } else if (!strcmp(option, "binname")) { /* filename of the binary? */
  504. str_redup(&conf.binname, line);
  505. } else if (!strcmp(option, "portmin")) {
  506. if (egg_isdigit(line[0]))
  507. conf.portmin = atoi(line);
  508. } else if (!strcmp(option, "portmax")) {
  509. if (egg_isdigit(line[0]))
  510. conf.portmax = atoi(line);
  511. } else if (!strcmp(option, "pscloak")) { /* should bots on this shell pscloak? */
  512. if (egg_isdigit(line[0]))
  513. conf.pscloak = atoi(line);
  514. } else if (!strcmp(option, "uid")) { /* new method uid */
  515. if (egg_isdigit(line[0]))
  516. conf.uid = atoi(line);
  517. } else if (!strcmp(option, "uname")) { /* new method uname */
  518. if (!conf.uname)
  519. conf.uname = strdup(line);
  520. else
  521. str_redup(&conf.uname, line);
  522. } else if (!strcmp(option, "watcher")) {
  523. if (egg_isdigit(line[0]))
  524. conf.watcher = atoi(line);
  525. } else {
  526. putlog(LOG_MISC, "*", "Unrecognized config option '%s'", option);
  527. }
  528. /* read in portmin */
  529. } else if (line[0] == '>') {
  530. newsplit(&line);
  531. conf.portmin = atoi(line);
  532. } else if (line[0] == '<') {
  533. newsplit(&line);
  534. conf.portmax = atoi(line);
  535. /* now to parse nick/hosts */
  536. } else if (line[0] != '#') {
  537. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  538. nick = newsplit(&line);
  539. if (!nick || (nick && !nick[0]))
  540. werr(ERR_BADCONF);
  541. if (line[0])
  542. ip = newsplit(&line);
  543. if (line[0])
  544. host = newsplit(&line);
  545. if (line[0])
  546. ipsix = newsplit(&line);
  547. conf_addbot(nick, ip, host, ipsix);
  548. }
  549. }
  550. inbuf[0] = 0;
  551. if (enc)
  552. free(temp_ptr);
  553. } /* while(fgets()) */
  554. fclose(f);
  555. free(inbuf);
  556. return 0;
  557. }
  558. int
  559. writeconf(char *filename, FILE * stream, int bits)
  560. {
  561. FILE *f = NULL;
  562. conf_bot *bot = NULL;
  563. int (*my_write) (FILE *, const char *, ... ) = NULL;
  564. char *p = NULL;
  565. if (bits & CONF_ENC)
  566. my_write = lfprintf;
  567. else if (!(bits & CONF_ENC))
  568. my_write = fprintf;
  569. #define comment(text) \
  570. if (bits & CONF_COMMENT) \
  571. my_write(f, "%s\n", text);
  572. if (stream) {
  573. f = stream;
  574. } else if (filename) {
  575. if (!(f = fopen(filename, "w")))
  576. return 1;
  577. }
  578. #ifndef CYGWIN_HACKS
  579. comment("# Lines beginning with # are what the preceeding line SHOULD be");
  580. comment("# They are simply comments and are not parsed at all.\n");
  581. my_write(f, "! uid %d\n", conf.uid);
  582. if ((bits & CONF_COMMENT) && conf.uid != myuid)
  583. my_write(f, "#! uid %d\n\n", myuid);
  584. if (!conf.uname || (conf.uname && conf.autouname && strcmp(conf.uname, my_uname()))) {
  585. comment("# Automatic");
  586. my_write(f, "! uname %s\n", my_uname());
  587. } else if (conf.uname && !conf.autouname && strcmp(conf.uname, my_uname())) {
  588. my_write(f, "! uname %s\n", conf.uname);
  589. comment("# autouname is OFF");
  590. my_write(f, "#! uname %s\n\n", my_uname());
  591. } else
  592. my_write(f, "! uname %s\n", conf.uname);
  593. my_write(f, "! username %s\n", conf.username ? conf.username : my_username());
  594. if (conf.username && strcmp(conf.username, my_username()))
  595. my_write(f, "#! username %s\n", my_username());
  596. my_write(f, "! homedir %s\n", conf.homedir ? conf.homedir : homedir());
  597. if (conf.homedir && strcmp(conf.homedir, homedir()))
  598. my_write(f, "#! homedir %s\n", homedir());
  599. comment("\n# binpath needs to be full path unless it begins with '~', which uses 'homedir', ie, '~/'");
  600. if (strstr(conf.binpath, homedir())) {
  601. p = replace(conf.binpath, homedir(), "~");
  602. my_write(f, "! binpath %s\n", p);
  603. } else
  604. my_write(f, "! binpath %s\n", conf.binpath);
  605. comment
  606. ("# binname is relative to binpath, if you change this, you'll need to manually remove the old one from crontab.");
  607. my_write(f, "! binname %s\n", conf.binname);
  608. comment("");
  609. comment("# portmin/max are for incoming connections (DCC) [0 for any]");
  610. my_write(f, "! portmin %d\n", conf.portmin);
  611. my_write(f, "! portmax %d\n", conf.portmax);
  612. comment("");
  613. comment("# Attempt to \"cloak\" the process name in `ps` for Linux?");
  614. my_write(f, "! pscloak %d\n", conf.pscloak);
  615. comment("");
  616. comment("# Automatically add the bot to crontab?");
  617. my_write(f, "! autocron %d\n", conf.autocron);
  618. comment("");
  619. comment("# Automatically update 'uname' if it changes? (DANGEROUS)");
  620. my_write(f, "! autouname %d\n", conf.autouname);
  621. comment("");
  622. comment("# This will spawn a child process for EACH BOT that will block ALL process hijackers.");
  623. my_write(f, "! watcher %d\n", conf.watcher);
  624. comment("");
  625. comment("# '|' means OR, [] means the enclosed is optional");
  626. comment("# A '+' in front of HOST means the HOST is ipv6");
  627. comment("# A '/' in front of BOT will disable that bot.");
  628. comment("#[/]BOT IP|. [+]HOST|. [IPV6-IP]");
  629. #endif /* CYGWIN_HACKS */
  630. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  631. my_write(f, "%s %s %s%s %s\n", bot->nick,
  632. bot->net.ip ? bot->net.ip : ".", bot->net.host6 ? "+" : "",
  633. bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "."), bot->net.ip6 ? bot->net.ip6 : "");
  634. }
  635. fflush(f);
  636. if (!stream)
  637. fclose(f);
  638. return 0;
  639. }
  640. static void
  641. conf_bot_dup(conf_bot * dest, conf_bot * src)
  642. {
  643. dest->nick = src->nick ? strdup(src->nick) : NULL;
  644. dest->pid_file = src->pid_file ? strdup(src->pid_file) : NULL;
  645. dest->net.ip = src->net.ip ? strdup(src->net.ip) : NULL;
  646. dest->net.host = src->net.host ? strdup(src->net.host) : NULL;
  647. dest->net.ip6 = src->net.ip6 ? strdup(src->net.ip6) : NULL;
  648. dest->net.host6 = src->net.host6 ? strdup(src->net.host6) : NULL;
  649. dest->net.v6 = src->net.v6;
  650. dest->u = src->u ? src->u : NULL;
  651. dest->pid = src->pid;
  652. dest->hub = src->hub;
  653. dest->localhub = src->localhub;
  654. dest->next = NULL;
  655. }
  656. void
  657. fill_conf_bot()
  658. {
  659. conf_bot *me = NULL;
  660. char *mynick = NULL;
  661. if (!conf.bots || !conf.bots->nick)
  662. return;
  663. if (localhub && conf.bots && conf.bots->nick) {
  664. mynick = strdup(conf.bots->nick);
  665. strlcpy(origbotname, conf.bots->nick, NICKLEN + 1);
  666. } else
  667. mynick = strdup(origbotname);
  668. for (me = conf.bots; me && me->nick; me = me->next)
  669. if (!strcmp(me->nick, mynick))
  670. break;
  671. if (me->nick && me->nick[0] == '/')
  672. werr(ERR_BOTDISABLED);
  673. if (!me->nick || (me->nick && strcmp(me->nick, mynick)))
  674. werr(ERR_BADBOT);
  675. free(mynick);
  676. /* for future, we may just want to make this a pointer to ->bots if we do an emech style currentbot-> */
  677. conf.bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  678. conf_bot_dup(conf.bot, me);
  679. }
  680. void
  681. bin_to_conf(void)
  682. {
  683. /* printf("Converting binary data to conf struct\n"); */
  684. conf.uid = atol(settings.uid);
  685. conf.username = strdup(settings.username);
  686. conf.uname = strdup(settings.uname);
  687. conf.homedir = strdup(settings.homedir);
  688. conf.binpath = strdup(settings.binpath);
  689. fix_tilde(&conf.binpath);
  690. conf.binname = strdup(settings.binname);
  691. conf.portmin = atol(settings.portmin);
  692. conf.portmax = atol(settings.portmax);
  693. conf.autouname = atoi(settings.autouname);
  694. conf.autocron = atoi(settings.autocron);
  695. conf.watcher = atoi(settings.watcher);
  696. conf.pscloak = atoi(settings.pscloak);
  697. /* BOTS */
  698. {
  699. char *p = NULL, *tmp = NULL, *tmpp = NULL;
  700. tmp = tmpp = strdup(settings.bots);
  701. while ((p = strchr(tmp, ','))) {
  702. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  703. *p++ = 0;
  704. if (!tmp[0])
  705. break;
  706. nick = newsplit(&tmp);
  707. if (!nick || (nick && !nick[0]))
  708. werr(ERR_BADCONF);
  709. if (tmp[0])
  710. ip = newsplit(&tmp);
  711. if (tmp[0])
  712. host = newsplit(&tmp);
  713. if (tmp[0])
  714. ipsix = newsplit(&tmp);
  715. conf_addbot(nick, ip, host, ipsix);
  716. tmp = p++;
  717. }
  718. free(tmpp);
  719. }
  720. tellconf();
  721. }