binary.c 25 KB

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