conf.c 22 KB

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