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/log/log.h>
35 #include <file-system.h>
39 static const std::string CKM_DATA_PATH = "/opt/data/ckm/";
40 static const std::string CKM_KEY_PREFIX = "key-";
41 static const std::string CKM_DB_KEY_PREFIX = "db-key-";
42 static const std::string CKM_DB_PREFIX = "db-";
43 static const std::string CKM_REMOVED_APP_PREFIX = "removed-app-";
45 } // namespace anonymous
49 FileSystem::FileSystem(uid_t uid)
53 std::string FileSystem::getDBPath() const
56 ss << CKM_DATA_PATH << CKM_DB_PREFIX << m_uid;
60 std::string FileSystem::getDKEKPath() const {
62 ss << CKM_DATA_PATH << CKM_KEY_PREFIX << m_uid;
66 std::string FileSystem::getDBDEKPath() const {
68 ss << CKM_DATA_PATH << CKM_DB_KEY_PREFIX << m_uid;
72 std::string FileSystem::getRemovedAppsPath() const {
74 ss << CKM_DATA_PATH << CKM_REMOVED_APP_PREFIX << m_uid;
78 RawBuffer FileSystem::loadFile(const std::string &path) const {
79 std::ifstream is(path);
84 std::istreambuf_iterator<char> begin(is),end;
85 std::vector<char> buff(begin,end); // This trick does not work with boost vector
87 RawBuffer buffer(buff.size());
88 memcpy(buffer.data(), buff.data(), buff.size());
92 RawBuffer FileSystem::getDKEK() const
94 return loadFile(getDKEKPath());
97 RawBuffer FileSystem::getDBDEK() const
99 return loadFile(getDBDEKPath());
102 bool FileSystem::saveFile(const std::string &path, const RawBuffer &buffer) const {
103 std::ofstream os(path, std::ios::out | std::ofstream::binary);
104 std::copy(buffer.begin(), buffer.end(), std::ostreambuf_iterator<char>(os));
108 bool FileSystem::saveDKEK(const RawBuffer &buffer) const {
109 return saveFile(getDKEKPath(), buffer);
112 bool FileSystem::saveDBDEK(const RawBuffer &buffer) const {
113 return saveFile(getDBDEKPath(), buffer);
116 bool FileSystem::addRemovedApp(const std::string &smackLabel) const
118 std::ofstream outfile;
119 outfile.open(getRemovedAppsPath(), std::ios_base::app);
120 outfile << smackLabel << std::endl;
122 return !outfile.fail();
125 AppLabelVector FileSystem::clearRemovedsApps() const
128 AppLabelVector removedApps;
130 std::ifstream removedAppsFile(getRemovedAppsPath());
131 if (removedAppsFile.is_open()) {
132 while (! removedAppsFile.eof() ) {
133 getline (removedAppsFile,line);
135 removedApps.push_back(line);
137 removedAppsFile.close();
139 // truncate the contents
140 std::ofstream truncateFile;
141 truncateFile.open(getRemovedAppsPath(), std::ofstream::out | std::ofstream::trunc);
142 truncateFile.close();
146 int FileSystem::init() {
148 if ((mkdir(CKM_DATA_PATH.c_str(), 0700)) && (errno != EEXIST)) {
150 LogError("Error in mkdir. Data directory could not be created. Errno: "
151 << err << " (" << strerror(err) << ")");
152 return -1; // TODO set up some error code
157 UidVector FileSystem::getUIDsFromDBFile() {
162 if((dirp = opendir(CKM_DATA_PATH.c_str())) == NULL) {
164 LogError("Error in opendir. Data directory could not be read. Errno: "
165 << err << " (" << strerror(err) << ")");
169 struct dirent pPrevDirEntry;
170 struct dirent* pDirEntry = NULL;
172 while ( (!readdir_r(dirp, &pPrevDirEntry, &pDirEntry)) && pDirEntry ) {
174 // Ignore files with diffrent prefix
175 if (strncmp(pDirEntry->d_name, CKM_KEY_PREFIX.c_str(), CKM_KEY_PREFIX.size())) {
179 // We find database. Let's extract user id.
181 uids.push_back(static_cast<uid_t>(std::stoi((pDirEntry->d_name)+CKM_KEY_PREFIX.size())));
182 } catch (const std::invalid_argument) {
183 LogError("Error in extracting uid from db file. Error=std::invalid_argument."
184 "This will be ignored.File=" << pDirEntry->d_name << "");
185 } catch(const std::out_of_range) {
186 LogError("Error in extracting uid from db file. Error=std::out_of_range."
187 "This will be ignored. File="<< pDirEntry->d_name << "");
195 int FileSystem::removeUserData() const {
196 int err, retCode = 0;
198 if (unlink(getDBPath().c_str())) {
201 LogError("Error in unlink user database: " << getDBPath()
202 << "Errno: " << errno << " " << strerror(err));
205 if (unlink(getDKEKPath().c_str())) {
208 LogError("Error in unlink user DKEK: " << getDKEKPath()
209 << "Errno: " << errno << " " << strerror(err));
212 if (unlink(getDBDEKPath().c_str())) {
215 LogError("Error in unlink user DBDEK: " << getDBDEKPath()
216 << "Errno: " << errno << " " << strerror(err));
219 if (unlink(getRemovedAppsPath().c_str())) {
222 LogError("Error in unlink user's Removed Apps File: " << getRemovedAppsPath()
223 << "Errno: " << errno << " " << strerror(err));