binary.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /* I hereby release this into the Public Domain - Bryan Drewery */
  2. /*
  3. * binary.c -- handles:
  4. * misc update functions
  5. * md5 hash verifying
  6. *
  7. */
  8. #include "common.h"
  9. #include "binary.h"
  10. #include "settings.h"
  11. #include "crypt.h"
  12. #include "shell.h"
  13. #include "misc.h"
  14. #include "main.h"
  15. #include "misc_file.h"
  16. #include "tandem.h"
  17. #include "botnet.h"
  18. #include "userrec.h"
  19. #include <sys/wait.h>
  20. #include <sys/types.h>
  21. #include <sys/mman.h>
  22. #include <signal.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. settings_t settings = {
  26. "\200\200\200\200\200\200\200\200\200\200\200\200\200\200\200",
  27. /* -- STATIC -- */
  28. "", "", "", "", "", "", "", "", "", "",
  29. /* -- DYNAMIC -- */
  30. "", "", "", "", "", "", "", "", "", "", "", "", "", "",
  31. /* -- PADDING */
  32. ""
  33. };
  34. static void edpack(settings_t *, const char *, int);
  35. #ifdef DEBUG
  36. static void tellconfig(settings_t *);
  37. #endif
  38. #define PACK_ENC 1
  39. #define PACK_DEC 2
  40. int checked_bin_buf = 0;
  41. #define MMAP_LOOP(_offset, _block_len, _total, _len) \
  42. for ((_offset) = 0; \
  43. (_offset) < (_total); \
  44. (_offset) += (_block_len), \
  45. (_len) = ((_total) - (_offset)) < (_block_len) ? \
  46. ((_total) - (_offset)) : \
  47. (_block_len) \
  48. )
  49. #define MMAP_READ(_map, _dest, _offset, _len) \
  50. memcpy((_dest), &(_map)[(_offset)], (_len)); \
  51. (_offset) += (_len);
  52. static char *
  53. bin_checksum(const char *fname, int todo)
  54. {
  55. MD5_CTX ctx;
  56. static char hash[MD5_HASH_LENGTH + 1] = "";
  57. unsigned char md5out[MD5_HASH_LENGTH + 1] = "", buf[PREFIXLEN + 1] = "";
  58. int fd = -1;
  59. size_t len = 0, offset = 0, size = 0, newpos = 0;
  60. unsigned char *map = NULL, *outmap = NULL;
  61. char *fname_bak = NULL;
  62. Tempfile *newbin = NULL;
  63. MD5_Init(&ctx);
  64. ++checked_bin_buf;
  65. hash[0] = 0;
  66. fixmod(fname);
  67. if (todo == GET_CHECKSUM) {
  68. fd = open(fname, O_RDONLY);
  69. if (fd == -1) werr(ERR_BINSTAT);
  70. size = lseek(fd, 0, SEEK_END);
  71. map = (unsigned char*) mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
  72. if ((void*)map == MAP_FAILED) goto fatal;
  73. MMAP_LOOP(offset, sizeof(buf) - 1, size, len) {
  74. if (!memcmp(&map[offset], &settings.prefix, PREFIXLEN))
  75. break;
  76. }
  77. MD5_Update(&ctx, map, offset);
  78. /* Hash everything after the packdata too */
  79. offset += sizeof(settings_t);
  80. MD5_Update(&ctx, &map[offset], size - offset);
  81. MD5_Final(md5out, &ctx);
  82. strlcpy(hash, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(hash));
  83. OPENSSL_cleanse(&ctx, sizeof(ctx));
  84. munmap(map, size);
  85. close(fd);
  86. }
  87. if (todo == GET_CONF) {
  88. fd = open(fname, O_RDONLY);
  89. if (fd == -1) werr(ERR_BINSTAT);
  90. size = lseek(fd, 0, SEEK_END);
  91. map = (unsigned char*) mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
  92. if ((void*)map == MAP_FAILED) goto fatal;
  93. /* Find the packdata */
  94. MMAP_LOOP(offset, sizeof(buf) - 1, size, len) {
  95. if (!memcmp(&map[offset], &settings.prefix, PREFIXLEN))
  96. break;
  97. }
  98. MD5_Update(&ctx, map, offset);
  99. /* Hash everything after the packdata too */
  100. MD5_Update(&ctx, &map[offset + sizeof(settings_t)], size - (offset + sizeof(settings_t)));
  101. MD5_Final(md5out, &ctx);
  102. strlcpy(hash, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(hash));
  103. OPENSSL_cleanse(&ctx, sizeof(ctx));
  104. settings_t newsettings;
  105. /* Read the settings struct into newsettings */
  106. MMAP_READ(map, &newsettings, offset, sizeof(settings));
  107. /* Decrypt the new data */
  108. edpack(&newsettings, hash, PACK_DEC);
  109. OPENSSL_cleanse(hash, sizeof(hash));
  110. /* Copy over only the dynamic data, leaving the pack config static */
  111. memcpy(&settings.bots, &newsettings.bots, SIZE_CONF);
  112. OPENSSL_cleanse(&newsettings, sizeof(settings_t));
  113. munmap(map, size);
  114. close(fd);
  115. return ".";
  116. }
  117. if (todo & WRITE_CHECKSUM) {
  118. newbin = new Tempfile("bin", 0);
  119. size = strlen(fname) + 2;
  120. fname_bak = (char *) my_calloc(1, size);
  121. simple_snprintf(fname_bak, size, "%s~", fname);
  122. fd = open(fname, O_RDONLY);
  123. if (fd == -1) werr(ERR_BINSTAT);
  124. size = lseek(fd, 0, SEEK_END);
  125. map = (unsigned char*) mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
  126. if ((void*)map == MAP_FAILED) goto fatal;
  127. /* Find settings struct in original binary */
  128. MMAP_LOOP(offset, sizeof(buf) - 1, size, len) {
  129. if (!memcmp(&map[offset], &settings.prefix, PREFIXLEN))
  130. break;
  131. }
  132. MD5_Update(&ctx, map, offset);
  133. /* Hash everything after the packdata too */
  134. MD5_Update(&ctx, &map[offset + sizeof(settings_t)], size - (offset + sizeof(settings_t)));
  135. MD5_Final(md5out, &ctx);
  136. strlcpy(hash, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(hash));
  137. OPENSSL_cleanse(&ctx, sizeof(ctx));
  138. strlcpy(settings.hash, hash, sizeof(settings.hash));
  139. /* encrypt the entire struct with the hash (including hash) */
  140. edpack(&settings, hash, PACK_ENC);
  141. //Don't clear hash if requested during the write.
  142. if (!(todo & GET_CHECKSUM))
  143. OPENSSL_cleanse(hash, sizeof(hash));
  144. /* Copy everything up to this point into the new binary (including the settings header/prefix) */
  145. outmap = (unsigned char*) mmap(0, size, PROT_WRITE, MAP_SHARED, newbin->fd, 0);
  146. if ((void*)outmap == MAP_FAILED) goto fatal;
  147. if (lseek(newbin->fd, size - 1, SEEK_SET) == -1) goto fatal;
  148. if (write(newbin->fd, "", 1) != 1) goto fatal;
  149. offset += PREFIXLEN;
  150. memcpy(outmap, map, offset);
  151. newpos = offset;
  152. if (todo & WRITE_PACK) {
  153. /* Now copy in our encrypted settings struct */
  154. memcpy(&outmap[newpos], &settings.hash, SIZE_PACK);
  155. #ifdef DEBUG
  156. sdprintf(STR("writing pack: %d\n"), SIZE_PACK);
  157. #endif
  158. } else {
  159. /* Just copy the original pack data to the new binary */
  160. memcpy(&outmap[newpos], &map[offset], SIZE_PACK);
  161. }
  162. offset += SIZE_PACK;
  163. newpos += SIZE_PACK;
  164. if (todo & WRITE_CONF) {
  165. /* Copy in the encrypted conf data */
  166. memcpy(&outmap[newpos], &settings.bots, SIZE_CONF);
  167. #ifdef DEBUG
  168. sdprintf(STR("writing conf: %d\n"), SIZE_CONF);
  169. #endif
  170. } else {
  171. /* Just copy the original conf data to the new binary */
  172. memcpy(&outmap[newpos], &map[offset], SIZE_CONF);
  173. }
  174. newpos += SIZE_CONF;
  175. offset += SIZE_CONF;
  176. /* Write the rest of the binary */
  177. memcpy(&outmap[newpos], &map[offset], size - offset);
  178. newpos += size - offset;
  179. offset += size - offset;
  180. munmap(map, size);
  181. close(fd);
  182. munmap(outmap, size);
  183. newbin->my_close();
  184. if (size != newpos) {
  185. delete newbin;
  186. fatal(STR("Binary corrupted"), 0);
  187. }
  188. if (movefile(fname, fname_bak)) {
  189. printf(STR("Failed to move file (%s -> %s): %s\n"), fname, fname_bak, strerror(errno));
  190. delete newbin;
  191. fatal("", 0);
  192. }
  193. if (movefile(newbin->file, fname)) {
  194. printf(STR("Failed to move file (%s -> %s): %s\n"), newbin->file, fname, strerror(errno));
  195. delete newbin;
  196. fatal("", 0);
  197. }
  198. fixmod(fname);
  199. unlink(fname_bak);
  200. delete newbin;
  201. return hash;
  202. fatal:
  203. if ((void*)map != MAP_FAILED)
  204. munmap(map, size);
  205. if (fd != -1)
  206. close(fd);
  207. if ((void*)outmap != MAP_FAILED)
  208. munmap(outmap, size);
  209. delete newbin;
  210. werr(ERR_BINSTAT);
  211. }
  212. return hash;
  213. }
  214. static int
  215. features_find(const char *buffer)
  216. {
  217. if (!egg_strcasecmp(buffer, STR("no_take")))
  218. return FEATURE_NO_TAKE;
  219. else if (!egg_strcasecmp(buffer, STR("no_mdop")))
  220. return FEATURE_NO_MDOP;
  221. return 0;
  222. }
  223. static int
  224. readcfg(const char *cfgfile)
  225. {
  226. FILE *f = NULL;
  227. if ((f = fopen(cfgfile, "r")) == NULL) {
  228. printf(STR("Error: Can't open '%s' for reading\n"), cfgfile);
  229. exit(1);
  230. }
  231. char *buffer = NULL, *p = NULL;
  232. int skip = 0, line = 0, feature = 0;
  233. printf(STR("Reading '%s' "), cfgfile);
  234. while ((!feof(f)) && ((buffer = step_thru_file(f)) != NULL)) {
  235. ++line;
  236. if ((*buffer)) {
  237. if (strchr(buffer, '\n'))
  238. *(char *) strchr(buffer, '\n') = 0;
  239. if ((skipline(buffer, &skip)))
  240. continue;
  241. if ((strchr(buffer, '<') || strchr(buffer, '>')) && !strstr(buffer, "SALT")) {
  242. printf(STR(" Failed\n"));
  243. printf(STR("%s:%d: error: Look at your configuration file again...\n"), cfgfile, line);
  244. // exit(1);
  245. }
  246. p = strchr(buffer, ' ');
  247. while (p && (strchr(LISTSEPERATORS, p[0])))
  248. *p++ = 0;
  249. if (p) {
  250. if (!egg_strcasecmp(buffer, STR("packname"))) {
  251. strlcpy(settings.packname, trim(p), sizeof settings.packname);
  252. printf(".");
  253. } else if (!egg_strcasecmp(buffer, STR("shellhash"))) {
  254. strlcpy(settings.shellhash, trim(p), sizeof settings.shellhash);
  255. printf(".");
  256. } else if (!egg_strcasecmp(buffer, STR("dccprefix"))) {
  257. strlcpy(settings.dcc_prefix, trim(p), sizeof settings.dcc_prefix);
  258. printf(".");
  259. } else if (!egg_strcasecmp(buffer, STR("owner"))) {
  260. strlcat(settings.owners, trim(p), sizeof(settings.owners));
  261. strlcat(settings.owners, ",", sizeof(settings.owners));
  262. printf(".");
  263. } else if (!egg_strcasecmp(buffer, STR("owneremail"))) {
  264. strlcat(settings.owneremail, trim(p), sizeof(settings.owneremail));
  265. strlcat(settings.owneremail, ",", sizeof(settings.owneremail));
  266. printf(".");
  267. } else if (!egg_strcasecmp(buffer, STR("hub"))) {
  268. strlcat(settings.hubs, trim(p), sizeof(settings.hubs));
  269. strlcat(settings.hubs, ",", sizeof(settings.hubs));
  270. printf(".");
  271. } else if (!egg_strcasecmp(buffer, STR("salt1"))) {
  272. strlcat(settings.salt1, trim(p), sizeof(settings.salt1));
  273. printf(".");
  274. } else if (!egg_strcasecmp(buffer, STR("salt2"))) {
  275. strlcat(settings.salt2, trim(p), sizeof(settings.salt2));
  276. printf(".");
  277. } else {
  278. printf("%s %s\n", buffer, p);
  279. printf(",");
  280. }
  281. } else { /* SINGLE DIRECTIVES */
  282. if ((feature = features_find(buffer))) {
  283. int features = atol(settings.features);
  284. features |= feature;
  285. simple_snprintf(settings.features, sizeof(settings.features), "%d", features);
  286. printf(".");
  287. }
  288. }
  289. }
  290. buffer = NULL;
  291. }
  292. if (f)
  293. fclose(f);
  294. printf(STR(" Success\n"));
  295. return 1;
  296. }
  297. static void edpack(settings_t *incfg, const char *in_hash, int what)
  298. {
  299. char *tmp = NULL, *hash = (char *) in_hash, nhash[51] = "";
  300. unsigned char *(*enc_dec_string)(const char *, unsigned char *, size_t *);
  301. size_t len = 0;
  302. if (what == PACK_ENC)
  303. enc_dec_string = encrypt_binary;
  304. else
  305. enc_dec_string = decrypt_binary;
  306. #define dofield(_field) do { \
  307. if (_field) { \
  308. len = sizeof(_field) - 1; \
  309. tmp = (char *) enc_dec_string(hash, (unsigned char *) _field, &len); \
  310. if (what == PACK_ENC) \
  311. egg_memcpy(_field, tmp, len); \
  312. else \
  313. simple_snprintf(_field, sizeof(_field), "%s", tmp); \
  314. OPENSSL_cleanse(tmp, len); \
  315. free(tmp); \
  316. } \
  317. } while (0)
  318. #define dohash(_field) do { \
  319. if (what == PACK_ENC) \
  320. strlcat(nhash, _field, sizeof(nhash)); \
  321. dofield(_field); \
  322. if (what == PACK_DEC) \
  323. strlcat(nhash, _field, sizeof(nhash)); \
  324. } while (0)
  325. #define update_hash() do { \
  326. MD5(NULL); \
  327. hash = MD5(nhash); \
  328. OPENSSL_cleanse(nhash, sizeof(nhash)); \
  329. nhash[0] = 0; \
  330. } while (0)
  331. /* -- STATIC -- */
  332. dohash(incfg->hash);
  333. dohash(incfg->packname);
  334. update_hash();
  335. dohash(incfg->shellhash);
  336. update_hash();
  337. dofield(incfg->dcc_prefix);
  338. dofield(incfg->features);
  339. dofield(incfg->owners);
  340. dofield(incfg->owneremail);
  341. dofield(incfg->hubs);
  342. dohash(incfg->salt1);
  343. dohash(incfg->salt2);
  344. update_hash();
  345. /* -- DYNAMIC -- */
  346. dofield(incfg->bots);
  347. dofield(incfg->uid);
  348. dofield(incfg->autouname);
  349. dofield(incfg->pscloak);
  350. dofield(incfg->autocron);
  351. dofield(incfg->watcher);
  352. dofield(incfg->uname);
  353. dofield(incfg->username);
  354. dofield(incfg->datadir);
  355. dofield(incfg->homedir);
  356. dofield(incfg->binpath);
  357. dofield(incfg->binname);
  358. dofield(incfg->portmin);
  359. dofield(incfg->portmax);
  360. OPENSSL_cleanse(nhash, sizeof(nhash));
  361. #undef dofield
  362. #undef dohash
  363. #undef update_hash
  364. }
  365. #ifdef DEBUG
  366. static void
  367. tellconfig(settings_t *incfg)
  368. {
  369. #define dofield(_field) printf("%s: %s\n", #_field, _field);
  370. // -- STATIC --
  371. dofield(incfg->hash);
  372. dofield(incfg->packname);
  373. dofield(incfg->shellhash);
  374. dofield(incfg->dcc_prefix);
  375. dofield(incfg->features);
  376. dofield(incfg->owners);
  377. dofield(incfg->owneremail);
  378. dofield(incfg->hubs);
  379. // dofield(incfg->salt1);
  380. // dofield(incfg->salt2);
  381. // -- DYNAMIC --
  382. dofield(incfg->bots);
  383. dofield(incfg->uid);
  384. dofield(incfg->autouname);
  385. dofield(incfg->pscloak);
  386. dofield(incfg->autocron);
  387. dofield(incfg->watcher);
  388. dofield(incfg->uname);
  389. dofield(incfg->username);
  390. dofield(incfg->datadir);
  391. dofield(incfg->homedir);
  392. dofield(incfg->binpath);
  393. dofield(incfg->binname);
  394. dofield(incfg->portmin);
  395. dofield(incfg->portmax);
  396. #undef dofield
  397. }
  398. #endif
  399. void
  400. check_sum(const char *fname, const char *cfgfile)
  401. {
  402. if (!settings.hash[0]) {
  403. if (!cfgfile)
  404. fatal(STR("Binary not initialized."), 0);
  405. readcfg(cfgfile);
  406. // tellconfig(&settings);
  407. if (bin_checksum(fname, WRITE_CHECKSUM|WRITE_CONF|WRITE_PACK))
  408. printf(STR("* Wrote settings to binary.\n"));
  409. exit(0);
  410. } else {
  411. char *hash = bin_checksum(fname, GET_CHECKSUM);
  412. // tellconfig(&settings);
  413. edpack(&settings, hash, PACK_DEC);
  414. INIT_SALTS;
  415. OPENSSL_cleanse(settings.salt1, sizeof(settings.salt1));
  416. OPENSSL_cleanse(settings.salt2, sizeof(settings.salt2));
  417. #ifdef DEBUG
  418. tellconfig(&settings);
  419. #endif
  420. int n = strcmp(settings.hash, hash);
  421. OPENSSL_cleanse(settings.hash, sizeof(settings.hash));
  422. OPENSSL_cleanse(hash, strlen(hash));
  423. if (n) {
  424. unlink(fname);
  425. fatal(STR("!! Invalid binary"), 0);
  426. }
  427. }
  428. }
  429. static bool check_bin_initialized(const char *fname)
  430. {
  431. int i = 0;
  432. size_t len = strlen(shell_escape(fname)) + 3 + 1;
  433. char *path = (char *) my_calloc(1, len);
  434. simple_snprintf(path, len, STR("%s -p"), shell_escape(fname));
  435. i = system(path);
  436. free(path);
  437. if (i != -1 && WEXITSTATUS(i) == 4)
  438. return 1;
  439. return 0;
  440. }
  441. void write_settings(const char *fname, int die, bool doconf)
  442. {
  443. char *hash = NULL;
  444. int bits = WRITE_CHECKSUM;
  445. /* see if the binary is already initialized or not */
  446. bool initialized = check_bin_initialized(fname);
  447. /* only write pack data if the binary is uninitialized
  448. * otherwise, assume it has similar/correct/updated pack data
  449. */
  450. if (!initialized)
  451. bits |= WRITE_PACK;
  452. if (doconf)
  453. bits |= WRITE_CONF;
  454. /* only bother writing anything if we have pack or doconf, checksum is worthless to write out */
  455. if (bits & (WRITE_PACK|WRITE_CONF)) {
  456. // Also get the checksum
  457. if (die == -1)
  458. bits |= GET_CHECKSUM;
  459. const char salt1[] = SALT1;
  460. const char salt2[] = SALT2;
  461. strlcpy(settings.salt1, salt1, sizeof(settings.salt1));
  462. strlcpy(settings.salt2, salt2, sizeof(settings.salt2));
  463. if ((hash = bin_checksum(fname, bits))) {
  464. printf(STR("* Wrote %ssettings to: %s.\n"), ((bits & WRITE_PACK) && !(bits & WRITE_CONF)) ? "pack " :
  465. ((bits & WRITE_CONF) && !(bits & WRITE_PACK)) ? "conf " :
  466. ((bits & WRITE_PACK) && (bits & WRITE_CONF)) ? "pack/conf " :
  467. "",
  468. fname);
  469. if (die == -1) { /* only bother decrypting if we aren't about to exit */
  470. edpack(&settings, hash, PACK_DEC);
  471. INIT_SALTS;
  472. OPENSSL_cleanse(hash, strlen(hash));
  473. }
  474. }
  475. if (die == -1) {
  476. OPENSSL_cleanse(settings.salt1, sizeof(settings.salt1));
  477. OPENSSL_cleanse(settings.salt2, sizeof(settings.salt2));
  478. }
  479. }
  480. if (die >= 0)
  481. exit(die);
  482. }
  483. static void
  484. clear_settings(void)
  485. {
  486. // egg_memset(&settings.bots, 0, sizeof(settings_t) - SIZE_PACK - PREFIXLEN);
  487. egg_memset(&settings.bots, 0, SIZE_CONF);
  488. }
  489. void conf_to_bin(conf_t *in, bool move, int die)
  490. {
  491. conf_bot *bot = NULL;
  492. char *newbin = NULL;
  493. clear_settings();
  494. sdprintf("converting conf to bin\n");
  495. simple_snprintf(settings.uid, sizeof(settings.uid), "%d", in->uid);
  496. simple_snprintf(settings.watcher, sizeof(settings.watcher), "%d", in->watcher);
  497. simple_snprintf(settings.autocron, sizeof(settings.autocron), "%d", in->autocron);
  498. simple_snprintf(settings.autouname, sizeof(settings.autouname), "%d", in->autouname);
  499. simple_snprintf(settings.portmin, sizeof(settings.portmin), "%d", in->portmin);
  500. simple_snprintf(settings.portmax, sizeof(settings.portmax), "%d", in->portmax);
  501. simple_snprintf(settings.pscloak, sizeof(settings.pscloak), "%d", in->pscloak);
  502. strlcpy(settings.binname, in->binname, sizeof(settings.binname));
  503. if (in->username)
  504. strlcpy(settings.username, in->username, sizeof(settings.username));
  505. if (in->uname)
  506. strlcpy(settings.uname, in->uname, sizeof(settings.uname));
  507. strlcpy(settings.datadir, in->datadir, sizeof(settings.datadir));
  508. if (in->homedir)
  509. strlcpy(settings.homedir, in->homedir, sizeof(settings.homedir));
  510. strlcpy(settings.binpath, in->binpath, sizeof(settings.binpath));
  511. for (bot = in->bots; bot && bot->nick; bot = bot->next) {
  512. simple_snprintf(settings.bots, sizeof(settings.bots), STR("%s%s%s %s %s%s %s,"),
  513. settings.bots && settings.bots[0] ? settings.bots : "",
  514. bot->disabled ? "/" : "",
  515. bot->nick,
  516. bot->net.ip ? bot->net.ip : "*",
  517. bot->net.host6 ? "+" : "",
  518. bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "*"),
  519. bot->net.ip6 ? bot->net.ip6 : "");
  520. }
  521. #ifndef CYGWIN_HACKS
  522. if (move)
  523. newbin = move_bin(in->binpath, in->binname, 0);
  524. else
  525. #endif /* !CYGWIN_HACKS */
  526. newbin = binname;
  527. // tellconfig(&settings);
  528. write_settings(newbin, -1, 1);
  529. if (die >= 0)
  530. exit(die);
  531. }
  532. void reload_bin_data() {
  533. if (bin_checksum(binname, GET_CONF)) {
  534. putlog(LOG_MISC, "*", STR("Rehashed config data from binary."));
  535. conf_bot *oldbots = NULL, *oldbot = NULL;
  536. bool was_localhub = conf.bot->localhub ? 1 : 0;
  537. /* save the old bots list */
  538. if (conf.bots)
  539. oldbots = conf_bots_dup(conf.bots);
  540. /* Save the old conf.bot */
  541. oldbot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  542. conf_bot_dup(oldbot, conf.bot);
  543. /* free up our current conf struct */
  544. free_conf();
  545. /* Fill conf[] with binary data from settings[] */
  546. bin_to_conf();
  547. /* fill up conf.bot using origbotname */
  548. fill_conf_bot(0); /* 0 to avoid exiting if conf.bot cannot be filled */
  549. if (was_localhub) {
  550. /* add any new bots not in userfile */
  551. conf_add_userlist_bots();
  552. /* deluser removed bots from conf */
  553. if (oldbots)
  554. deluser_removed_bots(oldbots, conf.bots);
  555. #ifdef this_is_handled_by_confedit_now
  556. /* no longer the localhub (or removed), need to alert the new one to rehash (or start it) */
  557. if (!conf.bot || !conf.bot->localhub) {
  558. conf_bot *localhub = conf_getlocalhub(conf.bots);
  559. /* then SIGHUP new localhub or spawn new localhub */
  560. if (localhub) {
  561. /* Check for pid again - may be using fork-interval */
  562. localhub->pid = checkpid(localhub->nick, localhub);
  563. if (localhub->pid)
  564. conf_killbot(NULL, localhub, SIGHUP); //restart the new localhub
  565. /* else
  566. start new localhub - done below in spawnbots() */
  567. }
  568. }
  569. /* start/disable new bots as necesary */
  570. conf_checkpids(conf.bots);
  571. spawnbots(1); //1 signifies to not start me!
  572. #endif
  573. }
  574. if (conf.bot && conf.bot->disabled) {
  575. if (tands > 0) {
  576. botnet_send_chat(-1, conf.bot->nick, STR("Bot disabled in binary."));
  577. botnet_send_bye(STR("Bot disabled in binary."));
  578. }
  579. if (server_online)
  580. nuke_server(STR("bbl"));
  581. werr(ERR_BOTDISABLED);
  582. } else if (!conf.bot) {
  583. conf.bot = oldbot;
  584. if (tands > 0) {
  585. botnet_send_chat(-1, conf.bot->nick, STR("Bot removed from binary."));
  586. botnet_send_bye(STR("Bot removed from binary."));
  587. }
  588. if (server_online)
  589. nuke_server(STR("it's been good, cya"));
  590. werr(ERR_BADBOT);
  591. }
  592. /* The bot nick changed! (case) */
  593. if (strcmp(conf.bot->nick, oldbot->nick)) {
  594. change_handle(conf.bot->u, conf.bot->nick);
  595. // var_set_by_name(conf.bot->nick, "nick", conf.bot->nick);
  596. // var_set_userentry(conf.bot->nick, "nick", conf.bot->nick);
  597. }
  598. free_bot(oldbot);
  599. if (oldbots)
  600. free_conf_bots(oldbots);
  601. if (!conf.bot->localhub)
  602. free_conf_bots(conf.bots);
  603. }
  604. }