binary.c 22 KB

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