binary.c 22 KB

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