/* * 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 append( "ipfwConfig(): Could not open " ) ; errmsg->append( Comun::sysIpfwd() ) ; qFatal( qPrintable(*errmsg) ) ; } char valor = '1' ; if ( sys_ipfwd.write ( &valor, sizeof(char) ) != sizeof(char) ) { errmsg = new QString() ; errmsg->append( "ipfwConfig(): Could not write to " ) ; errmsg->append( Comun::sysIpfwd() ) ; qFatal( qPrintable(*errmsg) ) ; } } std::cout << "Configuring: ip forwarding" << std::endl ; emit ipfwDone( QString("Ip forwarding configured") ) ; } void Config::siteConfig (QString serverName, QString documentRoot) { QFile *webServFile ; Comun *comun = new Comun() ; int i ; webServFile = new QFile(comun->webServSiteFile()) ; QTextStream webServFileF(webServFile) ; if (!webServFile->open( QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text )) { std::cout << "Could not create " << qPrintable(comun->webServSiteFile()) << std::endl ; perror("error: ") ; } for( i=0 ; i<2 ; i++) { if ( i == 1 ) serverName.prepend( "www." ) ; webServFileF << "webServPort() << ">\n" ; webServFileF << " ServerName " << serverName << "\n" ; webServFileF << " ServerAdmin noexisto@example.com\n" ; webServFileF << " DocumentRoot " << documentRoot << "\n" ; webServFileF << " \n" ; webServFileF << " AllowOverride None\n" ; webServFileF << " \n" ; webServFileF << " ErrorLog " << comun->webServErrFile(serverName) << "\n" ; webServFileF << " LogLevel warn\n" ; webServFileF << " CustomLog " << comun->webServAccFile(serverName) << " combined\n" ; webServFileF << " ServerSignature Off\n" ; webServFileF << "\n" ; } std::cout << "Configuring: VirtualHost" << std::endl ; webServFile->close() ; emit siteDone( QString("apache virtualhost configured") ) ; } void Config::hostConfig(QString nuevoHost, QString IP) { QString *errmsg = new QString() ; QFile *hostsFile = new QFile(Comun::hostsFile()) ; std::cout << "Configuring: " << qPrintable(Comun::hostsFile()) << std::endl ; QTextStream hostsFileF(hostsFile) ; if (!hostsFile->open( QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text )) { errmsg->append("Config::hostConfig(): Could not open ") ; errmsg->append( Comun::hostsFile() ) ; qFatal( qPrintable(*errmsg) ) ; } hostsFileF << qPrintable( IP ) << "\t" << nuevoHost << " www." << nuevoHost << "\n" ; hostsFile->close() ; emit hostDone( QString("Host paths configured") ) ; } void Config::webServStart() { int errordesalida ; char serverActivo ; QString *comando = new QString( "" ) ; QProcess *startWebServ = new QProcess(); connect(startWebServ, SIGNAL(error( QProcess::ProcessError)), this, SLOT(showWebServStartError(QProcess::ProcessError)) ) ; QFile *restapachestatus = new QFile(Comun::restApacheStatus()) ; if (!restapachestatus->open(QIODevice::ReadOnly | QIODevice::Text)) qFatal( "Could not open web server status backup file" ) ; restapachestatus->read ( &serverActivo, sizeof(char) ) ; if ( serverActivo == '1' ) { comando = new QString("/etc/init.d/apache2 restart"); } else if ( serverActivo == '0' ) { comando = new QString("/etc/init.d/apache2 start"); } else { qFatal( "Unknown error on web server status" ) ; } startWebServ->start(*comando) ; if (!startWebServ->waitForStarted() ) { qFatal( "Config::startWebServ->waitForStarted()" ) ; } if (!startWebServ->waitForFinished()) { qFatal( "fallo en startWebServ->waitForFinished()" ) ; } if ( (errordesalida = startWebServ->exitCode() ) != 0 ) { QString (err) = QString("webServStart() error: ") ; err.append( errordesalida ) ; qFatal( qPrintable(err) ) ; } std::cout << "Configuring: Starting Web server" << std::endl ; emit webServDone( QString("Web server ready") ) ; } void Config::webServStop() { int errordesalida ; char serverActivo ; QString *comando = new QString( "" ) ; QProcess *stopWebServ = new QProcess(); connect(stopWebServ, SIGNAL(error( QProcess::ProcessError)), this, SLOT(showWebServStopError(QProcess::ProcessError)) ) ; QFile *restapachestatus = new QFile(Comun::restApacheStatus()) ; if (!restapachestatus->open(QIODevice::ReadOnly | QIODevice::Text)) qFatal( "Could not open web server status backup file" ) ; restapachestatus->read ( &serverActivo, sizeof(char) ) ; if ( serverActivo == '1' ) { comando = new QString("/etc/init.d/apache2 restart"); } else if ( serverActivo == '0' ) { comando = new QString("/etc/init.d/apache2 stop"); } else { qFatal( "Unknown error on web server status" ) ; } stopWebServ->start(*comando) ; if (!stopWebServ->waitForStarted() ) { std::cout << "stopWebServ->waitForStarted() failed" << std::endl ; exit(1); } if (!stopWebServ->waitForFinished()) { std::cout << "stopWebServ->waitForFinished() failed" << std::endl ; exit(1); } if ( (errordesalida = stopWebServ->exitCode() ) != 0 ) { std::cout << "sebServStop() error: " << errordesalida << std::endl ; exit(errordesalida) ; } std::cout << "Restoring: Web server brought to its original status" << std::endl ; } void Config::resolvConfig( QString interfaz, QString cliente ) { tcpD *tcpdump = new tcpD( interfaz, cliente ) ; connect(tcpdump, SIGNAL(gotResolver( QString )), this, SLOT(w_resolverFile(QString))) ; connect(tcpdump, SIGNAL(gotResolver( QString )), this, SLOT(emit_iptables_can_go())) ; connect(tcpdump, SIGNAL(gotResolver( QString )), tcpdump, SLOT(kill())) ; tcpdump->start() ; } void Config::iptablesConfig( QString cliente, QString dominio, QString IPdest) { int salida ; QList comandos; QList::iterator i; QString( tmp ) ; QString wdominio = dominio ; wdominio.prepend( "www." ) ; comandos << QString("iptables -F INPUT") << QString( "iptables -F OUTPUT") << QString( "iptables -F FORWARD") << QString( "iptables -F -t nat") << QString( "iptables -P INPUT ACCEPT") << QString( "iptables -P OUTPUT ACCEPT") << QString( "iptables -P FORWARD ACCEPT" ) ; tmp.truncate(0) ; tmp.append( "iptables -A INPUT -p tcp --dport 80 --source " ) ; tmp.append( cliente ) ; tmp.append( " -j ACCEPT" ) ; comandos << tmp ; tmp.truncate(0) ; tmp.append( "iptables -A INPUT -p tcp --sport 53 " ) ; tmp.append( " -j ACCEPT" ) ; comandos << tmp ; tmp.truncate(0) ; tmp.append( "iptables -A FORWARD --source " ) ; tmp.append( cliente ) ; tmp.append( " -j ACCEPT" ) ; comandos << tmp ; tmp.truncate(0) ; tmp.append( "iptables -A FORWARD -p udp --dport 53 " ) ; tmp.append( " -j ACCEPT" ) ; comandos << tmp ; tmp.truncate(0) ; tmp.append( "iptables -A FORWARD -p tcp --dport 53 " ) ; tmp.append( " -j ACCEPT" ) ; comandos << tmp ; tmp.truncate(0) ; tmp.append( "iptables -A OUTPUT --destination " ) ; tmp.append( cliente ) ; tmp.append( " -j ACCEPT" ) ; comandos << tmp ; tmp.truncate(0) ; tmp.append( "iptables -A OUTPUT -p tcp --dport 53 " ) ; tmp.append( " -j ACCEPT" ) ; comandos << tmp ; tmp.truncate(0) ; // dnat tmp.append( "iptables -t nat -A PREROUTING -s " ) ; tmp.append ( cliente ) ; tmp.append ( " -d " ) ; tmp.append ( dominio ) ; tmp.append( " -p tcp --dport 80 -j DNAT --to-destination " ) ; tmp.append( IPdest ) ; comandos << tmp ; // www dnat tmp.replace( QRegExp( dominio ), wdominio ); comandos << tmp ; tmp.truncate(0) ; // snat tmp.append( "iptables -t nat -A POSTROUTING -p tcp -s " ) ; tmp.append ( dominio ) ; tmp.append( " --dport 80 -j SNAT --to-source " ) ; tmp.append( cliente ) ; comandos << tmp ; tmp.replace( QRegExp( dominio ), wdominio ); // www snat comandos << tmp << QString( "iptables -P INPUT DROP" ) << QString( "iptables -P OUTPUT DROP" ) << QString( "iptables -A INPUT -i lo -j ACCEPT" ) << QString( "iptables -A OUTPUT -o lo -j ACCEPT" ); for (i = comandos.begin(); i != comandos.end(); ++i) { if ( ( salida = system( qPrintable( *i ) ) ) == -1 ) qFatal("Config::iptablesConfig()") ; else { if (salida != 0) { QString( errmsg ) ; errmsg.append("Error (") ; errmsg.append( salida ); errmsg.append( ") when executing:\n\t") ; errmsg.append( (*i).toLocal8Bit().constData() ) ; errmsg.append( "\n" ) ; qFatal( errmsg.toLocal8Bit().constData() ) ; } } } std::cout << "Configuring: iptables" << std::endl ; emit iptablesDone( QString("iptables configured") ) ; } void Config::Restaurar() { backupRestorer *restaurar = new backupRestorer() ; webServStop() ; restaurar->ipfwConfig() ; restaurar->siteConfig() ; restaurar->iptablesConfig() ; restaurar->resolvConfig() ; restaurar->hostConfig() ; } // SLOTS void Config::showWebServStartError(QProcess::ProcessError error) { QString *errmsg = new QString( "Config::webServStart(): " ) ; errmsg->append( error ) ; qFatal( qPrintable( *errmsg ) ) ; } void Config::showWebServStopError(QProcess::ProcessError error) { QString *errmsg = new QString( "Config::webServStop(): " ) ; errmsg->append( error ) ; qFatal( qPrintable( *errmsg ) ) ; } void Config::w_resolverFile(QString resolver) { QFile *sysResolv = new QFile( Comun::resolvFile() ) ; if ( !sysResolv->open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate ) ) qFatal( "Config::w_resolverFile(): Could not open file" ) ; QTextStream sysResolvF(sysResolv) ; sysResolvF << "nameserver " << resolver.toLocal8Bit().data() << "\n" ; sysResolv->close() ; emit resolvDone( QString("Resolv configured") ) ; qDebug( "Configuring: Domain name resolution" ) ; qDebug( resolver.toLocal8Bit().data() ) ; } void Config::emit_iptables_can_go() { emit iptables_can_go(QString()) ; }