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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 std::string SNFMilterConfig::DefaultSocketFileName("/var/snf-milter/socket");
  56. const std::string SNFMilterConfig::DefaultPostfixIsChrootedSocketFileName("/var/spool/postfix/snf-milter/socket");
  57. const string IntegrateWithNoneKey("-with=none");
  58. const string IntegrateWithPostfixKey("-with=postfix");
  59. const string IntegrateWithSendmailKey("-with=sendmail");
  60. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  61. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  62. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. void
  64. SNFMilterConfig::DisplayHelp(std::string Version) {
  65. std::string ExclusiveCommands;
  66. std::string ExclusiveCommandsHelp;
  67. ExclusiveCommands = IntegrateWithPostfixKey + " | ";
  68. ExclusiveCommands += IntegrateWithSendmailKey + " | ";
  69. ExclusiveCommands += IntegrateWithNoneKey;
  70. ExclusiveCommandsHelp = " -with=postfix Integrate with postfix and start/reload postfix\n";
  71. ExclusiveCommandsHelp += " -with=sendmail Integrate with sendmail and start/reload sendmail\n";
  72. ExclusiveCommandsHelp += " (Not available on OpenBSD or FreeBSD)\n";
  73. ExclusiveCommandsHelp += " -with=none Remove any integration with all supported MTAs\n";
  74. cout
  75. << Version << endl
  76. << "Copyright (C) 2012, ARM Research Labs, LLC (www.armresearch.com)\n\n"
  77. << "Usage:\n\n"
  78. << "SNFMilterConfig "
  79. << UtilityConfig::HelpCommandLine(ExclusiveCommands) << "\n\n"
  80. << "SNFMilterConfig "
  81. << UtilityConfig::HelpDescription(ExclusiveCommandsHelp) << "\n"
  82. << "The configuration file name is:\n\n"
  83. << " " << DefaultConfigFile << "\n\n"
  84. << "If the above file doesn't exist, then it is copied from the following file:\n\n"
  85. << " " << SampleConfigFile << "\n\n"
  86. << "If integration with an MTA is specified, the MTA's configuration is reloaded "
  87. << "if the MTA is running.\n\n";
  88. };
  89. bool
  90. SNFMilterConfig::GetCommandLineInput(int argc, char* argv[]) {
  91. int i;
  92. int NumCommandsFound = 0;
  93. string OneInput;
  94. Command = NoCommand; // Default is to do nothing.
  95. for (i = 1; i < argc; i++) { // Check each input.
  96. OneInput = argv[i];
  97. if (OneInput == IntegrateWithPostfixKey) {
  98. Command = IntegrateWithPostfixCommand;
  99. NumCommandsFound++;
  100. } else if (0 == OneInput.find(IntegrateWithSendmailKey)) {
  101. std::string OsType;
  102. OsType = GetOperatingSystemType(); // Check whether the platform is supported.
  103. if ( ("OpenBSD" == OsType) || ("FreeBSD" == OsType) ) {
  104. std::string Temp;
  105. Temp = "Integration with sendmail is not supported on " + OsType;
  106. Temp += ".\n";
  107. Temp += "Please see " + InstallFile;
  108. Temp += " for information on integration with sendmail.\n";
  109. throw std::runtime_error(Temp);
  110. }
  111. Command = IntegrateWithSendmailCommand;
  112. NumCommandsFound++;
  113. } else if (OneInput == IntegrateWithNoneKey) {
  114. Command = IntegrateWithNoneCommand;
  115. NumCommandsFound++;
  116. } else {
  117. // Process command-line input by the base class.
  118. if (!ProcessCommandLineItem(OneInput)) {
  119. Command = UnknownCommand;
  120. return false; // Illegal input.
  121. }
  122. }
  123. }
  124. if (UpdateCredentialsSpecified()) {
  125. Command = UpdateCredentialsCommand;
  126. NumCommandsFound++;
  127. }
  128. if (SetupRepairSpecified()) {
  129. Command = SetupRepairCommand;
  130. NumCommandsFound++;
  131. }
  132. if (StartSnifferSpecified()) {
  133. Command = StartSnifferCommand;
  134. NumCommandsFound++;
  135. }
  136. if (StopSnifferSpecified()) {
  137. Command = StopSnifferCommand;
  138. NumCommandsFound++;
  139. }
  140. return ( (NumCommandsFound == 1) && CommandLineIsOkay() );
  141. }
  142. void
  143. SNFMilterConfig::ExecuteCommand() {
  144. Postfix.SetOperatingSystem(GetOperatingSystemType());
  145. Postfix.SetVerbose(Verbose());
  146. Postfix.SetExplain(Explain());
  147. Sendmail.SetOperatingSystem(GetOperatingSystemType());
  148. Sendmail.SetVerbose(Verbose());
  149. Sendmail.SetExplain(Explain());
  150. SetConfigFileName(DefaultConfigFile);
  151. if (SetupRepairCommand == Command) {
  152. CreateDefaultConfigFile(SampleConfigFile); // Create the file if it doesn't exist,
  153. // Set owner and mode in any case.
  154. }
  155. LoadConfig();
  156. LoadInfo(); // Load the file paths.
  157. LoadSocketInfo(); // Load the socket path.
  158. switch (Command) {
  159. case SetupRepairCommand:
  160. SetupRepair(SampleIdentityFile);
  161. SetupRepairSocketDir();
  162. break;
  163. case UpdateCredentialsCommand:
  164. UpdateRulebaseScriptCredentials();
  165. DownloadRulebase();
  166. UpdateIdentityFile();
  167. break;
  168. case IntegrateWithPostfixCommand:
  169. Postfix.Integrate(&SaveFile);
  170. UnintegrateWithAllExcept("postfix");
  171. if (Postfix.DefaultIsChrooted()) { // Update Sniffer file.
  172. SetSocketFileName(DefaultPostfixIsChrootedSocketFileName); // Update Sniffer configuration with chrooted socket spec.
  173. LoadConfig(); // Create the socket in the chrooted location.
  174. LoadSocketInfo();
  175. SetupRepairSocketDir();
  176. } else {
  177. SetSocketFileName(DefaultSocketFileName); // Update Sniffer configuration with non-chrooted socket spec.
  178. }
  179. break;
  180. case IntegrateWithSendmailCommand:
  181. Sendmail.Integrate(&SaveFile);
  182. UnintegrateWithAllExcept("sendmail");
  183. // Update Sniffer configuration with non-chrooted socket spec.
  184. SetSocketFileName(DefaultSocketFileName);
  185. break;
  186. case IntegrateWithNoneCommand:
  187. UnintegrateWithAllExcept();
  188. SetSocketFileName(DefaultSocketFileName); // Update Sniffer configuration with non-chrooted socket spec.
  189. break;
  190. case StartSnifferCommand:
  191. LoadCredentials();
  192. StartSniffer("snf-milter start", ApplicationName);
  193. break;
  194. case StopSnifferCommand:
  195. LoadCredentials();
  196. StopSniffer("snf-milter stop", ApplicationName);
  197. break;
  198. default:
  199. break;
  200. }
  201. }
  202. void
  203. SNFMilterConfig::SetSocketFileName(std::string NewSocketFileName) {
  204. std::string File = GetConfigFileName();
  205. if (Verbose()) {
  206. cout << "Setting socket to '" << NewSocketFileName << "' in '"
  207. << File << "'...";
  208. }
  209. if (!Explain()) {
  210. std::ifstream Input;
  211. Input.open(File.c_str());
  212. if (!Input) {
  213. std::string Temp;
  214. Temp = "Error opening the configuration file " + File;
  215. Temp += " to read from: ";
  216. Temp += strerror(errno);
  217. throw std::runtime_error(Temp);
  218. }
  219. std::ostringstream ContentStream;
  220. ContentStream << Input.rdbuf();
  221. Input.close();
  222. if (!Input) {
  223. std::string Temp;
  224. Temp = "Error closing the configuration file " + File;
  225. Temp += ": ";
  226. Temp += strerror(errno);
  227. throw std::runtime_error(Temp);
  228. }
  229. if (ContentStream.bad() || ContentStream.fail()) {
  230. std::string Temp;
  231. Temp = "Error reading the configuration file " + File;
  232. Temp += ": ";
  233. Temp += strerror(errno);
  234. throw std::runtime_error(Temp);
  235. }
  236. std::string Content;
  237. Content = ContentStream.str();
  238. std::string ElementName = "socket";
  239. std::string AttributeName = "path";
  240. ReplaceXmlAttribute(&Content, ElementName, AttributeName, NewSocketFileName);
  241. std::ofstream Output;
  242. Output.open(File.c_str(), std::ios::trunc);
  243. if (!Output) {
  244. std::string Temp;
  245. Temp = "Error opening the configuration file " + File;
  246. Temp += " to write to: ";
  247. Temp += strerror(errno);
  248. throw std::runtime_error(Temp);
  249. }
  250. Output << Content;
  251. if (Output.bad() || Output.fail()) {
  252. std::string Temp;
  253. Temp = "Error writing the configuration file " + File;
  254. Temp += ": ";
  255. Temp += strerror(errno);
  256. throw std::runtime_error(Temp);
  257. }
  258. Output.close();
  259. if (!Output) {
  260. std::string Temp;
  261. Temp = "Error closing the configuration file " + File;
  262. Temp += ": ";
  263. Temp += strerror(errno);
  264. throw std::runtime_error(Temp);
  265. }
  266. }
  267. OutputVerboseEnd();
  268. }
  269. void
  270. SNFMilterConfig::LoadSocketInfo() {
  271. std::string MilterElement = GetPlatformContents();
  272. codedweller::ConfigurationData PlatformConfig(MilterElement.c_str(), MilterElement.length());
  273. codedweller::ConfigurationElement SocketReader("milter");
  274. SocketReader
  275. .Element("socket")
  276. .Attribute("path", SocketFileName)
  277. .End("socket")
  278. .End("milter");
  279. SocketReader.interpret(PlatformConfig);
  280. }
  281. void
  282. SNFMilterConfig::SetupRepairSocketDir() {
  283. std::string SocketDir;
  284. std::string::size_type LastDirSepIndex = SocketFileName.rfind("/");
  285. SocketDir = ( (std::string::npos == LastDirSepIndex) ? SocketFileName : SocketFileName.substr(0, LastDirSepIndex));
  286. if (Verbose()) {
  287. cout << "Create the milter socket directory " << SocketDir << "...";
  288. }
  289. if (!Explain()) {
  290. if (!FileExists(SocketDir)) {
  291. MkDir(SocketDir);
  292. }
  293. SetMode(SocketDir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP);
  294. SetOwnerGroup(SocketDir);
  295. }
  296. OutputVerboseEnd();
  297. }
  298. void
  299. SNFMilterConfig::SaveFileState() {
  300. if (!Explain()) {
  301. SaveFile.CreateBackupFile(GetRulebaseScriptName());
  302. if (UpdateCredentialsSpecified()) {
  303. SaveFile.CreateBackupFile(GetRulebaseFileName());
  304. }
  305. SaveFile.CreateBackupFile(GetIdentityFileName());
  306. SaveFile.CreateBackupFile(GetIgnoreListFileName());
  307. }
  308. }
  309. void
  310. SNFMilterConfig::UnintegrateWithAllExcept(std::string Except) {
  311. if (Except != "postfix") {
  312. Postfix.Unintegrate(&SaveFile);
  313. }
  314. if (Except != "sendmail") {
  315. Sendmail.Unintegrate(&SaveFile);
  316. }
  317. }