summaryrefslogtreecommitdiff
authorzecke <zecke>2002-10-04 21:16:03 (UTC)
committer zecke <zecke>2002-10-04 21:16:03 (UTC)
commit10048c2dd7f7cd26c582d64b5e255b00b47de304 (patch) (side-by-side diff)
tree3b2a79d8218efe5f80d1b2e79e77b22cbc27c2ba
parentdc0344e987ba9b386056dd7bab7c5e34922eff5c (diff)
downloadopie-10048c2dd7f7cd26c582d64b5e255b00b47de304.zip
opie-10048c2dd7f7cd26c582d64b5e255b00b47de304.tar.gz
opie-10048c2dd7f7cd26c582d64b5e255b00b47de304.tar.bz2
Weird for some of you
_OS_LINUX_ does not get defined so now it's a if !define(_OS_LINUX) but this means you need to libuuid again I don't know how to fix that right. I should ask tt
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--core/launcher/transferserver.cpp14
1 files changed, 7 insertions, 7 deletions
diff --git a/core/launcher/transferserver.cpp b/core/launcher/transferserver.cpp
index a20df2f..f9204ab 100644
--- a/core/launcher/transferserver.cpp
+++ b/core/launcher/transferserver.cpp
@@ -1,622 +1,622 @@
/**********************************************************************
** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
**
** 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>
#ifndef _OS_LINUX_
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>
#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 );
}
/*
* small class in anonymous namespace
* to generate a QUUid for us
*/
namespace {
struct UidGen {
QString uuid();
};
-#if defined(_OS_LINUX_)
+#if !defined(_OS_LINUX_)
+ 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() {
QFile file( "/proc/sys/kernel/random/uuid" );
if (!file.open(IO_ReadOnly ) )
return QString::null;
QTextStream stream(&file);
return "{" + stream.read().stripWhiteSpace() + "}";
}
-#else
- QString UidGen::uuid() {
- uuid_t uuid;
- uuid_generate( uuid );
- return QUUid( uuid ).toString();
- }
#endif
}
QString SyncAuthentication::serverId()
{
Config cfg("Security");
cfg.setGroup("Sync");
QString r=cfg.readEntry("serverid");
if ( r.isEmpty() ) {
UidGen gen;
r = gen.uuid();
cfg.writeEntry("serverid", r );
}
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");
// QString allowedstr = cfg.readEntry("auth_peer","192.168.1.0");
uint auth_peer = cfg.readNumEntry("auth_peer",0xc0a80100);
// 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;
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 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"))==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 ) ) );
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 ) ) );
}
}
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;
// waiting for user name
if ( Wait_USER == state ) {
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" || !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)
if ( cmd == "ACCT" ) {
// even wu-ftp does not support it
send( "502 Command not implemented" );
}
// change working directory (CWD)
else if ( cmd == "CWD" ) {
if ( !args.isEmpty() ) {
if ( directory.cd( args, TRUE ) )
send( "250 Requested file action okay, completed" );
else
send( "550 Requested action not taken" );
}
else
send( "500 Syntax error, command unrecognized" );
}
// change to parent directory (CDUP)
else if ( cmd == "CDUP" ) {
if ( directory.cdUp() )
send( "250 Requested file action okay, completed" );
else
send( "550 Requested action not taken" );
}
// structure mount (SMNT)
else if ( cmd == "SMNT" ) {
// even wu-ftp does not support it
send( "502 Command not implemented" );
}
// reinitialize (REIN)
else if ( cmd == "REIN" ) {
// even wu-ftp does not support it
send( "502 Command not implemented" );
}
// TRANSFER PARAMETER COMMANDS
// data port (PORT)
else if ( cmd == "PORT" ) {
if ( parsePort( arg ) )
send( "200 Command okay" );
else
send( "500 Syntax error, command unrecognized" );
}
// passive (PASV)
else if ( cmd == "PASV" ) {
passiv = TRUE;
send( "227 Entering Passive Mode ("
+ address().toString().replace( QRegExp( "\\." ), "," ) + ","
+ QString::number( ( serversocket->port() ) >> 8 ) + ","
+ QString::number( ( serversocket->port() ) & 0xFF ) +")" );
}
// representation type (TYPE)
else if ( cmd == "TYPE" ) {
if ( arg.upper() == "A" || arg.upper() == "I" )
send( "200 Command okay" );
else
send( "504 Command not implemented for that parameter" );
}
// file structure (STRU)
else if ( cmd == "STRU" ) {
if ( arg.upper() == "F" )
send( "200 Command okay" );
else
send( "504 Command not implemented for that parameter" );
}
// transfer mode (MODE)
else if ( cmd == "MODE" ) {
if ( arg.upper() == "S" )
send( "200 Command okay" );
else
send( "504 Command not implemented for that parameter" );
}
// FTP SERVICE COMMANDS
// retrieve (RETR)
else if ( cmd == "RETR" )
if ( !args.isEmpty() && checkReadFile( absFilePath( args ) )
|| backupRestoreGzip( absFilePath( args ) ) ) {
send( "150 File status okay" );
sendFile( absFilePath( args ) );
}
else {
qDebug("550 Requested action not taken");
send( "550 Requested action not taken" );
}
// store (STOR)
else if ( cmd == "STOR" )
if ( !args.isEmpty() && checkWriteFile( absFilePath( args ) ) ) {
send( "150 File status okay" );
retrieveFile( absFilePath( args ) );
}
else
send( "550 Requested action not taken" );
// store unique (STOU)
else if ( cmd == "STOU" ) {
send( "502 Command not implemented" );
}
// append (APPE)
else if ( cmd == "APPE" ) {
send( "502 Command not implemented" );
}
// allocate (ALLO)
else if ( cmd == "ALLO" ) {
send( "200 Command okay" );
}
// restart (REST)
else if ( cmd == "REST" ) {
send( "502 Command not implemented" );
}
// rename from (RNFR)
else if ( cmd == "RNFR" ) {
renameFrom = QString::null;
if ( args.isEmpty() )
send( "500 Syntax error, command unrecognized" );
else {
QFile file( absFilePath( args ) );
if ( file.exists() ) {
send( "350 File exists, ready for destination name" );
renameFrom = absFilePath( args );
}
else
send( "550 Requested action not taken" );
}
}
// rename to (RNTO)
else if ( cmd == "RNTO" ) {
if ( lastCommand != "RNFR" )
send( "503 Bad sequence of commands" );
else if ( args.isEmpty() )
send( "500 Syntax error, command unrecognized" );
else {
QDir dir( absFilePath( args ) );
if ( dir.rename( renameFrom, absFilePath( args ), TRUE ) )
send( "250 Requested file action okay, completed." );
else
send( "550 Requested action not taken" );
}
}
// abort (ABOR)
else if ( cmd.contains( "ABOR" ) ) {
dtp->close();
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() ) {
send( "250 Requested file action okay, completed" );
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" );
}
}
// make directory (MKD)
else if ( cmd == "MKD" ) {
if ( args.isEmpty() ) {
qDebug(" Error: no arg");
send( "500 Syntax error, command unrecognized" );
}
else {
QDir dir;
if ( dir.mkdir( absFilePath( args ), TRUE ) )
send( "250 Requested file action okay, completed." );
else
send( "550 Requested action not taken" );