conf.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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. (int)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. (int)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, (int)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. // If conf_hubs is blank, revert to pack hubs
  288. if (strlen(settings.conf_hubs)) {
  289. conf.hubs = bd::String(settings.conf_hubs).split(',');
  290. } else {
  291. conf.hubs = bd::String(settings.hubs).split(',');
  292. }
  293. conf.localhub = NULL;
  294. conf.autocron = 1;
  295. conf.features = 0;
  296. conf.portmin = 0;
  297. conf.portmax = 0;
  298. conf.uid = -1;
  299. conf.username = NULL;
  300. conf.homedir = NULL;
  301. conf.datadir = strdup(STR("./..."));
  302. expand_tilde(&conf.datadir);
  303. }
  304. void conf_checkpids(conf_bot *bots, bool all)
  305. {
  306. conf_bot *bot = NULL;
  307. for (bot = bots; bot && bot->nick; bot = bot->next)
  308. if (all || (!all && bot->pid == 0))
  309. bot->pid = checkpid(bot->nick, bot);
  310. }
  311. static char* datafile(const char* prefix, const char* nick) {
  312. static char buf[DIRMAX] = "";
  313. char *tmpnick = NULL, *tmp_ptr = NULL;
  314. tmpnick = tmp_ptr = strdup(nick);
  315. strtolower(tmpnick);
  316. simple_snprintf(buf, sizeof buf, STR("%s/.%s.%s"), conf.datadir, prefix, tmpnick);
  317. free(tmp_ptr);
  318. return buf;
  319. }
  320. /*
  321. * Return the PID of a bot if it is running, otherwise return 0
  322. */
  323. pid_t
  324. checkpid(const char *nick, conf_bot *bot)
  325. {
  326. FILE *f = NULL;
  327. pid_t pid = 0;
  328. char buf[DIRMAX] = "";
  329. char *bufp = datafile("pid", nick);
  330. if (bot && !(bot->pid_file))
  331. bot->pid_file = strdup(bufp);
  332. else if (bot && strcasecmp(bot->pid_file, bufp))
  333. str_redup(&bot->pid_file, bufp);
  334. if ((f = fopen(bufp, "r"))) {
  335. char *pids = NULL;
  336. if (!fgets(buf, sizeof(buf), f)) {
  337. fclose(f);
  338. return 0;
  339. }
  340. fclose(f);
  341. remove_crlf(buf);
  342. if (!buf[0])
  343. return 0;
  344. bufp = buf;
  345. pids = newsplit(&bufp);
  346. if (str_isdigit(pids)) {
  347. pid = atoi(pids);
  348. if (kill(pid, SIGCHLD)) //Problem killing, most likely it's just not running.
  349. pid = 0;
  350. }
  351. if (bufp[0] && pid && can_stat(bufp) && (getpid() == pid) &&
  352. !strncasecmp(nick, origbotnick, HANDLEN)) {
  353. socksfile = strdup(bufp);
  354. return 0;
  355. }
  356. }
  357. return pid;
  358. }
  359. void
  360. conf_addbot(const char *nick, const char *ip, const char *host, const char *ip6)
  361. {
  362. conf_bot *bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  363. bot->next = NULL;
  364. bot->pid_file = NULL;
  365. if (nick[0] == '/') {
  366. bot->disabled = 1;
  367. ++nick;
  368. sdprintf(STR("%s is disabled."), nick);
  369. }
  370. bot->nick = strldup(nick, HANDLEN);
  371. bot->net.ip = NULL;
  372. bot->net.host = NULL;
  373. bot->net.ip6 = NULL;
  374. bot->net.host6 = NULL;
  375. if (host && host[0] == '+') {
  376. ++host;
  377. bot->net.host6 = strdup(host);
  378. } else if (host && !strchr(".*", host[0])) {
  379. bot->net.host = strdup(host);
  380. }
  381. if (ip && !strchr(".*", ip[0])) {
  382. int aftype = is_dotted_ip(ip);
  383. if (aftype == AF_INET)
  384. bot->net.ip = strdup(ip);
  385. #ifdef USE_IPV6
  386. else if (aftype == AF_INET6)
  387. bot->net.ip6 = strdup(ip);
  388. #endif /* USE_IPV6 */
  389. else if (aftype == 0) { /* The idiot used a hostname in place of ip */
  390. if (bot->net.host)
  391. free(bot->net.host);
  392. bot->net.host = strdup(ip);
  393. }
  394. }
  395. #ifdef USE_IPV6
  396. if (ip6 && !strchr(".*", ip6[0]) && is_dotted_ip(ip6) == AF_INET6)
  397. bot->net.ip6 = strdup(ip6);
  398. if (bot->net.ip6 || bot->net.host6)
  399. bot->net.v6 = 1;
  400. #endif /* USE_IPV6 */
  401. if (userlist)
  402. bot->u = get_user_by_handle(userlist, (char*)nick);
  403. else
  404. bot->u = NULL;
  405. // bot->pid = checkpid(nick, bot);
  406. if (is_hub(bot->nick))
  407. bot->hub = 1;
  408. /* not a hub
  409. AND
  410. * no bots added yet (first bot) yet, not disabled.
  411. OR
  412. * bots already listed but we dont have a localhub yet, so we're it!
  413. */
  414. if (!conf.localhub && !bot->hub && !bot->disabled) {
  415. bot->localhub = 1; /* first bot */
  416. conf.localhub = strdup(bot->nick);
  417. conf.localhub_socket = strdup(datafile("sock", conf.localhub));
  418. }
  419. list_append((struct list_type **) &(conf.bots), (struct list_type *) bot);
  420. }
  421. void
  422. free_bot(conf_bot *bot)
  423. {
  424. if (bot) {
  425. list_delete((struct list_type **) &(conf.bots), (struct list_type *) bot);
  426. free(bot->nick);
  427. free(bot->pid_file);
  428. if (bot->net.ip)
  429. free(bot->net.ip);
  430. if (bot->net.host)
  431. free(bot->net.host);
  432. if (bot->net.ip6)
  433. free(bot->net.ip6);
  434. if (bot->net.host6)
  435. free(bot->net.host6);
  436. free(bot);
  437. }
  438. }
  439. int
  440. conf_delbot(char *botn, bool kill)
  441. {
  442. conf_bot *bot = NULL;
  443. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  444. if (!strcasecmp(bot->nick, botn)) { /* found it! */
  445. if (kill) {
  446. bot->pid = checkpid(bot->nick, bot);
  447. conf_killbot(conf.bots, NULL, bot, SIGKILL);
  448. }
  449. free_bot(bot);
  450. return 0;
  451. }
  452. }
  453. return 1;
  454. }
  455. void
  456. free_conf()
  457. {
  458. free_conf_bots(conf.bots);
  459. free_bot(conf.bot);
  460. conf.bot = NULL;
  461. if (conf.localhub)
  462. free(conf.localhub);
  463. if (conf.username)
  464. free(conf.username);
  465. if (conf.datadir)
  466. free(conf.datadir);
  467. if (conf.homedir)
  468. free(conf.homedir);
  469. conf.hubs.clear();
  470. init_conf();
  471. }
  472. void
  473. free_conf_bots(conf_bot *list)
  474. {
  475. if (list) {
  476. conf_bot *bot = NULL, *bot_n = NULL;
  477. for (bot = list; bot; bot = bot_n) {
  478. bot_n = bot->next;
  479. free_bot(bot);
  480. }
  481. list = NULL;
  482. }
  483. }
  484. void prep_homedir(bool error)
  485. {
  486. if (!conf.homedir)
  487. str_redup(&conf.homedir, homedir());
  488. if (error && (!conf.homedir || !conf.homedir[0]))
  489. werr(ERR_NOHOMEDIR);
  490. }
  491. int
  492. parseconf(bool error)
  493. {
  494. if (error && conf.uid == -1 && !conf.homedir)
  495. werr(ERR_NOTINIT);
  496. if (!conf.username)
  497. str_redup(&conf.username, my_username());
  498. if (error && (!conf.username || !conf.username[0]))
  499. werr(ERR_NOUSERNAME);
  500. if (error && conf.uid != (signed) myuid) {
  501. sdprintf(STR("wrong uid, conf: %d :: %d"), conf.uid, myuid);
  502. werr(ERR_WRONGUID);
  503. } else if (!conf.uid)
  504. conf.uid = myuid;
  505. return 0;
  506. }
  507. int
  508. readconf(const char *fname, int bits)
  509. {
  510. int enc = (bits & CONF_ENC) ? 1 : 0;
  511. bd::Stream* stream;
  512. size_t bots = 0;
  513. if (enc) {
  514. const char salt1[] = SALT1;
  515. stream = new EncryptedStream(salt1);
  516. } else
  517. stream = new bd::Stream;
  518. sdprintf(STR("readconf(%s, %d)"), fname, enc);
  519. if (stream->loadFile(fname)) {
  520. delete stream;
  521. fatal(STR("Cannot read config"), 0);
  522. }
  523. conf.hubs.clear();
  524. free_conf_bots(conf.bots);
  525. bd::String line, option;
  526. unsigned short hublevel = 0;
  527. while (stream->tell() < stream->length()) {
  528. line = stream->getline().chomp().trim();
  529. // Skip blank lines
  530. if (!line)
  531. continue;
  532. sdprintf(STR("CONF LINE: %s"), line.c_str());
  533. // !strchr("_`|}][{*/#-+!abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPWRSTUVWXYZ", line[0])) {
  534. /* - uid */
  535. if (line[0] == '!') {
  536. ++line;
  537. line.trim();
  538. option.clear();
  539. if (line.length())
  540. option = newsplit(line);
  541. if (!option || !line)
  542. continue;
  543. // option.toLower();
  544. if (option == STR("autocron")) { /* automatically check/create crontab? */
  545. if (egg_isdigit(line[0]))
  546. conf.autocron = atoi(line.c_str());
  547. } else if (option == STR("username")) { /* shell username */
  548. str_redup(&conf.username, line.c_str());
  549. } else if (option == STR("homedir")) { /* homedir */
  550. str_redup(&conf.homedir, line.c_str());
  551. } else if (option == STR("datadir")) { /* datadir */
  552. str_redup(&conf.datadir, line.c_str());
  553. } else if (option == STR("portmin")) {
  554. if (egg_isdigit(line[0]))
  555. conf.portmin = atoi(line.c_str());
  556. } else if (option == STR("portmax")) {
  557. if (egg_isdigit(line[0]))
  558. conf.portmax = atoi(line.c_str());
  559. } else if (option == STR("uid")) { /* new method uid */
  560. if (str_isdigit(line.c_str()))
  561. conf.uid = atoi(line.c_str());
  562. } else if (option == STR("hub")) {
  563. if (line.split(' ').length() == 3) {
  564. conf.hubs << bd::String::printf("%s %d", line.c_str(), ++hublevel);
  565. }
  566. } else {
  567. putlog(LOG_MISC, "*", STR("Unrecognized config option '%s'"), option.c_str());
  568. }
  569. /* read in portmin */
  570. } else if (line[0] == '>') {
  571. conf.portmin = atoi(newsplit(line).c_str());
  572. } else if (line[0] == '<') {
  573. conf.portmax = atoi(newsplit(line).c_str());
  574. /* now to parse nick/hosts */
  575. } else if (line[0] != '#') {
  576. bd::String nick, host, ip, ipsix;
  577. nick = newsplit(line);
  578. if (!nick)
  579. werr(ERR_BADCONF);
  580. ip = newsplit(line);
  581. host = newsplit(line);
  582. ipsix = newsplit(line);
  583. conf_addbot(nick.c_str(), ip.c_str(), host.c_str(), ipsix.c_str());
  584. ++bots;
  585. }
  586. } /* while(fgets()) */
  587. if (bots >= 5)
  588. werr(ERR_TOOMANYBOTS);
  589. delete stream;
  590. return 0;
  591. }
  592. char s1_9[3] = "",s1_5[3] = "",s1_1[3] = "";
  593. bool hubSort (bd::String hub1, bd::String hub2) {
  594. bd::Array<bd::String> hub1params(static_cast<bd::String>(hub1).split(' '));
  595. bd::Array<bd::String> hub2params(static_cast<bd::String>(hub2).split(' '));
  596. unsigned short hub1level = 99, hub2level = 99;
  597. if (hub1params.length() == 4) {
  598. hub1level = atoi(static_cast<bd::String>(hub1params[3]).c_str());
  599. }
  600. if (hub2params.length() == 4) {
  601. hub2level = atoi(static_cast<bd::String>(hub2params[3]).c_str());
  602. }
  603. return hub1level < hub2level;
  604. }
  605. int
  606. writeconf(char *filename, int fd, int bits)
  607. {
  608. conf_bot *bot = NULL;
  609. int autowrote = 0;
  610. bd::Stream* stream;
  611. if (bits & CONF_ENC) {
  612. const char salt1[] = SALT1;
  613. stream = new EncryptedStream(salt1);
  614. } else
  615. stream = new bd::Stream;
  616. #define comment(text) do { \
  617. if (bits & CONF_COMMENT) \
  618. *stream << bd::String::printf(STR("%s\n"), text); \
  619. } while(0)
  620. char *p = NULL;
  621. comment("");
  622. #define conf_com() do { \
  623. if (do_confedit == CONF_AUTO) { \
  624. comment("# Automatically updated with -C"); \
  625. autowrote = 1; \
  626. } else \
  627. comment("#The correct line follows. (Not auto due to -c)"); \
  628. } while(0)
  629. if ((bits & CONF_COMMENT) && conf.uid != (signed) myuid) {
  630. conf_com();
  631. *stream << bd::String::printf(STR("%s! uid %d\n"), do_confedit == CONF_AUTO ? "" : "#", myuid);
  632. *stream << bd::String::printf(STR("%s! uid %d\n"), do_confedit == CONF_STATIC ? "" : "#", conf.uid);
  633. } else
  634. *stream << bd::String::printf(STR("! uid %d\n"), conf.uid);
  635. comment("");
  636. if (conf.username && my_username() && strcmp(conf.username, my_username())) {
  637. conf_com();
  638. *stream << bd::String::printf(STR("%s! username %s\n"), do_confedit == CONF_AUTO ? "" : "#", my_username());
  639. *stream << bd::String::printf(STR("%s! username %s\n"), do_confedit == CONF_STATIC ? "" : "#", conf.username);
  640. } else
  641. *stream << bd::String::printf(STR("! username %s\n"), conf.username ? conf.username : my_username() ? my_username() : "");
  642. if (conf.homedir && homedir(0) && strcmp(conf.homedir, homedir(0))) {
  643. conf_com();
  644. *stream << bd::String::printf(STR("%s! homedir %s\n"), do_confedit == CONF_AUTO ? "" : "#", homedir(0));
  645. *stream << bd::String::printf(STR("%s! homedir %s\n"), do_confedit == CONF_STATIC ? "" : "#", conf.homedir);
  646. } else
  647. *stream << bd::String::printf(STR("! homedir %s\n"), conf.homedir ? conf.homedir : homedir(0) ? homedir(0) : "");
  648. comment("");
  649. if (conf.datadir && strcmp(conf.datadir, "./...")) {
  650. comment("# datadir should be set to a static directory that is writable");
  651. if (homedir() && strstr(conf.datadir, homedir())) {
  652. p = replace(conf.datadir, homedir(), "~");
  653. *stream << bd::String::printf(STR("! datadir %s\n"), p);
  654. } else if (conf.homedir && strstr(conf.datadir, conf.homedir)) { /* Could be an older homedir */
  655. p = replace(conf.datadir, conf.homedir, "~");
  656. *stream << bd::String::printf(STR("! datadir %s\n"), p);
  657. } else
  658. *stream << bd::String::printf(STR("! datadir %s\n"), conf.datadir);
  659. comment("");
  660. }
  661. if (conf.portmin || conf.portmax) {
  662. comment("# portmin/max are for incoming connections (DCC) [0 for any] (These only make sense for HUBS)");
  663. *stream << bd::String::printf(STR("! portmin %d\n"), conf.portmin);
  664. *stream << bd::String::printf(STR("! portmax %d\n"), conf.portmax);
  665. comment("");
  666. }
  667. if (conf.autocron == 0) {
  668. comment("# Automatically add the bot to crontab?");
  669. *stream << bd::String::printf(STR("! autocron %d\n"), conf.autocron);
  670. comment("");
  671. }
  672. if (conf.hubs.length()) {
  673. comment("# Hubs this bot will connect to");
  674. // Sort hub list by hublevel
  675. bd::Array<bd::String> sortedhubs(conf.hubs);
  676. std::sort(sortedhubs.begin(), sortedhubs.end(), hubSort);
  677. for (size_t idx = 0; idx < sortedhubs.length(); ++idx) {
  678. bd::Array<bd::String> hubparams(static_cast<bd::String>(sortedhubs[idx]).split(' '));
  679. bd::String hubnick(hubparams[0]), address(hubparams[1]);
  680. in_port_t port = atoi(static_cast<bd::String>(hubparams[2]).c_str());
  681. *stream << bd::String::printf(STR("! hub %s %s %d\n"), hubnick.c_str(), address.c_str(), port);
  682. }
  683. comment("");
  684. }
  685. comment("# '|' means OR, [] means the enclosed is optional");
  686. comment("# A '+' in front of HOST means the HOST is ipv6");
  687. comment("# A '/' in front of BOT will disable that bot.");
  688. comment("#[/]BOT IP|* [+]HOST|* [IPV6-IP]");
  689. comment("#bot ip vhost");
  690. comment("#bot2 * vhost");
  691. comment("#bot3 ip");
  692. comment("#bot4 * +ipv6.vhost.com");
  693. comment("#bot5 * * ip:v6:ip:goes:here::");
  694. comment("### Hubs should have their own binary ###");
  695. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  696. *stream << bd::String::printf(STR("%s%s %s %s%s"),
  697. bot->disabled ? "/" : "", bot->nick,
  698. bot->net.ip ? bot->net.ip : "*", bot->net.host6 ? "+" : "",
  699. bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "*"));
  700. if (bot->net.ip6)
  701. *stream << bd::String::printf(STR(" %s"), bot->net.ip6 ? bot->net.ip6 : "");
  702. *stream << "\n";
  703. }
  704. if (fd != -1)
  705. stream->writeFile(fd);
  706. else
  707. stream->writeFile(filename);
  708. delete stream;
  709. return autowrote;
  710. }
  711. void
  712. conf_bot_dup(conf_bot *dest, conf_bot *src)
  713. {
  714. if (dest && src) {
  715. dest->nick = src->nick ? strdup(src->nick) : NULL;
  716. dest->pid_file = src->pid_file ? strdup(src->pid_file) : NULL;
  717. dest->net.ip = src->net.ip ? strdup(src->net.ip) : NULL;
  718. dest->net.host = src->net.host ? strdup(src->net.host) : NULL;
  719. dest->net.ip6 = src->net.ip6 ? strdup(src->net.ip6) : NULL;
  720. dest->net.host6 = src->net.host6 ? strdup(src->net.host6) : NULL;
  721. dest->net.v6 = src->net.v6;
  722. dest->u = src->u ? src->u : NULL;
  723. dest->pid = src->pid;
  724. dest->hub = src->hub;
  725. dest->localhub = src->localhub;
  726. dest->disabled = src->disabled;
  727. dest->next = NULL;
  728. }
  729. }
  730. conf_bot *conf_bots_dup(conf_bot *src)
  731. {
  732. conf_bot *ret = NULL;
  733. if (src) {
  734. conf_bot *bot = NULL, *newbot = NULL;
  735. for (bot = src; bot && bot->nick; bot = bot->next) {
  736. newbot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  737. conf_bot_dup(newbot, bot);
  738. list_append((struct list_type **) &(ret), (struct list_type *) newbot);
  739. }
  740. }
  741. return ret;
  742. }
  743. void deluser_removed_bots(conf_bot *oldlist, conf_bot *newlist)
  744. {
  745. if (oldlist) {
  746. conf_bot *botold = NULL, *botnew = NULL;
  747. bool found = 0;
  748. struct userrec *u = NULL;
  749. for (botold = oldlist; botold && botold->nick; botold = botold->next) {
  750. found = 0;
  751. for (botnew = newlist; botnew && botnew->nick; botnew = botnew->next) {
  752. if (!strcasecmp(botold->nick, botnew->nick)) {
  753. found = 1;
  754. break;
  755. }
  756. }
  757. if (!found && strcasecmp(botold->nick, origbotnick)) { /* Never kill ME.. will handle it elsewhere */
  758. /* No need to kill -- they are signalled and they will die on their own now */
  759. //botold->pid = checkpid(botold->nick, botold);
  760. //conf_killbot(conf.bots, NULL, botold, SIGKILL);
  761. if ((u = get_user_by_handle(userlist, botold->nick))) {
  762. putlog(LOG_MISC, "*", STR("Removing '%s' as it has been removed from the binary config."), botold->nick);
  763. if (server_online)
  764. check_this_user(botold->nick, 1, NULL);
  765. if (deluser(botold->nick)) {
  766. /* there is likely NO conf[]
  767. if (conf.bot->hub)
  768. write_userfile(-1);
  769. */
  770. }
  771. }
  772. }
  773. }
  774. }
  775. }
  776. void
  777. fill_conf_bot(bool fatal)
  778. {
  779. if (!conf.bots || !conf.bots->nick)
  780. return;
  781. char *mynick = NULL;
  782. conf_bot *me = NULL;
  783. /* This first clause should actually be obsolete */
  784. if (!used_B && conf.bots && conf.bots->nick) {
  785. mynick = strdup(conf.bots->nick);
  786. strlcpy(origbotnick, conf.bots->nick, sizeof(origbotnick));
  787. } else
  788. mynick = strldup(origbotnick, HANDLEN);
  789. sdprintf(STR("mynick: %s"), mynick);
  790. for (me = conf.bots; me && me->nick; me = me->next)
  791. if (!strcasecmp(me->nick, mynick))
  792. break;
  793. if (fatal && (!me || (me->nick && strcasecmp(me->nick, mynick))))
  794. werr(ERR_BADBOT);
  795. free(mynick);
  796. if (me) {
  797. if (!me->hub && me->localhub)
  798. sdprintf(STR("I am localhub!"));
  799. /* for future, we may just want to make this a pointer to ->bots if we do an emech style currentbot-> */
  800. conf.bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  801. conf_bot_dup(conf.bot, me);
  802. }
  803. }
  804. void
  805. bin_to_conf(bool error)
  806. {
  807. /* printf("Converting binary data to conf struct\n"); */
  808. conf.features = atol(settings.features);
  809. conf.uid = atol(settings.uid);
  810. if (settings.username[0])
  811. str_redup(&conf.username, settings.username);
  812. str_redup(&conf.datadir, settings.datadir);
  813. if (settings.homedir[0])
  814. str_redup(&conf.homedir, settings.homedir);
  815. conf.portmin = atol(settings.portmin);
  816. conf.portmax = atol(settings.portmax);
  817. conf.autocron = atoi(settings.autocron);
  818. prep_homedir(error);
  819. expand_tilde(&conf.datadir);
  820. /* PARSE/ADD BOTS */
  821. {
  822. char *p = NULL, *tmp = NULL, *tmpp = NULL;
  823. tmp = tmpp = strdup(settings.bots);
  824. while ((p = strchr(tmp, ','))) {
  825. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  826. *p++ = 0;
  827. if (!tmp[0])
  828. break;
  829. nick = newsplit(&tmp);
  830. if (!nick || (nick && !nick[0]))
  831. werr(ERR_BADCONF);
  832. if (tmp[0])
  833. ip = newsplit(&tmp);
  834. if (tmp[0])
  835. host = newsplit(&tmp);
  836. if (tmp[0])
  837. ipsix = newsplit(&tmp);
  838. conf_addbot(nick, ip, host, ipsix);
  839. tmp = p++;
  840. }
  841. free(tmpp);
  842. }
  843. char datadir[PATH_MAX] = "";
  844. if (!realpath(conf.datadir, datadir)) {
  845. ;
  846. }
  847. str_redup(&conf.datadir, datadir);
  848. if (!mkdir_p(conf.datadir) && error)
  849. werr(ERR_DATADIR);
  850. str_redup(&conf.datadir, replace(datadir, dirname(binname), "."));
  851. if (Tempfile::FindDir() == ERROR)
  852. werr(ERR_TMPSTAT);
  853. if (clear_tmpdir)
  854. clear_tmp(); /* clear out the tmp dir, no matter if we are localhub or not */
  855. conf_checkpids(conf.bots);
  856. tellconf();
  857. }
  858. void conf_update_hubs(struct userrec* list) {
  859. if (!conf.bot->hub && !conf.bot->localhub) {
  860. return;
  861. }
  862. bd::Array<bd::String> hubUsers, oldhubs(conf.hubs);
  863. // Count how many hubs there are
  864. for (struct userrec *u = list; u; u = u->next) {
  865. if (bot_hublevel(u) < 999) {
  866. hubUsers << u->handle;
  867. }
  868. }
  869. conf.hubs.clear();
  870. conf.hubs.Reserve(hubUsers.length());
  871. for (size_t idx = 0; idx < hubUsers.length(); ++idx) {
  872. struct userrec *u = get_user_by_handle(list, const_cast<char*>(static_cast<bd::String>(hubUsers[idx]).c_str()));
  873. struct bot_addr *bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  874. conf.hubs << bd::String::printf("%s %s %d %d", u->handle, bi->address, bi->telnet_port, bi->hublevel);
  875. }
  876. // Only rewrite binary / notify bots if the hubs changed
  877. if (conf.hubs == oldhubs) {
  878. return;
  879. }
  880. if (conf.bot->hub || conf.bot->localhub) {
  881. /* rewrite our binary */
  882. conf_to_bin(&conf, 0, -1);
  883. /* Now signal all of the old bots with SIGUSR1
  884. * They will auto die and determine new localhub, etc..
  885. */
  886. conf_checkpids(conf.bots);
  887. conf_killbot(conf.bots, conf.bot->nick, NULL, SIGUSR1, 1); /* Don't kill me. */
  888. }
  889. }
  890. void conf_add_userlist_bots()
  891. {
  892. conf_bot *bot = NULL;
  893. struct userrec *u = NULL;
  894. struct bot_addr *bi = NULL;
  895. char tmp[81] = "", uhost[UHOSTLEN] = "";
  896. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  897. /* Don't auto-add hubs. */
  898. if (!bot->hub && tands > 0 && !bot->disabled) {
  899. u = get_user_by_handle(userlist, bot->nick);
  900. if (!u) {
  901. putlog(LOG_MISC, "*", STR("Adding bot '%s' as it has been added to the binary config."), bot->nick);
  902. userlist = adduser(userlist, bot->nick, "none", "-", USER_OP, 1);
  903. u = get_user_by_handle(userlist, bot->nick);
  904. simple_snprintf(tmp, sizeof(tmp), "%li %s", (long)now, conf.bot->nick);
  905. set_user(&USERENTRY_ADDED, u, tmp);
  906. bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  907. bi->address = (char *) my_calloc(1, 1);
  908. bi->uplink = (char *) my_calloc(1, 1);
  909. bi->telnet_port = bi->relay_port = 3333;
  910. bi->hublevel = 999;
  911. set_user(&USERENTRY_BOTADDR, u, bi);
  912. }
  913. if (bot->net.ip && strcmp(bot->net.ip, "0.0.0.0")) {
  914. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.ip);
  915. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  916. addhost_by_handle(bot->nick, uhost);
  917. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.ip);
  918. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  919. addhost_by_handle(bot->nick, uhost);
  920. }
  921. if (bot->net.host) {
  922. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.host);
  923. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  924. addhost_by_handle(bot->nick, uhost);
  925. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.host);
  926. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  927. addhost_by_handle(bot->nick, uhost);
  928. }
  929. if (bot->net.host6) {
  930. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.host6);
  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", conf.username, bot->net.host6);
  934. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  935. addhost_by_handle(bot->nick, uhost);
  936. }
  937. if (bot->net.ip6 && strcmp(bot->net.ip6, "::")) {
  938. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.ip6);
  939. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  940. addhost_by_handle(bot->nick, uhost);
  941. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.ip6);
  942. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  943. addhost_by_handle(bot->nick, uhost);
  944. }
  945. }
  946. }
  947. }
  948. conf_bot *conf_getlocalhub(conf_bot *bots) {
  949. if (!bots)
  950. return NULL;
  951. conf_bot *localhub = bots;
  952. if (localhub->disabled)
  953. while (localhub && localhub->disabled)
  954. localhub = localhub->next;
  955. if (!localhub) return NULL;
  956. return !localhub->disabled ? localhub : NULL;
  957. }
  958. void conf_setmypid(pid_t pid) {
  959. conf.bot->pid = pid;
  960. conf_bot *bot = conf.bots;
  961. if (conf.bots) {
  962. for (; bot && strcasecmp(bot->nick, conf.bot->nick); bot = bot->next)
  963. ;
  964. if (bot)
  965. bot->pid = pid;
  966. }
  967. }