conf.c 20 KB

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