forked from lastpass/lastpass-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd-import.c
412 lines (362 loc) · 9.37 KB
/
cmd-import.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
/*
* command for importing vault entries from CSV file into the vault
*
* 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 "util.h"
#include "config.h"
#include "terminal.h"
#include "kdf.h"
#include "blob.h"
#include "endpoints.h"
#include "agent.h"
#include "list.h"
#include <getopt.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <search.h>
struct csv_record {
struct list_head field_head;
struct list_head list;
};
struct csv_field {
char *value;
struct list_head list;
};
enum csv_token {
CSV_NONE,
CSV_FIELD,
CSV_NL,
CSV_EOF
};
static enum csv_token csv_next_token(FILE *fp, char **retp)
{
int ch, nextch;
bool readch = false;
bool in_quote = false;
bool done = false;
bool eol = false;
char *rptr;
struct buffer result = {
.len = 0,
.max = 80,
.bytes = NULL,
};
result.bytes = xcalloc(result.max, 1);
*retp = NULL;
while (!done) {
ch = fgetc(fp);
if (ch == EOF)
break;
readch = true;
switch(ch)
{
case '\n':
case ',':
/* non-quoted newline / comma terminate field */
if (!in_quote) {
done = true;
eol = (ch == '\n');
break;
}
/* otherwise append */
buffer_append_char(&result, ch);
break;
case '"':
/*
* quote immediately after comma starts a
* double-quoted field
*/
if (result.len == 0 && !in_quote) {
in_quote = true;
continue;
}
if (in_quote) {
/*
* inside a dqfield, two double quotes adds a
* quote, print one
*/
nextch = fgetc(fp);
if (nextch == '"') {
buffer_append_char(&result, '"');
continue;
}
/* otherwise terminate the quote */
in_quote = false;
ungetc(nextch, fp);
continue;
}
/* quote not after a comma, treat as unescaped */
buffer_append_char(&result, ch);
break;
default:
buffer_append_char(&result, ch);
}
}
if (!readch)
return CSV_EOF;
rptr = result.bytes;
/* trim cr/nl, but not spaces (they may be significant) */
while (rptr[strlen(rptr)-1] == '\r' ||
rptr[strlen(rptr)-1] == '\n') {
rptr[strlen(rptr)-1] = '\0';
}
*retp = rptr;
if (eol)
return CSV_NL;
return CSV_FIELD;
}
static struct csv_record *csv_record_new()
{
struct csv_record *r;
r = new0(struct csv_record, 1);
INIT_LIST_HEAD(&r->field_head);
return r;
}
/*
* Return a list of csv_record items from parsing a CSV file.
*/
static void csv_parse(FILE *fp, struct list_head *list)
{
char *p;
enum csv_token token;
struct csv_record *record;
record = csv_record_new();
while ((token = csv_next_token(fp, &p))) {
if (p) {
struct csv_field *field = new0(struct csv_field, 1);
field->value = p;
list_add_tail(&field->list, &record->field_head);
}
if (token == CSV_NL || token == CSV_EOF) {
if (!list_empty(&record->field_head)) {
list_add_tail(&record->list, list);
record = csv_record_new();
}
}
if (token == CSV_EOF)
break;
}
free(record);
}
static struct account *new_import_account(unsigned char key[KDF_HASH_LEN])
{
struct account *account = new_account();
account_set_url(account, xstrdup(""), key);
account_set_username(account, xstrdup(""), key);
account_set_password(account, xstrdup(""), key);
account_set_note(account, xstrdup(""), key);
account_set_name(account, xstrdup(""), key);
account_set_group(account, xstrdup(""), key);
return account;
}
static int csv_parse_accounts(FILE *fp, struct list_head *account_list,
unsigned char key[KDF_HASH_LEN])
{
struct list_head items;
struct csv_record *record, *tmp_record, *first;
struct csv_field *field, *tmp_field;
int i = 0;
int num_accounts = 0;
int url_index = -1,
username_index = -1,
password_index = -1,
extra_index = -1,
name_index = -1,
grouping_index = -1,
fav_index = -1;
INIT_LIST_HEAD(&items);
csv_parse(fp, &items);
if (list_empty(&items))
return 0;
#define set_field_index(x) \
do { \
if (!strcmp(field->value, #x)) { \
x ## _index = i; \
} \
} while (0)
#define set_field(x, fieldname) \
do { \
if (i == x ## _index) { \
account_set_ ## fieldname (account, field->value, key); \
set = true; \
} \
} while (0)
/*
* first line should tell us the field matrix; if
* it doesn't reveal anything useful then we won't
* import anything
*/
record = list_first_entry(&items, struct csv_record, list);
list_for_each_entry(field, &record->field_head, list) {
set_field_index(url);
set_field_index(username);
set_field_index(password);
set_field_index(extra);
set_field_index(name);
set_field_index(grouping);
set_field_index(fav);
i++;
}
if (url_index == -1 && username_index == -1 &&
password_index == -1 && extra_index == -1 &&
name_index == -1 && grouping_index == -1 &&
fav_index == -1) {
die("Could not read the CSV header at the first line of the input file");
return 0;
}
first = record;
list_for_each_entry(record, &items, list) {
struct account *account;
if (record == first)
continue;
account = new_import_account(key);
i = 0;
list_for_each_entry(field, &record->field_head, list) {
bool set = false;
set_field(url, url);
set_field(username, username);
set_field(password, password);
set_field(name, name);
set_field(grouping, group);
set_field(extra, note);
if (i == fav_index) {
account->fav = field->value[0] == '1';
set = true;
}
/* free unknown field */
if (!set)
free(field->value);
i++;
}
num_accounts++;
list_add_tail(&account->list, account_list);
}
list_for_each_entry_safe(record, tmp_record, &items, list) {
list_for_each_entry_safe(field, tmp_field,
&record->field_head, list) {
free(field);
}
free(record);
}
return num_accounts;
}
/* dedupe based on password / url / name / username sets */
int csv_dedupe_compare(const void *k1, const void *k2)
{
const struct account *a1 = k1, *a2 = k2;
int r;
if ((r = strcmp(a1->password, a2->password)))
return r;
if ((r = strcmp(a1->username, a2->username)))
return r;
if ((r = strcmp(a1->url, a2->url)))
return r;
return strcmp(a1->name, a2->name);
}
void csv_dedupe_accounts(struct list_head *blob_accounts,
struct list_head *new_accounts)
{
struct account *account, *tmp;
void *search_tree = NULL;
list_for_each_entry(account, blob_accounts, list) {
tsearch(account, &search_tree, csv_dedupe_compare);
}
list_for_each_entry_safe(account, tmp, new_accounts, list) {
if (tfind(account, &search_tree, csv_dedupe_compare)) {
list_del(&account->list);
account_free(account);
}
}
}
int cmd_import(int argc, char **argv)
{
static struct option long_options[] = {
{"sync", required_argument, NULL, 'S'},
{"keep-dupes", no_argument, NULL, 'k'},
{0, 0, 0, 0}
};
int option;
int option_index;
enum blobsync sync = BLOB_SYNC_AUTO;
unsigned char key[KDF_HASH_LEN];
_cleanup_fclose_ FILE *fp;
struct session *session = NULL;
struct blob *blob = NULL;
struct list_head accounts;
struct account *account;
int count, new_count;
bool keep_dupes = false;
int ret;
while ((option = getopt_long(argc, argv, "", long_options, &option_index)) != -1) {
switch (option) {
case 'S':
sync = parse_sync_string(optarg);
break;
case 'k':
keep_dupes = true;
break;
case '?':
default:
die_usage(cmd_import_usage);
}
}
if (argc - optind < 1) {
fp = stdin;
} else {
char *filename = argv[optind];
fp = fopen(filename, "rb");
if (!fp)
die("Unable to open %s", filename);
}
init_all(sync, key, &session, &blob);
INIT_LIST_HEAD(&accounts);
count = csv_parse_accounts(fp, &accounts, key);
printf("Parsed %d accounts\n", count);
new_count = 0;
if (!keep_dupes)
csv_dedupe_accounts(&blob->account_head, &accounts);
list_for_each_entry(account, &accounts, list) {
new_count++;
};
if (count - new_count)
printf("Removed %d duplicate accounts\n", count - new_count);
ret = lastpass_upload(session, &accounts);
if (ret)
die("Import failed (%d)\n", ret);
session_free(session);
blob_free(blob);
return 0;
}