binary.c 8.8 KB

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