Example #1
0
void
Log_Init(const char *filename)
{
   time_t ltime;

   if (!filename) {
      return;
   }

   if (file_exists(filename)) {
      file_rotate(filename, 10);
   }

   logState.lock = mutex_alloc();
   logState.f = fopen(filename, "a");
   if (logState.f == NULL) {
      printf(LGPFX" Failed to create log file '%s'\n", filename);
   }
   file_chmod(filename, 0600);
   strncpy(logState.filePath, filename, sizeof logState.filePath);

   ltime = time(NULL);

   LogAlways(LGPFX" new log session: %s", asctime(localtime(&ltime)));
}
Example #2
0
static int
wallet_save_keys(struct wallet *wallet)
{
   struct config *cfg;
   int res;
   int n;

   n = hashtable_getnumentries(wallet->hash_keys);

   Log(LGPFX" saving %u key%s in %sencrypted wallet %s.\n",
       n, n > 1 ? "s" : "",
       wallet->pass ? "encrypted" : "NON-",
       wallet->filename);

   cfg = config_create();
   config_setint64(cfg, n, "numKeys");

   if (wallet->pass) {
      char saltStr[80];
      int64 count = 0;
      bool s;

      res = RAND_bytes(wallet->ckey->salt, sizeof wallet->ckey->salt);
      if (res != 1) {
         res = ERR_get_error();
         Log(LGPFX" RAND_bytes failed: %d\n", res);
         goto exit;
      }
      str_snprintf_bytes(saltStr, sizeof saltStr, NULL,
                         wallet->ckey->salt, sizeof wallet->ckey->salt);
      config_setstring(cfg, saltStr, "encryption.salt");
      s = crypt_set_key_from_passphrase(wallet->pass, wallet->ckey, &count);
      ASSERT(s);
      ASSERT(count >= CRYPT_NUM_ITERATIONS_OLD);
      config_setint64(cfg, count, "encryption.numIterations");
   }

   hashtable_for_each(wallet->hash_keys, wallet_save_key_cb, cfg);

   file_rotate(wallet->filename, 1);
   res = file_create(wallet->filename);
   if (res) {
      Log(LGPFX" failed to create file '%s': %s\n",
          wallet->filename, strerror(res));
      goto exit;
   }
   res = file_chmod(wallet->filename, 0600);
   if (res) {
      Log(LGPFX" failed to chmod 0600 wallet.dat: %s\n",
          strerror(res));
      goto exit;
   }
   res = config_write(cfg, wallet->filename);

exit:
   config_free(cfg);

   return res;
}