.pc/04_params.cpp.patch/havp/params.cpp
changeset 3 2d91f287b9b6
parent 2 5979296311cb
child 4 eda020b86e7f
equal deleted inserted replaced
2:5979296311cb 3:2d91f287b9b6
     1 /***************************************************************************
       
     2                           params.cpp  -  description
       
     3                              -------------------
       
     4     begin                : So Feb 20 2005
       
     5     copyright            : (C) 2005 by Peter Sebald / Christian Hilgers
       
     6     email                : christian@hilgers.ag
       
     7  ***************************************************************************/
       
     8 
       
     9 /***************************************************************************
       
    10  *                                                                         *
       
    11  *   This program is free software; you can redistribute it and/or modify  *
       
    12  *   it under the terms of the GNU General Public License as published by  *
       
    13  *   the Free Software Foundation; either version 2 of the License, or     *
       
    14  *   (at your option) any later version.                                   *
       
    15  *                                                                         *
       
    16  ***************************************************************************/
       
    17 
       
    18 #include "default.h"
       
    19 #include "params.h"
       
    20 #include "utils.h"
       
    21 
       
    22 #include <sys/socket.h>
       
    23 #include <netinet/in.h>
       
    24 #include <arpa/inet.h>
       
    25 #include <iostream>
       
    26 #include <fstream>
       
    27 #include <cstdlib>
       
    28 
       
    29 #ifndef INADDR_NONE
       
    30 #define INADDR_NONE ((unsigned long) -1)
       
    31 #endif
       
    32 
       
    33 map <string,string> Params::params;
       
    34 
       
    35 void Params::SetDefaults()
       
    36 {
       
    37     SetConfig("DISPLAYINITIALMESSAGES", "true");
       
    38     SetConfig("USER",		"havp");
       
    39     SetConfig("GROUP",		"havp");
       
    40     SetConfig("DAEMON",		"true");
       
    41     SetConfig("SERVERNUMBER",	"8");
       
    42     SetConfig("MAXSERVERS",	"150");
       
    43     SetConfig("PORT",		"8080");
       
    44     SetConfig("BIND_ADDRESS",	"");
       
    45     SetConfig("SOURCE_ADDRESS",	"");
       
    46     SetConfig("PARENTPROXY",	"");
       
    47     SetConfig("PARENTPORT",	"0");
       
    48     SetConfig("PARENTUSER",	"");
       
    49     SetConfig("PARENTPASSWORD",	"");
       
    50     SetConfig("ACCESSLOG",	ACCESSLOG);
       
    51     SetConfig("VIRUSLOG",	"");
       
    52     SetConfig("ERRORLOG",	ERRORLOG);
       
    53     SetConfig("TIMEFORMAT",	"%d/%m/%Y %H:%M:%S");
       
    54     SetConfig("LOG_OKS",	"true");
       
    55     SetConfig("LOGLEVEL",	"0");
       
    56     SetConfig("USESYSLOG",	"false");
       
    57     SetConfig("SYSLOGNAME",	"havp");
       
    58     SetConfig("SYSLOGFACILITY",	"daemon");
       
    59     SetConfig("SYSLOGLEVEL",	"info");
       
    60     SetConfig("SYSLOGVIRUSLEVEL","warning");
       
    61     SetConfig("SCANIMAGES",	"true");
       
    62     SetConfig("SKIPMIME",	"");
       
    63     SetConfig("SCANMIME",	"");
       
    64     SetConfig("MAXSCANSIZE",	"5000000");
       
    65     SetConfig("KEEPBACKBUFFER",	"200000");
       
    66     SetConfig("KEEPBACKTIME",	"5");
       
    67     SetConfig("TRICKLING",	"30");
       
    68     SetConfig("TRICKLINGBYTES",	"1");
       
    69     SetConfig("WHITELISTFIRST",	"true");
       
    70     SetConfig("WHITELIST",	WHITELISTFILE);
       
    71     SetConfig("BLACKLIST",	BLACKLISTFILE);
       
    72     SetConfig("TEMPLATEPATH",	TEMPLATEPATH);
       
    73     SetConfig("TEMPDIR",	"/var/tmp");
       
    74     SetConfig("SCANTEMPFILE",	"/var/tmp/havp/havp-XXXXXX");
       
    75     SetConfig("PIDFILE",	PIDFILE);
       
    76     SetConfig("TRANSPARENT",	"false");
       
    77     SetConfig("RANGE",		"false");
       
    78     SetConfig("PRELOADZIPHEADER","true");
       
    79     SetConfig("FORWARDED_IP",	"false");
       
    80     SetConfig("X_FORWARDED_FOR","false");
       
    81     SetConfig("STREAMUSERAGENT","");
       
    82     SetConfig("STREAMSCANSIZE",	"20000");
       
    83     SetConfig("DBRELOAD",	"60");
       
    84     SetConfig("FAILSCANERROR",	"true");
       
    85     SetConfig("MAXDOWNLOADSIZE","0");
       
    86     SetConfig("SCANNERTIMEOUT",	"10");
       
    87     SetConfig("IGNOREVIRUS",	"");
       
    88     SetConfig("DISABLELOCKINGFOR","AVG:ALL");
       
    89 //SCANNERS
       
    90     SetConfig("ENABLECLAMLIB","false");
       
    91         SetConfig("CLAMDBDIR","");
       
    92         SetConfig("CLAMBLOCKBROKEN","false");
       
    93         SetConfig("CLAMBLOCKMAX","false");
       
    94         SetConfig("CLAMBLOCKENCRYPTED","false");
       
    95         SetConfig("CLAMMAXSCANSIZE","20");
       
    96         SetConfig("CLAMMAXFILES","50");
       
    97         SetConfig("CLAMMAXFILESIZE","100");
       
    98         SetConfig("CLAMMAXRECURSION","8");
       
    99     SetConfig("ENABLECLAMD","false");
       
   100 	SetConfig("CLAMDSOCKET","/tmp/clamd");
       
   101 	SetConfig("CLAMDSERVER","");
       
   102 	SetConfig("CLAMDPORT","3310");
       
   103     SetConfig("ENABLEAVG","false");
       
   104         SetConfig("AVGSERVER","127.0.0.1");
       
   105         SetConfig("AVGPORT","55555");
       
   106     SetConfig("ENABLEAVESERVER","false");
       
   107         SetConfig("AVESOCKET","/var/run/aveserver");
       
   108     SetConfig("ENABLEFPROT","false");
       
   109         SetConfig("FPROTPORT","10200");
       
   110         SetConfig("FPROTSERVER","127.0.0.1");
       
   111         SetConfig("FPROTOPTIONS","");
       
   112     SetConfig("ENABLENOD32","false");
       
   113         SetConfig("NOD32SOCKET","/tmp/nod32d.sock");
       
   114         SetConfig("NOD32VERSION","25");
       
   115     SetConfig("ENABLETROPHIE","false");
       
   116         SetConfig("TROPHIEMAXFILES","50");
       
   117         SetConfig("TROPHIEMAXFILESIZE","10");
       
   118         SetConfig("TROPHIEMAXRATIO","250");
       
   119     SetConfig("ENABLESOPHIE","false");
       
   120 	SetConfig("SOPHIESOCKET","/var/run/sophie");
       
   121     SetConfig("ENABLEAVAST","false");
       
   122         SetConfig("AVASTSOCKET","/var/run/avast4/local.sock");
       
   123         SetConfig("AVASTSERVER","");
       
   124         SetConfig("AVASTPORT","5036");
       
   125     SetConfig("ENABLEARCAVIR","false");
       
   126         SetConfig("ARCAVIRSOCKET","/var/run/arcavird.socket");
       
   127         SetConfig("ARCAVIRVERSION","2007");
       
   128     SetConfig("ENABLEDRWEB","false");
       
   129         SetConfig("DRWEBSOCKET","/var/drweb/run/.daemon");
       
   130         SetConfig("DRWEBSERVER","");
       
   131         SetConfig("DRWEBPORT","3000");
       
   132         SetConfig("DRWEBHEURISTIC","true");
       
   133         SetConfig("DRWEBMALWARE","true");
       
   134 }
       
   135 
       
   136 bool Params::ReadConfig( string file )
       
   137 {
       
   138     ifstream input( file.c_str() );
       
   139 
       
   140     if ( !input )
       
   141     {
       
   142         cerr << "Could not open config file: " << file << endl;
       
   143         return false;
       
   144     }
       
   145 
       
   146     string::size_type Position;
       
   147     string line, key, val;
       
   148 
       
   149     while ( input )
       
   150     {
       
   151         getline( input, line );
       
   152 
       
   153         //Strip whitespace from beginning and end
       
   154         if ( (Position = line.find_first_not_of(" \t")) != string::npos )
       
   155         {
       
   156             line = line.substr(Position, (line.find_last_not_of(" \t", string::npos) - Position) + 1);
       
   157         }
       
   158 
       
   159         //Read next if nothing found
       
   160         if ( (Position == string::npos) || (line.size() == 0) ) continue;
       
   161 
       
   162         //Read next if commented
       
   163         if ( line.substr(0, 1) == "#" ) continue;
       
   164 
       
   165         //Find key and value
       
   166         if ( (Position = line.find_first_of(" \t")) != string::npos )
       
   167         {
       
   168             key = line.substr(0, Position);
       
   169 
       
   170             if ( key == "REMOVETHISLINE" )
       
   171             {
       
   172                 cout << "Configuration is not edited!" << endl;
       
   173                 cout << "You must delete REMOVETHISLINE option." << endl;
       
   174                 cout << "Review the configuration carefully. :)" << endl;
       
   175                 return false;
       
   176             }
       
   177 
       
   178             if ( (Position = line.find_first_not_of(" \t", Position + 1)) == string::npos )
       
   179             {
       
   180                 cout << "Invalid Config Line: " << line << endl;
       
   181                 return false;
       
   182             }
       
   183 
       
   184             val = line.substr( Position );
       
   185 
       
   186             Params::SetConfig( key, val );
       
   187         }
       
   188         else
       
   189         {
       
   190             cout << "Invalid Config Line: " << line << endl;
       
   191             return false;
       
   192         }
       
   193     }
       
   194 
       
   195     input.close();
       
   196 
       
   197     return true;
       
   198 }
       
   199 
       
   200 void Params::SetConfig( string param, string value )
       
   201 {
       
   202     string TempParams[] = {CONFIGPARAMS};
       
   203     bool ParamFound = false;
       
   204 
       
   205     param = UpperCase(param);
       
   206 
       
   207     for ( unsigned int i = 0; i < sizeof(TempParams)/sizeof(string); i++ )
       
   208     {
       
   209         if ( param == TempParams[i] )
       
   210         {
       
   211             ParamFound = true;
       
   212         }
       
   213     }
       
   214 
       
   215     if ( ParamFound )
       
   216     {
       
   217         if ( UpperCase(value) == "TRUE" || UpperCase(value) == "FALSE" )
       
   218         {
       
   219             value = UpperCase(value);
       
   220         }
       
   221 
       
   222         params[param] = value;
       
   223     }
       
   224     else
       
   225     {
       
   226         cout << "Unknown Config Parameter: " << param << endl;
       
   227         cout << "Exiting.." << endl;
       
   228         exit(1);
       
   229     }
       
   230 }
       
   231 
       
   232 int Params::GetConfigInt( string param )
       
   233 {
       
   234     return atoi( params[param].c_str() );
       
   235 }
       
   236 
       
   237 bool Params::GetConfigBool( string param )
       
   238 {
       
   239     if ( params[param] == "TRUE" )
       
   240     {
       
   241         return true;
       
   242     }
       
   243     else
       
   244     {
       
   245         return false;
       
   246     }
       
   247 }
       
   248 
       
   249 string Params::GetConfigString( string param )
       
   250 {
       
   251     return params[param];
       
   252 }
       
   253 
       
   254 void Params::ShowConfig( string cfgfile )
       
   255 {
       
   256     cout << endl << "# Using HAVP config: " << cfgfile << endl << endl;
       
   257     typedef map<string,string>::const_iterator CI;
       
   258     for(CI p = params.begin(); p != params.end(); ++p)
       
   259     {
       
   260         cout << p->first << "=" << p->second << '\n';
       
   261     }
       
   262     cout << endl;
       
   263 }
       
   264 
       
   265 void Params::Usage()
       
   266 {
       
   267     cout << endl << "Usage: havp [Options]" << endl << endl;
       
   268     cout << "HAVP Version " << VERSION << endl << endl;
       
   269     cout << "Possible options are:" << endl;
       
   270     cout << "--help | -h                         This pamphlet" << endl;
       
   271     cout << "--conf-file=FileName | -c Filename  Use this Config-File" << endl;
       
   272     cout << "--show-config | -s                  Show configuration HAVP is using" << endl << endl;
       
   273 }
       
   274 
       
   275 bool Params::SetParams( int argvT, char* argcT[] )
       
   276 {
       
   277     string option, value;
       
   278     string::size_type i1, i2;
       
   279 
       
   280     string cfgfile = CONFIGFILE;
       
   281     bool showconf = false;
       
   282 
       
   283     SetDefaults();
       
   284 
       
   285     while ( --argvT )
       
   286     {
       
   287         value = *++argcT;
       
   288         i1 = value.find_first_not_of("-");
       
   289 
       
   290         //No GNU options
       
   291         if ( i1 == 1 )
       
   292         {
       
   293             option = value.substr(i1, 1);
       
   294 
       
   295             if ( option == "c" )
       
   296             {
       
   297                 --argvT;
       
   298 
       
   299                 if ( argvT == 0 )
       
   300                 {
       
   301                     Usage();
       
   302                     return false;
       
   303                 }
       
   304                 value = *++argcT;
       
   305             }
       
   306             else if ( option == "s" )
       
   307             {
       
   308                 showconf = true;
       
   309             }
       
   310             else
       
   311             {
       
   312                 Usage();
       
   313                 return false;
       
   314             }
       
   315         }
       
   316         //GNU options
       
   317         else if ( i1 == 2 )
       
   318         {
       
   319             if ( (i2 = value.find("=")) != string::npos )
       
   320             {
       
   321                 option = value.substr(i1, i2 - i1);
       
   322 
       
   323                 if ( value.size() > i2 + 1 )
       
   324                 {
       
   325                     value = value.substr(i2 + 1);
       
   326                 }
       
   327                 else
       
   328                 {
       
   329                     Usage();
       
   330                     return false;
       
   331                 }
       
   332             }
       
   333             else
       
   334             {
       
   335                 option = value.substr(i1);
       
   336                 value = "";
       
   337             }
       
   338         }
       
   339         else
       
   340         {
       
   341             Usage();
       
   342             return false;
       
   343         }
       
   344 
       
   345         if ( option == "help" )
       
   346         {
       
   347             Usage();
       
   348             return false;
       
   349         }
       
   350         else if ( option == "show-config" )
       
   351         {
       
   352             showconf = true;
       
   353         }
       
   354         else if ( option == "conf-file" || option == "c" )
       
   355         {
       
   356             if (value == "")
       
   357             {
       
   358                 Usage();
       
   359                 return false;
       
   360             }
       
   361 
       
   362             cfgfile = value;
       
   363         }
       
   364         else if ( showconf == true )
       
   365         {
       
   366             //Nothing: prevent Usage
       
   367         } 
       
   368         else
       
   369         {
       
   370             Usage();
       
   371             return false;
       
   372         }
       
   373     }
       
   374 
       
   375     if ( ReadConfig( cfgfile ) == false )
       
   376     {
       
   377         return false;
       
   378     }
       
   379 
       
   380     if ( showconf == true )
       
   381     {
       
   382        ShowConfig( cfgfile );
       
   383        return false;
       
   384     }
       
   385 
       
   386     return TestConfig();
       
   387 }
       
   388 
       
   389 //Test that some options are sane
       
   390 bool Params::TestConfig()
       
   391 {
       
   392     if ( Params::GetConfigInt("SERVERNUMBER") < 1 )
       
   393     {
       
   394         cout << "Invalid Config: SERVERNUMBER needs to be greater than 0" << endl;
       
   395         return false;
       
   396     }
       
   397     if ( Params::GetConfigString("ACCESSLOG").substr(0,1) != "/"
       
   398          || (Params::GetConfigString("VIRUSLOG") != "" && Params::GetConfigString("VIRUSLOG").substr(0,1) != "/")
       
   399          || Params::GetConfigString("ERRORLOG").substr(0,1) != "/" )
       
   400     {
       
   401         cout << "Invalid Config: Log paths need to be abolute" << endl;
       
   402         return false;
       
   403     }
       
   404     if ( Params::GetConfigString("SCANTEMPFILE").find("XXXXXX") == string::npos )
       
   405     {
       
   406         cout << "Invalid Config: SCANTEMPFILE must contain string \"XXXXXX\"" << endl;
       
   407         return false;
       
   408     }
       
   409     if ( Params::GetConfigInt("MAXSERVERS") > 500 )
       
   410     {
       
   411         cout << "Note: MAXSERVERS is unusually high! You are sure you want this?" << endl;
       
   412     }
       
   413     if ( Params::GetConfigString("BIND_ADDRESS") == "NULL" ) Params::SetConfig("BIND_ADDRESS","");
       
   414     if ( Params::GetConfigString("BIND_ADDRESS") != "" )
       
   415     {
       
   416         if ( inet_addr( Params::GetConfigString("BIND_ADDRESS").c_str() ) == INADDR_NONE )
       
   417         {
       
   418             cout << "Invalid Config: Invalid BIND_ADDRESS" << endl;
       
   419             return false;
       
   420         }
       
   421     }
       
   422     if ( Params::GetConfigString("SOURCE_ADDRESS") == "NULL" ) Params::SetConfig("SOURCE_ADDRESS","");
       
   423     if ( Params::GetConfigString("SOURCE_ADDRESS") != "" )
       
   424     {
       
   425         if ( inet_addr( Params::GetConfigString("SOURCE_ADDRESS").c_str() ) == INADDR_NONE )
       
   426         {
       
   427             cout << "Invalid Config: Invalid SOURCE_ADDRESS" << endl;
       
   428             return false;
       
   429         }
       
   430     }
       
   431     if ( Params::GetConfigString("PARENTPROXY") != "" && Params::GetConfigInt("PARENTPORT") < 1 )
       
   432     {
       
   433         cout << "Invalid Config: Invalid PARENTPROXY/PARENTPORT" << endl;
       
   434         return false;
       
   435     }
       
   436     if ( Params::GetConfigInt("TRICKLING") > 0 && Params::GetConfigInt("TRICKLINGBYTES") < 1 )
       
   437     {
       
   438         cout << "Invalid Config: TRICKLINGBYTES needs to be greater than 0" << endl;
       
   439         return false;
       
   440     }
       
   441 
       
   442     return true;
       
   443 }