You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cpp 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // main.cpp
  2. //
  3. // Copyright (C) 2007, ARM Research Labs, LLC.
  4. // See www.armresearch.com for the copyright terms.
  5. //
  6. // This is the entry point for the SNF Milter.
  7. // The main() function acquires the configuration file path, sets up the
  8. // SNF engine, and passes control to the SNFMilter module via runSNFMilter().
  9. //
  10. // SNFMilter() makes use of the SNF engine until it is dismissed by the MTA via
  11. // the appropriate libmilter call.
  12. //
  13. // While SNFMilter() is running, it will get new configuration data for each
  14. // scan it is asked to perform. A configuration packet will be produced on
  15. // demand by the main module. The main module will check the configuration once
  16. // every second or so to make sure it has the correct current data. The
  17. // configuration data structure is defined in SNFMilter.hpp.
  18. //
  19. // When the SNFMilter() function returns, the main() function closes down the
  20. // SNF Engine and exits the program.
  21. #include <errno.h>
  22. #include <string.h>
  23. #include <exception>
  24. #include <iostream>
  25. #include "SNFMulti.hpp"
  26. #include "SNFMilter.hpp"
  27. #include "config.h"
  28. using namespace std;
  29. const char* SNF_MILTER_VERSION = "SNFMilter " PACKAGE_VERSION " Build: " __DATE__ " " __TIME__;
  30. static const string XCIShutdownResponse =
  31. "<snf><xci><server><response message=\'shutdown in progress\' code=\'1\'/></server></xci></snf>\n";
  32. class XCIShutdownWatcher : public snfXCIServerCommandHandler { // Shutdown watcher.
  33. public:
  34. XCIShutdownWatcher(){}
  35. string processXCIRequest(snf_xci& X) { // Here is how we process requests.
  36. if(0 == X.xci_server_command.find("shutdown")) { // If we find shutdown then
  37. smfi_stop(); // Stop processing messages.
  38. return XCIShutdownResponse; // respond with a message.
  39. } // If we get some other request
  40. return XCIErrorResponse; // return the error response.
  41. }
  42. };
  43. const int CorrectARGc = 2; // How many arguments we expect.
  44. void displayHelp() { // Display some help.
  45. cout
  46. << SNF_MILTER_VERSION << endl
  47. << "Copyright (C) 2007, ARM Research Labs, LLC (www.armresearch.com)" << endl
  48. << endl
  49. << "Use snfmilter <full path to configuration file>" << endl
  50. << "Example: /home/snfmilter/snfmilter /home/snfmilter/snf_milter.xml" << endl;
  51. };
  52. int main(int argc, char* argv[]) {
  53. // Get and check the command line arguments.
  54. if(CorrectARGc != argc) { // If our command line arguments
  55. displayHelp(); // don't look right then display
  56. return 0; // our help screen.
  57. }
  58. bool DebugMode = false; // This will be our debug mode.
  59. string argv0(argv[0]); // Capture how we were called.
  60. if(
  61. string::npos != argv0.find("Debug") || // If we find "Debug" or
  62. string::npos != argv0.find("debug") // "debug" in our command path
  63. ) { // then we are in DebugMode.
  64. DebugMode = true; // Set the flag and tell the
  65. cout << SNF_MILTER_VERSION << endl; // watchers.
  66. #ifdef SMFI_PROT_VERSION
  67. cout << "SMFI_PROT_VERSION: " << SMFI_PROT_VERSION << endl;
  68. #endif
  69. cout << "Debug Mode" << endl;
  70. }
  71. try { // Catch anything that breaks loose.
  72. snf_RulebaseHandler* MilterRulebase = new snf_RulebaseHandler(); // Allocate a rulebase handler.
  73. MilterRulebase->PlatformVersion(SNF_MILTER_VERSION); // Record our version identification.
  74. XCIShutdownWatcher ShutdownWatcher; // Make a server shutdown processor
  75. MilterRulebase->XCIServerCommandHandler(ShutdownWatcher); // and register it with the engine.
  76. MilterRulebase->open(argv[1], "", ""); // Open the rulebase.
  77. SNFMilterContextPool* MilterContexts =
  78. new SNFMilterContextPool(MilterRulebase); // Create the Milter Context Pool.
  79. runLibMilter(MilterContexts, DebugMode); // Run the milter.
  80. delete MilterContexts; // Destroy the context pool.
  81. MilterContexts = 0; // Forget it.
  82. MilterRulebase->close(); // Close down the rulebase handler.
  83. delete MilterRulebase; // Destroy the rulebase.
  84. MilterRulebase = 0; // Forget it.
  85. } // That's all folks.
  86. catch(exception& e) { // Report any normal exceptions.
  87. cerr << "SNFMilter Exception: " << e.what() << endl;
  88. }
  89. catch(...) { // Report any unexpected exceptions.
  90. cerr << "SNFMilter Panic! Unknown Exception!" << endl;
  91. }
  92. return 0; // Normally we return zero.
  93. }