summaryrefslogtreecommitdiff
path: root/src/manager/service/db-crypto.h
blob: 501a360b5b35520b39db4356c54ddbd304af7009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
/*
 * @file        db-crypto.h
 * @author      Zofia Abramowska (z.abramowska@samsung.com)
 * @version     1.0
 * @brief       Header of encrypted db access layer
 */
#ifndef CKM_DB_CRYPTO_H
#define CKM_DB_CRYPTO_H

#include <vector>
#include <string>

#include <dpl/db/sql_connection.h>

#include <ckm/ckm-type.h>
#include <exception.h>
#include <db-row.h>
#include <permission.h>
#include <protocols.h>

#pragma GCC diagnostic push
#pragma GCC diagnostic warning "-Wdeprecated-declarations"

namespace CKM {
namespace DB {
class Crypto {
public:
	using RowOptional = boost::optional<Row>;
	using RawBufferOptional = boost::optional<RawBuffer>;

	Crypto() : m_connection(nullptr), m_inUserTransaction(false) {}

	// user name instead of path?
	Crypto(const std::string &path, const RawBuffer &rawPass);
	Crypto(const Crypto &other) = delete;
	Crypto(Crypto &&other);

	Crypto &operator=(const Crypto &) = delete;
	Crypto &operator=(Crypto &&other);

	virtual ~Crypto();

	void saveRow(
		const Row &row);

	void saveRows(
		const Name &name,
		const Label &owner,
		const RowVector &rows);

	void updateRow(
		const Row &row);

	bool isNameLabelPresent(
		const Name &name,
		const Label &owner) const;

	RowOptional getRow(
		const Name &name,
		const Label &ownerLabel,
		DataType type);

	RowOptional getRow(
		const Name &name,
		const Label &ownerLabel,
		DataType typeRangeStart,
		DataType typeRangeStop);

	void getRows(
		const Name &name,
		const Label &ownerLabel,
		DataType type,
		RowVector &output);

	void getRows(
		const Name &name,
		const Label &ownerLabel,
		DataType typeRangeStart,
		DataType typeRangeStop,
		RowVector &output);

	void listNames(
		const Label &smackLabel,
		LabelNameVector &labelNameVector,
		DataType type);

	void listNames(
		const Label &smackLabel,
		LabelNameVector &labelNameVector,
		DataType typeRangeStart,
		DataType typeRangeStop);

	bool deleteRow(
		const Name &name,
		const Label &ownerLabel);

	// keys
	void saveKey(const Label &label, const RawBuffer &key);
	RawBufferOptional getKey(const Label &label);
	void deleteKey(const Label &label);

	// permissions
	void setPermission(
		const Name &name,
		const Label &ownerLabel,
		const Label &accessorLabel,
		const PermissionMask permissionMask);

	PermissionMaskOptional getPermissionRow(
		const Name &name,
		const Label &ownerLabel,
		const Label &accessorLabel) const;

	// transactions
	int beginTransaction();
	int commitTransaction();
	int rollbackTransaction();

	class Transaction {
	public:
		Transaction(Crypto *db) : m_db(db), m_inTransaction(false)
		{
			if (!m_db->m_inUserTransaction) {
				try {
					m_db->m_connection->ExecCommand("BEGIN EXCLUSIVE");
					m_db->m_inUserTransaction = true;
					m_inTransaction = true;
				} catch (const SqlConnection::Exception::InternalError &) {
					ThrowErr(Exc::TransactionFailed, "sqlite got into infinite busy state");
				} catch (const SqlConnection::Exception::Base &) {
					ThrowErr(Exc::TransactionFailed, "Couldn't begin transaction");
				}
			}
		}

		void commit()
		{
			if (m_inTransaction) {
				try {
					m_db->m_connection->CommitTransaction();
					m_db->m_inUserTransaction = false;
					m_inTransaction = false;
				} catch (const SqlConnection::Exception::InternalError &) {
					ThrowErr(Exc::TransactionFailed, "sqlite got into infinite busy state");
				} catch (const SqlConnection::Exception::Base &) {
					ThrowErr(Exc::TransactionFailed, "Couldn't commit transaction");
				}
			}
		}

		void rollback()
		{
			if (m_inTransaction) {
				try {
					m_db->m_connection->RollbackTransaction();
					m_db->m_inUserTransaction = false;
					m_inTransaction = false;
				} catch (const SqlConnection::Exception::InternalError &) {
					ThrowErr(Exc::TransactionFailed, "sqlite got into infinite busy state");
				} catch (const SqlConnection::Exception::Base &) {
					ThrowErr(Exc::TransactionFailed, "Couldn't rollback transaction");
				}
			}
		}

		~Transaction()
		{
			try {
				if (m_inTransaction) {
					m_db->m_inUserTransaction = false;
					m_db->m_connection->RollbackTransaction();
				}
			} catch (const SqlConnection::Exception::InternalError &) {
				ThrowErr(Exc::TransactionFailed, "sqlite got into infinite busy state");
			} catch (const SqlConnection::Exception::Base &) {
				LogError("Transaction rollback failed!");
			}
		}

	private:
		Crypto *m_db;
		bool m_inTransaction;
	};

protected:
	SqlConnection *m_connection;

private:
	bool m_inUserTransaction;

	void resetDB();
	void initDatabase();
	void createDBSchema();
	/**
	 * return current database version
	 *
	 * @param[out] schemaVersion    if success, will contain DB schema version code
	 *
	 * @return false on DB empty or corrupted, true if information read
	 */
	bool getDBVersion(int &schemaVersion);

	using ScriptOptional = boost::optional<std::string>;
	ScriptOptional getScript(const std::string &scriptName) const;
	ScriptOptional getMigrationScript(int db_version) const;

	Row getRow(
		const SqlConnection::DataCommandUniquePtr &selectCommand) const;

	void createTable(
		const char *create_cmd,
		const char *table_name);

	void createView(
		const char *create_cmd);

	class SchemaInfo {
	public:
		explicit SchemaInfo(const Crypto *db) : m_db(db) {}

		void setVersionInfo();
		bool getVersionInfo(int &version) const;

	private:
		const Crypto *m_db;
	};

public:
	class NameTable {
	public:
		explicit NameTable(SqlConnection *connection) : m_connection(connection) {}

		void addRow(
			const Name &name,
			const Label &ownerLabel);

		void deleteRow(
			const Name &name,
			const Label &ownerLabel);

		void deleteAllRows(
			const Label &ownerLabel);

		bool isPresent(
			const Name &name,
			const Label &ownerLabel) const;

	private:
		SqlConnection *m_connection;
	};

	class ObjectTable {
	public:
		explicit ObjectTable(SqlConnection *connection) : m_connection(connection) {}

		void addRow(
			const Row &row);
		void updateRow(
			const Row &row);

	private:
		SqlConnection *m_connection;
	};

	class PermissionTable {
	public:
		explicit PermissionTable(SqlConnection *connection) : m_connection(
				connection) {}

		void setPermission(
			const Name &name,
			const Label &ownerLabel,
			const Label &accessorLabel,
			const PermissionMask permissionMask);

		PermissionMaskOptional getPermissionRow(
			const Name &name,
			const Label &ownerLabel,
			const Label &accessorLabel) const;

	private:
		SqlConnection *m_connection;
	};
};

} // namespace DB
} // namespace CKM

#pragma GCC diagnostic pop
#endif // CKM_DB_CRYPTO_H