conf.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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(STR("tempdir: %s\n"), replace(tempdir, conf.homedir, "~"));
  45. sdprintf(STR("features: %d\n"), conf.features);
  46. sdprintf(STR("uid: %d\n"), conf.uid);
  47. sdprintf(STR("uname: %s\n"), conf.uname);
  48. sdprintf(STR("homedir: %s\n"), conf.homedir);
  49. sdprintf(STR("username: %s\n"), conf.username);
  50. sdprintf(STR("binpath: %s\n"), replace(conf.binpath, conf.homedir, "~"));
  51. sdprintf(STR("binname: %s\n"), conf.binname);
  52. sdprintf(STR("datadir: %s\n"), replace(conf.datadir, conf.homedir, "~"));
  53. sdprintf(STR("portmin: %d\n"), conf.portmin);
  54. sdprintf(STR("portmax: %d\n"), conf.portmax);
  55. sdprintf(STR("pscloak: %d\n"), conf.pscloak);
  56. sdprintf(STR("autocron: %d\n"), conf.autocron);
  57. sdprintf(STR("autouname: %d\n"), conf.autouname);
  58. sdprintf(STR("watcher: %d\n"), conf.watcher);
  59. sdprintf(STR("bots:\n"));
  60. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  61. i++;
  62. sdprintf(STR("%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(STR("me:\n"));
  73. sdprintf(STR("%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(STR(" ... 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. const 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(STR("Cannot stat tempfile"), 0);
  205. /* Okay, edit the file */
  206. if ((!((editor = getenv(STR("EDITOR"))) && strlen(editor)))
  207. && (!((editor = getenv(STR("VISUAL"))) && strlen(editor)))
  208. ) {
  209. editor = STR("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(STR("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, (char*)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, STR("waitpid() failed waiting for PID %d from \"%s\": %s\n"), pid, editor, strerror(errno));
  251. } else if (xpid != pid) {
  252. fprintf(stderr, STR("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, STR("\"%s\" exited with status %d\n"), editor, WEXITSTATUS(waiter));
  258. goto fatal;
  259. } else if (WIFSIGNALED(waiter)) {
  260. fprintf(stderr, STR("\"%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(STR("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(STR("* 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, STR("wraith."), 7) && strchr(p, '-'))
  341. conf.binname = strdup(STR("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(STR("./..."));
  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, STR("%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[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(STR("%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 && !strchr(".*", host[0])) {
  421. bot->net.host = strdup(host);
  422. }
  423. if (ip && !strchr(".*", ip[0])) {
  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. else if (aftype == 0) { /* The idiot used a hostname in place of ip */
  432. if (bot->net.host)
  433. free(bot->net.host);
  434. bot->net.host = strdup(ip);
  435. }
  436. }
  437. #ifdef USE_IPV6
  438. if (ip6 && !strchr(".*", ip6[0]) && is_dotted_ip(ip6) == AF_INET6)
  439. bot->net.ip6 = strdup(ip6);
  440. if (bot->net.ip6 || bot->net.host6)
  441. bot->net.v6 = 1;
  442. #endif /* USE_IPV6 */
  443. if (userlist)
  444. bot->u = get_user_by_handle(userlist, nick);
  445. else
  446. bot->u = NULL;
  447. // bot->pid = checkpid(nick, bot);
  448. if (settings.hubs && is_hub(bot->nick))
  449. bot->hub = 1;
  450. /* not a hub
  451. AND
  452. * no bots added yet (first bot) yet, not disabled.
  453. OR
  454. * bots already listed but we dont have a localhub yet, so we're it!
  455. */
  456. if (!conf.localhub && !bot->hub && !bot->disabled) {
  457. bot->localhub = 1; /* first bot */
  458. conf.localhub = strdup(bot->nick);
  459. }
  460. list_append((struct list_type **) &(conf.bots), (struct list_type *) bot);
  461. }
  462. void
  463. free_bot(conf_bot *bot)
  464. {
  465. if (bot) {
  466. list_delete((struct list_type **) &(conf.bots), (struct list_type *) bot);
  467. free(bot->nick);
  468. free(bot->pid_file);
  469. if (bot->net.ip)
  470. free(bot->net.ip);
  471. if (bot->net.host)
  472. free(bot->net.host);
  473. if (bot->net.ip6)
  474. free(bot->net.ip6);
  475. if (bot->net.host6)
  476. free(bot->net.host6);
  477. free(bot);
  478. }
  479. }
  480. int
  481. conf_delbot(char *botn, bool kill)
  482. {
  483. conf_bot *bot = NULL;
  484. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  485. if (!egg_strcasecmp(bot->nick, botn)) { /* found it! */
  486. if (kill) {
  487. bot->pid = checkpid(bot->nick, bot);
  488. conf_killbot(conf.bots, NULL, bot, SIGKILL);
  489. }
  490. free_bot(bot);
  491. return 0;
  492. }
  493. }
  494. return 1;
  495. }
  496. void
  497. free_conf()
  498. {
  499. free_conf_bots(conf.bots);
  500. free_bot(conf.bot);
  501. conf.bot = NULL;
  502. if (conf.localhub)
  503. free(conf.localhub);
  504. if (conf.uname)
  505. free(conf.uname);
  506. if (conf.username)
  507. free(conf.username);
  508. if (conf.datadir)
  509. free(conf.datadir);
  510. if (conf.homedir)
  511. free(conf.homedir);
  512. if (conf.binname)
  513. free(conf.binname);
  514. if (conf.binpath)
  515. free(conf.binpath);
  516. init_conf();
  517. }
  518. void
  519. free_conf_bots(conf_bot *list)
  520. {
  521. if (list) {
  522. conf_bot *bot = NULL, *bot_n = NULL;
  523. for (bot = list; bot; bot = bot_n) {
  524. bot_n = bot->next;
  525. free_bot(bot);
  526. }
  527. list = NULL;
  528. }
  529. }
  530. void prep_homedir(bool error)
  531. {
  532. if (!conf.homedir)
  533. str_redup(&conf.homedir, homedir());
  534. if (error && (!conf.homedir || !conf.homedir[0]))
  535. werr(ERR_NOHOMEDIR);
  536. }
  537. int
  538. parseconf(bool error)
  539. {
  540. if (error && conf.uid == -1 && !conf.uname)
  541. werr(ERR_NOTINIT);
  542. if (!conf.username)
  543. str_redup(&conf.username, my_username());
  544. if (error && (!conf.username || !conf.username[0]))
  545. werr(ERR_NOUSERNAME);
  546. #ifndef CYGWIN_HACKS
  547. if (error && conf.uid != (signed) myuid) {
  548. sdprintf(STR("wrong uid, conf: %d :: %d"), conf.uid, myuid);
  549. werr(ERR_WRONGUID);
  550. } else if (!conf.uid)
  551. conf.uid = myuid;
  552. if (conf.uname && strcmp(conf.uname, my_uname()) && !conf.autouname) {
  553. baduname(conf.uname, my_uname()); /* its not auto, and its not RIGHT, bail out. */
  554. sdprintf(("wrong uname, conf: %s :: %s"), conf.uname, my_uname());
  555. if (error)
  556. werr(ERR_WRONGUNAME);
  557. } else if (conf.uname && conf.autouname) { /* if autouname, dont bother comparing, just set uname to output */
  558. str_redup(&conf.uname, my_uname());
  559. } else if (!conf.uname) { /* if not set, then just set it, wont happen again next time... */
  560. conf.uname = strdup(my_uname());
  561. }
  562. #endif /* !CYGWIN_HACKS */
  563. return 0;
  564. }
  565. int
  566. readconf(const char *fname, int bits)
  567. {
  568. FILE *f = NULL;
  569. int i = 0, enc = (bits & CONF_ENC) ? 1 : 0;
  570. char *inbuf = NULL;
  571. const char salt1[] = SALT1;
  572. sdprintf(STR("readconf(%s, %d)"), fname, enc);
  573. Context;
  574. if (!(f = fopen(fname, "r")))
  575. fatal(STR("Cannot read config"), 0);
  576. free_conf_bots(conf.bots);
  577. inbuf = (char *) my_calloc(1, 201);
  578. while (fgets(inbuf, 201, f) != NULL) {
  579. char *line = NULL, *temp_ptr = NULL;
  580. remove_crlf(inbuf);
  581. if (enc)
  582. line = temp_ptr = decrypt_string(salt1, inbuf);
  583. else
  584. line = inbuf;
  585. if ((line && !line[0]) || line[0] == '\n') {
  586. if (enc)
  587. free(line);
  588. continue;
  589. }
  590. i++;
  591. rmspace(line);
  592. sdprintf(STR("CONF LINE: %s"), line);
  593. // !strchr("_`|}][{*/#-+!abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPWRSTUVWXYZ", line[0])) {
  594. if (enc && line[0] > '~') {
  595. sdprintf(STR("line %d, char %c "), i, line[0]);
  596. fatal(STR("Bad encryption"), 0);
  597. } else { /* line is good to parse */
  598. /* - uid */
  599. if (line[0] == '-') {
  600. newsplit(&line);
  601. if (conf.uid == -1)
  602. conf.uid = atoi(line);
  603. /* + uname */
  604. } else if (line[0] == '+') {
  605. newsplit(&line);
  606. if (!conf.uname)
  607. conf.uname = strdup(line);
  608. /* ! is misc options */
  609. } else if (line[0] == '!') {
  610. char *option = NULL;
  611. /* Only newplit if they did '! option' */
  612. if (line[1] == ' ')
  613. newsplit(&line);
  614. else
  615. ++line;
  616. if (line[0])
  617. option = newsplit(&line);
  618. if (!option || !line[0])
  619. continue;
  620. if (!egg_strcasecmp(option, STR("autocron"))) { /* automatically check/create crontab? */
  621. if (egg_isdigit(line[0]))
  622. conf.autocron = atoi(line);
  623. } else if (!egg_strcasecmp(option, STR("autouname"))) { /* auto update uname contents? */
  624. if (egg_isdigit(line[0]))
  625. conf.autouname = atoi(line);
  626. } else if (!egg_strcasecmp(option, STR("username"))) { /* shell username */
  627. str_redup(&conf.username, line);
  628. } else if (!egg_strcasecmp(option, STR("homedir"))) { /* homedir */
  629. str_redup(&conf.homedir, line);
  630. } else if (!egg_strcasecmp(option, STR("datadir"))) { /* datadir */
  631. str_redup(&conf.datadir, line);
  632. } else if (!egg_strcasecmp(option, STR("binpath"))) { /* path that the binary should move to? */
  633. str_redup(&conf.binpath, line);
  634. } else if (!egg_strcasecmp(option, STR("binname"))) { /* filename of the binary? */
  635. str_redup(&conf.binname, line);
  636. } else if (!egg_strcasecmp(option, STR("portmin"))) {
  637. if (egg_isdigit(line[0]))
  638. conf.portmin = atoi(line);
  639. } else if (!egg_strcasecmp(option, STR("portmax"))) {
  640. if (egg_isdigit(line[0]))
  641. conf.portmax = atoi(line);
  642. } else if (!egg_strcasecmp(option, STR("pscloak"))) { /* should bots on this shell pscloak? */
  643. if (egg_isdigit(line[0]))
  644. conf.pscloak = atoi(line);
  645. } else if (!egg_strcasecmp(option, STR("uid"))) { /* new method uid */
  646. if (str_isdigit(line))
  647. conf.uid = atoi(line);
  648. } else if (!egg_strcasecmp(option, STR("uname"))) { /* new method uname */
  649. str_redup(&conf.uname, line);
  650. } else if (!egg_strcasecmp(option, STR("watcher"))) {
  651. if (egg_isdigit(line[0]))
  652. conf.watcher = atoi(line);
  653. } else {
  654. putlog(LOG_MISC, "*", STR("Unrecognized config option '%s'"), option);
  655. }
  656. /* read in portmin */
  657. } else if (line[0] == '>') {
  658. newsplit(&line);
  659. conf.portmin = atoi(line);
  660. } else if (line[0] == '<') {
  661. newsplit(&line);
  662. conf.portmax = atoi(line);
  663. /* now to parse nick/hosts */
  664. } else if (line[0] != '#') {
  665. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  666. nick = newsplit(&line);
  667. if (!nick || (nick && !nick[0]))
  668. werr(ERR_BADCONF);
  669. if (line[0])
  670. ip = newsplit(&line);
  671. if (line[0])
  672. host = newsplit(&line);
  673. if (line[0])
  674. ipsix = newsplit(&line);
  675. conf_addbot(nick, ip, host, ipsix);
  676. }
  677. }
  678. inbuf[0] = 0;
  679. if (enc)
  680. free(temp_ptr);
  681. } /* while(fgets()) */
  682. fclose(f);
  683. free(inbuf);
  684. return 0;
  685. }
  686. char s1_9[3] = "",s1_5[3] = "",s1_1[3] = "";
  687. int
  688. writeconf(char *filename, FILE * stream, int bits)
  689. {
  690. FILE *f = NULL;
  691. conf_bot *bot = NULL;
  692. int (*my_write) (FILE *, const char *, ... ) = NULL;
  693. int autowrote = 0;
  694. if (bits & CONF_ENC)
  695. my_write = lfprintf;
  696. else if (!(bits & CONF_ENC))
  697. my_write = fprintf;
  698. #define comment(text) do { \
  699. if (bits & CONF_COMMENT) \
  700. my_write(f, STR("%s\n"), text); \
  701. } while(0)
  702. if (stream) {
  703. f = stream;
  704. } else if (filename) {
  705. if (!(f = fopen(filename, "w")))
  706. return 1;
  707. }
  708. #ifndef CYGWIN_HACKS
  709. char *p = NULL;
  710. comment("");
  711. comment("# Lines beginning with # are what the preceeding line SHOULD be");
  712. comment("# They are simply comments and are not parsed at all.\n");
  713. #define conf_com() do { \
  714. if (do_confedit == CONF_AUTO) { \
  715. comment("# Automatically updated with -C"); \
  716. autowrote = 1; \
  717. } else \
  718. comment("#The correct line follows. (Not auto due to -c)"); \
  719. } while(0)
  720. if ((bits & CONF_COMMENT) && conf.uid != (signed) myuid) {
  721. conf_com();
  722. my_write(f, STR("%s! uid %d\n"), do_confedit == CONF_AUTO ? "" : "#", myuid);
  723. my_write(f, STR("%s! uid %d\n"), do_confedit == CONF_STATIC ? "" : "#", conf.uid);
  724. } else
  725. my_write(f, STR("! uid %d\n"), conf.uid);
  726. if (!conf.uname || (conf.uname && conf.autouname && strcmp(conf.uname, my_uname()))) {
  727. autowrote = 1;
  728. if (conf.uname)
  729. comment("# autouname is ON");
  730. else
  731. comment("# Automatically updated empty uname");
  732. my_write(f, STR("! uname %s\n"), my_uname());
  733. if (conf.uname)
  734. my_write(f, STR("#! uname %s\n"), conf.uname);
  735. } else if (conf.uname && !conf.autouname && strcmp(conf.uname, my_uname())) {
  736. conf_com();
  737. my_write(f, STR("%s! uname %s\n"), do_confedit == CONF_AUTO ? "" : "#", my_uname());
  738. my_write(f, STR("%s! uname %s\n"), do_confedit == CONF_STATIC ? "" : "#", conf.uname);
  739. } else
  740. my_write(f, STR("! uname %s\n"), conf.uname);
  741. comment("");
  742. if (conf.username && my_username() && strcmp(conf.username, my_username())) {
  743. conf_com();
  744. my_write(f, STR("%s! username %s\n"), do_confedit == CONF_AUTO ? "" : "#", my_username());
  745. my_write(f, STR("%s! username %s\n"), do_confedit == CONF_STATIC ? "" : "#", conf.username);
  746. } else
  747. my_write(f, STR("! username %s\n"), conf.username ? conf.username : my_username() ? my_username() : "");
  748. if (conf.homedir && homedir(0) && strcmp(conf.homedir, homedir(0))) {
  749. conf_com();
  750. my_write(f, STR("%s! homedir %s\n"), do_confedit == CONF_AUTO ? "" : "#", homedir(0));
  751. my_write(f, STR("%s! homedir %s\n"), do_confedit == CONF_STATIC ? "" : "#", conf.homedir);
  752. } else
  753. my_write(f, STR("! homedir %s\n"), conf.homedir ? conf.homedir : homedir(0) ? homedir(0) : "");
  754. comment("\n# binpath needs to be full path unless it begins with '~', which uses 'homedir', ie, '~/'");
  755. if (homedir() && strstr(conf.binpath, homedir())) {
  756. p = replace(conf.binpath, homedir(), "~");
  757. my_write(f, STR("! binpath %s\n"), p);
  758. } else if (conf.homedir && strstr(conf.binpath, conf.homedir)) { /* Could be an older homedir */
  759. p = replace(conf.binpath, conf.homedir, "~");
  760. my_write(f, STR("! binpath %s\n"), p);
  761. } else
  762. my_write(f, STR("! binpath %s\n"), conf.binpath);
  763. comment("# binname is relative to binpath, if you change this, you'll need to manually remove the old one from crontab.");
  764. my_write(f, STR("! binname %s\n"), conf.binname);
  765. comment("");
  766. if (conf.datadir && strcmp(conf.datadir, "./...")) {
  767. comment("# datadir should be set to a static directory that is writable");
  768. if (homedir() && strstr(conf.datadir, homedir())) {
  769. p = replace(conf.datadir, homedir(), "~");
  770. my_write(f, STR("! datadir %s\n"), p);
  771. } else if (conf.homedir && strstr(conf.datadir, conf.homedir)) { /* Could be an older homedir */
  772. p = replace(conf.datadir, conf.homedir, "~");
  773. my_write(f, STR("! datadir %s\n"), p);
  774. } else
  775. my_write(f, STR("! datadir %s\n"), conf.datadir);
  776. comment("");
  777. }
  778. if (conf.portmin || conf.portmax) {
  779. comment("# portmin/max are for incoming connections (DCC) [0 for any] (These only make sense for HUBS)");
  780. my_write(f, STR("! portmin %d\n"), conf.portmin);
  781. my_write(f, STR("! portmax %d\n"), conf.portmax);
  782. comment("");
  783. }
  784. if (conf.pscloak) {
  785. comment("# Attempt to \"cloak\" the process name in `ps` for Linux?");
  786. my_write(f, STR("! pscloak %d\n"), conf.pscloak);
  787. comment("");
  788. }
  789. if (conf.autocron == 0) {
  790. comment("# Automatically add the bot to crontab?");
  791. my_write(f, STR("! autocron %d\n"), conf.autocron);
  792. comment("");
  793. }
  794. if (conf.autouname) {
  795. comment("# Automatically update 'uname' if it changes? (DANGEROUS)");
  796. my_write(f, STR("! autouname %d\n"), conf.autouname);
  797. comment("");
  798. }
  799. #ifdef NO
  800. comment("# This will spawn a child process for EACH BOT that will block ALL process hijackers.");
  801. my_write(f, STR("! watcher %d\n"), conf.watcher);
  802. comment("");
  803. #endif
  804. comment("# '|' means OR, [] means the enclosed is optional");
  805. comment("# A '+' in front of HOST means the HOST is ipv6");
  806. comment("# A '/' in front of BOT will disable that bot.");
  807. comment("#[/]BOT IP|* [+]HOST|* [IPV6-IP]");
  808. comment("#bot ip vhost");
  809. comment("#bot2 * vhost");
  810. comment("#bot3 ip");
  811. comment("#bot4 * +ipv6.vhost.com");
  812. comment("#bot5 * * ip:v6:ip:goes:here::");
  813. comment("### Hubs should have their own binary ###");
  814. #endif /* CYGWIN_HACKS */
  815. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  816. my_write(f, STR("%s%s %s %s%s %s\n"),
  817. bot->disabled ? "/" : "", bot->nick,
  818. bot->net.ip ? bot->net.ip : "*", bot->net.host6 ? "+" : "",
  819. bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "*"), bot->net.ip6 ? bot->net.ip6 : "");
  820. }
  821. fflush(f);
  822. if (!stream)
  823. fclose(f);
  824. return autowrote;
  825. }
  826. void
  827. conf_bot_dup(conf_bot *dest, conf_bot *src)
  828. {
  829. if (dest && src) {
  830. dest->nick = src->nick ? strdup(src->nick) : NULL;
  831. dest->pid_file = src->pid_file ? strdup(src->pid_file) : NULL;
  832. dest->net.ip = src->net.ip ? strdup(src->net.ip) : NULL;
  833. dest->net.host = src->net.host ? strdup(src->net.host) : NULL;
  834. dest->net.ip6 = src->net.ip6 ? strdup(src->net.ip6) : NULL;
  835. dest->net.host6 = src->net.host6 ? strdup(src->net.host6) : NULL;
  836. dest->net.v6 = src->net.v6;
  837. dest->u = src->u ? src->u : NULL;
  838. dest->pid = src->pid;
  839. dest->hub = src->hub;
  840. dest->localhub = src->localhub;
  841. dest->disabled = src->disabled;
  842. dest->next = NULL;
  843. }
  844. }
  845. conf_bot *conf_bots_dup(conf_bot *src)
  846. {
  847. conf_bot *ret = NULL;
  848. if (src) {
  849. conf_bot *bot = NULL, *newbot = NULL;
  850. for (bot = src; bot && bot->nick; bot = bot->next) {
  851. newbot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  852. conf_bot_dup(newbot, bot);
  853. list_append((struct list_type **) &(ret), (struct list_type *) newbot);
  854. }
  855. }
  856. return ret;
  857. }
  858. void deluser_removed_bots(conf_bot *oldlist, conf_bot *newlist)
  859. {
  860. if (oldlist) {
  861. conf_bot *botold = NULL, *botnew = NULL;
  862. bool found = 0;
  863. struct userrec *u = NULL;
  864. for (botold = oldlist; botold && botold->nick; botold = botold->next) {
  865. found = 0;
  866. for (botnew = newlist; botnew && botnew->nick; botnew = botnew->next) {
  867. if (!egg_strcasecmp(botold->nick, botnew->nick)) {
  868. found = 1;
  869. break;
  870. }
  871. }
  872. if (!found && egg_strcasecmp(botold->nick, origbotnick)) { /* Never kill ME.. will handle it elsewhere */
  873. /* No need to kill -- they are signalled and they will die on their own now */
  874. //botold->pid = checkpid(botold->nick, botold);
  875. //conf_killbot(conf.bots, NULL, botold, SIGKILL);
  876. if ((u = get_user_by_handle(userlist, botold->nick))) {
  877. putlog(LOG_MISC, "*", STR("Removing '%s' as it has been removed from the binary config."), botold->nick);
  878. if (server_online)
  879. check_this_user(botold->nick, 1, NULL);
  880. if (deluser(botold->nick)) {
  881. /* there is likely NO conf[]
  882. if (conf.bot->hub)
  883. write_userfile(-1);
  884. */
  885. }
  886. }
  887. }
  888. }
  889. }
  890. }
  891. void
  892. fill_conf_bot(bool fatal)
  893. {
  894. if (!conf.bots || !conf.bots->nick)
  895. return;
  896. char *mynick = NULL;
  897. conf_bot *me = NULL;
  898. /* This first clause should actually be obsolete */
  899. if (!used_B && conf.bots && conf.bots->nick) {
  900. mynick = strdup(conf.bots->nick);
  901. strlcpy(origbotnick, conf.bots->nick, HANDLEN + 1);
  902. } else
  903. mynick = strldup(origbotnick, HANDLEN);
  904. sdprintf(STR("mynick: %s"), mynick);
  905. for (me = conf.bots; me && me->nick; me = me->next)
  906. if (!egg_strcasecmp(me->nick, mynick))
  907. break;
  908. if (fatal && (!me || (me->nick && egg_strcasecmp(me->nick, mynick))))
  909. werr(ERR_BADBOT);
  910. free(mynick);
  911. if (me) {
  912. if (!me->hub && me->localhub)
  913. sdprintf(STR("I am localhub!"));
  914. /* for future, we may just want to make this a pointer to ->bots if we do an emech style currentbot-> */
  915. conf.bot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  916. conf_bot_dup(conf.bot, me);
  917. }
  918. }
  919. void
  920. bin_to_conf(bool error)
  921. {
  922. /* printf("Converting binary data to conf struct\n"); */
  923. conf.features = atol(settings.features);
  924. conf.uid = atol(settings.uid);
  925. if (settings.username[0])
  926. str_redup(&conf.username, settings.username);
  927. str_redup(&conf.uname, settings.uname);
  928. str_redup(&conf.datadir, settings.datadir);
  929. if (settings.homedir[0])
  930. str_redup(&conf.homedir, settings.homedir);
  931. str_redup(&conf.binpath, settings.binpath);
  932. str_redup(&conf.binname, settings.binname);
  933. conf.portmin = atol(settings.portmin);
  934. conf.portmax = atol(settings.portmax);
  935. conf.autouname = atoi(settings.autouname);
  936. conf.autocron = atoi(settings.autocron);
  937. conf.watcher = atoi(settings.watcher);
  938. conf.pscloak = atoi(settings.pscloak);
  939. prep_homedir(error);
  940. expand_tilde(&conf.datadir);
  941. expand_tilde(&conf.binpath);
  942. /* PARSE/ADD BOTS */
  943. {
  944. char *p = NULL, *tmp = NULL, *tmpp = NULL;
  945. tmp = tmpp = strdup(settings.bots);
  946. while ((p = strchr(tmp, ','))) {
  947. char *nick = NULL, *host = NULL, *ip = NULL, *ipsix = NULL;
  948. *p++ = 0;
  949. if (!tmp[0])
  950. break;
  951. nick = newsplit(&tmp);
  952. if (!nick || (nick && !nick[0]))
  953. werr(ERR_BADCONF);
  954. if (tmp[0])
  955. ip = newsplit(&tmp);
  956. if (tmp[0])
  957. host = newsplit(&tmp);
  958. if (tmp[0])
  959. ipsix = newsplit(&tmp);
  960. conf_addbot(nick, ip, host, ipsix);
  961. tmp = p++;
  962. }
  963. free(tmpp);
  964. }
  965. char datadir[PATH_MAX] = "";
  966. realpath(conf.datadir, datadir);
  967. str_redup(&conf.datadir, datadir);
  968. if (!mkdir_p(conf.datadir) && error)
  969. werr(ERR_DATADIR);
  970. str_redup(&conf.datadir, replace(datadir, conf.binpath, "."));
  971. if (Tempfile::FindDir() == ERROR)
  972. werr(ERR_TMPSTAT);
  973. if (clear_tmpdir)
  974. clear_tmp(); /* clear out the tmp dir, no matter if we are localhub or not */
  975. conf_checkpids(conf.bots);
  976. tellconf();
  977. }
  978. void conf_add_userlist_bots()
  979. {
  980. conf_bot *bot = NULL;
  981. struct userrec *u = NULL;
  982. struct bot_addr *bi = NULL;
  983. char tmp[81] = "", uhost[UHOSTLEN] = "";
  984. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  985. /* Don't auto-add hubs. */
  986. if (!bot->hub && tands > 0 && !bot->disabled) {
  987. u = get_user_by_handle(userlist, bot->nick);
  988. if (!u) {
  989. putlog(LOG_MISC, "*", STR("Adding bot '%s' as it has been added to the binary config."), bot->nick);
  990. userlist = adduser(userlist, bot->nick, "none", "-", USER_OP, 1);
  991. u = get_user_by_handle(userlist, bot->nick);
  992. egg_snprintf(tmp, sizeof(tmp), "%li [internal]", (long)now);
  993. set_user(&USERENTRY_ADDED, u, tmp);
  994. bi = (struct bot_addr *) my_calloc(1, sizeof(struct bot_addr));
  995. bi->address = (char *) my_calloc(1, 1);
  996. bi->uplink = (char *) my_calloc(1, 1);
  997. bi->telnet_port = bi->relay_port = 3333;
  998. bi->hublevel = 999;
  999. set_user(&USERENTRY_BOTADDR, u, bi);
  1000. }
  1001. if (bot->net.ip) {
  1002. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.ip);
  1003. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  1004. addhost_by_handle(bot->nick, uhost);
  1005. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.ip);
  1006. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  1007. addhost_by_handle(bot->nick, uhost);
  1008. }
  1009. if (bot->net.host) {
  1010. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.host);
  1011. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  1012. addhost_by_handle(bot->nick, uhost);
  1013. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.host);
  1014. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  1015. addhost_by_handle(bot->nick, uhost);
  1016. }
  1017. if (bot->net.host6) {
  1018. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.host6);
  1019. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  1020. addhost_by_handle(bot->nick, uhost);
  1021. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.host6);
  1022. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  1023. addhost_by_handle(bot->nick, uhost);
  1024. }
  1025. if (bot->net.ip6) {
  1026. simple_snprintf(uhost, sizeof(uhost), "*!%s@%s", conf.username, bot->net.ip6);
  1027. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  1028. addhost_by_handle(bot->nick, uhost);
  1029. simple_snprintf(uhost, sizeof(uhost), "*!~%s@%s", conf.username, bot->net.ip6);
  1030. if (!user_has_host(NULL, u, uhost) && !host_conflicts(uhost))
  1031. addhost_by_handle(bot->nick, uhost);
  1032. }
  1033. }
  1034. }
  1035. }
  1036. conf_bot *conf_getlocalhub(conf_bot *bots) {
  1037. if (!bots)
  1038. return NULL;
  1039. conf_bot *localhub = bots;
  1040. if (localhub->disabled)
  1041. while (localhub && localhub->disabled)
  1042. localhub = localhub->next;
  1043. if (!localhub) return NULL;
  1044. return !localhub->disabled ? localhub : NULL;
  1045. }
  1046. void conf_setmypid(pid_t pid) {
  1047. conf.bot->pid = pid;
  1048. conf_bot *bot = conf.bots;
  1049. if (conf.bots) {
  1050. for (; bot && egg_strcasecmp(bot->nick, conf.bot->nick); bot = bot->next)
  1051. ;
  1052. if (bot)
  1053. bot->pid = pid;
  1054. }
  1055. }