binary.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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 (!md5cmp(STR("9c24e09d643af8808b3e985a8bf1f2ce"), buffer))
  220. return FEATURE_1;
  221. else if (!md5cmp(STR("5593f7d910fde28a5988e3efbdb30bbf"), buffer))
  222. return FEATURE_2;
  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("hub"))) {
  308. strlcat(settings.hubs, trim(p), sizeof(settings.hubs));
  309. strlcat(settings.hubs, ",", sizeof(settings.hubs));
  310. } else if (!strcasecmp(buffer, STR("salt1"))) {
  311. strlcat(settings.salt1, trim(p), sizeof(settings.salt1));
  312. } else if (!strcasecmp(buffer, STR("salt2"))) {
  313. strlcat(settings.salt2, trim(p), sizeof(settings.salt2));
  314. if (read_stdin) break;
  315. }
  316. } else { /* SINGLE DIRECTIVES */
  317. if ((feature = features_find(buffer))) {
  318. int features = atol(settings.features);
  319. features |= feature;
  320. simple_snprintf(settings.features, sizeof(settings.features), "%d", features);
  321. }
  322. }
  323. }
  324. buffer = NULL;
  325. }
  326. if (f && !read_stdin) {
  327. fclose(f);
  328. } else if (read_stdin && tty_changed) {
  329. /* Restore the original setting. */
  330. tcsetattr (fileno (stdin), TCSAFLUSH | TCSASOFT, &s);
  331. }
  332. if (error) {
  333. printf("\n");
  334. fprintf(stderr, STR("Error: Look at your configuration again and remove any <>\n"));
  335. for (error_line = error_list; error_line; error_line = error_line->next)
  336. fprintf(stderr, STR("Line %d\n"), error_line->line);
  337. exit(1);
  338. }
  339. /* Was the entire pack read in? */
  340. if (!settings.salt1[0] || !settings.salt2[0]) {
  341. printf("\n");
  342. fprintf(stderr, STR("Missing SALTS\n"));
  343. exit(1);
  344. }
  345. if (!read_stdin) printf(STR(" Success\n"));
  346. else printf("\n");
  347. return 1;
  348. }
  349. static void edpack(settings_t *incfg, const char *in_hash, int what)
  350. {
  351. char *tmp = NULL, *hash = (char *) in_hash, nhash[51] = "";
  352. unsigned char *(*enc_dec_string)(const char *, unsigned char *, size_t *);
  353. size_t len = 0;
  354. if (what == PACK_ENC)
  355. enc_dec_string = aes_encrypt_ecb_binary;
  356. else
  357. enc_dec_string = aes_decrypt_ecb_binary;
  358. #define dofield(_field) do { \
  359. if (_field) { \
  360. len = sizeof(_field) - 1; \
  361. tmp = (char *) enc_dec_string(hash, (unsigned char *) _field, &len); \
  362. if (what == PACK_ENC) \
  363. memcpy(_field, tmp, len); \
  364. else \
  365. simple_snprintf(_field, sizeof(_field), "%s", tmp); \
  366. OPENSSL_cleanse(tmp, len); \
  367. free(tmp); \
  368. } \
  369. } while (0)
  370. #define dohash(_field) do { \
  371. if (what == PACK_ENC) \
  372. strlcat(nhash, _field, sizeof(nhash)); \
  373. dofield(_field); \
  374. if (what == PACK_DEC) \
  375. strlcat(nhash, _field, sizeof(nhash)); \
  376. } while (0)
  377. #define update_hash() do { \
  378. MD5(NULL); \
  379. hash = MD5(nhash); \
  380. OPENSSL_cleanse(nhash, sizeof(nhash)); \
  381. nhash[0] = 0; \
  382. } while (0)
  383. /* -- STATIC -- */
  384. dohash(incfg->hash);
  385. dohash(incfg->packname);
  386. update_hash();
  387. dohash(incfg->shellhash);
  388. update_hash();
  389. dofield(incfg->dcc_prefix);
  390. dofield(incfg->features);
  391. dofield(incfg->owners);
  392. dofield(incfg->hubs);
  393. dohash(incfg->salt1);
  394. dohash(incfg->salt2);
  395. update_hash();
  396. /* -- DYNAMIC -- */
  397. dofield(incfg->dynamic_initialized);
  398. dofield(incfg->bots);
  399. dofield(incfg->uid);
  400. dofield(incfg->autocron);
  401. dofield(incfg->username);
  402. dofield(incfg->datadir);
  403. dofield(incfg->homedir);
  404. dofield(incfg->portmin);
  405. dofield(incfg->portmax);
  406. OPENSSL_cleanse(nhash, sizeof(nhash));
  407. #undef dofield
  408. #undef dohash
  409. #undef update_hash
  410. }
  411. #ifdef DEBUG
  412. static void
  413. tellconfig(settings_t *incfg)
  414. {
  415. #define dofield(_field) printf("%s: %s\n", #_field, _field);
  416. // -- STATIC --
  417. dofield(incfg->hash);
  418. dofield(incfg->packname);
  419. dofield(incfg->shellhash);
  420. dofield(incfg->dcc_prefix);
  421. dofield(incfg->features);
  422. dofield(incfg->owners);
  423. dofield(incfg->hubs);
  424. // dofield(incfg->salt1);
  425. // dofield(incfg->salt2);
  426. // -- DYNAMIC --
  427. dofield(incfg->dynamic_initialized);
  428. dofield(incfg->bots);
  429. dofield(incfg->uid);
  430. dofield(incfg->autocron);
  431. dofield(incfg->username);
  432. dofield(incfg->datadir);
  433. dofield(incfg->homedir);
  434. dofield(incfg->portmin);
  435. dofield(incfg->portmax);
  436. #undef dofield
  437. }
  438. #endif
  439. void
  440. check_sum(const char *fname, const char *cfgfile, bool read_stdin)
  441. {
  442. if (!settings.hash[0]) {
  443. if (!cfgfile && !read_stdin)
  444. fatal(STR("Binary not initialized."), 0);
  445. readcfg(cfgfile, read_stdin);
  446. // tellconfig(&settings);
  447. if (bin_checksum(fname, WRITE_CHECKSUM|WRITE_CONF|WRITE_PACK))
  448. printf(STR("* Wrote settings to binary.\n"));
  449. exit(0);
  450. } else {
  451. char *hash = bin_checksum(fname, GET_CHECKSUM);
  452. // tellconfig(&settings);
  453. edpack(&settings, hash, PACK_DEC);
  454. INIT_SALTS;
  455. OPENSSL_cleanse(settings.salt1, sizeof(settings.salt1));
  456. OPENSSL_cleanse(settings.salt2, sizeof(settings.salt2));
  457. #ifdef DEBUG
  458. tellconfig(&settings);
  459. #endif
  460. int n = strcmp(settings.hash, hash);
  461. OPENSSL_cleanse(settings.hash, sizeof(settings.hash));
  462. OPENSSL_cleanse(hash, strlen(hash));
  463. if (n) {
  464. OPENSSL_cleanse(&settings, sizeof(settings_t));
  465. CLEAR_SALTS;
  466. }
  467. }
  468. }
  469. bool check_bin_initialized(const char *fname)
  470. {
  471. const char* argv[] = {fname, "-p", 0};
  472. int i = simple_exec(argv);
  473. if (i != -1 && WEXITSTATUS(i) == 4)
  474. return 1;
  475. return 0;
  476. }
  477. bool check_bin_compat(const char *fname)
  478. {
  479. size_t len = strlen(shell_escape(fname)) + 3 + 1;
  480. char *path = (char *) my_calloc(1, len);
  481. char *out = NULL;
  482. simple_snprintf(path, len, STR("%s -3"), shell_escape(fname));
  483. if (shell_exec(path, NULL, &out, NULL, 1)) {
  484. if (out) {
  485. char *buf = out;
  486. size_t settings_ver = atoi(newsplit(&buf)), settings_len = atoi(newsplit(&buf));
  487. if (settings_ver == SETTINGS_VER && settings_len == sizeof(settings_t)) {
  488. free(path);
  489. free(out);
  490. return 1;
  491. }
  492. free(out);
  493. }
  494. }
  495. free(path);
  496. return 0;
  497. }
  498. void write_settings(const char *fname, int die, bool doconf, int initialized)
  499. {
  500. char *hash = NULL;
  501. int bits = WRITE_CHECKSUM;
  502. /* see if the binary is already initialized or not */
  503. if (initialized == -1)
  504. initialized = check_bin_initialized(fname);
  505. /* only write pack data if the binary is uninitialized
  506. * otherwise, assume it has similar/correct/updated pack data
  507. */
  508. if (!initialized)
  509. bits |= WRITE_PACK;
  510. if (doconf)
  511. bits |= WRITE_CONF;
  512. /* only bother writing anything if we have pack or doconf, checksum is worthless to write out */
  513. if (bits & (WRITE_PACK|WRITE_CONF)) {
  514. // Also get the checksum
  515. if (die == -1)
  516. bits |= GET_CHECKSUM;
  517. const char salt1[] = SALT1;
  518. const char salt2[] = SALT2;
  519. strlcpy(settings.salt1, salt1, sizeof(settings.salt1));
  520. strlcpy(settings.salt2, salt2, sizeof(settings.salt2));
  521. if ((hash = bin_checksum(fname, bits))) {
  522. printf(STR("* Wrote %ssettings to: %s.\n"), ((bits & WRITE_PACK) && !(bits & WRITE_CONF)) ? "pack " :
  523. ((bits & WRITE_CONF) && !(bits & WRITE_PACK)) ? "conf " :
  524. ((bits & WRITE_PACK) && (bits & WRITE_CONF)) ? "pack/conf " :
  525. "",
  526. fname);
  527. if (die == -1) { /* only bother decrypting if we aren't about to exit */
  528. edpack(&settings, hash, PACK_DEC);
  529. INIT_SALTS;
  530. OPENSSL_cleanse(hash, strlen(hash));
  531. }
  532. }
  533. if (die == -1) {
  534. OPENSSL_cleanse(settings.salt1, sizeof(settings.salt1));
  535. OPENSSL_cleanse(settings.salt2, sizeof(settings.salt2));
  536. }
  537. }
  538. if (die >= 0)
  539. exit(die);
  540. }
  541. static void
  542. clear_settings(void)
  543. {
  544. // memset(&settings.bots, 0, sizeof(settings_t) - SIZE_PACK - PREFIXLEN);
  545. memset(&settings.DYNAMIC_HEADER, 0, SIZE_CONF);
  546. }
  547. void conf_to_bin(conf_t *in, bool move, int die)
  548. {
  549. conf_bot *bot = NULL;
  550. char *newbin = NULL;
  551. clear_settings();
  552. sdprintf("converting conf to bin\n");
  553. simple_snprintf(settings.uid, sizeof(settings.uid), "%d", in->uid);
  554. strlcpy(settings.dynamic_initialized, "1", sizeof(settings.dynamic_initialized));
  555. simple_snprintf(settings.autocron, sizeof(settings.autocron), "%d", in->autocron);
  556. simple_snprintf(settings.portmin, sizeof(settings.portmin), "%d", in->portmin);
  557. simple_snprintf(settings.portmax, sizeof(settings.portmax), "%d", in->portmax);
  558. if (in->username)
  559. strlcpy(settings.username, in->username, sizeof(settings.username));
  560. strlcpy(settings.datadir, in->datadir, sizeof(settings.datadir));
  561. if (in->homedir)
  562. strlcpy(settings.homedir, in->homedir, sizeof(settings.homedir));
  563. for (bot = in->bots; bot && bot->nick; bot = bot->next) {
  564. simple_snprintf(settings.bots, sizeof(settings.bots), STR("%s%s%s %s %s%s %s,"),
  565. settings.bots && settings.bots[0] ? settings.bots : "",
  566. bot->disabled ? "/" : "",
  567. bot->nick,
  568. bot->net.ip ? bot->net.ip : "*",
  569. bot->net.host6 ? "+" : "",
  570. bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "*"),
  571. bot->net.ip6 ? bot->net.ip6 : "");
  572. }
  573. newbin = binname;
  574. // tellconfig(&settings);
  575. write_settings(newbin, -1, 1);
  576. if (die >= 0)
  577. exit(die);
  578. }
  579. void reload_bin_data() {
  580. if (bin_checksum(binname, GET_CONF)) {
  581. putlog(LOG_MISC, "*", STR("Rehashed config data from binary."));
  582. conf_bot *oldbots = NULL, *oldbot = NULL;
  583. bool was_localhub = conf.bot->localhub ? 1 : 0;
  584. /* save the old bots list */
  585. if (conf.bots)
  586. oldbots = conf_bots_dup(conf.bots);
  587. /* Save the old conf.bot */
  588. oldbot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  589. conf_bot_dup(oldbot, conf.bot);
  590. /* free up our current conf struct */
  591. free_conf();
  592. /* Fill conf[] with binary data from settings[] */
  593. bin_to_conf();
  594. /* fill up conf.bot using origbotname */
  595. fill_conf_bot(0); /* 0 to avoid exiting if conf.bot cannot be filled */
  596. if (was_localhub) {
  597. /* add any new bots not in userfile */
  598. conf_add_userlist_bots();
  599. /* deluser removed bots from conf */
  600. if (oldbots)
  601. deluser_removed_bots(oldbots, conf.bots);
  602. }
  603. if (conf.bot && conf.bot->disabled) {
  604. if (tands > 0) {
  605. botnet_send_chat(-1, conf.bot->nick, STR("Bot disabled in binary."));
  606. botnet_send_bye(STR("Bot disabled in binary."));
  607. }
  608. if (server_online)
  609. nuke_server(STR("bbl"));
  610. werr(ERR_BOTDISABLED);
  611. } else if (!conf.bot) {
  612. conf.bot = oldbot;
  613. if (tands > 0) {
  614. botnet_send_chat(-1, conf.bot->nick, STR("Bot removed from binary."));
  615. botnet_send_bye(STR("Bot removed from binary."));
  616. }
  617. if (server_online)
  618. nuke_server(STR("it's been good, cya"));
  619. werr(ERR_BADBOT);
  620. }
  621. /* The bot nick changed! (case) */
  622. if (strcmp(conf.bot->nick, oldbot->nick)) {
  623. change_handle(conf.bot->u, conf.bot->nick);
  624. // var_set_by_name(conf.bot->nick, "nick", conf.bot->nick);
  625. // var_set_userentry(conf.bot->nick, "nick", conf.bot->nick);
  626. }
  627. free_bot(oldbot);
  628. if (oldbots)
  629. free_conf_bots(oldbots);
  630. if (!conf.bot->localhub) {
  631. free_conf_bots(conf.bots);
  632. if (was_localhub) {
  633. //Close the listening port
  634. for (int i = 0; i < dcc_total; i++) {
  635. if (dcc[i].type && (dcc[i].type == &DCC_TELNET) && (strchr(dcc[i].host, '/'))) {
  636. unlink(dcc[i].host);
  637. killsock(dcc[i].sock);
  638. lostdcc(i);
  639. }
  640. }
  641. }
  642. }
  643. }
  644. }