binary.c 13 KB

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