// /file PostfixIntegrate.cpp // // Copyright (C) 2011, ARM Research Labs, LLC. // See www.armresearch.com for the copyright terms. // // This file contains the functions for PostfixIntegrate. // // $Id$ // /////////////////////////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include #include #include "PostfixIntegrate.hpp" ////////////////////////////////////////////////////////////////////////////////////////////////////////// // Configuration. //////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////// const std::string SnfMilterMainCfSearchString("Added by SNFMilterConfig"); const std::string SnfMilterMainCfIntegrationString("smtpd_milters = unix:/var/snf-milter/socket # Added by SNFMilterConfig"); ////////////////////////////////////////////////////////////////////////////////////////////////////////// // End of configuration. ///////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////// void PostfixIntegrate::SetOperatingSystem(std::string OperatingSystemType) { MtaIsRunningCommand = "ps axl | grep -v grep | grep -q 'postfix/master'"; if ("OpenBSD" == OperatingSystemType) { PostfixMainCfPath = "/etc/postfix/main.cf"; PostfixMasterCfPath = "/etc/postfix/master.cf"; ReloadMtaCommand = "/usr/local/sbin/postfix reload"; } else if ("FreeBSD" == OperatingSystemType) { PostfixMainCfPath = "/usr/local/etc/postfix/main.cf"; PostfixMasterCfPath = "/usr/local/etc/postfix/master.cf"; ReloadMtaCommand = "/usr/local/sbin/postfix reload"; } else if ("Ubuntu" == OperatingSystemType) { PostfixMainCfPath = "/etc/postfix/main.cf"; PostfixMasterCfPath = "/etc/postfix/master.cf"; ReloadMtaCommand = "/usr/sbin/postfix reload"; } else if ("RedHat" == OperatingSystemType) { PostfixMainCfPath = "/etc/postfix/main.cf"; PostfixMasterCfPath = "/etc/postfix/master.cf"; ReloadMtaCommand = "/usr/sbin/postfix reload"; } else if ("Suse" == OperatingSystemType) { PostfixMainCfPath = "/etc/postfix/main.cf"; PostfixMasterCfPath = "/etc/postfix/master.cf"; ReloadMtaCommand = "/usr/sbin/postfix reload"; } else { std::ostringstream Temp; Temp << "***Error from PostfixIntegrate::SetOperatingSystem: Invalid value of OperatingSystemType: " << OperatingSystemType; throw std::runtime_error(Temp.str()); } } void PostfixIntegrate::Integrate(FileBackup *SaveFile) { if (IsIntegrated()) { return; } if (Verbose()) { std::cout << "Add to postfix file " << PostfixMainCfPath << ": '" << SnfMilterMainCfIntegrationString << "'..."; } if (!Explain()) { SaveFile->CreateBackupFile(PostfixMainCfPath); // Save any existing file. std::ofstream Output; // Append the configuration. Output.open(PostfixMainCfPath.c_str(), std::ios::app); if (!Output) { std::string Temp; Temp = "Error opening the postfix configuration file " + PostfixMainCfPath; Temp += " for writing: "; Temp += strerror(errno); throw std::runtime_error(Temp); } Output << SnfMilterMainCfIntegrationString << "\n"; if (!Output) { std::string Temp; Temp = "Error appending to the postfix configuration file " + PostfixMainCfPath; Temp += ": "; Temp += strerror(errno); throw std::runtime_error(Temp); } Output.close(); if (!Output) { std::string Temp; Temp = "Error closing the postfix configuration file " + PostfixMainCfPath; Temp += " after appending: "; Temp += strerror(errno); throw std::runtime_error(Temp); } } OutputVerboseEnd(); if (!ReloadMta()) { std::cerr << "Unable to reload the postfix configuration. Please run " << "'postfix reload' for the integration with SNFMilter to take effect."; } } void PostfixIntegrate::Unintegrate(FileBackup *SaveFile) { if (!IsIntegrated()) { return; } std::ifstream Input; if (Verbose()) { std::cout << "Remove integration in postfix file " << PostfixMainCfPath << "--\n"; } if (!Explain()) { SaveFile->CreateBackupFile(PostfixMainCfPath); // Save any existing file. Input.open(PostfixMainCfPath.c_str()); // Read the contents. if (!Input) { std::string Temp; Temp = "Error opening the postfix configuration file " + PostfixMainCfPath; Temp += " for reading: "; Temp += strerror(errno); throw std::runtime_error(Temp); } std::string Content; std::string Line; while (getline(Input, Line)) { if (std::string::npos != Line.find(SnfMilterMainCfSearchString)) { // Check for integration line. if (Verbose()) { std::cout << " Remove '" << Line << "'...\n"; } continue; // Do not copy this line. } Content += Line + "\n"; // Copy this line. } if (!Input.eof()) { // Should be at end-of-file. std::string Temp; Temp = "Error reading the postfix configuration file " + PostfixMainCfPath; Temp += ": "; Temp += strerror(errno); throw std::runtime_error(Temp); } Input.close(); if (Input.bad()) { std::string Temp; Temp = "Error closing the postfix configuration file " + PostfixMainCfPath; Temp += " after reading: "; Temp += strerror(errno); throw std::runtime_error(Temp); } if (!Explain()) { std::ofstream Output; // Write the updated contents. Output.open(PostfixMainCfPath.c_str(), std::ios::trunc); if (!Output) { std::string Temp; Temp = "Error opening the postfix configuration file " + PostfixMainCfPath; Temp += " for writing: "; Temp += strerror(errno); throw std::runtime_error(Temp); } Output << Content; if (!Output) { std::string Temp; Temp = "Error writing the postfix configuration file " + PostfixMainCfPath; Temp += ": "; Temp += strerror(errno); throw std::runtime_error(Temp); } Output.close(); if (!Output) { std::string Temp; Temp = "Error closing the postfix configuration file " + PostfixMainCfPath; Temp += " after writing: "; Temp += strerror(errno); throw std::runtime_error(Temp); } } } OutputVerboseEnd(); if (!ReloadMta()) { std::cerr << "Unable to reload the postfix configuration. Please run " << "'postfix reload' for the integration with SNFMilter to take effect."; } } bool PostfixIntegrate::MtaIsRunningDetected() { if (Verbose()) { std::cout << "Checking whether postfix is detected to be running..."; } bool IsRunningDetected; IsRunningDetected = (std::system(MtaIsRunningCommand.c_str()) == 0); if (Verbose()) { std::cout << (IsRunningDetected ? "yes..." : "no..."); } OutputVerboseEnd(); return IsRunningDetected; } bool PostfixIntegrate::ReloadMta() { if (!MtaIsRunningDetected()) { return true; } if (Verbose()) { std::cout << "Reloading postfix...\n"; std::cout.flush(); } bool Succeeded; if (!Explain()) { Succeeded = (std::system(ReloadMtaCommand.c_str()) == 0); if (Verbose()) { std::cout << (Succeeded ? "succeeded..." : "failed..."); } } OutputVerboseEnd(); return Succeeded; } bool PostfixIntegrate::IsIntegrated() { if (Verbose()) { std::cout << "Checking for any SNFMilter integration in the postfix file " << PostfixMainCfPath << "..."; } if (!FileExists(PostfixMainCfPath)) { if (Verbose()) { std::cout << "file doesn't exist; postfix is not integrated..."; } OutputVerboseEnd(); return false; } bool Integrated = false; std::ifstream Input; Input.open(PostfixMainCfPath.c_str()); // Read the contents. if (!Input) { std::string Temp; Temp = "Error opening the postfix configuration file " + PostfixMainCfPath; Temp += " for reading: "; Temp += strerror(errno); throw std::runtime_error(Temp); } std::string Line; while (getline(Input, Line)) { if (std::string::npos != Line.find(SnfMilterMainCfSearchString)) { // Check for integration line. Integrated = true; // Found it. break; } } Input.close(); if (Input.bad()) { std::string Temp; Temp = "Error closing the postfix configuration file " + PostfixMainCfPath; Temp += " after reading: "; Temp += strerror(errno); throw std::runtime_error(Temp); } if (Verbose()) { if (Integrated) { std::cout << "found '" << Line << "'..."; } else { std::cout << "none found..."; } } OutputVerboseEnd(); return Integrated; }