conf.c 28 KB

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