123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- // /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 <cstdlib>
- #include <cerrno>
- #include <cstring>
-
- #include <iostream>
- #include <exception>
- #include <stdexcept>
- #include <sstream>
- #include <fstream>
-
- #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;
-
- }
|