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);
75 bool isNameLabelPresent(
77 const Label &owner) const;
81 const Label &ownerLabel,
86 const Label &ownerLabel,
87 DataType typeRangeStart,
88 DataType typeRangeStop);
92 const Label &ownerLabel,
98 const Label &ownerLabel,
99 DataType typeRangeStart,
100 DataType typeRangeStop,
104 const Label &smackLabel,
105 LabelNameVector& labelNameVector,
109 const Label &smackLabel,
110 LabelNameVector& labelNameVector,
111 DataType typeRangeStart,
112 DataType typeRangeStop);
116 const Label &ownerLabel);
119 void saveKey(const Label& label, const RawBuffer &key);
120 RawBufferOptional getKey(const Label& label);
121 void deleteKey(const Label& label);
127 const Label &ownerLabel,
128 const Label &accessorLabel,
129 const PermissionMask permissionMask);
131 PermissionMaskOptional getPermissionRow(
133 const Label &ownerLabel,
134 const Label &accessorLabel) const;
138 int beginTransaction();
139 int commitTransaction();
140 int rollbackTransaction();
144 Transaction(Crypto *db)
146 m_inTransaction(false) {
147 if(!m_db->m_inUserTransaction) {
149 m_db->m_connection->ExecCommand("BEGIN EXCLUSIVE");
150 m_db->m_inUserTransaction = true;
151 m_inTransaction = true;
152 } Catch (SqlConnection::Exception::InternalError) {
153 LogError("sqlite got into infinite busy state");
154 ReThrow(Crypto::Exception::TransactionError);
155 } Catch (SqlConnection::Exception::Base) {
156 LogError("Couldn't begin transaction");
157 ReThrow(Crypto::Exception::TransactionError);
162 if(m_inTransaction) {
164 m_db->m_connection->CommitTransaction();
165 m_db->m_inUserTransaction = false;
166 m_inTransaction = false;
167 } Catch (SqlConnection::Exception::InternalError) {
168 LogError("sqlite got into infinite busy state");
169 ReThrow(Crypto::Exception::TransactionError);
170 } Catch (SqlConnection::Exception::Base) {
171 LogError("Couldn't commit transaction");
172 ReThrow(Crypto::Exception::TransactionError);
177 if(m_inTransaction) {
179 m_db->m_connection->RollbackTransaction();
180 m_db->m_inUserTransaction = false;
181 m_inTransaction = false;
182 } Catch (SqlConnection::Exception::InternalError) {
183 LogError("sqlite got into infinite busy state");
184 ReThrow(Crypto::Exception::TransactionError);
185 } Catch (SqlConnection::Exception::Base) {
186 LogError("Couldn't rollback transaction");
187 ReThrow(Crypto::Exception::TransactionError);
193 if(m_inTransaction) {
194 m_db->m_inUserTransaction = false;
195 m_db->m_connection->RollbackTransaction();
197 } Catch (SqlConnection::Exception::InternalError) {
198 LogError("sqlite got into infinite busy state");
199 ReThrow(Crypto::Exception::TransactionError);
200 } Catch (SqlConnection::Exception::Base) {
201 LogError("Transaction rollback failed!");
206 bool m_inTransaction;
210 SqlConnection* m_connection;
212 bool m_inUserTransaction;
216 void createDBSchema();
218 * return current database version
220 * @param[out] schemaVersion if success, will contain DB schema version code
222 * @return false on DB empty or corrupted, true if information read
224 bool getDBVersion(int & schemaVersion);
225 typedef boost::optional<std::string> ScriptOptional;
226 ScriptOptional getScript(const std::string &scriptName) const;
227 ScriptOptional getMigrationScript(int db_version) const;
230 const SqlConnection::DataCommandUniquePtr &selectCommand) const;
233 const char *create_cmd,
234 const char *table_name);
237 const char* create_cmd);
241 explicit SchemaInfo(const Crypto *db) : m_db(db) {}
243 void setVersionInfo();
244 bool getVersionInfo(int & version) const;
253 explicit NameTable(SqlConnection* connection) : m_connection(connection) {}
257 const Label &ownerLabel);
261 const Label &ownerLabel);
264 const Label &ownerLabel);
268 const Label &ownerLabel) const;
271 SqlConnection* m_connection;
276 explicit ObjectTable(SqlConnection* connection) : m_connection(connection) {}
282 SqlConnection* m_connection;
285 class PermissionTable {
287 explicit PermissionTable(SqlConnection* connection) : m_connection(connection) {}
291 const Label &ownerLabel,
292 const Label &accessorLabel,
293 const PermissionMask permissionMask);
295 PermissionMaskOptional getPermissionRow(
297 const Label &ownerLabel,
298 const Label &accessorLabel) const;
301 SqlConnection* m_connection;
307 #pragma GCC diagnostic pop