From 5a7472f399c88c66c5f92fbe51160ff806b3fab8 Mon Sep 17 00:00:00 2001 From: Eric Wertz Date: Sat, 31 May 2025 12:36:54 -0400 Subject: [PATCH] Added configurable root directory --- backend/HTTPResponse.cpp | 5 ++- backend/User.cpp | 6 +-- backend/User.h | 1 + backend/decrypt.cpp | 80 +++++++++++++++++++++++++++++----------- backend/main.cpp | 15 ++++++++ backend/util.cpp | 3 ++ backend/util.h | 3 ++ 7 files changed, 87 insertions(+), 26 deletions(-) diff --git a/backend/HTTPResponse.cpp b/backend/HTTPResponse.cpp index c7b43da..e2c2344 100644 --- a/backend/HTTPResponse.cpp +++ b/backend/HTTPResponse.cpp @@ -21,9 +21,10 @@ CHTTPResponse::CHTTPResponse(const CHTTPRequest& request) : _request(request) if (_wwwDir == "") { char resolved_path[PATH_MAX]; - if( realpath("www/", resolved_path) == NULL ) + std::string www_path = g_rootDir + "/www"; + if( realpath(www_path.c_str(), resolved_path) == NULL ) { - die("Failed to resolve path"); + die("Failed to resolve path: %s", www_path.c_str()); } _wwwDir = std::string(resolved_path) + "/"; } diff --git a/backend/User.cpp b/backend/User.cpp index 04c4eb0..42b16b3 100644 --- a/backend/User.cpp +++ b/backend/User.cpp @@ -13,7 +13,7 @@ #define SESSION_ID_LENGTH 32 -CUser::CUser() : _auth_db("data/auth.db") +CUser::CUser() : _auth_db(g_rootDir + "/data/auth.db") { } @@ -75,7 +75,7 @@ void CUser::authenticate( std::vector& fullSessionKeyFromCookie ) throw HTTPUnauthorizedException("Failed to decrypt database key: incorrect key length after decryption."); } - _user_db.setDbPath("data/user/" + _uuid + ".db"); + _user_db.setDbPath(g_rootDir + "/data/user/" + _uuid + ".db"); try { _user_db.connect(); _user_db.setKey(plaintext_db_key); @@ -169,7 +169,7 @@ void CUser::create(const std::string& jsonRequest) insert_user_query.execute(); // Configure and connect the user-specific database - _user_db.setDbPath("data/user/" + _uuid + ".db"); + _user_db.setDbPath(g_rootDir + "/data/user/" + _uuid + ".db"); try { _user_db.connect(); // Ensure this method attempts to create the DB if not exists _user_db.setKey(dbKey_plaintext); // Set the encryption key for the new user DB diff --git a/backend/User.h b/backend/User.h index 63854bc..4e62c9e 100644 --- a/backend/User.h +++ b/backend/User.h @@ -22,6 +22,7 @@ public: const std::string& getUsername() const { return _username; } const std::string& getUUID() const { return _uuid; } CDatabase& getDatabase() { return _user_db; } + private: std::string _username; std::string _key; diff --git a/backend/decrypt.cpp b/backend/decrypt.cpp index 2458b29..157e012 100644 --- a/backend/decrypt.cpp +++ b/backend/decrypt.cpp @@ -5,51 +5,89 @@ #include // For std::runtime_error #include // For std::system #include // For std::ostringstream +#include #include "sqlite3.h" // For sqlite3 access #include "util.h" // For hashString, decryptBytes, ENCRYPT_KEY_LENGTH, ENCRYPT_HASH_SALT_LENGTH #include "Database.h" // For CDatabase +#include "HTTPExceptions.h" // Function to print a vector of unsigned char as a hex string void print_hex(const std::vector& data) { - std::cout << "0x"; for (unsigned char byte : data) { - std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast(byte); + printf("%02x", byte); } - std::cout << std::dec << std::endl; // Reset to decimal for subsequent output + std::cout << std::endl; } // Function to convert a vector of unsigned char to a hex string (no 0x prefix) std::string vector_to_hex_string(const std::vector& data) { std::ostringstream oss; for (unsigned char byte : data) { - oss << std::hex << std::setw(2) << std::setfill('0') << static_cast(byte); + oss << std::hex << std::setfill('0') << std::setw(2) << static_cast(byte); } return oss.str(); } -int main() { - std::string username; - std::string password; +void print_usage(const char* program_name) { + std::cout << "Usage: " << program_name << " [options] \n"; + std::cout << "Options:\n"; + std::cout << " -d Root directory for database operations (default: current directory)\n"; + std::cout << " -h Show this help message\n"; +} - std::cout << "Enter username: "; - std::cin >> username; - std::cout << "Enter password: "; - std::cin >> password; +int main(int argc, char* argv[]) { + std::string username, password; + + // Parse command line arguments + int arg_index = 1; + while (arg_index < argc) { + if (strcmp(argv[arg_index], "-d") == 0) { + if (arg_index + 1 < argc) { + g_rootDir = argv[arg_index + 1]; + arg_index += 2; + } else { + std::cerr << "Error: -d flag requires a directory path.\n"; + print_usage(argv[0]); + return 1; + } + } else if (strcmp(argv[arg_index], "-h") == 0) { + print_usage(argv[0]); + return 0; + } else if (argv[arg_index][0] == '-') { + std::cerr << "Error: Unknown argument " << argv[arg_index] << "\n"; + print_usage(argv[0]); + return 1; + } else { + // Non-flag arguments are username and password + if (username.empty()) { + username = argv[arg_index]; + } else if (password.empty()) { + password = argv[arg_index]; + } else { + std::cerr << "Error: Too many arguments.\n"; + print_usage(argv[0]); + return 1; + } + arg_index++; + } + } - CDatabase auth_db("data/auth.db"); - try { - auth_db.connect(); - } catch (const std::exception& e) { - std::cerr << "Error connecting to auth database: " << e.what() << std::endl; + if (username.empty() || password.empty()) { + std::cerr << "Error: Username and password are required.\n"; + print_usage(argv[0]); return 1; } - std::string fetched_username; - std::vector key_from_db; - std::string uuid_from_db; // Not strictly needed for decryption but fetched in User::login - try { + // Connect to auth database + CDatabase auth_db(g_rootDir + "/data/auth.db"); + auth_db.connect(); + + std::string fetched_username; + std::vector key_from_db; + std::string uuid_from_db; // Not strictly needed for decryption but fetched in User::login + // Hash password (without salt for the initial lookup, similar to CUser::login) // The CUser::login uses a fixed size buffer and then calls hashString. // We need to replicate that or ensure hashString behaves correctly. @@ -131,7 +169,7 @@ int main() { std::string hex_key_string = vector_to_hex_string(dbKey_decrypted); std::cout << "Decrypted Key (Hex for command): " << hex_key_string << std::endl; - std::string user_db_path = "data/user/" + uuid_from_db + ".db"; + std::string user_db_path = g_rootDir + "/data/user/" + uuid_from_db + ".db"; // Construct the command for sqlite3mc with PRAGMA commands and keep shell open std::string command = "data/sqlite3mc \"file:" + user_db_path + "?cipher=aes256cbc&hexkey=" + hex_key_string + "\""; diff --git a/backend/main.cpp b/backend/main.cpp index aba716f..0d24b3e 100644 --- a/backend/main.cpp +++ b/backend/main.cpp @@ -13,6 +13,7 @@ #include "HTTPRequest.h" #include "HTTPRequestRouter.h" +#include "util.h" std::map threadMap; std::vector completeThreads; @@ -54,11 +55,13 @@ void print_usage(const char* program_name) { std::cout << "Usage: " << program_name << " [options]\n"; std::cout << "Options:\n"; std::cout << " -p Port to listen on (default: 9000)\n"; + std::cout << " -d Root directory for file operations (default: current directory)\n"; std::cout << " -h Show this help message\n"; } int main(int argc, char* argv[]) { int port = 9000; // Default port + std::string root_dir = "."; // Default to current directory // Parse command line arguments for (int i = 1; i < argc; i++) { @@ -75,6 +78,15 @@ int main(int argc, char* argv[]) { print_usage(argv[0]); return 1; } + } else if (strcmp(argv[i], "-d") == 0) { + if (i + 1 < argc) { + root_dir = argv[i + 1]; + i++; // Skip the next argument since we've used it as the directory + } else { + std::cerr << "Error: -d flag requires a directory path.\n"; + print_usage(argv[0]); + return 1; + } } else if (strcmp(argv[i], "-h") == 0) { print_usage(argv[0]); return 0; @@ -85,6 +97,9 @@ int main(int argc, char* argv[]) { } } + // Set the global root directory + g_rootDir = root_dir; + int server_socket; struct sockaddr_in server_address; diff --git a/backend/util.cpp b/backend/util.cpp index bd10c1a..bf955f2 100644 --- a/backend/util.cpp +++ b/backend/util.cpp @@ -15,6 +15,9 @@ #include "UTCDateTime.h" #include "HTTPExceptions.h" +// Global root directory for file operations (default to current directory) +std::string g_rootDir = "."; + void die( const char* str, ... ) { va_list args; diff --git a/backend/util.h b/backend/util.h index 7d77610..f626504 100644 --- a/backend/util.h +++ b/backend/util.h @@ -13,6 +13,9 @@ #define ENCRYPT_HASH_LENGTH 32 // SHA-256 #define ENCRYPT_HASH_SALT_LENGTH 16 +// Global root directory for file operations +extern std::string g_rootDir; + void die( const char* str, ... ); std::string string_format( std::string format, ... ); std::string print_progress( int count, int total ); -- 2.49.0