conf.c 33 KB

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