Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SNFMilterConfig.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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("-with=none");
  53. const string IntegrateWithPostfixKey("-with=postfix");
  54. const string IntegrateWithSendmailKey("-with=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 = " -with=postfix Integrate with postfix and start/reload postfix\n";
  68. ExclusiveCommandsHelp += " -with=sendmail Integrate with sendmail and start/reload sendmail\n";
  69. ExclusiveCommandsHelp += " (Not available on OpenBSD or FreeBSD)\n";
  70. ExclusiveCommandsHelp += " -with=none Remove any integration with all supported MTAs\n";
  71. cout
  72. << Version << endl
  73. << "Copyright (C) 2012, ARM Research Labs, LLC (www.armresearch.com)\n\n"
  74. << "Usage:\n\n"
  75. << "SNFMilterConfig "
  76. << UtilityConfig::HelpCommandLine(ExclusiveCommands) << "\n\n"
  77. << "SNFMilterConfig "
  78. << UtilityConfig::HelpDescription(ExclusiveCommandsHelp) << "\n"
  79. << "The configuration file name is:\n\n"
  80. << " " << DefaultConfigFile << "\n\n"
  81. << "If the above file doesn't exist, then it is copied from the following file:\n\n"
  82. << " " << SampleConfigFile << "\n\n"
  83. << "If integration with an MTA is specified, the MTA's configuration is reloaded "
  84. << "if the MTA 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. std::string OsType;
  99. OsType = GetOperatingSystemType(); // Check whether the platform is supported.
  100. if ( ("OpenBSD" == OsType) || ("FreeBSD" == OsType) ) {
  101. std::string Temp;
  102. Temp = "Integration with sendmail is not supported on " + OsType;
  103. Temp += ".\n";
  104. throw runtime_error(Temp);
  105. }
  106. Command = IntegrateWithSendmailCommand;
  107. NumCommandsFound++;
  108. } else if (OneInput == IntegrateWithNoneKey) {
  109. Command = IntegrateWithNoneCommand;
  110. NumCommandsFound++;
  111. } else {
  112. // Process command-line input by the base class.
  113. if (!ProcessCommandLineItem(OneInput)) {
  114. Command = UnknownCommand;
  115. return false; // Illegal input.
  116. }
  117. }
  118. }
  119. if (UpdateCredentialsSpecified()) {
  120. Command = UpdateCredentialsCommand;
  121. NumCommandsFound++;
  122. }
  123. if (SetupRepairSpecified()) {
  124. Command = SetupRepairCommand;
  125. NumCommandsFound++;
  126. }
  127. if (StartSnifferSpecified()) {
  128. Command = StartSnifferCommand;
  129. NumCommandsFound++;
  130. }
  131. if (StopSnifferSpecified()) {
  132. Command = StopSnifferCommand;
  133. NumCommandsFound++;
  134. }
  135. return ( (NumCommandsFound == 1) && CommandLineIsOkay() );
  136. }
  137. void
  138. SNFMilterConfig::ExecuteCommand() {
  139. Postfix.SetOperatingSystem(GetOperatingSystemType());
  140. Postfix.SetVerbose(Verbose());
  141. Postfix.SetExplain(Explain());
  142. Sendmail.SetOperatingSystem(GetOperatingSystemType());
  143. Sendmail.SetVerbose(Verbose());
  144. Sendmail.SetExplain(Explain());
  145. SetConfigFileName(DefaultConfigFile);
  146. if (SetupRepairCommand == Command) {
  147. CreateDefaultConfigFile(SampleConfigFile); // Create the file if it doesn't exist,
  148. // Set owner and mode in any case.
  149. }
  150. LoadConfig();
  151. LoadInfo(); // Load the file paths.
  152. LoadSocketInfo(); // Load the socket path.
  153. switch (Command) {
  154. case SetupRepairCommand:
  155. SetupRepair(SampleIdentityFile);
  156. SetupRepairSocketDir();
  157. break;
  158. case UpdateCredentialsCommand:
  159. UpdateRulebaseScriptCredentials();
  160. DownloadRulebase();
  161. UpdateIdentityFile();
  162. break;
  163. case IntegrateWithPostfixCommand:
  164. UnintegrateWithAllExcept("postfix");
  165. Postfix.Integrate(&SaveFile);
  166. break;
  167. case IntegrateWithSendmailCommand:
  168. UnintegrateWithAllExcept("sendmail");
  169. Sendmail.Integrate(&SaveFile);
  170. break;
  171. case IntegrateWithNoneCommand:
  172. UnintegrateWithAllExcept();
  173. break;
  174. case StartSnifferCommand:
  175. LoadCredentials();
  176. StartSniffer("snf-milter start", ApplicationName);
  177. break;
  178. case StopSnifferCommand:
  179. LoadCredentials();
  180. StopSniffer("snf-milter stop", ApplicationName);
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. void
  187. SNFMilterConfig::LoadSocketInfo() {
  188. std::string MilterElement = GetPlatformContents();
  189. ConfigurationData PlatformConfig(MilterElement.c_str(), MilterElement.length());
  190. ConfigurationElement SocketReader("milter");
  191. SocketReader
  192. .Element("socket")
  193. .Attribute("path", SocketFileName)
  194. .End("socket")
  195. .End("milter");
  196. SocketReader.interpret(PlatformConfig);
  197. }
  198. void
  199. SNFMilterConfig::SetupRepairSocketDir() {
  200. std::string SocketDir;
  201. std::string::size_type LastDirSepIndex = SocketFileName.rfind("/");
  202. SocketDir = ( (std::string::npos == LastDirSepIndex) ? SocketFileName : SocketFileName.substr(0, LastDirSepIndex));
  203. if (Verbose()) {
  204. cout << "Create the milter socket directory " << SocketDir << "...";
  205. }
  206. if (!Explain()) {
  207. if (!FileExists(SocketDir)) {
  208. MkDir(SocketDir);
  209. }
  210. SetMode(SocketDir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP);
  211. SetOwnerGroup(SocketDir);
  212. }
  213. OutputVerboseEnd();
  214. }
  215. void
  216. SNFMilterConfig::SaveFileState() {
  217. if (!Explain()) {
  218. SaveFile.CreateBackupFile(GetRulebaseScriptName());
  219. if (UpdateCredentialsSpecified()) {
  220. SaveFile.CreateBackupFile(GetRulebaseFileName());
  221. }
  222. SaveFile.CreateBackupFile(GetIdentityFileName());
  223. SaveFile.CreateBackupFile(GetIgnoreListFileName());
  224. }
  225. }
  226. #if 0
  227. void
  228. SNFMilterConfig::DoIntegrationCommand() {
  229. switch (Command) {
  230. case NoCommand:
  231. break;
  232. case IntegrateWithNoneCommand:
  233. UnintegrateWithAllExcept();
  234. break;
  235. case IntegrateWithPostfixCommand:
  236. UnintegrateWithAllExcept("postfix");
  237. Postfix.Integrate(&SaveFile);
  238. break;
  239. case IntegrateWithSendmailCommand:
  240. UnintegrateWithAllExcept("sendmail");
  241. Sendmail.Integrate(&SaveFile);
  242. break;
  243. default:
  244. {
  245. ostringstream Temp;
  246. Temp << "Internal error in SNFMilterConfig::DoIntegrationCommand: Invalid value of command: "
  247. << Command;
  248. throw runtime_error(Temp.str());
  249. }
  250. }
  251. }
  252. #endif
  253. void
  254. SNFMilterConfig::UnintegrateWithAllExcept(std::string Except) {
  255. if (Except != "postfix") {
  256. Postfix.Unintegrate(&SaveFile);
  257. }
  258. if (Except != "sendmail") {
  259. Sendmail.Unintegrate(&SaveFile);
  260. }
  261. }