Add getting pkgId form socket using new SM API 79/50179/9
authorAdam Malinowski <a.malinowsk2@partner.samsung.com>
Mon, 26 Oct 2015 13:25:31 +0000 (14:25 +0100)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Mon, 15 Feb 2016 14:51:32 +0000 (15:51 +0100)
Change-Id: Iabb1c021fd98c3998b4f7031f042d1c35a611fa6

src/manager/main/socket-2-id-mockup.cpp
src/manager/main/socket-2-id-wrapper.cpp
src/manager/main/socket-2-id.h

index 1d103cb..8f59680 100644 (file)
 #include <protocols.h>
 #include <socket-2-id.h>
 
-namespace CKM {
+namespace {
 
-int Socket2Id::getPkgIdFromSmack(const std::string &smack, std::string &pkgId)
-{
+int getPkgIdFromSmack(const std::string &smack, std::string &pkgId) {
     static const std::string SMACK_PREFIX_APPID  = "User::App::";
 
     if (smack.empty()) {
@@ -53,8 +52,11 @@ int Socket2Id::getPkgIdFromSmack(const std::string &smack, std::string &pkgId)
     return 0;
 }
 
-int Socket2Id::translate(int sock, std::string &result)
-{
+} // namespace anonymous
+
+namespace CKM {
+
+int Socket2Id::translate(int sock, std::string &result) {
     std::string smack;
     std::string pkgId;
 
index 2c037f8..e93474d 100644 (file)
 #include <protocols.h>
 #include <socket-2-id.h>
 
-namespace CKM {
-
-int Socket2Id::getPkgIdFromSmack(const std::string &smack, std::string &pkgId)
-{
-    // TODO
-    // Conversion from smack label to pkgId should be done
-    // by security-manager. Current version of security-manager
-    // does not support this feature yet.
-
-    static const std::string SMACK_PREFIX_APPID  = "User::App::";
-
-    if (smack.empty()) {
-        LogError("Smack is empty. Connection will be rejected");
-        return -1;
-    }
-
-    if (smack.compare(0, SMACK_PREFIX_APPID.size(), SMACK_PREFIX_APPID)) {
-        pkgId = "/" + smack;
-        LogDebug("Smack: " << smack << " Was translated to owner id: " << pkgId);
-        return 0;
-    }
-
-    std::string appId = smack.substr(SMACK_PREFIX_APPID.size(), std::string::npos);
+namespace {
 
+int getPkgIdFromSocket(int sock, std::string &pkgId) {
     char *pkg = nullptr;
 
-    if (0 > security_manager_get_app_pkgid(&pkg, appId.c_str())) {
-        LogError("Error in security_manager_get_app_pkgid");
-        return -1;
+    int ret = security_manager_identify_app_from_socket(sock, &pkg, nullptr);
+
+    if (ret == SECURITY_MANAGER_ERROR_NO_SUCH_OBJECT) {
+        LogError("Owner of socket is not connected with pkgid.");
+        return 1;
     }
 
-    if (!pkg) {
-        LogError("PkgId could not be NULL");
+    if (ret != SECURITY_MANAGER_SUCCESS) {
+        LogError("security_manager_identify_app_from_socket failed with error: "
+                 << ret);
         return -1;
     }
 
     pkgId = pkg;
     free(pkg);
-    LogDebug("Smack: " << smack << " Was translated to owner id: " << pkgId);
+    LogDebug("Socket: " << sock << " Was translated to owner id: " << pkgId);
     return 0;
 }
 
-int Socket2Id::translate(int sock, std::string &result)
-{
+} // namespace anonymous
+
+namespace CKM {
+
+int Socket2Id::translate(int sock, std::string &result) {
     std::string smack;
 
     if (0 > getCredentialsFromSocket(sock, smack))
@@ -83,8 +68,15 @@ int Socket2Id::translate(int sock, std::string &result)
     }
 
     std::string pkgId;
-    if (0 > getPkgIdFromSmack(smack, pkgId))
+    int retCode = getPkgIdFromSocket(sock, pkgId);
+
+    if (1 == retCode) {
+        pkgId = "/" + smack;
+    }
+
+    if (0 > retCode) {
         return -1;
+    }
 
     result = pkgId;
     m_stringMap.emplace(std::move(smack), std::move(pkgId));
index 85b0ef1..1fe025d 100644 (file)
@@ -36,7 +36,6 @@ public:
 
 private:
     int getCredentialsFromSocket(int sock, std::string &res);
-    int getPkgIdFromSmack(const std::string &smack, std::string &pkgId);
 
     typedef std::map<std::string, std::string> StringMap;
     StringMap m_stringMap;