-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.c
268 lines (231 loc) · 4.89 KB
/
shared.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
#define _CRT_SECURE_NO_WARNINGS
#include "shared.h"
#include "ctype.h"
void sortEntries(void)
{
bool changed;
do
{
changed = false;
for (int i = 0; i < state.entryCount - 1; ++i)
{
char t1[MAXTITLE];
char t2[MAXTITLE];
clearArray(t1, MAXTITLE);
clearArray(t2, MAXTITLE);
for (size_t j = 0; j < strlen(entries[i].title); ++j)
t1[j] = (char)toupper(entries[i].title[j]);
for (size_t j = 0; j < strlen(entries[i + 1].title); ++j)
t2[j] = (char)toupper(entries[i + 1].title[j]);
if (strcmp(t1, t2) > 0)
{
changed = true;
struct Entry tmp = { 0 };
strcpy(tmp.title, entries[i].title);
strcpy(tmp.id, entries[i].id);
strcpy(tmp.pw, entries[i].pw);
strcpy(tmp.misc, entries[i].misc);
strcpy(entries[i].title, entries[i + 1].title);
strcpy(entries[i].id, entries[i + 1].id);
strcpy(entries[i].pw, entries[i + 1].pw);
strcpy(entries[i].misc, entries[i + 1].misc);
strcpy(entries[i + 1].title, tmp.title);
strcpy(entries[i + 1].id, tmp.id);
strcpy(entries[i + 1].pw, tmp.pw);
strcpy(entries[i + 1].misc, tmp.misc);
}
}
}
while (changed);
}
void generatePassword(char *buf)
{
int rpos;
// clear password array first
clearArray(buf, MAXPW);
// add random special chars
for (int specialCount = 0; specialCount < settings.minSpecial; ++specialCount)
{
int rn = rand() % 127;
if (rn < 33 || rn == 44) // prevent control characters and commas
{
--specialCount;
continue;
}
if (ispunct((char)rn))
{
do
{
rpos = rand() % settings.passwordSize;
}
while (buf[rpos] != '\0');
buf[rpos] = (char)rn;
}
else
{
--specialCount;
continue;
}
}
// add random numeric chars
for (int numericCount = 0; numericCount < settings.minNumeric; ++numericCount)
{
int rn = rand() % 127;
if (rn < 33 || rn == 44)
{
--numericCount;
continue;
}
if (isdigit((char)rn))
{
do
{
rpos = rand() % settings.passwordSize;
}
while (buf[rpos] != '\0');
buf[rpos] = (char)rn;
}
else
{
--numericCount;
continue;
}
}
// add random uppercase chars
for (int uppercaseCount = 0; uppercaseCount < settings.minUppercase; ++uppercaseCount)
{
int rn = rand() % 127;
if (rn < 33 || rn == 44)
{
--uppercaseCount;
continue;
}
if (isupper((char)rn))
{
do
{
rpos = rand() % settings.passwordSize;
}
while (buf[rpos] != '\0');
buf[rpos] = (char)rn;
}
else
{
--uppercaseCount;
continue;
}
}
// fill in the remaining positions
for (int i = 0; i < settings.passwordSize; ++i)
{
if (buf[i] == '\0')
{
int rn = rand() % 127;
if (rn < 33 || rn == 44)
{
--i;
continue;
}
buf[i] = (char)rn;
}
}
}
void generateKeygen(char *buf)
{
for (int i = 0; i < 16; ++i)
{
int rn = rand() % 16;
sprintf(buf + i, "%X", rn);
}
}
void readSettings(char *iniFile)
{
FILE *f = fopen(iniFile, "r");
if (f == NULL)
{
settings.passwordSize = MINPW;
writeSettings(iniFile);
return;
}
settings.passwordSize = MINPW;
settings.minSpecial = 0;
settings.minNumeric = 0;
settings.minUppercase = 0;
settings.screenRow = 0;
settings.screenCol = 0;
char line[MAXLINE];
while (fgets(line, MAXLINE, f) != NULL)
{
if (line[0] == '#')
continue;
char setting[MAXLINE];
char value[MAXLINE];
char *l = line;
char *s = setting;
char *v = value;
clearArray(setting, MAXLINE);
clearArray(value, MAXLINE);
// find setting
while (*l && *l != '=')
{
*s = *l;
s++;
l++;
}
*s = '\0';
// find value
++l;
while (*l)
{
*v = *l;
l++;
v++;
}
*v = '\0';
if (strcmp(setting, "password_size") == 0) settings.passwordSize = atoi(value);
if (strcmp(setting, "min_special") == 0) settings.minSpecial = atoi(value);
if (strcmp(setting, "min_numeric") == 0) settings.minNumeric = atoi(value);
if (strcmp(setting, "min_uppercase") == 0) settings.minUppercase = atoi(value);
if (strcmp(setting, "window_row") == 0) settings.screenRow = atoi(value);
if (strcmp(setting, "window_col") == 0) settings.screenCol = atoi(value);
if (strcmp(setting, "keygen") == 0)
{
strncpy(settings.iv, value, 16);
settings.iv[16] = '\0';
}
}
if (strlen(settings.iv) < IV_SIZE - 1) // keygen is missing
writeSettings(iniFile);
fclose(f);
}
void removeCommas(char *text, int length)
{
for (int i = 0; i < length; ++i)
if (text[i] == ',')
text[i] = ' ';
}
void lowerCase(char *text, int length)
{
for (int i = 0; i < length; ++i)
text[i] = (char)tolower(text[i]);
}
void clearArray(char *text, int length)
{
for (int i = 0; i < length; ++i)
text[i] = '\0';
}
void exportDB(void)
{
FILE *exportFile;
exportFile = fopen("Trove_export.txt", "w");
for (int i = 0; i < state.entryCount; ++i)
{
char buf[MAXLINE];
sprintf(buf, "%s, %s, %s, %s\n", entries[i].title,
entries[i].id,
entries[i].pw,
entries[i].misc);
fwrite(buf, strlen(buf), 1, exportFile);
}
fclose(exportFile);
}