123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- // /file SendmailIntegrate.cpp
- //
- // Copyright (C) 2011, ARM Research Labs, LLC.
- // See www.armresearch.com for the copyright terms.
- //
- // This file contains the functions for SendmailIntegrate.
- //
- // $Id$
- //
- ///////////////////////////////////////////////////////////////////////////////////////////////////
-
- #include <cstdlib>
- #include <cerrno>
- #include <cstring>
-
- #include <iostream>
- #include <exception>
- #include <stdexcept>
- #include <sstream>
- #include <fstream>
-
- #include "SendmailIntegrate.hpp"
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- const std::string SnfMilterSendmailMcSearchString("Added by SNFMilterConfig");
-
- const std::string SnfMilterSendmailMcIntegrationString("INPUT_MAIL_FILTER(`SNFMilter', `S=unix:/var/snf-milter/socket')dnl # Added by SNFMilterConfig");
-
- const std::string MtaIsRunningCommand("ps axl | grep -v grep | grep -q ' sendmail: '");
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // End of configuration. /////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- void
- SendmailIntegrate::SetOperatingSystem(std::string OperatingSystemType) {
-
- if ("OpenBSD" == OperatingSystemType) {
-
- SendmailSendmailMcPath = "/etc/mail/sendmail.mc";
- SendmailSendmailCfPath = "/etc/mail/sendmail.cf";
- ReloadMtaCommand = "/usr/local/sbin/postfix reload";
-
- FileToBackup.push_back(SendmailSendmailMcPath);
- FileToBackup.push_back(SendmailSendmailCfPath);
-
- } else if ("FreeBSD" == OperatingSystemType) {
-
- SendmailSendmailMcPath = "/etc/mail/sendmail.mc";
- SendmailSendmailCfPath = "/etc/mail/sendmail.cf";
- ReloadMtaCommand = "/usr/local/sbin/postfix reload";
-
- FileToBackup.push_back(SendmailSendmailMcPath);
- FileToBackup.push_back(SendmailSendmailCfPath);
-
- } else if ("Ubuntu" == OperatingSystemType) {
-
- SendmailSendmailMcPath = "/etc/mail/sendmail.mc";
- SendmailSendmailCfPath = "/etc/mail/sendmail.cf";
- BuildInstallSendmailCfFile = "(cd /etc/mail; make)";
- ReloadMtaCommand = "/usr/sbin/service sendmail reload";
-
- FileToBackup.push_back(SendmailSendmailMcPath);
- FileToBackup.push_back(SendmailSendmailCfPath);
-
- } else if ("RedHat" == OperatingSystemType) {
-
- SendmailSendmailMcPath = "/etc/mail/sendmail.mc";
- SendmailSendmailCfPath = "/etc/mail/sendmail.cf";
- BuildInstallSendmailCfFile = "(cd /etc/mail; make)";
- ReloadMtaCommand = "/sbin/service sendmail reload";
-
- FileToBackup.push_back(SendmailSendmailMcPath);
- FileToBackup.push_back(SendmailSendmailCfPath);
-
- } else if ("Suse" == OperatingSystemType) {
-
- SendmailSendmailMcPath = "/etc/mail/linux.mc";
- SendmailSendmailCfPath = "/etc/mail/sendmail.cf";
- BuildInstallSendmailCfFile = "(cd /etc/mail; rm sendmail.cf; m4 /etc/mail/linux.mc > sendmail.cf)";
- ReloadMtaCommand = "/usr/sbin/postfix reload";
-
- FileToBackup.push_back(SendmailSendmailMcPath);
- FileToBackup.push_back(SendmailSendmailCfPath);
-
- } else {
-
- std::ostringstream Temp;
-
- Temp << "***Error from SendmailIntegrate::SetOperatingSystem: Invalid value of OperatingSystemType: "
- << OperatingSystemType;
-
- throw std::runtime_error(Temp.str());
-
- }
-
- }
-
- void
- SendmailIntegrate::Integrate(FileBackup *SaveFile) {
-
- if (IsIntegrated()) {
-
- return;
-
- }
-
- if (Verbose()) {
-
- std::cout << "Add to sendmail file " << SendmailSendmailMcPath << ": '"
- << SnfMilterSendmailMcIntegrationString << "' and generate new "
- << SendmailSendmailCfPath << " file with the command '"
- << BuildInstallSendmailCfFile << "'...";
-
- }
-
- if (!Explain()) {
-
- for (FileToBackupType::iterator iFile = FileToBackup.begin(); iFile != FileToBackup.end(); iFile++) {
-
- SaveFile->CreateBackupFile(*iFile); // Save any existing file.
-
- }
-
- std::ofstream Output; // Append the configuration.
-
- Output.open(SendmailSendmailMcPath.c_str(), std::ios::app);
- if (!Output) {
- std::string Temp;
-
- Temp = "Error opening the sendmail configuration file " + SendmailSendmailMcPath;
- Temp += " for writing: ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- Output << SnfMilterSendmailMcIntegrationString << "\n";
- if (!Output) {
- std::string Temp;
-
- Temp = "Error appending to the sendmail configuration file " + SendmailSendmailMcPath;
- Temp += ": ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- Output.close();
- if (!Output) {
- std::string Temp;
-
- Temp = "Error closing the sendmail configuration file " + SendmailSendmailMcPath;
- Temp += " after appending: ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
-
- }
-
- if (std::system(BuildInstallSendmailCfFile.c_str()) != 0) {
-
- std::string Temp;
-
- Temp = "Error generating sendmail configuration file " + SendmailSendmailCfPath;
- throw std::runtime_error(Temp);
-
- }
-
- }
-
- OutputVerboseEnd();
-
- if (!ReloadMta()) {
-
- std::cerr << "Unable to reload the sendmail configuration. Please reload "
- << " the sendmail configuration for the integration with SNFMilter to take effect.";
-
- }
-
- }
-
- void
- SendmailIntegrate::Unintegrate(FileBackup *SaveFile) {
-
- if (!IsIntegrated()) {
-
- return;
-
- }
-
- std::ifstream Input;
-
- if (Verbose()) {
-
- std::cout << "Remove integration in sendmail file " << SendmailSendmailMcPath << "--\n";
-
- }
-
- if (!Explain()) {
-
- for (FileToBackupType::iterator iFile = FileToBackup.begin(); iFile != FileToBackup.end(); iFile++) {
-
- SaveFile->CreateBackupFile(*iFile); // Save any existing file.
-
- }
-
- Input.open(SendmailSendmailMcPath.c_str()); // Read the contents.
- if (!Input) {
- std::string Temp;
-
- Temp = "Error opening the sendmail configuration file " + SendmailSendmailMcPath;
- 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(SnfMilterSendmailMcSearchString)) { // 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 sendmail configuration file " + SendmailSendmailMcPath;
- Temp += ": ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- Input.close();
- if (Input.bad()) {
- std::string Temp;
-
- Temp = "Error closing the sendmail configuration file " + SendmailSendmailMcPath;
- Temp += " after reading: ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- std::ofstream Output; // Write the updated contents.
-
- Output.open(SendmailSendmailMcPath.c_str(), std::ios::trunc);
- if (!Output) {
- std::string Temp;
-
- Temp = "Error opening the sendmail configuration file " + SendmailSendmailMcPath;
- Temp += " for writing: ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- Output << Content;
- if (!Output) {
- std::string Temp;
-
- Temp = "Error writing the sendmail configuration file " + SendmailSendmailMcPath;
- Temp += ": ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- Output.close();
- if (!Output) {
- std::string Temp;
-
- Temp = "Error closing the sendmail configuration file " + SendmailSendmailMcPath;
- Temp += " after writing: ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- if (std::system(BuildInstallSendmailCfFile.c_str()) != 0) { // Rebuild and install the sendmail configuration file.
-
- std::string Temp;
-
- Temp = "Error generating sendmail configuration file " + SendmailSendmailCfPath;
- throw std::runtime_error(Temp);
-
- }
-
- }
-
- OutputVerboseEnd();
-
- if (!ReloadMta()) {
-
- std::cerr << "Unable to reload the sendmail configuration. Please run "
- << "'sendmail reload' for the integration with SNFMilter to take effect.";
-
- }
-
- }
-
- bool
- SendmailIntegrate::MtaIsRunningDetected() {
-
- if (Verbose()) {
-
- std::cout << "Checking whether sendmail 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
- SendmailIntegrate::ReloadMta() {
-
- if (!MtaIsRunningDetected()) {
-
- return true;
-
- }
-
- if (Verbose()) {
-
- std::cout << "Reloading sendmail with the command '"
- << ReloadMtaCommand << "'...\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
- SendmailIntegrate::IsIntegrated() {
-
- if (Verbose()) {
-
- std::cout << "Checking for any SNFMilter integration in the sendmail file " << SendmailSendmailMcPath << "...";
-
- }
-
- if (!FileExists(SendmailSendmailMcPath)) {
-
- if (Verbose()) {
-
- std::cout << "file doesn't exist; sendmail is not integrated...";
-
- }
-
- OutputVerboseEnd();
-
- return false;
-
- }
-
- bool Integrated = false;
-
- std::ifstream Input;
-
- Input.open(SendmailSendmailMcPath.c_str()); // Read the contents.
- if (!Input) {
- std::string Temp;
-
- Temp = "Error opening the sendmail configuration file " + SendmailSendmailMcPath;
- Temp += " for reading: ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- std::string Line;
-
- while (getline(Input, Line)) {
-
- if (std::string::npos != Line.find(SnfMilterSendmailMcSearchString)) { // Check for integration line.
-
- Integrated = true; // Found it.
-
- break;
-
- }
-
- }
-
- Input.close();
- if (Input.bad()) {
- std::string Temp;
-
- Temp = "Error closing the sendmail configuration file " + SendmailSendmailMcPath;
- 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;
-
- }
|