- // /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"
- #include "PostfixMilterConf.hpp"
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // End of configuration. /////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- void
- PostfixIntegrate::SetOperatingSystem(std::string OperatingSystemType) {
-
- MtaIsRunningCommand = "ps axl | grep -v grep | grep -q 'postfix/master'";
-
- if ("OpenBSD" == OperatingSystemType) {
-
- PostfixDefaultIsChrooted = true;
- PostfixSocketSpec = "unix:/snf-milter/socket";
- PostfixMainCfPath = "/etc/postfix/main.cf";
- PostfixMasterCfPath = "/etc/postfix/master.cf";
- ReloadMtaCommand = "/usr/local/sbin/postfix reload";
-
- } else if ("FreeBSD" == OperatingSystemType) {
-
- PostfixDefaultIsChrooted = false;
- PostfixSocketSpec = "unix:/var/snf-milter/socket";
- PostfixMainCfPath = "/usr/local/etc/postfix/main.cf";
- PostfixMasterCfPath = "/usr/local/etc/postfix/master.cf";
- ReloadMtaCommand = "/usr/local/sbin/postfix reload";
-
- } else if ("Ubuntu" == OperatingSystemType) {
-
- PostfixDefaultIsChrooted = true;
- PostfixSocketSpec = "unix:/snf-milter/socket";
- PostfixMainCfPath = "/etc/postfix/main.cf";
- PostfixMasterCfPath = "/etc/postfix/master.cf";
- ReloadMtaCommand = "/usr/sbin/postfix reload";
-
- } else if ("RedHat" == OperatingSystemType) {
-
- PostfixDefaultIsChrooted = false;
- PostfixSocketSpec = "unix:/var/snf-milter/socket";
- PostfixMainCfPath = "/etc/postfix/main.cf";
- PostfixMasterCfPath = "/etc/postfix/master.cf";
- ReloadMtaCommand = "/usr/sbin/postfix reload";
-
- } else if ("Suse" == OperatingSystemType) {
-
- PostfixDefaultIsChrooted = false;
- PostfixSocketSpec = "unix:/var/snf-milter/socket";
- 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;
-
- }
-
- std::ifstream Input;
-
- if (Verbose()) {
-
- std::cout << "Integrate with postfix...\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;
- bool ModifiedLine = false;
-
- PostfixMilterConf MilterConf(PostfixSocketSpec); // Object to update the config line.
-
- while (getline(Input, Line)) {
-
- MilterConf.ConfLine(Line); // Load the object with the line.
-
- if (MilterConf.IsMilterLine() && !ModifiedLine) { // Check for milter integration line.
- // Ignore subsequence integration lines.
- MilterConf.AddIntegration(); // Found milter line. Add integration.
-
- if (Verbose()) {
-
- std::cout << " Replace '" << Line << "' with '"
- << MilterConf.ConfLine() << "'...\n";
-
- }
-
- Line = MilterConf.ConfLine(); // Copy updated line.
- ModifiedLine = true;
-
- }
-
- Content += Line + "\n"; // Copy this line.
-
- }
-
- if (!ModifiedLine) {
-
- MilterConf.ConfLine("");
- MilterConf.AddIntegration();
-
- if (Verbose()) {
-
- std::cout << " Add '" << MilterConf.ConfLine() << "'...\n";
-
- }
-
- Content += MilterConf.ConfLine() + "\n";
-
- }
-
- 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.";
-
- }
-
- }
-
- 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;
-
- PostfixMilterConf MilterConf(PostfixSocketSpec); // Object to update the config line.
-
- while (getline(Input, Line)) {
-
- MilterConf.ConfLine(Line); // Load the object with the line.
-
- if (MilterConf.IsIntegrated()) { // Check for integration.
-
- MilterConf.RemoveIntegration(); // Integrated. Remove the milter spec.
-
- if (Verbose()) {
-
- std::cout << " Replace '" << Line << "' with '"
- << MilterConf.ConfLine() << "'...\n";
-
- }
-
- Content += MilterConf.ConfLine(); // Copy updated line.
- if (MilterConf.ConfLine() != "") {
-
- Content += "\n";
-
- }
- continue;
-
- }
-
- 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);
- }
-
- PostfixMilterConf MilterConf(PostfixSocketSpec); // Object to update the config line.
- std::string Line;
-
- while (getline(Input, Line)) {
-
- MilterConf.ConfLine(Line);
-
- if (MilterConf.IsIntegrated()) { // 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;
-
- }
|