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);
118 std::string getSchema();
120 std::string getContent();
123 void saveKey(const Label& label, const RawBuffer &key);
124 RawBufferOptional getKey(const Label& label);
125 void deleteKey(const Label& label);
131 const Label &ownerLabel,
132 const Label &accessorLabel,
133 const PermissionMask permissionMask);
135 PermissionMaskOptional getPermissionRow(
137 const Label &ownerLabel,
138 const Label &accessorLabel) const;
142 int beginTransaction();
143 int commitTransaction();
144 int rollbackTransaction();
148 Transaction(Crypto *db)
150 m_inTransaction(false) {
151 if(!m_db->m_inUserTransaction) {
153 m_db->m_connection->ExecCommand("BEGIN EXCLUSIVE");
154 m_db->m_inUserTransaction = true;
155 m_inTransaction = true;
156 } Catch (SqlConnection::Exception::InternalError) {
157 LogError("sqlite got into infinite busy state");
158 ReThrow(Crypto::Exception::TransactionError);
159 } Catch (SqlConnection::Exception::Base) {
160 LogError("Couldn't begin transaction");
161 ReThrow(Crypto::Exception::TransactionError);
166 if(m_inTransaction) {
168 m_db->m_connection->CommitTransaction();
169 m_db->m_inUserTransaction = false;
170 m_inTransaction = false;
171 } Catch (SqlConnection::Exception::InternalError) {
172 LogError("sqlite got into infinite busy state");
173 ReThrow(Crypto::Exception::TransactionError);
174 } Catch (SqlConnection::Exception::Base) {
175 LogError("Couldn't commit transaction");
176 ReThrow(Crypto::Exception::TransactionError);
181 if(m_inTransaction) {
183 m_db->m_connection->RollbackTransaction();
184 m_db->m_inUserTransaction = false;
185 m_inTransaction = false;
186 } Catch (SqlConnection::Exception::InternalError) {
187 LogError("sqlite got into infinite busy state");
188 ReThrow(Crypto::Exception::TransactionError);
189 } Catch (SqlConnection::Exception::Base) {
190 LogError("Couldn't rollback transaction");
191 ReThrow(Crypto::Exception::TransactionError);
197 if(m_inTransaction) {
198 m_db->m_inUserTransaction = false;
199 m_db->m_connection->RollbackTransaction();
201 } Catch (SqlConnection::Exception::InternalError) {
202 LogError("sqlite got into infinite busy state");
203 ReThrow(Crypto::Exception::TransactionError);
204 } Catch (SqlConnection::Exception::Base) {
205 LogError("Transaction rollback failed!");
210 bool m_inTransaction;
214 SqlConnection* m_connection;
216 bool m_inUserTransaction;
220 void createDBSchema();
222 * return current database version
224 * @param[out] schemaVersion if success, will contain DB schema version code
226 * @return false on DB empty or corrupted, true if information read
228 bool getDBVersion(int & schemaVersion);
229 typedef boost::optional<std::string> ScriptOptional;
230 ScriptOptional getScript(const std::string &scriptName) const;
231 ScriptOptional getMigrationScript(int db_version) const;
234 const SqlConnection::DataCommandUniquePtr &selectCommand) const;
237 const char *create_cmd,
238 const char *table_name);
241 const char* create_cmd);
245 explicit SchemaInfo(const Crypto *db) : m_db(db) {}
247 void setVersionInfo();
248 bool getVersionInfo(int & version) const;
257 explicit NameTable(SqlConnection* connection) : m_connection(connection) {}
261 const Label &ownerLabel);
265 const Label &ownerLabel);
268 const Label &ownerLabel);
272 const Label &ownerLabel) const;
275 SqlConnection* m_connection;
280 explicit ObjectTable(SqlConnection* connection) : m_connection(connection) {}
286 SqlConnection* m_connection;
289 class PermissionTable {
291 explicit PermissionTable(SqlConnection* connection) : m_connection(connection) {}
295 const Label &ownerLabel,
296 const Label &accessorLabel,
297 const PermissionMask permissionMask);
299 PermissionMaskOptional getPermissionRow(
301 const Label &ownerLabel,
302 const Label &accessorLabel) const;
305 SqlConnection* m_connection;
311 #pragma GCC diagnostic pop