tizen 2.4 release
[framework/security/key-manager.git] / src / manager / service / file-system.cpp
index 7a02fe1..e2b953a 100644 (file)
@@ -47,44 +47,44 @@ const std::string CKM_KEY_BACKUP_PREFIX = "key-backup-";
 const std::string CKM_DB_KEY_PREFIX = "db-key-";
 const std::string CKM_DB_PREFIX = "db-";
 const std::string CKM_REMOVED_APP_PREFIX = "removed-app-";
-const std::string CKM_LOCK_FILE = "/var/run/key-manager.pid";
+const std::string CKM_LOCK_FILE = "/run/key-manager.pid";
 
 } // namespace anonymous
 
 namespace CKM {
 
-FileSystem::FileSystem(uid_t uid)
-  : m_uid(uid)
+FileSystem::FileSystem(const ClientID &clientID)
+  : m_clientID(clientID)
 {}
 
 std::string FileSystem::getDBPath() const
 {
     std::stringstream ss;
-    ss << CKM_DATA_PATH << CKM_DB_PREFIX << m_uid;
+    ss << CKM_DATA_PATH << CKM_DB_PREFIX << m_clientID;
     return ss.str();
 }
 
 std::string FileSystem::getDKEKPath() const {
     std::stringstream ss;
-    ss << CKM_DATA_PATH << CKM_KEY_PREFIX << m_uid;
+    ss << CKM_DATA_PATH << CKM_KEY_PREFIX << m_clientID;
     return ss.str();
 }
 
 std::string FileSystem::getDKEKBackupPath() const {
     std::stringstream ss;
-    ss << CKM_DATA_PATH << CKM_KEY_BACKUP_PREFIX << m_uid;
+    ss << CKM_DATA_PATH << CKM_KEY_BACKUP_PREFIX << m_clientID;
     return ss.str();
 }
 
 std::string FileSystem::getDBDEKPath() const {
     std::stringstream ss;
-    ss << CKM_DATA_PATH << CKM_DB_KEY_PREFIX << m_uid;
+    ss << CKM_DATA_PATH << CKM_DB_KEY_PREFIX << m_clientID;
     return ss.str();
 }
 
 std::string FileSystem::getRemovedAppsPath() const {
     std::stringstream ss;
-    ss << CKM_DATA_PATH << CKM_REMOVED_APP_PREFIX << m_uid;
+    ss << CKM_DATA_PATH << CKM_REMOVED_APP_PREFIX << m_clientID;
     return ss.str();
 }
 
@@ -217,15 +217,15 @@ int FileSystem::init() {
     return 0;
 }
 
-UidVector FileSystem::getUIDsFromDBFile() {
-    UidVector uids;
+ClientIDVector FileSystem::getClientIDsFromDBFile(const std::string zone) {
+    ClientIDVector clientIDVec;
     std::unique_ptr<DIR, std::function<int(DIR*)>>
         dirp(::opendir(CKM_DATA_PATH.c_str()), ::closedir);
 
     if (!dirp.get()) {
         int err = errno;
         LogError("Error in opendir. Data directory could not be read. Error: " << GetErrnoString(err));
-        return UidVector();
+        return ClientIDVector();
     }
 
     size_t len = offsetof(struct dirent, d_name) + pathconf(CKM_DATA_PATH.c_str(), _PC_NAME_MAX) + 1;
@@ -234,20 +234,31 @@ UidVector FileSystem::getUIDsFromDBFile() {
 
     if (!pEntry.get()) {
         LogError("Memory allocation failed.");
-        return UidVector();
+        return ClientIDVector();
     }
 
     struct dirent* pDirEntry = NULL;
 
     while ( (!readdir_r(dirp.get(), pEntry.get(), &pDirEntry)) && pDirEntry ) {
-        // Ignore files with diffrent prefix
         if (strncmp(pDirEntry->d_name, CKM_KEY_PREFIX.c_str(), CKM_KEY_PREFIX.size())) {
+            LogDebug("Not DomainKEK file.");
             continue;
         }
-
-        // We find database. Let's extract user id.
+#ifdef DB_PER_ZONE_ENABLE
+        if (strlen(pDirEntry->d_name + CKM_KEY_PREFIX.size()) <= zone.size()) {
+            LogError("Should not happen. "
+                "Key file[" << pDirEntry->d_name << "] clientID(zone + uid) length should be longer than zone name size");
+            continue;
+        }
+        if (strncmp(pDirEntry->d_name + CKM_KEY_PREFIX.size(), zone.c_str(), zone.size())) {
+            LogDebug("Another zone's DomainKEK file.");
+            continue;
+        }
+#else
+        (void) zone;
+#endif
         try {
-            uids.push_back(static_cast<uid_t>(std::stoi((pDirEntry->d_name)+CKM_KEY_PREFIX.size())));
+            clientIDVec.push_back(pDirEntry->d_name + CKM_KEY_PREFIX.size());
         } catch (const std::invalid_argument) {
             LogError("Error in extracting uid from db file. Error=std::invalid_argument."
                 "This will be ignored.File=" << pDirEntry->d_name << "");
@@ -256,49 +267,38 @@ UidVector FileSystem::getUIDsFromDBFile() {
                 "This will be ignored. File="<< pDirEntry->d_name << "");
         }
     }
-
-    return uids;
+    return clientIDVec;
 }
 
 int FileSystem::removeUserData() const {
-    int err, retCode = 0;
 
-    if (unlink(getDBPath().c_str())) {
-        retCode = -1;
-        err = errno;
-        LogError("Error in unlink user database: " << getDBPath()
-            << "Errno: " << errno << " " << GetErrnoString(err));
-    }
+    if (removeFile(getDBPath())
+        || removeFile(getDKEKPath())
+        || removeFile(getDKEKBackupPath())
+        || removeFile(getDBDEKPath())
+        || removeFile(getRemovedAppsPath())) {
 
-    if (unlink(getDKEKPath().c_str())) {
-        retCode = -1;
-        err = errno;
-        LogError("Error in unlink user DKEK: " << getDKEKPath()
-            << "Errno: " << errno << " " << GetErrnoString(err));
+        return -1;
     }
 
-    if (unlink(getDKEKBackupPath().c_str())) {
-        retCode = -1;
-        err = errno;
-        LogDebug("Unlink user backup DKEK failed (file probably does not exists): " << getDKEKBackupPath()
-            << "Errno: " << errno << " " << GetErrnoString(err));
-    }
+    return 0;
+}
 
-    if (unlink(getDBDEKPath().c_str())) {
-        retCode = -1;
-        err = errno;
-        LogError("Error in unlink user DBDEK: " << getDBDEKPath()
-            << "Errno: " << errno << " " << GetErrnoString(err));
-    }
+int FileSystem::removeFile(const std::string &path) const
+{
+    if (access(path.c_str(), F_OK) == 0
+        && unlink(path.c_str()) != 0) {
 
-    if (unlink(getRemovedAppsPath().c_str())) {
-        retCode = -1;
-        err = errno;
-        LogError("Error in unlink user's Removed Apps File: " << getRemovedAppsPath()
-            << "Errno: " << errno << " " << GetErrnoString(err));
+        int err = errno;
+
+        LogError("Unlink Path[" << path
+            << "] Errno[" << errno
+            << "] ErrnoString[" << GetErrnoString(err) << "]");
+
+       return -1;
     }
 
-    return retCode;
+    return 0;
 }
 
 FileLock FileSystem::lock()