2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 * @file FileSystem.cpp
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief Sample service implementation.
24 #include <sys/types.h>
33 #include <dpl/errno_string.h>
34 #include <dpl/log/log.h>
36 #include <file-system.h>
40 const std::string CKM_DATA_PATH = "/opt/data/ckm/";
41 const std::string CKM_KEY_PREFIX = "key-";
42 const std::string CKM_DB_KEY_PREFIX = "db-key-";
43 const std::string CKM_DB_PREFIX = "db-";
44 const std::string CKM_REMOVED_APP_PREFIX = "removed-app-";
45 const std::string CKM_LOCK_FILE = "/var/run/key-manager.pid";
47 } // namespace anonymous
51 FileSystem::FileSystem(uid_t uid)
55 std::string FileSystem::getDBPath() const
58 ss << CKM_DATA_PATH << CKM_DB_PREFIX << m_uid;
62 std::string FileSystem::getDKEKPath() const {
64 ss << CKM_DATA_PATH << CKM_KEY_PREFIX << m_uid;
68 std::string FileSystem::getDBDEKPath() const {
70 ss << CKM_DATA_PATH << CKM_DB_KEY_PREFIX << m_uid;
74 std::string FileSystem::getRemovedAppsPath() const {
76 ss << CKM_DATA_PATH << CKM_REMOVED_APP_PREFIX << m_uid;
80 RawBuffer FileSystem::loadFile(const std::string &path) const {
81 std::ifstream is(path);
86 std::istreambuf_iterator<char> begin(is),end;
87 std::vector<char> buff(begin,end); // This trick does not work with boost vector
89 RawBuffer buffer(buff.size());
90 memcpy(buffer.data(), buff.data(), buff.size());
94 RawBuffer FileSystem::getDKEK() const
96 return loadFile(getDKEKPath());
99 RawBuffer FileSystem::getDBDEK() const
101 return loadFile(getDBDEKPath());
104 bool FileSystem::saveFile(const std::string &path, const RawBuffer &buffer) const {
105 std::ofstream os(path, std::ios::out | std::ofstream::binary);
106 std::copy(buffer.begin(), buffer.end(), std::ostreambuf_iterator<char>(os));
110 bool FileSystem::saveDKEK(const RawBuffer &buffer) const {
111 return saveFile(getDKEKPath(), buffer);
114 bool FileSystem::saveDBDEK(const RawBuffer &buffer) const {
115 return saveFile(getDBDEKPath(), buffer);
118 bool FileSystem::addRemovedApp(const std::string &smackLabel) const
120 std::ofstream outfile;
121 outfile.open(getRemovedAppsPath(), std::ios_base::app);
122 outfile << smackLabel << std::endl;
124 return !outfile.fail();
127 AppLabelVector FileSystem::clearRemovedsApps() const
130 AppLabelVector removedApps;
132 std::ifstream removedAppsFile(getRemovedAppsPath());
133 if (removedAppsFile.is_open()) {
134 while (! removedAppsFile.eof() ) {
135 getline (removedAppsFile,line);
137 removedApps.push_back(line);
139 removedAppsFile.close();
141 // truncate the contents
142 std::ofstream truncateFile;
143 truncateFile.open(getRemovedAppsPath(), std::ofstream::out | std::ofstream::trunc);
144 truncateFile.close();
148 int FileSystem::init() {
150 if ((mkdir(CKM_DATA_PATH.c_str(), 0700)) && (errno != EEXIST)) {
152 LogError("Error in mkdir. Data directory could not be created. Errno: "
153 << err << " (" << GetErrnoString(err) << ")");
154 return -1; // TODO set up some error code
159 UidVector FileSystem::getUIDsFromDBFile() {
161 std::unique_ptr<DIR, std::function<int(DIR*)>>
162 dirp(::opendir(CKM_DATA_PATH.c_str()), ::closedir);
166 LogError("Error in opendir. Data directory could not be read. Error: " << GetErrnoString(err));
170 size_t len = offsetof(struct dirent, d_name) + pathconf(CKM_DATA_PATH.c_str(), _PC_NAME_MAX) + 1;
171 std::unique_ptr<struct dirent, std::function<void(void*)>>
172 pEntry(static_cast<struct dirent*>(::malloc(len)), ::free);
175 LogError("Memory allocation failed.");
179 struct dirent* pDirEntry = NULL;
181 while ( (!readdir_r(dirp.get(), pEntry.get(), &pDirEntry)) && pDirEntry ) {
183 // Ignore files with diffrent prefix
184 if (strncmp(pDirEntry->d_name, CKM_KEY_PREFIX.c_str(), CKM_KEY_PREFIX.size())) {
188 // We find database. Let's extract user id.
190 uids.push_back(static_cast<uid_t>(std::stoi((pDirEntry->d_name)+CKM_KEY_PREFIX.size())));
191 } catch (const std::invalid_argument) {
192 LogError("Error in extracting uid from db file. Error=std::invalid_argument."
193 "This will be ignored.File=" << pDirEntry->d_name << "");
194 } catch(const std::out_of_range) {
195 LogError("Error in extracting uid from db file. Error=std::out_of_range."
196 "This will be ignored. File="<< pDirEntry->d_name << "");
203 int FileSystem::removeUserData() const {
204 int err, retCode = 0;
206 if (unlink(getDBPath().c_str())) {
209 LogError("Error in unlink user database: " << getDBPath()
210 << "Errno: " << errno << " " << GetErrnoString(err));
213 if (unlink(getDKEKPath().c_str())) {
216 LogError("Error in unlink user DKEK: " << getDKEKPath()
217 << "Errno: " << errno << " " << GetErrnoString(err));
220 if (unlink(getDBDEKPath().c_str())) {
223 LogError("Error in unlink user DBDEK: " << getDBDEKPath()
224 << "Errno: " << errno << " " << GetErrnoString(err));
227 if (unlink(getRemovedAppsPath().c_str())) {
230 LogError("Error in unlink user's Removed Apps File: " << getRemovedAppsPath()
231 << "Errno: " << errno << " " << GetErrnoString(err));
237 FileLock FileSystem::lock()
239 FileLock fl(CKM_LOCK_FILE.c_str());