2 * security-manager, database access
4 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
6 * Contact: Rafal Krypa <r.krypa@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * @file privilege_db.cpp
24 * @author Krzysztof Sasiak <k.sasiak@samsung.com>
25 * @author Rafal Krypa <r.krypa@samsung.com>
27 * @brief This file contains declaration of the API to privileges database.
35 #include <dpl/log/log.h>
36 #include "privilege_db.h"
38 namespace SecurityManager {
40 /* Common code for handling SqlConnection exceptions */
42 T try_catch(const std::function<T()> &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());
57 PrivilegeDb::PrivilegeDb(const std::string &path)
60 mSqlConnection = new DB::SqlConnection(path,
61 DB::SqlConnection::Flag::None,
62 DB::SqlConnection::Flag::RW);
63 } catch (DB::SqlConnection::Exception::Base &e) {
64 LogError("Database initialization error: " << e.DumpToString());
65 ThrowMsg(PrivilegeDb::Exception::IOError,
66 "Database initialization error:" << e.DumpToString());
71 PrivilegeDb::~PrivilegeDb()
73 delete mSqlConnection;
76 PrivilegeDb &PrivilegeDb::getInstance()
78 static PrivilegeDb privilegeDb;
82 void PrivilegeDb::BeginTransaction(void)
85 mSqlConnection->BeginTransaction();
89 void PrivilegeDb::CommitTransaction(void)
92 mSqlConnection->CommitTransaction();
96 void PrivilegeDb::RollbackTransaction(void)
99 mSqlConnection->RollbackTransaction();
103 bool PrivilegeDb::PkgIdExists(const std::string &pkgId)
105 return try_catch<bool>([&] {
106 DB::SqlConnection::DataCommandAutoPtr command =
107 mSqlConnection->PrepareDataCommand(
108 Queries.at(QueryType::EPkgIdExists));
109 command->BindString(1, pkgId.c_str());
110 if (command->Step()) {
111 // pkgId found in the database
116 // pkgId not found in the database
121 bool PrivilegeDb::GetAppPkgId(const std::string &appId, std::string &pkgId)
123 return try_catch<bool>([&] {
124 DB::SqlConnection::DataCommandAutoPtr command =
125 mSqlConnection->PrepareDataCommand(Queries.at(QueryType::EGetPkgId));
126 command->BindString(1, appId.c_str());
128 if (!command->Step()) {
129 // No application with such appId
133 // application package found in the database, get it
134 pkgId = command->GetColumnString(0);
140 void PrivilegeDb::AddApplication(const std::string &appId,
141 const std::string &pkgId, uid_t uid, bool &pkgIdIsNew)
143 pkgIdIsNew = !(this->PkgIdExists(pkgId));
145 try_catch<void>([&] {
146 DB::SqlConnection::DataCommandAutoPtr command =
147 mSqlConnection->PrepareDataCommand(
148 Queries.at(QueryType::EAddApplication));
150 command->BindString(1, appId.c_str());
151 command->BindString(2, pkgId.c_str());
152 command->BindInteger(3, static_cast<unsigned int>(uid));
154 if (command->Step()) {
155 LogDebug("Unexpected SQLITE_ROW answer to query: " <<
156 Queries.at(QueryType::EAddApplication));
160 LogDebug("Added appId: " << appId << ", pkgId: " << pkgId);
164 void PrivilegeDb::RemoveApplication(const std::string &appId, uid_t uid,
167 try_catch<void>([&] {
169 if (!GetAppPkgId(appId, pkgId)) {
170 pkgIdIsNoMore = false;
174 DB::SqlConnection::DataCommandAutoPtr command =
175 mSqlConnection->PrepareDataCommand(
176 Queries.at(QueryType::ERemoveApplication));
178 command->BindString(1, appId.c_str());
179 command->BindInteger(2, static_cast<unsigned int>(uid));
181 if (command->Step()) {
182 LogDebug("Unexpected SQLITE_ROW answer to query: " <<
183 Queries.at(QueryType::ERemoveApplication));
187 LogDebug("Removed appId: " << appId);
189 pkgIdIsNoMore = !(this->PkgIdExists(pkgId));
193 void PrivilegeDb::GetPkgPrivileges(const std::string &pkgId, uid_t uid,
194 std::vector<std::string> ¤tPrivileges)
196 try_catch<void>([&] {
197 DB::SqlConnection::DataCommandAutoPtr command =
198 mSqlConnection->PrepareDataCommand(
199 Queries.at(QueryType::EGetPkgPrivileges));
200 command->BindString(1, pkgId.c_str());
201 command->BindInteger(2, static_cast<unsigned int>(uid));
203 while (command->Step()) {
204 std::string privilege = command->GetColumnString(0);
205 LogDebug("Got privilege: " << privilege);
206 currentPrivileges.push_back(privilege);
211 void PrivilegeDb::RemoveAppPrivileges(const std::string &appId, uid_t uid)
213 try_catch<void>([&] {
214 DB::SqlConnection::DataCommandAutoPtr command =
215 mSqlConnection->PrepareDataCommand(Queries.at(QueryType::ERemoveAppPrivileges));
217 command->BindString(1, appId.c_str());
218 command->BindInteger(2, static_cast<unsigned int>(uid));
219 if (command->Step()) {
220 LogDebug("Unexpected SQLITE_ROW answer to query: " <<
221 Queries.at(QueryType::ERemoveAppPrivileges));
224 LogDebug("Removed all privileges for appId: " << appId);
228 void PrivilegeDb::UpdateAppPrivileges(const std::string &appId, uid_t uid,
229 const std::vector<std::string> &privileges)
231 try_catch<void>([&] {
232 DB::SqlConnection::DataCommandAutoPtr command =
233 mSqlConnection->PrepareDataCommand(Queries.at(QueryType::EAddAppPrivileges));
234 command->BindString(1, appId.c_str());
235 command->BindInteger(2, static_cast<unsigned int>(uid));
237 RemoveAppPrivileges(appId, uid);
239 for (const auto &privilege : privileges) {
240 command->BindString(3, privilege.c_str());
243 LogDebug("Added privilege: " << privilege << " to appId: " << appId);
248 void PrivilegeDb::GetPrivilegeGroups(const std::string &privilege,
249 std::vector<std::string> &groups)
251 try_catch<void>([&] {
252 DB::SqlConnection::DataCommandAutoPtr command =
253 mSqlConnection->PrepareDataCommand(
254 Queries.at(QueryType::EGetPrivilegeGroups));
255 command->BindString(1, privilege.c_str());
257 while (command->Step()) {
258 std::string groupName = command->GetColumnString(0);
259 LogDebug("Privilege " << privilege << " gives access to group: " << groupName);
260 groups.push_back(groupName);
266 } //namespace SecurityManager