conf.c 19 KB

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