Change smack labeling to be appId based.
[platform/core/security/security-manager.git] / src / common / include / privilege_db.h
index b56f834..cf8bdcf 100644 (file)
@@ -45,6 +45,7 @@ const char *const PRIVILEGE_DB_PATH = tzplatform_mkpath(TZ_SYS_DB, ".security-ma
 
 enum class QueryType {
     EGetPkgPrivileges,
+    EGetAppPrivileges,
     EAddApplication,
     ERemoveApplication,
     EAddAppPrivileges,
@@ -52,6 +53,8 @@ enum class QueryType {
     EPkgIdExists,
     EGetPkgId,
     EGetPrivilegeGroups,
+    EGetUserApps,
+    EGetAppsInPkg
 };
 
 class PrivilegeDb {
@@ -60,9 +63,17 @@ class PrivilegeDb {
      */
 
 private:
+    /**
+     * Constructor
+     * @exception DB::SqlConnection::Exception::IOError on problems with database access
+     *
+     */
+    PrivilegeDb(const std::string &path = std::string(PRIVILEGE_DB_PATH));
+
     SecurityManager::DB::SqlConnection *mSqlConnection;
     const std::map<QueryType, const char * const > Queries = {
         { QueryType::EGetPkgPrivileges, "SELECT DISTINCT privilege_name FROM app_privilege_view WHERE pkg_name=? AND uid=? ORDER BY privilege_name"},
+        { QueryType::EGetAppPrivileges, "SELECT DISTINCT privilege_name FROM app_privilege_view WHERE app_name=? AND uid=? ORDER BY privilege_name"},
         { QueryType::EAddApplication, "INSERT INTO app_pkg_view (app_name, pkg_name, uid) VALUES (?, ?, ?)" },
         { QueryType::ERemoveApplication, "DELETE FROM app_pkg_view WHERE app_name=? AND uid=?" },
         { QueryType::EAddAppPrivileges, "INSERT INTO app_privilege_view (app_name, uid, privilege_name) VALUES (?, ?, ?)" },
@@ -70,9 +81,34 @@ private:
         { QueryType::EPkgIdExists, "SELECT * FROM pkg WHERE name=?" },
         { QueryType::EGetPkgId, " SELECT pkg_name FROM app_pkg_view WHERE app_name = ?" },
         { QueryType::EGetPrivilegeGroups, " SELECT name FROM privilege_group_view WHERE privilege_name = ?" },
+        { QueryType::EGetUserApps, "SELECT name FROM app WHERE uid=?" },
+        { QueryType::EGetAppsInPkg, " SELECT app_name FROM app_pkg_view WHERE pkg_name = ?" },
     };
 
     /**
+     * Container for initialized DataCommands, prepared for binding.
+     */
+    std::vector<DB::SqlConnection::DataCommandAutoPtr> m_commands;
+
+    /**
+     * Fills empty m_commands map with sql commands prepared for binding.
+     *
+     * Because the "sqlite3_prepare_v2" function takes many cpu cycles, the PrivilegeDb
+     * is optimized to call it only once for one query type.
+     * Designed to be used in the singleton contructor.
+     */
+    void initDataCommands();
+
+    /**
+     * Return prepared query for given query type.
+     * The query will be reset before returning.
+     *
+     * @param queryType query identifier
+     * @return reference to prepared, reset query
+     */
+    DB::SqlConnection::DataCommandAutoPtr & getQuery(QueryType queryType);
+
+    /**
      * Check if pkgId is already registered in database
      *
      * @param pkgId - package identifier
@@ -91,15 +127,10 @@ public:
         DECLARE_EXCEPTION_TYPE(Base, InternalError)
     };
 
-    /**
-     * Constructor
-     * @exception DB::SqlConnection::Exception::IOError on problems with database access
-     *
-     */
-    PrivilegeDb(const std::string &path = std::string(PRIVILEGE_DB_PATH));
-
     ~PrivilegeDb(void);
 
+    static PrivilegeDb &getInstance();
+
     /**
      * Begin transaction
      * @exception DB::SqlConnection::Exception::InternalError on internal error
@@ -143,16 +174,26 @@ public:
             std::vector<std::string> &currentPrivilege);
 
     /**
+     * Retrieve list of privileges assigned to an appId
+     *
+     * @param appId - application identifier
+     * @param uid - user identifier for whom privileges will be retrieved
+     * @param[out] currentPrivileges - list of current privileges assigned to appId
+     * @exception DB::SqlConnection::Exception::InternalError on internal error
+     */
+    void GetAppPrivileges(const std::string &appId, uid_t uid,
+        std::vector<std::string> &currentPrivileges);
+
+    /**
      * Add an application into the database
      *
      * @param appId - application identifier
      * @param pkgId - package identifier
      * @param uid - user identifier for whom application is going to be installed
-     * @param[out] pkgIdIsNew - return info if pkgId is new to the database
      * @exception DB::SqlConnection::Exception::InternalError on internal error
      */
     void AddApplication(const std::string &appId, const std::string &pkgId,
-            uid_t uid, bool &pkgIdIsNew);
+            uid_t uid);
 
     /**
      * Remove an application from the database
@@ -195,6 +236,25 @@ public:
     void GetPrivilegeGroups(const std::string &privilege,
         std::vector<std::string> &grp_names);
 
+    /**
+     * Retrieve list of apps assigned to user
+     *
+     * @param uid - user identifier
+     * @param[out] apps - list of apps assigned to user,
+     *                    this parameter do not need to be empty, but
+     *                    it is being overwritten during function call.
+     * @exception DB::SqlConnection::Exception::InternalError on internal error
+     */
+    void GetUserApps(uid_t uid, std::vector<std::string> &apps);
+    /**
+     * Retrieve a list of all application ids for a package id
+     *
+     * @param pkgId - package id
+     * @param[out] appIds - list of application ids for the package id
+     * @exception DB::SqlConnection::Exception::InternalError on internal error
+     */
+    void GetAppIdsForPkgId (const std::string &pkgId,
+        std::vector<std::string> &appIds);
 };
 
 } //namespace SecurityManager