conf.c 35 KB

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