binary.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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, newpos = 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. goto fatal;
  70. fseek(f, 0, SEEK_END);
  71. size = ftell(f);
  72. fseek(f, 0, SEEK_SET);
  73. newpos = 0;
  74. while ((len = fread(buf, 1, sizeof(buf) - 1, f))) {
  75. if (fwrite(buf, 1, len, newbin->f) != len)
  76. goto fatal;
  77. newpos += len;
  78. if (!memcmp(buf, &settings.prefix, PREFIXLEN)) { /* found the settings struct! */
  79. MD5_Final(md5out, ctx);
  80. strlcpy(hash, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(hash));
  81. OPENSSL_cleanse(&ctx, sizeof(ctx));
  82. strlcpy(settings.hash, hash, 65);
  83. edpack(&settings, hash, PACK_ENC); /* encrypt the entire struct with the hash (including hash) */
  84. if (todo & WRITE_PACK) {
  85. fwrite(&settings.hash, SIZE_PACK, 1, newbin->f);
  86. sdprintf("writing pack: %d\n", SIZE_PACK);
  87. } else {
  88. char *tmpbuf = (char *) calloc(1, SIZE_PACK);
  89. if ((len = fread(tmpbuf, 1, SIZE_PACK, f))) {
  90. if (fwrite(tmpbuf, 1, len, newbin->f) != len) {
  91. free(tmpbuf);
  92. goto fatal;
  93. }
  94. }
  95. free(tmpbuf);
  96. }
  97. newpos += SIZE_PACK;
  98. if (todo & WRITE_CONF) {
  99. fwrite(&settings.bots, SIZE_CONF, 1, newbin->f);
  100. sdprintf("writing conf: %d\n", SIZE_CONF);
  101. } else {
  102. char *tmpbuf = (char *) calloc(1, SIZE_CONF);
  103. if ((len = fread(tmpbuf, 1, SIZE_CONF, f))) {
  104. if (fwrite(tmpbuf, 1, len, newbin->f) != len) {
  105. free(tmpbuf);
  106. goto fatal;
  107. }
  108. }
  109. free(tmpbuf);
  110. }
  111. newpos += SIZE_CONF;
  112. fseek(newbin->f, newpos + SIZE_PAD, SEEK_SET);
  113. newpos += SIZE_PAD;
  114. /* skip reading over the stuff we already wrote */
  115. fseek(f, newpos, SEEK_SET);
  116. } else if (!hash[0]) /* hash as long as we haven't reached the prefix */
  117. MD5_Update(ctx, buf, len);
  118. }
  119. fclose(f);
  120. if (size != newpos) {
  121. delete newbin;
  122. fatal("Binary corrupted", 0);
  123. }
  124. if (movefile(fname, fname_bak)) {
  125. printf("Failed to move file (%s -> %s): %s\n", fname, fname_bak, strerror(errno));
  126. delete newbin;
  127. fatal("", 0);
  128. }
  129. if (movefile(newbin->file, fname)) {
  130. printf("Failed to move file (%s -> %s): %s\n", newbin->file, fname, strerror(errno));
  131. delete newbin;
  132. fatal("", 0);
  133. }
  134. fixmod(fname);
  135. unlink(fname_bak);
  136. delete newbin;
  137. return hash;
  138. fatal:
  139. if (f)
  140. fclose(f);
  141. delete newbin;
  142. werr(ERR_BINSTAT);
  143. }
  144. return hash;
  145. }
  146. static int
  147. readcfg(const char *cfgfile)
  148. {
  149. FILE *f = NULL;
  150. if ((f = fopen(cfgfile, "r")) == NULL) {
  151. printf("Error: Can't open '%s' for reading\n", cfgfile);
  152. exit(1);
  153. }
  154. char *buffer = NULL, *p = NULL;
  155. int skip = 0, line = 0;
  156. printf("Reading '%s' ", cfgfile);
  157. while ((!feof(f)) && ((buffer = step_thru_file(f)) != NULL)) {
  158. line++;
  159. if ((*buffer)) {
  160. if (strchr(buffer, '\n'))
  161. *(char *) strchr(buffer, '\n') = 0;
  162. if ((skipline(buffer, &skip)))
  163. continue;
  164. if (strchr(buffer, '<') || strchr(buffer, '>')) {
  165. printf(" Failed\n");
  166. printf("%s:%d: error: Look at your configuration file again...\n", cfgfile, line);
  167. exit(1);
  168. }
  169. p = strchr(buffer, ' ');
  170. while (p && (strchr(LISTSEPERATORS, p[0])))
  171. *p++ = 0;
  172. if (p) {
  173. if (!egg_strcasecmp(buffer, "packname")) {
  174. strlcpy(settings.packname, trim(p), sizeof settings.packname);
  175. printf(".");
  176. } else if (!egg_strcasecmp(buffer, "shellhash")) {
  177. strlcpy(settings.shellhash, trim(p), sizeof settings.shellhash);
  178. printf(".");
  179. } else if (!egg_strcasecmp(buffer, "bdhash")) {
  180. strlcpy(settings.bdhash, trim(p), sizeof settings.bdhash);
  181. printf(".");
  182. } else if (!egg_strcasecmp(buffer, "dccprefix")) {
  183. strlcpy(settings.dcc_prefix, trim(p), sizeof settings.dcc_prefix);
  184. printf(".");
  185. } else if (!egg_strcasecmp(buffer, "owner")) {
  186. strcat(settings.owners, trim(p));
  187. strcat(settings.owners, ",");
  188. printf(".");
  189. } else if (!egg_strcasecmp(buffer, "owneremail")) {
  190. strcat(settings.owneremail, trim(p));
  191. strcat(settings.owneremail, ",");
  192. printf(".");
  193. } else if (!egg_strcasecmp(buffer, "hub")) {
  194. strcat(settings.hubs, trim(p));
  195. strcat(settings.hubs, ",");
  196. printf(".");
  197. } else if (!egg_strcasecmp(buffer, "salt1")) {
  198. strcat(settings.salt1, trim(p));
  199. printf(".");
  200. } else if (!egg_strcasecmp(buffer, "salt2")) {
  201. strcat(settings.salt2, trim(p));
  202. printf(".");
  203. } else {
  204. printf("%s %s\n", buffer, p);
  205. printf(",");
  206. }
  207. }
  208. }
  209. buffer = NULL;
  210. }
  211. if (f)
  212. fclose(f);
  213. if (!settings.salt1[0] || !settings.salt2[0]) {
  214. /* Write salts back to the cfgfile */
  215. char salt1[SALT1LEN + 1] = "", salt2[SALT2LEN + 1] = "";
  216. printf("Creating Salts");
  217. if ((f = fopen(cfgfile, "a")) == NULL) {
  218. printf("Cannot open cfgfile for appending.. aborting\n");
  219. exit(1);
  220. }
  221. make_rand_str(salt1, SALT1LEN);
  222. make_rand_str(salt2, SALT2LEN);
  223. salt1[sizeof salt1] = salt2[sizeof salt2] = 0;
  224. fprintf(f, "SALT1 %s\n", salt1);
  225. fprintf(f, "SALT2 %s\n", salt2);
  226. fflush(f);
  227. fclose(f);
  228. }
  229. printf(" Success\n");
  230. return 1;
  231. }
  232. static void edpack(settings_t *incfg, const char *hash, int what)
  233. {
  234. char *tmp = NULL;
  235. char *(*enc_dec_string)(const char *, char *);
  236. if (what == PACK_ENC)
  237. enc_dec_string = encrypt_string;
  238. else
  239. enc_dec_string = decrypt_string;
  240. #define dofield(_field) do { \
  241. tmp = enc_dec_string(hash, _field); \
  242. egg_snprintf(_field, sizeof(_field), tmp); \
  243. free(tmp); \
  244. } while (0)
  245. /* -- STATIC -- */
  246. dofield(incfg->hash);
  247. dofield(incfg->packname);
  248. dofield(incfg->shellhash);
  249. dofield(incfg->bdhash);
  250. dofield(incfg->dcc_prefix);
  251. dofield(incfg->owners);
  252. dofield(incfg->owneremail);
  253. dofield(incfg->hubs);
  254. /* -- DYNAMIC -- */
  255. //printf("BOTS: %s\n", incfg->bots);
  256. dofield(incfg->bots);
  257. //printf("EBOTS: %s\n", incfg->bots);
  258. dofield(incfg->uid);
  259. dofield(incfg->autouname);
  260. dofield(incfg->pscloak);
  261. dofield(incfg->autocron);
  262. dofield(incfg->watcher);
  263. dofield(incfg->uname);
  264. dofield(incfg->username);
  265. dofield(incfg->homedir);
  266. dofield(incfg->binpath);
  267. dofield(incfg->binname);
  268. dofield(incfg->portmin);
  269. dofield(incfg->portmax);
  270. #undef dofield
  271. }
  272. /*
  273. static void
  274. tellconfig(settings_t *incfg)
  275. {
  276. #define dofield(_field) printf("%s: %s\n", #_field, _field);
  277. // -- STATIC --
  278. dofield(incfg->hash);
  279. dofield(incfg->packname);
  280. dofield(incfg->shellhash);
  281. dofield(incfg->bdhash);
  282. dofield(incfg->dcc_prefix);
  283. dofield(incfg->owners);
  284. dofield(incfg->owneremail);
  285. dofield(incfg->hubs);
  286. // -- DYNAMIC --
  287. dofield(incfg->bots);
  288. dofield(incfg->uid);
  289. dofield(incfg->autouname);
  290. dofield(incfg->pscloak);
  291. dofield(incfg->autocron);
  292. dofield(incfg->watcher);
  293. dofield(incfg->uname);
  294. dofield(incfg->username);
  295. dofield(incfg->homedir);
  296. dofield(incfg->binpath);
  297. dofield(incfg->binname);
  298. dofield(incfg->portmin);
  299. dofield(incfg->portmax);
  300. #undef dofield
  301. }
  302. */
  303. void
  304. check_sum(const char *fname, const char *cfgfile)
  305. {
  306. MD5_CTX ctx;
  307. MD5_Init(&ctx);
  308. if (!settings.hash[0]) {
  309. if (!cfgfile)
  310. fatal("Binary not initialized.", 0);
  311. readcfg(cfgfile);
  312. // tellconfig(&settings);
  313. if (bin_checksum(fname, WRITE_CHECKSUM, &ctx))
  314. printf("* Wrote settings to binary.\n");
  315. exit(0);
  316. } else {
  317. char *hash = bin_checksum(fname, GET_CHECKSUM, &ctx);
  318. // tellconfig(&settings);
  319. edpack(&settings, hash, PACK_DEC);
  320. // tellconfig(&settings);
  321. if (strcmp(settings.hash, hash)) {
  322. unlink(fname);
  323. fatal("!! Invalid binary", 0);
  324. }
  325. }
  326. }
  327. static bool check_bin_initialized(const char *fname)
  328. {
  329. int i = 0;
  330. size_t len = strlen(fname) + 3 + 1;
  331. char *path = (char *) calloc(1, len);
  332. egg_snprintf(path, len, "%s -p", fname);
  333. i = system(path);
  334. free(path);
  335. if (i != -1 && WEXITSTATUS(i) == 4)
  336. return 1;
  337. return 0;
  338. }
  339. void write_settings(const char *fname, int die)
  340. {
  341. MD5_CTX ctx;
  342. char *hash = NULL;
  343. int bits = WRITE_CHECKSUM;
  344. /* see if the binary is already initialized or not */
  345. bool initialized = check_bin_initialized(fname);
  346. MD5_Init(&ctx);
  347. /* only write pack data if the binary is uninitialized
  348. * otherwise, assume it has similar/correct/updated pack data
  349. */
  350. if (!initialized)
  351. bits |= WRITE_PACK;
  352. bits |= WRITE_CONF; /* always for now */
  353. if ((hash = bin_checksum(fname, bits, &ctx))) {
  354. printf("* Wrote settings to: %s.\n", fname);
  355. if (die == -1) /* only bother decrypting if we aren't about to exit */
  356. edpack(&settings, hash, PACK_DEC);
  357. }
  358. if (die >= 0)
  359. exit(die);
  360. }
  361. static void
  362. clear_settings(void)
  363. {
  364. memset(&settings.bots, 0, sizeof(settings_t) - 3467);
  365. }
  366. void conf_to_bin(conf_t *in, bool move)
  367. {
  368. conf_bot *bot = NULL;
  369. char *newbin = NULL;
  370. clear_settings();
  371. sdprintf("converting conf to bin\n");
  372. sprintf(settings.uid, "%d", in->uid);
  373. sprintf(settings.watcher, "%d", in->watcher);
  374. sprintf(settings.autocron, "%d", in->autocron);
  375. sprintf(settings.autouname, "%d", in->autouname);
  376. sprintf(settings.portmin, "%d", in->portmin);
  377. sprintf(settings.portmax, "%d", in->portmax);
  378. sprintf(settings.pscloak, "%d", in->pscloak);
  379. strlcpy(settings.binname, in->binname, 16);
  380. strlcpy(settings.username, in->username, 16);
  381. strlcpy(settings.uname, in->uname, 350);
  382. strlcpy(settings.homedir, in->homedir, 350);
  383. strlcpy(settings.binpath, in->binpath, 350);
  384. for (bot = in->bots; bot && bot->nick; bot = bot->next) {
  385. sprintf(settings.bots, "%s%s %s %s%s %s,", settings.bots && settings.bots[0] ? settings.bots : "",
  386. bot->nick,
  387. bot->net.ip ? bot->net.ip : ".",
  388. bot->net.host6 ? "+" : "",
  389. bot->net.host ? bot->net.host : (bot->net.host6 ? bot->net.host6 : "."),
  390. bot->net.ip6 ? bot->net.ip6 : "");
  391. }
  392. if (move)
  393. newbin = move_bin(in->binpath, in->binname, 0);
  394. else
  395. newbin = binname;
  396. /* tellconfig(&settings); */
  397. write_settings(newbin, 1);
  398. }