add save location -f

This commit is contained in:
Nikola Petrov 2023-08-29 22:02:55 +02:00
parent 8758536ed6
commit c3ab543baa
4 changed files with 77 additions and 32 deletions

View File

@ -19,6 +19,7 @@ enum class Arg
Error, // error Error, // error
Username, // update username Username, // update username
Name, // update label name Name, // update label name
File, // select save file
}; };
Arg get_args(int argc, char** argv, char** label); Arg get_args(int argc, char** argv, char** label);
@ -40,3 +41,5 @@ void arg_print_all_p(Buffer& decrypted_buffer, std::string& user_pass);
void arg_change(Buffer& decrypted_buffer, Buffer& encrypted_buffer, std::string& user_pass, Cryptography& crypto); void arg_change(Buffer& decrypted_buffer, Buffer& encrypted_buffer, std::string& user_pass, Cryptography& crypto);
void arg_show(Buffer& decrypted_buffer, const char* label); void arg_show(Buffer& decrypted_buffer, const char* label);
void arg_file(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto, std::string& save_location_path);

View File

@ -1,6 +1,7 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <fstream>
#include "arg_func.h" #include "arg_func.h"
#include "func.h" #include "func.h"
@ -14,17 +15,18 @@ void print_args()
printf_s(" Usage:\n\n"); printf_s(" Usage:\n\n");
printf_s(" password_manager.exe [flags]\n\n"); printf_s(" password_manager.exe [flags]\n\n");
printf_s(" Flags:\n\n"); printf_s(" Flags:\n\n");
printf_s(" -h \t print this message\n"); printf_s(" -h print this message\n");
printf_s(" <label> \t get password of this label can use GLOB\n"); printf_s(" <label> get password of this label can use GLOB\n");
printf_s(" -g <label> \t generate password of this label (or update if exists)\n"); printf_s(" -g <label> generate password of this label (or update if exists)\n");
printf_s(" -i <label> \t input password for this label (or update if exists)\n"); printf_s(" -i <label> input password for this label (or update if exists)\n");
printf_s(" -d <label> \t delete password of this label\n"); printf_s(" -d <label> delete password of this label\n");
printf_s(" -s <label> \t show password for this label\n"); printf_s(" -s <label> show password for this label\n");
printf_s(" -u <label> \t update username for this label\n"); printf_s(" -u <label> update username for this label\n");
printf_s(" -n <label> \t update label name\n"); printf_s(" -n <label> update label name\n");
printf_s(" -l \t list all labels\n"); printf_s(" -l list all labels\n");
printf_s(" -p \t print all passwords\n"); printf_s(" -p print all passwords\n");
printf_s(" -c \t change master password\n"); printf_s(" -c change master password\n");
printf_s(" -f <folder path> select save folder\n");
} }
Arg get_args(int argc, char** argv, char** label) { Arg get_args(int argc, char** argv, char** label) {
@ -58,6 +60,7 @@ Arg get_args(int argc, char** argv, char** label) {
if (!strcmp("-s", argv[1])) return Arg::Show; if (!strcmp("-s", argv[1])) return Arg::Show;
if (!strcmp("-u", argv[1])) return Arg::Username; if (!strcmp("-u", argv[1])) return Arg::Username;
if (!strcmp("-n", argv[1])) return Arg::Name; if (!strcmp("-n", argv[1])) return Arg::Name;
if (!strcmp("-f", argv[1])) return Arg::File;
return Arg::Error; return Arg::Error;
} }
@ -90,14 +93,14 @@ std::optional<LoginInfoPointer> arg_new_password(Buffer& decrypted_buffer, Buffe
if (pass < 0) if (pass < 0)
{ {
printf_s("Input username:"); printf_s("Input username: ");
std::getline(std::cin, username); std::getline(std::cin, username);
} }
if (generate) if (generate)
{ {
int default_length = 15; int default_length = 15;
printf_s("Input password length [%d]:", default_length); printf_s("Input password length [%d]: ", default_length);
std::string input; std::string input;
int length = default_length; int length = default_length;
@ -288,3 +291,18 @@ void arg_show(Buffer& decrypted_buffer, const char* label) {
printf_s("Password: %s\n", lip.password); printf_s("Password: %s\n", lip.password);
} }
} }
void arg_file(Buffer& decrypted_buffer, Buffer& encrypted_buffer, const char* label, Cryptography& crypto, std::string& save_location_path)
{
std::string save(label);
save.append("\\passwords.bin");
std::ofstream file(save_location_path, std::ios::binary);
file << save << std::endl;
file.close();
if (decrypted_buffer.taken <= sizeof(Index)) return;
crypto.encrypt(&decrypted_buffer, &encrypted_buffer);
encrypted_buffer.save_to_file(save);
}

View File

@ -1,4 +1,6 @@
#include <iostream> #include <iostream>
#include <fstream>
#include "win.h" #include "win.h"
#include "buffer.h" #include "buffer.h"
#include "cryptography.h" #include "cryptography.h"
@ -6,7 +8,7 @@
#include "arg_func.h" #include "arg_func.h"
#ifdef _DEBUG #ifdef _DEBUG
#define DEBUG 1 #define DEBUG 0
#endif // DEBUG #endif // DEBUG
int main(int argc, char** argv) int main(int argc, char** argv)
@ -14,7 +16,8 @@ int main(int argc, char** argv)
std::string user_pass = ""; std::string user_pass = "";
char* label = nullptr; char* label = nullptr;
Arg args = Arg::Error; Arg args = Arg::Error;
std::optional<std::string> path = get_save_path(); std::optional<std::string> save_location_path = std::nullopt;
std::string save_location = "";
#if DEBUG #if DEBUG
@ -22,19 +25,33 @@ int main(int argc, char** argv)
args = Arg::Print_all_p; args = Arg::Print_all_p;
std::string label_st = "facebook"; std::string label_st = "facebook";
label = label_st.data(); label = label_st.data();
path = "passwords.bin"; save_location = "passwords.bin";
#else #else
if (!path.has_value())
{
printf_s("Error opening save location\n");
return 0;
}
args = get_args(argc, argv, &label); args = get_args(argc, argv, &label);
if (args == Arg::Error) return 1; if (args == Arg::Error) return 1;
save_location_path = get_save_path();
if (!save_location_path.has_value())
{
printf_s("Error geting file for save path\n");
return 1;
}
std::fstream ifile(save_location_path.value(), std::ios::binary || std::ios::in);
if (ifile.is_open())
{
std::getline(ifile, save_location);
ifile.close();
}
if (save_location.empty() && args != Arg::File)
{
printf_s("No save location, try selecting file (-f)\n");
return 1;
}
printf_s("Input main password:"); printf_s("Input main password:");
user_pass = get_user_password(); user_pass = get_user_password();
if (user_pass.empty()) if (user_pass.empty())
@ -46,20 +63,24 @@ int main(int argc, char** argv)
#endif // DEBUG #endif // DEBUG
Buffer encrypted_buffer; Buffer encrypted_buffer;
if (!encrypted_buffer.load_from_file(path.value())) //load file
if (!(args == Arg::Generate || args == Arg::Input)) { if (!encrypted_buffer.load_from_file(save_location))
printf_s("No passwords, try generating password\n"); // if file doesn't exist, check if it's a new password is being generated if not, exit
if (!(args == Arg::Generate || args == Arg::Input || args == Arg::File)) {
printf_s("No passwords, try generating password (-g or -i)\n");
return 1; return 1;
} }
Cryptography crypto(user_pass.c_str(), user_pass.size()); Cryptography crypto(user_pass.c_str(), user_pass.size());
Buffer decrypted_buffer; Buffer decrypted_buffer;
//check if encrypted buffer is empty if not, decrypt it
if (encrypted_buffer.size > 0) if (encrypted_buffer.size > 0)
if (!crypto.decrypt(&encrypted_buffer, &decrypted_buffer)) return 1; if (!crypto.decrypt(&encrypted_buffer, &decrypted_buffer)) return 1;
if (decrypted_buffer.taken < 8) { //if decrypted buffer is empty, add index
Index index = { 0, 0 }; if (decrypted_buffer.taken < sizeof(Index)) {
Index index = { 0 };
index.offset = sizeof(Index); index.offset = sizeof(Index);
decrypted_buffer.add_end((uint8_t*)&index, sizeof(Index)); decrypted_buffer.add_end((uint8_t*)&index, sizeof(Index));
} }
@ -107,9 +128,13 @@ int main(int argc, char** argv)
case Arg::Show: case Arg::Show:
arg_show(decrypted_buffer, label); arg_show(decrypted_buffer, label);
break; break;
case Arg::File:
arg_file(decrypted_buffer, encrypted_buffer, label, crypto, save_location_path.value());
break;
} }
if (!login_info.has_value()) return 1; if (!login_info.has_value()) return 0;
#if DEBUG #if DEBUG

View File

@ -73,8 +73,7 @@ std::optional<std::string> get_save_path()
{ {
PWSTR path = nullptr; PWSTR path = nullptr;
// Get the user's documents directory HRESULT result = SHGetKnownFolderPath(FOLDERID_ProgramData, 0, nullptr, &path);
HRESULT result = SHGetKnownFolderPath(FOLDERID_Profile, 0, nullptr, &path);
if (!SUCCEEDED(result)) { if (!SUCCEEDED(result)) {
CoTaskMemFree(path); // Free the allocated memory CoTaskMemFree(path); // Free the allocated memory
@ -95,7 +94,7 @@ std::optional<std::string> get_save_path()
} }
userDirectory.append(L"\\passwords.bin"); userDirectory.append(L"\\save.txt");
int size = WideCharToMultiByte(CP_UTF8, 0, userDirectory.c_str(), -1, nullptr, 0, nullptr, nullptr); int size = WideCharToMultiByte(CP_UTF8, 0, userDirectory.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (size > 0) { if (size > 0) {