-rw-r--r-- | core/launcher/transferserver.cpp | 595 |
1 files changed, 293 insertions, 302 deletions
diff --git a/core/launcher/transferserver.cpp b/core/launcher/transferserver.cpp index 0337a94..371400e 100644 --- a/core/launcher/transferserver.cpp +++ b/core/launcher/transferserver.cpp @@ -18,5 +18,10 @@ ** **********************************************************************/ -#define _XOPEN_SOURCE +//#define _XOPEN_SOURCE + +#include <qtopia/global.h> +#include <qtopia/qpeapplication.h> + +#ifndef Q_OS_WIN32 #include <pwd.h> #include <sys/types.h> @@ -25,17 +30,11 @@ #include <time.h> #include <shadow.h> +#include <crypt.h> -/* we need the _OS_LINUX stuff first ! */ -#include <qglobal.h> - -#ifndef _OS_LINUX_ - -extern "C" -{ -#include <uuid/uuid.h> -#define UUID_H_INCLUDED -} +#else +#include <stdlib.h> +#include <time.h> +#endif -#endif // not defined linux #if defined(_OS_LINUX_) @@ -51,15 +50,19 @@ extern "C" #include <qfileinfo.h> #include <qregexp.h> -//#include <qpe/qcopchannel_qws.h> -#include <qpe/process.h> -#include <qpe/global.h> -#include <qpe/config.h> -#include <qpe/contact.h> -#include <qpe/quuid.h> -#include <qpe/version.h> -#include <qpe/qcopenvelope_qws.h> +//#include <qtopia/qcopchannel_qws.h> +#include <qtopia/process.h> +#include <qtopia/global.h> +#include <qtopia/config.h> +#include <qtopia/private/contact.h> +#include <qtopia/quuid.h> +#include <qtopia/version.h> +#ifdef Q_WS_QWS +#include <qtopia/qcopenvelope_qws.h> +#endif + +#include "launcherglobal.h" #include "transferserver.h" -#include <opie/oprocess.h> +#include <qtopia/qprocess.h> const int block_size = 51200; @@ -69,51 +72,35 @@ TransferServer::TransferServer( Q_UINT16 port, QObject *parent , : QServerSocket( port, 1, parent, name ) { + connections.setAutoDelete( TRUE ); if ( !ok() ) qWarning( "Failed to bind to port %d", port ); } -TransferServer::~TransferServer() +void TransferServer::authorizeConnections() { + QListIterator<ServerPI> it(connections); + while ( it.current() ) { + if ( !it.current()->verifyAuthorised() ) { + disconnect( it.current(), SIGNAL(connectionClosed(ServerPI *)), this, SLOT( closed(ServerPI *)) ); + connections.removeRef( it.current() ); + } else + ++it; } - -void TransferServer::newConnection( int socket ) -{ - (void) new ServerPI( socket, this ); } -/* - * small class in anonymous namespace - * to generate a QUUid for us - */ -namespace -{ -struct UidGen +void TransferServer::closed(ServerPI *item) { - QString uuid(); -}; -#if !defined(_OS_LINUX_) + connections.removeRef(item); +} -QString UidGen::uuid() +TransferServer::~TransferServer() { - 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() -{ - QFile file( "/proc/sys/kernel/random/uuid" ); - if (!file.open(IO_ReadOnly ) ) - return QString::null; - - QTextStream stream(&file); - return "{" + stream.read().stripWhiteSpace() + "}"; -} -#endif +void TransferServer::newConnection( int socket ) +{ + ServerPI *ptr = new ServerPI( socket, this ); + connect( ptr, SIGNAL(connectionClosed(ServerPI *)), this, SLOT( closed(ServerPI *)) ); + connections.append( ptr ); } @@ -123,7 +110,7 @@ QString SyncAuthentication::serverId() cfg.setGroup("Sync"); QString r = cfg.readEntry("serverid"); + if ( r.isEmpty() ) { - UidGen gen; - r = gen.uuid(); + r = Opie::Global::uuid(); cfg.writeEntry("serverid", r ); } @@ -141,12 +128,17 @@ QString SyncAuthentication::ownerName() } - return ""; + return QString::null; } QString SyncAuthentication::loginName() { - struct passwd *pw; + struct passwd *pw = 0L; +#ifndef Q_OS_WIN32 pw = getpwuid( geteuid() ); return QString::fromLocal8Bit( pw->pw_name ); +#else + //### revise + return QString(); +#endif } @@ -164,4 +156,5 @@ int SyncAuthentication::isAuthorized(QHostAddress peeraddress) uint mask = auth_peer_bits >= 32 // shifting by 32 is not defined ? 0xffffffff : (((1 << auth_peer_bits) - 1) << (32 - auth_peer_bits)); + return (peeraddress.ip4Addr() & mask) == auth_peer; } @@ -169,6 +162,5 @@ int SyncAuthentication::isAuthorized(QHostAddress peeraddress) bool SyncAuthentication::checkUser( const QString& user ) { - if ( user.isEmpty() ) - return FALSE; + if ( user.isEmpty() ) return FALSE; QString euser = loginName(); return user == euser; @@ -203,9 +195,15 @@ bool SyncAuthentication::checkPassword( const QString& password ) if ( password.isEmpty() ) { if ( denials < 1 || now > lastdenial + 600 ) { - QMessageBox::warning( 0, tr("Sync Connection"), + QMessageBox unauth( + tr("Sync Connection"), tr("<p>An unauthorized system is requesting access to this device." "<p>If you are using a version of Qtopia Desktop older than 1.5.1, " "please upgrade."), - tr("Deny") ); + QMessageBox::Warning, + QMessageBox::Cancel, QMessageBox::NoButton, QMessageBox::NoButton, + 0, QString::null, TRUE, WStyle_StaysOnTop); + unauth.setButtonText(QMessageBox::Cancel, tr("Deny")); + unauth.exec(); + denials++; lastdenial = now; @@ -215,43 +213,73 @@ bool SyncAuthentication::checkPassword( const QString& password ) // Second, check sync password... - QString pass = password.left(6); - /* old QtopiaDesktops are sending - * rootme newer versions got a Qtopia - * prefixed. Qtopia prefix will suceed - * until the sync software syncs up - * FIXME - */ - if ( pass == "rootme" || pass == "Qtopia") { - - QString cpassword = QString::fromLocal8Bit( crypt( password.mid(8).local8Bit(), "qp" ) ); - Config cfg("Security"); + + static int lock=0; + if ( lock ) return FALSE; + + ++lock; + if ( password.left(6) == "Qtopia" ) { + Config cfg( QPEApplication::qpeDir()+"/etc/Security.conf", Config::File ); cfg.setGroup("Sync"); - QString pwds = cfg.readEntry("Passwords"); - if ( QStringList::split(QChar(' '), pwds).contains(cpassword) ) + QStringList pwds = cfg.readListEntry("Passwords",' '); + for (QStringList::ConstIterator it=pwds.begin(); it!=pwds.end(); ++it) { +#ifndef Q_OS_WIN32 + QString cpassword = QString::fromLocal8Bit( + crypt( password.mid(8).local8Bit(), (*it).left(2).latin1() ) ); +#else + // ### revise + QString cpassword(""); +#endif + if ( *it == cpassword ) { + lock--; return TRUE; + } + } // Unrecognized system. Be careful... - - if ( (denials > 2 && now < lastdenial + 600) - || QMessageBox::warning(0, tr("Sync Connection"), + QMessageBox unrecbox( + 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 ) { + QMessageBox::Warning, + QMessageBox::Cancel, QMessageBox::Yes, QMessageBox::NoButton, + 0, QString::null, TRUE, WStyle_StaysOnTop); + unrecbox.setButtonText(QMessageBox::Cancel, tr("Deny")); + unrecbox.setButtonText(QMessageBox::Yes, tr("Allow")); + + if ( (denials > 2 && now < lastdenial+600) + || unrecbox.exec() != QMessageBox::Yes) + { denials++; lastdenial = now; + lock--; return FALSE; - } - else { + } else { + const char salty[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/."; + char salt[2]; + salt[0]= salty[rand() % (sizeof(salty)-1)]; + salt[1]= salty[rand() % (sizeof(salty)-1)]; +#ifndef Q_OS_WIN32 + QString cpassword = QString::fromLocal8Bit( + crypt( password.mid(8).local8Bit(), salt ) ); +#else + //### revise + QString cpassword(""); +#endif denials = 0; - cfg.writeEntry("Passwords", pwds + " " + cpassword); + pwds.prepend(cpassword); + cfg.writeEntry("Passwords",pwds,' '); + lock--; return TRUE; } } + lock--; return FALSE; } + ServerPI::ServerPI( int socket, QObject *parent , const char* name ) - : QSocket( parent, name ) , dtp( 0 ), serversocket( 0 ), waitsocket( 0 ) + : QSocket( parent, name ) , dtp( 0 ), serversocket( 0 ), waitsocket( 0 ), + storFileSize(-1) { state = Connected; @@ -263,10 +291,8 @@ ServerPI::ServerPI( int socket, QObject *parent , const char* name ) #ifndef INSECURE - if ( !SyncAuthentication::isAuthorized(peeraddress) ) { state = Forbidden; startTimer( 0 ); - } - else + } else #endif { @@ -278,5 +304,5 @@ ServerPI::ServerPI( int socket, QObject *parent , const char* name ) wait[i] = FALSE; - send( "220 Qtopia " QPE_VERSION " FTP Server" ); + send( "220 Qtopia " QPE_VERSION " FTP Server" ); // No tr state = Wait_USER; @@ -302,4 +328,17 @@ ServerPI::ServerPI( int socket, QObject *parent , const char* name ) ServerPI::~ServerPI() { + close(); + dtp->close(); + delete dtp; + delete serversocket; +} + +bool ServerPI::verifyAuthorised() +{ + if ( !SyncAuthentication::isAuthorized(peerAddress()) ) { + state = Forbidden; + return FALSE; + } + return TRUE; } @@ -307,5 +346,5 @@ void ServerPI::connectionClosed() { // qDebug( "Debug: Connection closed" ); - delete this; + emit connectionClosed(this); } @@ -359,6 +398,5 @@ void ServerPI::process( const QString& message ) // split message using "," as separator QStringList msg = QStringList::split( " ", message ); - if ( msg.isEmpty() ) - return ; + if ( msg.isEmpty() ) return; // command token @@ -384,6 +422,6 @@ void ServerPI::process( const QString& message ) // we always respond to QUIT, regardless of state if ( cmd == "QUIT" ) { - send( "211 Good bye!" ); - delete this; + send( "211 Good bye!" ); // No tr + close(); return ; } @@ -397,8 +435,8 @@ void ServerPI::process( const QString& message ) if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) { - send( "530 Please login with USER and PASS" ); + send( "530 Please login with USER and PASS" ); // No tr return ; } - send( "331 User name ok, need password" ); + send( "331 User name ok, need password" ); // No tr state = Wait_PASS; return ; @@ -409,8 +447,8 @@ void ServerPI::process( const QString& message ) if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) { - send( "530 Please login with USER and PASS" ); + send( "530 Please login with USER and PASS" ); // No tr return ; } - send( "230 User logged in, proceed" ); + send( "230 User logged in, proceed" ); // No tr state = Ready; return ; @@ -419,9 +457,12 @@ void ServerPI::process( const QString& message ) // ACCESS CONTROL COMMANDS + // Only an ALLO sent immediately before STOR is valid. + if ( cmd != "STOR" ) + storFileSize = -1; // account (ACCT) if ( cmd == "ACCT" ) { // even wu-ftp does not support it - send( "502 Command not implemented" ); + send( "502 Command not implemented" ); // No tr } @@ -431,10 +472,10 @@ void ServerPI::process( const QString& message ) if ( !args.isEmpty() ) { if ( directory.cd( args, TRUE ) ) - send( "250 Requested file action okay, completed" ); + send( "250 Requested file action okay, completed" ); // No tr else - send( "550 Requested action not taken" ); + send( "550 Requested action not taken" ); // No tr } else - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr } @@ -442,7 +483,7 @@ void ServerPI::process( const QString& message ) else if ( cmd == "CDUP" ) { if ( directory.cdUp() ) - send( "250 Requested file action okay, completed" ); + send( "250 Requested file action okay, completed" ); // No tr else - send( "550 Requested action not taken" ); + send( "550 Requested action not taken" ); // No tr } @@ -450,5 +491,5 @@ void ServerPI::process( const QString& message ) else if ( cmd == "SMNT" ) { // even wu-ftp does not support it - send( "502 Command not implemented" ); + send( "502 Command not implemented" ); // No tr } @@ -456,5 +497,5 @@ void ServerPI::process( const QString& message ) else if ( cmd == "REIN" ) { // even wu-ftp does not support it - send( "502 Command not implemented" ); + send( "502 Command not implemented" ); // No tr } @@ -466,7 +507,7 @@ void ServerPI::process( const QString& message ) else if ( cmd == "PORT" ) { if ( parsePort( arg ) ) - send( "200 Command okay" ); + send( "200 Command okay" ); // No tr else - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr } @@ -474,5 +515,5 @@ void ServerPI::process( const QString& message ) else if ( cmd == "PASV" ) { passiv = TRUE; - send( "227 Entering Passive Mode (" + send( "227 Entering Passive Mode (" // No tr + address().toString().replace( QRegExp( "\\." ), "," ) + "," + QString::number( ( serversocket->port() ) >> 8 ) + "," @@ -483,7 +524,7 @@ void ServerPI::process( const QString& message ) else if ( cmd == "TYPE" ) { if ( arg.upper() == "A" || arg.upper() == "I" ) - send( "200 Command okay" ); + send( "200 Command okay" ); // No tr else - send( "504 Command not implemented for that parameter" ); + send( "504 Command not implemented for that parameter" ); // No tr } @@ -491,7 +532,7 @@ void ServerPI::process( const QString& message ) else if ( cmd == "STRU" ) { if ( arg.upper() == "F" ) - send( "200 Command okay" ); + send( "200 Command okay" ); // No tr else - send( "504 Command not implemented for that parameter" ); + send( "504 Command not implemented for that parameter" ); // No tr } @@ -499,7 +540,7 @@ void ServerPI::process( const QString& message ) else if ( cmd == "MODE" ) { if ( arg.upper() == "S" ) - send( "200 Command okay" ); + send( "200 Command okay" ); // No tr else - send( "504 Command not implemented for that parameter" ); + send( "504 Command not implemented for that parameter" ); // No tr } @@ -512,10 +553,10 @@ void ServerPI::process( const QString& message ) if ( !args.isEmpty() && checkReadFile( absFilePath( args ) ) || backupRestoreGzip( absFilePath( args ) ) ) { - send( "150 File status okay" ); + send( "150 File status okay" ); // No tr sendFile( absFilePath( args ) ); } else { qDebug("550 Requested action not taken"); - send( "550 Requested action not taken" ); + send( "550 Requested action not taken" ); // No tr } @@ -523,28 +564,29 @@ void ServerPI::process( const QString& message ) else if ( cmd == "STOR" ) if ( !args.isEmpty() && checkWriteFile( absFilePath( args ) ) ) { - send( "150 File status okay" ); + send( "150 File status okay" ); // No tr retrieveFile( absFilePath( args ) ); } else - send( "550 Requested action not taken" ); + send( "550 Requested action not taken" ); // No tr // store unique (STOU) else if ( cmd == "STOU" ) { - send( "502 Command not implemented" ); + send( "502 Command not implemented" ); // No tr } // append (APPE) else if ( cmd == "APPE" ) { - send( "502 Command not implemented" ); + send( "502 Command not implemented" ); // No tr } // allocate (ALLO) else if ( cmd == "ALLO" ) { - send( "200 Command okay" ); + storFileSize = args.toInt(); + send( "200 Command okay" ); // No tr } // restart (REST) else if ( cmd == "REST" ) { - send( "502 Command not implemented" ); + send( "502 Command not implemented" ); // No tr } @@ -553,13 +595,13 @@ void ServerPI::process( const QString& message ) renameFrom = QString::null; if ( args.isEmpty() ) - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr else { QFile file( absFilePath( args ) ); if ( file.exists() ) { - send( "350 File exists, ready for destination name" ); + send( "350 File exists, ready for destination name" ); // No tr renameFrom = absFilePath( args ); } else - send( "550 Requested action not taken" ); + send( "550 Requested action not taken" ); // No tr } } @@ -568,13 +610,13 @@ void ServerPI::process( const QString& message ) else if ( cmd == "RNTO" ) { if ( lastCommand != "RNFR" ) - send( "503 Bad sequence of commands" ); + send( "503 Bad sequence of commands" ); // No tr else if ( args.isEmpty() ) - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr else { QDir dir( absFilePath( args ) ); if ( dir.rename( renameFrom, absFilePath( args ), TRUE ) ) - send( "250 Requested file action okay, completed." ); + send( "250 Requested file action okay, completed." ); // No tr else - send( "550 Requested action not taken" ); + send( "550 Requested action not taken" ); // No tr } } @@ -584,7 +626,7 @@ void ServerPI::process( const QString& message ) dtp->close(); if ( dtp->dtpMode() != ServerDTP::Idle ) - send( "426 Connection closed; transfer aborted" ); + send( "426 Connection closed; transfer aborted" ); // No tr else - send( "226 Closing data connection" ); + send( "226 Closing data connection" ); // No tr } @@ -592,14 +634,13 @@ void ServerPI::process( const QString& message ) else if ( cmd == "DELE" ) { if ( args.isEmpty() ) - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr else { QFile file( absFilePath( args ) ) ; if ( file.remove() ) { - send( "250 Requested file action okay, completed" ); + send( "250 Requested file action okay, completed" ); // No tr QCopEnvelope e("QPE/System", "linkChanged(QString)" ); e << file.name(); - } - else { - send( "550 Requested action not taken" ); + } else { + send( "550 Requested action not taken" ); // No tr } } @@ -609,11 +650,11 @@ void ServerPI::process( const QString& message ) else if ( cmd == "RMD" ) { if ( args.isEmpty() ) - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr else { QDir dir; if ( dir.rmdir( absFilePath( args ), TRUE ) ) - send( "250 Requested file action okay, completed" ); + send( "250 Requested file action okay, completed" ); // No tr else - send( "550 Requested action not taken" ); + send( "550 Requested action not taken" ); // No tr } } @@ -623,12 +664,12 @@ void ServerPI::process( const QString& message ) if ( args.isEmpty() ) { qDebug(" Error: no arg"); - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr } else { QDir dir; if ( dir.mkdir( absFilePath( args ), TRUE ) ) - send( "250 Requested file action okay, completed." ); + send( "250 Requested file action okay, completed." ); // No tr else - send( "550 Requested action not taken" ); + send( "550 Requested action not taken" ); // No tr } } @@ -642,7 +683,7 @@ void ServerPI::process( const QString& message ) else if ( cmd == "LIST" ) { if ( sendList( absFilePath( args ) ) ) - send( "150 File status okay" ); + send( "150 File status okay" ); // No tr else - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr } @@ -653,5 +694,5 @@ void ServerPI::process( const QString& message ) bool gzipfile = backupRestoreGzip( filePath ); if ( !fi.exists() && !gzipfile ) - send( "500 Syntax error, command unrecognized" ); + send( "500 Syntax error, command unrecognized" ); // No tr else { if ( !gzipfile ) @@ -668,5 +709,5 @@ void ServerPI::process( const QString& message ) QString size = out.left( out.find("\t") ); int guess = size.toInt() / 5; - if ( filePath.contains("doc") ) + if ( filePath.contains("doc") ) // No tr guess *= 1000; qDebug("sending back gzip guess of %d", guess); @@ -678,35 +719,35 @@ void ServerPI::process( const QString& message ) // name list (NLST) else if ( cmd == "NLST" ) { - send( "502 Command not implemented" ); + send( "502 Command not implemented" ); // No tr } // site parameters (SITE) else if ( cmd == "SITE" ) { - send( "502 Command not implemented" ); + send( "502 Command not implemented" ); // No tr } // system (SYST) else if ( cmd == "SYST" ) { - send( "215 UNIX Type: L8" ); + send( "215 UNIX Type: L8" ); // No tr } // status (STAT) else if ( cmd == "STAT" ) { - send( "502 Command not implemented" ); + send( "502 Command not implemented" ); // No tr } // help (HELP ) else if ( cmd == "HELP" ) { - send( "502 Command not implemented" ); + send( "502 Command not implemented" ); // No tr } // noop (NOOP) else if ( cmd == "NOOP" ) { - send( "200 Command okay" ); + send( "200 Command okay" ); // No tr } // not implemented else - send( "502 Command not implemented" ); + send( "502 Command not implemented" ); // No tr lastCommand = cmd; @@ -715,5 +756,5 @@ void ServerPI::process( const QString& message ) bool ServerPI::backupRestoreGzip( const QString &file ) { - return (file.find( "backup" ) != -1 && + return (file.find( "backup" ) != -1 && // No tr file.findRev( ".tgz" ) == (int)file.length() - 4 ); } @@ -721,5 +762,5 @@ bool ServerPI::backupRestoreGzip( const QString &file ) bool ServerPI::backupRestoreGzip( const QString &file, QStringList &targets ) { - if ( file.find( "backup" ) != -1 && + if ( file.find( "backup" ) != -1 && // No tr file.findRev( ".tgz" ) == (int)file.length() - 4 ) { QFileInfo info( file ); @@ -744,6 +785,5 @@ void ServerPI::sendFile( const QString& file ) if ( backupRestoreGzip( file, targets ) ) dtp->sendGzipFile( file, targets, peeraddress, peerport ); - else - dtp->sendFile( file, peeraddress, peerport ); + else dtp->sendFile( file, peeraddress, peerport ); } } @@ -762,5 +802,5 @@ void ServerPI::retrieveFile( const QString& file ) dtp->retrieveGzipFile( file, peeraddress, peerport ); else - dtp->retrieveFile( file, peeraddress, peerport ); + dtp->retrieveFile( file, peeraddress, peerport, storFileSize ); } } @@ -769,6 +809,5 @@ 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 @@ -781,5 +820,5 @@ bool ServerPI::parsePort( const QString& pp ) void ServerPI::dtpCompleted() { - send( "226 Closing data connection, file transfer successful" ); + send( "226 Closing data connection, file transfer successful" ); // No tr if ( dtp->dtpMode() == ServerDTP::RetrieveFile ) { QString fn = dtp->fileName(); @@ -791,4 +830,5 @@ void ServerPI::dtpCompleted() waitsocket = 0; dtp->close(); + storFileSize = -1; } @@ -797,5 +837,6 @@ void ServerPI::dtpFailed() dtp->close(); waitsocket = 0; - send( "451 Requested action aborted: local error in processing" ); + send( "451 Requested action aborted: local error in processing" ); // No tr + storFileSize = -1; } @@ -804,5 +845,6 @@ void ServerPI::dtpError( int ) dtp->close(); waitsocket = 0; - send( "451 Requested action aborted: local error in processing" ); + send( "451 Requested action aborted: local error in processing" ); // No tr + storFileSize = -1; } @@ -822,6 +864,5 @@ bool ServerPI::sendList( const QString& arg ) QFileInfo fi( fn ); - if ( !fi.exists() ) - return FALSE; + if ( !fi.exists() ) return FALSE; // return file listing @@ -845,5 +886,5 @@ bool ServerPI::sendList( const QString& arg ) } - ts << "total " << QString::number( total / 1024 ) << endl; + ts << "total " << QString::number( total / 1024 ) << endl; // No tr it.toFirst(); @@ -871,6 +912,5 @@ bool ServerPI::sendList( const QString& arg ) QString ServerPI::fileListing( QFileInfo *info ) { - if ( !info ) - return QString::null; + if ( !info ) return QString::null; QString s; @@ -898,8 +938,14 @@ QString ServerPI::fileListing( QFileInfo *info ) // owner - s += info->owner().leftJustify( 8, ' ', TRUE ) + " "; + QString o = info->owner(); + if ( o.isEmpty() ) + o = QString::number(info->ownerId()); + s += o.leftJustify( 8, ' ', TRUE ) + " "; // group - s += info->group().leftJustify( 8, ' ', TRUE ) + " "; + QString g = info->group(); + if ( g.isEmpty() ) + g = QString::number(info->groupId()); + s += g.leftJustify( 8, ' ', TRUE ) + " "; // file size in bytes @@ -922,49 +968,30 @@ QString ServerPI::fileListing( QFileInfo *info ) 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; @@ -975,6 +1002,5 @@ void ServerPI::newConnection( int socket ) //qDebug( "New incomming connection" ); - if ( !passiv ) - return ; + if ( !passiv ) return; if ( wait[SendFile] ) { @@ -991,5 +1017,5 @@ void ServerPI::newConnection( int socket ) dtp->retrieveGzipFile( waitfile ); else - dtp->retrieveFile( waitfile ); + dtp->retrieveFile( waitfile, storFileSize ); dtp->setSocket( socket ); } @@ -1012,6 +1038,5 @@ void ServerPI::newConnection( int socket ) QString ServerPI::absFilePath( const QString& file ) { - if ( file.isEmpty() ) - return file; + if ( file.isEmpty() ) return file; QString filepath( file ); @@ -1031,5 +1056,5 @@ void ServerPI::timerEvent( QTimerEvent * ) ServerDTP::ServerDTP( QObject *parent, const char* name) : QSocket( parent, name ), mode( Idle ), createTargzProc( 0 ), - retrieveTargzProc( 0 ), gzipProc( 0 ) + retrieveTargzProc( 0 ) { @@ -1039,17 +1064,15 @@ ServerDTP::ServerDTP( QObject *parent, const char* name) connect( this, SIGNAL( readyRead() ), SLOT( readyRead() ) ); - gzipProc = new OProcess( this, "gzipProc" ); - - createTargzProc = new OProcess( QString("tar"), this, "createTargzProc"); + createTargzProc = new QProcess( QString("tar"), this, "createTargzProc"); // No tr + createTargzProc->setCommunication( QProcess::Stdout ); createTargzProc->setWorkingDirectory( QDir::rootDirPath() ); - connect( createTargzProc, SIGNAL( processExited(OProcess *) ), SLOT( targzDone() ) ); + connect( createTargzProc, SIGNAL( processExited() ), SLOT( targzDone() ) ); - QStringList args = "tar"; - args += "-xv"; - retrieveTargzProc = new OProcess( args, this, "retrieveTargzProc" ); + retrieveTargzProc = new QProcess( this, "retrieveTargzProc" ); + retrieveTargzProc->setCommunication( QProcess::Stdin ); retrieveTargzProc->setWorkingDirectory( QDir::rootDirPath() ); - connect( retrieveTargzProc, SIGNAL( processExited(OProcess *) ), + connect( retrieveTargzProc, SIGNAL( processExited() ), SIGNAL( completed() ) ); - connect( retrieveTargzProc, SIGNAL( processExited(OProcess *) ), + connect( retrieveTargzProc, SIGNAL( processExited() ), SLOT( extractTarDone() ) ); } @@ -1058,5 +1081,14 @@ ServerDTP::~ServerDTP() { buf.close(); + if ( RetrieveFile == mode && file.isOpen() ) { + // We're being shutdown before the client closed. + file.close(); + if ( recvFileSize >= 0 && (int)file.size() != recvFileSize ) { + qDebug( "STOR incomplete" ); + file.remove(); + } + } else { file.close(); + } createTargzProc->kill(); } @@ -1066,6 +1098,5 @@ void ServerDTP::extractTarDone() qDebug("extract done"); #ifndef QT_NO_COP - - QCopEnvelope e( "QPE/Desktop", "restoreDone(QString)" ); + QCopEnvelope e( "QPE/System", "restoreDone(QString)" ); e << file.name(); #endif @@ -1091,8 +1122,7 @@ void ServerDTP::connected() emit completed(); mode = Idle; - } - else { - - if ( !file.atEnd() ) { + } else { + // Don't write more if there is plenty buffered already. + if ( bytesToWrite() <= block_size && !file.atEnd() ) { QCString s; s.resize( block_size ); @@ -1111,8 +1141,7 @@ void ServerDTP::connected() bytes_written = 0; qDebug("==>start send tar process"); - if ( !createTargzProc->start(OProcess::NotifyOnExit, OProcess::Stdout) ) - qWarning("Error starting %s or %s", - createTargzProc->args()[0].data(), - gzipProc->args()[0].data()); + if ( !createTargzProc->start() ) + qWarning("Error starting %s", + createTargzProc->arguments().join(" ").latin1()); break; case SendBuffer: @@ -1185,11 +1214,17 @@ void ServerDTP::connectionClosed() else if ( RetrieveFile == mode ) { file.close(); + if ( recvFileSize >= 0 && (int)file.size() != recvFileSize ) { + qDebug( "STOR incomplete" ); + file.remove(); + emit failed(); + } else { emit completed(); } + } else if ( RetrieveGzipFile == mode ) { qDebug("Done writing ungzip file; closing input"); - gzipProc->flushStdin(); - gzipProc->closeStdin(); + retrieveTargzProc->flushStdin(); + retrieveTargzProc->closeStdin(); } @@ -1245,11 +1280,11 @@ void ServerDTP::readyRead() } else if ( RetrieveGzipFile == mode ) { - if ( !gzipProc->isRunning() ) - gzipProc->start(OProcess::NotifyOnExit, (OProcess::Communication) ( OProcess::Stdin | OProcess::Stdout )); + if ( !retrieveTargzProc->isRunning() ) + retrieveTargzProc->start(); QByteArray s; s.resize( bytesAvailable() ); readBlock( s.data(), bytesAvailable() ); - gzipProc->writeStdin( s.data(), s.size() ); + retrieveTargzProc->writeToStdin( s ); qDebug("wrote %d bytes to ungzip ", s.size() ); } @@ -1263,33 +1298,18 @@ void ServerDTP::readyRead() } -void ServerDTP::writeTargzBlock(OProcess *, char *buffer, int buflen) +void ServerDTP::writeTargzBlock() { - writeBlock( buffer, buflen ); - qDebug("writeTargzBlock %d", buflen); - if ( !createTargzProc->isRunning() ) { - qDebug("tar and gzip done"); - emit completed(); - mode = Idle; - disconnect( gzipProc, SIGNAL( receivedStdout(OProcess *, char *, int ) ), - this, SLOT( writeTargzBlock(OProcess *, char *, int) ) ); - } + QByteArray block = createTargzProc->readStdout(); + writeBlock( block.data(), block.size() ); + qDebug("writeTargzBlock %d", block.size()); } void ServerDTP::targzDone() { - //qDebug("targz done"); - disconnect( createTargzProc, SIGNAL( receivedStdout(OProcess *, char *, int) ), - this, SLOT( gzipTarBlock(OProcess *, char *, int) ) ); - gzipProc->closeStdin(); -} - -void ServerDTP::gzipTarBlock(OProcess *, char *buffer, int buflen) -{ - //qDebug("gzipTarBlock"); - if ( !gzipProc->isRunning() ) { - //qDebug("auto start gzip proc"); - gzipProc->start(OProcess::NotifyOnExit, (OProcess::Communication) ( OProcess::Stdin | OProcess::Stdout )); - } - gzipProc->writeStdin( buffer, buflen ); + qDebug("tar and gzip done"); + emit completed(); + mode = Idle; + disconnect( createTargzProc, SIGNAL( readyReadStdout() ), + this, SLOT( writeTargzBlock() ) ); } @@ -1321,43 +1341,16 @@ void ServerDTP::sendGzipFile( const QString &fn, file.setName( fn ); - QStringList args = "tar"; - args += "-cv"; + QStringList args = "targzip"; + //args += "-cv"; args += archiveTargets; qDebug("sendGzipFile %s", args.join(" ").latin1() ); - createTargzProc->clearArguments( ); - *createTargzProc << args; + createTargzProc->setArguments( args ); connect( createTargzProc, - SIGNAL( receivedStdout(OProcess *, char *, int) ), SLOT( gzipTarBlock(OProcess *, char *, int) ) ); - - gzipProc->clearArguments( ); - *gzipProc << "gzip"; - connect( gzipProc, SIGNAL( receivedStdout(OProcess *, char *, int) ), - SLOT( writeTargzBlock(OProcess *, char *, int) ) ); + SIGNAL( readyReadStdout() ), SLOT( writeTargzBlock() ) ); } -void ServerDTP::gunzipDone() -{ - qDebug("gunzipDone"); - disconnect( gzipProc, SIGNAL( processExited() ), - this, SLOT( gunzipDone() ) ); - retrieveTargzProc->closeStdin(); - disconnect( gzipProc, SIGNAL( receivedStdout(OProcess *, char *, int) ), - this, SLOT( tarExtractBlock(OProcess *, char *, int) ) ); -} - -void ServerDTP::tarExtractBlock(OProcess *, char *buffer, int buflen) -{ - qDebug("tarExtractBlock"); - if ( !retrieveTargzProc->isRunning() ) { - qDebug("auto start ungzip proc"); - if ( !retrieveTargzProc->start(OProcess::NotifyOnExit, OProcess::Stdin) ) - qWarning(" failed to start tar -x process"); - } - retrieveTargzProc->writeStdin( buffer, buflen ); -} - - -void ServerDTP::retrieveFile( const QString fn, const QHostAddress& host, Q_UINT16 port ) +void ServerDTP::retrieveFile( const QString fn, const QHostAddress& host, Q_UINT16 port, int fileSize ) { + recvFileSize = fileSize; file.setName( fn ); mode = RetrieveFile; @@ -1365,6 +1358,7 @@ void ServerDTP::retrieveFile( const QString fn, const QHostAddress& host, Q_UINT } -void ServerDTP::retrieveFile( const QString fn ) +void ServerDTP::retrieveFile( const QString fn, int fileSize ) { + recvFileSize = fileSize; file.setName( fn ); mode = RetrieveFile; @@ -1377,10 +1371,7 @@ void ServerDTP::retrieveGzipFile( const QString &fn ) mode = RetrieveGzipFile; - gzipProc->clearArguments(); - *gzipProc << "gunzip"; - connect( gzipProc, SIGNAL( readyReadStdout() ), - SLOT( tarExtractBlock() ) ); - connect( gzipProc, SIGNAL( processExited() ), - SLOT( gunzipDone() ) ); + retrieveTargzProc->setArguments( "targunzip" ); + connect( retrieveTargzProc, SIGNAL( processExited() ), + SLOT( extractTarDone() ) ); } |