conf.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. char cfile[DIRMAX] = "";
  25. conf_t conf; /* global conf struct */
  26. conf_t conffile; /* just some config options only avail during loading */
  27. static void
  28. tellconf(conf_t * inconf)
  29. {
  30. conf_bot *bot;
  31. int i = 0;
  32. sdprintf("uid: %d\n", inconf->uid);
  33. sdprintf("uname: %s\n", inconf->uname);
  34. sdprintf("homedir: %s\n", inconf->homedir);
  35. sdprintf("binpath: %s\n", inconf->binpath);
  36. sdprintf("binname: %s\n", inconf->binname);
  37. sdprintf("portmin: %d\n", inconf->portmin);
  38. sdprintf("portmax: %d\n", inconf->portmax);
  39. sdprintf("pscloak: %d\n", inconf->pscloak);
  40. sdprintf("autocron: %d\n", inconf->autocron);
  41. sdprintf("autouname: %d\n", inconf->autouname);
  42. sdprintf("watcher: %d\n", inconf->watcher);
  43. sdprintf("bots:\n");
  44. for (bot = inconf->bots; bot && bot->nick; bot = bot->next) {
  45. i++;
  46. sdprintf("%d: %s IP: %s HOST: %s IP6: %s HOST6: %s v6: %d PID: %d\n", i,
  47. bot->nick,
  48. bot->net.ip ? bot->net.ip : "",
  49. bot->net.host ? bot->net.host : "", bot->net.ip6 ? bot->net.ip6 : "", bot->net.host6 ? bot->net.host6 : "",
  50. bot->net.v6,
  51. bot->pid);
  52. }
  53. sdprintf("bot:\n");
  54. if (inconf->bot && ((bot = inconf->bot)))
  55. sdprintf("%s IP: %s HOST: %s IP6: %s HOST6: %s v6: %d PID: %d\n",
  56. bot->nick,
  57. bot->net.ip ? bot->net.ip : "",
  58. bot->net.host ? bot->net.host : "", bot->net.ip6 ? bot->net.ip6 : "", bot->net.host6 ? bot->net.host6 : "",
  59. bot->net.v6,
  60. bot->pid);
  61. }
  62. #ifdef LEAF
  63. void
  64. spawnbots()
  65. {
  66. conf_bot *bot = NULL;
  67. for (bot = conffile.bots; bot && bot->nick; bot = bot->next) {
  68. if (bot->nick[0] == '/') {
  69. /* kill it if running */
  70. if (bot->pid)
  71. kill(bot->pid, SIGKILL);
  72. else
  73. continue;
  74. } else if (!strcmp(bot->nick, conf.bot->nick) || (bot->pid && !updating)) {
  75. continue;
  76. } else {
  77. int status = 0;
  78. char *run = NULL;
  79. size_t size = 0;
  80. if (updating && bot->pid) {
  81. kill(bot->pid, SIGKILL);
  82. /* remove the pid incase we start the new bot before the old dies */
  83. unlink(bot->pid_file);
  84. }
  85. size = strlen(bot->nick) + strlen(binname) + 20;
  86. run = (char *) my_calloc(1, size);
  87. egg_snprintf(run, size, "%s -B %s", binname, bot->nick);
  88. sdprintf("Spawning '%s': %s", bot->nick, run);
  89. status = system(run);
  90. if (status == -1 || WEXITSTATUS(status))
  91. sdprintf("Failed to spawn '%s': %s", bot->nick, strerror(errno));
  92. free(run);
  93. }
  94. }
  95. }
  96. int
  97. killbot(char *botnick)
  98. {
  99. conf_bot *bot = NULL;
  100. for (bot = conffile.bots; bot && bot->nick; bot = bot->next) {
  101. if (bot->nick[0] == '/')
  102. continue;
  103. else if (!egg_strcasecmp(botnick, bot->nick)) {
  104. if (bot->pid)
  105. return kill(bot->pid, SIGKILL);
  106. }
  107. }
  108. return -1;
  109. }
  110. #endif /* LEAF */
  111. #ifndef CYGWIN_HACKS
  112. static uid_t save_euid, save_egid;
  113. static int
  114. swap_uids()
  115. {
  116. save_euid = geteuid();
  117. save_egid = getegid();
  118. return (setegid(getgid()) || seteuid(getuid()))? -1 : 0;
  119. }
  120. static int
  121. swap_uids_back()
  122. {
  123. return (setegid(save_egid) || seteuid(save_euid)) ? -1 : 0;
  124. }
  125. void
  126. confedit()
  127. {
  128. Tempfile tmpconf = Tempfile("conf");
  129. char *editor = NULL;
  130. mode_t um;
  131. int waiter;
  132. pid_t pid, xpid;
  133. um = umask(077);
  134. writeconf(NULL, tmpconf.f, CONF_COMMENT);
  135. fclose(tmpconf.f);
  136. (void) umask(um);
  137. if (!can_stat(tmpconf.file))
  138. fatal("Cannot stat tempfile", 0);
  139. /* Okay, edit the file */
  140. if ((!((editor = getenv("VISUAL")) && strlen(editor)))
  141. && (!((editor = getenv("EDITOR")) && strlen(editor)))
  142. ) {
  143. editor = "vi";
  144. /*
  145. #if defined(DEBIAN)
  146. editor = "/usr/bin/editor";
  147. #elif defined(_PATH_VI)
  148. editor = _PATH_VI;
  149. #else
  150. editor = "/usr/ucb/vi";
  151. #endif
  152. */
  153. }
  154. signal(SIGINT, SIG_IGN);
  155. signal(SIGQUIT, SIG_IGN);
  156. signal(SIGCONT, SIG_DFL);
  157. swap_uids();
  158. switch (pid = fork()) {
  159. case -1:
  160. fatal("Cannot fork", 0);
  161. case 0:
  162. {
  163. char *run = NULL;
  164. size_t size = tmpconf.len + strlen(editor) + 5;
  165. setgid(getgid());
  166. setuid(getuid());
  167. run = (char *) my_calloc(1, size);
  168. /* child */
  169. egg_snprintf(run, size, "%s %s", editor, tmpconf.file);
  170. execlp("/bin/sh", "/bin/sh", "-c", run, NULL);
  171. perror(editor);
  172. exit(1);
  173. /*NOTREACHED*/}
  174. default:
  175. /* parent */
  176. break;
  177. }
  178. /* parent */
  179. while (1) {
  180. xpid = waitpid(pid, &waiter, WUNTRACED);
  181. if (xpid == -1) {
  182. fprintf(stderr, "waitpid() failed waiting for PID %d from \"%s\": %s\n", pid, editor, strerror(errno));
  183. } else if (xpid != pid) {
  184. fprintf(stderr, "wrong PID (%d != %d) from \"%s\"\n", xpid, pid, editor);
  185. goto fatal;
  186. } else if (WIFSTOPPED(waiter)) {
  187. /* raise(WSTOPSIG(waiter)); Not needed and breaks in job control shell */
  188. } else if (WIFEXITED(waiter) && WEXITSTATUS(waiter)) {
  189. fprintf(stderr, "\"%s\" exited with status %d\n", editor, WEXITSTATUS(waiter));
  190. goto fatal;
  191. } else if (WIFSIGNALED(waiter)) {
  192. fprintf(stderr, "\"%s\" killed; signal %d (%score dumped)\n", editor, WTERMSIG(waiter),
  193. # ifdef CYGWIN_HACKS
  194. 0
  195. # else
  196. WCOREDUMP(waiter)
  197. # endif
  198. /* CYGWIN_HACKS */
  199. ? "" : "no ");
  200. goto fatal;
  201. } else {
  202. break;
  203. }
  204. }
  205. (void) signal(SIGINT, SIG_DFL);
  206. (void) signal(SIGQUIT, SIG_DFL);
  207. swap_uids_back();
  208. if (!can_stat(tmpconf.file))
  209. fatal("Error reading new config file", 0);
  210. readconf(tmpconf.file, 0); /* read cleartext conf tmp into &settings */
  211. unlink(tmpconf.file);
  212. fix_tilde(&conffile.binpath);
  213. conf_to_bin(&conffile); /* will exit */
  214. exit(0); /* never reached */
  215. fatal:
  216. unlink(tmpconf.file);
  217. exit(1);
  218. }
  219. #endif /* !CYGWIN_HACKS */
  220. void
  221. init_conf()
  222. {
  223. conffile.bots = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  224. conffile.bots->nick = NULL;
  225. conffile.bots->next = NULL;
  226. conffile.bot = NULL;
  227. conffile.watcher = 0;
  228. #ifdef CYGWIN_HACKS
  229. conffile.autocron = 0;
  230. #else
  231. conffile.autocron = 1;
  232. #endif /* !CYGWIN_HACKS */
  233. conffile.autouname = 0;
  234. #ifdef CYGWIN_HACKS
  235. conffile.binpath = strdup(homedir());
  236. #else /* !CYGWIN_HACKS */
  237. # ifdef LEAF
  238. conffile.binpath = strdup(STR("~/"));
  239. # endif /* LEAF */
  240. # ifdef HUB
  241. conffile.binpath = strdup(dirname(binname));
  242. # endif /* HUB */
  243. #endif /* CYGWIN_HACKS */
  244. #ifdef LEAF
  245. conffile.binname = strdup(STR(".sshrc"));
  246. #endif /* LEAF */
  247. #ifdef HUB
  248. {
  249. char *p = NULL;
  250. p = strrchr(binname, '/');
  251. p++;
  252. conffile.binname = strdup(p);
  253. }
  254. #endif /* HUB */
  255. conffile.portmin = 0;
  256. conffile.portmax = 0;
  257. conffile.pscloak = 0;
  258. conffile.uid = 0;
  259. conffile.uname = NULL;
  260. conffile.username = NULL;
  261. conffile.homedir = NULL;
  262. }
  263. /*
  264. * Return the PID of a bot if it is running, otherwise return 0
  265. */
  266. pid_t
  267. checkpid(char *nick, conf_bot *bot)
  268. {
  269. FILE *f = NULL;
  270. char buf[DIRMAX] = "", s[11] = "", *tmpnick = NULL, *tmp_ptr = NULL;
  271. tmpnick = tmp_ptr = strdup(nick);
  272. if (tmpnick[0] == '/')
  273. tmpnick++;
  274. egg_snprintf(buf, sizeof buf, "%s.pid.%s", tempdir, tmpnick);
  275. free(tmp_ptr);
  276. if (bot && !(bot->pid_file))
  277. bot->pid_file = strdup(buf);
  278. else if (bot && strcmp(bot->pid_file, buf))
  279. str_redup(&bot->pid_file, buf);
  280. if ((f = fopen(buf, "r"))) {
  281. pid_t xx = 0;
  282. fgets(s, 10, f);
  283. remove_crlf(s);
  284. fclose(f);
  285. xx = atoi(s);
  286. // if (bot) {
  287. int x = 0;
  288. x = kill(xx, SIGCHLD);
  289. if (x == -1 && errno == ESRCH)
  290. return 0;
  291. else if (x == 0)
  292. return xx;
  293. // } else {
  294. // return xx ? xx : 0;
  295. // }
  296. }
  297. return 0;
  298. }
  299. #ifdef HUB
  300. static
  301. #endif /* HUB */
  302. void
  303. conf_addbot(char *nick, char *ip, char *host, char *ip6)
  304. {
  305. conf_bot *bot = NULL;
  306. for (bot = conffile.bots; bot && bot->nick; bot = bot->next) ;
  307. bot->next = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  308. bot->next->next = NULL;
  309. bot->pid_file = NULL;
  310. bot->nick = strdup(nick);
  311. #ifdef LEAF
  312. if (bot == conffile.bots) {
  313. bot->localhub = 1; /* first bot */
  314. conffile.localhub = strdup(nick ? nick : origbotname);
  315. /* perhaps they did -B localhub-bot ? */
  316. if (origbotname[0] && !strcmp(origbotname, bot->nick))
  317. localhub = 1;
  318. }
  319. #endif /* LEAF */
  320. bot->net.ip = NULL;
  321. bot->net.host = NULL;
  322. bot->net.ip6 = NULL;
  323. bot->net.host6 = NULL;
  324. if (host && host[0] == '+') {
  325. host++;
  326. bot->net.host6 = strdup(host);
  327. } else if (host && strcmp(host, ".")) {
  328. bot->net.host = strdup(host);
  329. }
  330. if (ip && strcmp(ip, ".") && is_dotted_ip(ip) == AF_INET)
  331. bot->net.ip = strdup(ip);
  332. if (ip6 && strcmp(ip6, ".") && is_dotted_ip(ip) == AF_INET6)
  333. bot->net.ip6 = strdup(ip6);
  334. if (bot->net.ip6 || bot->net.host6)
  335. bot->net.v6 = 1;
  336. bot->u = NULL;
  337. bot->pid = checkpid(nick, bot);
  338. }
  339. void
  340. free_bot(char *botn)
  341. {
  342. conf_bot *bot = NULL, *old = NULL;
  343. for (bot = conffile.bots; bot && bot->nick; old = bot, bot = bot->next) {
  344. if (!strcmp(botn, bot->nick)) {
  345. free(bot->nick);
  346. free(bot->pid_file);
  347. if (bot->net.ip)
  348. free(bot->net.ip);
  349. if (bot->net.host)
  350. free(bot->net.host);
  351. if (bot->net.ip6)
  352. free(bot->net.ip6);
  353. if (bot->net.host6)
  354. free(bot->net.host6);
  355. if (old)
  356. old->next = bot->next;
  357. else
  358. conffile.bots = bot->next;
  359. free(bot);
  360. break;
  361. }
  362. }
  363. }
  364. #ifdef LEAF
  365. int
  366. conf_delbot(char *botn)
  367. {
  368. conf_bot *bot = NULL;
  369. for (bot = conffile.bots; bot && bot->nick; bot = bot->next) {
  370. if (!strcmp(bot->nick, botn)) { /* found it! */
  371. bot->pid = checkpid(bot->nick, bot);
  372. killbot(bot->nick);
  373. free_bot(bot->nick);
  374. return 0;
  375. }
  376. }
  377. return 1;
  378. }
  379. #endif /* LEAF */
  380. static void
  381. free_conf_bots(void)
  382. {
  383. conf_bot *bot = NULL, *bot_n = NULL;
  384. for (bot = conffile.bots; bot; bot = bot_n) {
  385. bot_n = bot->next;
  386. free_bot(bot->nick);
  387. }
  388. }
  389. void free_conf(void)
  390. {
  391. free_conf_bots();
  392. free(conffile.uname);
  393. free(conffile.username);
  394. free(conffile.homedir);
  395. free(conffile.binname);
  396. free(conffile.binpath);
  397. }
  398. int
  399. parseconf()
  400. {
  401. if (!conffile.bots->nick && !conffile.bots->next) /* no bots ! */
  402. werr(ERR_NOBOTS);
  403. if (conffile.username) {
  404. str_redup(&conffile.username, my_username());
  405. } else {
  406. conffile.username = strdup(my_username());
  407. }
  408. #ifndef CYGWIN_HACKS
  409. if (conffile.uid && conffile.uid != myuid) {
  410. sdprintf("wrong uid, conf: %d :: %d", conffile.uid, myuid);
  411. werr(ERR_WRONGUID);
  412. } else if (!conffile.uid)
  413. conffile.uid = myuid;
  414. if (conffile.uname && strcmp(conffile.uname, my_uname()) && !conffile.autouname) {
  415. baduname(conffile.uname, my_uname()); /* its not auto, and its not RIGHT, bail out. */
  416. sdprintf("wrong uname, conf: %s :: %s", conffile.uname, my_uname());
  417. werr(ERR_WRONGUNAME);
  418. } else if (conffile.uname && conffile.autouname) { /* if autouname, dont bother comparing, just set uname to output */
  419. str_redup(&conffile.uname, my_uname());
  420. } else if (!conffile.uname) { /* if not set, then just set it, wont happen again next time... */
  421. conffile.uname = strdup(my_uname());
  422. }
  423. if (conffile.homedir) {
  424. str_redup(&conffile.homedir, homedir());
  425. } else {
  426. conffile.homedir = strdup(homedir());
  427. }
  428. fix_tilde(&conffile.binpath);
  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 *) my_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. fatal("Bad encryption", 0);
  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->net.ip ? bot->net.ip : ".", bot->net.host6 ? "+" : "",
  623. bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "."), bot->net.ip6 ? bot->net.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->net.ip = src->net.ip ? strdup(src->net.ip) : NULL;
  636. dest->net.host = src->net.host ? strdup(src->net.host) : NULL;
  637. dest->net.ip6 = src->net.ip6 ? strdup(src->net.ip6) : NULL;
  638. dest->net.host6 = src->net.host6 ? strdup(src->net.host6) : NULL;
  639. dest->net.v6 = src->net.v6;
  640. dest->u = src->u ? src->u : NULL;
  641. dest->pid = src->pid;
  642. #ifdef LEAF
  643. dest->localhub = src->localhub;
  644. #endif /* LEAF */
  645. dest->next = NULL;
  646. }
  647. void
  648. fillconf(conf_t * inconf)
  649. {
  650. conf_bot *bot = NULL;
  651. char *mynick = NULL;
  652. if (localhub && conffile.bots && conffile.bots->nick) {
  653. mynick = strdup(conffile.bots->nick);
  654. strncpyz(origbotname, conffile.bots->nick, NICKLEN + 1);
  655. } else
  656. mynick = strdup(origbotname);
  657. for (bot = conffile.bots; bot && bot->nick; bot = bot->next)
  658. if (!strcmp(bot->nick, mynick))
  659. break;
  660. if (bot->nick && bot->nick[0] == '/')
  661. werr(ERR_BOTDISABLED);
  662. if (!bot->nick || (bot->nick && strcmp(bot->nick, mynick)))
  663. werr(ERR_BADBOT);
  664. free(mynick);
  665. inconf->bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  666. conf_bot_dup(inconf->bot, bot);
  667. inconf->localhub = conffile.localhub ? strdup(conffile.localhub) : NULL;
  668. inconf->binpath = conffile.binpath ? strdup(conffile.binpath) : NULL;
  669. inconf->binname = conffile.binname ? strdup(conffile.binname) : NULL;
  670. inconf->uname = conffile.uname ? strdup(conffile.uname) : NULL;
  671. inconf->username = conffile.username ? strdup(conffile.username) : NULL;
  672. inconf->homedir = conffile.homedir ? strdup(conffile.homedir) : NULL;
  673. inconf->autocron = conffile.autocron;
  674. inconf->watcher = conffile.watcher;
  675. inconf->autouname = conffile.autouname;
  676. inconf->portmin = conffile.portmin;
  677. inconf->portmax = conffile.portmax;
  678. inconf->pscloak = conffile.pscloak;
  679. inconf->uid = conffile.uid;
  680. }
  681. void
  682. bin_to_conf(void)
  683. {
  684. /* printf("Converting binary data to conf struct\n"); */
  685. conffile.uid = atol(settings.uid);
  686. conffile.username = strdup(settings.username);
  687. conffile.uname = strdup(settings.uname);
  688. conffile.homedir = strdup(settings.homedir);
  689. conffile.binpath = strdup(settings.binpath);
  690. conffile.binname = strdup(settings.binname);
  691. conffile.portmin = atol(settings.portmin);
  692. conffile.portmax = atol(settings.portmax);
  693. conffile.autouname = atoi(settings.autouname);
  694. conffile.autocron = atoi(settings.autocron);
  695. conffile.watcher = atoi(settings.watcher);
  696. conffile.pscloak = atoi(settings.pscloak);
  697. /* BOTS */
  698. {
  699. char *p = NULL, *tmp = NULL, *tmpp = NULL;
  700. tmp = tmpp = strdup(settings.bots);
  701. while ((p = strchr(tmp, ','))) {
  702. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  703. *p++ = 0;
  704. if (!tmp[0])
  705. break;
  706. nick = newsplit(&tmp);
  707. if (!nick || (nick && !nick[0]))
  708. werr(ERR_BADCONF);
  709. if (tmp[0])
  710. ip = newsplit(&tmp);
  711. if (tmp[0])
  712. host = newsplit(&tmp);
  713. if (tmp[0])
  714. ipsix = newsplit(&tmp);
  715. conf_addbot(nick, ip, host, ipsix);
  716. tmp = p++;
  717. }
  718. free(tmpp);
  719. }
  720. tellconf(&conffile);
  721. }