Extract BaseService from Service class
[platform/core/security/security-manager.git] / src / common / include / privilege_db.h
1 /*
2  * security-manager, database access
3  *
4  * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Contact: Rafal Krypa <r.krypa@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 /*
23  * @file        privilege_db.h
24  * @author      Krzysztof Sasiak <k.sasiak@samsung.com>
25  * @author      Rafal Krypa <r.krypa@samsung.com>
26  * @version     1.0
27  * @brief       This file contains declaration of the API to privilges database.
28  */
29
30 #include <cstdio>
31 #include <list>
32 #include <map>
33 #include <stdbool.h>
34 #include <string>
35
36 #include <dpl/db/sql_connection.h>
37 #include <tzplatform_config.h>
38
39 #ifndef PRIVILEGE_DB_H_
40 #define PRIVILEGE_DB_H_
41
42 namespace SecurityManager {
43
44 const char *const PRIVILEGE_DB_PATH = tzplatform_mkpath(TZ_SYS_DB, ".security-manager.db");
45
46 enum class QueryType {
47     EGetPkgPrivileges,
48     EAddApplication,
49     ERemoveApplication,
50     EAddAppPrivileges,
51     ERemoveAppPrivileges,
52     EPkgIdExists,
53     EGetPkgId,
54     EGetPrivilegeGroups,
55     EGetUserApps,
56 };
57
58 class PrivilegeDb {
59     /**
60      * PrivilegeDb database class
61      */
62
63 private:
64     /**
65      * Constructor
66      * @exception DB::SqlConnection::Exception::IOError on problems with database access
67      *
68      */
69     PrivilegeDb(const std::string &path = std::string(PRIVILEGE_DB_PATH));
70
71     SecurityManager::DB::SqlConnection *mSqlConnection;
72     const std::map<QueryType, const char * const > Queries = {
73         { QueryType::EGetPkgPrivileges, "SELECT DISTINCT privilege_name FROM app_privilege_view WHERE pkg_name=? AND uid=? ORDER BY privilege_name"},
74         { QueryType::EAddApplication, "INSERT INTO app_pkg_view (app_name, pkg_name, uid) VALUES (?, ?, ?)" },
75         { QueryType::ERemoveApplication, "DELETE FROM app_pkg_view WHERE app_name=? AND uid=?" },
76         { QueryType::EAddAppPrivileges, "INSERT INTO app_privilege_view (app_name, uid, privilege_name) VALUES (?, ?, ?)" },
77         { QueryType::ERemoveAppPrivileges, "DELETE FROM app_privilege_view WHERE app_name=? AND uid=?" },
78         { QueryType::EPkgIdExists, "SELECT * FROM pkg WHERE name=?" },
79         { QueryType::EGetPkgId, " SELECT pkg_name FROM app_pkg_view WHERE app_name = ?" },
80         { QueryType::EGetPrivilegeGroups, " SELECT name FROM privilege_group_view WHERE privilege_name = ?" },
81         { QueryType::EGetUserApps, "SELECT name FROM app WHERE uid=?" },
82     };
83
84     /**
85      * Container for initialized DataCommands, prepared for binding.
86      */
87     std::vector<DB::SqlConnection::DataCommandAutoPtr> m_commands;
88
89     /**
90      * Fills empty m_commands map with sql commands prepared for binding.
91      *
92      * Because the "sqlite3_prepare_v2" function takes many cpu cycles, the PrivilegeDb
93      * is optimized to call it only once for one query type.
94      * Designed to be used in the singleton contructor.
95      */
96     void initDataCommands();
97
98     /**
99      * Return prepared query for given query type.
100      * The query will be reset before returning.
101      *
102      * @param queryType query identifier
103      * @return reference to prepared, reset query
104      */
105     DB::SqlConnection::DataCommandAutoPtr & getQuery(QueryType queryType);
106
107     /**
108      * Check if pkgId is already registered in database
109      *
110      * @param pkgId - package identifier
111      * @exception DB::SqlConnection::Exception::InternalError on internal error
112      * @return true if pkgId exists in the database
113      *
114      */
115     bool PkgIdExists(const std::string &pkgId);
116
117 public:
118     class Exception
119     {
120       public:
121         DECLARE_EXCEPTION_TYPE(SecurityManager::Exception, Base)
122         DECLARE_EXCEPTION_TYPE(Base, IOError)
123         DECLARE_EXCEPTION_TYPE(Base, InternalError)
124     };
125
126     ~PrivilegeDb(void);
127
128     static PrivilegeDb &getInstance();
129
130     /**
131      * Begin transaction
132      * @exception DB::SqlConnection::Exception::InternalError on internal error
133      *
134      */
135     void BeginTransaction(void);
136
137     /**
138      * Commit transaction
139      * @exception DB::SqlConnection::Exception::InternalError on internal error
140      *
141      */
142     void CommitTransaction(void);
143
144     /**
145      * Rollback transaction
146      * @exception DB::SqlConnection::Exception::InternalError on internal error
147      *
148      */
149     void RollbackTransaction(void);
150
151     /**
152      * Return package id associated with a given application id
153      *
154      * @param appId - application identifier
155      * @param[out] pkgId - return application's pkgId
156      * @return true is application exists, false otherwise
157      * @exception DB::SqlConnection::Exception::InternalError on internal error
158      */
159     bool GetAppPkgId(const std::string &appId, std::string &pkgId);
160
161     /**
162      * Retrieve list of privileges assigned to a pkgId
163      *
164      * @param pkgId - package identifier
165      * @param uid - user identifier for whom privileges will be retrieved
166      * @param[out] currentPrivileges - list of current privileges assigned to pkgId
167      * @exception DB::SqlConnection::Exception::InternalError on internal error
168      */
169     void GetPkgPrivileges(const std::string &pkgId, uid_t uid,
170             std::vector<std::string> &currentPrivilege);
171
172     /**
173      * Add an application into the database
174      *
175      * @param appId - application identifier
176      * @param pkgId - package identifier
177      * @param uid - user identifier for whom application is going to be installed
178      * @param[out] pkgIdIsNew - return info if pkgId is new to the database
179      * @exception DB::SqlConnection::Exception::InternalError on internal error
180      */
181     void AddApplication(const std::string &appId, const std::string &pkgId,
182             uid_t uid, bool &pkgIdIsNew);
183
184     /**
185      * Remove an application from the database
186      *
187      * @param appId - application identifier
188      * @param uid - user identifier whose application is going to be uninstalled
189      * @param[out] pkgIdIsNoMore - return info if pkgId is in the database
190      * @exception DB::SqlConnection::Exception::InternalError on internal error
191      */
192     void RemoveApplication(const std::string &appId, uid_t uid, bool &pkgIdIsNoMore);
193
194     /**
195      * Remove privileges assigned to application
196      *
197      * @param appId - application identifier
198      * @param uid - user identifier for whom privileges will be removed
199      * @exception DB::SqlConnection::Exception::InternalError on internal error
200      */
201     void RemoveAppPrivileges(const std::string &appId, uid_t uid);
202
203     /**
204      * Update privileges assigned to application
205      * To assure data integrity this method must be called inside db transaction.
206      *
207      * @param appId - application identifier
208      * @param uid - user identifier for whom privileges will be updated
209      * @param privileges - list of privileges to assign
210      * @exception DB::SqlConnection::Exception::InternalError on internal error
211      */
212     void UpdateAppPrivileges(const std::string &appId, uid_t uid,
213             const std::vector<std::string> &privileges);
214
215     /**
216      * Retrieve list of group ids assigned to a privilege
217      *
218      * @param privilege - privilege identifier
219      * @param[out] grp_names - list of group names assigned to the privilege
220      * @exception DB::SqlConnection::Exception::InternalError on internal error
221      */
222     void GetPrivilegeGroups(const std::string &privilege,
223         std::vector<std::string> &grp_names);
224
225     /**
226      * Retrieve list of apps assigned to user
227      *
228      * @param uid - user identifier
229      * @param[out] apps - list of apps assigned to user,
230      *                    this parameter do not need to be empty, but
231      *                    it is being overwritten during function call.
232      * @exception DB::SqlConnection::Exception::InternalError on internal error
233      */
234     void GetUserApps(uid_t uid, std::vector<std::string> &apps);
235 };
236
237 } //namespace SecurityManager
238
239 #endif // PRIVILEGE_DB_H_