binary.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * binary.c -- handles:
  3. * misc update functions
  4. * md5 hash verifying
  5. *
  6. */
  7. #include "common.h"
  8. #include "binary.h"
  9. #include "settings.h"
  10. #include "crypt.h"
  11. #include "shell.h"
  12. #include "misc.h"
  13. #include "main.h"
  14. #include "misc_file.h"
  15. /*
  16. typedef struct encdata_struct {
  17. char prefix[PREFIXLEN];
  18. char data[65];
  19. } encdata_t;
  20. static encdata_t encdata = {
  21. "AAAAAAAAAAAAAAAA",
  22. ""
  23. };
  24. */
  25. settings_t settings = {
  26. "AAAAAAAAAAAAAAAA",
  27. "", "", "", "", "", "", "", "", "", "", "",
  28. };
  29. #define PACK_ENC 1
  30. #define PACK_DEC 2
  31. static void edpack(struct settings_struct *, const char *, int);
  32. int checked_bin_buf = 0;
  33. static char *
  34. bin_md5(const char *fname, int todo, MD5_CTX * ctx)
  35. {
  36. static char hash[MD5_HASH_LENGTH + 1] = "";
  37. unsigned char md5out[MD5_HASH_LENGTH + 1] = "", buf[17] = "";
  38. FILE *f = NULL;
  39. size_t len = 0;
  40. checked_bin_buf++;
  41. if (!(f = fopen(fname, "rb")))
  42. werr(ERR_BINSTAT);
  43. while ((len = fread(buf, 1, sizeof buf - 1, f))) {
  44. if (!memcmp(buf, &settings.prefix, PREFIXLEN)) {
  45. break;
  46. }
  47. MD5_Update(ctx, buf, len);
  48. }
  49. fclose(f);
  50. MD5_Final(md5out, ctx);
  51. strncpyz(hash, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(hash));
  52. OPENSSL_cleanse(&ctx, sizeof(ctx));
  53. if (todo == WRITE_MD5) {
  54. char *fname_bak = NULL, s[DIRMAX] = "";
  55. FILE *fn = NULL; /* the new binary */
  56. int i = 0, fd = -1;
  57. size_t size = 0;
  58. size = strlen(fname) + 2;
  59. fname_bak = calloc(1, size);
  60. egg_snprintf(fname_bak, size, "%s~", fname);
  61. size = 0;
  62. if (!(f = fopen(fname, "rb")))
  63. werr(ERR_BINSTAT);
  64. fseek(f, 0, SEEK_END);
  65. size = ftell(f);
  66. fseek(f, 0, SEEK_SET);
  67. egg_snprintf(s, sizeof s, ".bin-XXXXXX");
  68. if ((fd = mkstemp(s)) == -1 || (fn = fdopen(fd, "wb")) == NULL) {
  69. if (fd != -1) {
  70. unlink(s);
  71. close(fd);
  72. }
  73. fatal("Can't create temporary file!", 0);
  74. }
  75. while ((len = fread(buf, 1, sizeof(buf) - 1, f))) {
  76. if (i) { /* to skip bytes for hash */
  77. i -= sizeof(buf) - 1;
  78. continue;
  79. }
  80. if (fwrite(buf, sizeof(buf) - 1, 1, fn) != 1) {
  81. fclose(f);
  82. fclose(fn);
  83. unlink(s);
  84. werr(ERR_BINSTAT);
  85. }
  86. /*
  87. if (!memcmp(buf, &encdata.prefix, PREFIXLEN)) {
  88. // now we have 65 for data :D
  89. char *enc_hash = NULL;
  90. enc_hash = encrypt_string(SALT1, hash);
  91. fwrite(enc_hash, strlen(enc_hash), 1, fn);
  92. i = strlen(enc_hash); // skip the next strlen(enc_hash) bytes
  93. free(enc_hash);
  94. }
  95. */
  96. if (!memcmp(buf, &settings.prefix, PREFIXLEN)) {
  97. strncpyz(settings.hash, hash, 65);
  98. edpack(&settings, hash, PACK_ENC);
  99. fwrite(&settings.hash, sizeof(struct settings_struct) - PREFIXLEN, 1, fn);
  100. i = sizeof(struct settings_struct) - PREFIXLEN;
  101. }
  102. }
  103. fclose(f);
  104. fclose(fn);
  105. if (movefile(fname, fname_bak))
  106. fatal("Crappy os :D", 0);
  107. if (movefile(s, fname))
  108. fatal("Crappy os :D", 0);
  109. fixmod(fname);
  110. unlink(fname_bak);
  111. unlink(s);
  112. }
  113. return hash;
  114. }
  115. static int
  116. readcfg(const char *cfgfile)
  117. {
  118. FILE *f = NULL;
  119. char *buffer = NULL, *p = NULL;
  120. int skip = 0, line = 0;
  121. f = fopen(cfgfile, "r");
  122. if (!f) {
  123. printf("Error: Can't open '%s' for reading\n", cfgfile);
  124. exit(1);
  125. }
  126. printf("Reading '%s' ", cfgfile);
  127. while ((!feof(f)) && ((buffer = step_thru_file(f)) != NULL)) {
  128. line++;
  129. if ((*buffer)) {
  130. if (strchr(buffer, '\n'))
  131. *(char *) strchr(buffer, '\n') = 0;
  132. if ((skipline(buffer, &skip)))
  133. continue;
  134. if (strchr(buffer, '<') || strchr(buffer, '>')) {
  135. printf(" Failed\n");
  136. printf("%s:%d: error: Look at your configuration file again...\n", cfgfile, line);
  137. exit(1);
  138. }
  139. p = strchr(buffer, ' ');
  140. while (p && (strchr(LISTSEPERATORS, p[0])))
  141. *p++ = 0;
  142. if (p) {
  143. if (!egg_strcasecmp(buffer, "packname")) {
  144. strncpyz(settings.packname, trim(p), sizeof settings.packname);
  145. printf(".");
  146. } else if (!egg_strcasecmp(buffer, "shellhash")) {
  147. strncpyz(settings.shellhash, trim(p), sizeof settings.shellhash);
  148. printf(".");
  149. } else if (!egg_strcasecmp(buffer, "bdhash")) {
  150. strncpyz(settings.bdhash, trim(p), sizeof settings.bdhash);
  151. printf(".");
  152. } else if (!egg_strcasecmp(buffer, "dccprefix")) {
  153. strncpyz(settings.dcc_prefix, trim(p), sizeof settings.dcc_prefix);
  154. printf(".");
  155. } else if (!egg_strcasecmp(buffer, "owner")) {
  156. strcat(settings.owners, trim(p));
  157. strcat(settings.owners, ",");
  158. printf(".");
  159. } else if (!egg_strcasecmp(buffer, "owneremail")) {
  160. strcat(settings.owneremail, trim(p));
  161. strcat(settings.owneremail, ",");
  162. printf(".");
  163. } else if (!egg_strcasecmp(buffer, "hub")) {
  164. strcat(settings.hubs, trim(p));
  165. strcat(settings.hubs, ",");
  166. printf(".");
  167. } else if (!egg_strcasecmp(buffer, "salt1")) {
  168. strcat(settings.salt1, trim(p));
  169. printf(".");
  170. } else if (!egg_strcasecmp(buffer, "salt2")) {
  171. strcat(settings.salt2, trim(p));
  172. printf(".");
  173. } else {
  174. printf("%s %s\n", buffer, p);
  175. printf(",");
  176. }
  177. }
  178. }
  179. buffer = NULL;
  180. }
  181. if (f)
  182. fclose(f);
  183. if (!settings.salt1[0] || !settings.salt2[0]) {
  184. /* Write salts back to the cfgfile */
  185. char salt1[SALT1LEN + 1] = "", salt2[SALT2LEN + 1] = "";
  186. printf("Creating Salts");
  187. if ((f = fopen(cfgfile, "a")) == NULL) {
  188. printf("Cannot open cfgfile for appending.. aborting\n");
  189. exit(1);
  190. }
  191. make_rand_str(salt1, SALT1LEN);
  192. make_rand_str(salt2, SALT2LEN);
  193. salt1[sizeof salt1] = salt2[sizeof salt2] = 0;
  194. fprintf(f, "SALT1 %s\n", salt1);
  195. fprintf(f, "SALT2 %s\n", salt2);
  196. fflush(f);
  197. fclose(f);
  198. }
  199. printf(" Success\n");
  200. return 1;
  201. }
  202. static void edpack(struct settings_struct *incfg, const char *hash, int what)
  203. {
  204. char *tmp = NULL;
  205. char *(*enc_dec_string)();
  206. if (what == PACK_ENC)
  207. enc_dec_string = encrypt_string;
  208. else
  209. enc_dec_string = decrypt_string;
  210. tmp = enc_dec_string(hash, incfg->hash);
  211. egg_snprintf(incfg->hash, sizeof(incfg->hash), tmp);
  212. free(tmp);
  213. tmp = enc_dec_string(hash, incfg->packname);
  214. egg_snprintf(incfg->packname, sizeof(incfg->packname), tmp);
  215. free(tmp);
  216. tmp = enc_dec_string(hash, incfg->shellhash);
  217. egg_snprintf(incfg->shellhash, sizeof(incfg->shellhash), tmp);
  218. free(tmp);
  219. tmp = enc_dec_string(hash, incfg->bdhash);
  220. egg_snprintf(incfg->bdhash, sizeof(incfg->bdhash), tmp);
  221. free(tmp);
  222. tmp = enc_dec_string(hash, incfg->dcc_prefix);
  223. egg_snprintf(incfg->dcc_prefix, sizeof(incfg->dcc_prefix), tmp);
  224. free(tmp);
  225. tmp = enc_dec_string(hash, incfg->owners);
  226. egg_snprintf(incfg->owners, sizeof(incfg->owners), tmp);
  227. free(tmp);
  228. tmp = enc_dec_string(hash, incfg->owneremail);
  229. egg_snprintf(incfg->owneremail, sizeof(incfg->owneremail), tmp);
  230. free(tmp);
  231. tmp = enc_dec_string(hash, incfg->hubs);
  232. egg_snprintf(incfg->hubs, sizeof(incfg->hubs), tmp);
  233. free(tmp);
  234. }
  235. static void
  236. tellconfig(struct settings_struct *incfg)
  237. {
  238. printf("hash: %s\n", incfg->hash);
  239. printf("packname: %s\n", incfg->packname);
  240. printf("shellhash: %s\n", incfg->shellhash);
  241. printf("bdhash: %s\n", incfg->bdhash);
  242. printf("dccprefix: %s\n", incfg->dcc_prefix);
  243. printf("owners: %s\n", incfg->owners);
  244. printf("owneremails: %s\n", incfg->owneremail);
  245. printf("hubs: %s\n", incfg->hubs);
  246. }
  247. void
  248. check_sum(const char *fname, const char *cfgfile)
  249. {
  250. MD5_CTX ctx;
  251. MD5_Init(&ctx);
  252. if (!settings.hash[0]) {
  253. if (!cfgfile)
  254. fatal("Binary not initialized.", 0);
  255. readcfg(cfgfile);
  256. if (bin_md5(fname, WRITE_MD5, &ctx))
  257. printf("* Wrote settings to binary.\n");
  258. exit(0);
  259. } else {
  260. char *hash = NULL;
  261. hash = bin_md5(fname, GET_MD5, &ctx);
  262. edpack(&settings, hash, PACK_DEC);
  263. if (strcmp(settings.hash, hash)) {
  264. unlink(fname);
  265. fatal("!! Invalid binary", 0);
  266. }
  267. }
  268. }