forked from lastpass/lastpass-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.c
655 lines (575 loc) · 16.6 KB
/
edit.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
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
643
644
645
646
647
648
649
650
651
652
653
654
655
/*
* common routines for editing / adding accounts
*
* 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 "cmd.h"
#include "endpoints.h"
#include "blob.h"
#include "agent.h"
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "util.h"
#define MAX_NOTE_LEN (unsigned long) 45000
#if defined(__linux__) || defined(__CYGWIN__)
static char *shared_memory_dir(void)
{
return xstrdup("/dev/shm");
}
#elif defined(__APPLE__) && defined(__MACH__)
static const char *shared_memory_dir_mount =
"dir=\"$(mktemp -d \"${TMPDIR:-/tmp}/lpass.XXXXXXXXXXXXX\")\"\n"
"dev=\"$(hdid -drivekey system-image=yes -nomount 'ram://32768' | cut -d ' ' -f 1)\"\n"
"[[ -z $dev ]] && exit 1\n"
"newfs_hfs -M 700 \"$dev\" >/dev/null 2>&1 || exit 1\n"
"mount -t hfs -o noatime -o nobrowse \"$dev\" \"$dir\" || exit 1\n"
"echo \"$dev\"\necho \"$dir\"\n";
static const char *shared_memory_dir_unmount =
"umount \"$SECURE_TMPDIR\"\n"
"diskutil quiet eject \"$RAMDISK_DEV\"\n"
"rm -rf \"$SECURE_TMPDIR\"\n";
static void shared_memory_dir_eject(void)
{
system(shared_memory_dir_unmount);
}
static char *shared_memory_dir(void)
{
char *stored = getenv("SECURE_TMPDIR");
if (stored)
return xstrdup(stored);
_cleanup_free_ char *dev = NULL;
char *dir = NULL;
size_t len;
FILE *script = popen(shared_memory_dir_mount, "r");
if (!script)
return NULL;
len = 0;
if (getline(&dev, &len, script) <= 0) {
pclose(script);
return NULL;
}
trim(dev);
len = 0;
if (getline(&dir, &len, script) <= 0) {
pclose(script);
return NULL;
}
trim(dir);
setenv("SECURE_TMPDIR", dir, true);
setenv("RAMDISK_DEV", dev, true);
atexit(shared_memory_dir_eject);
return dir;
}
#else
static char *shared_memory_dir(void)
{
char *tmpdir = getenv("SECURE_TMPDIR");
if (!tmpdir) {
if (!(tmpdir = getenv("TMPDIR")))
tmpdir = "/tmp";
fprintf(stderr,
"Warning: Using %s as secure temporary directory.\n"
"Recommend using tmpfs and encrypted swap.\n"
"Set SECURE_TMPDIR environment variable to override.\n",
tmpdir);
sleep(5);
}
return xstrdup(tmpdir);
}
#endif
_noreturn_ static inline void die_unlink_errno(const char *str, const char *file, const char *dir)
{
int saved = errno;
if (file)
unlink(file);
if (dir)
rmdir(dir);
errno = saved;
die_errno("%s", str);
}
static void assign_account_value(struct account *account,
const char *label,
char *value,
int lineno,
unsigned char key[KDF_HASH_LEN])
{
struct field *editable_field = NULL;
#define assign_if(title, field) do { \
if (!strcmp(label, title)) { \
account_set_##field(account, xstrdup(trim(value)), key); \
return; \
} \
} while (0)
/*
* "Name" may be used in note templates; only assign fullname
* in the first line.
*/
if (lineno == 1) {
assign_if("Name", fullname);
}
assign_if("URL", url);
assign_if("Username", username);
assign_if("Password", password);
assign_if("Application", appname);
assign_if("Notes", note);
if (!strcmp(label, "Reprompt")) {
account->pwprotect = !strcmp(trim(value), "Yes");
return;
}
/* if we got here maybe it's a secure note field */
list_for_each_entry(editable_field, &account->field_head, list) {
if (!strcmp(label, editable_field->name)) {
field_set_value(account, editable_field,
xstrdup(trim(value)), key);
return;
}
}
/* Some other name: value pair -- treat like a new field */
editable_field = new0(struct field, 1);
editable_field->name = xstrdup(label);
editable_field->type = xstrdup("password");
field_set_value(account, editable_field, xstrdup(trim(value)), key);
list_add_tail(&editable_field->list, &account->field_head);
#undef assign_if
}
static
int read_file_buf(FILE *fp, char **value_out, size_t *len_out)
{
size_t len;
size_t read;
char *value;
*len_out = 0;
*value_out = NULL;
for (len = 0, value = xmalloc(8192 + 1); ; value = xrealloc(value, len + 8192 + 1)) {
read = fread(value + len, 1, 8192, fp);
len += read;
if (read != 8192) {
if (ferror(fp))
return -EIO;
break;
}
}
value[len] = '\0';
*value_out = value;
*len_out = len;
return 0;
}
enum note_type get_note_type(struct account *account)
{
struct field *editable_field;
list_for_each_entry(editable_field, &account->field_head, list) {
if (!strcmp(editable_field->name, "NoteType")) {
return notes_get_type_by_name(editable_field->value);
}
}
return NOTE_TYPE_NONE;
}
struct parsed_name_value
{
char *name;
char *value;
int lineno;
struct list_head list;
};
/*
* Read a file representing all of the data in an account.
* We generate this file when editing an account, and parse it back
* after a user has edited it.
*
* Multiline values are accepted (though they may not be supported by
* lastpass in all cases).
*
* Once the "Notes:" label is encountered, everything else is concatenated
* into the note.
*
* Name: text0
* URL: text1
* [...]
* Notes:
* notes text here
*/
static void parse_account_file(FILE *input, enum note_type note_type,
struct list_head *list_head)
{
_cleanup_free_ char *line = NULL;
ssize_t read;
size_t len = 0;
char *name, *delim, *value = NULL;
bool parsing_notes = false;
int ret;
int lineno = 0;
struct parsed_name_value *current = NULL;
/* parse label: [value] */
while ((read = getline(&line, &len, input)) != -1) {
lineno++;
line = trim(line);
delim = strchr(line, ':');
if (!delim) {
/* non keyed strings go to existing field (if any) */
if (current)
xstrappendf(¤t->value, "\n%s", line);
continue;
}
name = xstrndup(line, delim - line);
value = xstrdup(delim + 1);
/*
* If this is a known notetype, append any non-existent
* keys to the existing field. For example, Proc-Type
* in the ssh private key field goes into private key,
* not a Proc-Type field.
*/
if (note_type != NOTE_TYPE_NONE &&
!note_has_field(note_type, name) && current &&
note_field_is_multiline(note_type, current->name)) {
xstrappendf(¤t->value, "\n%s", line);
free(name);
free(value);
continue;
}
if (!strcmp(name, "Notes")) {
parsing_notes = true;
free(name);
free(value);
break;
}
current = new0(struct parsed_name_value, 1);
current->name = name;
current->value = value;
current->lineno = lineno;
list_add_tail(¤t->list, list_head);
}
if (!parsing_notes)
return;
/* everything else goes into notes section */
value = NULL;
len = 0;
ret = read_file_buf(input, &value, &len);
if (ret)
return;
if (len > MAX_NOTE_LEN) {
die("Maximum note length is %lu bytes (was %lu)",
MAX_NOTE_LEN, len);
}
current = new0(struct parsed_name_value, 1);
current->name = xstrdup("Notes");
current->value = value;
current->lineno = lineno;
list_add_tail(¤t->list, list_head);
}
static void read_account_file(FILE *input, struct account *account,
unsigned char key[KDF_HASH_LEN])
{
LIST_HEAD(fields);
struct parsed_name_value *entry, *tmp;
parse_account_file(input, get_note_type(account), &fields);
list_for_each_entry_safe(entry, tmp, &fields, list) {
assign_account_value(account, entry->name, entry->value,
entry->lineno, key);
free(entry->name);
free(entry->value);
list_del(&entry->list);
free(entry);
}
}
static
struct field *add_default_field(struct account *account,
const char *field_name,
unsigned char key[KDF_HASH_LEN])
{
struct field *editable_field = NULL;
bool found = false;
list_for_each_entry(editable_field, &account->field_head, list) {
if (!strcmp(editable_field->name, field_name)) {
found = true;
break;
}
}
if (found)
return editable_field;
editable_field = new0(struct field, 1);
editable_field->type = xstrdup("text");
editable_field->name = xstrdup(field_name);
field_set_value(account, editable_field, xstrdup(""), key);
list_add_tail(&editable_field->list, &account->field_head);
return editable_field;
}
static void add_default_fields(struct account *account,
enum note_type note_type,
unsigned char key[KDF_HASH_LEN])
{
int i;
struct note_template *tmpl;
if (note_type <= NOTE_TYPE_NONE || note_type >= NUM_NOTE_TYPES)
return;
/*
* Add a new, empty field for any label in the template which
* does not already exist in the account.
*/
tmpl = ¬e_templates[note_type];
for (i=0; tmpl->fields[i]; i++) {
/*
* ... but skip these: they are already handled by the
* collapse code.
*/
if (!strcmp(tmpl->fields[i], "Username"))
continue;
if (!strcmp(tmpl->fields[i], "Password"))
continue;
add_default_field(account, tmpl->fields[i], key);
}
}
static int write_account_file(FILE *fp, struct account *account,
unsigned char key[KDF_HASH_LEN])
{
struct field *editable_field = NULL;
enum note_type note_type;
#define write_field(title, field) do { \
if (fprintf(fp, "%s: %s\n", title, field) < 0) \
return -errno; \
} while (0)
write_field("Name", account->fullname);
note_type = get_note_type(account);
if (account->is_app) {
struct app *app = account_to_app(account);
write_field("Application", app->appname);
} else if (note_type != NOTE_TYPE_NONE) {
add_default_fields(account, note_type, key);
} else {
write_field("URL", account->url);
write_field("Username", account->username);
write_field("Password", account->password);
}
list_for_each_entry(editable_field, &account->field_head, list) {
write_field(editable_field->name, editable_field->value);
}
if (account->pwprotect) {
write_field("Reprompt", "Yes");
}
if (fprintf(fp, "Notes: # Add notes below this line.\n%s", account->note) < 0)
return -errno;
return 0;
#undef write_field
}
int edit_account(struct session *session,
struct blob *blob,
enum blobsync sync,
struct account *editable,
enum edit_choice choice,
const char *field,
bool non_interactive,
unsigned char key[KDF_HASH_LEN])
{
size_t len;
struct account *notes_expansion, *notes_collapsed = NULL;
struct field *editable_field = NULL;
_cleanup_free_ char *tmppath = NULL;
_cleanup_free_ char *tmpdir = NULL;
_cleanup_free_ char *editcmd = NULL;
int tmpfd;
FILE *tmpfile;
char *value;
int ret;
struct share *old_share = editable->share;
notes_expansion = notes_expand(editable);
if (notes_expansion) {
notes_collapsed = editable;
editable = notes_expansion;
} else if (choice == EDIT_FIELD)
die("Editing fields of entries that are not secure notes is currently not supported.");
switch(choice)
{
case EDIT_USERNAME:
value = editable->username;
break;
case EDIT_PASSWORD:
value = editable->password;
break;
case EDIT_URL:
value = editable->url;
break;
case EDIT_NAME:
value = editable->fullname;
break;
case EDIT_FIELD:
editable_field = add_default_field(editable, field, key);
value = editable_field->value;
break;
case EDIT_NOTES:
value = editable->note;
break;
default:
value = NULL;
}
if (!non_interactive) {
if (editable->pwprotect) {
unsigned char pwprotect_key[KDF_HASH_LEN];
if (!agent_load_key(pwprotect_key))
die("Could not authenticate for protected entry.");
if (memcmp(pwprotect_key, key, KDF_HASH_LEN))
die("Current key is not on-disk key.");
}
if (strcmp(editable->id, "0"))
lastpass_log_access(sync, session, key, editable);
tmpdir = shared_memory_dir();
xstrappend(&tmpdir, "/lpass.XXXXXX");
if (!mkdtemp(tmpdir))
die_errno("mkdtemp");
xasprintf(&tmppath, "%s/lpass.XXXXXX", tmpdir);
tmpfd = mkstemp(tmppath);
if (tmpfd < 0)
die_unlink_errno("mkstemp", tmppath, tmpdir);
tmpfile = fdopen(tmpfd, "w");
if (!tmpfile)
die_unlink_errno("fdopen", tmppath, tmpdir);
if (choice == EDIT_ANY) {
if (write_account_file(tmpfile, editable, key))
die_unlink_errno("fprintf", tmppath, tmpdir);
} else {
if (fprintf(tmpfile, "%s\n", value) < 0)
die_unlink_errno("fprintf", tmppath, tmpdir);
}
fclose(tmpfile);
xasprintf(&editcmd, "${VISUAL:-${EDITOR:-vi}} '%s'", tmppath);
if (system(editcmd) < 0)
die_unlink_errno("system($VISUAL)", tmppath, tmpdir);
tmpfile = fopen(tmppath, "r");
} else
tmpfile = stdin;
if (!tmpfile)
die_unlink_errno("fopen", tmppath, tmpdir);
if (choice == EDIT_NOTES) {
ret = read_file_buf(tmpfile, &value, &len);
if (ret)
die_unlink_errno("fread(tmpfile)", tmppath, tmpdir);
} else if (choice == EDIT_ANY) {
read_account_file(tmpfile, editable, key);
value = NULL;
} else {
ret = read_file_buf(tmpfile, &value, &len);
if (ret)
die_unlink_errno("fread(tmpfile)", tmppath, tmpdir);
}
fclose(tmpfile);
if (value) {
len = strlen(value);
if (len && value[len - 1] == '\n')
value[len - 1] = '\0';
}
if (tmppath) {
unlink(tmppath);
rmdir(tmpdir);
}
if (choice == EDIT_USERNAME)
account_set_username(editable, value, key);
else if (choice == EDIT_PASSWORD)
account_set_password(editable, value, key);
else if (choice == EDIT_URL)
account_set_url(editable, value, key);
else if (choice == EDIT_NAME)
account_set_fullname(editable, value, key);
else if (choice == EDIT_NOTES)
account_set_note(editable, value, key);
else if (choice == EDIT_FIELD) {
if (!value || !strlen(value)) {
list_del(&editable_field->list);
field_free(editable_field);
} else
field_set_value(editable, editable_field, value, key);
}
if (notes_expansion && notes_collapsed) {
editable = notes_collapsed;
notes_collapsed = notes_collapse(notes_expansion);
account_free(notes_expansion);
account_set_note(editable, xstrdup(notes_collapsed->note), key);
account_set_fullname(editable, xstrdup(notes_collapsed->fullname), key);
editable->pwprotect = notes_collapsed->pwprotect;
account_free(notes_collapsed);
}
account_assign_share(blob, editable, key);
if (old_share != editable->share) {
die("Use lpass mv to move items to/from shared folders");
}
lastpass_update_account(sync, key, session, editable, blob);
blob_save(blob, key);
session_free(session);
blob_free(blob);
return 0;
}
int edit_new_account(struct session *session,
struct blob *blob,
enum blobsync sync,
const char *name,
enum edit_choice choice,
const char *field,
bool non_interactive,
bool is_app,
enum note_type note_type,
unsigned char key[KDF_HASH_LEN])
{
struct app *app;
struct account *account;
if (note_type != NOTE_TYPE_NONE &&
choice != EDIT_NOTES && choice != EDIT_ANY) {
die("Note type may only be used with secure notes");
}
if (is_app) {
app = new_app();
account = &app->account;
} else {
account = new_account();
}
account->id = xstrdup("0");
account->attachkey = xstrdup("");
account->attachkey_encrypted = xstrdup("");
account_set_password(account, xstrdup(""), key);
account_set_fullname(account, xstrdup(name), key);
account_set_username(account, xstrdup(""), key);
account_set_note(account, xstrdup(""), key);
if (choice == EDIT_NOTES || note_type != NOTE_TYPE_NONE) {
account->url = xstrdup("http://sn");
} else {
account->url = xstrdup("");
}
account_assign_share(blob, account, key);
list_add(&account->list, &blob->account_head);
if (note_type != NOTE_TYPE_NONE) {
char *note_type_str = NULL;
xasprintf(¬e_type_str, "NoteType:%s\n",
notes_get_name(note_type));
account_set_note(account, note_type_str, key);
}
return edit_account(session, blob, sync, account, choice, field,
non_interactive, key);
}