Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SNFMilterConfig.cpp 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // /file SNFMilterConfig.cpp
  2. //
  3. // Copyright (C) 2011, ARM Research Labs, LLC.
  4. // See www.armresearch.com for the copyright terms.
  5. //
  6. // This file contains the functions for SNFMilterConfig.
  7. //
  8. // $Id$
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////////////////////////
  11. #include <errno.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/types.h>
  15. #include <pwd.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <unistd.h>
  19. #include <exception>
  20. #include <stdexcept>
  21. #include <sstream>
  22. #include <iostream>
  23. #include <fstream>
  24. #include <vector>
  25. #include "SNFMilterConfig.hpp"
  26. using namespace std;
  27. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
  29. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  30. // Initialize default configuration file path.
  31. #ifdef WIN
  32. // Windows OS.
  33. const std::string SNFMilterConfig::DefaultConfigFile("C:\\SNF\\SNFMilter.xml");
  34. const std::string SNFMilterConfig::SampleConfigFile("C:\\SNF\\SNFMilter.xml.sample");
  35. const std::string SNFMilterConfig::SampleIdentityFile("C:\\SNF\\identity.xml.sample");
  36. #else
  37. #ifdef DEFAULT_CONFIG_DIR
  38. // *nix, DEFAULT_CONFIG_DIR is specified on the compile command line.
  39. const std::string SNFMilterConfig::DefaultConfigFile(DEFAULT_CONFIG_DIR "/snf-milter/SNFMilter.xml");
  40. const std::string SNFMilterConfig::SampleConfigFile(DEFAULT_CONFIG_DIR "/snf-milter/SNFMilter.xml.sample");
  41. const std::string SNFMilterConfig::SampleIdentityFile(DEFAULT_CONFIG_DIR "/snf-milter/identity.xml.sample");
  42. #else
  43. // Not Windows, and DEFAULT_CONFIG_DIR is not specified on the compile
  44. // command line. In this case, we don't know the default path for the
  45. // configuration file.
  46. const std::string SNFMilterConfig::DefaultConfigFile("");
  47. const std::string SNFMilterConfig::SampleConfigFile("");
  48. const std::string SNFMilterConfig::SampleIdentityFile("");
  49. #endif
  50. #endif
  51. const string IntegrateWithNoneKey("-mta=none");
  52. const string IntegrateWithPostfixKey("-mta=postfix");
  53. const string IntegrateWithSendmailKey("-mta=sendmail");
  54. const string SnfMilterMainCfSearchString("Added by SNFMilterConfig");
  55. const string SnfMilterMainCfIntegrationString("smtpd_milters = unix:/var/snf-milter/socket $smtpd_milters # Added by SNFMilterConfig");
  56. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  58. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  59. void
  60. SNFMilterConfig::DisplayHelp(std::string Version) {
  61. cout
  62. << Version << endl
  63. << "Copyright (C) 2012, ARM Research Labs, LLC (www.armresearch.com)\n\n"
  64. << "Usage:\n\n"
  65. << "SNFMilterConfig "
  66. << IntegrateWithPostfixKey << " | "
  67. << IntegrateWithSendmailKey << " | "
  68. << IntegrateWithNoneKey << " "
  69. << UtilityConfig::HelpCommandLine() << "\n\n"
  70. << "SNFMilterConfig creates the configuration files (snf-config-file and the\n"
  71. << "ignore list file), the rulebase download script (default: getRulebase) if\n"
  72. << "they don't exist, and also the identity file.\n\n"
  73. << " -mta=postfix Integrate with postfix\n"
  74. << " -mta=sendmail Integrate with sendmail\n"
  75. << " -mta=none Remove any integration with all supported MTAs\n"
  76. << UtilityConfig::HelpDescription() << "\n"
  77. << "If snf-config-file is not specified, then the following file is used if it exists:\n\n"
  78. << " " << DefaultConfigFile << "\n\n"
  79. << "If snf-config-file is not specified and the above file doesn't exist, then it is\n"
  80. << "copied from the following file:\n\n"
  81. << " " << SampleConfigFile << "\n\n";
  82. };
  83. bool
  84. SNFMilterConfig::GetCommandLineInput(int argc, char* argv[]) {
  85. int i;
  86. int NumCommandsFound = 0;
  87. string OneInput;
  88. MtaCommand = NoCommand; // Default is to do nothing.
  89. for (i = 1; i < argc; i++) { // Check each input.
  90. OneInput = argv[i];
  91. if (OneInput == IntegrateWithNoneKey) {
  92. MtaCommand = IntegrateWithNoneCmd;
  93. NumCommandsFound++;
  94. } else if (OneInput == IntegrateWithPostfixKey) {
  95. MtaCommand = IntegrateWithPostfixCmd;
  96. NumCommandsFound++;
  97. } else if (0 == OneInput.find(IntegrateWithSendmailKey)) {
  98. MtaCommand = IntegrateWithSendmailCmd;
  99. NumCommandsFound++;
  100. } else {
  101. // Process command-line input by the base class.
  102. if (!ProcessCommandLineItem(OneInput)) {
  103. return false; // Illegal input.
  104. }
  105. }
  106. }
  107. return (NumCommandsFound == 1);
  108. }
  109. void
  110. SNFMilterConfig::LoadSocketInfo() {
  111. std::string MilterElement = GetPlatformContents();
  112. ConfigurationData PlatformConfig(MilterElement.c_str(), MilterElement.length());
  113. ConfigurationElement SocketReader("milter");
  114. SocketReader
  115. .Element("socket")
  116. .Attribute("path", SocketFileName)
  117. .End("socket")
  118. .End("milter");
  119. SocketReader.interpret(PlatformConfig);
  120. }
  121. void
  122. SNFMilterConfig::CreateSocketDir() {
  123. std::string SocketDir;
  124. std::string::size_type LastDirSepIndex = SocketFileName.rfind("/");
  125. SocketDir = ( (std::string::npos == LastDirSepIndex) ? SocketFileName : SocketFileName.substr(0, LastDirSepIndex));
  126. if (Verbose()) {
  127. cout << "Create the milter socket directory " << SocketDir << "...";
  128. }
  129. if (!Explain()) {
  130. if (!FileExists(SocketDir)) {
  131. MkDir(SocketDir);
  132. }
  133. SetMode(SocketDir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP);
  134. SetOwnerGroup(SocketDir);
  135. }
  136. OutputVerboseEnd();
  137. }
  138. void
  139. SNFMilterConfig::CreateLoadConfig() {
  140. CheckAndSetConfigFileName(&DefaultConfigFile, 1); // Load the config file name.
  141. if (!Explain()) {
  142. SaveFile.CreateBackupFile(GetConfigFileName());
  143. }
  144. CreateDefaultConfigFile(SampleConfigFile); // Create the file if it doesn't exist.
  145. LoadConfig();
  146. LoadInfo(); // Load the file paths.
  147. LoadSocketInfo(); // Load the socket path.
  148. }
  149. void
  150. SNFMilterConfig::SaveFileState() {
  151. if (!Explain()) {
  152. SaveFile.CreateBackupFile(GetRulebaseScriptName());
  153. if (UpdateCredentialsSpecified()) {
  154. SaveFile.CreateBackupFile(GetRulebaseFileName());
  155. }
  156. SaveFile.CreateBackupFile(GetIdentityFileName());
  157. SaveFile.CreateBackupFile(GetIgnoreListFileName());
  158. }
  159. }
  160. void
  161. SNFMilterConfig::UpdateConfigFiles() {
  162. SetMode(GetConfigFileName(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); // Set permissions.
  163. SetOwnerGroup(GetConfigFileName()); // Set to sniffer user.
  164. CreateDefaultIdentityFile(SampleIdentityFile);
  165. CreateUpdateRulebaseScript();
  166. UpdateLogDir();
  167. UpdateIgnoreListFile();
  168. }
  169. void
  170. SNFMilterConfig::DoIntegrationCommand() {
  171. switch (MtaCommand) {
  172. case NoCommand:
  173. break;
  174. case IntegrateWithNoneCmd:
  175. UnintegrateWithAllExcept();
  176. break;
  177. case IntegrateWithPostfixCmd:
  178. UnintegrateWithAllExcept("postfix");
  179. Postfix.Integrate(&SaveFile);
  180. break;
  181. case IntegrateWithSendmailCmd:
  182. UnintegrateWithAllExcept("sendmail");
  183. // Sendmail.Integrate(&SaveFile);
  184. break;
  185. default:
  186. {
  187. ostringstream Temp;
  188. Temp << "Internal error in SNFMilterConfig::DoIntegrationCommand: Invalid value of command: "
  189. << MtaCommand;
  190. throw runtime_error(Temp.str());
  191. }
  192. }
  193. }
  194. void
  195. SNFMilterConfig::UnintegrateWithAllExcept(std::string Except) {
  196. if (Except != "postfix") {
  197. Postfix.Unintegrate(&SaveFile);
  198. }
  199. #if 0
  200. if (Except != "sendmail") {
  201. Sendmail.Unintegrate(&SaveFile);
  202. }
  203. #endif
  204. }