-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathotpdb.c
476 lines (400 loc) · 9.83 KB
/
otpdb.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
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
/*
* Copyright (c) 2018 Reyk Floeter <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <time.h>
#include <getopt.h>
#include <err.h>
#include "common.h"
#include "base32.h"
static int oathdb_sync(struct oathdb *);
struct oathdb *
oathdb_open(int read_only)
{
FILE *fp;
struct oathdb *db = NULL;
int fd;
char *line = NULL;
size_t linesz = 0;
ssize_t linelen;
if (read_only) {
if ((fp = fopen(OATH_DB_PATH, "r")) == NULL) {
/* DB does not exist, oath is disabled */
warn("%s", OATH_DB_PATH);
return (NULL);
}
} else {
if ((fp = fopen(OATH_DB_PATH, "r+")) == NULL) {
/* DB does not exist, oath is disabled */
warn("%s", OATH_DB_PATH);
return (NULL);
}
if (flock(fileno(fp), LOCK_EX|LOCK_NB) == -1) {
warn("flock %s", OATH_DB_PATH);
fclose(fp);
return (NULL);
}
}
if ((db = calloc(1, sizeof(*db))) == NULL) {
warn("calloc");
return (NULL);
}
db->db_read_only = read_only;
db->db_fp = fp;
if (!read_only) {
strlcpy(db->db_tmp, "/tmp/otp-XXXXXXXXXX",
sizeof(db->db_tmp));
if ((fd = mkstemp(db->db_tmp)) == -1) {
warn("mkstemp");
return (NULL);
}
if ((db->db_tmpfp = fdopen(fd, "r+")) == NULL) {
warn("fdopen");
return (NULL);
}
while ((linelen = getline(&line, &linesz, fp)) != -1) {
/* normalize and validate line */
line[strcspn(line, "\r\n")] = '\0';
/* write it to the temporary file */
fprintf(db->db_tmpfp, "%s\n", line);
}
if (line != NULL)
explicit_bzero(line, strlen(line));
free(line);
}
return (db);
}
int
oathdb_sync(struct oathdb *db)
{
char *line = NULL;
size_t linesz = 0;
ssize_t linelen;
if (db->db_read_only) {
warn("file opened read-only %s", OATH_DB_PATH);
return (-1);
}
if (fseek(db->db_fp, 0, SEEK_SET) == -1) {
warn("could not rewind %s", OATH_DB_PATH);
return (-1);
}
if (fseek(db->db_tmpfp, 0, SEEK_SET) == -1) {
warn("could not rewind %s", db->db_tmp);
return (-1);
}
if (ftruncate(fileno(db->db_fp), 0) == -1) {
warn("could not truncate %s", OATH_DB_PATH);
}
while ((linelen = getline(&line, &linesz, db->db_tmpfp))!= -1) {
if (fputs(line, db->db_fp) == EOF) {
warn("could not write to %s", OATH_DB_PATH);
free(line);
return (-1);
}
}
if (line != NULL)
explicit_bzero(line, strlen(line));
free(line);
return (0);
}
int
oathdb_close(struct oathdb *db)
{
int ret = 0;
if (db == NULL)
return (0);
if (!db->db_read_only)
ret = oathdb_sync(db);
if (db->db_fp != NULL)
fclose(db->db_fp);
if (db->db_tmpfp != NULL)
fclose(db->db_tmpfp);
if (strncmp("/tmp/otp", db->db_tmp, strlen("/tmp/otp")) == 0)
unlink(db->db_tmp);
free(db);
return (ret);
}
char *
oathdb_nextval(char **line)
{
char *p, *val;
if ((p = strchr(*line, ':')) == NULL)
return (NULL);
*p++ = '\0';
val = *line;
*line = p;
return (val);
}
struct oath_key *
oathdb_parsekey(const char *name, char *line)
{
struct oath_key *oak = NULL;
const char *errstr;
char *val = line;
time_t created;
unsigned int type;
unsigned int hash;
uint64_t counter;
uint8_t digits;
time_t margin;
char *key;
/*
* The format is (name: is already split out):
* name:created:type:hash:digits:counter:margin:key
* or for TOTP defaults
* name:::::::key
*/
/* created */
if ((val = oathdb_nextval(&line)) == NULL)
return (NULL);
if (*val == ':' || *val == '0')
created = 0;
else {
created = strtonum(val, 0, INT64_MAX, &errstr);
if (errstr != NULL)
return (NULL);
}
/* type */
if ((val = oathdb_nextval(&line)) == NULL)
return (NULL);
if (*val == ':' || *val == '0')
type = 0;
else {
type = strtonum(val, 0, OATH_TYPE_MAX, &errstr);
if (errstr != NULL)
return (NULL);
}
/* hash */
if ((val = oathdb_nextval(&line)) == NULL)
return (NULL);
if (*val == ':' || *val == '0')
hash = OATH_HASH;
else {
hash = strtonum(val, 0, OATH_HASH_MAX, &errstr);
if (errstr != NULL)
return (NULL);
}
/* digits */
if ((val = oathdb_nextval(&line)) == NULL)
return (NULL);
if (*val == ':' || *val == '0')
digits = OATH_DIGITS;
else {
digits = strtonum(val, 1, OATH_DIGITS_MAX, &errstr);
if (errstr != NULL)
return (NULL);
}
/* counter (or t0 in TOTP mode) */
if ((val = oathdb_nextval(&line)) == NULL)
return (NULL);
if (*val == ':' || *val == '0')
counter = OATH_HOTP_COUNTER;
else {
/* XXX strtonum's maximum is signed long long */
counter = strtonum(val, 0, LLONG_MAX, &errstr);
if (errstr != NULL)
return (NULL);
}
/* margin */
if ((val = oathdb_nextval(&line)) == NULL)
return (NULL);
if (*val == ':' || *val == '0')
margin = OATH_TOTP_MARGIN;
else {
margin = strtonum(val, 0, INT64_MAX, &errstr);
if (errstr != NULL)
return (NULL);
}
/* base32 key */
key = line;
if ((oak = calloc(1, sizeof(*oak))) == NULL ||
(oak->oak_name = strdup(name)) == NULL ||
(oak->oak_key = strdup(key)) == NULL) {
oath_freekey(oak);
return (NULL);
}
oak->oak_created = created;
oak->oak_type = type;
oak->oak_hash = hash;
oak->oak_digits = digits;
oak->oak_counter = counter;
oak->oak_margin = margin;
return (oak);
}
struct oath_key *
oathdb_getkey(struct oathdb *db, const char *name)
{
struct oath_key *oak = NULL;
char *line = NULL, *entry, *val;
size_t linesz = 0;
ssize_t linelen;
FILE *fp;
/* either read from the writeable copy or from the file directly */
fp = db->db_read_only ? db->db_fp : db->db_tmpfp;
/* start from the beginning */
if (fseek(fp, 0, SEEK_SET) == -1) {
warn("could not rewind %s", OATH_DB_PATH);
return (NULL);
}
while ((linelen = getline(&line, &linesz, fp))!= -1) {
line[strcspn(line, "\r\n")] = '\0';
entry = line;
/* find key */
if ((val = oathdb_nextval(&entry)) == NULL) {
warnx("invalid line in %s", OATH_DB_PATH);
continue;
}
if (strcmp(name, val) != 0)
continue;
if ((oak = oathdb_parsekey(name, entry)) == NULL) {
warnx("failed to parse line in %s", OATH_DB_PATH);
break;
}
}
if (line != NULL)
explicit_bzero(line, strlen(line));
free(line);
return (oak);
}
int
oathdb_setkey(struct oathdb *db, struct oath_key *oak)
{
char *keyline = NULL;
char *line = NULL, *entry, *val, *line2 = NULL;
size_t linesz = 0;
ssize_t linelen;
int ret = -1, found = 0;
if (db->db_read_only) {
warn("file opened read-only %s", OATH_DB_PATH);
return (-1);
}
if (oathdb_sync(db) != 0)
return (-1);
/* start from the beginning */
if (fseek(db->db_fp, 0, SEEK_SET) == -1) {
warn("could not rewind %s", OATH_DB_PATH);
goto done;
}
if (fseek(db->db_tmpfp, 0, SEEK_SET) == -1) {
warn("could not rewind %s", db->db_tmp);
return (-1);
}
if (ftruncate(fileno(db->db_tmpfp), 0) == -1) {
warn("could not truncate %s", OATH_DB_PATH);
}
if (oak->oak_key != NULL) {
/* Fill in default values */
if (oak->oak_hash == OATH_HASH_DEFAULT)
oak->oak_hash = OATH_HASH;
if (oak->oak_type == OATH_TYPE_TOTP &&
oak->oak_margin == 0)
oak->oak_margin = OATH_TOTP_MARGIN;
if (oak->oak_created == 0)
oak->oak_created = time(NULL);
if (oak->oak_digits == 0)
oak->oak_digits = OATH_DIGITS;
/* create new line */
if (asprintf(&keyline,
"%s:%lld:%u:%u:%u:%lld:%lld:%s",
oak->oak_name,
oak->oak_created,
oak->oak_type,
oak->oak_hash,
oak->oak_digits,
oak->oak_counter,
oak->oak_margin,
oak->oak_key) == -1)
goto done;
}
while ((linelen = getline(&line, &linesz, db->db_fp))!= -1) {
line[strcspn(line, "\r\n")] = '\0';
if ((entry = line2 = strdup(line)) == NULL) {
warn("strdup");
continue;
}
/* find key */
if ((val = oathdb_nextval(&entry)) == NULL) {
warnx("invalid line in %s", OATH_DB_PATH);
free(line2);
continue;
}
if (strcmp(oak->oak_name, val) != 0) {
free(line2);
if (fprintf(db->db_tmpfp, "%s\n", line) == EOF) {
warn("could not write to %s", OATH_DB_PATH);
goto done;
}
continue;
}
explicit_bzero(line2, strlen(line2));
free(line2);
/* remove duplicate or line if no new key was given */
if (found++ || keyline == NULL) {
continue;
}
if (fprintf(db->db_tmpfp, "%s\n", keyline) == EOF) {
warn("could not write update to %s", OATH_DB_PATH);
goto done;
}
}
/* add line if it is a new entry */
if (keyline != NULL && !found &&
fprintf(db->db_tmpfp, "%s\n", keyline) == EOF) {
warn("could not write update to %s", OATH_DB_PATH);
goto done;
}
ret = 0;
done:
if (line != NULL)
explicit_bzero(line, strlen(line));
if (keyline != NULL)
explicit_bzero(keyline, strlen(keyline));
free(line);
free(keyline);
return (ret);
}
int
oathdb_delkey(struct oathdb *db, char *name)
{
struct oath_key oak;
/* An empty key will cause setkey to delete the entry */
oak.oak_name = name;
oak.oak_key = NULL;
return (oathdb_setkey(db, &oak));
}
int
oathdb_putkey(struct oathdb *db, char *line)
{
struct oath_key *oak = NULL;
char *name;
int ret;
if ((name = oathdb_nextval(&line)) == NULL) {
warnx("invalid name");
return (-1);
}
if ((oak = oathdb_parsekey(name, line)) == NULL) {
warnx("invalid line");
return (-1);
}
ret = oathdb_setkey(db, oak);
oath_freekey(oak);
return (ret);
}