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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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::DefaultSampleConfigFile("C:\\SNF\\SNFMilter.xml.sample");
  35. #else
  36. #ifdef DEFAULT_CONFIG_DIR
  37. // *nix, DEFAULT_CONFIG_DIR is specified on the compile command line.
  38. const std::string SNFMilterConfig::DefaultConfigFile(DEFAULT_CONFIG_DIR "/snf-milter/SNFMilter.xml");
  39. const std::string SNFMilterConfig::DefaultSampleConfigFile(DEFAULT_CONFIG_DIR "/snf-milter/SNFMilter.xml.sample");
  40. #else
  41. // Not Windows, and DEFAULT_CONFIG_DIR is not specified on the compile
  42. // command line. In this case, we don't know the default path for the
  43. // configuration file.
  44. const std::string SNFMilterConfig::DefaultConfigFile("");
  45. const std::string SNFMilterConfig::DefaultSampleConfigFile("");
  46. #endif
  47. #endif
  48. const string ConfigFileKey("-config=");
  49. const string IntegrateWithNoneKey("-mta=none");
  50. const string IntegrateWithPostfixKey("-mta=postfix");
  51. const string IntegrateWithSendmailKey("-mta=sendmail");
  52. const string SnfMilterMainCfSearchString("Added by SNFMilterConfig");
  53. const string SnfMilterMainCfIntegrationString("smtpd_milters = unix:/var/snf-milter/socket $smtpd_milters # Added by SNFMilterConfig");
  54. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  55. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  56. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. void
  58. SNFMilterConfig::DisplayHelp(std::string Version) {
  59. cout
  60. << Version << endl
  61. << "Copyright (C) 2012, ARM Research Labs, LLC (www.armresearch.com)\n\n"
  62. << "Usage:\n\n"
  63. << "SNFMilterConfig [" << ConfigFileKey << "snf-config-file] "
  64. << IntegrateWithPostfixKey << " | "
  65. << IntegrateWithSendmailKey << " | "
  66. << IntegrateWithNoneKey << " "
  67. << UtilityConfig::HelpCommandLine() << "\n\n"
  68. << "SNFMilterConfig creates the configuration files (snf-config-file and the ignore list file) and the\n"
  69. << "rulebase download script (default: getRulebase) if they don't exist.\n\n"
  70. << " -config=snf-config-file Specifies the configuration file\n"
  71. << " -mta=postfix Integrate with postfix\n"
  72. << " -mta=sendmail Integrate with sendmail\n"
  73. << " -mta=none Remove any integration with all supported MTAs\n"
  74. << UtilityConfig::HelpDescription() << "\n"
  75. << "If snf-config-file is not specified, then the following file is used:\n\n"
  76. << " " << DefaultConfigFile << "\n\n";
  77. };
  78. bool
  79. SNFMilterConfig::GetCommandLineInput(int argc, char* argv[]) {
  80. int i;
  81. int NumCommandsFound = 0;
  82. string OneInput;
  83. string ConfigFile;
  84. for (i = 1; i < argc; i++) { // Check each input.
  85. OneInput = argv[i];
  86. if (0 == OneInput.find(ConfigFileKey)) {
  87. ConfigFile = OneInput.substr(ConfigFileKey.length());
  88. } else if (OneInput == IntegrateWithNoneKey) {
  89. Command = IntegrateWithNoneCmd;
  90. NumCommandsFound++;
  91. } else if (OneInput == IntegrateWithPostfixKey) {
  92. Command = IntegrateWithPostfixCmd;
  93. NumCommandsFound++;
  94. } else if (0 == OneInput.find(IntegrateWithSendmailKey)) {
  95. Command = IntegrateWithSendmailCmd;
  96. NumCommandsFound++;
  97. } else {
  98. // Process command-line input by the base class.
  99. if (!ProcessCommandLineItem(OneInput)) {
  100. return false; // Illegal input.
  101. }
  102. }
  103. }
  104. if (0 == ConfigFile.length()) { // Load default config file name.
  105. ConfigFile = DefaultConfigFile;
  106. }
  107. LoadConfigFile(ConfigFile);
  108. LoadInfo(); // Load the file paths.
  109. LoadSocketInfo(); // Load the socket path.
  110. return (NumCommandsFound == 1);
  111. }
  112. void
  113. SNFMilterConfig::LoadSocketInfo() {
  114. std::string MilterElement = GetPlatformContents();
  115. ConfigurationData PlatformConfig(MilterElement.c_str(), MilterElement.length());
  116. ConfigurationElement SocketReader("milter");
  117. SocketReader
  118. .Element("socket")
  119. .Attribute("path", SocketFileName)
  120. .End("socket")
  121. .End("milter");
  122. SocketReader.interpret(PlatformConfig);
  123. }
  124. void
  125. SNFMilterConfig::CreateSocketDir() {
  126. std::string SocketDir;
  127. std::string::size_type LastDirSepIndex = SocketFileName.rfind("/");
  128. SocketDir = ( (std::string::npos == LastDirSepIndex) ? SocketFileName : SocketFileName.substr(0, LastDirSepIndex));
  129. if (Verbose()) {
  130. cout << "Create the milter socket directory " << SocketDir << "...";
  131. }
  132. if (!Explain()) {
  133. if (!FileExists(SocketDir)) {
  134. MkDir(SocketDir);
  135. }
  136. SetMode(SocketDir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP);
  137. SetOwnerGroup(SocketDir);
  138. }
  139. OutputVerboseEnd();
  140. }
  141. void
  142. SNFMilterConfig::UpdateConfigFiles() {
  143. std::string ConfigFileName = GetConfigFileName();
  144. if (!FileExists(ConfigFileName)) {
  145. Copy(DefaultSampleConfigFile, ConfigFileName); // Use SNFMilter.xml.sample.
  146. }
  147. SetMode(ConfigFileName, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); // Set permissions.
  148. SetOwnerGroup(ConfigFileName); // Set to sniffer user.
  149. UpdateLogDir();
  150. UpdateIgnoreListFile();
  151. }
  152. void
  153. SNFMilterConfig::DoIntegrationCommand() {
  154. switch (Command) {
  155. case IntegrateWithNoneCmd:
  156. UnintegrateWithAll();
  157. break;
  158. case IntegrateWithPostfixCmd:
  159. IntegrateWithPostfix();
  160. break;
  161. case IntegrateWithSendmailCmd:
  162. IntegrateWithSendmail();
  163. break;
  164. default:
  165. {
  166. ostringstream Temp;
  167. Temp << "Internal error in SNFMilterConfig::DoCommand: Invalid value of command: " << Command;
  168. throw runtime_error(Temp.str());
  169. }
  170. }
  171. }
  172. void
  173. SNFMilterConfig::UnintegrateWithAll() {
  174. UnintegrateWithPostfix();
  175. UnintegrateWithSendmail();
  176. // TODO: Restart MTA.
  177. // Do not remove the socket directory; users might have placed
  178. // files in it. This happened with the /tmp directory; it was
  179. // supposed to be only for files that would be deleted on reboot.
  180. // However, admins stored files that they wished to be persistent
  181. // across reboots in /tmp.
  182. }
  183. void
  184. SNFMilterConfig::IntegrateWithPostfix() {
  185. UnintegrateWithAll(); // Remove any existing integration.
  186. if (Verbose()) {
  187. cout << "Add to postfix file " << PostfixMainCfPath << ": '"
  188. << SnfMilterMainCfIntegrationString << "'...";
  189. }
  190. if (!Explain()) {
  191. ofstream Output; // Append the configuration.
  192. Output.open(PostfixMainCfPath.c_str(), ios::app);
  193. if (!Output) {
  194. string Temp;
  195. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  196. Temp += " for writing: ";
  197. Temp += strerror(errno);
  198. throw runtime_error(Temp);
  199. }
  200. Output << SnfMilterMainCfIntegrationString << "\n";
  201. if (!Output) {
  202. string Temp;
  203. Temp = "Error appending to the postfix configuration file " + PostfixMainCfPath;
  204. Temp += ": ";
  205. Temp += strerror(errno);
  206. throw runtime_error(Temp);
  207. }
  208. Output.close();
  209. if (!Output) {
  210. string Temp;
  211. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  212. Temp += " after appending: ";
  213. Temp += strerror(errno);
  214. throw runtime_error(Temp);
  215. }
  216. }
  217. OutputVerboseEnd();
  218. CreateSocketDir();
  219. StartOrRestartMta("postfix");
  220. }
  221. void
  222. SNFMilterConfig::UnintegrateWithPostfix() {
  223. ifstream Input;
  224. if (Verbose()) {
  225. cout << "Remove any integration in postfix file " << PostfixMainCfPath << "--\n";
  226. }
  227. Input.open(PostfixMainCfPath.c_str()); // Read the contents.
  228. if (!Input) {
  229. string Temp;
  230. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  231. Temp += " for reading: ";
  232. Temp += strerror(errno);
  233. throw runtime_error(Temp);
  234. }
  235. string Content;
  236. string Line;
  237. while (getline(Input, Line)) {
  238. if (string::npos != Line.find(SnfMilterMainCfSearchString)) { // Check for integration line.
  239. if (Verbose()) {
  240. cout << " Remove '" << Line << "'...\n";
  241. }
  242. continue; // Do not copy this line.
  243. }
  244. Content += Line + "\n"; // Copy this line.
  245. }
  246. if (!Input.eof()) { // Should be at end-of-file.
  247. string Temp;
  248. Temp = "Error reading the postfix configuration file " + PostfixMainCfPath;
  249. Temp += ": ";
  250. Temp += strerror(errno);
  251. throw runtime_error(Temp);
  252. }
  253. Input.close();
  254. if (Input.bad()) {
  255. string Temp;
  256. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  257. Temp += " after reading: ";
  258. Temp += strerror(errno);
  259. throw runtime_error(Temp);
  260. }
  261. if (!Explain()) {
  262. ofstream Output; // Write the updated contents.
  263. Output.open(PostfixMainCfPath.c_str(), ios::trunc);
  264. if (!Output) {
  265. string Temp;
  266. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  267. Temp += " for writing: ";
  268. Temp += strerror(errno);
  269. throw runtime_error(Temp);
  270. }
  271. Output << Content;
  272. if (!Output) {
  273. string Temp;
  274. Temp = "Error writing the postfix configuration file " + PostfixMainCfPath;
  275. Temp += ": ";
  276. Temp += strerror(errno);
  277. throw runtime_error(Temp);
  278. }
  279. Output.close();
  280. if (!Output) {
  281. string Temp;
  282. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  283. Temp += " after writing: ";
  284. Temp += strerror(errno);
  285. throw runtime_error(Temp);
  286. }
  287. }
  288. OutputVerboseEnd();
  289. }
  290. void
  291. SNFMilterConfig::IntegrateWithSendmail() {
  292. throw runtime_error("Integration with sendmail is not implemented");
  293. }
  294. void
  295. SNFMilterConfig::UnintegrateWithSendmail() {
  296. std::cerr << "Unintegration with sendmail is not implemented" << "\n";
  297. }