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
|
#include "auth.h"
#include "http.h"
#include "jwt.h"
#include <cjson/cJSON.h>
#include <dynstr.h>
#include <openssl/sha.h>
#include <errno.h>
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
struct auth
{
struct dynstr dir, db;
};
enum {KEYLEN = 32};
static char *dump_db(const char *const path)
{
char *ret = NULL;
FILE *f = NULL;
struct stat sb;
if (stat(path, &sb))
{
fprintf(stderr, "%s: stat(2): %s\n", __func__, strerror(errno));
goto end;
}
else if (sb.st_size > SIZE_MAX)
{
fprintf(stderr, "%s: %s too big (%llu bytes, %zu max)\n",
__func__, path, (unsigned long long)sb.st_size, (size_t)SIZE_MAX);
goto end;
}
else if (!(f = fopen(path, "rb")))
{
fprintf(stderr, "%s: fopen(3): %s\n", __func__, strerror(errno));
goto end;
}
else if (!(ret = malloc(sb.st_size + 1)))
{
fprintf(stderr, "%s: malloc(3): %s\n", __func__, strerror(errno));
goto end;
}
else if (!fread(ret, sb.st_size, 1, f))
{
fprintf(stderr, "%s: failed to dump %zu bytes, ferror=%d\n",
__func__, (size_t)sb.st_size, ferror(f));
goto end;
}
ret[sb.st_size] = '\0';
end:
if (f && fclose(f))
{
fprintf(stderr, "%s: fclose(3): %s\n", __func__, strerror(errno));
free(ret);
return NULL;
}
return ret;
}
static int decode_hex(const char *const hex, unsigned char *buf, size_t n)
{
for (const char *s = hex; *s; s += 2)
{
const char nibble[sizeof "00"] = {*s, *(s + 1)};
if (!n)
return -1;
*buf++ = strtoul(nibble, NULL, 16);
n--;
}
return n ? -1 : 0;
}
static int find_cookie(const cJSON *const users, const char *const cookie)
{
const cJSON *u;
cJSON_ArrayForEach(u, users)
{
const cJSON *const n = cJSON_GetObjectItem(u, "name"),
*const k = cJSON_GetObjectItem(u, "key");
const char *name, *key;
unsigned char dkey[KEYLEN];
if (!n || !(name = cJSON_GetStringValue(n)))
{
fprintf(stderr, "%s: missing username\n", __func__);
return -1;
}
else if (!k || !(key = cJSON_GetStringValue(k)))
{
fprintf(stderr, "%s: missing key\n", __func__);
return -1;
}
else if (decode_hex(key, dkey, sizeof dkey))
{
fprintf(stderr, "%s: decode_hex failed\n", __func__);
return -1;
}
const int res = jwt_check(cookie, dkey, sizeof dkey);
if (!res)
return 0;
if (res < 0)
{
fprintf(stderr, "%s: jwt_decode failed\n", __func__);
return -1;
}
}
return 1;
}
int auth_cookie(const struct auth *const a, const struct http_cookie *const c)
{
int ret = -1;
char *db = NULL;
cJSON *json = NULL;
if (!c->field || !c->value)
return 1;
else if (!(db = dump_db(a->db.str)))
{
fprintf(stderr, "%s: dump_db failed\n", __func__);
goto end;
}
else if (!(json = cJSON_Parse(db)))
{
fprintf(stderr, "%s: cJSON_Parse failed\n", __func__);
goto end;
}
const cJSON *const users = cJSON_GetObjectItem(json, "users");
if (!users)
{
fprintf(stderr, "%s: could not find users\n", __func__);
goto end;
}
else if (!cJSON_IsArray(users))
{
fprintf(stderr, "%s: expected JSON array for users\n", __func__);
goto end;
}
else if ((ret = find_cookie(users, c->value)) < 0)
{
fprintf(stderr, "%s: find_cookie failed\n", __func__);
goto end;
}
end:
free(db);
cJSON_Delete(json);
return ret;
}
static int generate_cookie(const cJSON *const json, const char *const path,
const char *const name, const char *const key, char **const cookie)
{
unsigned char dkey[KEYLEN];
int ret = -1;
char *jwt = NULL;
if (decode_hex(key, dkey, sizeof dkey))
{
fprintf(stderr, "%s: decode_hex failed\n", __func__);
goto end;
}
else if (!(jwt = jwt_encode(name, dkey, sizeof dkey)))
{
fprintf(stderr, "%s: jwt_encode failed\n", __func__);
goto end;
}
else if (!(*cookie = http_cookie_create(name, jwt)))
{
fprintf(stderr, "%s: http_cookie_create failed\n", __func__);
goto end;
}
ret = 0;
end:
free(jwt);
return ret;
}
static int compare_pwd(const char *const salt, const char *const password,
const char *const exp_password)
{
int ret = -1;
enum {SALT_LEN = SHA256_DIGEST_LENGTH};
unsigned char dec_salt[SALT_LEN], sha256[SHA256_DIGEST_LENGTH];
const size_t slen = strlen(salt),
len = strlen(password), n = sizeof dec_salt + len;
unsigned char *const salted = malloc(n);
if (slen != SALT_LEN * 2)
{
fprintf(stderr, "%s: unexpected salt length: %zu\n", __func__, slen);
goto end;
}
else if (!salted)
{
fprintf(stderr, "%s: malloc(3): %s\n", __func__, strerror(errno));
goto end;
}
else if (decode_hex(salt, dec_salt, sizeof dec_salt))
{
fprintf(stderr, "%s: decode_hex failed\n", __func__);
goto end;
}
memcpy(salted, dec_salt, sizeof dec_salt);
memcpy(salted + sizeof dec_salt, password, len);
if (!SHA256(salted, n, sha256))
{
fprintf(stderr, "%s: SHA256 (first round) failed\n", __func__);
goto end;
}
enum {ROUNDS = 1000 - 1};
for (int i = 0; i < ROUNDS; i++)
if (!SHA256(sha256, sizeof sha256, sha256))
{
fprintf(stderr, "%s: SHA256 failed\n", __func__);
goto end;
}
char sha256_str[sizeof
"00000000000000000000000000000000"
"00000000000000000000000000000000"];
for (struct {char *p; size_t i;} a = {.p = sha256_str};
a.i < sizeof sha256 / sizeof *sha256; a.i++, a.p += 2)
sprintf(a.p, "%02x", sha256[a.i]);
if (!strcmp(sha256_str, exp_password))
ret = 0;
else
/* Positive error code for password mismatch. */
ret = 1;
end:
free(salted);
return ret;
}
int auth_login(const struct auth *const a, const char *const user,
const char *const password, char **const cookie)
{
int ret = -1;
const char *const path = a->db.str;
char *const db = dump_db(path);
cJSON *json = NULL;
if (!db)
{
fprintf(stderr, "%s: dump_db failed\n", __func__);
goto end;
}
else if (!(json = cJSON_Parse(db)))
{
fprintf(stderr, "%s: cJSON_Parse failed\n", __func__);
goto end;
}
const cJSON *const users = cJSON_GetObjectItem(json, "users");
if (!users)
{
fprintf(stderr, "%s: could not find users\n", __func__);
goto end;
}
else if (!cJSON_IsArray(users))
{
fprintf(stderr, "%s: expected JSON array for users\n", __func__);
goto end;
}
cJSON *u;
cJSON_ArrayForEach(u, users)
{
const cJSON *const n = cJSON_GetObjectItem(u, "name"),
*const s = cJSON_GetObjectItem(u, "salt"),
*const p = cJSON_GetObjectItem(u, "password"),
*const k = cJSON_GetObjectItem(u, "key");
const char *name, *salt, *pwd, *key;
if (!n || !(name = cJSON_GetStringValue(n)))
{
fprintf(stderr, "%s: missing username\n", __func__);
goto end;
}
else if (!s || !(salt = cJSON_GetStringValue(s)))
{
fprintf(stderr, "%s: missing salt\n", __func__);
goto end;
}
else if (!p || !(pwd = cJSON_GetStringValue(p)))
{
fprintf(stderr, "%s: missing password\n", __func__);
goto end;
}
else if (!k || !(key = cJSON_GetStringValue(k)))
{
fprintf(stderr, "%s: missing key\n", __func__);
goto end;
}
else if (!strcmp(name, user))
{
const int res = compare_pwd(salt, password, pwd);
if (res < 0)
{
fprintf(stderr, "%s: generate_cookie failed\n", __func__);
goto end;
}
else if (!res)
{
if (generate_cookie(json, path, name, key, cookie))
{
fprintf(stderr, "%s: generate_cookie failed\n", __func__);
goto end;
}
ret = 0;
goto end;
}
}
}
ret = 1;
end:
free(db);
cJSON_Delete(json);
return ret;
}
void auth_free(struct auth *const a)
{
if (a)
{
dynstr_free(&a->dir);
dynstr_free(&a->db);
}
free(a);
}
const char *auth_dir(const struct auth *const a)
{
return a->dir.str;
}
static int create_db(const char *const path)
{
int ret = -1;
const int fd = open(path, O_WRONLY | O_CREAT, 0600);
if (fd < 0)
{
fprintf(stderr, "%s: open(2): %s\n", __func__, strerror(errno));
goto end;
}
static const char db[] = "{\"users\": []}\n";
for (struct {size_t n; const void *p;}
a = {.n = strlen(db), .p = db}; a.n;)
{
const ssize_t n = write(fd, a.p, a.n);
if (n < 0)
{
fprintf(stderr, "%s: write(2): %s\n", __func__, strerror(errno));
goto end;
}
a.n -= n;
a.p = ((const char *)a.p) + n;
}
printf("Created login database at %s\n", path);
ret = 0;
end:
if (fd >= 0 && close(fd))
{
fprintf(stderr, "%s: close(2): %s\n", __func__, strerror(errno));
return -1;
}
return ret;
}
static int init_db(struct auth *const a)
{
struct stat sb;
const char *const path = a->db.str;
if (stat(path, &sb))
{
if (errno == ENOENT)
return create_db(path);
else
{
fprintf(stderr, "%s: stat(2): %s\n", __func__, strerror(errno));
return -1;
}
}
return 0;
}
struct auth *auth_alloc(const char *const dir)
{
struct auth *const a = malloc(sizeof *a);
if (!a)
{
fprintf(stderr, "%s: malloc(3) auth: %s\n", __func__, strerror(errno));
goto failure;
}
*a = (const struct auth){0};
dynstr_init(&a->db);
dynstr_init(&a->dir);
if (dynstr_append(&a->dir, "%s", dir))
{
fprintf(stderr, "%s: dynstr_append failed\n", __func__);
goto failure;
}
else if (dynstr_append(&a->db, "%s/db.json", dir))
{
fprintf(stderr, "%s: dynstr_append failed\n", __func__);
goto failure;
}
else if (init_db(a))
{
fprintf(stderr, "%s: init_db failed\n", __func__);
goto failure;
}
return a;
failure:
auth_free(a);
return NULL;
}
|