• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

yapet/cfgfile.cc

Go to the documentation of this file.
00001 // $Id: cfgfile.cc 3356 2010-09-18 21:06:17Z rafi $
00002 //
00003 // Copyright (C) 2008-2010  Rafael Ostertag
00004 //
00005 // This file is part of YAPET.
00006 //
00007 // YAPET is free software: you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the Free Software
00009 // Foundation, either version 3 of the License, or (at your option) any later
00010 // version.
00011 //
00012 // YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
00013 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 // FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00015 // details.
00016 //
00017 // You should have received a copy of the GNU General Public License along with
00018 // YAPET.  If not, see <http://www.gnu.org/licenses/>.
00019 //
00020 
00021 #ifdef HAVE_CONFIG_H
00022 # include <config.h>
00023 #endif
00024 
00025 #ifdef HAVE_UNISTD_H
00026 # include <unistd.h>
00027 #endif
00028 
00029 #ifdef HAVE_STDLIB_H
00030 # include <stdlib.h>
00031 #endif
00032 
00033 #ifdef HAVE_PWD_H
00034 # include <pwd.h>
00035 #endif
00036 
00037 #ifdef HAVE_FSTREAM
00038 # include <fstream>
00039 #endif
00040 
00041 #ifdef HAVE_SSTREAM
00042 # include <sstream>
00043 #endif
00044 
00045 #ifdef CFGDEBUG
00046 # ifdef HAVE_IOSTREAM
00047 #  include <iostream>
00048 # endif
00049 #endif
00050 
00051 #ifdef HAVE_ASSERT_H
00052 # include <assert.h>
00053 #endif
00054 
00055 #include "consts.h"
00056 #include "cfgfile.h"
00057 #include "cfg.h"
00058 
00059 using namespace YAPET::CONFIG;
00060 
00061 std::string
00062 ConfigFile::getHomeDir() const {
00063     std::string homedir ("");
00064 #ifdef HAVE_GETENV
00065     char* hd = getenv ("HOME");
00066 
00067     if (hd != NULL) {
00068         homedir = hd;
00069 
00070         if (homedir[homedir.length() ] != '/')
00071             homedir.push_back ('/');
00072 
00073         return homedir;
00074     }
00075 
00076 #endif
00077 #if defined(HAVE_GETPWUID) && defined (HAVE_GETUID)
00078     struct passwd* pwd;
00079     pwd = getpwuid (getuid() );
00080 
00081     if (pwd != NULL) {
00082         homedir = pwd->pw_dir;
00083 
00084         if (homedir[homedir.length() ] != '/')
00085             homedir.push_back ('/');
00086 
00087         return homedir;
00088     }
00089 
00090 #endif
00091     assert (!homedir.empty() );
00092     return homedir;
00093 }
00094 
00095 void
00096 ConfigFile::parseFile() {
00097 #ifdef CFGDEBUG
00098     std::cout << " === ";
00099     std::cout << "ConfigFile::parseFile()";
00100     std::cout << ":" << std::endl;
00101 #endif
00102 
00103     try {
00104         std::ifstream cfgsin (cfgfilepath.c_str() );
00105 
00106         if (!cfgsin)
00107             return;
00108 
00109         const int MAX_LENGTH = 1024;
00110         char line[MAX_LENGTH];
00111 
00112         while (cfgsin.getline (line, MAX_LENGTH) ) {
00113             std::string l (line);
00114 
00115         ReadResult res;
00116         if ( (res = readOption<std::string>(l, "load=", filetoload)) != OPTION_NOT_FOUND) {
00117         if (res == OPTION_EMPTY)
00118             continue;
00119 
00120         // Append the suffix if necessary
00121         if ( filetoload.find (YAPET::CONSTS::Consts::getDefaultSuffix(),
00122                       filetoload.length() -
00123                       YAPET::CONSTS::Consts::getDefaultSuffix().length() )
00124              == std::string::npos )
00125             filetoload += YAPET::CONSTS::Consts::getDefaultSuffix();
00126         
00127         // Check if we have to replace the ~. It will only be replaced
00128         // if it is the first character of the file to load.
00129         if (filetoload.at (0) == '~') {
00130 #ifdef CFGDEBUG
00131                     std::cout << "\tReplace ~ in load option by " <<
00132             getHomeDir() << std::endl;
00133 #endif
00134                     filetoload.erase (0, 1);
00135                     filetoload = getHomeDir() + filetoload;
00136                 }
00137 
00138                 continue;
00139             }
00140 
00141         if (readOption<unsigned int>(l, "locktimeout=", locktimeout) != OPTION_NOT_FOUND)
00142         continue;
00143 
00144         if (readOption<bool>(l, "checkfsecurity=", usefsecurity) != OPTION_NOT_FOUND)
00145         continue;
00146 
00147         if (readOption<bool>(l, "allowlockquit=", allowlockquit) != OPTION_NOT_FOUND)
00148         continue;
00149 
00150         if (readOption<unsigned int>(l, "pwinputtimeout=", pwinputtimeout) != OPTION_NOT_FOUND)
00151         continue;
00152 
00153             // Yes, the file can say that it should be ignored!
00154         if (readOption<bool>(l, "ignorerc=", ignorerc) != OPTION_NOT_FOUND)
00155         continue;
00156 
00157         std::string tmp;
00158         if ( (res = readOption<std::string>(l, "pwgen_rng=", tmp)) != OPTION_NOT_FOUND) {
00159         if (res == OPTION_EMPTY)
00160             continue;
00161 
00162         if (tmp == "devrandom") {
00163             pwgen_rng = YAPET::PWGEN::DEVRANDOM;
00164             continue;
00165         }
00166 
00167         if (tmp == "devurandom") {
00168             pwgen_rng = YAPET::PWGEN::DEVURANDOM;
00169             continue;
00170         }
00171             
00172         if (tmp == "lrand48") {
00173             pwgen_rng = YAPET::PWGEN::LRAND48;
00174             continue;
00175         }
00176 
00177         if (tmp == "rand") {
00178             pwgen_rng = YAPET::PWGEN::RAND;
00179             continue;
00180         }
00181 
00182         pwgen_rng = YAPET::PWGEN::AUTO;
00183         continue;
00184         }
00185 
00186         if (readOption<size_t>(l, "pwgen_pwlen=", pwgen_pwlen) != OPTION_NOT_FOUND)
00187         continue;
00188 
00189         if (readOption<bool>(l, "pwgen_letters=", pwgen_letters) != OPTION_NOT_FOUND)
00190         continue;
00191 
00192         if (readOption<bool>(l, "pwgen_digits=", pwgen_digits) != OPTION_NOT_FOUND)
00193         continue;
00194 
00195         if (readOption<bool>(l, "pwgen_punct=", pwgen_punct) != OPTION_NOT_FOUND)
00196         continue;
00197 
00198         if (readOption<bool>(l, "pwgen_special=", pwgen_special) != OPTION_NOT_FOUND)
00199         continue;
00200 
00201         if (readOption<bool>(l, "pwgen_other=", pwgen_other) != OPTION_NOT_FOUND)
00202         continue;
00203         }
00204 
00205         cfgsin.close();
00206     } catch (...) {
00207         // lazy me
00208     }
00209 }
00210 
00211 ConfigFile::ConfigFile (std::string cfgfile) : filetoload (Config::getDefPetfile() ),
00212                            usefsecurity (Config::getDefFilesecurity() ),
00213                            locktimeout (Config::getDefTimeout() ),
00214                            pwinputtimeout (Config::getDefPwInputTimeout() ),
00215                            allowlockquit (Config::getDefAllowLockQuit() ),
00216                            ignorerc (false),
00217                            cfgfilepath (""),
00218                            opensuccess (true),
00219                            pwgen_letters(Config::getDefCPoolLetters()),
00220                            pwgen_digits(Config::getDefCPoolDigits()),
00221                            pwgen_punct(Config::getDefCPoolPunct()),
00222                            pwgen_special(Config::getDefCPoolSpecial()),
00223                            pwgen_other(Config::getDefCPoolOther()),
00224                            pwgen_rng(Config::getDefPWGenRNG()),
00225                            pwgen_pwlen(Config::getDefPWLength())
00226                            
00227 {
00228 #ifdef CFGDEBUG
00229     std::cout << " === ";
00230     std::cout << "ConfigFile::ConfigFile(std::string)";
00231     std::cout << ":" << std::endl;
00232 #endif
00233 
00234     if (cfgfile.empty() ) {
00235         cfgfilepath = getHomeDir() +
00236                       YAPET::CONSTS::Consts::getDefaultRCFilename();
00237 #ifdef CFGDEBUG
00238         std::cout << "\tcfgfile.empty() == true" << std::endl;
00239         std::cout << "\tcfgfilepath: " << cfgfilepath << std::endl;
00240 #endif
00241     } else {
00242         cfgfilepath = cfgfile;
00243 #ifdef CFGDEBUG
00244         std::cout << "\tcfgfile.empty() == false" << std::endl;
00245         std::cout << "\tcfgfilepath = cfgfile: " << cfgfilepath << std::endl;
00246 #endif
00247     }
00248 
00249     if (access (cfgfilepath.c_str(), R_OK | F_OK) == -1) {
00250 #ifdef CFGDEBUG
00251         std::cout << "\taccess to " << cfgfilepath << " denied." << std::endl;
00252 #endif
00253         cfgfilepath.clear();
00254         opensuccess = false;
00255         return;
00256     }
00257 
00258     parseFile();
00259 }
00260 
00261 ConfigFile::ConfigFile (const ConfigFile& cfgfile) : 
00262     filetoload (cfgfile.filetoload),
00263     usefsecurity(cfgfile.usefsecurity),
00264     locktimeout (cfgfile.locktimeout),
00265     pwinputtimeout (cfgfile.pwinputtimeout),
00266     allowlockquit (cfgfile.allowlockquit),
00267     cfgfilepath (cfgfile.cfgfilepath),
00268     opensuccess (cfgfile.opensuccess),
00269     pwgen_letters (cfgfile.pwgen_letters),
00270     pwgen_digits (cfgfile.pwgen_digits),
00271     pwgen_punct (cfgfile.pwgen_punct),
00272     pwgen_special (cfgfile.pwgen_special),
00273     pwgen_other (cfgfile.pwgen_other),
00274     pwgen_rng (cfgfile.pwgen_rng),
00275     pwgen_pwlen (cfgfile.pwgen_pwlen) {
00276 }
00277 
00278 const ConfigFile&
00279 ConfigFile::operator= (const ConfigFile & cfgfile) {
00280     if (&cfgfile == this)
00281         return *this;
00282 
00283     filetoload = cfgfile.filetoload;
00284     usefsecurity = cfgfile.usefsecurity;
00285     locktimeout = cfgfile.locktimeout;
00286     pwinputtimeout = cfgfile.pwinputtimeout;
00287     allowlockquit = cfgfile.allowlockquit;
00288     cfgfilepath = cfgfile.cfgfilepath;
00289     opensuccess = cfgfile.opensuccess;
00290     pwgen_letters = cfgfile.pwgen_letters;
00291     pwgen_digits = cfgfile.pwgen_digits;
00292     pwgen_punct = cfgfile.pwgen_punct;
00293     pwgen_special = cfgfile.pwgen_special;
00294     pwgen_other = cfgfile.pwgen_other;
00295     pwgen_rng = cfgfile.pwgen_rng;
00296     pwgen_pwlen = cfgfile.pwgen_pwlen;
00297 
00298     return *this;
00299 }

Generated on Sun Sep 19 2010 15:37:13 for YAPET by  doxygen 1.7.1