Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. std::string ExclusiveCommands;
  62. std::string ExclusiveCommandsHelp;
  63. ExclusiveCommands = IntegrateWithPostfixKey + " | ";
  64. ExclusiveCommands += IntegrateWithSendmailKey + " | ";
  65. ExclusiveCommands += IntegrateWithNoneKey;
  66. ExclusiveCommandsHelp = " -mta=postfix Integrate with postfix\n";
  67. ExclusiveCommandsHelp += " -mta=sendmail Integrate with sendmail\n";
  68. ExclusiveCommandsHelp += " -mta=none Remove any integration with all supported MTAs\n";
  69. cout
  70. << Version << endl
  71. << "Copyright (C) 2012, ARM Research Labs, LLC (www.armresearch.com)\n\n"
  72. << "Usage:\n\n"
  73. << "SNFMilterConfig "
  74. << UtilityConfig::HelpCommandLine(ExclusiveCommands) << "\n\n"
  75. << "SNFMilterConfig "
  76. << UtilityConfig::HelpDescription(ExclusiveCommandsHelp) << "\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. Command = NoCommand; // Default is to do nothing.
  89. for (i = 1; i < argc; i++) { // Check each input.
  90. OneInput = argv[i];
  91. if (OneInput == IntegrateWithPostfixKey) {
  92. Command = IntegrateWithPostfixCommand;
  93. NumCommandsFound++;
  94. } else if (0 == OneInput.find(IntegrateWithSendmailKey)) {
  95. Command = IntegrateWithSendmailCommand;
  96. NumCommandsFound++;
  97. } else if (OneInput == IntegrateWithNoneKey) {
  98. Command = IntegrateWithNoneCommand;
  99. NumCommandsFound++;
  100. } else {
  101. // Process command-line input by the base class.
  102. if (!ProcessCommandLineItem(OneInput)) {
  103. Command = UnknownCommand;
  104. return false; // Illegal input.
  105. }
  106. }
  107. }
  108. if (UpdateCredentialsSpecified()) {
  109. Command = UpdateCredentialsCommand;
  110. NumCommandsFound++;
  111. }
  112. if (SetupRepairSpecified()) {
  113. Command = SetupRepairCommand;
  114. NumCommandsFound++;
  115. }
  116. if (StartSnifferSpecified()) {
  117. Command = StartSnifferCommand;
  118. NumCommandsFound++;
  119. }
  120. if (StopSnifferSpecified()) {
  121. Command = StopSnifferCommand;
  122. NumCommandsFound++;
  123. }
  124. return (NumCommandsFound == 1);
  125. }
  126. void
  127. SNFMilterConfig::ExecuteCommand() {
  128. switch (Command) {
  129. case SetupRepairCommand:
  130. CreateLoadConfig(); // Save config file state create default
  131. // config if necessary, and load config.
  132. SetupRepair(SampleIdentityFile);
  133. SetupRepairSocketDir();
  134. break;
  135. case UpdateCredentialsCommand:
  136. UpdateRulebaseScriptCredentials();
  137. DownloadRulebase();
  138. UpdateIdentityFile();
  139. break;
  140. case IntegrateWithPostfixCommand:
  141. break;
  142. case IntegrateWithSendmailCommand:
  143. break;
  144. case IntegrateWithNoneCommand:
  145. break;
  146. case StartSnifferCommand:
  147. break;
  148. case StopSnifferCommand:
  149. break;
  150. default:
  151. break;
  152. }
  153. #if 0
  154. SnifferRunningStatue RunningStatus;
  155. #endif
  156. SnfMilterConfig.CreateLoadConfig(); // Save config file state and load config.
  157. // Load the default if necessary.
  158. #if 0
  159. SnfMilterConfig.SaveFileState(); // Save state of all other files.
  160. RunningStatus = SnfMilterConfig.GetSnifferRunningStatus(); // Get state before changing anything.
  161. #endif
  162. SnfMilterConfig.UpdateConfigFiles(); // Create/update config files
  163. SnfMilterConfig.CreateUpdateRulebaseScript(); // Create/update GetRulebase.
  164. SnfMilterConfig.DownloadRulebase(); // Download rulebase.
  165. SnfMilterConfig.UpdateIdentityFile(); // Update Identity file with credentials,
  166. // if credentials were specified.
  167. SnfMilterConfig.DoIntegrationCommand(); // Integrate/unintegrate.
  168. #if 0
  169. if (SnifferIsStopped == RunningStatus) {
  170. SnfMilterConfig.StartSniffer();
  171. } else if (SnifferRunningStatusIsUknown == RunningStatus) {
  172. std::cout << "SNFMilterConfig: Unable to determine whether SNFMilter is running.\n"
  173. << "Please start SNFMilter if it isn't running.\n";
  174. }
  175. #else
  176. SnfMilterConfig.StartSniffer("snf-milter");
  177. #endif
  178. }
  179. void
  180. SNFMilterConfig::LoadSocketInfo() {
  181. std::string MilterElement = GetPlatformContents();
  182. ConfigurationData PlatformConfig(MilterElement.c_str(), MilterElement.length());
  183. ConfigurationElement SocketReader("milter");
  184. SocketReader
  185. .Element("socket")
  186. .Attribute("path", SocketFileName)
  187. .End("socket")
  188. .End("milter");
  189. SocketReader.interpret(PlatformConfig);
  190. }
  191. void
  192. SNFMilterConfig::SetupRepairSocketDir() {
  193. std::string SocketDir;
  194. std::string::size_type LastDirSepIndex = SocketFileName.rfind("/");
  195. SocketDir = ( (std::string::npos == LastDirSepIndex) ? SocketFileName : SocketFileName.substr(0, LastDirSepIndex));
  196. if (Verbose()) {
  197. cout << "Create the milter socket directory " << SocketDir << "...";
  198. }
  199. if (!Explain()) {
  200. if (!FileExists(SocketDir)) {
  201. MkDir(SocketDir);
  202. }
  203. SetMode(SocketDir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP);
  204. SetOwnerGroup(SocketDir);
  205. }
  206. OutputVerboseEnd();
  207. }
  208. void
  209. SNFMilterConfig::CreateLoadConfig() {
  210. CheckAndSetConfigFileName(&DefaultConfigFile, 1); // Load the config file name.
  211. CreateDefaultConfigFile(SampleConfigFile); // Create the file if it doesn't exist,
  212. // Set owner and mode in any case.
  213. LoadConfig();
  214. LoadInfo(); // Load the file paths.
  215. LoadSocketInfo(); // Load the socket path.
  216. }
  217. void
  218. SNFMilterConfig::SaveFileState() {
  219. if (!Explain()) {
  220. SaveFile.CreateBackupFile(GetRulebaseScriptName());
  221. if (UpdateCredentialsSpecified()) {
  222. SaveFile.CreateBackupFile(GetRulebaseFileName());
  223. }
  224. SaveFile.CreateBackupFile(GetIdentityFileName());
  225. SaveFile.CreateBackupFile(GetIgnoreListFileName());
  226. }
  227. }
  228. void
  229. SNFMilterConfig::DoIntegrationCommand() {
  230. switch (Command) {
  231. case NoCommand:
  232. break;
  233. case IntegrateWithNoneCommand:
  234. UnintegrateWithAllExcept();
  235. break;
  236. case IntegrateWithPostfixCommand:
  237. UnintegrateWithAllExcept("postfix");
  238. Postfix.Integrate(&SaveFile);
  239. break;
  240. case IntegrateWithSendmailCommand:
  241. UnintegrateWithAllExcept("sendmail");
  242. // Sendmail.Integrate(&SaveFile);
  243. break;
  244. default:
  245. {
  246. ostringstream Temp;
  247. Temp << "Internal error in SNFMilterConfig::DoIntegrationCommand: Invalid value of command: "
  248. << Command;
  249. throw runtime_error(Temp.str());
  250. }
  251. }
  252. }
  253. void
  254. SNFMilterConfig::UnintegrateWithAllExcept(std::string Except) {
  255. if (Except != "postfix") {
  256. Postfix.Unintegrate(&SaveFile);
  257. }
  258. #if 0
  259. if (Except != "sendmail") {
  260. Sendmail.Unintegrate(&SaveFile);
  261. }
  262. #endif
  263. }