summaryrefslogtreecommitdiff
path: root/core/qws/transferserver.cpp
Side-by-side diff
Diffstat (limited to 'core/qws/transferserver.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--core/qws/transferserver.cpp32
1 files changed, 16 insertions, 16 deletions
diff --git a/core/qws/transferserver.cpp b/core/qws/transferserver.cpp
index 30bf438..daf63ec 100644
--- a/core/qws/transferserver.cpp
+++ b/core/qws/transferserver.cpp
@@ -201,205 +201,205 @@ bool SyncAuthentication::checkPassword( const QString& password )
cpwd = QString::fromLocal8Bit( spw->sp_pwdp );
// Note: some systems use more than crypt for passwords.
QString cpassword = QString::fromLocal8Bit( crypt( password.local8Bit(), cpwd.local8Bit() ) );
if ( cpwd == cpassword )
return TRUE;
#endif
static int lastdenial = 0;
static int denials = 0;
int now = time(0);
// Detect old Qtopia Desktop (no password)
if ( password.isEmpty() ) {
if ( denials < 1 || now > lastdenial + 600 ) {
QMessageBox::warning( 0, 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") );
denials++;
lastdenial = now;
}
return FALSE;
}
// 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");
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 ) {
denials++;
lastdenial = now;
return FALSE;
}
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
#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;
dtp = new ServerDTP( this );
connect( dtp, SIGNAL( completed() ), SLOT( dtpCompleted() ) );
connect( dtp, SIGNAL( failed() ), SLOT( dtpFailed() ) );
- connect( dtp, SIGNAL( error( int ) ), SLOT( dtpError( int ) ) );
+ connect( dtp, SIGNAL( error(int) ), SLOT( dtpError(int) ) );
directory = QDir::currentDirPath();
static int p = 1024;
while ( !serversocket || !serversocket->ok() ) {
delete serversocket;
serversocket = new ServerSocket( ++p, this );
}
- connect( serversocket, SIGNAL( newIncomming( int ) ),
- SLOT( newConnection( int ) ) );
+ 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;
//qDebug( "Reply: %s", msg.latin1() );
}
void ServerPI::read()
{
while ( canReadLine() )
process( readLine().stripWhiteSpace() );
}
bool ServerPI::checkReadFile( const QString& file )
{
QString filename;
if ( file[0] != "/" )
filename = directory.path() + "/" + file;
else
filename = file;
QFileInfo fi( filename );
return ( fi.exists() && fi.isReadable() );
}
bool ServerPI::checkWriteFile( const QString& file )
{
QString filename;
if ( file[0] != "/" )
filename = directory.path() + "/" + file;
else
filename = file;
QFileInfo fi( filename );
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 ;
// 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 ) {
QStringList copy( msg );
// FIXME: for Qt3
// copy.pop_front()
copy.remove( copy.begin() );
args = copy.join( " " );
}
//qDebug( "args: %s", args.latin1() );
// we always respond to QUIT, regardless of state
if ( cmd == "QUIT" ) {
send( "211 Good bye!" );
delete this;
return ;
}
// connected to client
if ( Connected == state )
return ;
@@ -953,208 +953,208 @@ QString ServerPI::permissionString( QFileInfo *info )
// 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 += "-";
// 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 += "-";
return s;
}
void ServerPI::newConnection( int socket )
{
//qDebug( "New incomming connection" );
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 ) )
dtp->retrieveGzipFile( waitfile );
else
dtp->retrieveFile( waitfile );
dtp->setSocket( socket );
}
else if ( wait[SendByteArray] ) {
dtp->sendByteArray( waitarray );
dtp->setSocket( socket );
}
else if ( wait[RetrieveByteArray] ) {
qDebug("retrieve byte array");
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;
QString filepath( file );
if ( file[0] != "/" )
filepath = directory.path() + "/" + file;
return filepath;
}
void ServerPI::timerEvent( QTimerEvent * )
{
connectionClosed();
}
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( bytesWritten(int) ), SLOT( bytesWritten(int) ) );
connect( this, SIGNAL( readyRead() ), SLOT( readyRead() ) );
gzipProc = new OProcess( this, "gzipProc" );
createTargzProc = new OProcess( QString("tar"), this, "createTargzProc");
createTargzProc->setWorkingDirectory( QDir::rootDirPath() );
- connect( createTargzProc, SIGNAL( processExited(OProcess *) ), SLOT( targzDone() ) );
+ connect( createTargzProc, SIGNAL( processExited(OProcess*) ), SLOT( targzDone() ) );
QStringList args = "tar";
args += "-xv";
retrieveTargzProc = new OProcess( args, this, "retrieveTargzProc" );
retrieveTargzProc->setWorkingDirectory( QDir::rootDirPath() );
- connect( retrieveTargzProc, SIGNAL( processExited(OProcess *) ),
+ connect( retrieveTargzProc, SIGNAL( processExited(OProcess*) ),
SIGNAL( completed() ) );
- connect( retrieveTargzProc, SIGNAL( processExited(OProcess *) ),
+ 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 {
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(OProcess::NotifyOnExit, OProcess::Stdout) )
qWarning("Error starting %s or %s",
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() );
buf.close();
break;
case RetrieveFile:
// retrieve file mode
if ( file.exists() && !file.remove() ) {
emit failed();
mode = Idle;
return ;
}
if ( !file.open( IO_WriteOnly) ) {
emit failed();
mode = Idle;
return ;
}
// qDebug( "Debug: Retrieving file %s", file.name().latin1() );
break;
case RetrieveGzipFile:
qDebug("=-> starting tar process to receive .tgz file");
break;
case RetrieveBuffer:
@@ -1188,248 +1188,248 @@ void ServerDTP::connectionClosed()
else if ( SendBuffer == mode ) {
if ( bytes_written == buf.size() )
emit completed();
else
emit failed();
}
// retrieve file mode
else if ( RetrieveFile == mode ) {
file.close();
emit completed();
}
else if ( RetrieveGzipFile == mode ) {
qDebug("Done writing ungzip file; closing input");
gzipProc->flushStdin();
gzipProc->closeStdin();
}
// retrieve buffer mode
else if ( RetrieveBuffer == mode ) {
buf.close();
emit completed();
}
mode = Idle;
}
void ServerDTP::bytesWritten( int bytes )
{
bytes_written += bytes;
// send file mode
if ( SendFile == mode ) {
if ( bytes_written == file.size() ) {
// qDebug( "Debug: Sending complete: %d bytes", file.size() );
file.close();
emit completed();
mode = Idle;
}
else if ( !file.atEnd() ) {
QCString s;
s.resize( block_size );
int bytes = file.readBlock( s.data(), block_size );
writeBlock( s.data(), bytes );
}
}
// send buffer mode
if ( SendBuffer == mode ) {
if ( bytes_written == buf.size() ) {
// qDebug( "Debug: Sending complete: %d bytes", buf.size() );
emit completed();
mode = Idle;
}
}
}
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(OProcess::NotifyOnExit, (OProcess::Communication) ( OProcess::Stdin | OProcess::Stdout ));
QByteArray s;
s.resize( bytesAvailable() );
readBlock( s.data(), bytesAvailable() );
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(OProcess *, char *buffer, int buflen)
{
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) ) );
+ disconnect( gzipProc, SIGNAL( receivedStdout(OProcess*,char*,int) ),
+ this, SLOT( writeTargzBlock(OProcess*,char*,int) ) );
}
}
void ServerDTP::targzDone()
{
//qDebug("targz done");
- disconnect( createTargzProc, SIGNAL( receivedStdout(OProcess *, char *, int) ),
- this, SLOT( gzipTarBlock(OProcess *, char *, int) ) );
+ 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 );
}
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 );
mode = SendFile;
}
void ServerDTP::sendGzipFile( const QString &fn,
const QStringList &archiveTargets,
const QHostAddress& host, Q_UINT16 port )
{
sendGzipFile( fn, archiveTargets );
connectToHost( host.toString(), port );
}
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->clearArguments( );
*createTargzProc << args;
connect( createTargzProc,
- SIGNAL( receivedStdout(OProcess *, char *, int) ), SLOT( gzipTarBlock(OProcess *, char *, int) ) );
+ 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) ) );
+ 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( receivedStdout(OProcess *, char *, int) ),
- this, SLOT( tarExtractBlock(OProcess *, char *, int) ) );
+ 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 )
{
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->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 );
}
void ServerDTP::sendByteArray( const QByteArray& array, const QHostAddress& host, Q_UINT16 port )
{
buf.setBuffer( array );
mode = SendBuffer;
connectToHost( host.toString(), port );
}
void ServerDTP::sendByteArray( const QByteArray& array )
{
buf.setBuffer( array );
mode = SendBuffer;
}
void ServerDTP::retrieveByteArray( const QHostAddress& host, Q_UINT16 port )
{
buf.setBuffer( QByteArray() );
mode = RetrieveBuffer;
connectToHost( host.toString(), port );
}
void ServerDTP::retrieveByteArray()
{
buf.setBuffer( QByteArray() );
mode = RetrieveBuffer;
}
void ServerDTP::setSocket( int socket )
{
QSocket::setSocket( socket );
connected();
}