conf.c 33 KB

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