2 * security-manager, database access
4 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
6 * Contact: Rafal Krypa <r.krypa@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 * @file privilege_db.h
24 * @author Krzysztof Sasiak <k.sasiak@samsung.com>
25 * @author Rafal Krypa <r.krypa@samsung.com>
27 * @brief This file contains declaration of the API to privilges database.
36 #include <dpl/db/sql_connection.h>
37 #include <tzplatform_config.h>
39 #ifndef PRIVILEGE_DB_H_
40 #define PRIVILEGE_DB_H_
42 namespace SecurityManager {
44 const char *const PRIVILEGE_DB_PATH = tzplatform_mkpath(TZ_SYS_DB, ".security-manager.db");
46 enum class QueryType {
58 * PrivilegeDb database class
62 SecurityManager::DB::SqlConnection *mSqlConnection;
63 const std::map<QueryType, const char * const > Queries = {
64 { QueryType::EGetPkgPrivileges, "SELECT privilege_name FROM app_privilege_view WHERE pkg_name=? AND uid=?"},
65 { QueryType::EAddApplication, "INSERT INTO app_pkg_view (app_name, pkg_name, uid) VALUES (?, ?, ?)" },
66 { QueryType::ERemoveApplication, "DELETE FROM app_pkg_view WHERE app_name=? AND uid=?" },
67 { QueryType::EAddAppPrivileges, "INSERT INTO app_privilege_view (app_name, uid, privilege_name) VALUES (?, ?, ?)" },
68 { QueryType::ERemoveAppPrivileges, "DELETE FROM app_privilege_view WHERE app_name=? AND uid=?" },
69 { QueryType::EPkgIdExists, "SELECT * FROM pkg WHERE name=?" },
70 { QueryType::EGetPkgId, " SELECT pkg_name FROM app_pkg_view WHERE app_name = ?" },
74 * Check if pkgId is already registered in database
76 * @param pkgId - package identifier
77 * @exception DB::SqlConnection::Exception::InternalError on internal error
78 * @return true if pkgId exists in the database
81 bool PkgIdExists(const std::string &pkgId);
87 DECLARE_EXCEPTION_TYPE(SecurityManager::Exception, Base)
88 DECLARE_EXCEPTION_TYPE(Base, IOError)
89 DECLARE_EXCEPTION_TYPE(Base, InternalError)
94 * @exception DB::SqlConnection::Exception::IOError on problems with database access
97 PrivilegeDb(const std::string &path = std::string(PRIVILEGE_DB_PATH));
103 * @exception DB::SqlConnection::Exception::InternalError on internal error
106 void BeginTransaction(void);
110 * @exception DB::SqlConnection::Exception::InternalError on internal error
113 void CommitTransaction(void);
116 * Rollback transaction
117 * @exception DB::SqlConnection::Exception::InternalError on internal error
120 void RollbackTransaction(void);
123 * Return package id associated with a given application id
125 * @param appId - application identifier
126 * @param[out] pkgId - return application's pkgId
127 * @return true is application exists, false otherwise
128 * @exception DB::SqlConnection::Exception::InternalError on internal error
130 bool GetAppPkgId(const std::string &appId, std::string &pkgId);
133 * Retrieve list of privileges assigned to a pkgId
135 * @param pkgId - package identifier
136 * @param uid - user identifier for whom privileges will be retrieved
137 * @param[out] currentPrivileges - list of current privileges assigned to pkgId
138 * @exception DB::SqlConnection::Exception::InternalError on internal error
140 void GetPkgPrivileges(const std::string &pkgId, uid_t uid,
141 std::vector<std::string> ¤tPrivilege);
144 * Add an application into the database
146 * @param appId - application identifier
147 * @param pkgId - package identifier
148 * @param uid - user identifier for whom application is going to be installed
149 * @param[out] pkgIdIsNew - return info if pkgId is new to the database
150 * @exception DB::SqlConnection::Exception::InternalError on internal error
152 void AddApplication(const std::string &appId, const std::string &pkgId,
153 uid_t uid, bool &pkgIdIsNew);
156 * Remove an application from the database
158 * @param appId - application identifier
159 * @param uid - user identifier whose application is going to be uninstalled
160 * @param[out] pkgIdIsNoMore - return info if pkgId is in the database
161 * @exception DB::SqlConnection::Exception::InternalError on internal error
163 void RemoveApplication(const std::string &appId, uid_t uid, bool &pkgIdIsNoMore);
166 * Remove privileges assigned to application
168 * @param appId - application identifier
169 * @param uid - user identifier for whom privileges will be removed
170 * @exception DB::SqlConnection::Exception::InternalError on internal error
172 void RemoveAppPrivileges(const std::string &appId, uid_t uid);
175 * Update privileges assigned to application
176 * To assure data integrity this method must be called inside db transaction.
178 * @param appId - application identifier
179 * @param uid - user identifier for whom privileges will be updated
180 * @param privileges - list of privileges to assign
181 * @exception DB::SqlConnection::Exception::InternalError on internal error
183 void UpdateAppPrivileges(const std::string &appId, uid_t uid,
184 const std::vector<std::string> &privileges);
188 } //namespace SecurityManager
190 #endif // PRIVILEGE_DB_H_