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
|
#include "Compass.h"
Compass::Compass() {
}
Compass::~Compass() {
}
void Compass::perform(int argc, char* argv[]) {
// Figure out action
if (argc < 2) {
std::cerr << ERR_NO_ACTION << std::endl;
std::cout << "Run 'compass help' for more information." << std::endl;
return;
}
try {
std::string action(argv[1]);
// Need Help?
if (action == "help") {
initNoArgs(argc, argv);
help();
}
// Supported Actions
else if (action == "initialize") {
initNoArgs(argc, argv);
initialize();
}
else if (action == "create") {
std::string service = initOneArg(argc, argv);
create(service);
}
else if (action == "walk" && ALLOW_WALK) {
initNoArgs(argc, argv);
walk();
}
else if (action == "rekey") {
initNoArgs(argc, argv);
rekey();
}
else if (action == "get") {
std::string service = initOneArg(argc, argv);
get(service);
}
else if (action == "delete") {
std::string service = initOneArg(argc, argv);
_delete(service);
}
else if (action == "show") {
initNoArgs(argc, argv);
show("", false);
}
else if (action == "show1") {
std::string service = initOneArg(argc, argv);
show(service, false);
}
else if (action == "showlike") {
std::string service = initOneArg(argc, argv);
show(service, true);
}
else if (action == "random") {
initNoArgs(argc, argv);
random();
}
else if (action == "markreset") {
initNoArgs(argc, argv);
markReset("");
}
else if (action == "markreset1") {
std::string service = initOneArg(argc, argv);
markReset(service);
}
else if (action == "checkreset") {
initNoArgs(argc, argv);
checkReset("");
}
else if (action == "checkreset1") {
std::string service = initOneArg(argc, argv);
checkReset(service);
}
else if (action == "ruser") {
std::string service = initOneArg(argc, argv);
ruser(service);
}
else if (action == "rpass") {
std::string service = initOneArg(argc, argv);
rpass(service);
}
else {
std::cerr << ERR_INVALID_ACTION << std::endl;
std::cout << "Run 'compass help' for more information." << std::endl;
}
}
catch (const int e) {
std::cerr << ERR_GENERIC << std::endl;
}
catch (const CryptoPP::Exception& e) {
std::cerr << ERR_GENERIC << std::endl;
}
catch (const double e) {
std::cerr << ERR_INPUT << std::endl;
}
catch (const bool e) {
// do nothing (message was produced by called function)
}
}
void Compass::initNoArgs(int argc, char* argv[]) {
opt.parseArgv(2, argc, argv);
}
std::string Compass::initOneArg(int argc, char* argv[]) {
if (argc < 3) {
std::cerr << ERR_REQ_PARAMS_MISSING << std::endl;
throw false;
}
std::string arg(argv[2]);
opt.checkString(arg);
opt.parseArgv(3, argc, argv);
return arg;
}
void Compass::showResult(Credential c) {
std::cout << "Username is: " << c.username << std::endl;
if (opt.pp)
std::cout << "Password is: " << c.password << std::endl;
else
pasteClipboard(c.password, !opt.cc, false);
}
void Compass::pasteClipboard(std::string text, bool clean, bool recur) {
#ifdef WIN32
if (OpenClipboard(NULL)) {
HGLOBAL clipbuffer;
char* buffer;
EmptyClipboard();
const char* source = text.c_str();
clipbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(source) + 1);
buffer = (char*)GlobalLock(clipbuffer);
strcpy(buffer, LPCSTR(source));
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT, clipbuffer);
CloseClipboard();
std::cout << MSG_CLIPBOARD << std::endl;
}
else {
std::cerr << ERR_CLIPBOARD << std::endl;
}
#else
#ifdef __APPLE__
std::string sink = "pbcopy";
#else
std::string sink = "xclip -selection c";
#endif // __APPLE__
// Mac/Linux procedure
std::string cmd = "printf %s '" + text + "' | " + sink;
system(cmd.c_str());
if (!recur) std::cout << MSG_CLIPBOARD << std::endl;
else exit(EXIT_SUCCESS);
// Cleanup
if (clean) {
pid_t pid = fork();
if (pid == 0) {
usleep(SECONDS_PSWD_CLEANUP * 1000000);
pasteClipboard(CLEAN_STRING, clean, true);
}
else if (pid < 0) {
std::cerr << WARN_CANT_CLEANUP << std::endl;
}
}
#endif // WIN32
if (!clean) std::cout << WARN_WONT_CLEANUP << std::endl;
}
void Compass::help() {
Help h;
h.dispGeneral();
}
void Compass::initialize() {
std::cout << "WARNING: This will destroy the keychain (if one exists) at '" << opt.dr << "'" << std::endl;
std::cout << "You may still abort this action by pressing CTRL-C now. If you wish to procede, create a new master password for your keychain below..." << std::endl;
std::cout << std::endl;
Keychain* kc = Keychain::newKeychain(opt.dr);
kc->saveKeychain();
delete kc;
}
void Compass::create(std::string service) {
Keychain* kc = Keychain::loadKeychain(opt.rh, opt.pt, opt.dr);
Credential c;
if (opt.user != "" && opt.pass != "")
c = kc->create(service, opt.user, opt.pass);
else if (opt.user != "")
c = kc->create(service, opt.user, opt.getPasswordSpec());
else if (opt.pass != "")
c = kc->create(service, opt.pass);
else
c = kc->create(service, opt.getPasswordSpec());
std::cout << "Created new credential for " << service << "(" << kc->getServiceCount(service) << ")" << std::endl;
showResult(c);
kc->saveKeychain();
delete kc;
}
void Compass::walk() {
Keychain* kc = Keychain::loadKeychain(opt.rh, opt.pt, opt.dr);
kc->walk();
delete kc;
}
void Compass::rekey() {
Keychain* kc = Keychain::loadKeychain(opt.rh, opt.pt, opt.dr);
Cryptor::rekey();
std::cout << "Enter a new master password for your keychain... (CTRL-C to abort)" << std::endl;
kc->saveKeychain();
delete kc;
}
void Compass::get(std::string service) {
Keychain* kc = Keychain::loadKeychain(opt.rh, opt.pt, opt.dr);
Credential c;
if (opt.cn != -1)
c = kc->get(service, opt.cn);
else if (opt.user != "")
c = kc->get(service, opt.user);
else
c = kc->get(service);
std::cout << "Found from " << service << "(" << kc->getServiceCount(service) << ")" << std::endl;
showResult(c);
if (c.reset) {
std::cout << "************************************************" << std::endl;
std::cout << "* Notice: this credential is marked for reset! *" << std::endl;
std::cout << "************************************************" << std::endl;
}
delete kc;
}
void Compass::_delete(std::string service) {
Keychain* kc = Keychain::loadKeychain(opt.rh, opt.pt, opt.dr);
bool b;
if (opt.cn != -1)
b = kc->_delete(service, opt.cn);
else if (opt.user != "")
b = kc->_delete(service, opt.user);
else
b = kc->_delete(service);
if (b)
std::cout << service << "(" << kc->getServiceCount(service) << ") has been fully removed from the keychain." << std::endl;
else
std::cout << service << "(" << kc->getServiceCount(service) << ") has been partially removed from the keychain." << std::endl;
kc->saveKeychain();
delete kc;
}
void Compass::show(std::string service, bool like) {
Keychain* kc = Keychain::loadKeychain(opt.rh, opt.pt, opt.dr);
if (service == "")
kc->show();
else if (like)
kc->showlike(service);
else
kc->show1(service);
delete kc;
}
void Compass::random() {
std::cout << Cryptor::createRandomPassword(opt.getPasswordSpec()) << std::endl;
}
void Compass::markReset(std::string service) {
Keychain* kc = Keychain::loadKeychain(opt.rh, opt.pt, opt.dr);
int i;
if (service == "")
i = kc->markResetAll();
else
i = kc->markResetOne(service);
std::cout << "Marked " << i << " credentials for reset." << std::endl;
kc->saveKeychain();
delete kc;
}
void Compass::checkReset(std::string service) {
Keychain* kc = Keychain::loadKeychain(opt.rh, opt.pt, opt.dr);
int i;
if (service == "")
i = kc->checkResetAll();
else
i = kc->checkResetOne(service);
std::cout << i << " credentials are marked for reset." << std::endl;
delete kc;
}
void Compass::ruser(std::string service) {
Keychain* kc = Keychain::loadKeychain(opt.rh, opt.pt, opt.dr);
Credential c;
if (opt.user == "") {
if (opt.cn == -1)
c = kc->ruser(service);
else
c = kc->ruser(service, opt.cn);
}
else {
if (opt.cn == -1)
c = kc->ruser(service, opt.user);
else
c = kc->ruser(service, opt.user, opt.cn);
}
std::cout << "Altered from " << service << "(" << kc->getServiceCount(service) << ")" << std::endl;
showResult(c);
kc->saveKeychain();
delete kc;
}
void Compass::rpass(std::string service) {
Keychain* kc = Keychain::loadKeychain(opt.rh, opt.pt, opt.dr);
Credential c;
if (opt.pass == "") {
if (opt.user == "" && opt.cn == -1)
c = kc->rpass(service, opt.getPasswordSpec());
else if (opt.cn != -1)
c = kc->rpass(service, opt.getPasswordSpec(), opt.cn);
else
c = kc->rpass(service, opt.getPasswordSpec(), opt.user);
}
else {
if (opt.user == "" && opt.cn == -1)
c = kc->rpass(service, opt.pass);
else if (opt.cn != -1)
c = kc->rpass(service, opt.pass, opt.cn);
else
c = kc->rpass(service, opt.pass, opt.user);
}
std::cout << "Altered from " << service << "(" << kc->getServiceCount(service) << ")" << std::endl;
showResult(c);
kc->saveKeychain();
delete kc;
}
|