binary.c 20 KB

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