Fix for windows

This commit is contained in:
Nikola Petrov
2024-07-19 01:49:34 +02:00
parent 65e30edb8f
commit e0af8874c0
3 changed files with 18 additions and 27 deletions

View File

@@ -357,5 +357,5 @@ void arg_file(Buffer &decrypted_buffer, Buffer &encrypted_buffer, const char *la
void arg_size(Buffer &encrypted_buffer)
{
printf("File size is: %ldB\n", encrypted_buffer.taken);
printf("File size is: %zuB\n", encrypted_buffer.taken);
}

165
source/main.cpp Normal file
View File

@@ -0,0 +1,165 @@
#include <iostream>
#include <fstream>
#include <cstring>
#include "sys.hpp"
#include "buffer.hpp"
#include "aes256.hpp"
#include "func.hpp"
#include "arg_func.hpp"
#define DEBUG 0
int main(int argc, char **argv)
{
std::string user_pass = "";
char *label = nullptr;
Arg args = Arg::Error;
std::optional<std::string> save_location_path = std::nullopt;
std::string save_location = "";
args = get_args(argc, argv, &label);
if (args == Arg::Error)
return 0;
#if DEBUG
save_location = "passwords.bin";
#else
save_location_path = get_save_path();
if (!save_location_path.has_value())
{
printf("Error geting file for save path\n");
return 1;
}
std::ifstream ifile(save_location_path.value(), std::ios::binary);
if (ifile.is_open())
{
std::getline(ifile, save_location);
ifile.close();
}
if (save_location.empty() && args != Arg::File)
{
printf("No save location, try selecting save folder (-f)\n");
return 1;
}
#endif // DEBUG
Buffer encrypted_buffer;
// load file
if (!encrypted_buffer.load_from_file(save_location))
// 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("No passwords, try generating password (-g or -i)\n");
return 1;
}
printf("Input main password:");
user_pass = get_user_password();
if (user_pass.empty())
{
printf("Error getting password\n");
return 1;
}
Buffer decrypted_buffer;
// check if encrypted buffer is empty if not, decrypt it
if (encrypted_buffer.taken > 0)
Aes256::decrypt(user_pass, encrypted_buffer, decrypted_buffer);
// if decrypted buffer is empty, add index
if (decrypted_buffer.taken < sizeof(Index))
{
Index index = {0};
std::memcpy(&index.magic, ".bin", 4);
index.offset = sizeof(Index);
decrypted_buffer.add_end((uint8_t *)&index, sizeof(Index));
}
// check for magic num to see if file is decrypted correcly
if (std::memcmp(decrypted_buffer.buffer, ".bin", 4))
{
printf("Wrong password!!\n");
return 1;
}
std::optional<LoginInfoPointer> login_info;
switch (args)
{
case Arg::Get:
login_info = arg_get(decrypted_buffer, label);
break;
case Arg::Generate:
login_info = arg_new_password(decrypted_buffer, encrypted_buffer, label, user_pass, true);
break;
case Arg::List:
arg_list(decrypted_buffer);
break;
case Arg::Delete:
arg_delete(decrypted_buffer, encrypted_buffer, label, user_pass);
break;
case Arg::Print_all_p:
arg_print_all_p(decrypted_buffer, user_pass);
break;
case Arg::Input:
login_info = arg_new_password(decrypted_buffer, encrypted_buffer, label, user_pass, false);
break;
case Arg::Username:
arg_username(decrypted_buffer, encrypted_buffer, label, user_pass);
break;
case Arg::Name:
arg_label_name(decrypted_buffer, encrypted_buffer, label, user_pass);
break;
case Arg::Change:
arg_change(decrypted_buffer, encrypted_buffer, user_pass);
break;
case Arg::Show:
arg_show(decrypted_buffer, label);
break;
case Arg::File:
arg_file(decrypted_buffer, encrypted_buffer, label, user_pass, save_location_path.value());
break;
case Arg::Size:
arg_size(encrypted_buffer);
break;
case Arg::Error:
break;
}
if (!login_info.has_value())
return 0;
#if DEBUG
printf("Username: %s\n", login_info.value().username);
printf("Password: %s\n", login_info.value().password);
#else
printf("Username: %s\n", login_info.value().username);
put_data_on_clipboard(login_info.value().password);
printf("Password copied to clipboard\n");
#endif // _DEBUG
return 0;
}

View File

@@ -1,5 +1,6 @@
#include <iostream>
#include <string>
#include <filesystem>
#include "sys.hpp"
#include "func.hpp"
@@ -79,42 +80,33 @@ std::string get_user_password()
std::optional<std::string> get_save_path()
{
PWSTR path = nullptr;
HRESULT result = SHGetKnownFolderPath(FOLDERID_ProgramData, 0, nullptr, &path);
if (!SUCCEEDED(result))
namespace fs = std::filesystem;
const char *path = std::getenv("PROGRAMDATA");
if (path == nullptr)
{
CoTaskMemFree(path); // Free the allocated memory
return {};
}
std::wstring userDirectory(path);
CoTaskMemFree(path); // Free the allocated memory
fs::path userDirectory(path);
userDirectory /= "password_manager";
userDirectory.append(L"\\password_manager");
DWORD attrib = GetFileAttributes(userDirectory.c_str());
// check if it exists
if (!(attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY)))
if (!fs::exists(userDirectory))
{
if (!CreateDirectory(userDirectory.c_str(), nullptr))
// Directory doesn't exist, create it
if (!fs::create_directory(userDirectory))
{
return {};
}
}
userDirectory.append(L"\\save.txt");
int size = WideCharToMultiByte(CP_UTF8, 0, userDirectory.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (size > 0)
else if (!fs::is_directory(userDirectory))
{
std::string str(size, 0);
WideCharToMultiByte(CP_UTF8, 0, userDirectory.c_str(), -1, &str[0], size, nullptr, nullptr);
return str;
// Path exists but is not a directory
return {};
}
return {};
userDirectory /= "save.txt";
return userDirectory.string();
}
#else
@@ -123,7 +115,6 @@ std::optional<std::string> get_save_path()
#include <iostream>
#include <termios.h>
#include <unistd.h>
#include <filesystem>
bool put_data_on_clipboard(const char *text)
{