-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserActions.cpp
270 lines (240 loc) · 8.06 KB
/
userActions.cpp
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
#include <assert.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <string>
#include <vector>
#include "header.hpp"
using namespace std;
//Search in Entries for for place. Return the indexes.
vector <int> searchEntry( string place ) {
vector <int> found;
for( int i=0;i<(int)Entries.size();i++) if( Entries[i]->place == place ) found.push_back(i);
return found;
}
//Search in Entries for the exact same pair (place,user) of val. Return true if present, false otherwise.
bool searchCollisions( entry* val ) {
for(int i=0;i<(int)Entries.size();i++) {
if( Entries[i]->place==val->place and Entries[i]->user==val->user ){
return true;
}
}
return false;
}
//Add an entry.
void add( ) {
entry* val=new entry();
val->place = questionForm("Place:");
val->user = questionForm("User:");
while(1) {
val->pass = hiddenQuestionForm("Password:");
string pswCheck = hiddenQuestionForm("Insert again password:");
if( val->pass == pswCheck ) break;
else cout << "The second password doesn't correspond with the first. Try again.\n";
}
vector <int> found=searchEntry(val->place);
int cc=found.size();
if ( cc == 0 ) { //If there isn't another entry with the same place
Entries.push_back(val);
cout << "Entry has been registered.\n";
}
else{ //Else gives the opportunity to update an existing entry or to add a new
cout << val->place << " has already some entries:\n";
for(int i=0;i<cc;i++){ //list of entries with the same place
entry* x=Entries[found[i]];
cout << "\t" << i+1 << "- User: " << x->user << " ; Pass: " << strToAst(x->pass) << "\n";
}
string answer=questionForm("Do you want to add a new entry (write new) or \
to update one of the entry (write its number)?");
if( answer == "new" ) { //Add a new if it doesn't collide
if( !searchCollisions(val) ) {
Entries.push_back(val);
cout << "Entry has been registered.\n";
}
else {
cout << "The pair (place, user) = (" << val->place << ", " << val->user << ") is already present.\n";
}
}
else { //Update if doesn't collide
int nn=atoi(answer.c_str());
if ( 1<=nn and nn<=cc ) {
entry* x=Entries[found[nn-1]];
if( x->user == val->user or !searchCollisions(val) ) {
Entries[found[nn-1]]=val;
cout << "Entry has been updated.\n";
}
else {
cout << "The pair (place, user) = (" << val->place << ", " << val->user << ") is already present.\n";
}
}
else{ //If the value given by the user is wrong
cout << "You entered an invalid value.\n";
}
}
}
}
//Remove an entry.
void remove() {
string place = questionForm("Place:");
vector <int> found=searchEntry(place);
int cc=found.size();
if( cc==0 ) cout << "The place " << place << " isn't in the password list.\n";
else if( cc==1 ) {
Entries.erase(Entries.begin()+found[0]);
cout << "Entry has been deleted.\n";
}
else {
cout << "The place " << place << " is present in more than one entry:\n";
for(int i=0;i<cc;i++){
entry* x=Entries[found[i]];
cout << "\t" << i+1 << "- User: " << x->user << " ; Pass: " << strToAst(x->pass) << "\n";
}
string answer=questionForm("Which (* for all) should I delete?");
if( answer == "*" ) {
for(int i=cc-1; i>=0 ;i--) { //cancello dall'ultimo per non rompere i puntatori
assert( Entries[found[i]]->place == place );
Entries.erase(Entries.begin()+found[i]);
}
cout << "All entries relative to " << place << " have been deleted.\n";
}
else {
int nn=atoi(answer.c_str());
if ( 1<=nn and nn<=cc ) {
Entries.erase(Entries.begin()+found[nn-1]);
cout << "Entry has been deleted.\n";
}
else{
cout << "You entered an invalid value.\n";
}
}
}
}
//Retrieve an entry.
void retrieve( ) {
string place = questionForm("Place:");
vector <int> found=searchEntry(place);
int cc=found.size();
if( cc==0 ) cout << "The place " << place << " isn't in the password list.\n";
else {
for(int i=0;i<cc;i++) {
entry* x=Entries[found[i]];
cout << "\t" << i+1 << "- User: " << x->user << " ; Pass : " << x->pass << "\n";
}
}
}
//Retrieve (and show) all entries.
void retrieveAll( ) {
for(int i=0;i<(int)Entries.size();i++) {
entry* x=Entries[i];
cout << "Place: " << x->place << " ; User: " << x->user << " ; Pass : " << x->pass << "\n";
}
}
void importPasswords() {
bool encBool=decisionForm("Are the passwords in the file encrypted?");
string encKey;
if( encBool ) {
while (1) {
encKey=hiddenQuestionForm("Insert the key used for decryption:");
string pswCheck=hiddenQuestionForm("Insert again the key:");
if( encKey == pswCheck) break;
else cout << "The second key doesn't correspond with the first. Try again.\n";
}
}
bool sepBool=false;
if( !encBool ) sepBool=decisionForm("Is the separator between place,user,password used (otherwise space will be used)?");
string separator;
if(sepBool) separator=SEPARATOR;
else separator=" ";
string sourcePath;
while( 1 ) {
sourcePath=questionForm("Write the absolute file path from which to import: ");
if( access( sourcePath.c_str() , R_OK ) == 0 ) break;
cout << "The path entered doesn't correspond to a readable file.\n";
}
ifstream source( sourcePath.c_str() );
int nn, collNum=0, errLine=0;
source >> nn;
string stupidNewLine;//To avoid errors with getline
getline(source,stupidNewLine);
for( int i=0;i<nn;i++){
entry* newOne=new entry();
string line;
getline(source,line);
vector <string> pieces=splitString(line,separator);
if( pieces.size() != 3 ) errLine++;
else {
newOne->place=pieces[0];
newOne->user=pieces[1];
newOne->pass=pieces[2];
if( encBool ) newOne->dec(encKey);
if( !searchCollisions(newOne) ) Entries.push_back(newOne);
else collNum++;
}
}
save();
cout << "\nAll passwords have been imported (" << collNum << " collisions and " << errLine << " errors)\n";
}
void exportPasswords() {
bool encBool=decisionForm("Do you want the passwords to be encrypted (otherwise just plain-text)?");
string encKey;
if( encBool ) {
while (1) {
encKey=hiddenQuestionForm("Insert the key to use for encryption:");
string pswCheck=hiddenQuestionForm("Insert again the key:");
if( encKey == pswCheck) break;
else cout << "The second key doesn't correspond with the first. Try again.\n";
}
}
bool sepBool=false;
if( !encBool ) sepBool=decisionForm("Do you want a separator between place,user,password (suggested if some spaces are used)?");
string separator;
if(sepBool) separator=SEPARATOR;
else separator=" ";
bool fileBool=decisionForm("Do you want to export in a file?");
if(fileBool) {
ofstream dest( questionForm("Write the absolute path:").c_str() );
dest << Entries.size() << "\n";
for( int i=0;i<(int)Entries.size();i++){
entry* x=Entries[i];
x->enc(encKey);
dest << x->place << separator << x->user << separator << x->pass << "\n";
x->dec(encKey);
}
dest.close();
cout << "\nAll passwords have been saved in the specified file.\n";
}
else {
cout << "\n";
cout << Entries.size() << "\n";
for( int i=0;i<(int)Entries.size();i++){
entry* x=Entries[i];
cout << x->place << separator << x->user << separator << x->pass << "\n";
}
cout << "\nAll passwords have been printed.\n";
}
}
void changePrivateKey() {
string privateKey;
while (1) {
privateKey = hiddenQuestionForm("Insert the private key (used for encryption):");
string pswCheck = hiddenQuestionForm("Insert the private key again:");
if( privateKey == pswCheck ) break;
else cout << "The second key doesn't correspond with the first. Try again.\n";
}
setPrivateKey( privateKey );
}
void choosePrivateKey() {
string privateKey;
while (1) {
privateKey = hiddenQuestionForm("Insert the private key, that will bey used as master password for encrypting passwords:");
string pswCheck = hiddenQuestionForm("Insert the private key again:");
if( privateKey == pswCheck ) break;
else cout << "The second key doesn't correspond with the first. Try again.\n";
}
system(("mkdir "+mainFolderPath).c_str()); //Creates the folder ~/.passwordKeeper
setPrivateKey( privateKey );
}
void destroy () { //Deletes the folder if present
system( ("rm -fr "+mainFolderPath).c_str() );
}