conf.c 26 KB

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