"Fossies" - the Fresh Open Source Software Archive

Member "hermes-1.9/src/Configfile.cpp.in" (29 Jun 2014, 5494 Bytes) of package /linux/privat/hermes-1.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /**
    2  * hermes antispam proxy
    3  * Copyright (C) 2006, 2007 Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
    4  *
    5  * This program is free software; you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation; version 2 of the License
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License along
   15  * with this program; if not, write to the Free Software Foundation, Inc.,
   16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   17  *
   18  * @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
   19  */
   20 #include "Configfile.h"
   21 
   22 #include <unistd.h>
   23 
   24 extern LOGGER_CLASS hermes_log;
   25 
   26 /**
   27  * default config
   28  *
   29  */
   30 Configfile::Configfile()
   31 {
   32 %templ_default_values%
   33 }
   34 
   35 void Configfile::parse(string file)
   36 {
   37   ifstream f;
   38   char line[255];
   39   int equalpos;
   40 
   41   LINF("parsing "+Utils::get_canonical_filename(file)+" configuration file");
   42   f.open(file.c_str(),ios::in);
   43   while(!f.eof())
   44   {
   45     f.getline(line,255);
   46     string l=Utils::trim(line);
   47     if('#'!=l[0]&&l!=""&&l.find("="))
   48     {
   49       equalpos=l.find("=");
   50       string option=Utils::trim(l.substr(0,equalpos));
   51       string value=Utils::trim(l.substr(equalpos+1));
   52       //if(false==cfg.getBackground())
   53       //  cout << option << " -> " << value << endl;
   54 
   55       //this is a bit of a hack, but simplifies a lot this function
   56       #define PARSE_INT(x,y) if(x==option) y=Configfile::parseAsInt(value); else
   57       #define PARSE_BOOL(x,y) if(x==option) y=Configfile::parseAsBool(value); else
   58       #define PARSE_STRING(x,y) if(x==option) y=Configfile::parseAsString(value); else
   59       #define PARSE_LIST(x,y) if(x==option) y=Configfile::parseAsList(value); else
   60 
   61       %templ_parsevars%
   62       {
   63         throw Exception("Option \""+option+"\" with value \""+value+"\" is not recognized",__FILE__,__LINE__);
   64       }
   65       #undef PARSE_INT
   66       #undef PARSE_BOOL
   67       #undef PARSE_STRING
   68       #undef PARSE_LIST
   69     }
   70   }
   71   #ifndef WIN32
   72     uid=Utils::usertouid(user);
   73     gid=Utils::grouptogid(group);
   74   #endif //WIN32
   75   f.close();
   76 }
   77 
   78 //again, this is a BIG HACK, but it simplifies code a lot
   79 #define GET_VAR(x,y,z) z Configfile::x(){ return y;}
   80 
   81 GET_VAR(getUid,uid,int)
   82 GET_VAR(getGid,gid,int)
   83 %templ_getmethods%
   84 
   85 #undef GET_VAR
   86 
   87 void Configfile::validateConfig()
   88 {
   89   #ifndef WIN32
   90     //check if we are root if we want to bind to a port lower than 1024
   91     if(getuid()!=0&&listening_port<1024)
   92       throw Exception(_("You can't bind to a port lower than 1024 without being root"),__FILE__,__LINE__);
   93   #endif //WIN32
   94 
   95   #ifdef HAVE_SSL
   96     //check if ssl is usable
   97     if(!Utils::file_exists(certificate_file))
   98       throw Exception("Certificate file "+certificate_file+" doesn't exist.\nTo generate a certificate look in hermesrc.example, there is an example there.",__FILE__,__LINE__);
   99 
  100     if(!Utils::file_exists(private_key_file))
  101       throw Exception("Private key file "+private_key_file+" doesn't exist.\nTo generate a private key look in hermesrc.example, there is an example there.",__FILE__,__LINE__);
  102   #endif //HAVE_SSL
  103 
  104   #ifndef WIN32
  105   //check if chroot dir exist //TODO: check that files needed in chroot exist
  106   //for now only /etc/resolv.conf, but we're working on it :-D
  107   if(""!=chroot&&!Utils::dir_exists(chroot))
  108     throw Exception("Directory "+chroot+" doesn't exist, can't chroot to it.",__FILE__,__LINE__);
  109   #endif //WIN32
  110 
  111   //check if we have submit_stats on but no user and password
  112   if(getSubmitStats()&&(""==getSubmitStatsUsername()||""==getSubmitStatsPassword()))
  113     throw Exception("You have configured hermes to send stats, but have not configured a username or password.\n"
  114       "If you don't have one, go to http://www.hermes-project.com and register there",__FILE__,__LINE__);
  115 
  116   #ifndef HAVE_SSL
  117   //check if we have activated submit_stats_ssl not having ssl activated
  118   if(getSubmitStatsSsl())
  119     throw Exception("You have configured stats submission through SSL, but hermes was compiled without SSL support",__FILE__,__LINE__);
  120   #endif //HAVE_SSL
  121   
  122 }
  123 
  124 string Configfile::parseAsString(string str)
  125 {
  126   //remove "" round the string
  127   if('"'==str[0])
  128     str=str.substr(1);
  129   if('"'==str[str.length()-1])
  130     str=str.substr(0,str.length()-1);
  131 
  132   return str;
  133 }
  134 
  135 bool Configfile::parseAsBool(string str)
  136 {
  137   if("yes"==str||"on"==str||"1"==str||"true"==str)
  138     return true;
  139   else
  140     return false;
  141 }
  142 
  143 long int Configfile::parseAsInt(string str)
  144 {
  145   long int value;
  146 
  147   errno=0; //to know why we do this, read NOTES on strtol(3)
  148   value=strtol(str.c_str(),NULL,10);
  149   if(errno)
  150     throw Exception("Error parsing as int ("+Utils::errnotostrerror(errno)+")",__FILE__,__LINE__);
  151 
  152   return value;
  153 }
  154 
  155 list<string> Configfile::parseAsList(string str)
  156 {
  157   list<string> tmpList;
  158   string::size_type startpos=0,endpos=0,len;
  159   string tmpstr;
  160 
  161   str=Configfile::parseAsString(str); //remove quotes around string
  162 
  163   len=str.length();
  164   while(startpos<len&&string::npos!=endpos)
  165   {
  166     endpos=str.find(',',startpos);
  167     if(string::npos==endpos)
  168       tmpstr=str.substr(startpos);
  169     else
  170       tmpstr=str.substr(startpos,endpos-startpos);
  171     tmpList.push_back(Utils::trim(tmpstr));
  172     startpos=endpos+1;
  173   }
  174 
  175   return tmpList;
  176 }