Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SNFMilterConfig.cpp 9.9KB

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