conf.c 30 KB

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