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

SNFServerConfig.cpp 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // /file SNFServerConfig.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 SNFServerConfig.
  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 "SNFServerConfig.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 SNFServerConfig::DefaultConfigFile("C:\\SNF\\SNFServer.xml");
  34. const std::string SNFServerConfig::SampleConfigFile("C:\\SNF\\SNFServer.xml.sample");
  35. const std::string SNFServerConfig::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 SNFServerConfig::DefaultConfigFile(DEFAULT_CONFIG_DIR "/snf-server/SNFServer.xml");
  41. const std::string SNFServerConfig::SampleConfigFile(DEFAULT_CONFIG_DIR "/snf-server/SNFServer.xml.sample");
  42. const std::string SNFServerConfig::SampleIdentityFile(DEFAULT_CONFIG_DIR "/snf-server/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 SNFServerConfig::DefaultConfigFile("");
  49. const std::string SNFServerConfig::SampleConfigFile("");
  50. const std::string SNFServerConfig::SampleIdentityFile("");
  51. const std::string InstallFile("INSTALL");
  52. #endif
  53. #endif
  54. const string SNFServerConfig::ApplicationName("SNFServer");
  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. SNFServerConfig::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. << "SNFServerConfig "
  77. << UtilityConfig::HelpCommandLine(ExclusiveCommands) << "\n\n"
  78. << "SNFServerConfig "
  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. SNFServerConfig::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. SNFServerConfig::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. switch (Command) {
  156. case SetupRepairCommand:
  157. SetupRepair(SampleIdentityFile);
  158. break;
  159. case UpdateCredentialsCommand:
  160. UpdateRulebaseScriptCredentials();
  161. DownloadRulebase();
  162. UpdateIdentityFile();
  163. break;
  164. case IntegrateWithPostfixCommand:
  165. Postfix.Integrate(&SaveFile);
  166. UnintegrateWithAllExcept("postfix");
  167. break;
  168. case IntegrateWithSendmailCommand:
  169. Sendmail.Integrate(&SaveFile);
  170. UnintegrateWithAllExcept("sendmail");
  171. break;
  172. case IntegrateWithNoneCommand:
  173. UnintegrateWithAllExcept();
  174. break;
  175. case StartSnifferCommand:
  176. LoadCredentials();
  177. StartSniffer("snf-server start", ApplicationName);
  178. break;
  179. case StopSnifferCommand:
  180. LoadCredentials();
  181. StopSniffer("snf-server stop", ApplicationName);
  182. break;
  183. default:
  184. break;
  185. }
  186. }
  187. void
  188. SNFServerConfig::SaveFileState() {
  189. if (!Explain()) {
  190. SaveFile.CreateBackupFile(GetRulebaseScriptName());
  191. if (UpdateCredentialsSpecified()) {
  192. SaveFile.CreateBackupFile(GetRulebaseFileName());
  193. }
  194. SaveFile.CreateBackupFile(GetIdentityFileName());
  195. SaveFile.CreateBackupFile(GetIgnoreListFileName());
  196. }
  197. }
  198. void
  199. SNFServerConfig::UnintegrateWithAllExcept(std::string Except) {
  200. if (Except != "postfix") {
  201. Postfix.Unintegrate(&SaveFile);
  202. }
  203. if (Except != "sendmail") {
  204. Sendmail.Unintegrate(&SaveFile);
  205. }
  206. }