/* * Localwebfaker - Web sites faker for LAN * Copyright (C) 2007-2009 Lucas J. González * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see userConfDir()) ) qFatal( "Config::creaConfDir(): Error" ) ; std::cout << "Creating: Personal directory" << std::endl ; std::cout << "Creating: Config directory" << std::endl ; } void backupCreator:: creaLogsDir () { Comun *comun = new Comun(); QDir root("/"); QDir Logs(comun->LogsDir()); if( !root.mkpath(comun->LogsDir()) ) qFatal( "Error creating logs directory" ) ; std::cout << "Creating: Logs directory" << std::endl ; } void backupCreator::Respaldar() { std::cout << "----------" << std::endl ; creaConfDir(); creaLogsDir(); ipfwConfig() ; resolvConfig() ; IptablesConfig(); hostConfig(); ApacheStatusConfig(); std::cout << "----------" << std::endl ; } void backupCreator:: hostConfig() { Comun *comun = new Comun() ; QFile *hostsFile = new QFile(comun->hostsFile()) ; QFile *hostsRestFile = new QFile(comun->restHostsFile()) ; if( hostsRestFile->exists() ) { if ( (hostsRestFile->remove () ) == false ) { QString *errmsg = new QString("Fallo al borrar el archivo ") ; errmsg->append( comun->restHostsFile() ) ; qFatal(qPrintable(*errmsg)) ; } } if ( !hostsFile->copy(comun->restHostsFile() ) ) { QString *errmsg = new QString("Imposible copiar el archivo ") ; errmsg->append( comun->hostsFile() ) ; qFatal(qPrintable(*errmsg)) ; } std::cout << "Backing up: hosts file"<< std::endl ; hostsFile->close(); hostsRestFile->close(); } void backupCreator::ApacheStatusConfig() { w_ApacheStatus_Rest(apacheEjecutandose()) ; std::cout << "Backing up: Web server status" << std::endl ; } bool backupCreator::apacheEjecutandose() { int errordesalida ; QProcess *checkApacheStatus = new QProcess(); QString *comando = new QString("ps -A"); QByteArray *proceso = new QByteArray(" apache2"); QByteArray *result = new QByteArray() ; connect(checkApacheStatus, SIGNAL(error( QProcess::ProcessError)), this, SLOT(printPsapacheError(QProcess::ProcessError)) ) ; checkApacheStatus->start(*comando) ; if (!checkApacheStatus->waitForStarted() ) { std::cout << "checkApacheStatus->waitForStarted() failed" << std::endl ; exit(1); } if (!checkApacheStatus->waitForFinished()) { std::cout << "checkApacheStatus->waitForFinished() failed" << std::endl ; exit(1); } if ( (errordesalida = checkApacheStatus->exitCode() ) != 0 ) { std::cout << "apacheEjecutandose() error: " << errordesalida << std::endl ; exit(errordesalida) ; } *result = checkApacheStatus->readAll(); if( result->contains ( *proceso ) ) { return true ; } return false ; } void backupCreator::w_ApacheStatus_Rest (bool stat) { char estado; Comun *miConf = new Comun(); QFile FApacheStatus( miConf->restApacheStatus() ); if (stat == 0) estado = '0'; else estado = '1'; FApacheStatus.open(QIODevice::WriteOnly | QIODevice::Text); FApacheStatus.write(&estado, sizeof(char)); FApacheStatus.putChar('\n'); FApacheStatus.close(); } void backupCreator::resolvConfig() { QFile *resolvFile = new QFile( Comun::resolvFile() ) ; QFile *restResolvFile = new QFile( Comun::restResolvFile() ) ; if( restResolvFile->exists() ) restResolvFile->remove() ; if( !resolvFile->copy( Comun::restResolvFile() ) ) qFatal( "Could not copy resolv file" ) ; std::cout << "Backing up: resolv file" << std::endl ; } void backupCreator::IptablesConfig() { QByteArray iptables_Conf = r_Iptables_Sys() ; w_Iptables_Rest(iptables_Conf) ; std::cout << "Backing up: Iptables configuration" << std::endl ; } QByteArray backupCreator::r_Iptables_Sys() { int errordesalida ; QString *errmsg = new QString() ; QProcess *iptablessave = new QProcess(); QString comando = "iptables-save"; QString args = ""; connect(iptablessave, SIGNAL(error( QProcess::ProcessError)), this, SLOT(printIptablessaveError(QProcess::ProcessError)) ) ; iptablessave->start(comando) ; if (!iptablessave->waitForStarted() ) { errmsg = new QString() ; errmsg->append( "backupCreator::r_Iptables_Sys(): iptablessave->waitForStarted() failed" ) ; qFatal( qPrintable(*errmsg) ) ; } if (!iptablessave->waitForFinished()) { errmsg = new QString() ; errmsg->append( "backupCreator::r_Iptables_Sys(): iptablessave->waitForFinished() failed" ) ; qFatal( qPrintable(*errmsg) ) ; } if ( (errordesalida = iptablessave->exitCode() ) != 0 ) { errmsg = new QString() ; errmsg->append( errordesalida ) ; qFatal( qPrintable(*errmsg) ) ; } QByteArray *result = new QByteArray() ; *result = iptablessave->readAll(); return *result ; } void backupCreator::w_Iptables_Rest(QByteArray datos) { QFile FrestIptables( Comun::restIptables() ) ; if( !FrestIptables.open( QIODevice::WriteOnly | QIODevice::Text ) ) qFatal( "backupCreator::w_Iptables_Rest(): Could not open file" ) ; if ( FrestIptables.write(datos, datos.size()) == -1 ) qFatal( "backupCreator::w_Iptables_Rest(): Could not write to file Error" ) ; FrestIptables.close(); } bool backupCreator::r_Ipfw_Sys () { QString *errmsg = new QString( "" ) ; QFile sys_ipfwd( Comun::sysIpfwd() ); if (!sys_ipfwd.open( QIODevice::ReadOnly | QIODevice::Text )) { errmsg->append( "r_Ipfw_Sys(): Could not open " ) ; errmsg->append( Comun::sysIpfwd() ) ; qFatal( qPrintable(*errmsg) ) ; } char valor ; if ( sys_ipfwd.read ( &valor, sizeof(char) ) != sizeof(char) ) { errmsg = new QString() ; errmsg->append( "r_Ipfw_Sys(): Could not read from " ) ; errmsg->append( Comun::sysIpfwd() ) ; qFatal( qPrintable(*errmsg) ) ; } if ( valor == '1' ) return true; else if ( valor == '0' ) return false; else { errmsg = new QString() ; errmsg->append( "Value read from " ) ; errmsg->append( Comun::sysIpfwd() ) ; errmsg->append( "not valid" ) ; qFatal( qPrintable(*errmsg) ) ; } return false ; } void backupCreator::ipfwConfig () { char valor ; int salida ; QString *errmsg = new QString() ; QFile sys_ipfwd( Comun::sysIpfwd() ); QFile rest_ipfwd( Comun::restIpfwd() ); // sys if (!sys_ipfwd.open( QIODevice::ReadWrite | QIODevice::Text )) { errmsg->append( "w_Ipfw_Rest(): Could not read " ) ; errmsg->append( Comun::sysIpfwd() ) ; errmsg->append( " for backing it up" ) ; qFatal( qPrintable(*errmsg) ) ; } if ( rest_ipfwd.exists() ) { if ( !rest_ipfwd.remove() ) { errmsg->append("w_Ipfw_Rest(): Could not delete ") ; errmsg->append( Comun::restIpfwd() ) ; qFatal( qPrintable(*errmsg) ) ; } } if( (salida = sys_ipfwd.read ( &valor, sizeof(char) )) == -1 ) qFatal( "w_Ipfw_Rest(): Could not read ip forward backup file" ) ; else if( salida == 0) qFatal( "w_Ipfw_Rest(): There are no remainig data into ip forward backup file" ) ; // rest if (!rest_ipfwd.open( QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate )) { errmsg->append( "w_Ipfw_Rest(): Could not open " ) ; errmsg->append( Comun::restIpfwd() ) ; errmsg->append( " for backing it up" ) ; qFatal( qPrintable(*errmsg) ) ; } if( (salida = rest_ipfwd.write ( &valor, sizeof(char) )) == -1 ) { std::cout << "w_Ipfw_Rest(): " << salida << " bytes written to ip forward backup file" << std::endl ; qFatal( "w_Ipfw_Rest(): Could not write to ip forward backup file" ) ; } std::cout << "Backing up: Ip Forwarding" << std::endl ; } // SLOTS void backupCreator::printIptablessaveError(QProcess::ProcessError error) { std::cout << "Could not init iptables-save" << error << std::endl ; } void backupCreator::printPsapacheError(QProcess::ProcessError error) { std::cout << "Could not run \"ps -A | awk {'print $4'} | grep -Eo 'apache2' | uniqiptables-save\"" << error << std::endl ; }