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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // snf2check.cpp
  2. //
  3. // Copyright 2002-2004 MicroNeil Research Corporation.
  4. //
  5. // 20040502 _M - Completed testing. Digest checking rocks!
  6. // 20040419 _M - Beginning modification to check an embedded digest of the entire
  7. // rulebase file. The digest is embedded in a way that makes it backward compatible
  8. // with all prior versions.
  9. // This utility opens a Message Sniffer version 2 rule base file and authenticates
  10. // the file. If the authentiaction fails then the utility returns a nonzero value.
  11. // If the file autneticates correctly then the utility returns a zero value.
  12. //
  13. // This utility can be used to check that a download was performed properly so that
  14. // corrupted rule base files won't be placed into service in a Message Sniffer engine.
  15. #include <unistd.h>
  16. #include <cstdio>
  17. #include <cctype>
  18. #include <ctime>
  19. #include <cstdlib>
  20. #include <fstream>
  21. #include <iostream>
  22. #include <string>
  23. #include "snf_engine.hpp"
  24. using namespace std; //introduces namespace std
  25. const int RULEFILE_PARM = 1; // Rule Base File Parameter.
  26. const int AUTHENTICATION_PARM = 2; // Parm position of authentication.
  27. const int CORRECT_ARGS = 3; // Correct number of command line args.
  28. const int OK = 0; // OK result.
  29. const int ERROR_CMDLINE = 65; // Command line error result.
  30. const int ERROR_RULE_FILE = 67; // Rule file open error result.
  31. const int ERROR_RULE_DATA = 68; // Rule file authentication error result.
  32. const int ERROR_RULE_AUTH = 73; // Authentication failed on the rule base.
  33. const int ERROR_BAD_MATRIX = 71; // Something pushed us out of range.
  34. //////////////////////////////////////////////////////////////////////////////////////////
  35. // main()... well, that's where it all happens.
  36. int main( int argc, char* argv[] ) {
  37. // Check the command line and set option flags.
  38. // First, are the right number of parameters present?
  39. // If not then remind the user of what the command line should look like.
  40. if(argc != CORRECT_ARGS) {
  41. cerr << "snf2check: Bad Command Line" << endl
  42. << endl
  43. << "use snf2check.exe rulefilepath authentication" << endl
  44. << endl
  45. << "Version 2 w/Mangler Digest Checking - build "
  46. << __DATE__ << " " << __TIME__ << endl
  47. << endl
  48. << "Copyright (C) 2002-2008 MicroNeil Research Corporation"
  49. << endl;
  50. return ERROR_CMDLINE;
  51. }
  52. // Capture & rewrite path info...
  53. string RuleFileString(argv[RULEFILE_PARM]); // Rule file path...
  54. string Authentication(argv[AUTHENTICATION_PARM]); // Authentication...
  55. const int LicenseLength = 8; // License IDs are this long.
  56. string License(RuleFileString); // The license is in the rule file string.
  57. int LicensePosition = License.find_last_of("\\/"); // Find the license position.
  58. LicensePosition += 1; // Move past the stroke if present.
  59. string LicenseID( // Extract the licenseID.
  60. License.substr(LicensePosition,LicenseLength));
  61. ///////////////////////////////////////////////////////////////////////////////////////
  62. // Load the RuleBase.
  63. ///////////////////////////////////////////////////////////////////////////////////////
  64. TokenMatrix RuleBase; // Create the rule base object.
  65. try {
  66. RuleBase.Load(RuleFileString);
  67. }
  68. catch(TokenMatrix::BadAllocation) { // If we can't allocate memory report it.
  69. cerr << "snf2check: " << License << " ERROR_RULE_DATA!" << endl;
  70. return ERROR_RULE_DATA;
  71. }
  72. catch(...) { // If something else goes wrong report the following error...
  73. cerr << "snf2check: " << License << " ERROR_RULE_FILE!" << endl;
  74. return ERROR_RULE_FILE;
  75. }
  76. ///////////////////////////////////////////////////////////////////////////////////////
  77. // Validate the RuleBase is properly authenticated.
  78. ///////////////////////////////////////////////////////////////////////////////////////
  79. // SecurityKey starts with
  80. string SecurityKey(LicenseID); // the License ID then we add
  81. SecurityKey += string(argv[AUTHENTICATION_PARM]); // the authentication code.
  82. try {
  83. RuleBase.Validate(SecurityKey);
  84. }
  85. catch(...) { // If the validation fails then we produce this error...
  86. cerr << "snf2check: " << License << " ERROR_RULE_AUTH! " << endl;
  87. return ERROR_RULE_AUTH;
  88. }
  89. ///////////////////////////////////////////////////////////////////////////////////////
  90. // Verify the integrity of the RuleBase - this can only happen if Validated!
  91. ///////////////////////////////////////////////////////////////////////////////////////
  92. try {
  93. RuleBase.Verify(SecurityKey);
  94. }
  95. catch(...) { // If the verification process fails then we produce this error...
  96. cerr << "snf2check: " << License << " ERROR_BAD_MATRIX! " << endl;
  97. return ERROR_BAD_MATRIX;
  98. }
  99. // If we made it through all of that, then we're golden!
  100. return OK;
  101. }