Skip to content

Commit

Permalink
Certificates
Browse files Browse the repository at this point in the history
  • Loading branch information
KonklaveTtv committed Aug 30, 2023
1 parent b3b1046 commit eabe139
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 26 deletions.
17 changes: 10 additions & 7 deletions src/curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ CurlHandleWrapper::CurlHandleWrapper() : curl(nullptr) {
ErrorHandler::handleErrorAndReturn(ErrorHandler::ErrorType::LIBCURL_ERROR);
return;
}

if (curl) {
// Set up location of SSL certs for Linux
curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt");
}
}

// Destructor for CurlHandleWrapper class
Expand All @@ -76,7 +71,7 @@ void CurlHandleWrapper::cleanup() {
}

// Function to set up a cURL handle with various settings
CURL* setupCurlHandle(CurlHandleWrapper &curlWrapper, bool useSSL, bool verifyPeer, bool verifyHost, bool verbose,
CURL* setupCurlHandle(CurlHandleWrapper &curlWrapper, bool useSSL, const string& sslCertPath, bool verifyPeer, bool verifyHost, bool verbose,
const string& senderEmail, const string& smtpUsername, string& mailPass, int smtpPort, const string& smtpServer) {

// Initialize the curlWrapper
Expand All @@ -86,7 +81,15 @@ CURL* setupCurlHandle(CurlHandleWrapper &curlWrapper, bool useSSL, bool verifyPe
ErrorHandler::handleErrorAndReturn(ErrorHandler::ErrorType::LIBCURL_ERROR);
return nullptr;
}


// Set SSL certificate path
if (!sslCertPath.empty()) {
curl_easy_setopt(curl, CURLOPT_CAINFO, sslCertPath.c_str());
} else {
// Set up native location of SSL certificates
curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
}

// SMTP server configuration
string smtpUrl = "smtp://" + smtpServer + ":" + to_string(smtpPort);
curl_easy_setopt(curl, CURLOPT_URL, smtpUrl.c_str());
Expand Down
3 changes: 3 additions & 0 deletions src/errorhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ void ErrorHandler::handleErrorAndReturn(ErrorType error, const string& extraInfo
case ErrorType::SMTP_AUTH_ERROR:
cerr << "Authentication with SMTP server failed." << endl;
break;
case ErrorType::SSL_CERT_PATH_ERROR:
cerr << "SSL certificate path is not valid." << endl;
break;
#ifdef UNIT_TESTING
default:
cerr << "" << endl;
Expand Down
19 changes: 17 additions & 2 deletions src/fileutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ string mockVenuesCsvPath = "src/test/mock_venues.csv";
string mockConfigJsonPath = "src/test/mock_config.json";
}

// Function to check a file exists at a given path
bool ConsoleUtils::fileExists(const std::string& filename) {
std::ifstream file(filename);
return file.good();
}

// Function to trim leading and trailing spaces from a string
string ConsoleUtils::trim(const string& str){
// Trimming function
Expand Down Expand Up @@ -150,8 +156,17 @@ std::string ConsoleUtils::passwordEntry(bool& initColor) {
continue;
}

std::cout << std::endl << "Confirm your email password: ";

if (initColor) {
std::cout << std::endl << "Confirm your email password: ";
} else {
#ifndef UNIT_TESTING
ConsoleUtils::setColor(ConsoleUtils::Color::ORANGE); // Orange for input
#endif
std::cout << std::endl << "Confirm your email password: ";
#ifndef UNIT_TESTING
ConsoleUtils::resetColor(); // Reset color
#endif
}
confirm.clear();
while (true) {
ch = getchar();
Expand Down
2 changes: 1 addition & 1 deletion src/include/curl.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CurlHandleWrapper {
};

// Function to set up a cURL handle with the given options
CURL* setupCurlHandle(CurlHandleWrapper &curlWrapper, bool useSSL, bool verifyPeer, bool verifyHost, bool verbose,
CURL* setupCurlHandle(CurlHandleWrapper &curlWrapper, bool useSSL, const std::string& sslCertPath, bool verifyPeer, bool verifyHost, bool verbose,
const std::string& senderEmail, const std::string& smtpUsername, std::string& mailPass, int smtpPort, const std::string& smtpServer);

#endif // CURL_H
3 changes: 2 additions & 1 deletion src/include/errorhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class ErrorHandler {
SMTP_SERVER_LENGTH_ERROR,
SMTP_PORT_FORMAT_ERROR,
SMTP_CONNECTION_ERROR,
SMTP_AUTH_ERROR
SMTP_AUTH_ERROR,
SSL_CERT_PATH_ERROR
};

// Function to handle errors and display appropriate messages based on the type of error
Expand Down
3 changes: 3 additions & 0 deletions src/include/fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ class ConsoleUtils {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Clear input buffer
}

// Function to check a file exists at a given path
static bool fileExists(const std::string& filename);

// Method to trim leading and trailing spaces from a string
static std::string trim(const std::string& str);

Expand Down
2 changes: 1 addition & 1 deletion src/include/mail.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class EmailManager {


// Function to display the email settings from the configuration file
static void viewEmailSettings(bool useSSL, bool verifyPeer, bool verifyHost, bool verbose,
static void viewEmailSettings(bool useSSL, const std::string& sslCertPath, bool verifyPeer, bool verifyHost, bool verbose,
const std::string& senderEmail, int smtpPort, const std::string& smtpServer);

// Function to validate an email address format
Expand Down
3 changes: 2 additions & 1 deletion src/include/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class MenuManager {
std::vector<SelectedVenue>& selectedVenuesForTemplates,
std::vector<SelectedVenue>& selectedVenuesForEmail,
std::map<std::string, std::pair<std::string, std::string>>& emailToTemplate,
std::string& sslCertPath,
std::string& subject,
std::string& message,
std::string& attachmentName,
Expand Down Expand Up @@ -153,7 +154,7 @@ class MenuManager {
static int displayTemplateOptions();
static int displayConfigurationOptions();

static bool editConfigurationSettings(bool& useSSL, bool& verifyPeer, bool& verifyHost, bool& verbose,
static bool editConfigurationSettings(bool& useSSL, std::string& sslCertPath, bool& verifyPeer, bool& verifyHost, bool& verbose,
std::string& senderEmail, std::string& smtpUsername,
std::string& mailPass, int& smtpPort, std::string& smtpServer, bool& initColor);

Expand Down
9 changes: 6 additions & 3 deletions src/mail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int totalCustomEmails;
int totalTemplateEmails;

// Function to display current email settings
void EmailManager::viewEmailSettings(bool useSSL, bool verifyPeer, bool verifyHost, bool verbose,
void EmailManager::viewEmailSettings(bool useSSL, const string& sslCertPath, bool verifyPeer, bool verifyHost, bool verbose,
const string& senderEmail, int smtpPort, const string& smtpServer) {
// Display the email settings in a structured format
#ifndef UNIT_TESTING
Expand All @@ -30,8 +30,11 @@ void EmailManager::viewEmailSettings(bool useSSL, bool verifyPeer, bool verifyHo
cout << "SMTP Server: " << smtpServer << endl
<< "SMTP Port: " << smtpPort << endl
<< "Sender Email: " << senderEmail << endl
<< "SSL: " << (useSSL ? "true" : "false") << endl
<< "verifyPeer: " << (verifyPeer ? "true" : "false") << endl
<< "SSL: " << (useSSL ? "true" : "false") << endl;
if (!sslCertPath.empty()) {
cout << "SSL Cert Path: " << sslCertPath << endl;
}
cout << "verifyPeer: " << (verifyPeer ? "true" : "false") << endl
<< "verifyHost: " << (verifyHost ? "true" : "false") << endl
<< "verbose: " << (verbose ? "true" : "false") << endl;
#ifndef UNIT_TESTING
Expand Down
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main() {
// Initialize necessary variables
vector<Venue> venues;
vector<SelectedVenue> selectedVenuesForTemplates;
string configVenuesCsvPath, smtpServer, smtpUsername, mailPass, senderEmail, subject, message, attachmentName, attachmentPath, attachmentSize;
string configVenuesCsvPath, smtpServer, smtpUsername, sslCertPath, mailPass, senderEmail, subject, message, attachmentName, attachmentPath, attachmentSize;
int smtpPort;
bool templateExists = false;
bool initColor = true;
Expand All @@ -32,7 +32,7 @@ int main() {

// Set up and initialize CURL
CurlHandleWrapper::init();
CURL* curl = setupCurlHandle(curlWrapper, useSSL, verifyPeer, verifyHost, verbose, senderEmail, smtpUsername, mailPass, smtpPort, smtpServer);
CURL* curl = setupCurlHandle(curlWrapper, useSSL, sslCertPath, verifyPeer, verifyHost, verbose, senderEmail, smtpUsername, mailPass, smtpPort, smtpServer);
if (!curl) {
ErrorHandler::handleErrorAndThrow(ErrorHandler::ErrorType::LIBCURL_ERROR);
return 1;
Expand Down Expand Up @@ -76,6 +76,7 @@ int main() {
selectedVenuesForTemplates,
selectedVenuesForEmail,
emailToTemplate,
sslCertPath,
subject,
message,
attachmentName,
Expand Down
61 changes: 54 additions & 7 deletions src/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ bool MenuManager::navigateMenus(EmailManager& emailManager,
vector<SelectedVenue>& selectedVenuesForTemplates,
vector<SelectedVenue>& selectedVenuesForEmail,
map<string, pair<string, string>>& emailToTemplate,
string& sslCertPath,
string& subject,
string& message,
string& attachmentName,
string& attachmentPath,
string& attachmentSize,
VenueFilter& venueFilter,
bool useSSL,
bool useSSL,
bool verifyPeer,
bool verifyHost,
bool verbose,
Expand Down Expand Up @@ -122,10 +123,10 @@ bool MenuManager::navigateMenus(EmailManager& emailManager,
int subChoice = displayConfigurationOptions();
switch (subChoice) {
case SHOW_EMAIL_SETTINGS_OPTION:
EmailManager::viewEmailSettings(useSSL, verifyPeer, verifyHost, verbose, senderEmail, smtpPort, smtpServer);
EmailManager::viewEmailSettings(useSSL, sslCertPath, verifyPeer, verifyHost, verbose, senderEmail, smtpPort, smtpServer);
continue;
case EDIT_EMAIL_SETTINGS_OPTION:
MenuManager::editConfigurationSettings(useSSL, verifyPeer, verifyHost, verbose, senderEmail, smtpUsername, mailPass, smtpPort, smtpServer, initColor);
MenuManager::editConfigurationSettings(useSSL, sslCertPath, verifyPeer, verifyHost, verbose, senderEmail, smtpUsername, mailPass, smtpPort, smtpServer, initColor);
continue;
case RETURN_TO_MAIN_MENU_FROM_CONFIGURATION_OPTIONS:
// Logic to return to the main menu
Expand Down Expand Up @@ -186,7 +187,7 @@ int MenuManager::displayMenuOptions() {
#ifndef UNIT_TESTING
ConsoleUtils::setColor(ConsoleUtils::Color::RED); // Blue for headers
#endif
cout << "EXITING VENUESENDER" << endl;
cout << "Exiting VenueSender" << endl;
#ifndef UNIT_TESTING
ConsoleUtils::resetColor(); // Reset to default color
#endif
Expand Down Expand Up @@ -385,7 +386,7 @@ int MenuManager::displayConfigurationOptions() {
}

// Function to override the settings set by config.json
bool MenuManager::editConfigurationSettings(bool& useSSL, bool& verifyPeer, bool& verifyHost, bool& verbose,
bool MenuManager::editConfigurationSettings(bool& useSSL, string& sslCertPath, bool& verifyPeer, bool& verifyHost, bool& verbose,
string& senderEmail, string& smtpUsername,
string& mailPass, int& smtpPort, string& smtpServer,
bool& initColor) {
Expand All @@ -409,6 +410,7 @@ bool MenuManager::editConfigurationSettings(bool& useSSL, bool& verifyPeer, bool
ConsoleUtils::resetColor();
#endif

// Edit Peer verification setting
while (true) {
#ifndef UNIT_TESTING
ConsoleUtils::setColor(ConsoleUtils::Color::ORANGE);
Expand All @@ -432,6 +434,51 @@ bool MenuManager::editConfigurationSettings(bool& useSSL, bool& verifyPeer, bool
}
}

// Edit SSL Cert Path
while (true) {
#ifndef UNIT_TESTING
ConsoleUtils::setColor(ConsoleUtils::Color::ORANGE);
#endif
cout << "SSL Cert Path (if unsure press enter on a new line): ";
string sslCertPathInput;
getline(cin, sslCertPathInput);
ConsoleUtils::clearInputBuffer();
#ifndef UNIT_TESTING
ConsoleUtils::resetColor();
#endif

// Remove quotes and trim spaces
sslCertPathInput.erase(remove(sslCertPathInput.begin(), sslCertPathInput.end(), '\''), sslCertPathInput.end());
sslCertPathInput = ConsoleUtils::trim(sslCertPathInput);

// Whitespace check (full whitespace)
if (all_of(sslCertPathInput.begin(), sslCertPathInput.end(), ::isspace)) {
ErrorHandler::handleErrorAndReturn(ErrorHandler::ErrorType::SSL_CERT_PATH_ERROR);
continue;
}

// ANSI escape code check
if (sslCertPathInput.find("\033") != string::npos) {
ErrorHandler::handleErrorAndReturn(ErrorHandler::ErrorType::INVALID_INPUT_ERROR);
continue;
}

if (!sslCertPathInput.empty()) {
// Check if the path exists
if (!ConsoleUtils::fileExists(sslCertPathInput)) { // check sslCertPathInput
ErrorHandler::handleErrorAndReturn(ErrorHandler::ErrorType::SSL_CERT_PATH_ERROR);
continue;
} else {
sslCertPath = sslCertPathInput; // then update sslCertPath
break;
}
} else {
// If the user left it blank, don't update the SSL cert path
break;
}
}


// Edit Peer verification setting
while (true) {
#ifndef UNIT_TESTING
Expand Down Expand Up @@ -652,7 +699,7 @@ bool MenuManager::editConfigurationSettings(bool& useSSL, bool& verifyPeer, bool
// Edit Mail password
mailPass = ConsoleUtils::passwordEntry(initColor); // Assuming the passwordEntry function is available within the same class or public

EmailManager::viewEmailSettings(useSSL, verifyPeer, verifyHost, verbose, senderEmail, smtpPort, smtpServer);
EmailManager::viewEmailSettings(useSSL, sslCertPath, verifyPeer, verifyHost, verbose, senderEmail, smtpPort, smtpServer);

#ifndef UNIT_TESTING
ConsoleUtils::setColor(ConsoleUtils::Color::LIGHT_BLUE);
Expand All @@ -664,7 +711,7 @@ bool MenuManager::editConfigurationSettings(bool& useSSL, bool& verifyPeer, bool
#ifndef UNIT_TESTING
ConsoleUtils::setColor(ConsoleUtils::Color::GREEN);
#endif
cout << "SETTINGS UPDATED" << endl;
cout << "Settings Updated" << endl;
#ifndef UNIT_TESTING
ConsoleUtils::resetColor();
#endif
Expand Down
3 changes: 2 additions & 1 deletion src/test/test_venuesender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ TEST_CASE("EmailManager::viewEmailSettings functionality", "[EmailManager]") {

EmailManager emailManager;

emailManager.viewEmailSettings(true, true, true, true, "mock@example.com", 587, "mock_smtp_server");
emailManager.viewEmailSettings(true, "/etc/ssl/certs/ca-certificates.crt", true, true, true, "mock@example.com", 587, "mock_smtp_server");

cout.rdbuf(oldCoutStreamBuf);

Expand All @@ -256,6 +256,7 @@ TEST_CASE("EmailManager::viewEmailSettings functionality", "[EmailManager]") {
"SMTP Port: 587\n"
"Sender Email: mock@example.com\n"
"SSL: true\n"
"SSL Cert Path: /etc/ssl/certs/ca-certificates.crt\n"
"verifyPeer: true\n"
"verifyHost: true\n"
"verbose: true\n";
Expand Down

0 comments on commit eabe139

Please sign in to comment.