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