1
0

conf.c 22 KB

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