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 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 SNFMilterConfig::ApplicationName("SNFMilter");
  52. const string IntegrateWithNoneKey("-mta=none");
  53. const string IntegrateWithPostfixKey("-mta=postfix");
  54. const string IntegrateWithSendmailKey("-mta=sendmail");
  55. const string SnfMilterMainCfSearchString("Added by SNFMilterConfig");
  56. const string SnfMilterMainCfIntegrationString("smtpd_milters = unix:/var/snf-milter/socket $smtpd_milters # Added by SNFMilterConfig");
  57. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  59. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  60. void
  61. SNFMilterConfig::DisplayHelp(std::string Version) {
  62. std::string ExclusiveCommands;
  63. std::string ExclusiveCommandsHelp;
  64. ExclusiveCommands = IntegrateWithPostfixKey + " | ";
  65. ExclusiveCommands += IntegrateWithSendmailKey + " | ";
  66. ExclusiveCommands += IntegrateWithNoneKey;
  67. ExclusiveCommandsHelp = " -mta=postfix Integrate with postfix and start/reload postfix\n";
  68. ExclusiveCommandsHelp += " -mta=sendmail Integrate with sendmail and start/reload sendmail\n";
  69. ExclusiveCommandsHelp += " -mta=none Remove any integration with all supported MTAs\n";
  70. cout
  71. << Version << endl
  72. << "Copyright (C) 2012, ARM Research Labs, LLC (www.armresearch.com)\n\n"
  73. << "Usage:\n\n"
  74. << "SNFMilterConfig "
  75. << UtilityConfig::HelpCommandLine(ExclusiveCommands) << "\n\n"
  76. << "SNFMilterConfig "
  77. << UtilityConfig::HelpDescription(ExclusiveCommandsHelp) << "\n"
  78. << "If snf-config-file is not specified, then the following file is used if it exists:\n\n"
  79. << " " << DefaultConfigFile << "\n\n"
  80. << "If snf-config-file is not specified and the above file doesn't exist, then it is\n"
  81. << "copied from the following file:\n\n"
  82. << " " << SampleConfigFile << "\n\n"
  83. << "If integration with an MTA is specified, that MTA is started if it isn't running,\n"
  84. << "or the MTA's configuration is reloaded if it is running.\n\n";
  85. };
  86. bool
  87. SNFMilterConfig::GetCommandLineInput(int argc, char* argv[]) {
  88. int i;
  89. int NumCommandsFound = 0;
  90. string OneInput;
  91. Command = NoCommand; // Default is to do nothing.
  92. for (i = 1; i < argc; i++) { // Check each input.
  93. OneInput = argv[i];
  94. if (OneInput == IntegrateWithPostfixKey) {
  95. Command = IntegrateWithPostfixCommand;
  96. NumCommandsFound++;
  97. } else if (0 == OneInput.find(IntegrateWithSendmailKey)) {
  98. Command = IntegrateWithSendmailCommand;
  99. NumCommandsFound++;
  100. } else if (OneInput == IntegrateWithNoneKey) {
  101. Command = IntegrateWithNoneCommand;
  102. NumCommandsFound++;
  103. } else {
  104. // Process command-line input by the base class.
  105. if (!ProcessCommandLineItem(OneInput)) {
  106. Command = UnknownCommand;
  107. return false; // Illegal input.
  108. }
  109. }
  110. }
  111. if (UpdateCredentialsSpecified()) {
  112. Command = UpdateCredentialsCommand;
  113. NumCommandsFound++;
  114. }
  115. if (SetupRepairSpecified()) {
  116. Command = SetupRepairCommand;
  117. NumCommandsFound++;
  118. }
  119. if (StartSnifferSpecified()) {
  120. Command = StartSnifferCommand;
  121. NumCommandsFound++;
  122. }
  123. if (StopSnifferSpecified()) {
  124. Command = StopSnifferCommand;
  125. NumCommandsFound++;
  126. }
  127. return (NumCommandsFound == 1);
  128. }
  129. void
  130. SNFMilterConfig::ExecuteCommand() {
  131. switch (Command) {
  132. case SetupRepairCommand:
  133. CreateLoadConfig(); // Save config file state create default
  134. // config if necessary, and load config.
  135. SetupRepair(SampleIdentityFile);
  136. SetupRepairSocketDir();
  137. break;
  138. case UpdateCredentialsCommand:
  139. UpdateRulebaseScriptCredentials();
  140. DownloadRulebase();
  141. UpdateIdentityFile();
  142. break;
  143. case IntegrateWithPostfixCommand:
  144. break;
  145. case IntegrateWithSendmailCommand:
  146. break;
  147. case IntegrateWithNoneCommand:
  148. break;
  149. case StartSnifferCommand:
  150. StartSniffer("snf-milter start", ApplicationName);
  151. break;
  152. case StopSnifferCommand:
  153. StopSniffer("snf-milter stop", ApplicationName);
  154. break;
  155. default:
  156. break;
  157. }
  158. }
  159. void
  160. SNFMilterConfig::LoadSocketInfo() {
  161. std::string MilterElement = GetPlatformContents();
  162. ConfigurationData PlatformConfig(MilterElement.c_str(), MilterElement.length());
  163. ConfigurationElement SocketReader("milter");
  164. SocketReader
  165. .Element("socket")
  166. .Attribute("path", SocketFileName)
  167. .End("socket")
  168. .End("milter");
  169. SocketReader.interpret(PlatformConfig);
  170. }
  171. void
  172. SNFMilterConfig::SetupRepairSocketDir() {
  173. std::string SocketDir;
  174. std::string::size_type LastDirSepIndex = SocketFileName.rfind("/");
  175. SocketDir = ( (std::string::npos == LastDirSepIndex) ? SocketFileName : SocketFileName.substr(0, LastDirSepIndex));
  176. if (Verbose()) {
  177. cout << "Create the milter socket directory " << SocketDir << "...";
  178. }
  179. if (!Explain()) {
  180. if (!FileExists(SocketDir)) {
  181. MkDir(SocketDir);
  182. }
  183. SetMode(SocketDir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP);
  184. SetOwnerGroup(SocketDir);
  185. }
  186. OutputVerboseEnd();
  187. }
  188. void
  189. SNFMilterConfig::CreateLoadConfig() {
  190. CheckAndSetConfigFileName(&DefaultConfigFile, 1); // Load the config file name.
  191. CreateDefaultConfigFile(SampleConfigFile); // Create the file if it doesn't exist,
  192. // Set owner and mode in any case.
  193. LoadConfig();
  194. LoadInfo(); // Load the file paths.
  195. LoadSocketInfo(); // Load the socket path.
  196. }
  197. void
  198. SNFMilterConfig::SaveFileState() {
  199. if (!Explain()) {
  200. SaveFile.CreateBackupFile(GetRulebaseScriptName());
  201. if (UpdateCredentialsSpecified()) {
  202. SaveFile.CreateBackupFile(GetRulebaseFileName());
  203. }
  204. SaveFile.CreateBackupFile(GetIdentityFileName());
  205. SaveFile.CreateBackupFile(GetIgnoreListFileName());
  206. }
  207. }
  208. void
  209. SNFMilterConfig::DoIntegrationCommand() {
  210. switch (Command) {
  211. case NoCommand:
  212. break;
  213. case IntegrateWithNoneCommand:
  214. UnintegrateWithAllExcept();
  215. break;
  216. case IntegrateWithPostfixCommand:
  217. UnintegrateWithAllExcept("postfix");
  218. Postfix.Integrate(&SaveFile);
  219. break;
  220. case IntegrateWithSendmailCommand:
  221. UnintegrateWithAllExcept("sendmail");
  222. // Sendmail.Integrate(&SaveFile);
  223. break;
  224. default:
  225. {
  226. ostringstream Temp;
  227. Temp << "Internal error in SNFMilterConfig::DoIntegrationCommand: Invalid value of command: "
  228. << Command;
  229. throw runtime_error(Temp.str());
  230. }
  231. }
  232. }
  233. void
  234. SNFMilterConfig::UnintegrateWithAllExcept(std::string Except) {
  235. if (Except != "postfix") {
  236. Postfix.Unintegrate(&SaveFile);
  237. }
  238. #if 0
  239. if (Except != "sendmail") {
  240. Sendmail.Unintegrate(&SaveFile);
  241. }
  242. #endif
  243. }