4d87cdd63242a7fa313b65998a9eff2c6a6fae3c
[platform/core/security/security-manager.git] / src / common / privilege_db.cpp
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.cpp
24  * @author      Krzysztof Sasiak <k.sasiak@samsung.com>
25  * @author      Rafal Krypa <r.krypa@samsung.com>
26  * @version     0.1
27  * @brief       This file contains declaration of the API to privileges database.
28  */
29
30 #include <cstdio>
31 #include <list>
32 #include <string>
33 #include <iostream>
34
35 #include <dpl/log/log.h>
36 #include "privilege_db.h"
37
38 namespace SecurityManager {
39
40 /* Common code for handling SqlConnection exceptions */
41 template <typename T>
42 T try_catch(const std::function<T()> &func)
43 {
44     try {
45         return func();
46     } catch (DB::SqlConnection::Exception::SyntaxError &e) {
47         LogError("Syntax error in command: " << e.DumpToString());
48         ThrowMsg(PrivilegeDb::Exception::InternalError,
49             "Syntax error in command: " << e.DumpToString());
50     } catch (DB::SqlConnection::Exception::InternalError &e) {
51         LogError("Mysterious internal error in SqlConnection class" << e.DumpToString());
52         ThrowMsg(PrivilegeDb::Exception::InternalError,
53             "Mysterious internal error in SqlConnection class: " << e.DumpToString());
54     }
55 }
56
57 PrivilegeDb::PrivilegeDb(const std::string &path)
58 {
59     try {
60         mSqlConnection = new DB::SqlConnection(path,
61                 DB::SqlConnection::Flag::None,
62                 DB::SqlConnection::Flag::RW);
63         initDataCommands();
64     } catch (DB::SqlConnection::Exception::Base &e) {
65         LogError("Database initialization error: " << e.DumpToString());
66         ThrowMsg(PrivilegeDb::Exception::IOError,
67                 "Database initialization error:" << e.DumpToString());
68
69     };
70 }
71
72 void PrivilegeDb::initDataCommands()
73 {
74     for (auto &it : Queries) {
75         m_commands.push_back(mSqlConnection->PrepareDataCommand(it.second));
76     }
77 }
78
79 DB::SqlConnection::DataCommandAutoPtr & PrivilegeDb::getQuery(QueryType queryType)
80 {
81     auto &command = m_commands.at(static_cast<size_t>(queryType));
82     command->Reset();
83     return command;
84 }
85
86 PrivilegeDb::~PrivilegeDb()
87 {
88     delete mSqlConnection;
89 }
90
91 PrivilegeDb &PrivilegeDb::getInstance()
92 {
93     static PrivilegeDb privilegeDb;
94     return privilegeDb;
95 }
96
97 void PrivilegeDb::BeginTransaction(void)
98 {
99     try_catch<void>([&] {
100         mSqlConnection->BeginTransaction();
101     });
102 }
103
104 void PrivilegeDb::CommitTransaction(void)
105 {
106     try_catch<void>([&] {
107         mSqlConnection->CommitTransaction();
108     });
109 }
110
111 void PrivilegeDb::RollbackTransaction(void)
112 {
113     try_catch<void>([&] {
114         mSqlConnection->RollbackTransaction();
115     });
116 }
117
118 bool PrivilegeDb::PkgIdExists(const std::string &pkgId)
119 {
120     return try_catch<bool>([&] {
121         auto &command = getQuery(QueryType::EPkgIdExists);
122         command->BindString(1, pkgId.c_str());
123         return command->Step();
124     });
125 }
126
127 bool PrivilegeDb::GetAppPkgId(const std::string &appId, std::string &pkgId)
128 {
129     return try_catch<bool>([&] {
130         auto &command = getQuery(QueryType::EGetPkgId);
131         command->BindString(1, appId.c_str());
132
133         if (!command->Step()) {
134             // No application with such appId
135             return false;
136         }
137
138         // application package found in the database, get it
139         pkgId = command->GetColumnString(0);
140
141         return true;
142     });
143 }
144
145 void PrivilegeDb::AddApplication(const std::string &appId,
146         const std::string &pkgId, uid_t uid, bool &pkgIdIsNew)
147 {
148     pkgIdIsNew = !(this->PkgIdExists(pkgId));
149
150     try_catch<void>([&] {
151         auto &command = getQuery(QueryType::EAddApplication);
152         command->BindString(1, appId.c_str());
153         command->BindString(2, pkgId.c_str());
154         command->BindInteger(3, static_cast<unsigned int>(uid));
155
156         if (command->Step()) {
157             LogDebug("Unexpected SQLITE_ROW answer to query: " <<
158                     Queries.at(QueryType::EAddApplication));
159         };
160
161         LogDebug("Added appId: " << appId << ", pkgId: " << pkgId);
162     });
163 }
164
165 void PrivilegeDb::RemoveApplication(const std::string &appId, uid_t uid,
166         bool &pkgIdIsNoMore)
167 {
168     try_catch<void>([&] {
169         std::string pkgId;
170         if (!GetAppPkgId(appId, pkgId)) {
171             pkgIdIsNoMore = false;
172             return;
173         }
174
175         auto &command = getQuery(QueryType::ERemoveApplication);
176         command->BindString(1, appId.c_str());
177         command->BindInteger(2, static_cast<unsigned int>(uid));
178
179         if (command->Step()) {
180             LogDebug("Unexpected SQLITE_ROW answer to query: " <<
181                     Queries.at(QueryType::ERemoveApplication));
182         };
183
184         LogDebug("Removed appId: " << appId);
185
186         pkgIdIsNoMore = !(this->PkgIdExists(pkgId));
187     });
188 }
189
190 void PrivilegeDb::GetPkgPrivileges(const std::string &pkgId, uid_t uid,
191         std::vector<std::string> &currentPrivileges)
192 {
193     try_catch<void>([&] {
194         auto &command = getQuery(QueryType::EGetPkgPrivileges);
195         command->BindString(1, pkgId.c_str());
196         command->BindInteger(2, static_cast<unsigned int>(uid));
197
198         while (command->Step()) {
199             std::string privilege = command->GetColumnString(0);
200             LogDebug("Got privilege: " << privilege);
201             currentPrivileges.push_back(privilege);
202         };
203     });
204 }
205
206 void PrivilegeDb::RemoveAppPrivileges(const std::string &appId, uid_t uid)
207 {
208     try_catch<void>([&] {
209         auto &command = getQuery(QueryType::ERemoveAppPrivileges);
210         command->BindString(1, appId.c_str());
211         command->BindInteger(2, static_cast<unsigned int>(uid));
212         if (command->Step()) {
213             LogDebug("Unexpected SQLITE_ROW answer to query: " <<
214                     Queries.at(QueryType::ERemoveAppPrivileges));
215         }
216
217         LogDebug("Removed all privileges for appId: " << appId);
218     });
219 }
220
221 void PrivilegeDb::UpdateAppPrivileges(const std::string &appId, uid_t uid,
222         const std::vector<std::string> &privileges)
223 {
224     try_catch<void>([&] {
225         auto &command = getQuery(QueryType::EAddAppPrivileges);
226         command->BindString(1, appId.c_str());
227         command->BindInteger(2, static_cast<unsigned int>(uid));
228
229         RemoveAppPrivileges(appId, uid);
230
231         for (const auto &privilege : privileges) {
232             command->BindString(3, privilege.c_str());
233             command->Step();
234             command->Reset();
235             LogDebug("Added privilege: " << privilege << " to appId: " << appId);
236         }
237     });
238 }
239
240 void PrivilegeDb::GetPrivilegeGroups(const std::string &privilege,
241         std::vector<std::string> &groups)
242 {
243    try_catch<void>([&] {
244         auto &command = getQuery(QueryType::EGetPrivilegeGroups);
245         command->BindString(1, privilege.c_str());
246
247         while (command->Step()) {
248             std::string groupName = command->GetColumnString(0);
249             LogDebug("Privilege " << privilege << " gives access to group: " << groupName);
250             groups.push_back(groupName);
251         };
252     });
253 }
254
255 void PrivilegeDb::GetUserApps(uid_t uid, std::vector<std::string> &apps)
256 {
257    try_catch<void>([&] {
258         auto &command = getQuery(QueryType::EGetUserApps);
259         command->BindInteger(1, static_cast<unsigned int>(uid));
260         apps.clear();
261         while (command->Step()) {
262             std::string app = command->GetColumnString(0);
263             LogDebug("User " << uid << " has app " << app << " installed");
264             apps.push_back(app);
265         };
266     });
267 }
268
269
270 } //namespace SecurityManager