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 @@ -21,25 +21,26 @@ #include <pwd.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <time.h> #include <shadow.h> /* 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 #if defined(_OS_LINUX_) #include <shadow.h> #endif #include <qdir.h> #include <qfile.h> @@ -50,66 +51,70 @@ 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 "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 ) { 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); return "{" + stream.read().stripWhiteSpace() + "}"; } #endif } QString SyncAuthentication::serverId() @@ -154,25 +159,26 @@ int SyncAuthentication::isAuthorized(QHostAddress peeraddress) // QHostAddress allowed; // allowed.setAddress(allowedstr); // uint auth_peer = allowed.ip4Addr(); uint auth_peer_bits = cfg.readNumEntry("auth_peer_bits",24); 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; } 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 ) { #ifdef ALLOW_UNIX_USER_FTP // First, check system password... struct passwd *pw = 0; struct spwd *spw = 0; @@ -221,54 +227,56 @@ bool SyncAuthentication::checkPassword( const QString& password ) Config cfg("Security"); cfg.setGroup("Sync"); QString pwds = cfg.readEntry("Passwords"); if ( QStringList::split(QChar(' '),pwds).contains(cpassword) ) return TRUE; // 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; } } return FALSE; } ServerPI::ServerPI( int socket, QObject *parent , const char* name ) : QSocket( parent, name ) , dtp( 0 ), serversocket( 0 ), waitsocket( 0 ) { state = Connected; 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; for( int i = 0; i < 4; i++ ) wait[i] = FALSE; send( "220 Qtopia " QPE_VERSION " FTP Server" ); state = Wait_USER; @@ -284,25 +292,24 @@ ServerPI::ServerPI( int socket, QObject *parent , const char* name ) while ( !serversocket || !serversocket->ok() ) { delete serversocket; serversocket = new ServerSocket( ++p, this ); } connect( serversocket, SIGNAL( newIncomming( int ) ), SLOT( newConnection( int ) ) ); } } ServerPI::~ServerPI() { - } void ServerPI::connectionClosed() { // qDebug( "Debug: Connection closed" ); delete this; } void ServerPI::send( const QString& msg ) { QTextStream os( this ); os << msg << endl; @@ -342,25 +349,26 @@ bool ServerPI::checkWriteFile( const QString& file ) if ( fi.exists() ) if ( !QFile( filename ).remove() ) return FALSE; return TRUE; } 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; if ( msg.count() >= 2 ) arg = msg[1]; // full argument string QString args; if ( msg.count() >= 2 ) { @@ -581,25 +589,26 @@ void ServerPI::process( const QString& message ) } // delete (DELE) else if ( cmd == "DELE" ) { if ( args.isEmpty() ) send( "500 Syntax error, command unrecognized" ); 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) else if ( cmd == "RMD" ) { if ( args.isEmpty() ) send( "500 Syntax error, command unrecognized" ); else { QDir dir; if ( dir.rmdir( absFilePath( args ), TRUE ) ) @@ -725,49 +734,51 @@ bool ServerPI::backupRestoreGzip( const QString &file, QStringList &targets ) void ServerPI::sendFile( const QString& file ) { if ( passiv ) { wait[SendFile] = TRUE; waitfile = file; if ( waitsocket ) 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 ) { wait[RetrieveFile] = TRUE; waitfile = file; if ( waitsocket ) newConnection( waitsocket ); } else { QStringList targets; if ( backupRestoreGzip( file, targets ) ) dtp->retrieveGzipFile( file, peeraddress, peerport ); else dtp->retrieveFile( file, peeraddress, peerport ); } } 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; } void ServerPI::dtpCompleted() { send( "226 Closing data connection, file transfer successful" ); if ( dtp->dtpMode() == ServerDTP::RetrieveFile ) { @@ -801,25 +812,26 @@ bool ServerPI::sendList( const QString& arg ) QBuffer buffer( listing ); if ( !buffer.open( IO_WriteOnly ) ) return FALSE; QTextStream ts( &buffer ); 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; } // return directory listing else if ( fi.isDir() ) { QDir dir( fn ); const QFileInfoList *list = dir.entryInfoList( QDir::All | QDir::Hidden ); QFileInfoListIterator it( *list ); @@ -849,25 +861,26 @@ bool ServerPI::sendList( const QString& arg ) waitarray = buffer.buffer(); wait[SendByteArray] = TRUE; if ( waitsocket ) newConnection( waitsocket ); } else 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() ) s += "l"; else s += "-"; // permisson string s += permissionString( info ) + " "; @@ -899,59 +912,79 @@ QString ServerPI::fileListing( QFileInfo *info ) + QString::number( date.day() ).rightJustify( 2, ' ', TRUE ) + " " + QString::number( time.hour() ).rightJustify( 2, '0', TRUE ) + ":" + QString::number( time.minute() ).rightJustify( 2,'0', TRUE ) + " "; // file name s += info->fileName(); 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 dtp->sendFile( waitfile ); dtp->setSocket( socket ); } else if ( wait[RetrieveFile] ) { qDebug("check retrieve file"); if ( backupRestoreGzip( waitfile ) ) @@ -969,25 +1002,26 @@ void ServerPI::newConnection( int socket ) dtp->retrieveByteArray(); dtp->setSocket( socket ); } else waitsocket = 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; } void ServerPI::timerEvent( QTimerEvent * ) { connectionClosed(); @@ -995,101 +1029,100 @@ void ServerPI::timerEvent( QTimerEvent * ) ServerDTP::ServerDTP( QObject *parent, const char* name) : QSocket( parent, name ), mode( Idle ), createTargzProc( 0 ), 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(); file.close(); createTargzProc->kill(); } void ServerDTP::extractTarDone() { qDebug("extract done"); #ifndef QT_NO_COP + QCopEnvelope e( "QPE/Desktop", "restoreDone(QString)" ); e << file.name(); #endif } void ServerDTP::connected() { // send file mode switch ( mode ) { case SendFile : if ( !file.exists() || !file.open( IO_ReadOnly) ) { emit failed(); mode = Idle; return; } //qDebug( "Debug: Sending file '%s'", file.name().latin1() ); 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 ); } } break; case SendGzipFile: if ( createTargzProc->isRunning() ) { // SHOULDN'T GET HERE, BUT DOING A SAFETY CHECK ANYWAY 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; } // qDebug( "Debug: Sending byte array" ); bytes_written = 0; while( !buf.atEnd() ) putch( buf.getch() ); @@ -1203,71 +1236,70 @@ void ServerDTP::bytesWritten( int bytes ) void ServerDTP::readyRead() { // retrieve file mode if ( RetrieveFile == mode ) { QCString s; 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; connectToHost( host.toString(), port ); } void ServerDTP::sendFile( const QString fn ) { file.setName( fn ); @@ -1283,75 +1315,78 @@ void ServerDTP::sendGzipFile( const QString &fn, } void ServerDTP::sendGzipFile( const QString &fn, const QStringList &archiveTargets ) { mode = SendGzipFile; 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 ); mode = RetrieveFile; connectToHost( host.toString(), port ); } void ServerDTP::retrieveFile( const QString fn ) { file.setName( fn ); mode = RetrieveFile; } 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() ) ); } void ServerDTP::retrieveGzipFile( const QString &fn, const QHostAddress& host, Q_UINT16 port ) { retrieveGzipFile( fn ); connectToHost( host.toString(), port ); } |