Diffstat (limited to 'core/launcher/transferserver.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | core/launcher/transferserver.cpp | 191 |
1 files changed, 113 insertions, 78 deletions
diff --git a/core/launcher/transferserver.cpp b/core/launcher/transferserver.cpp index cbda247..0337a94 100644 --- a/core/launcher/transferserver.cpp +++ b/core/launcher/transferserver.cpp @@ -27,13 +27,14 @@ /* we need the _OS_LINUX stuff first ! */ #include <qglobal.h> #ifndef _OS_LINUX_ -extern "C" { +extern "C" +{ #include <uuid/uuid.h> #define UUID_H_INCLUDED } #endif // not defined linux @@ -56,13 +57,13 @@ extern "C" { #include <qpe/contact.h> #include <qpe/quuid.h> #include <qpe/version.h> #include <qpe/qcopenvelope_qws.h> #include "transferserver.h" -#include "qprocess.h" +#include <opie/oprocess.h> const int block_size = 51200; TransferServer::TransferServer( Q_UINT16 port, QObject *parent , const char* name ) : QServerSocket( port, 1, parent, name ) @@ -70,40 +71,44 @@ TransferServer::TransferServer( Q_UINT16 port, QObject *parent , if ( !ok() ) qWarning( "Failed to bind to port %d", port ); } TransferServer::~TransferServer() { - } void TransferServer::newConnection( int socket ) { (void) new ServerPI( socket, this ); } /* * small class in anonymous namespace * to generate a QUUid for us */ -namespace { - struct UidGen { +namespace +{ +struct UidGen +{ QString uuid(); }; #if !defined(_OS_LINUX_) - QString UidGen::uuid() { + +QString UidGen::uuid() +{ uuid_t uuid; uuid_generate( uuid ); return QUUid( uuid ).toString(); } #else /* * linux got a /proc/sys/kernel/random/uuid file * it'll generate the uuids for us */ - QString UidGen::uuid() { +QString UidGen::uuid() +{ QFile file( "/proc/sys/kernel/random/uuid" ); if (!file.open(IO_ReadOnly ) ) return QString::null; QTextStream stream(&file); @@ -160,13 +165,14 @@ int SyncAuthentication::isAuthorized(QHostAddress peeraddress) ? 0xffffffff : (((1<<auth_peer_bits)-1)<<(32-auth_peer_bits)); return (peeraddress.ip4Addr() & mask) == auth_peer; } bool SyncAuthentication::checkUser( const QString& user ) { - if ( user.isEmpty() ) return FALSE; + if ( user.isEmpty() ) + return FALSE; QString euser = loginName(); return user == euser; } bool SyncAuthentication::checkPassword( const QString& password ) { @@ -227,18 +233,18 @@ bool SyncAuthentication::checkPassword( const QString& password ) // Unrecognized system. Be careful... if ( (denials > 2 && now < lastdenial+600) || QMessageBox::warning(0,tr("Sync Connection"), tr("<p>An unrecognized system is requesting access to this device." "<p>If you have just initiated a Sync for the first time, this is normal."), - tr("Allow"),tr("Deny"), 0, 1, 1 ) ==1 ) - { + tr("Allow"), tr("Deny"), 0, 1, 1 ) == 1 ) { denials++; lastdenial=now; return FALSE; - } else { + } + else { denials=0; cfg.writeEntry("Passwords",pwds+" "+cpassword); return TRUE; } } @@ -253,16 +259,18 @@ ServerPI::ServerPI( int socket, QObject *parent , const char* name ) setSocket( socket ); peerport = peerPort(); peeraddress = peerAddress(); #ifndef INSECURE + if ( !SyncAuthentication::isAuthorized(peeraddress) ) { state = Forbidden; startTimer( 0 ); - } else + } + else #endif { connect( this, SIGNAL( readyRead() ), SLOT( read() ) ); connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); passiv = FALSE; @@ -290,13 +298,12 @@ ServerPI::ServerPI( int socket, QObject *parent , const char* name ) SLOT( newConnection( int ) ) ); } } ServerPI::~ServerPI() { - } void ServerPI::connectionClosed() { // qDebug( "Debug: Connection closed" ); delete this; @@ -348,13 +355,14 @@ bool ServerPI::checkWriteFile( const QString& file ) void ServerPI::process( const QString& message ) { //qDebug( "Command: %s", message.latin1() ); // split message using "," as separator QStringList msg = QStringList::split( " ", message ); - if ( msg.isEmpty() ) return; + if ( msg.isEmpty() ) + return ; // command token QString cmd = msg[0].upper(); // argument token QString arg; @@ -587,13 +595,14 @@ void ServerPI::process( const QString& message ) else { QFile file( absFilePath( args ) ) ; if ( file.remove() ) { send( "250 Requested file action okay, completed" ); QCopEnvelope e("QPE/System", "linkChanged(QString)" ); e << file.name(); - } else { + } + else { send( "550 Requested action not taken" ); } } } // remove directory (RMD) @@ -731,13 +740,14 @@ void ServerPI::sendFile( const QString& file ) newConnection( waitsocket ); } else { QStringList targets; if ( backupRestoreGzip( file, targets ) ) dtp->sendGzipFile( file, targets, peeraddress, peerport ); - else dtp->sendFile( file, peeraddress, peerport ); + else + dtp->sendFile( file, peeraddress, peerport ); } } void ServerPI::retrieveFile( const QString& file ) { if ( passiv ) { @@ -755,13 +765,14 @@ void ServerPI::retrieveFile( const QString& file ) } } bool ServerPI::parsePort( const QString& pp ) { QStringList p = QStringList::split( ",", pp ); - if ( p.count() != 6 ) return FALSE; + if ( p.count() != 6 ) + return FALSE; // h1,h2,h3,h4,p1,p2 peeraddress = QHostAddress( ( p[0].toInt() << 24 ) + ( p[1].toInt() << 16 ) + ( p[2].toInt() << 8 ) + p[3].toInt() ); peerport = ( p[4].toInt() << 8 ) + p[5].toInt(); return TRUE; @@ -807,13 +818,14 @@ bool ServerPI::sendList( const QString& arg ) QString fn = arg; if ( fn.isEmpty() ) fn = directory.path(); QFileInfo fi( fn ); - if ( !fi.exists() ) return FALSE; + if ( !fi.exists() ) + return FALSE; // return file listing if ( fi.isFile() ) { ts << fileListing( &fi ) << endl; } @@ -855,13 +867,14 @@ bool ServerPI::sendList( const QString& arg ) dtp->sendByteArray( buffer.buffer(), peeraddress, peerport ); return TRUE; } QString ServerPI::fileListing( QFileInfo *info ) { - if ( !info ) return QString::null; + if ( !info ) + return QString::null; QString s; // type char if ( info->isDir() ) s += "d"; else if ( info->isSymLink() ) @@ -905,47 +918,67 @@ QString ServerPI::fileListing( QFileInfo *info ) return s; } QString ServerPI::permissionString( QFileInfo *info ) { - if ( !info ) return QString( "---------" ); + if ( !info ) + return QString( "---------" ); QString s; // user - if ( info->permission( QFileInfo::ReadUser ) ) s += "r"; - else s += "-"; - if ( info->permission( QFileInfo::WriteUser ) ) s += "w"; - else s += "-"; - if ( info->permission( QFileInfo::ExeUser ) ) s += "x"; - else s += "-"; + if ( info->permission( QFileInfo::ReadUser ) ) + s += "r"; + else + s += "-"; + if ( info->permission( QFileInfo::WriteUser ) ) + s += "w"; + else + s += "-"; + if ( info->permission( QFileInfo::ExeUser ) ) + s += "x"; + else + s += "-"; // group - if ( info->permission( QFileInfo::ReadGroup ) ) s += "r"; - else s += "-"; - if ( info->permission( QFileInfo::WriteGroup ) )s += "w"; - else s += "-"; - if ( info->permission( QFileInfo::ExeGroup ) ) s += "x"; - else s += "-"; + if ( info->permission( QFileInfo::ReadGroup ) ) + s += "r"; + else + s += "-"; + if ( info->permission( QFileInfo::WriteGroup ) ) + s += "w"; + else + s += "-"; + if ( info->permission( QFileInfo::ExeGroup ) ) + s += "x"; + else + s += "-"; // exec - if ( info->permission( QFileInfo::ReadOther ) ) s += "r"; - else s += "-"; - if ( info->permission( QFileInfo::WriteOther ) ) s += "w"; - else s += "-"; - if ( info->permission( QFileInfo::ExeOther ) ) s += "x"; - else s += "-"; + if ( info->permission( QFileInfo::ReadOther ) ) + s += "r"; + else + s += "-"; + if ( info->permission( QFileInfo::WriteOther ) ) + s += "w"; + else + s += "-"; + if ( info->permission( QFileInfo::ExeOther ) ) + s += "x"; + else + s += "-"; return s; } void ServerPI::newConnection( int socket ) { //qDebug( "New incomming connection" ); - if ( !passiv ) return; + if ( !passiv ) + return ; if ( wait[SendFile] ) { QStringList targets; if ( backupRestoreGzip( waitfile, targets ) ) dtp->sendGzipFile( waitfile, targets ); else @@ -975,13 +1008,14 @@ void ServerPI::newConnection( int socket ) for( int i = 0; i < 4; i++ ) wait[i] = FALSE; } QString ServerPI::absFilePath( const QString& file ) { - if ( file.isEmpty() ) return file; + if ( file.isEmpty() ) + return file; QString filepath( file ); if ( file[0] != "/" ) filepath = directory.path() + "/" + file; return filepath; @@ -1001,28 +1035,25 @@ retrieveTargzProc( 0 ), gzipProc( 0 ) connect( this, SIGNAL( connected() ), SLOT( connected() ) ); connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); connect( this, SIGNAL( bytesWritten( int ) ), SLOT( bytesWritten( int ) ) ); connect( this, SIGNAL( readyRead() ), SLOT( readyRead() ) ); - gzipProc = new QProcess( this, "gzipProc" ); - gzipProc->setCommunication( QProcess::Stdin | QProcess::Stdout ); + gzipProc = new OProcess( this, "gzipProc" ); - createTargzProc = new QProcess( QString("tar"), this, "createTargzProc"); - createTargzProc->setCommunication( QProcess::Stdout ); + createTargzProc = new OProcess( QString("tar"), this, "createTargzProc"); createTargzProc->setWorkingDirectory( QDir::rootDirPath() ); - connect( createTargzProc, SIGNAL( processExited() ), SLOT( targzDone() ) ); + connect( createTargzProc, SIGNAL( processExited(OProcess *) ), SLOT( targzDone() ) ); QStringList args = "tar"; args += "-xv"; - retrieveTargzProc = new QProcess( args, this, "retrieveTargzProc" ); - retrieveTargzProc->setCommunication( QProcess::Stdin ); + retrieveTargzProc = new OProcess( args, this, "retrieveTargzProc" ); retrieveTargzProc->setWorkingDirectory( QDir::rootDirPath() ); - connect( retrieveTargzProc, SIGNAL( processExited() ), + connect( retrieveTargzProc, SIGNAL( processExited(OProcess *) ), SIGNAL( completed() ) ); - connect( retrieveTargzProc, SIGNAL( processExited() ), + connect( retrieveTargzProc, SIGNAL( processExited(OProcess *) ), SLOT( extractTarDone() ) ); } ServerDTP::~ServerDTP() { buf.close(); @@ -1031,12 +1062,13 @@ ServerDTP::~ServerDTP() } void ServerDTP::extractTarDone() { qDebug("extract done"); #ifndef QT_NO_COP + QCopEnvelope e( "QPE/Desktop", "restoreDone(QString)" ); e << file.name(); #endif } void ServerDTP::connected() @@ -1055,13 +1087,14 @@ void ServerDTP::connected() bytes_written = 0; if ( file.size() == 0 ) { //make sure it doesn't hang on empty files file.close(); emit completed(); mode = Idle; - } else { + } + else { if( !file.atEnd() ) { QCString s; s.resize( block_size ); int bytes = file.readBlock( s.data(), block_size ); writeBlock( s.data(), bytes ); @@ -1074,16 +1107,16 @@ void ServerDTP::connected() qWarning("Previous tar --gzip process is still running; killing it..."); createTargzProc->kill(); } bytes_written = 0; qDebug("==>start send tar process"); - if ( !createTargzProc->start() ) + if ( !createTargzProc->start(OProcess::NotifyOnExit, OProcess::Stdout) ) qWarning("Error starting %s or %s", - createTargzProc->arguments().join(" ").latin1(), - gzipProc->arguments().join(" ").latin1() ); + createTargzProc->args()[0].data(), + gzipProc->args()[0].data()); break; case SendBuffer: if ( !buf.open( IO_ReadOnly) ) { emit failed(); mode = Idle; return; @@ -1209,59 +1242,58 @@ void ServerDTP::readyRead() s.resize( bytesAvailable() ); readBlock( s.data(), bytesAvailable() ); file.writeBlock( s.data(), s.size() ); } else if ( RetrieveGzipFile == mode ) { if ( !gzipProc->isRunning() ) - gzipProc->start(); + gzipProc->start(OProcess::NotifyOnExit, (OProcess::Communication) ( OProcess::Stdin | OProcess::Stdout )); QByteArray s; s.resize( bytesAvailable() ); readBlock( s.data(), bytesAvailable() ); - gzipProc->writeToStdin( s ); + gzipProc->writeStdin( s.data(), s.size() ); qDebug("wrote %d bytes to ungzip ", s.size() ); } // retrieve buffer mode else if ( RetrieveBuffer == mode ) { QCString s; s.resize( bytesAvailable() ); readBlock( s.data(), bytesAvailable() ); buf.writeBlock( s.data(), s.size() ); } } -void ServerDTP::writeTargzBlock() +void ServerDTP::writeTargzBlock(OProcess *, char *buffer, int buflen) { - QByteArray block = gzipProc->readStdout(); - writeBlock( block.data(), block.size() ); - qDebug("writeTargzBlock %d", block.size()); + writeBlock( buffer, buflen ); + qDebug("writeTargzBlock %d", buflen); if ( !createTargzProc->isRunning() ) { qDebug("tar and gzip done"); emit completed(); mode = Idle; - disconnect( gzipProc, SIGNAL( readyReadStdout() ), - this, SLOT( writeTargzBlock() ) ); + disconnect( gzipProc, SIGNAL( receivedStdout(OProcess *, char *, int ) ), + this, SLOT( writeTargzBlock(OProcess *, char *, int) ) ); } } void ServerDTP::targzDone() { //qDebug("targz done"); - disconnect( createTargzProc, SIGNAL( readyReadStdout() ), - this, SLOT( gzipTarBlock() ) ); + disconnect( createTargzProc, SIGNAL( receivedStdout(OProcess *, char *, int) ), + this, SLOT( gzipTarBlock(OProcess *, char *, int) ) ); gzipProc->closeStdin(); } -void ServerDTP::gzipTarBlock() +void ServerDTP::gzipTarBlock(OProcess *, char *buffer, int buflen) { //qDebug("gzipTarBlock"); if ( !gzipProc->isRunning() ) { //qDebug("auto start gzip proc"); - gzipProc->start(); + gzipProc->start(OProcess::NotifyOnExit, (OProcess::Communication) ( OProcess::Stdin | OProcess::Stdout )); } - gzipProc->writeToStdin( createTargzProc->readStdout() ); + gzipProc->writeStdin( buffer, buflen ); } void ServerDTP::sendFile( const QString fn, const QHostAddress& host, Q_UINT16 port ) { file.setName( fn ); mode = SendFile; @@ -1289,40 +1321,42 @@ void ServerDTP::sendGzipFile( const QString &fn, file.setName( fn ); QStringList args = "tar"; args += "-cv"; args += archiveTargets; qDebug("sendGzipFile %s", args.join(" ").latin1() ); - createTargzProc->setArguments( args ); + createTargzProc->clearArguments( ); + *createTargzProc << args; connect( createTargzProc, - SIGNAL( readyReadStdout() ), SLOT( gzipTarBlock() ) ); + SIGNAL( receivedStdout(OProcess *, char *, int) ), SLOT( gzipTarBlock(OProcess *, char *, int) ) ); - gzipProc->setArguments( "gzip" ); - connect( gzipProc, SIGNAL( readyReadStdout() ), - SLOT( writeTargzBlock() ) ); + gzipProc->clearArguments( ); + *gzipProc << "gzip"; + connect( gzipProc, SIGNAL( receivedStdout(OProcess *, char *, int) ), + SLOT( writeTargzBlock(OProcess *, char *, int) ) ); } void ServerDTP::gunzipDone() { qDebug("gunzipDone"); disconnect( gzipProc, SIGNAL( processExited() ), this, SLOT( gunzipDone() ) ); retrieveTargzProc->closeStdin(); - disconnect( gzipProc, SIGNAL( readyReadStdout() ), - this, SLOT( tarExtractBlock() ) ); + disconnect( gzipProc, SIGNAL( receivedStdout(OProcess *, char *, int) ), + this, SLOT( tarExtractBlock(OProcess *, char *, int) ) ); } -void ServerDTP::tarExtractBlock() +void ServerDTP::tarExtractBlock(OProcess *, char *buffer, int buflen) { - qDebug("ungzipTarBlock"); + qDebug("tarExtractBlock"); if ( !retrieveTargzProc->isRunning() ) { qDebug("auto start ungzip proc"); - if ( !retrieveTargzProc->start() ) + if ( !retrieveTargzProc->start(OProcess::NotifyOnExit, OProcess::Stdin) ) qWarning(" failed to start tar -x process"); } - retrieveTargzProc->writeToStdin( gzipProc->readStdout() ); + retrieveTargzProc->writeStdin( buffer, buflen ); } void ServerDTP::retrieveFile( const QString fn, const QHostAddress& host, Q_UINT16 port ) { file.setName( fn ); @@ -1339,13 +1373,14 @@ void ServerDTP::retrieveFile( const QString fn ) void ServerDTP::retrieveGzipFile( const QString &fn ) { qDebug("retrieveGzipFile %s", fn.latin1()); file.setName( fn ); mode = RetrieveGzipFile; - gzipProc->setArguments( "gunzip" ); + gzipProc->clearArguments(); + *gzipProc << "gunzip"; connect( gzipProc, SIGNAL( readyReadStdout() ), SLOT( tarExtractBlock() ) ); connect( gzipProc, SIGNAL( processExited() ), SLOT( gunzipDone() ) ); } |