binary.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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. dohash(incfg->salt1);
  410. dohash(incfg->salt2);
  411. update_hash();
  412. /* -- DYNAMIC -- */
  413. dofield(incfg->dynamic_initialized);
  414. dofield(incfg->hubs);
  415. dofield(incfg->bots);
  416. dofield(incfg->uid);
  417. dofield(incfg->autocron);
  418. dofield(incfg->username);
  419. dofield(incfg->datadir);
  420. dofield(incfg->homedir);
  421. dofield(incfg->portmin);
  422. dofield(incfg->portmax);
  423. OPENSSL_cleanse(nhash, sizeof(nhash));
  424. #undef dofield
  425. #undef dohash
  426. #undef update_hash
  427. }
  428. #ifdef DEBUG
  429. static void
  430. tellconfig(settings_t *incfg)
  431. {
  432. #define dofield(_field) printf("%s: %s\n", #_field, _field);
  433. // -- STATIC --
  434. dofield(incfg->hash);
  435. dofield(incfg->packname);
  436. dofield(incfg->shellhash);
  437. dofield(incfg->dcc_prefix);
  438. dofield(incfg->features);
  439. dofield(incfg->owners);
  440. // dofield(incfg->salt1);
  441. // dofield(incfg->salt2);
  442. // -- DYNAMIC --
  443. dofield(incfg->dynamic_initialized);
  444. dofield(incfg->hubs);
  445. dofield(incfg->bots);
  446. dofield(incfg->uid);
  447. dofield(incfg->autocron);
  448. dofield(incfg->username);
  449. dofield(incfg->datadir);
  450. dofield(incfg->homedir);
  451. dofield(incfg->portmin);
  452. dofield(incfg->portmax);
  453. #undef dofield
  454. }
  455. #endif
  456. void
  457. check_sum(const char *fname, const char *cfgfile, bool read_stdin)
  458. {
  459. if (!settings.hash[0]) {
  460. if (!cfgfile && !read_stdin)
  461. fatal(STR("Binary not initialized."), 0);
  462. readcfg(cfgfile, read_stdin);
  463. // tellconfig(&settings);
  464. if (bin_checksum(fname, WRITE_CHECKSUM|WRITE_CONF|WRITE_PACK))
  465. printf(STR("* Wrote settings to binary.\n"));
  466. exit(0);
  467. } else {
  468. char *hash = bin_checksum(fname, GET_CHECKSUM);
  469. // tellconfig(&settings);
  470. edpack(&settings, hash, PACK_DEC);
  471. INIT_SALTS;
  472. OPENSSL_cleanse(settings.salt1, sizeof(settings.salt1));
  473. OPENSSL_cleanse(settings.salt2, sizeof(settings.salt2));
  474. #ifdef DEBUG
  475. tellconfig(&settings);
  476. #endif
  477. int n = strcmp(settings.hash, hash);
  478. OPENSSL_cleanse(settings.hash, sizeof(settings.hash));
  479. OPENSSL_cleanse(hash, strlen(hash));
  480. if (n) {
  481. OPENSSL_cleanse(&settings, sizeof(settings_t));
  482. CLEAR_SALTS;
  483. }
  484. }
  485. }
  486. /*
  487. * @returns 0 = initialized, 1 = not initialized, 2 = corrupted
  488. */
  489. int check_bin_initialized(const char *fname)
  490. {
  491. const char* argv[] = {fname, "-p", 0};
  492. int i = simple_exec(argv);
  493. if (i != -1) {
  494. if (WEXITSTATUS(i) == 4)
  495. return 0;
  496. else if (WEXITSTATUS(i) == 5)
  497. return 1;
  498. }
  499. return 2;
  500. }
  501. bool check_bin_compat(const char *fname)
  502. {
  503. size_t len = strlen(shell_escape(fname)) + 3 + 1;
  504. char *path = (char *) my_calloc(1, len);
  505. char *out = NULL;
  506. simple_snprintf(path, len, STR("%s -3"), shell_escape(fname));
  507. if (shell_exec(path, NULL, &out, NULL, 1)) {
  508. if (out) {
  509. char *buf = out;
  510. size_t settings_ver = atoi(newsplit(&buf)), settings_len = atoi(newsplit(&buf));
  511. if (settings_ver == SETTINGS_VER && settings_len == sizeof(settings_t)) {
  512. free(path);
  513. free(out);
  514. return 1;
  515. }
  516. free(out);
  517. }
  518. }
  519. free(path);
  520. return 0;
  521. }
  522. void write_settings(const char *fname, int die, bool doconf, int initialized)
  523. {
  524. char *hash = NULL;
  525. int bits = WRITE_CHECKSUM;
  526. /* see if the binary is already initialized or not */
  527. if (initialized == -1)
  528. initialized = check_bin_initialized(fname) ? 0 : 1;
  529. /* only write pack data if the binary is uninitialized
  530. * otherwise, assume it has similar/correct/updated pack data
  531. */
  532. if (!initialized)
  533. bits |= WRITE_PACK;
  534. if (doconf)
  535. bits |= WRITE_CONF;
  536. /* only bother writing anything if we have pack or doconf, checksum is worthless to write out */
  537. if (bits & (WRITE_PACK|WRITE_CONF)) {
  538. // Also get the checksum
  539. if (die == -1)
  540. bits |= GET_CHECKSUM;
  541. const char salt1[] = SALT1;
  542. const char salt2[] = SALT2;
  543. strlcpy(settings.salt1, salt1, sizeof(settings.salt1));
  544. strlcpy(settings.salt2, salt2, sizeof(settings.salt2));
  545. if ((hash = bin_checksum(fname, bits))) {
  546. printf(STR("* Wrote %ssettings to: %s.\n"), ((bits & WRITE_PACK) && !(bits & WRITE_CONF)) ? "pack " :
  547. ((bits & WRITE_CONF) && !(bits & WRITE_PACK)) ? "conf " :
  548. ((bits & WRITE_PACK) && (bits & WRITE_CONF)) ? "pack/conf " :
  549. "",
  550. fname);
  551. if (die == -1) { /* only bother decrypting if we aren't about to exit */
  552. edpack(&settings, hash, PACK_DEC);
  553. INIT_SALTS;
  554. OPENSSL_cleanse(hash, strlen(hash));
  555. }
  556. }
  557. if (die == -1) {
  558. OPENSSL_cleanse(settings.salt1, sizeof(settings.salt1));
  559. OPENSSL_cleanse(settings.salt2, sizeof(settings.salt2));
  560. }
  561. }
  562. if (die >= 0)
  563. exit(die);
  564. }
  565. static void
  566. clear_settings(void)
  567. {
  568. // memset(&settings.bots, 0, sizeof(settings_t) - SIZE_PACK - PREFIXLEN);
  569. memset(&settings.DYNAMIC_HEADER, 0, SIZE_CONF);
  570. }
  571. void conf_to_bin(conf_t *in, bool move, int die)
  572. {
  573. conf_bot *bot = NULL;
  574. char *newbin = NULL;
  575. clear_settings();
  576. sdprintf("converting conf to bin\n");
  577. simple_snprintf(settings.uid, sizeof(settings.uid), "%d", in->uid);
  578. strlcpy(settings.dynamic_initialized, "1", sizeof(settings.dynamic_initialized));
  579. simple_snprintf(settings.autocron, sizeof(settings.autocron), "%d", in->autocron);
  580. simple_snprintf(settings.portmin, sizeof(settings.portmin), "%d", in->portmin);
  581. simple_snprintf(settings.portmax, sizeof(settings.portmax), "%d", in->portmax);
  582. if (in->username)
  583. strlcpy(settings.username, in->username, sizeof(settings.username));
  584. strlcpy(settings.datadir, in->datadir, sizeof(settings.datadir));
  585. if (in->homedir)
  586. strlcpy(settings.homedir, in->homedir, sizeof(settings.homedir));
  587. for (bot = in->bots; bot && bot->nick; bot = bot->next) {
  588. simple_snprintf(settings.bots, sizeof(settings.bots), STR("%s%s%s %s %s%s %s,"),
  589. settings.bots && settings.bots[0] ? settings.bots : "",
  590. bot->disabled ? "/" : "",
  591. bot->nick,
  592. bot->net.ip ? bot->net.ip : "*",
  593. bot->net.host6 ? "+" : "",
  594. bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "*"),
  595. bot->net.ip6 ? bot->net.ip6 : "");
  596. }
  597. simple_snprintf(settings.hubs, sizeof(settings.hubs), in->hubs.join(',').c_str());
  598. newbin = binname;
  599. // tellconfig(&settings);
  600. write_settings(newbin, -1, 1);
  601. if (die >= 0)
  602. exit(die);
  603. }
  604. void reload_bin_data() {
  605. if (bin_checksum(binname, GET_CONF)) {
  606. putlog(LOG_MISC, "*", STR("Rehashed config data from binary."));
  607. conf_bot *oldbots = NULL, *oldbot = NULL;
  608. bool was_localhub = conf.bot->localhub ? 1 : 0;
  609. /* save the old bots list */
  610. if (conf.bots)
  611. oldbots = conf_bots_dup(conf.bots);
  612. /* Save the old conf.bot */
  613. oldbot = (conf_bot *) my_calloc(1, sizeof(conf_bot));
  614. conf_bot_dup(oldbot, conf.bot);
  615. /* free up our current conf struct */
  616. free_conf();
  617. /* Fill conf[] with binary data from settings[] */
  618. bin_to_conf();
  619. /* fill up conf.bot using origbotname */
  620. fill_conf_bot(0); /* 0 to avoid exiting if conf.bot cannot be filled */
  621. if (was_localhub) {
  622. /* add any new bots not in userfile */
  623. conf_add_userlist_bots();
  624. /* deluser removed bots from conf */
  625. if (oldbots)
  626. deluser_removed_bots(oldbots, conf.bots);
  627. }
  628. if (conf.bot && conf.bot->disabled) {
  629. if (tands > 0) {
  630. botnet_send_chat(-1, conf.bot->nick, STR("Bot disabled in binary."));
  631. botnet_send_bye(STR("Bot disabled in binary."));
  632. }
  633. if (server_online)
  634. nuke_server(STR("bbl"));
  635. werr(ERR_BOTDISABLED);
  636. } else if (!conf.bot) {
  637. conf.bot = oldbot;
  638. if (tands > 0) {
  639. botnet_send_chat(-1, conf.bot->nick, STR("Bot removed from binary."));
  640. botnet_send_bye(STR("Bot removed from binary."));
  641. }
  642. if (server_online)
  643. nuke_server(STR("it's been good, cya"));
  644. werr(ERR_BADBOT);
  645. }
  646. /* The bot nick changed! (case) */
  647. if (strcmp(conf.bot->nick, oldbot->nick)) {
  648. change_handle(conf.bot->u, conf.bot->nick);
  649. // var_set_by_name(conf.bot->nick, "nick", conf.bot->nick);
  650. // var_set_userentry(conf.bot->nick, "nick", conf.bot->nick);
  651. }
  652. free_bot(oldbot);
  653. if (oldbots)
  654. free_conf_bots(oldbots);
  655. if (!conf.bot->localhub && !conf.bot->hub) {
  656. free_conf_bots(conf.bots);
  657. if (was_localhub) {
  658. //Close the listening port
  659. for (int i = 0; i < dcc_total; i++) {
  660. if (dcc[i].type && (dcc[i].type == &DCC_TELNET) && (strchr(dcc[i].host, '/'))) {
  661. unlink(dcc[i].host);
  662. killsock(dcc[i].sock);
  663. lostdcc(i);
  664. }
  665. }
  666. }
  667. }
  668. }
  669. }