aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-05-14 03:07:43 +0200
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:25:46 +0200
commit4241bcd4e14095e4340a0300e205f6fdc503f1d8 (patch)
tree609041efdeac4cd8836c3b0a8569ffdf956c4d5d /src
parentb995e79eaf3ac940547beb902c1e1da5c08308ec (diff)
Remove user/group classes, user public key/group id directly to database instead
Diffstat (limited to 'src')
-rw-r--r--src/Database.cpp91
-rw-r--r--src/DatabaseStorage.cpp98
-rw-r--r--src/Group.cpp52
-rw-r--r--src/LocalUser.cpp11
-rw-r--r--src/LocalUserEncrypted.cpp27
-rw-r--r--src/User.cpp37
6 files changed, 77 insertions, 239 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)
diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp
index 501bd35..d7f42bb 100644
--- a/src/DatabaseStorage.cpp
+++ b/src/DatabaseStorage.cpp
@@ -1,7 +1,4 @@
#include "../include/odhtdb/DatabaseStorage.hpp"
-#include "../include/odhtdb/RemoteUser.hpp"
-#include "../include/odhtdb/LocalUser.hpp"
-#include "../include/odhtdb/LocalUserEncrypted.hpp"
#include "../include/odhtdb/Group.hpp"
#include "../include/odhtdb/FileUtils.hpp"
#include "../include/odhtdb/bin2hex.hpp"
@@ -28,22 +25,6 @@ namespace odhtdb
const u64 QUARANTINE_STORAGE_TIME_MICROSECONDS = 60 * 1.0e6;
const u16 STORAGE_VERSION = 3;
-
- DatabaseStorageObject::DatabaseStorageObject(const Hash &_requestHash, DataView &_data, u64 _timestamp, const Signature::PublicKey &_creatorPublicKey) :
- requestHash(_requestHash),
- data(_data),
- createdTimestamp(_timestamp),
- creatorPublicKey(_creatorPublicKey)
- {
-
- }
-
- DatabaseStorageQuarantineObject::DatabaseStorageQuarantineObject(DataView &_data, u64 _timestamp, const Signature::PublicKey &_creatorPublicKey) :
- data(_data), createdTimestamp(_timestamp), creatorPublicKey(_creatorPublicKey)
- {
- auto time = chrono::high_resolution_clock::now().time_since_epoch();
- storedTimestamp = chrono::duration_cast<chrono::microseconds>(time).count();
- }
static void sqlite_exec_checked(sqlite3 *db, const char *sql)
{
@@ -431,11 +412,8 @@ namespace odhtdb
return query.next();
}
- void DatabaseStorage::createStorage(const Hash &hash, Group *creatorGroup, u64 timestamp, const void *data, usize size)
+ void DatabaseStorage::createStorage(const Hash &hash, const Signature::PublicKey &adminPublicKey, const DataView &adminGroupId, u64 timestamp, const void *data, usize size)
{
- assert(creatorGroup->getUsers().size() == 1);
- User *creator = (User*)creatorGroup->getUsers()[0];
-
sqlite3_exec(sqliteDb, "BEGIN", 0, 0, 0);
{
sqlite3_reset(insertNodeStmt);
@@ -448,15 +426,15 @@ namespace odhtdb
rc = sqlite3_bind_int64(insertNodeStmt, 2, timestamp);
bindCheckError(rc);
- rc = sqlite3_bind_blob(insertNodeStmt, 3, creator->getPublicKey().getData(), creator->getPublicKey().getSize(), SQLITE_STATIC);
+ rc = sqlite3_bind_blob(insertNodeStmt, 3, adminPublicKey.getData(), adminPublicKey.getSize(), SQLITE_STATIC);
bindCheckError(rc);
- rc = sqlite3_bind_blob(insertNodeStmt, 4, creatorGroup->getId().data, GROUP_ID_LENGTH, SQLITE_STATIC);
+ rc = sqlite3_bind_blob(insertNodeStmt, 4, adminGroupId.data, GROUP_ID_LENGTH, SQLITE_STATIC);
bindCheckError(rc);
sqlite_step_rollback_on_failure(sqliteDb, insertNodeStmt, "insert data into Node");
- addGroup(hash, creatorGroup);
- addUser(hash, creator->getPublicKey(), creatorGroup->getId());
+ addGroup(hash, adminGroupId, ADMIN_PERMISSION);
+ addUser(hash, adminPublicKey, adminGroupId);
}
{
sqlite3_reset(insertNodeRawStmt);
@@ -475,7 +453,7 @@ namespace odhtdb
auto nodeDecryptionKeyResult = getNodeDecryptionKey(hash);
if(nodeDecryptionKeyResult.first)
- decryptNodeData(hash, nodeDecryptionKeyResult.second, &creator->getPublicKey(), creatorGroup->getId(), timestamp);
+ decryptNodeData(hash, nodeDecryptionKeyResult.second, &adminPublicKey, adminGroupId, timestamp);
}
void DatabaseStorage::appendStorage(const Hash &nodeHash, const Hash &dataHash, DatabaseOperation operation, const Signature::PublicKey &creatorPublicKey, u64 timestamp, const void *data, usize size, const DataView &additionalDataView)
@@ -591,7 +569,7 @@ namespace odhtdb
sqlite3_exec(sqliteDb, "COMMIT", 0, 0, 0);
}
- void DatabaseStorage::addGroup(const Hash &nodeHash, Group *group)
+ void DatabaseStorage::addGroup(const Hash &nodeHash, const DataView &groupId, const Permission &permissions)
{
sqlite3_reset(insertGroupStmt);
sqlite3_clear_bindings(insertGroupStmt);
@@ -600,17 +578,17 @@ namespace odhtdb
rc = sqlite3_bind_blob(insertGroupStmt, 1, nodeHash.getData(), nodeHash.getSize(), SQLITE_STATIC);
bindCheckError(rc);
- rc = sqlite3_bind_blob(insertGroupStmt, 2, group->getId().data, GROUP_ID_LENGTH, SQLITE_STATIC);
+ rc = sqlite3_bind_blob(insertGroupStmt, 2, groupId.data, GROUP_ID_LENGTH, SQLITE_STATIC);
bindCheckError(rc);
- rc = sqlite3_bind_int(insertGroupStmt, 3, group->getPermission().getPermissionLevel());
+ rc = sqlite3_bind_int(insertGroupStmt, 3, permissions.getPermissionLevel());
bindCheckError(rc);
- rc = sqlite3_bind_int64(insertGroupStmt, 4, group->getPermission().getPermissionFlags());
+ rc = sqlite3_bind_int64(insertGroupStmt, 4, permissions.getPermissionFlags());
bindCheckError(rc);
sqlite_step_rollback_on_failure(sqliteDb, insertGroupStmt, "insert data into NodeGroup");
- Log::debug("Created group %s in node %s", bin2hex((const char*)group->getId().data, GROUP_ID_LENGTH).c_str(), nodeHash.toString().c_str());
+ Log::debug("Created group %s in node %s", bin2hex((const char*)groupId.data, GROUP_ID_LENGTH).c_str(), nodeHash.toString().c_str());
}
void DatabaseStorage::addUserToGroup(const Hash &nodeHash, const Signature::PublicKey &userPublicKey, const DataView &groupId)
@@ -669,6 +647,36 @@ namespace odhtdb
callbackFunc(data);
}
}
+
+ bool DatabaseStorage::isUserAllowedToAddDataInNode(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const
+ {
+ SqlQuery queryCreatorGroupWithRightsToAddData(sqliteDb,
+ "SELECT nodeGroup.rowid FROM NodeUserGroupAssoc AS userGroupAssoc"
+ " INNER JOIN NodeGroup AS nodeGroup ON nodeGroup.groupId = userGroupAssoc.groupId"
+ " WHERE userGroupAssoc.node = ? AND userGroupAssoc.userPublicKey = ? AND (nodeGroup.permissionFlags & ?) != 0",
+ { DataView(nodeHash.getData(), nodeHash.getSize()), DataView((void*)userPublicKey.getData(), userPublicKey.getSize()), (i64)PermissionType::ADD_DATA });
+ return queryCreatorGroupWithRightsToAddData.next();
+ }
+
+ bool DatabaseStorage::isUserAllowedToAddUserToGroupInNode(const Hash &nodeHash, const Signature::PublicKey &userPublicKey, const DataView &groupToAddUserTo) const
+ {
+ SqlQuery queryGroupToAdd(sqliteDb, "SELECT permissionLevel FROM NodeGroup WHERE groupId = ?", { groupToAddUserTo });
+ if(!queryGroupToAdd.next())
+ {
+ // TODO: Add to quarantine?
+ Log::error("There is no group with id %s in node %s", bin2hex((const char*)groupToAddUserTo.data, groupToAddUserTo.size).c_str(), nodeHash.toString().c_str());
+ return false;
+ }
+
+ int groupToAddPermissionLevel = queryGroupToAdd.getInt(0);
+
+ SqlQuery queryCreatorGroupWithRightsToAddUserToGroup(sqliteDb,
+ "SELECT nodeGroup.rowid FROM NodeUserGroupAssoc AS userGroupAssoc"
+ " INNER JOIN NodeGroup AS nodeGroup ON nodeGroup.groupId = userGroupAssoc.groupId"
+ " WHERE userGroupAssoc.node = ? AND userGroupAssoc.userPublicKey = ? AND (nodeGroup.permissionLevel = ? AND ((nodeGroup.permissionFlags & ?) != 0) OR (nodeGroup.permissionLevel > ? AND (nodeGroup.permissionFlags & ?) != 0))",
+ { DataView(nodeHash.getData(), nodeHash.getSize()), DataView((void*)userPublicKey.getData(), userPublicKey.getSize()), groupToAddPermissionLevel, (i64)PermissionType::ADD_USER_SAME_LEVEL, groupToAddPermissionLevel, (i64)PermissionType::ADD_USER_HIGHER_LEVEL });
+ return queryCreatorGroupWithRightsToAddUserToGroup.next();
+ }
#if 0
bool DatabaseStorage::storeLocalUser(const string &username, const Signature::KeyPair &keyPair, const string &password)
{
@@ -943,12 +951,7 @@ namespace odhtdb
bool DatabaseStorage::decryptNodeAddData(i64 rowId, const Hash &nodeHash, const Hash &dataHash, u64 timestamp, const Signature::PublicKey *creatorPublicKey, const DataView &encryptedData, const shared_ptr<OwnedMemory> decryptionKey)
{
- SqlQuery queryCreatorGroupWithRightsToAddData(sqliteDb,
- "SELECT nodeGroup.rowid FROM NodeUserGroupAssoc AS userGroupAssoc"
- " INNER JOIN NodeGroup AS nodeGroup ON nodeGroup.groupId = userGroupAssoc.groupId"
- " WHERE userGroupAssoc.node = ? AND userGroupAssoc.userPublicKey = ? AND (nodeGroup.permissionFlags & ?) != 0",
- { DataView(nodeHash.getData(), nodeHash.getSize()), DataView((void*)creatorPublicKey->getData(), creatorPublicKey->getSize()), (i64)PermissionType::ADD_DATA });
- if(!queryCreatorGroupWithRightsToAddData.next())
+ if(!isUserAllowedToAddDataInNode(nodeHash, *creatorPublicKey))
{
// 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.
@@ -976,22 +979,7 @@ namespace odhtdb
bool DatabaseStorage::decryptNodeAddUser(i64 rowId, const Hash &nodeHash, const Hash &dataHash, u64 timestamp, const Signature::PublicKey *creatorPublicKey, const Signature::PublicKey *userToAddPublicKey, const DataView &groupToAddUserTo, const shared_ptr<OwnedMemory> decryptionKey)
{
- SqlQuery queryGroupToAdd(sqliteDb, "SELECT permissionLevel FROM NodeGroup WHERE groupId = ?", { groupToAddUserTo });
- if(!queryGroupToAdd.next())
- {
- // TODO: Add to quarantine?
- Log::error("There is no group with id %s in node %s", bin2hex((const char*)groupToAddUserTo.data, groupToAddUserTo.size).c_str(), nodeHash.toString().c_str());
- return false;
- }
-
- int groupToAddPermissionLevel = queryGroupToAdd.getInt(0);
-
- SqlQuery queryCreatorGroupWithRightsToAddUserToGroup(sqliteDb,
- "SELECT nodeGroup.rowid FROM NodeUserGroupAssoc AS userGroupAssoc"
- " INNER JOIN NodeGroup AS nodeGroup ON nodeGroup.groupId = userGroupAssoc.groupId"
- " WHERE userGroupAssoc.node = ? AND userGroupAssoc.userPublicKey = ? AND (nodeGroup.permissionLevel = ? AND ((nodeGroup.permissionFlags & ?) != 0) OR (nodeGroup.permissionLevel > ? AND (nodeGroup.permissionFlags & ?) != 0))",
- { DataView(nodeHash.getData(), nodeHash.getSize()), DataView((void*)creatorPublicKey->getData(), creatorPublicKey->getSize()), groupToAddPermissionLevel, (i64)PermissionType::ADD_USER_SAME_LEVEL, groupToAddPermissionLevel, (i64)PermissionType::ADD_USER_HIGHER_LEVEL });
- if(!queryCreatorGroupWithRightsToAddUserToGroup.next())
+ if(!isUserAllowedToAddUserToGroupInNode(nodeHash, *creatorPublicKey, groupToAddUserTo))
{
// 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.
diff --git a/src/Group.cpp b/src/Group.cpp
deleted file mode 100644
index 9db3e4e..0000000
--- a/src/Group.cpp
+++ /dev/null
@@ -1,52 +0,0 @@
-#include "../include/odhtdb/Group.hpp"
-#include "../include/odhtdb/User.hpp"
-#include <cstring>
-
-using namespace std;
-
-namespace odhtdb
-{
- Group::Group(uint8_t _id[GROUP_ID_LENGTH], const Permission &_permission) :
- permission(_permission)
- {
- memcpy(id, _id, GROUP_ID_LENGTH);
- }
-
- Group::~Group()
- {
-
- }
-
- void Group::addUser(const User *user)
- {
- users.push_back(user);
- }
-
- bool Group::removeUser(const User *user)
- {
- for(std::vector<const User*>::iterator it = users.begin(); it != users.end(); ++it)
- {
- if(*it == user)
- {
- users.erase(it);
- return true;
- }
- }
- return false;
- }
-
- DataView Group::getId() const
- {
- return { (void*)id, GROUP_ID_LENGTH };
- }
-
- const Permission& Group::getPermission() const
- {
- return permission;
- }
-
- const vector<const User*>& Group::getUsers() const
- {
- return users;
- }
-}
diff --git a/src/LocalUser.cpp b/src/LocalUser.cpp
deleted file mode 100644
index 950c953..0000000
--- a/src/LocalUser.cpp
+++ /dev/null
@@ -1,11 +0,0 @@
-#include "../include/odhtdb/LocalUser.hpp"
-
-namespace odhtdb
-{
- LocalUser::LocalUser(const Signature::KeyPair &_keyPair, Group *group) :
- User(User::Type::LOCAL, group),
- keyPair(_keyPair)
- {
-
- }
-}
diff --git a/src/LocalUserEncrypted.cpp b/src/LocalUserEncrypted.cpp
deleted file mode 100644
index a414c89..0000000
--- a/src/LocalUserEncrypted.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-#include "../include/odhtdb/LocalUserEncrypted.hpp"
-#include "../include/odhtdb/PasswordHash.hpp"
-#include <cstring>
-
-namespace odhtdb
-{
- EncryptedPrivateKey::EncryptedPrivateKey()
- {
- memset(nonce, 0, ENCRYPTION_NONCE_BYTE_SIZE);
- memset(encryptedPrivateKey, 0, ENCRYPTION_CHECKSUM_BYTE_SIZE + PRIVATE_KEY_NUM_BYTES);
- }
-
- EncryptedPrivateKey::EncryptedPrivateKey(const EncryptedPrivateKey &other)
- {
- memcpy(nonce, other.nonce, ENCRYPTION_NONCE_BYTE_SIZE);
- memcpy(encryptedPrivateKey, other.encryptedPrivateKey, ENCRYPTION_CHECKSUM_BYTE_SIZE + PRIVATE_KEY_NUM_BYTES);
- }
-
- Signature::PrivateKey EncryptedPrivateKey::decrypt(const DataView &plainPassword, const DataView &salt) const
- {
- OwnedMemory hashedPassword = hashPassword(plainPassword, salt);
- Decryption decryptedPrivateKey(DataView((void*)encryptedPrivateKey, ENCRYPTION_CHECKSUM_BYTE_SIZE + PRIVATE_KEY_NUM_BYTES),
- DataView((void*)nonce, ENCRYPTION_NONCE_BYTE_SIZE),
- DataView(hashedPassword.data, hashedPassword.size));
- return { (const char*)decryptedPrivateKey.getDecryptedText().data, decryptedPrivateKey.getDecryptedText().size };
- }
-}
diff --git a/src/User.cpp b/src/User.cpp
deleted file mode 100644
index 559f8db..0000000
--- a/src/User.cpp
+++ /dev/null
@@ -1,37 +0,0 @@
-#include "../include/odhtdb/User.hpp"
-#include "../include/odhtdb/Group.hpp"
-
-namespace odhtdb
-{
- User::User(Type _type, Group *group) : type(_type)
- {
- addToGroup(group);
- }
-
- User::~User()
- {
- for(Group *group : groups)
- {
- group->removeUser(this);
- }
- }
-
- void User::addToGroup(Group *group)
- {
- if(group)
- {
- groups.emplace_back(group);
- group->addUser(this);
- }
- }
-
- bool User::isAllowedToPerformAction(PermissionType action) const
- {
- for(Group *group : getGroups())
- {
- if(group->getPermission().getFlag(action))
- return true;
- }
- return false;
- }
-}