From 4b00d47d3a5e31cf2e7b41b0ee67e751c08d8f3d Mon Sep 17 00:00:00 2001 From: mr-alice Date: Tue, 30 Aug 2016 21:28:32 +0200 Subject: [PATCH] added permission check for friends to see shared files or not --- .../src/file_sharing/directory_storage.cc | 32 +++++++++++++++---- .../src/file_sharing/directory_storage.h | 12 +++++++ libretroshare/src/file_sharing/p3filelists.cc | 11 +++++-- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/libretroshare/src/file_sharing/directory_storage.cc b/libretroshare/src/file_sharing/directory_storage.cc index f5b90f8d6..60662e701 100644 --- a/libretroshare/src/file_sharing/directory_storage.cc +++ b/libretroshare/src/file_sharing/directory_storage.cc @@ -385,6 +385,14 @@ public: // Low level stuff. Should normally not be used externally. + const FileStorageNode *getNode(DirectoryStorage::EntryIndex indx) const + { + if(checkIndex(indx,FileStorageNode::TYPE_FILE | FileStorageNode::TYPE_DIR)) + return mNodes[indx] ; + else + return NULL ; + } + const DirEntry *getDirEntry(DirectoryStorage::EntryIndex indx) const { if(!checkIndex(indx,FileStorageNode::TYPE_DIR)) @@ -450,7 +458,7 @@ public: hits[0] = 1 ; // because 0 is never the child of anyone for(uint32_t i=0;itype() == FileStorageNode::TYPE_DIR) + if(mNodes[i] != NULL && mNodes[i]->type() == FileStorageNode::TYPE_DIR) { // stamp the kids const DirEntry& de = *static_cast(mNodes[i]) ; @@ -942,12 +950,24 @@ bool LocalDirectoryStorage::extractData(const EntryIndex& indx,DirDetails& d) // here we should update the file sharing flags - d.flags.clear() ; - d.parent_groups.clear(); + return getFileSharingPermissions(indx,d.flags,d.parent_groups) ; +} + +bool LocalDirectoryStorage::getFileSharingPermissions(const EntryIndex& indx,FileStorageFlags& flags,std::list& parent_groups) +{ + RS_STACK_MUTEX(mDirStorageMtx) ; + + flags.clear() ; + parent_groups.clear(); std::string base_dir; - for(DirectoryStorage::EntryIndex i=((d.type==DIR_TYPE_FILE)?((intptr_t)d.parent):((intptr_t)d.ref));;) + const InternalFileHierarchyStorage::FileStorageNode *n = mFileHierarchy->getNode(indx) ; + + if(n == NULL) + return false ; + + for(DirectoryStorage::EntryIndex i=((n->type()==InternalFileHierarchyStorage::FileStorageNode::TYPE_FILE)?((intptr_t)n->parent_index):indx);;) { const InternalFileHierarchyStorage::DirEntry *e = mFileHierarchy->getDirEntry(i) ; @@ -972,8 +992,8 @@ bool LocalDirectoryStorage::extractData(const EntryIndex& indx,DirDetails& d) return false ; } #warning we should use a NodeGroupId here - d.flags = it->second.shareflags; - d.parent_groups = it->second.parent_groups; + flags = it->second.shareflags; + parent_groups = it->second.parent_groups; } return true; diff --git a/libretroshare/src/file_sharing/directory_storage.h b/libretroshare/src/file_sharing/directory_storage.h index c86477d19..4f5a48f38 100644 --- a/libretroshare/src/file_sharing/directory_storage.h +++ b/libretroshare/src/file_sharing/directory_storage.h @@ -164,6 +164,18 @@ public: */ bool getFileInfo(DirectoryStorage::EntryIndex i,FileInfo& info) ; + /*! + * \brief getFileSharingPermissions + * Computes the flags and parent groups for any index. + * \param indx index of the entry to compute the flags for + * \param flags computed flags + * \param parent_groups computed parent groups + * \return + * false if the index is not valid + * false otherwise + */ + bool getFileSharingPermissions(const EntryIndex& indx, FileStorageFlags &flags, std::list& parent_groups); + virtual bool extractData(const EntryIndex& indx,DirDetails& d) ; /*! diff --git a/libretroshare/src/file_sharing/p3filelists.cc b/libretroshare/src/file_sharing/p3filelists.cc index e164a87d0..0d634854d 100644 --- a/libretroshare/src/file_sharing/p3filelists.cc +++ b/libretroshare/src/file_sharing/p3filelists.cc @@ -756,10 +756,17 @@ void p3FileDatabase::handleDirSyncRequest(RsFileListsSyncRequestItem *item) ritem->request_id = item->request_id; ritem->entry_index = item->entry_index ; + std::list node_groups; + FileStorageFlags node_flags; + if(entry_type != DIR_TYPE_DIR) { - P3FILELISTS_DEBUG() << " Directory does not exist anymore, or is not a directory. Answering with proper flags." << std::endl; - + P3FILELISTS_DEBUG() << " Directory does not exist anymore, or is not a directory, or permission denied. Answering with proper flags." << std::endl; + ritem->flags = RsFileListsItem::FLAGS_SYNC_RESPONSE | RsFileListsItem::FLAGS_ENTRY_WAS_REMOVED ; + } + else if(item->entry_index != 0 && (!mLocalSharedDirs->getFileSharingPermissions(item->entry_index,node_flags,node_groups) || !(rsPeers->computePeerPermissionFlags(item->PeerId(),node_flags,node_groups) & RS_FILE_HINTS_BROWSABLE))) + { + std::cerr << "(EE) cannot get file permissions for entry index " << (void*)(intptr_t)item->entry_index << ", or permission denied." << std::endl; ritem->flags = RsFileListsItem::FLAGS_SYNC_RESPONSE | RsFileListsItem::FLAGS_ENTRY_WAS_REMOVED ; } else