binary.c 14 KB

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