-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql.c
396 lines (343 loc) · 10.9 KB
/
sql.c
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sql.h"
/* Define queries */
static const char yubisql_select_data[] = "SELECT publicid,privateid,key,digest FROM mapping WHERE username = ?;";
static const char yubisql_select_state[] = "SELECT session,timecode,tokencount FROM mapping WHERE username = ?;";
static const char yubisql_update_state[] = "UPDATE mapping SET session = ?, timecode = ?, tokencount = ? WHERE username = ?;";
static const char yubisql_create_credentials[] = "INSERT INTO mapping (username, publicid, privateid, key, digest) VALUES (?, ?, ?, ?, ?);";
static const char yubisql_delete_credentials[] = "DELETE FROM mapping WHERE username = ?;";
static const char yubisql_list_users[] = "SELECT username FROM mapping;";
static const char yubisql_create_table[] = \
"CREATE TABLE mapping( \
username TEXT PRIMARY KEY, \
publicid TEXT NOT NULL, \
key TEXT NOT NULL, \
privateid TEXT NOT NULL, \
session INTEGER DEFAULT 0, \
timecode INTEGER DEFAULT 0, \
tokencount INTEGER DEFAULT 0, \
digest TEXT NOT NULL \
);";
/* Transactions */
static const char yubisql_begin[] = "BEGIN IMMEDIATE;";
static const char yubisql_end[] = "COMMIT;";
static const char yubisql_rollback[] = "ROLLBACK;";
sqlite3*
init(const char* dbname)
{
sqlite3 *ppDb = NULL;
if (sqlite3_open(dbname, &ppDb) != SQLITE_OK) {
sqlite3_close(ppDb);
return NULL;
}
return ppDb;
}
void
sql_close(sqlite3* db)
{
sqlite3_close(db);
}
int try_start_transaction(sqlite3* db)
{
int response;
sqlite3_stmt *ppStmt = NULL;
/* Prepare the request */
response = sqlite3_prepare_v2(db, yubisql_begin, (int) sizeof(yubisql_begin), &ppStmt, NULL);
if (response != SQLITE_OK) {
/* Should never ever happen */
sqlite3_finalize(ppStmt);
return OTP_SQL_ERR;
}
/* Run and verify response */
response = sqlite3_step(ppStmt);
sqlite3_finalize(ppStmt);
switch (response) {
case SQLITE_DONE:
return OTP_SQL_OK;
case SQLITE_BUSY:
return OTP_SQL_MAY_RETRY;
default:
return OTP_SQL_ERR;
}
}
static int
try_or_rollback(sqlite3* db, sqlite3_stmt *ppStmt)
{
int response;
/* Run and verify response */
response = sqlite3_step(ppStmt);
sqlite3_finalize(ppStmt);
switch (response) {
case SQLITE_DONE:
return OTP_SQL_OK;
case SQLITE_BUSY:
rollback(db);
return OTP_SQL_MAY_RETRY;
default:
rollback(db);
return OTP_SQL_ERR;
}
}
#define compile_or_rollback(db,ppStmt,response) \
if (response != SQLITE_OK) { \
/* Should never ever happen */ \
sqlite3_finalize(ppStmt); \
rollback(db); \
return OTP_SQL_ERR; \
}
int try_end_transaction(sqlite3* db)
{
int response;
sqlite3_stmt *ppStmt = NULL;
/* Prepare the request */
response = sqlite3_prepare_v2(db, yubisql_end, (int) sizeof(yubisql_end) - 1, &ppStmt, NULL);
compile_or_rollback(db,ppStmt,response)
return try_or_rollback(db, ppStmt);
}
static void
rollback_r(sqlite3* db, int rec)
{
int response;
sqlite3_stmt *ppStmt = NULL;
/* Prepare the query */
response = sqlite3_prepare_v2(db, yubisql_rollback, (int) sizeof(yubisql_rollback) - 1, &ppStmt, NULL);
if (response != SQLITE_OK) {
/* Should never ever happen */
sqlite3_finalize(ppStmt);
return;
}
/* Run the query, and clean it immediately */
response = sqlite3_step(ppStmt);
sqlite3_finalize(ppStmt);
/* If we didn't achieved to rollback, let's try another time */
if ((response != SQLITE_OK)
&& (!rec)) {
rollback_r(db, 1);
}
}
void
rollback(sqlite3* db)
{
rollback_r(db,0);
}
struct otp_data*
get_otp_data (sqlite3* db, const struct user* user)
{
const unsigned char *ret;
int response, len;
sqlite3_stmt *ppStmt = NULL;
struct otp_data *data;
/* Prepare the request ! */
response = sqlite3_prepare_v2(db, yubisql_select_data, (int) sizeof(yubisql_select_data), &ppStmt, NULL);
if (response != SQLITE_OK) {
sqlite3_finalize(ppStmt);
return NULL;
}
response = sqlite3_bind_text(ppStmt, 1, user->name, user->len, SQLITE_STATIC);
if (response != SQLITE_OK) {
sqlite3_finalize(ppStmt);
return NULL;
}
/* Run it and verify the format of the response */
response = sqlite3_step(ppStmt);
if ((response != SQLITE_ROW)
|| (sqlite3_column_count(ppStmt) != 4)
|| (sqlite3_column_type(ppStmt, 0) != SQLITE_TEXT)
|| (sqlite3_column_type(ppStmt, 1) != SQLITE_TEXT)
|| (sqlite3_column_type(ppStmt, 2) != SQLITE_TEXT)
|| (sqlite3_column_type(ppStmt, 3) != SQLITE_TEXT)) {
sqlite3_finalize(ppStmt);
return NULL;
}
/* Allocate the result struct */
data = calloc(1ul, sizeof(struct otp_data));
if (data == NULL) {
sqlite3_finalize(ppStmt);
return NULL;
}
/* Extract and copy each data */
/* Public ID */
ret = sqlite3_column_text(ppStmt,0);
len = sqlite3_column_bytes(ppStmt,0);
if (len == OTP_PUB_ID_HEX_LEN) {
memcpy(data->pubid, ret, OTP_PUB_ID_HEX_LEN);
} else {
sqlite3_finalize(ppStmt);
free(data);
return NULL;
}
/* AES key */
ret = sqlite3_column_text(ppStmt,2);
len = sqlite3_column_bytes(ppStmt,2);
if (len == OTP_KEY_HEX_LEN) {
memcpy(data->key, ret, OTP_KEY_HEX_LEN);
} else {
sqlite3_finalize(ppStmt);
free(data);
return NULL;
}
/* Private ID hash */
data->privid_hash = strdup((const char *)sqlite3_column_text(ppStmt,1));
data->digest_name = strdup((const char *)sqlite3_column_text(ppStmt,3));
/* If there is more data, we failed */
if (sqlite3_step(ppStmt) == SQLITE_DONE) {
sqlite3_finalize(ppStmt);
return data;
} else {
sqlite3_finalize(ppStmt);
free(data);
return NULL;
}
}
int
try_get_credentials(sqlite3* db, struct otp_state* store, const struct user* user)
{
int response;
sqlite3_stmt *ppStmt = NULL;
/* Prepare the request ! */
response = sqlite3_prepare_v2(db, yubisql_select_state, (int) sizeof(yubisql_select_state), &ppStmt, NULL);
compile_or_rollback(db,ppStmt,response)
response = sqlite3_bind_text(ppStmt, 1, user->name, user->len, SQLITE_STATIC);
compile_or_rollback(db,ppStmt,response)
/* Run and verify response */
response = sqlite3_step(ppStmt);
switch (response) {
case SQLITE_ROW:
break;
case SQLITE_BUSY:
sqlite3_finalize(ppStmt);
rollback(db);
return OTP_SQL_MAY_RETRY;
default:
sqlite3_finalize(ppStmt);
rollback(db);
return OTP_SQL_ERR;
}
if ((sqlite3_column_count(ppStmt) != 3)
|| (sqlite3_column_type(ppStmt,0) != SQLITE_INTEGER)
|| (sqlite3_column_type(ppStmt,1) != SQLITE_INTEGER)
|| (sqlite3_column_type(ppStmt,2) != SQLITE_INTEGER)) {
sqlite3_finalize(ppStmt);
return OTP_SQL_ERR;
}
store->session_counter = (unsigned short) sqlite3_column_int(ppStmt, 0);
store->timecode = (unsigned int) sqlite3_column_int(ppStmt, 1);
store->token_count = (unsigned char) sqlite3_column_int(ppStmt, 2);
return try_or_rollback(db, ppStmt);
}
int
try_update_credentials(sqlite3* db, const struct otp_state* otp, const struct user* user)
{
int response;
sqlite3_stmt *ppStmt = NULL;
/* Prepare the request ! */
response = sqlite3_prepare_v2(db, yubisql_update_state, (int) sizeof(yubisql_update_state), &ppStmt, NULL);
compile_or_rollback(db,ppStmt,response)
response = sqlite3_bind_int(ppStmt, 1, otp->session_counter);
compile_or_rollback(db,ppStmt,response)
response = sqlite3_bind_int(ppStmt, 2, (int)otp->timecode);
compile_or_rollback(db,ppStmt,response)
response = sqlite3_bind_int(ppStmt, 3, otp->token_count);
compile_or_rollback(db,ppStmt,response)
response = sqlite3_bind_text(ppStmt, 4, user->name, user->len, SQLITE_STATIC);
compile_or_rollback(db,ppStmt,response)
/* Run and verify that it's ok */
return try_or_rollback(db, ppStmt);
}
int
try_create_credentials(sqlite3* db, struct otp_data* data, const struct user* user)
{
int response;
sqlite3_stmt *ppStmt = NULL;
/* Prepare the request ! */
response = sqlite3_prepare_v2(db, yubisql_create_credentials, (int) sizeof(yubisql_create_credentials), &ppStmt, NULL);
compile_or_rollback(db,ppStmt,response)
response = sqlite3_bind_text(ppStmt, 1, user->name, user->len, SQLITE_STATIC);
compile_or_rollback(db,ppStmt,response)
response = sqlite3_bind_text(ppStmt, 2, data->pubid, (int) OTP_PUB_ID_HEX_LEN, SQLITE_STATIC);
compile_or_rollback(db,ppStmt,response)
response = sqlite3_bind_text(ppStmt, 3, data->privid_hash, -1, SQLITE_STATIC);
compile_or_rollback(db,ppStmt,response)
response = sqlite3_bind_text(ppStmt, 4, data->key, (int) OTP_KEY_HEX_LEN, SQLITE_STATIC);
compile_or_rollback(db,ppStmt,response)
response = sqlite3_bind_text(ppStmt, 5, data->digest_name, -1, SQLITE_STATIC);
compile_or_rollback(db,ppStmt,response)
return try_or_rollback(db, ppStmt);
}
int
try_delete_credentials(sqlite3* db, const struct user* user)
{
int response;
sqlite3_stmt *ppStmt = NULL;
response = sqlite3_prepare_v2(db, yubisql_delete_credentials, (int) sizeof(yubisql_delete_credentials), &ppStmt, NULL);
compile_or_rollback(db,ppStmt,response)
response = sqlite3_bind_text(ppStmt, 1, user->name, user->len, SQLITE_STATIC);
compile_or_rollback(db,ppStmt,response)
return try_or_rollback(db, ppStmt);
}
void
list_users (sqlite3* db)
{
int response;
sqlite3_stmt *ppStmt = NULL;
/* Prepare the request ! */
response = sqlite3_prepare_v2(db, yubisql_list_users, (int) sizeof(yubisql_list_users), &ppStmt, NULL);
if (response != SQLITE_OK) {
sqlite3_finalize(ppStmt);
printf("Unable to search\n");
return;
}
/* Extract all the responses */
while ((response = sqlite3_step(ppStmt)) == SQLITE_ROW) {
if ((sqlite3_column_count(ppStmt) != 1)
|| (sqlite3_column_type(ppStmt, 0) != SQLITE_TEXT)) {
printf("Error while searching for users: database error\n");
} else {
printf("%s\n",sqlite3_column_text(ppStmt, 0));
}
}
if (response == SQLITE_DONE) {
return;
}
printf("Error while searching for users: SQL error\n");
}
void
create_database(sqlite3* db)
{
int response;
sqlite3_stmt *ppStmt = NULL;
/* Prepare the request ! */
response = sqlite3_prepare_v2(db, yubisql_create_table, (int) sizeof(yubisql_create_table), &ppStmt, NULL);
if (response != SQLITE_OK) {
sqlite3_finalize(ppStmt);
printf("Unable to prepare the query that would create the table\n");
return;
}
/* Run and verify response */
response = sqlite3_step(ppStmt);
sqlite3_finalize(ppStmt);
switch (response) {
case SQLITE_DONE:
printf("Database successfuly created\n");
break;
default:
printf("Unable to create database (%i)\n", response);
break;
}
}
void
free_otp_data(struct otp_data *a)
{
if (a->digest_name != NULL) {
free(a->digest_name);
}
if (a->privid_hash != NULL) {
free(a->privid_hash);
}
free(a);
}