Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

main.cpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // main.cpp
  2. //
  3. // Copyright (C) 2011, ARM Research Labs, LLC.
  4. // See www.armresearch.com for the copyright terms.
  5. //
  6. // Configure the identity.xml and getRulebase files with the license
  7. // and authentication credentials.
  8. //
  9. // The license and authentication credentials are specified on the
  10. // command line, and are used to create the identity.xml and update
  11. // the getRulebase files with that information.
  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 "SNFIdentityConfig.hpp"
  27. using namespace std;
  28. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  29. // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
  30. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. /// Version string.
  32. const char* SNF_IDENTITY_VERSION = "SNFIdentity 0.0.1 Build: " __DATE__ " " __TIME__;
  33. /// Locations to search for default configuration files.
  34. const string DefaultConfigFile[] = {
  35. "/etc/snf-milter/SNFMilter.xml",
  36. "/usr/local/etc/snf-milter/SNFMilter.xml",
  37. "/etc/snf-server/SNFServer.xml",
  38. "/usr/local/etc/snf-server/SNFServer.xml"
  39. };
  40. const size_t DefaultConfigFileSize = sizeof DefaultConfigFile / sizeof DefaultConfigFile[0];
  41. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  43. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  44. void RestoreFiles(SNFIdentityConfig *Config) {
  45. try {
  46. cerr << "Restoring all configuration files...";
  47. Config->SaveFile.RestoreAllFilesFromBackup();
  48. cerr << "done.\n";
  49. }
  50. catch(exception& e) {
  51. cerr << "SNFIdentityConfig::SaveFile Exception: " << e.what() << endl;
  52. }
  53. }
  54. int main(int argc, char* argv[]) {
  55. SNFIdentityConfig SnfIdentityConfig;
  56. if (!SnfIdentityConfig.GetCommandLineInput(argc, argv) || // If our command line arguments
  57. SnfIdentityConfig.Help()) { // don't look right, or if help is
  58. // requested then display our help
  59. SnfIdentityConfig.DisplayHelp(SNF_IDENTITY_VERSION, // screen.
  60. DefaultConfigFile,
  61. DefaultConfigFileSize);
  62. return 0;
  63. }
  64. bool DebugMode = false; // This will be our debug mode.
  65. string argv0(argv[0]); // Capture how we were called.
  66. if(
  67. string::npos != argv0.find("Debug") || // If we find "Debug" or
  68. string::npos != argv0.find("debug") // "debug" in our command path
  69. ) { // then we are in DebugMode.
  70. DebugMode = true; // Set the flag and tell the
  71. cout << SNF_IDENTITY_VERSION << endl; // watchers.
  72. cout << "Debug Mode" << endl;
  73. }
  74. try { // Catch anything that breaks loose.
  75. SnfIdentityConfig.CheckAndLoadConfigFile(DefaultConfigFile, // Load configuration from the
  76. DefaultConfigFileSize); // config file specified on the
  77. // command line, or the default file.
  78. SnfIdentityConfig.UpdateRulebaseScriptCredentials();
  79. SnfIdentityConfig.DownloadRulebase();
  80. SnfIdentityConfig.CreateIdentityFile();
  81. } // That's all folks.
  82. catch(exception& e) { // Report any normal exceptions.
  83. cerr << "SNFIdentity Exception: " << e.what() << endl;
  84. RestoreFiles(&SnfIdentityConfig);
  85. }
  86. catch (snfCFGmgr::LoadFailure) { // Error loading configuration file.
  87. cerr << "snfCFGmgr Exception: Unable to load the configuration file "
  88. << SnfIdentityConfig.GetConfigFileName() << endl;
  89. RestoreFiles(&SnfIdentityConfig);
  90. }
  91. catch(...) { // Report any unexpected exceptions.
  92. cerr << "SNFIdentity Panic! Unknown Exception!" << endl;
  93. RestoreFiles(&SnfIdentityConfig);
  94. }
  95. return 0; // Normally we return zero.
  96. }