conf.c 33 KB

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