Alias list returns only aliases available to the calling label. 51/28851/1
authorMaciej J. Karpiuk <m.karpiuk2@samsung.com>
Wed, 1 Oct 2014 14:27:21 +0000 (16:27 +0200)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Thu, 16 Oct 2014 13:44:30 +0000 (15:44 +0200)
All CKM tests pass now.

Change-Id: I719d830cb5ea0e3cd1dda367a8c927a35095e225

src/manager/service/ckm-logic.cpp
src/manager/service/db-crypto.cpp
src/manager/service/db-crypto.h

index 41b2db5..e6f0cbb 100644 (file)
@@ -462,9 +462,9 @@ RawBuffer CKMLogic::getDataList(
         auto &handler = m_userDataMap[cred.uid];
         Try {
             if (dataType == DBDataType::CERTIFICATE || dataType == DBDataType::BINARY_DATA) {
-                handler.database.getAliases(dataType, aliasVector);
+                handler.database.getAliases(cred.smackLabel, dataType, aliasVector);
             } else {
-                handler.database.getKeyAliases(aliasVector);
+                handler.database.getKeyAliases(cred.smackLabel, aliasVector);
             }
         } Catch (CKM::Exception) {
             LogError("Failed to get aliases");
index 02a6afd..519dcf2 100644 (file)
@@ -81,17 +81,6 @@ namespace {
             //                     2     3
             " AND dataType BETWEEN ? AND ?;";
 
-    const char *select_key_type_cmd =
-            "SELECT alias FROM CKM_TABLE WHERE "
-            //                1
-                " dataType >= ? AND "
-            //                2
-                " dataType <= ?;";
-
-    const char *select_type_cmd =
-            //                                          1
-            "SELECT alias FROM CKM_TABLE WHERE dataType=?;";
-
     const char *delete_alias_cmd =
             //                                 1
             "DELETE FROM CKM_TABLE WHERE alias=?;";
@@ -137,6 +126,22 @@ namespace {
     const char *delete_permission_cmd =
             //                                        1           2
             "DELETE FROM PERMISSION_TABLE WHERE alias=? AND label=?;";
+
+
+// CKM_TABLE x PERMISSION_TABLE
+
+    const char *select_type_cross_cmd =
+            //                                                1              2                                                                     3
+            "SELECT c.alias FROM CKM_TABLE c WHERE c.dataType=? AND (c.label=? OR c.alias IN (SELECT p.alias FROM PERMISSION_TABLE p WHERE p.label=?));";
+
+    const char *select_key_type_cross_cmd =
+            "SELECT c.alias FROM CKM_TABLE c WHERE "
+            //                  1
+                " c.dataType >= ? AND "
+            //                  2
+                " c.dataType <= ? AND "
+            //             3                                                                     4
+                " (c.label=? OR c.alias IN (SELECT p.alias FROM PERMISSION_TABLE p WHERE p.label=?));";
 }
 
 namespace CKM {
@@ -435,13 +440,16 @@ using namespace DB;
     }
 
     void DBCrypto::getSingleType(
+            const std::string &clnt_label,
             DBDataType type,
             AliasVector& aliases) const
     {
         Try{
             SqlConnection::DataCommandUniquePtr selectCommand =
-                            m_connection->PrepareDataCommand(select_type_cmd);
+                            m_connection->PrepareDataCommand(select_type_cross_cmd);
             selectCommand->BindInteger(1, static_cast<int>(type));
+            selectCommand->BindString(2, clnt_label.c_str());
+            selectCommand->BindString(3, clnt_label.c_str());
 
             while(selectCommand->Step()) {
                 Alias alias;
@@ -461,21 +469,24 @@ using namespace DB;
     }
 
     void DBCrypto::getAliases(
+        const std::string &clnt_label,
         DBDataType type,
         AliasVector& aliases)
     {
-        getSingleType(type, aliases);
+        getSingleType(clnt_label, type, aliases);
     }
 
 
-    void DBCrypto::getKeyAliases(AliasVector &aliases)
+    void DBCrypto::getKeyAliases(const std::string &clnt_label, AliasVector &aliases)
     {
         Try{
             Transaction transaction(this);
             SqlConnection::DataCommandUniquePtr selectCommand =
-                            m_connection->PrepareDataCommand(select_key_type_cmd);
+                            m_connection->PrepareDataCommand(select_key_type_cross_cmd);
             selectCommand->BindInteger(1, static_cast<int>(DBDataType::DB_KEY_FIRST));
             selectCommand->BindInteger(2, static_cast<int>(DBDataType::DB_KEY_LAST));
+            selectCommand->BindString(3, clnt_label.c_str());
+            selectCommand->BindString(4, clnt_label.c_str());
 
             while(selectCommand->Step()) {
                 Alias alias;
index ab35a03..cf6922e 100644 (file)
@@ -73,9 +73,10 @@ namespace CKM {
                     const Alias &alias,
                     const std::string &clnt_label);
             void getAliases(
+                    const std::string &clnt_label,
                     DBDataType dataType,
                     AliasVector &aliases);
-            void getKeyAliases(AliasVector &aliases);
+            void getKeyAliases(const std::string &clnt_label, AliasVector &aliases);
             bool deleteDBRow(
                     const Alias& alias,
                     const std::string &clnt_label);
@@ -207,7 +208,7 @@ namespace CKM {
             bool checkAliasExist(const std::string &alias) const;
             std::string getLabelForAlias(const std::string& alias) const;
             bool checkGlobalAliasExist(const std::string& alias) const;
-            void getSingleType(DBDataType type, AliasVector& aliases) const;
+            void getSingleType(const std::string &clnt_label, DBDataType type, AliasVector& aliases) const;
    };
 } // namespace CKM