binary.c 23 KB

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