Provide support for loading privilege-group mapping
[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     EGetAppPrivileges,
49     EAddApplication,
50     ERemoveApplication,
51     EAddAppPrivileges,
52     ERemoveAppPrivileges,
53     EPkgIdExists,
54     EGetPkgId,
55     EGetPrivilegeGroups,
56     EGetUserApps,
57     EGetAppsInPkg
58 };
59
60 class PrivilegeDb {
61     /**
62      * PrivilegeDb database class
63      */
64
65 private:
66     /**
67      * Constructor
68      * @exception DB::SqlConnection::Exception::IOError on problems with database access
69      *
70      */
71     PrivilegeDb(const std::string &path = std::string(PRIVILEGE_DB_PATH));
72
73     SecurityManager::DB::SqlConnection *mSqlConnection;
74     const std::map<QueryType, const char * const > Queries = {
75         { QueryType::EGetPkgPrivileges, "SELECT DISTINCT privilege_name FROM app_privilege_view WHERE pkg_name=? AND uid=? ORDER BY privilege_name"},
76         { QueryType::EGetAppPrivileges, "SELECT DISTINCT privilege_name FROM app_privilege_view WHERE app_name=? AND uid=? ORDER BY privilege_name"},
77         { QueryType::EAddApplication, "INSERT INTO app_pkg_view (app_name, pkg_name, uid) VALUES (?, ?, ?)" },
78         { QueryType::ERemoveApplication, "DELETE FROM app_pkg_view WHERE app_name=? AND uid=?" },
79         { QueryType::EAddAppPrivileges, "INSERT INTO app_privilege_view (app_name, uid, privilege_name) VALUES (?, ?, ?)" },
80         { QueryType::ERemoveAppPrivileges, "DELETE FROM app_privilege_view WHERE app_name=? AND uid=?" },
81         { QueryType::EPkgIdExists, "SELECT * FROM pkg WHERE name=?" },
82         { QueryType::EGetPkgId, " SELECT pkg_name FROM app_pkg_view WHERE app_name = ?" },
83         { QueryType::EGetPrivilegeGroups, " SELECT group_name FROM privilege_group_view WHERE privilege_name = ?" },
84         { QueryType::EGetUserApps, "SELECT name FROM app WHERE uid=?" },
85         { QueryType::EGetAppsInPkg, " SELECT app_name FROM app_pkg_view WHERE pkg_name = ?" },
86     };
87
88     /**
89      * Container for initialized DataCommands, prepared for binding.
90      */
91     std::vector<DB::SqlConnection::DataCommandAutoPtr> m_commands;
92
93     /**
94      * Fills empty m_commands map with sql commands prepared for binding.
95      *
96      * Because the "sqlite3_prepare_v2" function takes many cpu cycles, the PrivilegeDb
97      * is optimized to call it only once for one query type.
98      * Designed to be used in the singleton contructor.
99      */
100     void initDataCommands();
101
102     /**
103      * Return prepared query for given query type.
104      * The query will be reset before returning.
105      *
106      * @param queryType query identifier
107      * @return reference to prepared, reset query
108      */
109     DB::SqlConnection::DataCommandAutoPtr & getQuery(QueryType queryType);
110
111     /**
112      * Check if pkgId is already registered in database
113      *
114      * @param pkgId - package identifier
115      * @exception DB::SqlConnection::Exception::InternalError on internal error
116      * @return true if pkgId exists in the database
117      *
118      */
119     bool PkgIdExists(const std::string &pkgId);
120
121 public:
122     class Exception
123     {
124       public:
125         DECLARE_EXCEPTION_TYPE(SecurityManager::Exception, Base)
126         DECLARE_EXCEPTION_TYPE(Base, IOError)
127         DECLARE_EXCEPTION_TYPE(Base, InternalError)
128     };
129
130     ~PrivilegeDb(void);
131
132     static PrivilegeDb &getInstance();
133
134     /**
135      * Begin transaction
136      * @exception DB::SqlConnection::Exception::InternalError on internal error
137      *
138      */
139     void BeginTransaction(void);
140
141     /**
142      * Commit transaction
143      * @exception DB::SqlConnection::Exception::InternalError on internal error
144      *
145      */
146     void CommitTransaction(void);
147
148     /**
149      * Rollback transaction
150      * @exception DB::SqlConnection::Exception::InternalError on internal error
151      *
152      */
153     void RollbackTransaction(void);
154
155     /**
156      * Return package id associated with a given application id
157      *
158      * @param appId - application identifier
159      * @param[out] pkgId - return application's pkgId
160      * @return true is application exists, false otherwise
161      * @exception DB::SqlConnection::Exception::InternalError on internal error
162      */
163     bool GetAppPkgId(const std::string &appId, std::string &pkgId);
164
165     /**
166      * Retrieve list of privileges assigned to a pkgId
167      *
168      * @param pkgId - package identifier
169      * @param uid - user identifier for whom privileges will be retrieved
170      * @param[out] currentPrivileges - list of current privileges assigned to pkgId
171      * @exception DB::SqlConnection::Exception::InternalError on internal error
172      */
173     void GetPkgPrivileges(const std::string &pkgId, uid_t uid,
174             std::vector<std::string> &currentPrivilege);
175
176     /**
177      * Retrieve list of privileges assigned to an appId
178      *
179      * @param appId - application identifier
180      * @param uid - user identifier for whom privileges will be retrieved
181      * @param[out] currentPrivileges - list of current privileges assigned to appId
182      * @exception DB::SqlConnection::Exception::InternalError on internal error
183      */
184     void GetAppPrivileges(const std::string &appId, uid_t uid,
185         std::vector<std::string> &currentPrivileges);
186
187     /**
188      * Add an application into the database
189      *
190      * @param appId - application identifier
191      * @param pkgId - package identifier
192      * @param uid - user identifier for whom application is going to be installed
193      * @exception DB::SqlConnection::Exception::InternalError on internal error
194      */
195     void AddApplication(const std::string &appId, const std::string &pkgId,
196             uid_t uid);
197
198     /**
199      * Remove an application from the database
200      *
201      * @param appId - application identifier
202      * @param uid - user identifier whose application is going to be uninstalled
203      * @param[out] pkgIdIsNoMore - return info if pkgId is in the database
204      * @exception DB::SqlConnection::Exception::InternalError on internal error
205      */
206     void RemoveApplication(const std::string &appId, uid_t uid, bool &pkgIdIsNoMore);
207
208     /**
209      * Remove privileges assigned to application
210      *
211      * @param appId - application identifier
212      * @param uid - user identifier for whom privileges will be removed
213      * @exception DB::SqlConnection::Exception::InternalError on internal error
214      */
215     void RemoveAppPrivileges(const std::string &appId, uid_t uid);
216
217     /**
218      * Update privileges assigned to application
219      * To assure data integrity this method must be called inside db transaction.
220      *
221      * @param appId - application identifier
222      * @param uid - user identifier for whom privileges will be updated
223      * @param privileges - list of privileges to assign
224      * @exception DB::SqlConnection::Exception::InternalError on internal error
225      */
226     void UpdateAppPrivileges(const std::string &appId, uid_t uid,
227             const std::vector<std::string> &privileges);
228
229     /**
230      * Retrieve list of group ids assigned to a privilege
231      *
232      * @param privilege - privilege identifier
233      * @param[out] grp_names - list of group names assigned to the privilege
234      * @exception DB::SqlConnection::Exception::InternalError on internal error
235      */
236     void GetPrivilegeGroups(const std::string &privilege,
237         std::vector<std::string> &grp_names);
238
239     /**
240      * Retrieve list of apps assigned to user
241      *
242      * @param uid - user identifier
243      * @param[out] apps - list of apps assigned to user,
244      *                    this parameter do not need to be empty, but
245      *                    it is being overwritten during function call.
246      * @exception DB::SqlConnection::Exception::InternalError on internal error
247      */
248     void GetUserApps(uid_t uid, std::vector<std::string> &apps);
249     /**
250      * Retrieve a list of all application ids for a package id
251      *
252      * @param pkgId - package id
253      * @param[out] appIds - list of application ids for the package id
254      * @exception DB::SqlConnection::Exception::InternalError on internal error
255      */
256     void GetAppIdsForPkgId (const std::string &pkgId,
257         std::vector<std::string> &appIds);
258 };
259
260 } //namespace SecurityManager
261
262 #endif // PRIVILEGE_DB_H_