Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // main.cpp
  2. //
  3. // Copyright (C) 2012, ARM Research Labs, LLC.
  4. // See www.armresearch.com for the copyright terms.
  5. //
  6. // Create and maintain the sniffer configuration file for SNFMilter.
  7. //
  8. // The configuration file and instructions are specified on the
  9. // command line, and are used to create/modify the configuration file,
  10. // and integrate or unintegrate with the specified MTA.
  11. //
  12. //
  13. // $Id$
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////
  16. #include <errno.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <sys/types.h>
  20. #include <pwd.h>
  21. #include <sys/stat.h>
  22. #include <exception>
  23. #include <iostream>
  24. #include <fstream>
  25. #include "SNFMulti.hpp"
  26. #include "SNFMilterConfig.hpp"
  27. using namespace std;
  28. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  29. // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
  30. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. /// Version string.
  32. const char* SNF_MILTERCONFIG_VERSION = "SNFMilterConfig 0.0.1 Build: " __DATE__ " " __TIME__;
  33. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  35. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  36. void RestoreFiles(SNFMilterConfig *Config) {
  37. try {
  38. cerr << "Restoring all configuration files...";
  39. Config->SaveFile.RestoreAllFilesFromBackup();
  40. cerr << "done.\n";
  41. }
  42. catch(exception& e) {
  43. cerr << "SNFMilterConfig::SaveFile Exception: " << e.what() << endl;
  44. }
  45. }
  46. int main(int argc, char* argv[]) {
  47. SNFMilterConfig SnfMilterConfig;
  48. if (!SnfMilterConfig.GetCommandLineInput(argc, argv) || // If our command line arguments
  49. SnfMilterConfig.Help()) { // don't look right, or if help is
  50. // requested then display our help
  51. SnfMilterConfig.DisplayHelp(SNF_MILTERCONFIG_VERSION); // screen.
  52. return 0;
  53. }
  54. bool DebugMode = false; // This will be our debug mode.
  55. string argv0(argv[0]); // Capture how we were called.
  56. if(
  57. string::npos != argv0.find("Debug") || // If we find "Debug" or
  58. string::npos != argv0.find("debug") // "debug" in our command path
  59. ) { // then we are in DebugMode.
  60. DebugMode = true; // Set the flag and tell the
  61. cout << SNF_MILTERCONFIG_VERSION << endl; // watchers.
  62. cout << "Debug Mode" << endl;
  63. }
  64. try { // Catch anything that breaks loose.
  65. #if 0
  66. SnifferRunningStatue RunningStatus;
  67. #endif
  68. SnfMilterConfig.CreateLoadConfig(); // Save config file state and load config.
  69. // Load the default if necessary.
  70. SnfMilterConfig.SaveFileState(); // Save state of all other files.
  71. #if 0
  72. RunningStatus = SnfMilterConfig.GetSnifferRunningStatus(); // Get state before changing anything.
  73. #endif
  74. SnfMilterConfig.UpdateConfigFiles(); // Create/update config files
  75. SnfMilterConfig.CreateUpdateRulebaseScript(); // Create/update GetRulebase.
  76. SnfMilterConfig.DownloadRulebase(); // Download rulebase.
  77. SnfMilterConfig.UpdateIdentityFile(); // Update Identity file with credentials,
  78. // if credentials were specified.
  79. SnfMilterConfig.DoIntegrationCommand(); // Integrate/unintegrate.
  80. #if 0
  81. if (SnifferIsStopped == RunningStatus) {
  82. SnfMilterConfig.StartSniffer();
  83. } else if (SnifferRunningStatusIsUknown == RunningStatus) {
  84. std::cout << "SNFMilterConfig: Unable to determine whether SNFMilter is running.\n"
  85. << "Please start SNFMilter if it isn't running.\n";
  86. }
  87. #else
  88. SnfMilterConfig.StartSniffer("snf-milter");
  89. #endif
  90. } // That's all folks.
  91. catch(exception& e) { // Report any normal exceptions.
  92. cerr << "\n\nSNFMilterConfig Exception: " << e.what() << endl << endl;
  93. RestoreFiles(&SnfMilterConfig);
  94. }
  95. catch (snfCFGmgr::LoadFailure) { // Error loading configuration file.
  96. cerr << "\n\nsnfCFGmgr Exception: Unable to load the configuration file "
  97. << SnfMilterConfig.GetConfigFileName() << endl << endl;
  98. RestoreFiles(&SnfMilterConfig);
  99. }
  100. catch(...) { // Report any unexpected exceptions.
  101. cerr << "\n\nSNFMilterConfig Panic! Unknown Exception!" << endl << endl;
  102. RestoreFiles(&SnfMilterConfig);
  103. }
  104. return 0; // Normally we return zero.
  105. }