conf.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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("homedir: %s\n", conf.homedir);
  44. sdprintf("binpath: %s\n", replace(conf.binpath, conf.homedir, "~"));
  45. sdprintf("binname: %s\n", conf.binname);
  46. sdprintf("datadir: %s\n", replace(conf.datadir, conf.homedir, "~"));
  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. expand_tilde(&conf.binpath);
  283. expand_tilde(&conf.datadir);
  284. unlink(tmpconf.file);
  285. conf_to_bin(&conf, 0, -1);
  286. if (localhub_pid)
  287. kill(localhub_pid, SIGUSR1);
  288. exit(0);
  289. fatal:
  290. unlink(tmpconf.file);
  291. exit(1);
  292. }
  293. #endif /* !CYGWIN_HACKS */
  294. void
  295. init_conf()
  296. {
  297. // conf.bots = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  298. // conf.bots->nick = NULL;
  299. // conf.bots->next = NULL;
  300. conf.bots = NULL;
  301. conf.bot = NULL;
  302. conf.watcher = 0;
  303. #ifdef CYGWIN_HACKS
  304. conf.autocron = 0;
  305. #else
  306. conf.autocron = 1;
  307. #endif /* !CYGWIN_HACKS */
  308. conf.autouname = 0;
  309. #ifdef CYGWIN_HACKS
  310. conf.binpath = strdup(homedir());
  311. #else /* !CYGWIN_HACKS */
  312. conf.binpath = strdup(dirname(binname));
  313. #endif /* CYGWIN_HACKS */
  314. char *p = strrchr(binname, '/');
  315. p++;
  316. if (!strncmp(p, "wraith.", 7) && strchr(p, '-'))
  317. conf.binname = strdup("wraith");
  318. else
  319. conf.binname = strdup(p);
  320. conf.portmin = 0;
  321. conf.portmax = 0;
  322. conf.pscloak = 0;
  323. conf.uid = -1;
  324. conf.uname = NULL;
  325. conf.username = NULL;
  326. conf.homedir = NULL;
  327. conf.datadir = strdup("~/.ssh/...");
  328. expand_tilde(&conf.datadir);
  329. }
  330. void conf_checkpids()
  331. {
  332. conf_bot *bot = NULL;
  333. for (bot = conf.bots; bot && bot->nick; bot = bot->next)
  334. bot->pid = checkpid(bot->nick, bot);
  335. }
  336. /*
  337. * Return the PID of a bot if it is running, otherwise return 0
  338. */
  339. pid_t
  340. checkpid(const char *nick, conf_bot *bot)
  341. {
  342. FILE *f = NULL;
  343. char buf[DIRMAX] = "", *tmpnick = NULL, *tmp_ptr = NULL;
  344. pid_t pid = 0;
  345. tmpnick = tmp_ptr = strdup(nick);
  346. simple_snprintf(buf, sizeof buf, "%s/.pid.%s", conf.datadir, tmpnick);
  347. free(tmp_ptr);
  348. if (bot && !(bot->pid_file))
  349. bot->pid_file = strdup(buf);
  350. else if (bot && strcmp(bot->pid_file, buf))
  351. str_redup(&bot->pid_file, buf);
  352. if ((f = fopen(buf, "r"))) {
  353. char *bufp = NULL, *pids = NULL;
  354. fgets(buf, sizeof(buf), f);
  355. fclose(f);
  356. remove_crlf(buf);
  357. if (!buf || !buf[0])
  358. return 0;
  359. bufp = buf;
  360. pids = newsplit(&bufp);
  361. if (str_isdigit(pids)) {
  362. pid = atoi(pids);
  363. if (kill(pid, SIGCHLD)) //Problem killing, most likely it's just not running.
  364. pid = 0;
  365. }
  366. //There is a socksfile given and it's accessable, plus the pid in the file is my own
  367. //So it's a good chance we just did a soft restart
  368. if (bufp[0] && pid && can_stat(bufp) && (getpid() == pid) &&
  369. !egg_strcasecmp(nick, origbotname)) {
  370. socksfile = strdup(bufp);
  371. return 0;
  372. }
  373. }
  374. return pid;
  375. }
  376. void
  377. conf_addbot(char *nick, char *ip, char *host, char *ip6)
  378. {
  379. conf_bot *bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  380. bot->next = NULL;
  381. bot->pid_file = NULL;
  382. if (nick[0] == '/') {
  383. bot->disabled = 1;
  384. nick++;
  385. sdprintf("%s is disabled.", nick);
  386. }
  387. bot->nick = strdup(nick);
  388. bot->net.ip = NULL;
  389. bot->net.host = NULL;
  390. bot->net.ip6 = NULL;
  391. bot->net.host6 = NULL;
  392. if (host && host[0] == '+') {
  393. host++;
  394. bot->net.host6 = strdup(host);
  395. } else if (host && strcmp(host, ".")) {
  396. bot->net.host = strdup(host);
  397. }
  398. if (ip && strcmp(ip, ".")) {
  399. int aftype = is_dotted_ip(ip);
  400. if (aftype == AF_INET)
  401. bot->net.ip = strdup(ip);
  402. #ifdef USE_IPV6
  403. else if (aftype == AF_INET6)
  404. bot->net.ip6 = strdup(ip);
  405. #endif /* USE_IPV6 */
  406. }
  407. #ifdef USE_IPV6
  408. if (ip6 && strcmp(ip6, ".") && is_dotted_ip(ip6) == AF_INET6)
  409. bot->net.ip6 = strdup(ip6);
  410. if (bot->net.ip6 || bot->net.host6)
  411. bot->net.v6 = 1;
  412. #endif /* USE_IPV6 */
  413. if (userlist)
  414. bot->u = get_user_by_handle(userlist, nick);
  415. else
  416. bot->u = NULL;
  417. // bot->pid = checkpid(nick, bot);
  418. if (settings.hubs) {
  419. char *p = settings.hubs, *p2 = NULL, hubbuf[HANDLEN + 1] ="";
  420. size_t len = 0;
  421. while (p && *p) {
  422. if ((p2 = strchr(p, ' '))) {
  423. len = p2 - p;
  424. simple_snprintf(hubbuf, len + 1, "%s", p);
  425. if (!egg_strcasecmp(bot->nick, hubbuf)) {
  426. bot->hub = 1;
  427. break;
  428. }
  429. }
  430. if ((p = strchr(p, ',')))
  431. p++;
  432. }
  433. }
  434. /* not a hub
  435. AND
  436. * no bots added yet (first bot) yet, not disabled.
  437. OR
  438. * bots already listed but we dont have a localhub yet, so we're it!
  439. */
  440. if (!bot->hub && ((!conf.bots && !bot->disabled) || (conf.bots && !conf.localhub))) {
  441. bot->localhub = 1; /* first bot */
  442. conf.localhub = strdup(bot->nick);
  443. }
  444. list_append((struct list_type **) &(conf.bots), (struct list_type *) bot);
  445. }
  446. void
  447. free_bot(conf_bot *bot)
  448. {
  449. if (bot) {
  450. list_delete((struct list_type **) &(conf.bots), (struct list_type *) bot);
  451. free(bot->nick);
  452. free(bot->pid_file);
  453. if (bot->net.ip)
  454. free(bot->net.ip);
  455. if (bot->net.host)
  456. free(bot->net.host);
  457. if (bot->net.ip6)
  458. free(bot->net.ip6);
  459. if (bot->net.host6)
  460. free(bot->net.host6);
  461. free(bot);
  462. }
  463. }
  464. int
  465. conf_delbot(char *botn)
  466. {
  467. conf_bot *bot = NULL;
  468. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  469. if (!strcmp(bot->nick, botn)) { /* found it! */
  470. bot->pid = checkpid(bot->nick, bot);
  471. conf_killbot(NULL, bot, SIGKILL);
  472. free_bot(bot);
  473. return 0;
  474. }
  475. }
  476. return 1;
  477. }
  478. void
  479. free_conf()
  480. {
  481. free_conf_bots(conf.bots);
  482. free_bot(conf.bot);
  483. conf.bot = NULL;
  484. free(conf.localhub);
  485. free(conf.uname);
  486. free(conf.username);
  487. if (conf.datadir)
  488. free(conf.datadir);
  489. free(conf.homedir);
  490. free(conf.binname);
  491. free(conf.binpath);
  492. }
  493. void
  494. free_conf_bots(conf_bot *list)
  495. {
  496. if (list) {
  497. conf_bot *bot = NULL, *bot_n = NULL;
  498. for (bot = list; bot; bot = bot_n) {
  499. bot_n = bot->next;
  500. free_bot(bot);
  501. }
  502. list = NULL;
  503. }
  504. }
  505. int
  506. parseconf(bool error)
  507. {
  508. if (conf.username) {
  509. str_redup(&conf.username, my_username());
  510. } else {
  511. conf.username = strdup(my_username());
  512. }
  513. #ifndef CYGWIN_HACKS
  514. if (error && conf.uid != (signed) myuid) {
  515. sdprintf("wrong uid, conf: %d :: %d", conf.uid, myuid);
  516. werr(ERR_WRONGUID);
  517. } else if (!conf.uid)
  518. conf.uid = myuid;
  519. if (conf.uname && strcmp(conf.uname, my_uname()) && !conf.autouname) {
  520. baduname(conf.uname, my_uname()); /* its not auto, and its not RIGHT, bail out. */
  521. sdprintf("wrong uname, conf: %s :: %s", conf.uname, my_uname());
  522. if (error)
  523. werr(ERR_WRONGUNAME);
  524. } else if (conf.uname && conf.autouname) { /* if autouname, dont bother comparing, just set uname to output */
  525. str_redup(&conf.uname, my_uname());
  526. } else if (!conf.uname) { /* if not set, then just set it, wont happen again next time... */
  527. conf.uname = strdup(my_uname());
  528. }
  529. if (conf.homedir) {
  530. str_redup(&conf.homedir, homedir());
  531. } else {
  532. conf.homedir = strdup(homedir());
  533. }
  534. #endif /* !CYGWIN_HACKS */
  535. return 0;
  536. }
  537. int
  538. readconf(const char *fname, int bits)
  539. {
  540. FILE *f = NULL;
  541. int i = 0, enc = (bits & CONF_ENC) ? 1 : 0;
  542. char *inbuf = NULL;
  543. sdprintf("readconf(%s, %d)", fname, enc);
  544. Context;
  545. if (!(f = fopen(fname, "r")))
  546. fatal("Cannot read config", 0);
  547. free_conf_bots(conf.bots);
  548. inbuf = (char *) my_calloc(1, 201);
  549. while (fgets(inbuf, 201, f) != NULL) {
  550. char *line = NULL, *temp_ptr = NULL;
  551. remove_crlf(inbuf);
  552. if (enc)
  553. line = temp_ptr = decrypt_string(settings.salt1, inbuf);
  554. else
  555. line = inbuf;
  556. if ((line && !line[0]) || line[0] == '\n') {
  557. if (enc)
  558. free(line);
  559. continue;
  560. }
  561. i++;
  562. rmspace(line);
  563. sdprintf("CONF LINE: %s", line);
  564. // !strchr("_`|}][{*/#-+!abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPWRSTUVWXYZ", line[0])) {
  565. if (enc && line[0] > '~') {
  566. sdprintf("line %d, char %c ", i, line[0]);
  567. fatal("Bad encryption", 0);
  568. } else { /* line is good to parse */
  569. /* - uid */
  570. if (line[0] == '-') {
  571. newsplit(&line);
  572. if (conf.uid == -1)
  573. conf.uid = atoi(line);
  574. /* + uname */
  575. } else if (line[0] == '+') {
  576. newsplit(&line);
  577. if (!conf.uname)
  578. conf.uname = strdup(line);
  579. /* ! is misc options */
  580. } else if (line[0] == '!') {
  581. char *option = NULL;
  582. newsplit(&line);
  583. if (line[0])
  584. option = newsplit(&line);
  585. if (!option)
  586. continue;
  587. if (!strcmp(option, "autocron")) { /* automatically check/create crontab? */
  588. if (egg_isdigit(line[0]))
  589. conf.autocron = atoi(line);
  590. } else if (!strcmp(option, "autouname")) { /* auto update uname contents? */
  591. if (egg_isdigit(line[0]))
  592. conf.autouname = atoi(line);
  593. } else if (!strcmp(option, "username")) { /* shell username */
  594. conf.username = strdup(line);
  595. } else if (!strcmp(option, "homedir")) { /* homedir */
  596. conf.homedir = strdup(line);
  597. } else if (!strcmp(option, "datadir")) { /* datadir */
  598. conf.datadir = strdup(line);
  599. } else if (!strcmp(option, "binpath")) { /* path that the binary should move to? */
  600. str_redup(&conf.binpath, line);
  601. } else if (!strcmp(option, "binname")) { /* filename of the binary? */
  602. str_redup(&conf.binname, line);
  603. } else if (!strcmp(option, "portmin")) {
  604. if (egg_isdigit(line[0]))
  605. conf.portmin = atoi(line);
  606. } else if (!strcmp(option, "portmax")) {
  607. if (egg_isdigit(line[0]))
  608. conf.portmax = atoi(line);
  609. } else if (!strcmp(option, "pscloak")) { /* should bots on this shell pscloak? */
  610. if (egg_isdigit(line[0]))
  611. conf.pscloak = atoi(line);
  612. } else if (!strcmp(option, "uid")) { /* new method uid */
  613. if (str_isdigit(line))
  614. conf.uid = atoi(line);
  615. } else if (!strcmp(option, "uname")) { /* new method uname */
  616. if (!conf.uname)
  617. conf.uname = strdup(line);
  618. else
  619. str_redup(&conf.uname, line);
  620. } else if (!strcmp(option, "watcher")) {
  621. if (egg_isdigit(line[0]))
  622. conf.watcher = atoi(line);
  623. } else {
  624. putlog(LOG_MISC, "*", "Unrecognized config option '%s'", option);
  625. }
  626. /* read in portmin */
  627. } else if (line[0] == '>') {
  628. newsplit(&line);
  629. conf.portmin = atoi(line);
  630. } else if (line[0] == '<') {
  631. newsplit(&line);
  632. conf.portmax = atoi(line);
  633. /* now to parse nick/hosts */
  634. } else if (line[0] != '#') {
  635. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  636. nick = newsplit(&line);
  637. if (!nick || (nick && !nick[0]))
  638. werr(ERR_BADCONF);
  639. if (line[0])
  640. ip = newsplit(&line);
  641. if (line[0])
  642. host = newsplit(&line);
  643. if (line[0])
  644. ipsix = newsplit(&line);
  645. conf_addbot(nick, ip, host, ipsix);
  646. }
  647. }
  648. inbuf[0] = 0;
  649. if (enc)
  650. free(temp_ptr);
  651. } /* while(fgets()) */
  652. fclose(f);
  653. free(inbuf);
  654. return 0;
  655. }
  656. int
  657. writeconf(char *filename, FILE * stream, int bits)
  658. {
  659. FILE *f = NULL;
  660. conf_bot *bot = NULL;
  661. int (*my_write) (FILE *, const char *, ... ) = NULL;
  662. int autowrote = 0;
  663. if (bits & CONF_ENC)
  664. my_write = lfprintf;
  665. else if (!(bits & CONF_ENC))
  666. my_write = fprintf;
  667. #define comment(text) do { \
  668. if (bits & CONF_COMMENT) \
  669. my_write(f, "%s\n", text); \
  670. } while(0)
  671. if (stream) {
  672. f = stream;
  673. } else if (filename) {
  674. if (!(f = fopen(filename, "w")))
  675. return 1;
  676. }
  677. #ifndef CYGWIN_HACKS
  678. char *p = NULL;
  679. comment("# Lines beginning with # are what the preceeding line SHOULD be");
  680. comment("# They are simply comments and are not parsed at all.\n");
  681. #define conf_com() do { \
  682. if (do_confedit == CONF_AUTO) { \
  683. comment("# Automatically updated with -C"); \
  684. autowrote = 1; \
  685. } else \
  686. comment("#The correct line follows. (Not auto due to -c)"); \
  687. } while(0)
  688. if ((bits & CONF_COMMENT) && conf.uid != (signed) myuid) {
  689. conf_com();
  690. my_write(f, "%s! uid %d\n", do_confedit == CONF_AUTO ? "" : "#", myuid);
  691. my_write(f, "%s! uid %d\n", do_confedit == CONF_STATIC ? "" : "#", conf.uid);
  692. } else
  693. my_write(f, "! uid %d\n", conf.uid);
  694. if (!conf.uname || (conf.uname && conf.autouname && strcmp(conf.uname, my_uname()))) {
  695. autowrote = 1;
  696. if (conf.uname)
  697. comment("# autouname is ON");
  698. else
  699. comment("# Automatically updated empty uname");
  700. my_write(f, "! uname %s\n", my_uname());
  701. if (conf.uname)
  702. my_write(f, "#! uname %s\n", conf.uname);
  703. } else if (conf.uname && !conf.autouname && strcmp(conf.uname, my_uname())) {
  704. conf_com();
  705. my_write(f, "%s! uname %s\n", do_confedit == CONF_AUTO ? "" : "#", my_uname());
  706. my_write(f, "%s! uname %s\n", do_confedit == CONF_STATIC ? "" : "#", conf.uname);
  707. } else
  708. my_write(f, "! uname %s\n", conf.uname);
  709. comment("");
  710. if (conf.username && strcmp(conf.username, my_username())) {
  711. conf_com();
  712. my_write(f, "%s! username %s\n", do_confedit == CONF_AUTO ? "" : "#", my_username());
  713. my_write(f, "%s! username %s\n", do_confedit == CONF_STATIC ? "" : "#", conf.username);
  714. } else
  715. my_write(f, "! username %s\n", conf.username ? conf.username : my_username());
  716. if (conf.homedir && strcmp(conf.homedir, homedir(0))) {
  717. conf_com();
  718. my_write(f, "%s! homedir %s\n", do_confedit == CONF_AUTO ? "" : "#", homedir(0));
  719. my_write(f, "%s! homedir %s\n", do_confedit == CONF_STATIC ? "" : "#", conf.homedir);
  720. } else
  721. my_write(f, "! homedir %s\n", conf.homedir ? conf.homedir : homedir(0));
  722. comment("\n# binpath needs to be full path unless it begins with '~', which uses 'homedir', ie, '~/'");
  723. if (strstr(conf.binpath, homedir())) {
  724. p = replace(conf.binpath, homedir(), "~");
  725. my_write(f, "! binpath %s\n", p);
  726. } else
  727. my_write(f, "! binpath %s\n", conf.binpath);
  728. comment("# binname is relative to binpath, if you change this, you'll need to manually remove the old one from crontab.");
  729. my_write(f, "! binname %s\n", conf.binname);
  730. comment("");
  731. comment("# datadir should be set to a static directory that is writable");
  732. if (strstr(conf.datadir, homedir())) {
  733. p = replace(conf.datadir, homedir(), "~");
  734. my_write(f, "! datadir %s\n", p);
  735. } else
  736. my_write(f, "! datadir %s\n", conf.datadir);
  737. comment("");
  738. comment("# portmin/max are for incoming connections (DCC) [0 for any]");
  739. my_write(f, "! portmin %d\n", conf.portmin);
  740. my_write(f, "! portmax %d\n", conf.portmax);
  741. comment("");
  742. comment("# Attempt to \"cloak\" the process name in `ps` for Linux?");
  743. my_write(f, "! pscloak %d\n", conf.pscloak);
  744. comment("");
  745. comment("# Automatically add the bot to crontab? (Disable if binname has funky chars that need escaping)");
  746. my_write(f, "! autocron %d\n", conf.autocron);
  747. comment("");
  748. comment("# Automatically update 'uname' if it changes? (DANGEROUS)");
  749. my_write(f, "! autouname %d\n", conf.autouname);
  750. comment("");
  751. comment("# This will spawn a child process for EACH BOT that will block ALL process hijackers.");
  752. my_write(f, "! watcher %d\n", conf.watcher);
  753. comment("");
  754. comment("# '|' means OR, [] means the enclosed is optional");
  755. comment("# A '+' in front of HOST means the HOST is ipv6");
  756. comment("# A '/' in front of BOT will disable that bot.");
  757. comment("#[/]BOT IP|. [+]HOST|. [IPV6-IP]");
  758. comment("#***** 1.2.3: Hubs CAN be mixed with leaf bots, but is not fully tested; it is not recommended. ******");
  759. #endif /* CYGWIN_HACKS */
  760. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  761. my_write(f, "%s%s %s %s%s %s\n",
  762. bot->disabled ? "/" : "", bot->nick,
  763. bot->net.ip ? bot->net.ip : ".", bot->net.host6 ? "+" : "",
  764. bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "."), bot->net.ip6 ? bot->net.ip6 : "");
  765. }
  766. fflush(f);
  767. if (!stream)
  768. fclose(f);
  769. return autowrote;
  770. }
  771. static void
  772. conf_bot_dup(conf_bot *dest, conf_bot *src)
  773. {
  774. if (dest && src) {
  775. dest->nick = src->nick ? strdup(src->nick) : NULL;
  776. dest->pid_file = src->pid_file ? strdup(src->pid_file) : NULL;
  777. dest->net.ip = src->net.ip ? strdup(src->net.ip) : NULL;
  778. dest->net.host = src->net.host ? strdup(src->net.host) : NULL;
  779. dest->net.ip6 = src->net.ip6 ? strdup(src->net.ip6) : NULL;
  780. dest->net.host6 = src->net.host6 ? strdup(src->net.host6) : NULL;
  781. dest->net.v6 = src->net.v6;
  782. dest->u = src->u ? src->u : NULL;
  783. dest->pid = src->pid;
  784. dest->hub = src->hub;
  785. dest->localhub = src->localhub;
  786. dest->disabled = src->disabled;
  787. dest->next = NULL;
  788. }
  789. }
  790. conf_bot *conf_bots_dup(conf_bot *src)
  791. {
  792. conf_bot *ret = NULL;
  793. if (src) {
  794. conf_bot *bot = NULL, *newbot = NULL;
  795. for (bot = src; bot && bot->nick; bot = bot->next) {
  796. newbot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  797. conf_bot_dup(newbot, bot);
  798. list_append((struct list_type **) &(ret), (struct list_type *) newbot);
  799. }
  800. }
  801. return ret;
  802. }
  803. void kill_removed_bots(conf_bot *oldlist, conf_bot *newlist)
  804. {
  805. if (oldlist) {
  806. conf_bot *botold = NULL, *botnew = NULL;
  807. bool found = 0;
  808. struct userrec *u = NULL;
  809. for (botold = oldlist; botold && botold->nick; botold = botold->next) {
  810. found = 0;
  811. for (botnew = newlist; botnew && botnew->nick; botnew = botnew->next) {
  812. if (!egg_strcasecmp(botold->nick, botnew->nick)) {
  813. found = 1;
  814. break;
  815. }
  816. }
  817. if (!found) {
  818. conf_killbot(NULL, botold, SIGKILL);
  819. if ((u = get_user_by_handle(userlist, botold->nick))) {
  820. putlog(LOG_MISC, "*", "Removing '%s' as it has been removed from the binary config.", botold->nick);
  821. if (server_online)
  822. check_this_user(botold->nick, 1, NULL);
  823. if (deluser(botold->nick)) {
  824. /* there is likely NO conf[]
  825. if (conf.bot->hub)
  826. write_userfile(-1);
  827. */
  828. }
  829. }
  830. }
  831. }
  832. }
  833. }
  834. void
  835. fill_conf_bot()
  836. {
  837. if (!conf.bots || !conf.bots->nick)
  838. return;
  839. char *mynick = NULL;
  840. conf_bot *me = NULL;
  841. /* This first clause should actually be obsolete */
  842. if (!used_B && conf.bots && conf.bots->nick) {
  843. mynick = strdup(conf.bots->nick);
  844. strlcpy(origbotname, conf.bots->nick, NICKLEN + 1);
  845. } else
  846. mynick = strdup(origbotname);
  847. sdprintf("mynick: %s", mynick);
  848. for (me = conf.bots; me && me->nick; me = me->next)
  849. if (!egg_strcasecmp(me->nick, mynick))
  850. break;
  851. if (!me || (me->nick && egg_strcasecmp(me->nick, mynick)))
  852. werr(ERR_BADBOT);
  853. free(mynick);
  854. /* for future, we may just want to make this a pointer to ->bots if we do an emech style currentbot-> */
  855. conf.bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  856. conf_bot_dup(conf.bot, me);
  857. }
  858. void
  859. bin_to_conf(void)
  860. {
  861. /* printf("Converting binary data to conf struct\n"); */
  862. conf.uid = atol(settings.uid);
  863. conf.username = strdup(settings.username);
  864. conf.uname = strdup(settings.uname);
  865. conf.datadir = strdup(settings.datadir);
  866. expand_tilde(&conf.datadir);
  867. conf.homedir = strdup(settings.homedir);
  868. conf.binpath = strdup(settings.binpath);
  869. expand_tilde(&conf.binpath);
  870. conf.binname = strdup(settings.binname);
  871. conf.portmin = atol(settings.portmin);
  872. conf.portmax = atol(settings.portmax);
  873. conf.autouname = atoi(settings.autouname);
  874. conf.autocron = atoi(settings.autocron);
  875. conf.watcher = atoi(settings.watcher);
  876. conf.pscloak = atoi(settings.pscloak);
  877. /* BOTS */
  878. {
  879. char *p = NULL, *tmp = NULL, *tmpp = NULL;
  880. tmp = tmpp = strdup(settings.bots);
  881. while ((p = strchr(tmp, ','))) {
  882. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  883. *p++ = 0;
  884. if (!tmp[0])
  885. break;
  886. nick = newsplit(&tmp);
  887. if (!nick || (nick && !nick[0]))
  888. werr(ERR_BADCONF);
  889. if (tmp[0])
  890. ip = newsplit(&tmp);
  891. if (tmp[0])
  892. host = newsplit(&tmp);
  893. if (tmp[0])
  894. ipsix = newsplit(&tmp);
  895. conf_addbot(nick, ip, host, ipsix);
  896. tmp = p++;
  897. }
  898. free(tmpp);
  899. }
  900. Tempfile::FindDir();
  901. if (clear_tmpdir)
  902. clear_tmp(); /* clear out the tmp dir, no matter if we are localhub or not */
  903. conf_checkpids();
  904. tellconf();
  905. }
  906. void conf_add_userlist_bots()
  907. {
  908. conf_bot *bot = NULL;
  909. struct userrec *u = NULL;
  910. struct bot_addr *bi = NULL;
  911. char tmp[81] = "", uhost[UHOSTLEN] = "";
  912. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  913. /* Don't auto-add hubs. */
  914. if (!bot->hub) {
  915. u = get_user_by_handle(userlist, bot->nick);
  916. if (!u) {
  917. putlog(LOG_MISC, "*", "Adding bot '%s' as it has been added to the binary config.", bot->nick);
  918. userlist = adduser(userlist, bot->nick, "none", "-", USER_OP, 1);
  919. u = get_user_by_handle(userlist, bot->nick);
  920. egg_snprintf(tmp, sizeof(tmp), "%li [internal]", now);
  921. set_user(&USERENTRY_ADDED, u, tmp);
  922. bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  923. bi->address = (char *) my_calloc(1, 1);
  924. bi->uplink = (char *) my_calloc(1, 1);
  925. bi->telnet_port = bi->relay_port = 3333;
  926. bi->hublevel = 999;
  927. set_user(&USERENTRY_BOTADDR, u, bi);
  928. }
  929. if (bot->net.ip) {
  930. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.ip);
  931. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  932. addhost_by_handle(bot->nick, uhost);
  933. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", bot->nick, bot->net.ip);
  934. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  935. addhost_by_handle(bot->nick, uhost);
  936. }
  937. if (bot->net.host) {
  938. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.host);
  939. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  940. addhost_by_handle(bot->nick, uhost);
  941. }
  942. if (bot->net.host6) {
  943. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.host6);
  944. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  945. addhost_by_handle(bot->nick, uhost);
  946. }
  947. if (bot->net.ip6) {
  948. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.ip6);
  949. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  950. addhost_by_handle(bot->nick, uhost);
  951. }
  952. }
  953. }
  954. }