forked from lastpass/lastpass-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.c
185 lines (173 loc) · 6.66 KB
/
notes.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
/*
* routines for classifying secure notes
*
* 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 <string.h>
#include "notes.h"
#include "util.h"
/* Templates for shared note types */
struct note_template note_templates[] = {
[ NOTE_TYPE_AMEX ] = {
.name = "American Express",
.shortname = "amex",
.fields = { NULL }},
[ NOTE_TYPE_BANK ] = {
.name = "Bank Account",
.shortname = "bank",
.fields = { "Bank Name", "Account Type", "Routing Number", "Account Number", "SWIFT Code", "IBAN Number", "Pin", "Branch Address", "Branch Phone", NULL }},
[ NOTE_TYPE_CREDIT ] = {
.name = "Credit Card",
.shortname = "credit-card",
.fields = { "Name on Card", "Type", "Number", "Security Code", "Start Date", "Expiration Date", NULL }},
[ NOTE_TYPE_DATABASE ] = {
.name = "Database",
.shortname = "database",
.fields = { "Type", "Hostname", "Port", "Database", "Username", "Password", "SID", "Alias", NULL }},
[ NOTE_TYPE_DRIVERS_LICENSE ] = {
.name = "Driver's License",
.shortname = "drivers-license",
.fields = { "Number", "Expiration Date", "License Class", "Name", "Address", "City / Town", "State", "ZIP / Postal Code", "Country", "Date of Birth", "Sex", "Height", NULL }},
[ NOTE_TYPE_EMAIL ] = {
.name = "Email Account",
.shortname = "email",
.fields = { "Username", "Password", "Server", "Port", "Type", "SMTP Server", "SMTP Port", NULL }},
[ NOTE_TYPE_HEALTH_INSURANCE ] = {
.name = "Health Insurance",
.shortname = "health-insurance",
.fields = { "Company", "Company Phone", "Policy Type", "Policy Number", "Group ID", "Member Name", "Member ID", "Physician Name", "Physician Phone", "Physician Address", "Co-pay", NULL }},
[ NOTE_TYPE_IM ] = {
.name = "Instant Messenger",
.shortname = "im",
.fields = { "Type", "Username", "Password", "Server", "Port", NULL }},
[ NOTE_TYPE_INSURANCE ] = {
.name = "Insurance",
.shortname = "insurance",
.fields = { "Company", "Policy Type", "Policy Number", "Expiration", "Agent Name", "Agent Phone", "URL", NULL }},
[ NOTE_TYPE_MASTERCARD ] = {
.name = "Mastercard",
.shortname = "mastercard",
.fields = { NULL }},
[ NOTE_TYPE_MEMBERSHIP ] = {
.name = "Membership",
.shortname = "membership",
.fields = { "Organization", "Membership Number", "Member Name", "Start Date", "Expiration Date", "Website", "Telephone", "Password", NULL }},
[ NOTE_TYPE_PASSPORT ] = {
.name = "Passport",
.shortname = "passport",
.fields = { "Type", "Name", "Country", "Number", "Sex", "Nationality", "Date of Birth", "Issued Date", "Expiration Date", NULL }},
[ NOTE_TYPE_SERVER ] = {
.name = "Server",
.shortname = "server",
.fields = { "Hostname", "Username", "Password", NULL }},
[ NOTE_TYPE_SSN ] = {
.name = "Social Security",
.shortname = "ssn",
.fields = { "Name", "Number", NULL }},
[ NOTE_TYPE_SOFTWARE_LICENSE ] = {
.name = "Software License",
.shortname = "software-license",
.fields = { "License Key", "Licensee", "Version", "Publisher", "Support Email", "Website", "Price", "Purchase Date", "Order Number", "Number of Licenses", "Order Total", NULL }},
[ NOTE_TYPE_SSH_KEY ] = {
.name = "SSH Key",
.shortname = "ssh-key",
.fields = { "Bit Strength", "Format", "Passphrase", "Private Key", "Public Key", "Hostname", "Date", NULL }},
[ NOTE_TYPE_VISA ] = {
.name = "VISA",
.shortname = "visa",
.fields = { NULL }},
[ NOTE_TYPE_WIFI ] = {
.name = "Wi-Fi Password",
.shortname = "wifi",
.fields = { "SSID", "Password", "Connection Type", "Connection Mode", "Authentication", "Encryption", "Use 802.1X", "FIPS Mode", "Key Type", "Protected", "Key Index", NULL }},
};
const char *notes_get_name(enum note_type note_type)
{
if (note_type <= NOTE_TYPE_NONE || note_type >= NUM_NOTE_TYPES)
return "";
return note_templates[note_type].name;
}
bool note_field_is_multiline(enum note_type note_type, const char *field)
{
return note_type == NOTE_TYPE_SSH_KEY && !strcmp(field, "Private Key");
}
bool note_has_field(enum note_type note_type, const char *field)
{
const char **p;
if (note_type <= NOTE_TYPE_NONE || note_type >= NUM_NOTE_TYPES)
return true;
p = note_templates[note_type].fields;
while (*p) {
if (!strcmp(field, *p))
return true;
p++;
}
return false;
}
enum note_type notes_get_type_by_shortname(const char *type_str)
{
BUILD_BUG_ON(ARRAY_SIZE(note_templates) != NUM_NOTE_TYPES);
size_t i;
for (i = 0; i < NUM_NOTE_TYPES; i++) {
if (!strcasecmp(type_str, note_templates[i].shortname))
return i;
}
return NOTE_TYPE_NONE;
}
enum note_type notes_get_type_by_name(const char *type_str)
{
size_t i;
for (i = 0; i < NUM_NOTE_TYPES; i++) {
if (!strcasecmp(type_str, note_templates[i].name))
return i;
}
return NOTE_TYPE_NONE;
}
char *note_type_usage()
{
int i;
char *start = "--note-type=TYPE\n\nValid values for TYPE:\n";
size_t alloc_len = strlen(start) + 1;
char *usage_str;
for (i = 0; i < NUM_NOTE_TYPES; i++)
alloc_len += strlen(note_templates[i].shortname) + 2;
usage_str = xcalloc(1, alloc_len);
strlcat(usage_str, start, alloc_len);
for (i = 0; i < NUM_NOTE_TYPES; i++) {
strlcat(usage_str, "\t", alloc_len);
strlcat(usage_str, note_templates[i].shortname, alloc_len);
if (i != NUM_NOTE_TYPES - 1)
strlcat(usage_str, "\n", alloc_len);
}
return usage_str;
}