binary.c 8.3 KB

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