|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587 |
-
-
-
-
-
-
-
-
-
-
- #if defined(WIN32) || defined(WIN64)
- #include <windows.h>
- #endif
-
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <cstring>
- #include <string>
- #include <ctime>
- #include <cctype>
- #include <unistd.h>
- #include <dirent.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include "timing.hpp"
- #include "networking.hpp"
- #include "threading.hpp"
- #include "snf_xci.hpp"
-
- #include "config.h"
-
- using namespace std;
-
- const char* VERSION_INFO = "SNF Client Version " PACKAGE_VERSION " Build: " __DATE__ " " __TIME__;
-
- time_t Timestamp() {
- time_t rawtime;
- time(&rawtime);
- return rawtime;
- }
-
- string Timestamp(time_t t) {
- char TimestampBfr[20];
- tm* gmt;
- gmt = gmtime(&t);
- sprintf(TimestampBfr,"%04d%02d%02d%02d%02d%02d\0",
- gmt->tm_year+1900,
- gmt->tm_mon+1,
- gmt->tm_mday,
- gmt->tm_hour,
- gmt->tm_min,
- gmt->tm_sec
- );
- return string(TimestampBfr);
- }
-
- string ErrorMessage(int argc, char** argv, string ErrorText) {
-
- ostringstream FailurePreamble;
-
- string TimestampString = Timestamp(Timestamp());
-
- FailurePreamble << TimestampString << ", ";
- for(int a = 1; a < argc; a++) {
- FailurePreamble << "arg" << a << "=" << argv[a];
- if(a < argc-1) { FailurePreamble << ", "; }
- else { FailurePreamble << " : "; }
- }
-
- FailurePreamble << ErrorText << endl;
-
- return FailurePreamble.str();
- }
-
- int main(int argc, char* argv[]) {
-
-
-
- bool DebugMode = false;
- string argv0(argv[0]);
- if(
- string::npos != argv0.find("Debug") ||
- string::npos != argv0.find("debug")
- ) {
- DebugMode = true;
- cout << "Debug Mode" << endl;
- }
-
- string FileToScan;
- string RequestString;
- bool ItIsACommand = false;
- bool ItIsAScan = false;
- bool GetXHDRs = false;
- bool ItIsAGBUdb = false;
- bool ItIsAReport = false;
-
- switch(argc) {
- case 2: {
- FileToScan = argv[1];
-
-
-
- if(0 == FileToScan.find("-status.second")) {
- ItIsAReport = true;
-
- RequestString = "<snf><xci><report><request><status class='second'/></request></report></xci></snf>";
-
- } else
-
- if(0 == FileToScan.find("-status.minute")) {
- ItIsAReport = true;
-
- RequestString = "<snf><xci><report><request><status class='minute'/></request></report></xci></snf>";
-
- } else
-
- if(0 == FileToScan.find("-status.hour")) {
- ItIsAReport = true;
-
- RequestString = "<snf><xci><report><request><status class='hour'/></request></report></xci></snf>";
-
- } else
-
- if(0 == FileToScan.find("-shutdown")) {
- ItIsACommand = true;
- if(DebugMode) { cout << "Command: shutdown" << endl; }
-
-
-
- RequestString = "<snf><xci><server><command command=\'shutdown\'/></server></xci></snf>\n";
-
- } else
-
- if('-' == FileToScan.at(0)) {
- goto BadCommandLine;
-
- } else {
- ItIsAScan = true;
- if(DebugMode) { cout << "Scan: " << FileToScan << endl; }
-
-
-
- RequestString = "<snf><xci><scanner><scan file=\'";
- RequestString.append(FileToScan);
- RequestString.append("\'/></scanner></xci></snf>\n");
- }
-
- break;
- }
-
- case 3: {
- string CommandString = argv[1];
- FileToScan = argv[2];
-
-
-
- if(0 == CommandString.find("-xhdr")) {
- ItIsAScan = true;
- GetXHDRs = true;
- if(DebugMode) {
- cout << "(xhdr)Scan: " << FileToScan << endl;
- }
-
-
-
- RequestString = "<snf><xci><scanner><scan file=\'";
- RequestString.append(FileToScan);
- RequestString.append("\' xhdr=\'yes\'/></scanner></xci></snf>\n");
-
- } else
-
-
-
- if(0 == CommandString.find("-source=")) {
- ItIsAScan = true;
-
- const int SourceIPIndex = CommandString.find("=") + 1;
- IP4Address SourceIP = CommandString.substr(SourceIPIndex);
-
- if(DebugMode) {
- cout << "([" << (string) SourceIP
- << "])Scan: " << FileToScan << endl;
- }
-
-
-
- RequestString = "<snf><xci><scanner><scan file=\'";
- RequestString.append(FileToScan);
- RequestString.append("\' ip=\'");
- RequestString.append((string) SourceIP);
- RequestString.append("\'/></scanner></xci></snf>\n");
-
- } else
-
-
-
- if(0 == CommandString.find("-test")) {
- ItIsAGBUdb = true;
-
-
-
- RequestString = "<snf><xci><gbudb><test ip=\'";
- RequestString.append(argv[2]);
- RequestString.append("\'/></gbudb></xci></snf>\n");
-
- } else
-
-
-
- if(0 == CommandString.find("-good")) {
- ItIsAGBUdb = true;
-
-
-
- RequestString = "<snf><xci><gbudb><good ip=\'";
- RequestString.append(argv[2]);
- RequestString.append("\'/></gbudb></xci></snf>\n");
-
- } else
-
-
-
- if(0 == CommandString.find("-bad")) {
- ItIsAGBUdb = true;
-
-
-
- RequestString = "<snf><xci><gbudb><bad ip=\'";
- RequestString.append(argv[2]);
- RequestString.append("\'/></gbudb></xci></snf>\n");
-
- } else
-
-
-
- if(0 == CommandString.find("-drop")) {
- ItIsAGBUdb = true;
-
-
-
- RequestString = "<snf><xci><gbudb><drop ip=\'";
- RequestString.append(argv[2]);
- RequestString.append("\'/></gbudb></xci></snf>\n");
-
- } else
-
-
-
- if(CommandString.at(0) != '-') {
- ItIsAScan = true;
- FileToScan = argv[2];
- if(DebugMode) {
- cout << "(compat)Scan: " << FileToScan << endl;
- }
-
-
-
- RequestString = "<snf><xci><scanner><scan file=\'";
- RequestString.append(FileToScan);
- RequestString.append("\'/></scanner></xci></snf>\n");
-
- } else
-
- goto BadCommandLine;
-
- break;
- }
-
- case 4: {
-
- string Command1String = argv[1];
- string Command2String = argv[2];
- FileToScan = argv[3];
-
-
-
- if(0 == Command1String.find("-source=")) {
- string tmp = Command2String;
- Command2String = Command1String;
- Command1String = tmp;
- }
-
- if(
- 0 == Command1String.find("-xhdr") &&
- 0 == Command2String.find("-source=")
- ) {
- ItIsAScan = true;
- GetXHDRs = true;
-
- const int SourceIPIndex = Command2String.find("=") + 1;
- IP4Address SourceIP = Command1String.substr(SourceIPIndex);
-
- if(DebugMode) {
- cout << "(xhdr [" << (string) SourceIP
- << "])Scan: " << FileToScan << endl;
- }
-
-
-
- RequestString = "<snf><xci><scanner><scan file=\'";
- RequestString.append(FileToScan);
- RequestString.append("\' xhdr=\'yes\' ip=\'");
- RequestString.append((string) SourceIP);
- RequestString.append("\'/></scanner></xci></snf>\n");
-
- } else
-
- goto BadCommandLine;
-
- break;
- }
-
- case 6: {
- string CommandString = argv[1];
-
-
-
- if(0 == CommandString.find("-set")) {
- ItIsAGBUdb = true;
-
-
-
- RequestString = "<snf><xci><gbudb><set ip=\'";
- RequestString.append(argv[2]);
- RequestString.append("\'");
-
- if(
- 'g' == argv[3][0] ||
- 'G' == argv[3][0] ||
- 'b' == argv[3][0] ||
- 'B' == argv[3][0] ||
- 'u' == argv[3][0] ||
- 'U' == argv[3][0] ||
- 'i' == argv[3][0] ||
- 'I' == argv[3][0]
- ) {
- RequestString.append(" type=\'");
- RequestString.append(argv[3]);
- RequestString.append("\'");
- }
- else if('-' != argv[3][0]) goto BadCommandLine;
-
- if(isdigit(argv[4][0])) {
- RequestString.append(" b=\'");
- RequestString.append(argv[4]);
- RequestString.append("\'");
- }
- else if('-' != argv[4][0]) goto BadCommandLine;
-
- if(isdigit(argv[5][0])) {
- RequestString.append(" g=\'");
- RequestString.append(argv[5]);
- RequestString.append("\'");
- }
- else if('-' != argv[5][0]) goto BadCommandLine;
-
- RequestString.append("/></gbudb></xci></snf>\n");
- }
-
- else goto BadCommandLine;
-
- break;
- }
-
-
-
- default: {
- BadCommandLine:
- cout
- << endl
- << VERSION_INFO << endl
- << endl
- << "Help:" << endl
- << endl
- << " To scan a message file use: " << endl
- << " SNFClient.exe [-xhdr] [-source=<IP4Address>] <FileNameToScan>" << endl
- << " or: SNFClient.exe <Authenticationxx> <FileNameToScan>" << endl
- << endl
- << " To test an IP with GBUdb use: " << endl
- << " SNFClient.exe -test <IP4Address>" << endl
- << endl
- << " To update GBUdb records use: " << endl
- << " SNFClient.exe -set <IP4Address> <flag> <bad> <good>" << endl
- << " or: SNFClient.exe -drop <IP4Address>" << endl
- << " or: SNFClient.exe -good <IP4Address>" << endl
- << " or: SNFClient.exe -bad <IP4Address>" << endl
- << endl
- << " To check SNFServer status use: " << endl
- << " SNFClient.exe -status.second" << endl
- << " or: SNFClient.exe -status.minute" << endl
- << " or: SNFClient.exe -status.hour" << endl
- << endl
- << " To shut down the SNFServer use: " << endl
- << " SNFClient.exe -shutdown" << endl
- << endl
- << " For more information see www.armresearch.com" << endl
- << " (C) 2007-2008 Arm Research Labs, LLC." << endl;
-
- return 0;
- }
- }
-
-
-
- if(DebugMode) { cout << RequestString << endl; }
-
-
-
- string ERRFname;
- ERRFname = argv[0];
- ERRFname.append(".err");
- const int FailSafeResult = 0;
- const int StatusReportError = 99;
-
-
-
- string ResultString;
- bool ConnectSuccess = false;
-
-
-
-
- const int ResultBufferSize = 4096;
- char ResultBuffer[ResultBufferSize+1];
- memset(ResultBuffer, 0, sizeof(ResultBuffer));
-
- const int Tries = 20;
- Sleeper SleepAfterAttempt(100);
-
- const int OpenTries = 90;
- Sleeper WaitForOpen(10);
-
- const int ReadTries = 900;
- Sleeper SleepBeforeReading(10);
-
-
-
-
- for(int tryagain = Tries; (0<tryagain) && (!ConnectSuccess); tryagain--) {
- try {
- ResultString = "";
- TCPHost SNFServer(9001);
- SNFServer.makeNonBlocking();
-
- for(int tries = OpenTries; 0 < tries; tries--) {
- try { SNFServer.open(); } catch(...) {}
- if(SNFServer.isOpen()) break;
- else WaitForOpen();
- }
-
- if(SNFServer.isOpen()) {
-
- SNFServer.transmit(
- RequestString.c_str(), RequestString.length());
-
- for(int tries = ReadTries; 0 < tries; tries--) {
- SleepBeforeReading();
- memset(ResultBuffer, 0, sizeof(ResultBuffer));
- SNFServer.receive(ResultBuffer, ResultBufferSize);
- ResultString.append(ResultBuffer);
- if(string::npos ==
- ResultString.rfind("</snf>",ResultString.length())) {
- continue;
- } else {
- ConnectSuccess = true;
- break;
- }
- }
- SNFServer.close();
- }
- } catch(...) { }
- if(!ConnectSuccess) SleepAfterAttempt();
- }
-
- if(!ConnectSuccess) {
-
- if(DebugMode) {
- cout << FileToScan << ": ";
- cout << "Could Not Connect!" << endl;
- }
-
- ofstream ERRF(ERRFname.c_str(), ios::out | ios::ate | ios::app);
- ERRF << ErrorMessage(argc, argv, "Could Not Connect!");
- ERRF.close();
-
- if(ItIsAReport) {
- return StatusReportError;
- }
- else return FailSafeResult;
- }
-
-
-
- if(DebugMode) { cout << ResultString << endl; }
-
- snf_xci Reader(ResultString);
- if(Reader.bad()) {
-
- if(DebugMode) {
- cout << FileToScan << ": ";
- cout << "Bad result from server!" << endl;
- cout << ResultString << endl;
- }
-
- ofstream ERRF(ERRFname.c_str(), ios::out | ios::ate | ios::app);
- ERRF << ErrorMessage(argc, argv,
- "Bad result from server! " + ResultString);
- return FailSafeResult;
- }
-
- if(0 < Reader.xci_error_message.length()) {
-
- if(DebugMode) {
- cout << FileToScan << ": ";
- cout << "XCI Error!: " << Reader.xci_error_message << endl;
- }
-
- ofstream ERRF(ERRFname.c_str(), ios::out | ios::ate | ios::app);
- ERRF << ErrorMessage(argc, argv,
- "XCI Error!: " + Reader.xci_error_message);
- return FailSafeResult;
- }
-
-
-
- if(ItIsACommand) {
- cout << " [Server Says: " << Reader.xci_server_response << "]" << endl;
-
- }
-
- else if(ItIsAScan) {
- int ResultCode = Reader.scanner_result_code;
-
- if(0 < Reader.scanner_result_xhdr.length()) {
- cout << endl << Reader.scanner_result_xhdr << endl;
- }
-
- if(DebugMode) { cout << "(" << ResultCode << ")"; }
-
- if( (0 < ResultCode) && (64 > ResultCode) ) {
- if(DebugMode) { cout << "[spam]" << endl; }
- return ResultCode;
- } else
- if(0 == ResultCode) {
- if(DebugMode) { cout << "[clean]" << endl; }
- return 0;
- } else {
- if(DebugMode) { cout << "[Fail Safe!]" << endl; }
- return FailSafeResult;
- }
- }
-
- else if(ItIsAGBUdb) {
- cout
- << "GBUdb Record for " << Reader.gbudb_result_ip << endl
- << " Type Flag: " << Reader.gbudb_result_type << endl
- << " Bad Count: " << Reader.gbudb_result_bad_count << endl
- << " Good Count: " << Reader.gbudb_result_good_count << endl
- << "Probability: " << Reader.gbudb_result_probability << endl
- << " Confidence: " << Reader.gbudb_result_confidence << endl
- << " Range: " << Reader.gbudb_result_range << endl
- << " Code: " << Reader.gbudb_result_code << endl
- << endl;
-
- return Reader.gbudb_result_code;
- }
-
- else if(ItIsAReport) {
- cout
- << endl << "<!-- Status Report -->" << endl
- << Reader.report_response << endl;
-
- return 0;
- }
-
-
- if(DebugMode) { cout << "End Of Logic [Fail Safe!!]" << endl; }
- return FailSafeResult;
- }
|