123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- // /file SNFMilterConfig.cpp
- //
- // Copyright (C) 2011, ARM Research Labs, LLC.
- // See www.armresearch.com for the copyright terms.
- //
- // This file contains the functions for SNFMilterConfig.
- //
- // $Id$
- //
- ///////////////////////////////////////////////////////////////////////////////////////////////////
-
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <pwd.h>
-
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
-
- #include <exception>
- #include <stdexcept>
- #include <sstream>
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include "SNFMilterConfig.hpp"
-
- using namespace std;
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- // Initialize default configuration file path.
- #ifdef WIN
-
- // Windows OS.
- const std::string SNFMilterConfig::DefaultConfigFile("C:\\SNF\\SNFMilter.xml");
- const std::string SNFMilterConfig::SampleConfigFile("C:\\SNF\\SNFMilter.xml.sample");
- const std::string SNFMilterConfig::SampleIdentityFile("C:\\SNF\\identity.xml.sample");
-
- #else
-
- #ifdef DEFAULT_CONFIG_DIR
- // *nix, DEFAULT_CONFIG_DIR is specified on the compile command line.
- const std::string SNFMilterConfig::DefaultConfigFile(DEFAULT_CONFIG_DIR "/snf-milter/SNFMilter.xml");
- const std::string SNFMilterConfig::SampleConfigFile(DEFAULT_CONFIG_DIR "/snf-milter/SNFMilter.xml.sample");
- const std::string SNFMilterConfig::SampleIdentityFile(DEFAULT_CONFIG_DIR "/snf-milter/identity.xml.sample");
- #else
- // Not Windows, and DEFAULT_CONFIG_DIR is not specified on the compile
- // command line. In this case, we don't know the default path for the
- // configuration file.
- const std::string SNFMilterConfig::DefaultConfigFile("");
- const std::string SNFMilterConfig::SampleConfigFile("");
- const std::string SNFMilterConfig::SampleIdentityFile("");
- #endif
-
- #endif
-
- const string IntegrateWithNoneKey("-mta=none");
- const string IntegrateWithPostfixKey("-mta=postfix");
- const string IntegrateWithSendmailKey("-mta=sendmail");
-
- const string SnfMilterMainCfSearchString("Added by SNFMilterConfig");
-
- const string SnfMilterMainCfIntegrationString("smtpd_milters = unix:/var/snf-milter/socket $smtpd_milters # Added by SNFMilterConfig");
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // End of configuration. /////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- void
- SNFMilterConfig::DisplayHelp(std::string Version) {
-
- cout
- << Version << endl
- << "Copyright (C) 2012, ARM Research Labs, LLC (www.armresearch.com)\n\n"
- << "Usage:\n\n"
- << "SNFMilterConfig "
- << IntegrateWithPostfixKey << " | "
- << IntegrateWithSendmailKey << " | "
- << IntegrateWithNoneKey << " "
- << UtilityConfig::HelpCommandLine() << "\n\n"
- << "SNFMilterConfig creates the configuration files (snf-config-file and the\n"
- << "ignore list file), the rulebase download script (default: getRulebase) if\n"
- << "they don't exist, and also the identity file.\n\n"
- << " -mta=postfix Integrate with postfix\n"
- << " -mta=sendmail Integrate with sendmail\n"
- << " -mta=none Remove any integration with all supported MTAs\n"
- << UtilityConfig::HelpDescription() << "\n"
- << "If snf-config-file is not specified, then the following file is used if it exists:\n\n"
- << " " << DefaultConfigFile << "\n\n"
- << "If snf-config-file is not specified and the above file doesn't exist, then it is\n"
- << "copied from the following file:\n\n"
- << " " << SampleConfigFile << "\n\n";
-
- };
-
- bool
- SNFMilterConfig::GetCommandLineInput(int argc, char* argv[]) {
-
- int i;
- int NumCommandsFound = 0;
- string OneInput;
-
- MtaCommand = NoCommand; // Default is to do nothing.
-
- for (i = 1; i < argc; i++) { // Check each input.
-
- OneInput = argv[i];
-
- if (OneInput == IntegrateWithNoneKey) {
-
- MtaCommand = IntegrateWithNoneCmd;
- NumCommandsFound++;
-
- } else if (OneInput == IntegrateWithPostfixKey) {
-
- MtaCommand = IntegrateWithPostfixCmd;
- NumCommandsFound++;
-
- } else if (0 == OneInput.find(IntegrateWithSendmailKey)) {
-
- MtaCommand = IntegrateWithSendmailCmd;
- NumCommandsFound++;
-
- } else {
-
- // Process command-line input by the base class.
- if (!ProcessCommandLineItem(OneInput)) {
-
- return false; // Illegal input.
-
- }
-
- }
-
- }
-
- return (NumCommandsFound == 1);
-
- }
-
- void
- SNFMilterConfig::LoadSocketInfo() {
-
- std::string MilterElement = GetPlatformContents();
-
- ConfigurationData PlatformConfig(MilterElement.c_str(), MilterElement.length());
-
- ConfigurationElement SocketReader("milter");
-
- SocketReader
- .Element("socket")
- .Attribute("path", SocketFileName)
- .End("socket")
- .End("milter");
-
- SocketReader.interpret(PlatformConfig);
-
- }
-
- void
- SNFMilterConfig::CreateSocketDir() {
-
- std::string SocketDir;
-
- std::string::size_type LastDirSepIndex = SocketFileName.rfind("/");
-
- SocketDir = ( (std::string::npos == LastDirSepIndex) ? SocketFileName : SocketFileName.substr(0, LastDirSepIndex));
-
- if (Verbose()) {
-
- cout << "Create the milter socket directory " << SocketDir << "...";
-
- }
-
- if (!Explain()) {
-
- if (!FileExists(SocketDir)) {
-
- MkDir(SocketDir);
-
- }
-
- SetMode(SocketDir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP);
- SetOwnerGroup(SocketDir);
-
- }
-
- OutputVerboseEnd();
-
- }
-
- void
- SNFMilterConfig::CreateLoadConfig() {
-
- CheckAndSetConfigFileName(&DefaultConfigFile, 1); // Load the config file name.
-
- if (!Explain()) {
-
- SaveFile.CreateBackupFile(GetConfigFileName());
-
- }
-
- CreateDefaultConfigFile(SampleConfigFile); // Create the file if it doesn't exist.
-
- LoadConfig();
- LoadInfo(); // Load the file paths.
- LoadSocketInfo(); // Load the socket path.
-
- }
-
- void
- SNFMilterConfig::SaveFileState() {
-
- if (!Explain()) {
-
- SaveFile.CreateBackupFile(GetRulebaseScriptName());
- SaveFile.CreateBackupFile(GetRulebaseFileName());
- SaveFile.CreateBackupFile(GetIdentityFileName());
- SaveFile.CreateBackupFile(GetIgnoreListFileName());
-
- }
-
- }
-
- void
- SNFMilterConfig::UpdateConfigFiles() {
-
- SetMode(GetConfigFileName(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); // Set permissions.
- SetOwnerGroup(GetConfigFileName()); // Set to sniffer user.
-
- CreateDefaultIdentityFile(SampleIdentityFile);
- CreateUpdateRulebaseScript();
-
- UpdateLogDir();
- UpdateIgnoreListFile();
-
- }
-
- void
- SNFMilterConfig::DoIntegrationCommand() {
-
- switch (MtaCommand) {
-
- case NoCommand:
- break;
-
- case IntegrateWithNoneCmd:
- UnintegrateWithAllExcept();
- break;
-
- case IntegrateWithPostfixCmd:
- UnintegrateWithAllExcept("postfix");
- Postfix.Integrate(&SaveFile);
- break;
-
- case IntegrateWithSendmailCmd:
- UnintegrateWithAllExcept("sendmail");
- // Sendmail.Integrate(&SaveFile);
- break;
-
- default:
-
- {
- ostringstream Temp;
-
- Temp << "Internal error in SNFMilterConfig::DoIntegrationCommand: Invalid value of command: "
- << MtaCommand;
-
- throw runtime_error(Temp.str());
-
- }
-
- }
-
- }
-
- void
- SNFMilterConfig::UnintegrateWithAllExcept(std::string Except) {
-
- if (Except != "postfix") {
-
- Postfix.Unintegrate(&SaveFile);
-
- }
- #if 0
- if (Except != "sendmail") {
-
- Sendmail.Unintegrate(&SaveFile);
-
- }
- #endif
-
- }
|