Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SNFMilterConfig.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. SaveFile.CreateBackupFile(ConfigFileName); // Save any existing file.
  145. if (!FileExists(ConfigFileName)) {
  146. Copy(DefaultSampleConfigFile, ConfigFileName); // Use SNFMilter.xml.sample.
  147. }
  148. SetMode(ConfigFileName, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); // Set permissions.
  149. SetOwnerGroup(ConfigFileName); // Set to sniffer user.
  150. UpdateLogDir();
  151. UpdateIgnoreListFile();
  152. }
  153. void
  154. SNFMilterConfig::DoIntegrationCommand() {
  155. switch (Command) {
  156. case IntegrateWithNoneCmd:
  157. UnintegrateWithAllExcept();
  158. break;
  159. case IntegrateWithPostfixCmd:
  160. UnintegrateWithAllExcept("postfix");
  161. Postfix.Integrate(&SaveFile);
  162. break;
  163. case IntegrateWithSendmailCmd:
  164. UnintegrateWithAllExcept("sendmail");
  165. // Sendmail.Integrate(&SaveFile);
  166. break;
  167. default:
  168. {
  169. ostringstream Temp;
  170. Temp << "Internal error in SNFMilterConfig::DoIntegrationCommand: Invalid value of command: "
  171. << Command;
  172. throw runtime_error(Temp.str());
  173. }
  174. }
  175. }
  176. void
  177. SNFMilterConfig::UnintegrateWithAllExcept(std::string Except) {
  178. if (Except != "postfix") {
  179. Postfix.Unintegrate(&SaveFile);
  180. }
  181. #if 0
  182. if (Except != "sendmail") {
  183. Sendmail.Unintegrate(&SaveFile);
  184. }
  185. #endif
  186. }
  187. void
  188. SNFMilterConfig::IntegrateWithPostfix() {
  189. // UnintegrateWithAll(); // Remove any existing integration.
  190. if (Verbose()) {
  191. cout << "Add to postfix file " << PostfixMainCfPath << ": '"
  192. << SnfMilterMainCfIntegrationString << "'...";
  193. }
  194. if (!Explain()) {
  195. ofstream Output; // Append the configuration.
  196. Output.open(PostfixMainCfPath.c_str(), ios::app);
  197. if (!Output) {
  198. string Temp;
  199. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  200. Temp += " for writing: ";
  201. Temp += strerror(errno);
  202. throw runtime_error(Temp);
  203. }
  204. Output << SnfMilterMainCfIntegrationString << "\n";
  205. if (!Output) {
  206. string Temp;
  207. Temp = "Error appending to the postfix configuration file " + PostfixMainCfPath;
  208. Temp += ": ";
  209. Temp += strerror(errno);
  210. throw runtime_error(Temp);
  211. }
  212. Output.close();
  213. if (!Output) {
  214. string Temp;
  215. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  216. Temp += " after appending: ";
  217. Temp += strerror(errno);
  218. throw runtime_error(Temp);
  219. }
  220. }
  221. OutputVerboseEnd();
  222. CreateSocketDir();
  223. StartOrRestartMta("postfix");
  224. }
  225. void
  226. SNFMilterConfig::UnintegrateWithPostfix() {
  227. ifstream Input;
  228. if (Verbose()) {
  229. cout << "Remove any integration in postfix file " << PostfixMainCfPath << "--\n";
  230. }
  231. Input.open(PostfixMainCfPath.c_str()); // Read the contents.
  232. if (!Input) {
  233. string Temp;
  234. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  235. Temp += " for reading: ";
  236. Temp += strerror(errno);
  237. throw runtime_error(Temp);
  238. }
  239. string Content;
  240. string Line;
  241. while (getline(Input, Line)) {
  242. if (string::npos != Line.find(SnfMilterMainCfSearchString)) { // Check for integration line.
  243. if (Verbose()) {
  244. cout << " Remove '" << Line << "'...\n";
  245. }
  246. continue; // Do not copy this line.
  247. }
  248. Content += Line + "\n"; // Copy this line.
  249. }
  250. if (!Input.eof()) { // Should be at end-of-file.
  251. string Temp;
  252. Temp = "Error reading the postfix configuration file " + PostfixMainCfPath;
  253. Temp += ": ";
  254. Temp += strerror(errno);
  255. throw runtime_error(Temp);
  256. }
  257. Input.close();
  258. if (Input.bad()) {
  259. string Temp;
  260. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  261. Temp += " after reading: ";
  262. Temp += strerror(errno);
  263. throw runtime_error(Temp);
  264. }
  265. if (!Explain()) {
  266. ofstream Output; // Write the updated contents.
  267. Output.open(PostfixMainCfPath.c_str(), ios::trunc);
  268. if (!Output) {
  269. string Temp;
  270. Temp = "Error opening the postfix configuration file " + PostfixMainCfPath;
  271. Temp += " for writing: ";
  272. Temp += strerror(errno);
  273. throw runtime_error(Temp);
  274. }
  275. Output << Content;
  276. if (!Output) {
  277. string Temp;
  278. Temp = "Error writing the postfix configuration file " + PostfixMainCfPath;
  279. Temp += ": ";
  280. Temp += strerror(errno);
  281. throw runtime_error(Temp);
  282. }
  283. Output.close();
  284. if (!Output) {
  285. string Temp;
  286. Temp = "Error closing the postfix configuration file " + PostfixMainCfPath;
  287. Temp += " after writing: ";
  288. Temp += strerror(errno);
  289. throw runtime_error(Temp);
  290. }
  291. }
  292. OutputVerboseEnd();
  293. }
  294. void
  295. SNFMilterConfig::IntegrateWithSendmail() {
  296. throw runtime_error("Integration with sendmail is not implemented");
  297. }
  298. void
  299. SNFMilterConfig::UnintegrateWithSendmail() {
  300. std::cerr << "Unintegration with sendmail is not implemented" << "\n";
  301. }