// Print all the data include the entity properties
void EntityManager_PrintFull(FILE *ofp, const char *header, const EntityManager *entityManager) {
  htab *t = NULL;

  if (header != NULL) fprintf(ofp, "%s\n", header); // print the header even for NULL items
  if (entityManager == NULL) {
    snprintf(errStr, sizeof(errStr), "EntityManager_PrintFull: entityManager must not be NULL");
    return;
  }
  t = entityManager->Users->Items;
  if (hfirst(t)) {
    do {
      fullPrintUser(ofp, "", (void *)hstuff(t));
    } while (hnext(t));
  }
  t = entityManager->Groups->Items;
  if (hfirst(t)) {
    do {
      fullPrintGroup(ofp, "", (void *)hstuff(t));
    } while (hnext(t));
  }
  t = entityManager->Resources->Items;
  if (hfirst(t)) {
    do {
      fullPrintResource(ofp, "", (void *)hstuff(t));
    } while (hnext(t));
  }
  fprintf(ofp, "\n");
}
STATIC bool getValue(htab *t, const unsigned char *key, unsigned char **val) {
  int16_t len = 0;

  if (t == NULL || key == NULL) {
    assert(LIB_NAME "Hash structure and key string must not be NULL" && (false || Storage_TestMode));
    return false;
  }
  if (Utils_GetCharArrayLen(key, &len, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN) == false) return false;
  len += UTILS_STR_LEN_SIZE;
  if (hfind(t, key, len) == false) {
    return false;
  }
  if (Utils_GetCharArrayLen((unsigned char *)hstuff(t), &len, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN) == false) return false;
  Utils_CreateAndCopyUcString(val, (unsigned char *)hstuff(t), len + UTILS_STR_LEN_SIZE);
  return true;
}
STATIC bool calcHash(const SecureStorageS *storage, unsigned char *hash) {
  int16_t i = 0, len = 0, secretLen = 0;
  htab *t = NULL;
  unsigned char tmpHash[SHA256_LEN], cHash[crypto_auth_BYTES];
  unsigned char *data = NULL;

  if (storage == NULL || hash == NULL) {
    assert(LIB_NAME "Storage structure and hash string must not be NULL" && (false || Storage_TestMode));
    return false;
  }
  t = storage->Data;
  memset(hash, 0, SHA256_LEN);
  if (hfirst(t)) {
    do {
      for (i = 0; i < 2; i++) {
        if (i == 0)
          data = (unsigned char *)hkey(t);
        else
          data = (unsigned char *)hstuff(t);
        if (Utils_GetCharArrayLen(data, &len, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN) == false) return false;
        len += UTILS_STR_LEN_SIZE;
        if (Utils_GetCharArrayLen(storage->caSecret, &secretLen, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN) == false) {
          return false;
        }
        Crypto_CalcHmac(&(storage->caSecret[UTILS_STR_LEN_SIZE]), secretLen, data, len, cHash);
        memcpy(tmpHash, cHash, SHA256_LEN);
        calcHashXor(hash, tmpHash, crypto_auth_BYTES);
      }
    } while (hnext(t));
  }
  return true;
}
STATIC bool updateData(const SecureStorageS *storage, const unsigned char *key, const unsigned char *val) {
  int16_t kLen = 0, vLen = 0;
  unsigned char *keyStr = NULL, *valStr = NULL;
  htab *t = NULL;

  if (storage == NULL || key == NULL || val == NULL) {
    assert(LIB_NAME "Storage structure and key, value strings must not be NULL" && (false || Storage_TestMode));
    return false;
  }
  t = storage->Data;
  clearKey(storage, key);
  if (Utils_GetCharArrayLen(key, &kLen, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN) == false ||
      Utils_GetCharArrayLen(val, &vLen, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN) == false)
    return false;
  if (hadd(t, key, kLen + UTILS_STR_LEN_SIZE, val)) {
    Utils_CreateAndCopyUcString(&keyStr, key, kLen + UTILS_STR_LEN_SIZE);
    hkey(t) = keyStr;
    Utils_CreateAndCopyUcString(&valStr, val, vLen + UTILS_STR_LEN_SIZE);
    hstuff(t) = valStr;
  } else {
    printf("Internal error: item: %s must be free\n", key);
    return false;
  }
  return true;
}
STATIC void freeData(htab *t) {
  if (t == NULL) return;
  while (hcount(t)) {
    Utils_Free(hkey(t));
    Utils_Free(hstuff(t));
    hdel(t);
  }
  hdestroy(t);
}
// Handling the hash and the random entries:
//   Both are not encrypted and not hex represented in the table therefore,
//   they must be converted to hex representation and
//   are saved with prefix to be destinguest when reading the file
STATIC void writeKeyValue(FILE *fp, const SecureStorageS *storage) {
  htab *t = NULL;

  if (storage == NULL) {
    assert(LIB_NAME "Storage structure must not be NULL" && (false || Storage_TestMode));
    return;
  }
  t = storage->Data;
  fprintf(fp, TOTAL_STR_FMT, (int)hcount(t)); // number of items in the hash
  debug_print("Total number of items to write %d\n", (int16_t)hcount(t));
  if (hfirst(t)) {
    do {
      Utils_WriteCharArray(fp, (unsigned char *)hkey(t));
      Utils_WriteCharArray(fp, (unsigned char *)hstuff(t));
      if (SECURE_DEBUG) {
        printDecryptedData("write:", (unsigned char *)hkey(t), (unsigned char *)hstuff(t), storage->caSecret);
      }
    } while (hnext(t));
  }
}
STATIC bool removeUserFromAllGroups(EntityManager *entityManager, const char *name) {
  htab *t = NULL;
  groupData *g = NULL;

  if (entityManager == NULL || name == NULL) {
    assert(LIB_NAME "EntityManager structure and name string must not be NULL" && (false || Entity_TestMode));
    return false;
  }
  t = entityManager->Groups->Items;
  if (hfirst(t)) {
    do {
      g = (groupData *)hstuff(t);
      EntityManager_RemoveUserFromGroup(entityManager, g->Name, name);
    } while (hnext(t));
  }
  return true;
}
STATIC bool clearKey(const SecureStorageS *storage, const unsigned char *key) {
  int16_t len = 0;
  htab *t = NULL;

  if (storage == NULL || key == NULL) {
    assert(LIB_NAME "Storage structure and key string must not be NULL" && (false || Storage_TestMode));
    return false;
  }
  t = storage->Data;
  if (Utils_GetCharArrayLen(key, &len, KEY_VAL_MIN_STR_LEN, KEY_VAL_MAX_STR_LEN) == false) return false;
  if (key != NULL && hfind(t, key, len + UTILS_STR_LEN_SIZE) == true) { // override existing item
    Utils_Free(hkey(t));
    Utils_Free(hstuff(t));
    hdel(t);
    return true;
  }
  return false;
}
STATIC bool removeUserFromAllResources(EntityManager *entityManager, const char *name) {
  htab *t = NULL;
  resourceData *r = NULL;
  void *a = NULL;

  if (entityManager == NULL || name == NULL) {
    assert(LIB_NAME "EntityManager structure and name string must not be NULL" && (false || Entity_TestMode));
    return false;
  }
  t = entityManager->Resources->Items;
  if (hfirst(t)) {
    do {
      r = (resourceData *)hstuff(t);
      if (EntityManager_GetProperty(entityManager, r->Name, ACL_PROPERTY_NAME, &a) == true) {
        Acl_RemoveEntry(a, name);
      }
    } while (hnext(t));
  }
  return true;
}
Beispiel #10
0
int print_rpc_args(json_rpc_t *json_rpc, char *s, size_t n)
{
	int ret;
	size_t rem = n;

	if (hfirst(json_rpc->params_htab)) do
	{
		const char *name;
		json_val_t *val;

		name = (const char *)hkey(json_rpc->params_htab);
		val = hstuff(json_rpc->params_htab);

		ret = print_basic_type(s, rem, name, val);

		s += ret;
		rem -= ret;
	}
	while (hnext(json_rpc->params_htab) && rem > 0);

	return n-rem;
}