34a4ed1d19ecf717263e8beaf34d91608ed94cee
[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 };
56
57 class PrivilegeDb {
58     /**
59      * PrivilegeDb database class
60      */
61
62 private:
63     /**
64      * Constructor
65      * @exception DB::SqlConnection::Exception::IOError on problems with database access
66      *
67      */
68     PrivilegeDb(const std::string &path = std::string(PRIVILEGE_DB_PATH));
69
70     SecurityManager::DB::SqlConnection *mSqlConnection;
71     const std::map<QueryType, const char * const > Queries = {
72         { QueryType::EGetPkgPrivileges, "SELECT DISTINCT privilege_name FROM app_privilege_view WHERE pkg_name=? AND uid=? ORDER BY privilege_name"},
73         { QueryType::EAddApplication, "INSERT INTO app_pkg_view (app_name, pkg_name, uid) VALUES (?, ?, ?)" },
74         { QueryType::ERemoveApplication, "DELETE FROM app_pkg_view WHERE app_name=? AND uid=?" },
75         { QueryType::EAddAppPrivileges, "INSERT INTO app_privilege_view (app_name, uid, privilege_name) VALUES (?, ?, ?)" },
76         { QueryType::ERemoveAppPrivileges, "DELETE FROM app_privilege_view WHERE app_name=? AND uid=?" },
77         { QueryType::EPkgIdExists, "SELECT * FROM pkg WHERE name=?" },
78         { QueryType::EGetPkgId, " SELECT pkg_name FROM app_pkg_view WHERE app_name = ?" },
79         { QueryType::EGetPrivilegeGroups, " SELECT name FROM privilege_group_view WHERE privilege_name = ?" },
80     };
81
82     /**
83      * Check if pkgId is already registered in database
84      *
85      * @param pkgId - package identifier
86      * @exception DB::SqlConnection::Exception::InternalError on internal error
87      * @return true if pkgId exists in the database
88      *
89      */
90     bool PkgIdExists(const std::string &pkgId);
91
92 public:
93     class Exception
94     {
95       public:
96         DECLARE_EXCEPTION_TYPE(SecurityManager::Exception, Base)
97         DECLARE_EXCEPTION_TYPE(Base, IOError)
98         DECLARE_EXCEPTION_TYPE(Base, InternalError)
99     };
100
101     ~PrivilegeDb(void);
102
103     static PrivilegeDb &getInstance();
104
105     /**
106      * Begin transaction
107      * @exception DB::SqlConnection::Exception::InternalError on internal error
108      *
109      */
110     void BeginTransaction(void);
111
112     /**
113      * Commit transaction
114      * @exception DB::SqlConnection::Exception::InternalError on internal error
115      *
116      */
117     void CommitTransaction(void);
118
119     /**
120      * Rollback transaction
121      * @exception DB::SqlConnection::Exception::InternalError on internal error
122      *
123      */
124     void RollbackTransaction(void);
125
126     /**
127      * Return package id associated with a given application id
128      *
129      * @param appId - application identifier
130      * @param[out] pkgId - return application's pkgId
131      * @return true is application exists, false otherwise
132      * @exception DB::SqlConnection::Exception::InternalError on internal error
133      */
134     bool GetAppPkgId(const std::string &appId, std::string &pkgId);
135
136     /**
137      * Retrieve list of privileges assigned to a pkgId
138      *
139      * @param pkgId - package identifier
140      * @param uid - user identifier for whom privileges will be retrieved
141      * @param[out] currentPrivileges - list of current privileges assigned to pkgId
142      * @exception DB::SqlConnection::Exception::InternalError on internal error
143      */
144     void GetPkgPrivileges(const std::string &pkgId, uid_t uid,
145             std::vector<std::string> &currentPrivilege);
146
147     /**
148      * Add an application into the database
149      *
150      * @param appId - application identifier
151      * @param pkgId - package identifier
152      * @param uid - user identifier for whom application is going to be installed
153      * @param[out] pkgIdIsNew - return info if pkgId is new to the database
154      * @exception DB::SqlConnection::Exception::InternalError on internal error
155      */
156     void AddApplication(const std::string &appId, const std::string &pkgId,
157             uid_t uid, bool &pkgIdIsNew);
158
159     /**
160      * Remove an application from the database
161      *
162      * @param appId - application identifier
163      * @param uid - user identifier whose application is going to be uninstalled
164      * @param[out] pkgIdIsNoMore - return info if pkgId is in the database
165      * @exception DB::SqlConnection::Exception::InternalError on internal error
166      */
167     void RemoveApplication(const std::string &appId, uid_t uid, bool &pkgIdIsNoMore);
168
169     /**
170      * Remove privileges assigned to application
171      *
172      * @param appId - application identifier
173      * @param uid - user identifier for whom privileges will be removed
174      * @exception DB::SqlConnection::Exception::InternalError on internal error
175      */
176     void RemoveAppPrivileges(const std::string &appId, uid_t uid);
177
178     /**
179      * Update privileges assigned to application
180      * To assure data integrity this method must be called inside db transaction.
181      *
182      * @param appId - application identifier
183      * @param uid - user identifier for whom privileges will be updated
184      * @param privileges - list of privileges to assign
185      * @exception DB::SqlConnection::Exception::InternalError on internal error
186      */
187     void UpdateAppPrivileges(const std::string &appId, uid_t uid,
188             const std::vector<std::string> &privileges);
189
190     /**
191      * Retrieve list of group ids assigned to a privilege
192      *
193      * @param privilege - privilege identifier
194      * @param[out] grp_names - list of group names assigned to the privilege
195      * @exception DB::SqlConnection::Exception::InternalError on internal error
196      */
197     void GetPrivilegeGroups(const std::string &privilege,
198         std::vector<std::string> &grp_names);
199
200 };
201
202 } //namespace SecurityManager
203
204 #endif // PRIVILEGE_DB_H_