aboutsummaryrefslogtreecommitdiff
path: root/src/Database.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Database.cpp')
-rw-r--r--src/Database.cpp91
1 files changed, 34 insertions, 57 deletions
diff --git a/src/Database.cpp b/src/Database.cpp
index 88ac8e4..9985bb9 100644
--- a/src/Database.cpp
+++ b/src/Database.cpp
@@ -1,7 +1,5 @@
#include "../include/odhtdb/Database.hpp"
#include "../include/odhtdb/Group.hpp"
-#include "../include/odhtdb/LocalUser.hpp"
-#include "../include/odhtdb/RemoteUser.hpp"
#include "../include/odhtdb/Encryption.hpp"
#include "../include/odhtdb/DhtKey.hpp"
#include "../include/odhtdb/bin2hex.hpp"
@@ -61,19 +59,25 @@ namespace odhtdb
return DataView(result, allocationSize);
}
- DatabaseCreateResponse::DatabaseCreateResponse(LocalUser *_nodeAdminUser, shared_ptr<OwnedMemory> _key, shared_ptr<Hash> _hash) :
- nodeAdminUser(_nodeAdminUser),
+ DatabaseCreateResponse::DatabaseCreateResponse(std::shared_ptr<Signature::KeyPair> _nodeAdminKeyPair, std::shared_ptr<OwnedMemory> _nodeAdminGroupId, shared_ptr<OwnedMemory> _key, shared_ptr<Hash> _hash) :
+ nodeAdminKeyPair(_nodeAdminKeyPair),
+ nodeAdminGroupId(_nodeAdminGroupId),
key(_key),
hash(_hash)
{
}
- const LocalUser* DatabaseCreateResponse::getNodeAdminUser() const
+ const shared_ptr<Signature::KeyPair> DatabaseCreateResponse::getNodeAdminKeyPair() const
{
- return nodeAdminUser;
+ return nodeAdminKeyPair;
}
-
+
+ const shared_ptr<OwnedMemory> DatabaseCreateResponse::getNodeAdminGroupId() const
+ {
+ return nodeAdminGroupId;
+ }
+
const shared_ptr<OwnedMemory> DatabaseCreateResponse::getNodeEncryptionKey() const
{
return key;
@@ -262,9 +266,6 @@ namespace odhtdb
if(!ok)
Log::warn("Failed to put request to get old data");
});
-
- //node.listen(CREATE_DATA_HASH, bind(&Database::listenCreateData, this, _1));
- //node.listen(ADD_DATA_HASH, bind(&Database::listenAddData, this, _1));
}
void Database::stopSeeding(const Hash &nodeHash)
@@ -285,27 +286,22 @@ namespace odhtdb
{
databaseStorage.loadNode(nodeHash);
}
-
- unique_ptr<DatabaseCreateResponse> Database::create()
- {
- return create(Signature::KeyPair());
- }
- unique_ptr<DatabaseCreateResponse> Database::create(const Signature::KeyPair &creatorKeyPair)
+ unique_ptr<DatabaseCreateResponse> Database::create()
{
+ shared_ptr<Signature::KeyPair> creatorKeyPair = make_shared<Signature::KeyPair>();
+
// TODO: Should this be declared static? is there any difference in behavior/performance?
boost::uuids::random_generator uuidGen;
auto adminGroupId = uuidGen();
assert(adminGroupId.size() == GROUP_ID_LENGTH);
- auto adminGroup = new Group(adminGroupId.data, ADMIN_PERMISSION);
- LocalUser *nodeAdminUser = LocalUser::create(creatorKeyPair, adminGroup);
// Header
sibs::SafeSerializer serializer;
serializer.add(DATABASE_CREATE_PACKET_STRUCTURE_VERSION); // Packet structure version
u64 timestampCombined = getSyncedTimestampUtc().getCombined();
serializer.add(timestampCombined);
- serializer.add((u8*)nodeAdminUser->getPublicKey().getData(), PUBLIC_KEY_NUM_BYTES);
+ serializer.add((u8*)creatorKeyPair->getPublicKey().getData(), PUBLIC_KEY_NUM_BYTES);
serializer.add(adminGroupId.data, adminGroupId.size());
try
@@ -316,7 +312,7 @@ namespace odhtdb
shared_ptr<Hash> hashRequestKey = make_shared<Hash>(serializer.getBuffer().data(), serializer.getBuffer().size());
databaseStorage.setNodeDecryptionKey(*hashRequestKey, DataView(encryptionKey->data, encryptionKey->size));
- databaseStorage.createStorage(*hashRequestKey, adminGroup, timestampCombined, (const u8*)serializer.getBuffer().data(), serializer.getBuffer().size());
+ databaseStorage.createStorage(*hashRequestKey, creatorKeyPair->getPublicKey(), DataView(adminGroupId.data, adminGroupId.size()), timestampCombined, (const u8*)serializer.getBuffer().data(), serializer.getBuffer().size());
DhtKey dhtKey(*hashRequestKey);
Value createDataValue(move(serializer.getBuffer()));
@@ -327,7 +323,9 @@ namespace odhtdb
Log::warn("Failed to put: %s, what to do?", "Database::create");
});
- return make_unique<DatabaseCreateResponse>(nodeAdminUser, encryptionKey, hashRequestKey);
+ shared_ptr<OwnedMemory> adminGroupIdResponse = make_shared<OwnedMemory>(new u8[GROUP_ID_LENGTH], GROUP_ID_LENGTH);
+ memcpy(adminGroupIdResponse->data, adminGroupId.data, GROUP_ID_LENGTH);
+ return make_unique<DatabaseCreateResponse>(creatorKeyPair, adminGroupIdResponse, encryptionKey, hashRequestKey);
}
catch (EncryptionException &e)
{
@@ -335,9 +333,9 @@ namespace odhtdb
}
}
- void Database::addData(const DatabaseNode &nodeInfo, const LocalUser *userToPerformActionWith, DataView dataToAdd)
+ void Database::addData(const DatabaseNode &nodeInfo, const Signature::KeyPair &userToPerformActionWith, DataView dataToAdd)
{
- if(!userToPerformActionWith->isAllowedToPerformAction(PermissionType::ADD_DATA))
+ if(!databaseStorage.isUserAllowedToAddDataInNode(*nodeInfo.getRequestHash(), userToPerformActionWith.getPublicKey()))
{
// TODO: User might have permission to perform operation, but we haven't got the packet that adds user to the group with the permission,
// or we haven't received the packet that modifies group with the permission to perform the operation.
@@ -345,7 +343,7 @@ namespace odhtdb
// and remote peers would accept our request to perform operation if they haven't received the operation that removes the user from the group.
// How to handle this?
string errMsg = "User ";
- errMsg += userToPerformActionWith->getPublicKey().toString();
+ errMsg += userToPerformActionWith.getPublicKey().toString();
errMsg += " is not allowed to perform the operation: ADD_USER";
throw PermissionDeniedException(errMsg);
}
@@ -359,11 +357,11 @@ namespace odhtdb
DataView encryptionKey(nodeInfo.getNodeEncryptionKey()->data, ENCRYPTION_KEY_BYTE_SIZE);
Encryption encryptedBody(dataToAdd, DataView(), encryptionKey);
DataView requestData = combine(serializer, encryptedBody);
- string signedRequestData = userToPerformActionWith->getPrivateKey().sign(requestData);
- DataView stagedAddObject = combine(userToPerformActionWith->getPublicKey(), signedRequestData);
+ string signedRequestData = userToPerformActionWith.getPrivateKey().sign(requestData);
+ DataView stagedAddObject = combine(userToPerformActionWith.getPublicKey(), signedRequestData);
Hash requestDataHash(stagedAddObject.data, stagedAddObject.size);
DataView encryptedDataView((char*)requestData.data + serializer.getBuffer().size(), requestData.size - serializer.getBuffer().size());
- databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_DATA, userToPerformActionWith->getPublicKey(), timestampCombined, (u8*)stagedAddObject.data, stagedAddObject.size, encryptedDataView);
+ databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_DATA, userToPerformActionWith.getPublicKey(), timestampCombined, (u8*)stagedAddObject.data, stagedAddObject.size, encryptedDataView);
delete[] (char*)requestData.data;
DhtKey dhtKey(requestDataHash);
@@ -377,32 +375,14 @@ namespace odhtdb
});
}
- Group* getGroupWithRightsToAddUserToGroup(const vector<Group*> &groups, Group *groupToAddUserTo)
- {
- for(auto group : groups)
- {
- const auto &groupPermission = group->getPermission();
- if(groupPermission.getFlag(PermissionType::ADD_USER_HIGHER_LEVEL) && groupPermission.getPermissionLevel() < groupToAddUserTo->getPermission().getPermissionLevel())
- {
- return group;
- }
- else if(groupPermission.getFlag(PermissionType::ADD_USER_SAME_LEVEL) && groupPermission.getPermissionLevel() == groupToAddUserTo->getPermission().getPermissionLevel())
- {
- return group;
- }
- }
- return nullptr;
- }
-
- void Database::addUser(const DatabaseNode &nodeInfo, const LocalUser *userToPerformActionWith, const Signature::PublicKey &userToAddPublicKey, Group *groupToAddUserTo)
+ void Database::addUser(const DatabaseNode &nodeInfo, const Signature::KeyPair &userToPerformActionWith, const Signature::PublicKey &userToAddPublicKey, const DataView &groupToAddUserTo)
{
- auto groupWithAddUserRights = getGroupWithRightsToAddUserToGroup(userToPerformActionWith->getGroups(), groupToAddUserTo);
- if(!groupWithAddUserRights)
+ if(!databaseStorage.isUserAllowedToAddUserToGroupInNode(*nodeInfo.getRequestHash(), userToPerformActionWith.getPublicKey(), groupToAddUserTo))
{
string errMsg = "The user ";
- errMsg += userToPerformActionWith->getPublicKey().toString();
+ errMsg += userToPerformActionWith.getPublicKey().toString();
errMsg += " does not belong to any group that is allowed to add an user to the group ";
- errMsg += bin2hex((const char*)groupToAddUserTo->getId().data, groupToAddUserTo->getId().size).c_str();
+ errMsg += bin2hex((const char*)groupToAddUserTo.data, groupToAddUserTo.size).c_str();
throw PermissionDeniedException(errMsg);
}
@@ -413,7 +393,7 @@ namespace odhtdb
serializer.add(DatabaseOperation::ADD_USER);
usize additionalDataOffset = serializer.getBuffer().size();
serializer.add((u8*)userToAddPublicKey.getData(), PUBLIC_KEY_NUM_BYTES);
- serializer.add((uint8_t*)groupToAddUserTo->getId().data, groupToAddUserTo->getId().size);
+ serializer.add((uint8_t*)groupToAddUserTo.data, groupToAddUserTo.size);
// TODO: Should this be declared static? is there any difference in behavior/performance?
boost::uuids::random_generator uuidGen;
@@ -422,11 +402,11 @@ namespace odhtdb
serializer.add(padding.data, padding.size());
DataView requestData { serializer.getBuffer().data(), serializer.getBuffer().size() };
- string signedRequestData = userToPerformActionWith->getPrivateKey().sign(requestData);
- DataView stagedAddObject = combine(userToPerformActionWith->getPublicKey(), signedRequestData);
+ string signedRequestData = userToPerformActionWith.getPrivateKey().sign(requestData);
+ DataView stagedAddObject = combine(userToPerformActionWith.getPublicKey(), signedRequestData);
Hash requestDataHash(stagedAddObject.data, stagedAddObject.size);
DataView additionalDataView((void*)(static_cast<const char*>(requestData.data) + additionalDataOffset), requestData.size - additionalDataOffset);
- databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_USER, userToPerformActionWith->getPublicKey(), timestampCombined, (u8*)stagedAddObject.data, stagedAddObject.size, additionalDataView);
+ databaseStorage.appendStorage(*nodeInfo.getRequestHash(), requestDataHash, DatabaseOperation::ADD_USER, userToPerformActionWith.getPublicKey(), timestampCombined, (u8*)stagedAddObject.data, stagedAddObject.size, additionalDataView);
DhtKey dhtKey(requestDataHash);
Value addDataValue((u8*)stagedAddObject.data, stagedAddObject.size);
@@ -492,10 +472,7 @@ namespace odhtdb
if(deserializer.getSize() < ENCRYPTION_NONCE_BYTE_SIZE)
throw sibs::DeserializeException("Unsigned encrypted body is too small (unable to extract nonce)");
- auto adminGroup = new Group(adminGroupId, ADMIN_PERMISSION);
- // TODO: Username is encrypted, we dont know it... unless we have encryption key, in which case we should modify the user name and set it
- auto creatorUser = RemoteUser::create(userPublicKey, adminGroup);
- databaseStorage.createStorage(hash, adminGroup, creationDate, value->data.data(), value->data.size());
+ databaseStorage.createStorage(hash, userPublicKey, DataView(adminGroupId, GROUP_ID_LENGTH), creationDate, value->data.data(), value->data.size());
}
void Database::deserializeAddRequest(const shared_ptr<dht::Value> &value, const Hash &requestDataHash, const std::shared_ptr<Hash> &nodeHash, const shared_ptr<OwnedMemory> encryptionKey)