2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Zofia Abramowska (z.abramowska@samsung.com)
20 * @brief Header of encrypted db access layer
29 #include <dpl/db/sql_connection.h>
31 #include <ckm/ckm-type.h>
33 #include <permission.h>
34 #include <protocols.h>
36 #pragma GCC diagnostic push
37 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
43 typedef boost::optional<Row> RowOptional;
44 typedef boost::optional<RawBuffer> RawBufferOptional;
48 DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
49 DECLARE_EXCEPTION_TYPE(Base, InternalError)
50 DECLARE_EXCEPTION_TYPE(Base, TransactionError)
51 DECLARE_EXCEPTION_TYPE(Base, InvalidArgs)
55 m_inUserTransaction(false)
57 // user name instead of path?
58 Crypto(const std::string &path, const RawBuffer &rawPass);
59 Crypto(const Crypto &other) = delete;
60 Crypto(Crypto &&other);
62 Crypto& operator=(const Crypto& ) = delete;
63 Crypto& operator=(Crypto&& other);
73 const RowVector &rows);
78 bool isNameLabelPresent(
80 const Label &owner) const;
84 const Label &ownerLabel,
89 const Label &ownerLabel,
90 DataType typeRangeStart,
91 DataType typeRangeStop);
95 const Label &ownerLabel,
101 const Label &ownerLabel,
102 DataType typeRangeStart,
103 DataType typeRangeStop,
107 const Label &smackLabel,
108 LabelNameVector& labelNameVector,
112 const Label &smackLabel,
113 LabelNameVector& labelNameVector,
114 DataType typeRangeStart,
115 DataType typeRangeStop);
119 const Label &ownerLabel);
122 void saveKey(const Label& label, const RawBuffer &key);
123 RawBufferOptional getKey(const Label& label);
124 void deleteKey(const Label& label);
130 const Label &ownerLabel,
131 const Label &accessorLabel,
132 const PermissionMask permissionMask);
134 PermissionMaskOptional getPermissionRow(
136 const Label &ownerLabel,
137 const Label &accessorLabel) const;
141 int beginTransaction();
142 int commitTransaction();
143 int rollbackTransaction();
147 Transaction(Crypto *db)
149 m_inTransaction(false) {
150 if(!m_db->m_inUserTransaction) {
152 m_db->m_connection->ExecCommand("BEGIN EXCLUSIVE");
153 m_db->m_inUserTransaction = true;
154 m_inTransaction = true;
155 } Catch (SqlConnection::Exception::InternalError) {
156 LogError("sqlite got into infinite busy state");
157 ReThrow(Crypto::Exception::TransactionError);
158 } Catch (SqlConnection::Exception::Base) {
159 LogError("Couldn't begin transaction");
160 ReThrow(Crypto::Exception::TransactionError);
165 if(m_inTransaction) {
167 m_db->m_connection->CommitTransaction();
168 m_db->m_inUserTransaction = false;
169 m_inTransaction = false;
170 } Catch (SqlConnection::Exception::InternalError) {
171 LogError("sqlite got into infinite busy state");
172 ReThrow(Crypto::Exception::TransactionError);
173 } Catch (SqlConnection::Exception::Base) {
174 LogError("Couldn't commit transaction");
175 ReThrow(Crypto::Exception::TransactionError);
180 if(m_inTransaction) {
182 m_db->m_connection->RollbackTransaction();
183 m_db->m_inUserTransaction = false;
184 m_inTransaction = false;
185 } Catch (SqlConnection::Exception::InternalError) {
186 LogError("sqlite got into infinite busy state");
187 ReThrow(Crypto::Exception::TransactionError);
188 } Catch (SqlConnection::Exception::Base) {
189 LogError("Couldn't rollback transaction");
190 ReThrow(Crypto::Exception::TransactionError);
196 if(m_inTransaction) {
197 m_db->m_inUserTransaction = false;
198 m_db->m_connection->RollbackTransaction();
200 } Catch (SqlConnection::Exception::InternalError) {
201 LogError("sqlite got into infinite busy state");
202 ReThrow(Crypto::Exception::TransactionError);
203 } Catch (SqlConnection::Exception::Base) {
204 LogError("Transaction rollback failed!");
209 bool m_inTransaction;
213 SqlConnection* m_connection;
215 bool m_inUserTransaction;
219 void createDBSchema();
221 * return current database version
223 * @param[out] schemaVersion if success, will contain DB schema version code
225 * @return false on DB empty or corrupted, true if information read
227 bool getDBVersion(int & schemaVersion);
228 typedef boost::optional<std::string> ScriptOptional;
229 ScriptOptional getScript(const std::string &scriptName) const;
230 ScriptOptional getMigrationScript(int db_version) const;
233 const SqlConnection::DataCommandUniquePtr &selectCommand) const;
236 const char *create_cmd,
237 const char *table_name);
240 const char* create_cmd);
244 explicit SchemaInfo(const Crypto *db) : m_db(db) {}
246 void setVersionInfo();
247 bool getVersionInfo(int & version) const;
256 explicit NameTable(SqlConnection* connection) : m_connection(connection) {}
260 const Label &ownerLabel);
264 const Label &ownerLabel);
267 const Label &ownerLabel);
271 const Label &ownerLabel) const;
274 SqlConnection* m_connection;
279 explicit ObjectTable(SqlConnection* connection) : m_connection(connection) {}
287 SqlConnection* m_connection;
290 class PermissionTable {
292 explicit PermissionTable(SqlConnection* connection) : m_connection(connection) {}
296 const Label &ownerLabel,
297 const Label &accessorLabel,
298 const PermissionMask permissionMask);
300 PermissionMaskOptional getPermissionRow(
302 const Label &ownerLabel,
303 const Label &accessorLabel) const;
306 SqlConnection* m_connection;
312 #pragma GCC diagnostic pop