conf.c 20 KB

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