// /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 PostfixIntegrate"); const std::string SnfMilterMainCfIntegrationString("smtpd_milters = unix:/var/snf-milter/socket $smtpd_milters # Added by PostfixIntegrate"); ////////////////////////////////////////////////////////////////////////////////////////////////////////// // End of configuration. ///////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////// void PostfixIntegrate::SetOperatingSystem(std::string OperatingSystemType) { MtaIsRunningCommand = "ps -axl root | grep -v grep | grep -q 'postfix/master'"; if ("OpenBSD" == OperatingSystemType) { PostfixMainCfPath = "/usr/local/etc/postfix/main.cf"; PostfixMasterCfPath = "/usr/local/etc/postfix/master.cf"; ReloadMtaCommand = "/usr/local/sbin/postfix reload"; } else if ("FreeBSD" == OperatingSystemType) { PostfixMainCfPath = "/etc/postfix/main.cf"; PostfixMasterCfPath = "/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(); } 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(); } 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 (Verbose()) { std::cout << "Reloading postfix..."; } 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 (!FileExists(PostfixMainCfPath)) { return false; } bool Integrated = false; if (Verbose()) { std::cout << "Checking for any SNFMilter integration in the postfix file " << PostfixMainCfPath << "--\n"; } 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); } OutputVerboseEnd(); return Integrated; }