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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
#include "app.h"
#include "mytime.h"
#include <assert.h>
#include <errno.h>
#include <linux/limits.h>
#include <libgen.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sqlite3.h>
#include <fcntl.h>
static void sqliteError(void *pArg, int iErrCode, const char *zMsg) {
// @CLEANUP: We should stuff these somewhere, but for now just print it
fprintf(stderr, "(%d) %s\n", iErrCode, zMsg);
}
static int generate_secret(char *out) {
unsigned char bytes[16];
int fd = open("/dev/urandom", O_RDONLY);
if (fd < 0) abort();
ssize_t n = read(fd, bytes, 16);
close(fd);
if (n != 16) abort();
static const char hex[] = "0123456789abcdef";
for (int i = 0; i < 16; i++) {
out[i*2] = hex[(bytes[i] >> 4) & 0x0f];
out[i*2+1] = hex[bytes[i] & 0x0f];
}
out[32] = '\0';
return 0;
}
// https://stackoverflow.com/questions/2336242/recursive-mkdir-system-call-on-unix
static void _mkdir(const char *dir) {
char tmp[PATH_MAX];
char *p = NULL;
size_t len;
int rc;
snprintf(tmp, sizeof(tmp),"%s",dir);
len = strlen(tmp);
if (tmp[len - 1] == '/')
tmp[len - 1] = 0;
for (p = tmp + 1; *p; p++)
if (*p == '/') {
*p = 0;
rc = mkdir(tmp, S_IRWXU);
if(rc != 0 && errno != EEXIST) {
abort();
}
*p = '/';
}
rc = mkdir(tmp, S_IRWXU);
if(rc != 0 && errno != EEXIST) {
abort();
}
}
void app_startup(struct App *app) {
// @CLEANUP: Move this to main? doesn't belong here at least
sqlite3_config(SQLITE_CONFIG_LOG, sqliteError, NULL);
sqlite3_config(SQLITE_CONFIG_URI, 1);
int err;
printf("Running migrations against %s\n", app->dbname);
if(memcmp("file::memory", app->dbname, 12) != 0) {
char *full = strdup(app->dbname);
char *dbdir = dirname(full);
_mkdir(dbdir);
free(full);
}
sqlite3 *conn;
if((err = sqlite3_open(app->dbname, &conn)) != SQLITE_OK) {
fprintf(stderr, "(%d) %s\n", err, sqlite3_errmsg(conn));
abort();
}
sqlite3_db_config(conn, SQLITE_DBCONFIG_ENABLE_FKEY, 1, NULL);
if(sqlite3_exec(conn, "CREATE TABLE IF NOT EXISTS migration (id INTEGER PRIMARY KEY NOT NULL)", NULL, NULL, NULL) != SQLITE_OK) {
abort();
}
sqlite3_stmt *stmt;
if(sqlite3_prepare_v2(conn, "SELECT MAX(id) FROM migration", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
uint64_t maxVersion = 0;
while((err = sqlite3_step(stmt)) == SQLITE_ROW) {
maxVersion = sqlite3_column_int64(stmt, 0);
}
if(err != SQLITE_DONE) {
abort();
}
if(sqlite3_finalize(stmt) != SQLITE_OK) {
abort();
}
if(maxVersion < 1) {
char *prog =
"BEGIN TRANSACTION;"
"CREATE TABLE backups ("
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
"client TEXT NOT NULL, "
"status TEXT NOT NULL, "
"started TEXT NOT NULL, "
"completed TEXT "
") STRICT";
if(sqlite3_exec(conn, prog, NULL, NULL, NULL) != SQLITE_OK) {
abort();
}
sqlite3_stmt *stmt;
if(sqlite3_prepare_v2(conn, "INSERT INTO migration (id) VALUES (?1)", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_int64(stmt, 1, 1) != SQLITE_OK) {
abort();
}
if(sqlite3_step(stmt) != SQLITE_DONE) {
abort();
}
if(sqlite3_finalize(stmt) != SQLITE_OK) {
abort();
}
if(sqlite3_exec(conn, "COMMIT TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) {
abort();
}
}
if(maxVersion < 2) {
char *prog =
"BEGIN TRANSACTION;"
"CREATE TABLE clients ("
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
"name TEXT NOT NULL "
") STRICT;"
"INSERT INTO clients(name) "
"SELECT b.client "
"FROM backups b "
"GROUP BY b.client;"
"CREATE TABLE backups_next ("
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
"client INTEGER NOT NULL, "
"status TEXT NOT NULL, "
"started TEXT NOT NULL, "
"completed TEXT ,"
"FOREIGN KEY(client) REFERENCES clients(id)"
") STRICT;"
"INSERT INTO backups_next(id, client, status, started, completed) "
"SELECT b.id, c.id, b.status, b.started, b.completed "
"FROM backups b "
"JOIN clients c ON b.client = c.name;"
"DROP TABLE backups;"
"ALTER TABLE backups_next RENAME TO backups;";
if(sqlite3_exec(conn, prog, NULL, NULL, NULL) != SQLITE_OK) {
abort();
}
sqlite3_stmt *stmt;
if(sqlite3_prepare_v2(conn, "INSERT INTO migration (id) VALUES (?1)", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_int64(stmt, 1, 2) != SQLITE_OK) {
abort();
}
if(sqlite3_step(stmt) != SQLITE_DONE) {
abort();
}
if(sqlite3_finalize(stmt) != SQLITE_OK) {
abort();
}
if(sqlite3_exec(conn, "COMMIT TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) {
abort();
}
}
if(maxVersion < 3) {
char *prog =
"BEGIN TRANSACTION;"
"CREATE UNIQUE INDEX clients_name ON clients(name)";
if(sqlite3_exec(conn, prog, NULL, NULL, NULL) != SQLITE_OK) {
abort();
}
sqlite3_stmt *stmt;
if(sqlite3_prepare_v2(conn, "INSERT INTO migration (id) VALUES (?1)", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_int64(stmt, 1, 3) != SQLITE_OK) {
abort();
}
if(sqlite3_step(stmt) != SQLITE_DONE) {
abort();
}
if(sqlite3_finalize(stmt) != SQLITE_OK) {
abort();
}
if(sqlite3_exec(conn, "COMMIT TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) {
abort();
}
}
if(maxVersion < 4) {
char *prog =
"BEGIN TRANSACTION;"
"ALTER TABLE clients ADD COLUMN secret TEXT NOT NULL DEFAULT ''";
if(sqlite3_exec(conn, prog, NULL, NULL, NULL) != SQLITE_OK) {
abort();
}
sqlite3_stmt *stmt;
if(sqlite3_prepare_v2(conn, "INSERT INTO migration (id) VALUES (?1)", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_int64(stmt, 1, 4) != SQLITE_OK) {
abort();
}
if(sqlite3_step(stmt) != SQLITE_DONE) {
abort();
}
if(sqlite3_finalize(stmt) != SQLITE_OK) {
abort();
}
if(sqlite3_exec(conn, "COMMIT TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) {
abort();
}
}
}
int submit_report(struct App *app, char *client, char *secret, int is_finish) {
sqlite3 *conn;
int err;
if((err = sqlite3_open(app->dbname, &conn)) != SQLITE_OK) {
abort();
}
struct timespec now = get_current_time();
int64_t clientId;
{
sqlite3_stmt *stmt;
if(sqlite3_prepare_v2(conn, "SELECT id, secret FROM clients WHERE name = ?1", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_text(stmt, 1, client, -1, SQLITE_STATIC) != SQLITE_OK) {
abort();
}
if(sqlite3_step(stmt) != SQLITE_ROW) {
abort();
}
if(sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
abort();
}
clientId = sqlite3_column_int64(stmt, 0);
const char *stored_secret = (const char *)sqlite3_column_text(stmt, 1);
if(stored_secret == NULL || strcmp(stored_secret, secret) != 0) {
sqlite3_finalize(stmt);
sqlite3_close(conn);
return -2;
}
if(sqlite3_step(stmt) != SQLITE_DONE) {
abort();
}
if(sqlite3_finalize(stmt) != SQLITE_OK) {
abort();
}
}
if(!is_finish) {
// REV_BEGIN
sqlite3_stmt *stmt;
// Mark any existing RUNNING backup as INTERRUPTED
if(sqlite3_prepare_v2(conn, "UPDATE backups SET status = 'INTERRUPTED', completed = datetime(?1, 'unixepoch', 'subsec') WHERE client = ?2 AND status = 'RUNNING'", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_double(stmt, 1, TIME_AS_FLOAT(now)) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_int64(stmt, 2, clientId) != SQLITE_OK) {
abort();
}
if(sqlite3_step(stmt) != SQLITE_DONE) {
abort();
}
if(sqlite3_finalize(stmt) != SQLITE_OK) {
abort();
}
stmt = NULL;
// Insert the new backup
if(sqlite3_prepare_v2(conn, "INSERT INTO backups (client, status, started) VALUES (?1, ?2, datetime(?3, 'unixepoch', 'subsec'))", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_int64(stmt, 1, clientId) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_text(stmt, 2, "RUNNING", -1, SQLITE_STATIC) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_double(stmt, 3, TIME_AS_FLOAT(now)) != SQLITE_OK) {
abort();
}
if(sqlite3_step(stmt) != SQLITE_DONE) {
abort();
}
if(sqlite3_finalize(stmt) != SQLITE_OK) {
abort();
}
} else {
// REV_FINISH
uint64_t backupId;
sqlite3_stmt *stmt;
if(sqlite3_prepare_v2(conn, "SELECT id FROM backups WHERE client = ?1 AND status = 'RUNNING'", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_int64(stmt, 1, clientId) != SQLITE_OK) {
abort();
}
if(sqlite3_step(stmt) != SQLITE_ROW) {
abort();
}
if(sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
abort();
}
backupId = sqlite3_column_int64(stmt, 0);
if(sqlite3_step(stmt) != SQLITE_DONE) {
abort();
}
if(sqlite3_finalize(stmt) != SQLITE_OK) {
abort();
}
stmt = NULL;
if(sqlite3_prepare_v2(conn, "UPDATE backups SET status = ?1, completed = datetime(?2, 'unixepoch', 'subsec') WHERE id = ?3", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_text(stmt, 1, "FINISHED", -1, SQLITE_STATIC) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_double(stmt, 2, TIME_AS_FLOAT(now)) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_int64(stmt, 3, backupId) != SQLITE_OK) {
abort();
}
if(sqlite3_step(stmt) != SQLITE_DONE) {
abort();
}
if(sqlite3_finalize(stmt) != SQLITE_OK) {
abort();
}
}
if(sqlite3_close(conn) != SQLITE_OK) {
abort();
}
return 0;
}
int create_client(struct App *app, char *name, struct CreateClientResult *result) {
sqlite3 *conn;
int err;
if((err = sqlite3_open(app->dbname, &conn)) != SQLITE_OK) {
abort();
}
if(generate_secret(result->secret) != 0) {
abort();
}
{
sqlite3_stmt *stmt;
if(sqlite3_prepare_v2(conn, "INSERT INTO clients (name, secret) VALUES (?1, ?2)", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_text(stmt, 1, name, -1, SQLITE_STATIC) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_text(stmt, 2, result->secret, -1, SQLITE_STATIC) != SQLITE_OK) {
abort();
}
if(sqlite3_step(stmt) != SQLITE_DONE) {
abort();
}
result->id = sqlite3_last_insert_rowid(conn);
if(sqlite3_finalize(stmt) != SQLITE_OK) {
abort();
}
}
if(sqlite3_close(conn) != SQLITE_OK) {
abort();
}
return 0;
}
int delete_client(struct App *app, int64_t client_id) {
sqlite3 *conn;
if(sqlite3_open(app->dbname, &conn) != SQLITE_OK) {
abort();
}
sqlite3_stmt *stmt;
if(sqlite3_prepare_v2(conn, "DELETE FROM clients WHERE id = ?1", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_int64(stmt, 1, client_id) != SQLITE_OK) {
abort();
}
if(sqlite3_step(stmt) != SQLITE_DONE) {
abort();
}
if(sqlite3_finalize(stmt) != SQLITE_OK) {
abort();
}
if(sqlite3_close(conn) != SQLITE_OK) {
abort();
}
return 0;
}
int list_clients(struct App *app, struct ListClientResult *result) {
sqlite3 *conn;
int err;
if((err = sqlite3_open(app->dbname, &conn)) != SQLITE_OK) {
abort();
}
{
sqlite3_stmt *stmt;
const char *sql =
"SELECT c.id, c.name, c.secret, unixepoch(b_success.completed,'subsec'), b_last.status "
"FROM clients c "
"LEFT JOIN backups b_success ON b_success.client = c.id "
" AND b_success.id = (SELECT id FROM backups WHERE client = c.id AND status = 'FINISHED' ORDER BY id DESC LIMIT 1) "
"LEFT JOIN backups b_last ON b_last.client = c.id "
" AND b_last.id = (SELECT id FROM backups WHERE client = c.id ORDER BY id DESC LIMIT 1) "
"WHERE c.id > ?1 ORDER BY c.id ASC LIMIT ?2";
if(sqlite3_prepare_v2(conn, sql, -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_int64(stmt, 1, -1) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_int64(stmt, 2, 16) != SQLITE_OK) {
abort();
}
int ret;
int index = 0;
while((ret = sqlite3_step(stmt)) == SQLITE_ROW) {
assert(index < 16);
if(sqlite3_column_type(stmt, 0) == SQLITE_NULL) {
abort();
}
int64_t clientId = sqlite3_column_int64(stmt, 0);
const uint8_t *name = sqlite3_column_text(stmt, 1);
const uint8_t *secret = sqlite3_column_text(stmt, 2);
const double last_backup = sqlite3_column_double(stmt, 3);
const uint8_t *backup_status = sqlite3_column_text(stmt, 4);
result->clients[index].id = clientId;
strncpy(result->clients[index].name, (char*)name, CLIENT_NAME_MAX);
strncpy(result->clients[index].secret, secret ? (char*)secret : "", CLIENT_SECRET_LEN);
result->clients[index].last_backup = time_from_double(last_backup);
if (backup_status == NULL) {
strncpy(result->clients[index].status, "MISSING", BACKUP_STATUS_MAX);
result->clients[index].is_stale = 0;
} else if (strcmp((char*)backup_status, "RUNNING") == 0) {
strncpy(result->clients[index].status, "RUNNING", BACKUP_STATUS_MAX);
result->clients[index].is_stale = 0;
} else if (strcmp((char*)backup_status, "INTERRUPTED") == 0) {
strncpy(result->clients[index].status, "INTERRUPTED", BACKUP_STATUS_MAX);
result->clients[index].is_stale = 0;
} else {
strncpy(result->clients[index].status, "OK", BACKUP_STATUS_MAX);
result->clients[index].is_stale = 0;
}
index++;
}
if(ret != SQLITE_DONE) {
abort();
}
result->clients_len = index;
if(sqlite3_finalize(stmt) != SQLITE_OK) {
abort();
}
}
if(sqlite3_close(conn) != SQLITE_OK) {
abort();
}
return 0;
}
int get_client(struct App *app, int64_t client_id, struct GetClientOnlyResult *result) {
sqlite3 *conn;
int err;
if((err = sqlite3_open(app->dbname, &conn)) != SQLITE_OK) {
abort();
}
result->found = 0;
sqlite3_stmt *stmt;
if(sqlite3_prepare_v2(conn, "SELECT id, name, secret FROM clients WHERE id = ?1", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_int64(stmt, 1, client_id) != SQLITE_OK) {
abort();
}
int ret = sqlite3_step(stmt);
if(ret == SQLITE_ROW) {
result->found = 1;
result->id = sqlite3_column_int64(stmt, 0);
const uint8_t *name = sqlite3_column_text(stmt, 1);
const uint8_t *secret = sqlite3_column_text(stmt, 2);
strncpy(result->name, (char*)name, CLIENT_NAME_MAX);
strncpy(result->secret, (char*)secret, CLIENT_SECRET_LEN);
} else if(ret != SQLITE_DONE) {
abort();
}
if(sqlite3_finalize(stmt) != SQLITE_OK) {
abort();
}
if(sqlite3_close(conn) != SQLITE_OK) {
abort();
}
return 0;
}
int get_client_backups(struct App *app, int64_t client_id, struct GetBackupsResult *result) {
sqlite3 *conn;
int err;
if((err = sqlite3_open(app->dbname, &conn)) != SQLITE_OK) {
abort();
}
uint16_t limit = result->backups_len;
sqlite3_stmt *stmt;
if(sqlite3_prepare_v2(conn, "SELECT id, status, unixepoch(started, 'subsec'), unixepoch(completed, 'subsec') FROM backups WHERE client = ?1 ORDER BY started DESC LIMIT ?2", -1, &stmt, NULL) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_int64(stmt, 1, client_id) != SQLITE_OK) {
abort();
}
if(sqlite3_bind_int(stmt, 2, limit) != SQLITE_OK) {
abort();
}
int ret;
int index = 0;
while((ret = sqlite3_step(stmt)) == SQLITE_ROW) {
if(index >= limit) break;
result->backups[index].id = sqlite3_column_int64(stmt, 0);
const uint8_t *status = sqlite3_column_text(stmt, 1);
const double started = sqlite3_column_double(stmt, 2);
const double completed = sqlite3_column_double(stmt, 3);
strncpy(result->backups[index].status, status ? (char*)status : "", BACKUP_STATUS_MAX);
result->backups[index].started = time_from_double(started);
result->backups[index].completed = time_from_double(completed);
index++;
}
if(ret != SQLITE_DONE && ret != SQLITE_ROW) {
abort();
}
result->backups_len = index;
if(sqlite3_finalize(stmt) != SQLITE_OK) {
abort();
}
if(sqlite3_close(conn) != SQLITE_OK) {
abort();
}
return 0;
}
|