-
Notifications
You must be signed in to change notification settings - Fork 291
/
config.c
457 lines (386 loc) · 11.7 KB
/
config.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
/*
* configuration file handling
*
* Copyright (C) 2014-2018 LastPass.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
*
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*
* See LICENSE.OpenSSL for more details regarding this exception.
*/
#include "config.h"
#include "util.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <utime.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/hmac.h>
/*
* Map well-known pathnames to their configuration type.
*/
struct pathname_type_tuple {
char *name;
enum config_type type;
};
struct pathname_type_tuple pathname_type_lookup[] = {
{ "env", CONFIG_CONFIG },
{ "blob", CONFIG_DATA },
{ "iterations", CONFIG_DATA },
{ "username", CONFIG_DATA },
{ "verify", CONFIG_DATA },
{ "plaintext_key", CONFIG_DATA },
{ "trusted_id", CONFIG_DATA },
{ "session_uid", CONFIG_DATA },
{ "session_sessionid", CONFIG_DATA },
{ "session_token", CONFIG_DATA },
{ "session_privatekey", CONFIG_DATA },
{ "session_server", CONFIG_DATA },
{ "lpass.log", CONFIG_DATA },
{ "agent.sock", CONFIG_RUNTIME },
{ "uploader.pid", CONFIG_RUNTIME },
};
char *config_type_to_xdg[] = {
[CONFIG_DATA] = "XDG_DATA_HOME",
[CONFIG_CONFIG] = "XDG_CONFIG_HOME",
[CONFIG_RUNTIME] = "XDG_RUNTIME_DIR",
};
static
char *get_xdg_dir(const char *xdg_var)
{
char *home;
char *retstr = NULL;
if (getenv(xdg_var))
return xstrdup(getenv(xdg_var));
/*
* $XDG var not set in environment; decide whether
* to use backups locations based on existence of
* $XDG_RUNTIME_DIR.
*/
if (!getenv("XDG_RUNTIME_DIR"))
return NULL;
home = getenv("HOME");
if (!home)
return NULL;
if (!strcmp(xdg_var, "XDG_DATA_HOME"))
xasprintf(&retstr, "%s/.local/share", home);
else if (!strcmp(xdg_var, "XDG_CONFIG_HOME"))
xasprintf(&retstr, "%s/.config", home);
return retstr;
}
/*
* Get the path to a config file given its name and the type of file.
*
* lpass looks for files in the following directories:
*
* First, if $LPASS_HOME is set, everything goes there.
*
* After that, if it is a persistent, user-specific data file,
* it goes in $XDG_DATA_HOME/lpass.
*
* If a configuration item, it goes in $XDG_CONFIG_HOME.
*
* If a purely runtime item (socket, pidfile, etc) it goes in
* $XDG_RUNTIME_HOME.
*
* If none of the $XDG environment variables are set, fall-back
* to ~/.lpass.
*/
static
char *config_path_for_type(enum config_type type, const char *name)
{
char *home, *path, *xdg_env;
_cleanup_free_ char *config = NULL;
_cleanup_free_ char *xdg_dir = NULL;
struct stat sbuf;
int ret;
xdg_env = config_type_to_xdg[type];
home = getenv("LPASS_HOME");
if (home)
config = xstrdup(home);
else if ((xdg_dir = get_xdg_dir(xdg_env))) {
xasprintf(&config, "%s/lpass", xdg_dir);
} else {
home = getenv("HOME");
if (!home)
die("HOME is not set");
xasprintf(&config, "%s/.lpass", home);
}
ret = stat(config, &sbuf);
if ((ret == -1 && errno == ENOENT) || !S_ISDIR(sbuf.st_mode)) {
unlink(config);
if (mkdir(config, 0700) < 0)
die_errno("mkdir(%s)", config);
} else if (ret == -1)
die_errno("stat(%s)", config);
_cleanup_free_ char *buffer = xstrdup(name);
_cleanup_free_ char *dir_path = xstrdup(config);
char *saveptr = NULL;
for (char *token = strtok_r(buffer, "/", &saveptr); token && saveptr && strlen(saveptr) > 0; token = strtok_r(NULL, "/", &saveptr)) {
xstrappendf(&dir_path, "/%s", token);
ret = stat(dir_path, &sbuf);
if ((ret == -1 && errno == ENOENT) || !S_ISDIR(sbuf.st_mode)) {
unlink(dir_path);
if (mkdir(dir_path, 0700) < 0)
die_errno("mkdir(%s)", dir_path);
} else if (ret == -1)
die_errno("stat(%s)", dir_path);
}
xasprintf(&path, "%s/%s", config, name);
return path;
}
enum config_type config_path_type(const char *name)
{
unsigned int i;
/* aliases are config files */
if (!strncmp(name, "alias", 5)) {
return CONFIG_CONFIG;
}
/* lock files are runtime */
if (strlen(name) >= 5 && !strcmp(name + strlen(name) - 5, ".lock")) {
return CONFIG_RUNTIME;
}
/* categorized this configuration file by name? */
for (i=0; i < ARRAY_SIZE(pathname_type_lookup); i++) {
if (!strcmp(name, pathname_type_lookup[i].name)) {
return pathname_type_lookup[i].type;
}
}
/* everything else is config_data */
return CONFIG_DATA;
}
char *config_path(const char *name)
{
return config_path_for_type(config_path_type(name), name);
}
FILE *config_fopen(const char *name, const char *mode)
{
_cleanup_free_ char *path = config_path(name);
return fopen(path, mode);
}
void config_touch(const char *name)
{
_cleanup_free_ char *path = NULL;
path = config_path(name);
if (utime(path, NULL) < 0)
die_errno("utime");
}
bool config_exists(const char *name)
{
_cleanup_free_ char *path = NULL;
struct stat sbuf;
path = config_path(name);
return stat(path, &sbuf) != -1;
}
time_t config_mtime(const char *name)
{
_cleanup_free_ char *path = NULL;
struct stat sbuf;
path = config_path(name);
if (stat(path, &sbuf) < 0)
return 0;
return sbuf.st_mtime;
}
bool config_unlink(const char *name)
{
_cleanup_free_ char *path = config_path(name);
return unlink(path) == 0;
}
void config_write_string(const char *name, const char *string)
{
config_write_buffer(name, string, strlen(string));
}
void config_write_buffer(const char *name, const char *buffer, size_t len)
{
_cleanup_free_ char *tempname = NULL;
_cleanup_free_ char *finalpath = config_path(name);
int tempfd;
FILE *tempfile = NULL;
xasprintf(&tempname, "%s.XXXXXX", finalpath);
tempfd = mkstemp(tempname);
if (tempfd < 0)
die_errno("mkstemp(%s)", tempname);
tempfile = fdopen(tempfd, "w");
if (!tempfile)
goto error;
if (fwrite(buffer, 1, len, tempfile) != len)
goto error;
fclose(tempfile);
tempfile = NULL;
if (rename(tempname, finalpath) < 0)
goto error;
return;
error:
tempfd = errno;
if (tempfile)
fclose(tempfile);
unlink(tempname);
errno = tempfd;
die_errno("config-%s", name);
}
char *config_read_string(const char *name)
{
_cleanup_free_ char *buffer = NULL;
size_t len = config_read_buffer(name, (unsigned char **) &buffer);
if (!buffer)
return NULL;
return xstrndup(buffer, len);
}
size_t config_read_buffer(const char *name, unsigned char **out)
{
_cleanup_fclose_ FILE *file = NULL;
unsigned char *buffer;
size_t len, read;
file = config_fopen(name, "r");
if (!file) {
*out = NULL;
return 0;
}
for (len = 0, buffer = xmalloc(8192); ; buffer = xrealloc(buffer, len + 8192)) {
read = fread(buffer + len, 1, 8192, file);
len += read;
if (read != 8192) {
if (ferror(file))
die_errno("fread(config-%s)", name);
break;
}
}
*out = buffer;
return len;
}
/*
* ciphertext = IV | aes-256-cbc(plaintext, key)
* authenticated-ciphertext = HMAC-SHA256(ciphertext, key) | ciphertext
*
* These two functions work with `authenticated-ciphertext`.
*/
static size_t encrypt_buffer(const char *buffer, size_t in_len, unsigned const char key[KDF_HASH_LEN], char **out)
{
EVP_CIPHER_CTX *ctx;
char *ciphertext;
unsigned char iv[AES_BLOCK_SIZE];
int out_len;
unsigned int hmac_len;
size_t len;
if (!RAND_bytes(iv, AES_BLOCK_SIZE))
die("Could not generate random bytes for CBC IV.");
ciphertext = xcalloc(in_len + AES_BLOCK_SIZE * 2 + SHA256_DIGEST_LENGTH, 1);
ctx = EVP_CIPHER_CTX_new();
if (!ctx)
goto error;
len = SHA256_DIGEST_LENGTH;
memcpy(ciphertext + len, iv, AES_BLOCK_SIZE);
len += AES_BLOCK_SIZE;
if (!EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
goto error;
if (!EVP_EncryptUpdate(ctx, (unsigned char *)(ciphertext + len), &out_len, (unsigned char *)buffer, in_len))
goto error;
len += out_len;
if (!EVP_EncryptFinal_ex(ctx, (unsigned char *)(ciphertext + len), &out_len))
goto error;
len += out_len;
if (!HMAC(EVP_sha256(), key, KDF_HASH_LEN, (unsigned char *)(ciphertext + SHA256_DIGEST_LENGTH), len - SHA256_DIGEST_LENGTH, (unsigned char *)ciphertext, &hmac_len))
goto error;
EVP_CIPHER_CTX_free(ctx);
*out = ciphertext;
return len;
error:
EVP_CIPHER_CTX_free(ctx);
free(ciphertext);
die("Failed to encrypt data.");
}
static size_t decrypt_buffer(const unsigned char *buffer, size_t in_len, unsigned const char key[KDF_HASH_LEN], unsigned char **out)
{
EVP_CIPHER_CTX *ctx;
unsigned char *plaintext = NULL;
int out_len;
unsigned int hmac_len;
size_t len;
unsigned char hmac[SHA256_DIGEST_LENGTH];
ctx = EVP_CIPHER_CTX_new();
if (!ctx)
goto error;
if (in_len < (SHA256_DIGEST_LENGTH + AES_BLOCK_SIZE * 2))
goto error;
if (!HMAC(EVP_sha256(), key, KDF_HASH_LEN, (unsigned char *)(buffer + SHA256_DIGEST_LENGTH), in_len - SHA256_DIGEST_LENGTH, hmac, &hmac_len))
goto error;
if (CRYPTO_memcmp(hmac, buffer, SHA256_DIGEST_LENGTH))
goto error;
plaintext = xcalloc(in_len + AES_BLOCK_SIZE, 1);
if (!EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, (unsigned char *)(buffer + SHA256_DIGEST_LENGTH)))
goto error;
if (!EVP_DecryptUpdate(ctx, (unsigned char *)plaintext, &out_len, (unsigned char *)(buffer + SHA256_DIGEST_LENGTH + AES_BLOCK_SIZE), in_len - SHA256_DIGEST_LENGTH - AES_BLOCK_SIZE))
goto error;
len = out_len;
if (!EVP_DecryptFinal_ex(ctx, (unsigned char *)(plaintext + out_len), &out_len))
goto error;
len += out_len;
EVP_CIPHER_CTX_free(ctx);
*out = plaintext;
return len;
error:
EVP_CIPHER_CTX_free(ctx);
free(plaintext);
*out = NULL;
return 0;
}
void config_write_encrypted_string(const char *name, const char *string, unsigned const char key[KDF_HASH_LEN])
{
config_write_encrypted_buffer(name, string, strlen(string), key);
}
void config_write_encrypted_buffer(const char *name, const char *buffer, size_t len, unsigned const char key[KDF_HASH_LEN])
{
_cleanup_free_ char *encrypted_buffer = NULL;
len = encrypt_buffer(buffer, len, key, &encrypted_buffer);
config_write_buffer(name, encrypted_buffer, len);
}
char *config_read_encrypted_string(const char *name, unsigned const char key[KDF_HASH_LEN])
{
_cleanup_free_ char *buffer = NULL;
size_t len = config_read_encrypted_buffer(name, (unsigned char **) &buffer, key);
if (!buffer)
return NULL;
return xstrndup(buffer, len);
}
size_t config_read_encrypted_buffer(const char *name, unsigned char **buffer, unsigned const char key[KDF_HASH_LEN])
{
_cleanup_free_ unsigned char *encrypted_buffer = NULL;
size_t len;
len = config_read_buffer(name, &encrypted_buffer);
if (!encrypted_buffer) {
*buffer = NULL;
return 0;
}
return decrypt_buffer(encrypted_buffer, len, key, buffer);
}