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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sqlite3.h>
#include <sodium.h>
#include "db.h"
#include "schema.sql.h"
#define SQLITE(func,...) do{if(sqlite3_##func(__VA_ARGS__)!=SQLITE_OK){die_sqlite("sqlite3_" #func);}}while(0)
#define DATABASE_VERSION 1
#define PASSHASH_OPSLIMIT 3
#define PASSHASH_MEMLIMIT crypto_pwhash_MEMLIMIT_INTERACTIVE
sqlite3 *database=NULL;
__attribute__((noreturn))
static void die_sqlite(const char *func){
die("%s: %s",func,sqlite3_errmsg(database));
}
struct hashed_password {
char str[crypto_pwhash_STRBYTES + 1];
int length; // -1 if hashing failed
};
static struct hashed_password hash_password(const char *pass) {
struct hashed_password result;
int ret = crypto_pwhash_str(
result.str, pass, strlen(pass),
PASSHASH_OPSLIMIT, PASSHASH_MEMLIMIT);
if (ret != 0) {
result.length = -1;
} else {
result.length = crypto_pwhash_STRBYTES;
}
return result;
}
void db_init(void){
SQLITE(config,SQLITE_CONFIG_SERIALIZED);
SQLITE(initialize);
SQLITE(open_v2,"db.db",&database,SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE,NULL);
SQLITE(busy_timeout,database,500);
sqlite3_stmt *stmt;
int res = sqlite3_prepare_v2(database, "select version from Meta", -1, &stmt, NULL);
if (res == SQLITE_OK && sqlite3_step(stmt) == SQLITE_ROW) {
i64 version = sqlite3_column_int64(stmt, 0);
SQLITE(finalize, stmt);
if (version != DATABASE_VERSION) {
die("Database version incompatible: database %" PRIi64 ", application %d",
version, DATABASE_VERSION);
}
} else {
if (res == SQLITE_OK) SQLITE(finalize, stmt);
char *str=malloc(schema_sql_len+1,char);
memcpy(str,schema_sql,schema_sql_len);
str[schema_sql_len]='\0';
SQLITE(exec,database,str,NULL,NULL,NULL);
free(str);
SQLITE(prepare_v2, database, "insert into Meta (version) values (?)", -1, &stmt, NULL);
SQLITE(bind_int64, stmt, 1, DATABASE_VERSION);
if (sqlite3_step(stmt) != SQLITE_DONE) {
die("Could not set database version: %s", sqlite3_errmsg(database));
}
}
}
void db_reinit(void){
SQLITE(open_v2,"db.db",&database,SQLITE_OPEN_READWRITE,NULL);
SQLITE(busy_timeout,database,500);
}
void db_close(void){
sqlite3_close(database);
SQLITE(shutdown);
database=NULL;
}
static char* gen_room_name(void){
const int name_len=8;
const char *alphabet="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int alpha_len=strlen(alphabet);
char *name=malloc(name_len+1,char);
for(int i=0;i<name_len;i++){
name[i]=alphabet[random()%alpha_len];
}
name[name_len]='\0';
debug("Generated name: %s",name);
return name;
}
struct db_name_id db_create_room(void){
char *name=gen_room_name();
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,"insert into Rooms (name) values (?)",-1,&stmt,NULL);
SQLITE(bind_text,stmt,1,name,-1,SQLITE_STATIC);
bool success=sqlite3_step(stmt)==SQLITE_DONE;
SQLITE(finalize,stmt);
i64 userid=sqlite3_last_insert_rowid(database);
if(!success){
free(name);
return (struct db_name_id){NULL,-1};
}
return (struct db_name_id){name,userid};
}
bool db_add_member(i64 roomid,i64 userid){
assert(roomid!=-1&&userid!=-1);
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,"insert into Members (room, user) values (?, ?)",-1,&stmt,NULL);
SQLITE(bind_int64,stmt,1,roomid);
SQLITE(bind_int64,stmt,2,userid);
bool success=sqlite3_step(stmt)==SQLITE_DONE;
SQLITE(finalize,stmt);
return success;
}
bool db_is_member(i64 roomid,i64 userid){
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,"select 1 from Members where room = ? and user = ?",-1,&stmt,NULL);
SQLITE(bind_int64,stmt,1,roomid);
SQLITE(bind_int64,stmt,2,userid);
bool success=sqlite3_step(stmt)==SQLITE_ROW;
SQLITE(finalize,stmt);
return success;
}
struct db_user_list db_list_members(i64 roomid){
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,
"select U.id, U.name "
"from Users as U, Members as M "
"where M.room = ? and M.user = U.id"
,-1,&stmt,NULL);
SQLITE(bind_int64,stmt,1,roomid);
struct db_user_list ul;
i64 cap=4;
ul.count=0;
ul.list=malloc(cap,struct db_name_id);
int ret;
while((ret=sqlite3_step(stmt))==SQLITE_ROW){
if(ul.count==cap){
cap*=2;
ul.list=realloc(ul.list,cap,struct db_name_id);
}
ul.list[ul.count].id=sqlite3_column_int64(stmt,0);
ul.list[ul.count].name=strdup((const char*)sqlite3_column_text(stmt,1));
ul.count++;
}
if(ret!=SQLITE_DONE)die_sqlite("sqlite3_step");
SQLITE(finalize,stmt);
return ul;
}
i64 db_find_room(const char *name){
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,"select id from Rooms where name = ?",-1,&stmt,NULL);
SQLITE(bind_text,stmt,1,name,-1,SQLITE_STATIC);
i64 roomid=-1;
if(sqlite3_step(stmt)==SQLITE_ROW){
roomid=sqlite3_column_int64(stmt,0);
}
SQLITE(finalize,stmt);
return roomid;
}
char* db_get_roomname(i64 roomid){
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,"select name from Rooms where id = ?",-1,&stmt,NULL);
SQLITE(bind_int64,stmt,1,roomid);
const unsigned char *name_sq=NULL;
if(sqlite3_step(stmt)==SQLITE_ROW){
name_sq=sqlite3_column_text(stmt,0);
}
char *name=NULL;
if(name_sq)name=strdup((const char*)name_sq);
SQLITE(finalize,stmt);
return name;
}
struct db_room_list db_list_rooms(i64 userid){
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,
"select M.room, R.name "
"from Members as M, Rooms as R "
"where M.user = ? and M.room = R.id"
,-1,&stmt,NULL);
SQLITE(bind_int64,stmt,1,userid);
struct db_room_list rl;
i64 cap=4;
rl.count=0;
rl.list=malloc(cap,struct db_name_id);
int ret;
while((ret=sqlite3_step(stmt))==SQLITE_ROW){
if(rl.count==cap){
cap*=2;
rl.list=realloc(rl.list,cap,struct db_name_id);
}
rl.list[rl.count].id=sqlite3_column_int64(stmt,0);
rl.list[rl.count].name=strdup((const char*)sqlite3_column_text(stmt,1));
rl.count++;
}
if(ret!=SQLITE_DONE)die_sqlite("sqlite3_step");
SQLITE(finalize,stmt);
return rl;
}
i64 db_create_user(const char *name,const char *pass){
struct hashed_password passhash = hash_password(pass);
if (passhash.length == -1) return -1;
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,"insert into Users (name, passhash) values (?, ?)",-1,&stmt,NULL);
SQLITE(bind_text,stmt,1,name,-1,SQLITE_STATIC);
SQLITE(bind_text,stmt,2,passhash.str,passhash.length,SQLITE_STATIC);
bool success=sqlite3_step(stmt)==SQLITE_DONE;
SQLITE(finalize,stmt);
if(success){
return sqlite3_last_insert_rowid(database);
} else {
return -1;
}
}
bool db_set_username(i64 userid, const char *name) {
sqlite3_stmt *stmt;
SQLITE(prepare_v2, database, "update Users set name = ? where id = ?", -1, &stmt, NULL);
SQLITE(bind_text, stmt, 1, name, -1, SQLITE_STATIC);
SQLITE(bind_int64, stmt, 2, userid);
bool success = sqlite3_step(stmt) == SQLITE_DONE;
SQLITE(finalize, stmt);
return success;
}
bool db_set_pass(i64 userid, const char *pass) {
struct hashed_password passhash = hash_password(pass);
if (passhash.length == -1) return false;
sqlite3_stmt *stmt;
SQLITE(prepare_v2, database, "update Users set passhash = ? where id = ?", -1, &stmt, NULL);
SQLITE(bind_text, stmt, 1, passhash.str, passhash.length, SQLITE_STATIC);
SQLITE(bind_int64, stmt, 2, userid);
bool success = sqlite3_step(stmt) == SQLITE_DONE;
SQLITE(finalize, stmt);
return success;
}
char* db_get_username(i64 userid){
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,"select name from Users where id = ?",-1,&stmt,NULL);
SQLITE(bind_int64,stmt,1,userid);
const unsigned char *name_sq=NULL;
if(sqlite3_step(stmt)==SQLITE_ROW){
name_sq=sqlite3_column_text(stmt,0);
}
char *name=NULL;
if(name_sq)name=strdup((const char*)name_sq);
SQLITE(finalize,stmt);
return name;
}
bool db_check_pass(i64 userid, const char *pass) {
sqlite3_stmt *stmt;
SQLITE(prepare_v2, database, "select passhash from Users where id = ?", -1, &stmt, NULL);
SQLITE(bind_int64, stmt, 1, userid);
const unsigned char *passhash_sq = NULL;
if (sqlite3_step(stmt) == SQLITE_ROW) {
passhash_sq = sqlite3_column_text(stmt, 0);
}
int ret = crypto_pwhash_str_verify((const char*)passhash_sq, pass, strlen(pass));
bool success = ret == 0;
bool rehash = success &&
crypto_pwhash_str_needs_rehash(
(const char*)passhash_sq, PASSHASH_OPSLIMIT, PASSHASH_MEMLIMIT);
SQLITE(finalize, stmt);
if (rehash) {
fprintf(stderr, "Rehashing password for userid=%" PRIi64 "\n", userid);
// Ignore whether this succeeds; if it doesn't, we continue using the old hash
db_set_pass(userid, pass);
}
return success;
}
i64 db_find_user(const char *name){
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,"select id from Users where name = ?",-1,&stmt,NULL);
SQLITE(bind_text,stmt,1,name,-1,SQLITE_STATIC);
i64 userid=-1;
if(sqlite3_step(stmt)==SQLITE_ROW){
userid=sqlite3_column_int64(stmt,0);
}
SQLITE(finalize,stmt);
return userid;
}
struct db_strings_list db_user_tokens(i64 userid){
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,"select token from Firebase where user = ?",-1,&stmt,NULL);
SQLITE(bind_int64,stmt,1,userid);
struct db_strings_list sl;
i64 cap=4;
sl.count=0;
sl.list=malloc(cap,char*);
int ret;
while((ret=sqlite3_step(stmt))==SQLITE_ROW){
if(sl.count==cap){
cap*=2;
sl.list=realloc(sl.list,cap,char*);
}
sl.list[sl.count]=strdup((const char*)sqlite3_column_text(stmt,0));
sl.count++;
}
if(ret!=SQLITE_DONE)die_sqlite("sqlite3_step");
SQLITE(finalize,stmt);
return sl;
}
bool db_add_token(i64 userid,const char *token){
assert(userid!=-1);
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,"delete from Firebase where token = ?",-1,&stmt,NULL);
SQLITE(bind_text,stmt,1,token,-1,SQLITE_STATIC);
bool success=sqlite3_step(stmt)==SQLITE_DONE;
SQLITE(finalize,stmt);
if(!success)return false;
SQLITE(prepare_v2,database,"insert into Firebase (user, token) values (?, ?)",-1,&stmt,NULL);
SQLITE(bind_int64,stmt,1,userid);
SQLITE(bind_text,stmt,2,token,-1,SQLITE_STATIC);
success=sqlite3_step(stmt)==SQLITE_DONE;
SQLITE(finalize,stmt);
return success;
}
bool db_delete_token(i64 userid,const char *token){
assert(userid!=-1);
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,"delete from Firebase where user = ? and token = ?",-1,&stmt,NULL);
SQLITE(bind_int64,stmt,1,userid);
SQLITE(bind_text,stmt,2,token,-1,SQLITE_STATIC);
bool success=sqlite3_step(stmt)==SQLITE_DONE;
SQLITE(finalize,stmt);
return success;
}
void db_create_message(i64 roomid,i64 userid,i64 timestamp,const char *message){
sqlite3_stmt *stmt;
SQLITE(prepare_v2,database,
"insert into Messages (room, user, time, message) "
"values (?, ?, ?, ?)"
,-1,&stmt,NULL);
SQLITE(bind_int64,stmt,1,roomid);
SQLITE(bind_int64,stmt,2,userid);
SQLITE(bind_int64,stmt,3,timestamp);
SQLITE(bind_blob,stmt,4,message,strlen(message),SQLITE_STATIC);
if(sqlite3_step(stmt)!=SQLITE_DONE)die_sqlite("sqlite3_step");
SQLITE(finalize,stmt);
}
struct db_message_list db_get_messages(i64 roomid,i64 count){
return db_get_messages_before(roomid,count,-1);
}
struct db_message_list db_get_messages_before(i64 roomid,i64 count,i64 beforeid){
assert(count>=0);
sqlite3_stmt *stmt;
if(beforeid<0){
SQLITE(prepare_v2,database,
"select id, user, time, message "
"from Messages "
"where room = ? "
"order by time desc "
"limit ?"
,-1,&stmt,NULL);
SQLITE(bind_int64,stmt,1,roomid);
SQLITE(bind_int64,stmt,2,count);
} else {
SQLITE(prepare_v2,database,
"select id, user, time, message "
"from Messages "
"where room = ? and time < (select time from Messages where id = ?) "
"order by time desc "
"limit ?"
,-1,&stmt,NULL);
SQLITE(bind_int64,stmt,1,roomid);
SQLITE(bind_int64,stmt,2,beforeid);
SQLITE(bind_int64,stmt,3,count);
}
struct db_message_list ml;
i64 cap=count;
ml.count=0;
ml.list=malloc(cap,struct db_message);
int ret;
while((ret=sqlite3_step(stmt))==SQLITE_ROW){
if(ml.count==cap){
die("sqlite gave too many rows while 'limit %" PRIi64 "' was present",count);
}
ml.list[ml.count].msgid=sqlite3_column_int64(stmt,0);
ml.list[ml.count].roomid=roomid;
ml.list[ml.count].userid=sqlite3_column_int64(stmt,1);
ml.list[ml.count].timestamp=sqlite3_column_int64(stmt,2);
ml.list[ml.count].message=strdup((const char*)sqlite3_column_text(stmt,3));
ml.count++;
}
if(ret!=SQLITE_DONE)die_sqlite("sqlite3_step");
SQLITE(finalize,stmt);
return ml;
}
void db_nullify_name_id(struct db_name_id ni){
if(ni.name)free(ni.name);
}
void db_nullify_room_list(struct db_room_list rl){
for(i64 i=0;i<rl.count;i++){
free(rl.list[i].name);
}
if(rl.list)free(rl.list);
rl.list=NULL;
}
void db_nullify_user_list(struct db_user_list ul){
for(i64 i=0;i<ul.count;i++){
free(ul.list[i].name);
}
if(ul.list)free(ul.list);
ul.list=NULL;
}
void db_nullify_message_list(struct db_message_list ml){
for(i64 i=0;i<ml.count;i++){
free(ml.list[i].message);
}
if(ml.list)free(ml.list);
ml.list=NULL;
}
void db_nullify_strings_list(struct db_strings_list sl){
for(i64 i=0;i<sl.count;i++){
free(sl.list[i]);
}
if(sl.list)free(sl.list);
sl.list=NULL;
}
|