summaryrefslogtreecommitdiff
path: root/core/launcher/transferserver.cpp
Side-by-side diff
Diffstat (limited to 'core/launcher/transferserver.cpp') (more/less context) (show whitespace changes)
-rw-r--r--core/launcher/transferserver.cpp207
1 files changed, 153 insertions, 54 deletions
diff --git a/core/launcher/transferserver.cpp b/core/launcher/transferserver.cpp
index 7294f9c..a6dab07 100644
--- a/core/launcher/transferserver.cpp
+++ b/core/launcher/transferserver.cpp
@@ -1,120 +1,239 @@
/**********************************************************************
-** Copyright (C) 2000 Trolltech AS. All rights reserved.
+** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
**
-** This file is part of Qtopia Environment.
+** This file is part of the Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#define _XOPEN_SOURCE
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
+#include <time.h>
+#include <shadow.h>
+
+extern "C" {
+#include <uuid/uuid.h>
+#define UUID_H_INCLUDED
+}
#if defined(_OS_LINUX_)
#include <shadow.h>
#endif
#include <qdir.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qdatastream.h>
#include <qmessagebox.h>
#include <qstringlist.h>
#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>
+#ifdef QWS
#include <qpe/qcopenvelope_qws.h>
+#endif
#include "transferserver.h"
#include "qprocess.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 );
}
-bool accessAuthorized(QHostAddress peeraddress)
+QString SyncAuthentication::serverId()
+{
+ Config cfg("Security");
+ cfg.setGroup("Sync");
+ QString r=cfg.readEntry("serverid");
+ if ( r.isEmpty() ) {
+ uuid_t uuid;
+ uuid_generate( uuid );
+ cfg.writeEntry("serverid",(r = QUuid( uuid ).toString()));
+ }
+ return r;
+}
+
+QString SyncAuthentication::ownerName()
+{
+ QString vfilename = Global::applicationFileName("addressbook",
+ "businesscard.vcf");
+ if (QFile::exists(vfilename)) {
+ Contact c;
+ c = Contact::readVCard( vfilename )[0];
+ return c.fullName();
+ }
+
+ return "";
+}
+
+QString SyncAuthentication::loginName()
+{
+ struct passwd *pw;
+ pw = getpwuid( geteuid() );
+ return QString::fromLocal8Bit( pw->pw_name );
+}
+
+int SyncAuthentication::isAuthorized(QHostAddress peeraddress)
{
Config cfg("Security");
cfg.setGroup("Sync");
- uint auth_peer = cfg.readNumEntry("auth_peer",0xc0a80100);
+ QString allowedstr = cfg.readEntry("auth_peer","192.168.1.0");
+ QHostAddress allowed;
+ allowed.setAddress(allowedstr);
+ uint auth_peer = allowed.ip4Addr();
uint auth_peer_bits = cfg.readNumEntry("auth_peer_bits",24);
- bool ok = (peeraddress.ip4Addr() & (((1<<auth_peer_bits)-1)<<(32-auth_peer_bits)))
- == auth_peer;
- /* Allows denial-of-service attack.
- if ( !ok ) {
- QMessageBox::warning(0,tr("Security"),
- tr("<p>An attempt to access this device from %1 has been denied.")
- .arg(peeraddress.toString()));
+ 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;
+ 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;
+
+ pw = getpwuid( geteuid() );
+ spw = getspnam( pw->pw_name );
+
+ QString cpwd = QString::fromLocal8Bit( pw->pw_passwd );
+ if ( cpwd == "x" && spw )
+ 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 ok;
+ return FALSE;
}
+ // Second, check sync password...
+ if ( password.left(6) == "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"))==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 ( !accessAuthorized(peeraddress) ) {
+ 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 transfer service ready!" );
+ 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 ) ) );
directory = QDir::currentDirPath();
static int p = 1024;
@@ -142,55 +261,24 @@ 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::checkUser( const QString& user )
-{
- if ( user.isEmpty() ) return FALSE;
-
- struct passwd *pw;
- pw = getpwuid( geteuid() );
- QString euser = QString::fromLocal8Bit( pw->pw_name );
- return user == euser;
-}
-
-bool ServerPI::checkPassword( const QString& /* password */ )
-{
- // ### HACK for testing on local host
- return true;
-
- /*
- struct passwd *pw = 0;
- struct spwd *spw = 0;
-
- pw = getpwuid( geteuid() );
- spw = getspnam( pw->pw_name );
-
- QString cpwd = QString::fromLocal8Bit( pw->pw_passwd );
- if ( cpwd == "x" && spw )
- cpwd = QString::fromLocal8Bit( spw->sp_pwdp );
-
- QString cpassword = QString::fromLocal8Bit( crypt( password.local8Bit(), cpwd.local8Bit() ) );
- return cpwd == cpassword;
-*/
-}
-
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() );
}
@@ -245,38 +333,37 @@ void ServerPI::process( const QString& message )
send( "211 Good bye!" );
delete this;
return;
}
// connected to client
if ( Connected == state )
return;
// waiting for user name
if ( Wait_USER == state ) {
- if ( cmd != "USER" || msg.count() < 2 || !checkUser( arg ) ) {
+ if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) {
send( "530 Please login with USER and PASS" );
return;
}
send( "331 User name ok, need password" );
state = Wait_PASS;
return;
}
// waiting for password
if ( Wait_PASS == state ) {
- if ( cmd != "PASS" || !checkPassword( arg ) ) {
- //if ( cmd != "PASS" || msg.count() < 2 || !checkPassword( arg ) ) {
+ if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) {
send( "530 Please login with USER and PASS" );
return;
}
send( "230 User logged in, proceed" );
state = Ready;
return;
}
// ACCESS CONTROL COMMANDS
// account (ACCT)
@@ -445,30 +532,33 @@ void ServerPI::process( const QString& message )
if ( dtp->dtpMode() != ServerDTP::Idle )
send( "426 Connection closed; transfer aborted" );
else
send( "226 Closing data connection" );
}
// delete (DELE)
else if ( cmd == "DELE" ) {
if ( args.isEmpty() )
send( "500 Syntax error, command unrecognized" );
else {
QFile file( absFilePath( args ) ) ;
- if ( file.remove() )
+ if ( file.remove() ) {
send( "250 Requested file action okay, completed" );
- else
+ QCopEnvelope e("QPE/System", "linkChanged(QString)" );
+ e << file.name();
+ } 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 ) )
send( "250 Requested file action okay, completed" );
else
send( "550 Requested action not taken" );
}
@@ -625,27 +715,34 @@ bool ServerPI::parsePort( const QString& pp )
QStringList p = QStringList::split( ",", pp );
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()
{
- dtp->close();
- waitsocket = 0;
send( "226 Closing data connection, file transfer successful" );
+ if ( dtp->dtpMode() == ServerDTP::RetrieveFile ) {
+ QString fn = dtp->fileName();
+ if ( fn.right(8)==".desktop" && fn.find("/Documents/")>=0 ) {
+ QCopEnvelope e("QPE/System", "linkChanged(QString)" );
+ e << fn;
+ }
+ }
+ waitsocket = 0;
+ dtp->close();
}
void ServerPI::dtpFailed()
{
dtp->close();
waitsocket = 0;
send( "451 Requested action aborted: local error in processing" );
}
void ServerPI::dtpError( int )
{
dtp->close();
@@ -843,25 +940,25 @@ QString ServerPI::absFilePath( const QString& file )
filepath = directory.path() + "/" + file;
return filepath;
}
void ServerPI::timerEvent( QTimerEvent * )
{
connectionClosed();
}
-ServerDTP::ServerDTP( QObject *parent, const char* name )
+ServerDTP::ServerDTP( QObject *parent = 0, const char* name = 0)
: 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 );
@@ -882,26 +979,28 @@ retrieveTargzProc( 0 ), gzipProc( 0 )
}
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;
}