/* * 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 remove() ) qFatal("Could not delete site file") ; else std::cout << "Restoring: site file" << std::endl ; } void backupRestorer:: hostConfig() { Comun *comun = new Comun() ; QFile *hostsFile = new QFile(comun->hostsFile()) ; QFile *hostsRestFile = new QFile(comun->restHostsFile()) ; if ( hostsRestFile->exists() ) { hostsFile->remove() ; if ( !hostsRestFile->copy( comun->hostsFile()) ) qFatal( "Could not restore the original hosts file !!!") ; } std::cout << "Restoring: hosts file" << std::endl ; } void backupRestorer::ipfwConfig () { char valor ; int salida ; QString *errmsg = new QString() ; QFile sys_ipfwd( Comun::sysIpfwd() ); QFile rest_ipfwd( Comun::restIpfwd() ); // rest if (!rest_ipfwd.open( QIODevice::ReadOnly | QIODevice::Text )) { errmsg = new QString() ; errmsg->append( "backupRestorer::ipfwConfig(): Could not open " ) ; errmsg->append( Comun::restIpfwd() ) ; errmsg->append( " for restoring it" ) ; qFatal( qPrintable(*errmsg) ) ; } if( (salida = rest_ipfwd.read ( &valor, sizeof(char) )) == -1 ) qFatal( "backupRestorer::ipfwConfig(): Could not read ip forward backup file" ) ; else if( salida == 0) qFatal( "backupRestorer::ipfwConfig(): There are no remaining data into the ip forward backup file" ) ; rest_ipfwd.close() ; // sys if (!sys_ipfwd.open( QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate)) { errmsg = new QString() ; errmsg->append( "backupRestorer::ipfwConfig(): Could not open " ) ; errmsg->append( Comun::sysIpfwd() ) ; errmsg->append( " for restoring it" ) ; qFatal( qPrintable(*errmsg) ) ; } std::cout << "Restoring: ip forwarding" << std::endl ; if( (salida = sys_ipfwd.write ( &valor, sizeof(char) )) == -1 ) qFatal( "backupRestorer::ipfwConfig(): Could not open ip forward backup file" ) ; else if( salida != sizeof(char) ) qFatal( "backupRestorer::ipfwConfig(): Partial write to ip forward backup file" ) ; sys_ipfwd.close() ; } void backupRestorer::iptablesConfig () { iR = new QProcess() ; connect(iR, SIGNAL(error( QProcess::ProcessError)), this, SLOT(iptablesRestoreError(QProcess::ProcessError)) ) ; connect(iR, SIGNAL(readyReadStandardError()), this, SLOT(iptablesConfigOutput()) ) ; connect(iR, SIGNAL(readyReadStandardError()), this, SLOT(iptablesConfigOutput()) ) ; QList reglas ; QList::iterator iter ; QString ( errmsg ) ; old = new QFile( Comun::restIptables() ) ; if (!old->open( QIODevice::ReadOnly | QIODevice::Text )) { errmsg.append( "backupRestorer::iptablesConfig(): Could not open " ) ; errmsg.append( Comun::restIptables() ) ; qFatal( qPrintable(errmsg) ) ; } while (!old->atEnd()) reglas << old->readLine() ; iR->start( "iptables-restore" ) ; if (!iR->waitForStarted() ) qFatal( "iR->waitForStarted() failed" ) ; for( iter=reglas.begin(); iter!=reglas.end(); ++iter ) { QByteArray tmp = iter->toUtf8().constData() ; iR->write( tmp.constData() ) ; } iR->waitForBytesWritten() ; iR->closeWriteChannel() ; if (!iR->waitForFinished() ) { std::cout << "Error: " << qPrintable(old->errorString()) << std::endl ; qFatal( "iR->waitForFinished() failed" ) ; } std::cout << "Restoring: iptables" << std::endl ; } void backupRestorer::resolvConfig () { QFile *sysResolv = new QFile(Comun::resolvFile()) ; QFile *restResolv = new QFile(Comun::restResolvFile()) ; QFile *sourceResolv = new QFile(Comun::sysResolvFile()) ; QString *buffer = new QString(); if (restResolv->open(QFile::ReadOnly)) { QTextStream readData(restResolv); *buffer = readData.readAll(); } if (sourceResolv->open(QFile::WriteOnly | QFile::Truncate)) { QTextStream writeData(sourceResolv); writeData << qPrintable(*buffer); } std::cout << "Restoring: resolvConfig" << std::endl ; } void backupRestorer::iptablesRestoreError( QProcess::ProcessError error ) { QString ( errmsg ) ; errmsg.append( "ERROR " ) ; errmsg.append( sizeof(error) ) ; errmsg.append( ": backupRestorer::iptablesConfig ()" ) ; std::cout << errmsg.toUtf8().data() << std::endl ; std::cout << qPrintable(old->errorString()) << std::endl ; qFatal ( errmsg.toUtf8() ) ; } void backupRestorer::iptablesConfigOutput() { QByteArray result = iR->readAllStandardError() ; std::cout << result.data() << std::endl ; }