summaryrefslogtreecommitdiff
authorzecke <zecke>2003-08-28 14:50:19 (UTC)
committer zecke <zecke>2003-08-28 14:50:19 (UTC)
commit225fa91f6d57f211d53ec686518ca15fc5278d21 (patch) (unidiff)
tree3b7f3bc38693383b45027115efac1176ed5cd1b0
parent704de5567caccd769c693676a55a4af45c85e044 (diff)
downloadopie-225fa91f6d57f211d53ec686518ca15fc5278d21.zip
opie-225fa91f6d57f211d53ec686518ca15fc5278d21.tar.gz
opie-225fa91f6d57f211d53ec686518ca15fc5278d21.tar.bz2
Use old Opie changes
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--core/launcher/transferserver.cpp595
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
@@ -14,32 +14,31 @@
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20#define _XOPEN_SOURCE 20//#define _XOPEN_SOURCE
21
22#include <qtopia/global.h>
23#include <qtopia/qpeapplication.h>
24
25#ifndef Q_OS_WIN32
21#include <pwd.h> 26#include <pwd.h>
22#include <sys/types.h> 27#include <sys/types.h>
23#include <unistd.h> 28#include <unistd.h>
24#include <stdlib.h> 29#include <stdlib.h>
25#include <time.h> 30#include <time.h>
26#include <shadow.h> 31#include <shadow.h>
32#include <crypt.h>
27 33
28/* we need the _OS_LINUX stuff first ! */ 34#else
29#include <qglobal.h> 35#include <stdlib.h>
30 36#include <time.h>
31#ifndef _OS_LINUX_ 37#endif
32
33extern "C"
34{
35#include <uuid/uuid.h>
36#define UUID_H_INCLUDED
37}
38 38
39#endif // not defined linux
40 39
41#if defined(_OS_LINUX_) 40#if defined(_OS_LINUX_)
42#include <shadow.h> 41#include <shadow.h>
43#endif 42#endif
44 43
45#include <qdir.h> 44#include <qdir.h>
@@ -47,87 +46,75 @@ extern "C"
47#include <qtextstream.h> 46#include <qtextstream.h>
48#include <qdatastream.h> 47#include <qdatastream.h>
49#include <qmessagebox.h> 48#include <qmessagebox.h>
50#include <qstringlist.h> 49#include <qstringlist.h>
51#include <qfileinfo.h> 50#include <qfileinfo.h>
52#include <qregexp.h> 51#include <qregexp.h>
53//#include <qpe/qcopchannel_qws.h> 52//#include <qtopia/qcopchannel_qws.h>
54#include <qpe/process.h> 53#include <qtopia/process.h>
55#include <qpe/global.h> 54#include <qtopia/global.h>
56#include <qpe/config.h> 55#include <qtopia/config.h>
57#include <qpe/contact.h> 56#include <qtopia/private/contact.h>
58#include <qpe/quuid.h> 57#include <qtopia/quuid.h>
59#include <qpe/version.h> 58#include <qtopia/version.h>
60#include <qpe/qcopenvelope_qws.h> 59#ifdef Q_WS_QWS
60#include <qtopia/qcopenvelope_qws.h>
61#endif
62
63#include "launcherglobal.h"
61 64
62#include "transferserver.h" 65#include "transferserver.h"
63#include <opie/oprocess.h> 66#include <qtopia/qprocess.h>
64 67
65const int block_size = 51200; 68const int block_size = 51200;
66 69
67TransferServer::TransferServer( Q_UINT16 port, QObject *parent , 70TransferServer::TransferServer( Q_UINT16 port, QObject *parent ,
68 const char* name ) 71 const char* name )
69 : QServerSocket( port, 1, parent, name ) 72 : QServerSocket( port, 1, parent, name )
70{ 73{
74 connections.setAutoDelete( TRUE );
71 if ( !ok() ) 75 if ( !ok() )
72 qWarning( "Failed to bind to port %d", port ); 76 qWarning( "Failed to bind to port %d", port );
73} 77}
74 78
75TransferServer::~TransferServer() 79void TransferServer::authorizeConnections()
76{ 80{
81 QListIterator<ServerPI> it(connections);
82 while ( it.current() ) {
83 if ( !it.current()->verifyAuthorised() ) {
84 disconnect( it.current(), SIGNAL(connectionClosed(ServerPI *)), this, SLOT( closed(ServerPI *)) );
85 connections.removeRef( it.current() );
86 } else
87 ++it;
77} 88}
78
79void TransferServer::newConnection( int socket )
80{
81 (void) new ServerPI( socket, this );
82} 89}
83 90
84/* 91void TransferServer::closed(ServerPI *item)
85 * small class in anonymous namespace
86 * to generate a QUUid for us
87 */
88namespace
89{
90struct UidGen
91{ 92{
92 QString uuid(); 93 connections.removeRef(item);
93}; 94}
94#if !defined(_OS_LINUX_)
95 95
96QString UidGen::uuid() 96TransferServer::~TransferServer()
97{ 97{
98 uuid_t uuid;
99 uuid_generate( uuid );
100 return QUUid( uuid ).toString();
101} 98}
102#else
103/*
104* linux got a /proc/sys/kernel/random/uuid file
105* it'll generate the uuids for us
106*/
107QString UidGen::uuid()
108{
109 QFile file( "/proc/sys/kernel/random/uuid" );
110 if (!file.open(IO_ReadOnly ) )
111 return QString::null;
112
113 QTextStream stream(&file);
114 99
115 return "{" + stream.read().stripWhiteSpace() + "}"; 100void TransferServer::newConnection( int socket )
116} 101{
117#endif 102 ServerPI *ptr = new ServerPI( socket, this );
103 connect( ptr, SIGNAL(connectionClosed(ServerPI *)), this, SLOT( closed(ServerPI *)) );
104 connections.append( ptr );
118} 105}
119 106
120QString SyncAuthentication::serverId() 107QString SyncAuthentication::serverId()
121{ 108{
122 Config cfg("Security"); 109 Config cfg("Security");
123 cfg.setGroup("Sync"); 110 cfg.setGroup("Sync");
124 QString r = cfg.readEntry("serverid"); 111 QString r = cfg.readEntry("serverid");
112
125 if ( r.isEmpty() ) { 113 if ( r.isEmpty() ) {
126 UidGen gen; 114 r = Opie::Global::uuid();
127 r = gen.uuid();
128 cfg.writeEntry("serverid", r ); 115 cfg.writeEntry("serverid", r );
129 } 116 }
130 return r; 117 return r;
131} 118}
132 119
133QString SyncAuthentication::ownerName() 120QString SyncAuthentication::ownerName()
@@ -137,20 +124,25 @@ QString SyncAuthentication::ownerName()
137 if (QFile::exists(vfilename)) { 124 if (QFile::exists(vfilename)) {
138 Contact c; 125 Contact c;
139 c = Contact::readVCard( vfilename )[0]; 126 c = Contact::readVCard( vfilename )[0];
140 return c.fullName(); 127 return c.fullName();
141 } 128 }
142 129
143 return ""; 130 return QString::null;
144} 131}
145 132
146QString SyncAuthentication::loginName() 133QString SyncAuthentication::loginName()
147{ 134{
148 struct passwd *pw; 135 struct passwd *pw = 0L;
136#ifndef Q_OS_WIN32
149 pw = getpwuid( geteuid() ); 137 pw = getpwuid( geteuid() );
150 return QString::fromLocal8Bit( pw->pw_name ); 138 return QString::fromLocal8Bit( pw->pw_name );
139#else
140 //### revise
141 return QString();
142#endif
151} 143}
152 144
153int SyncAuthentication::isAuthorized(QHostAddress peeraddress) 145int SyncAuthentication::isAuthorized(QHostAddress peeraddress)
154{ 146{
155 Config cfg("Security"); 147 Config cfg("Security");
156 cfg.setGroup("Sync"); 148 cfg.setGroup("Sync");
@@ -160,19 +152,19 @@ int SyncAuthentication::isAuthorized(QHostAddress peeraddress)
160 // QHostAddress allowed; 152 // QHostAddress allowed;
161 // allowed.setAddress(allowedstr); 153 // allowed.setAddress(allowedstr);
162 // uint auth_peer = allowed.ip4Addr(); 154 // uint auth_peer = allowed.ip4Addr();
163 uint auth_peer_bits = cfg.readNumEntry("auth_peer_bits", 24); 155 uint auth_peer_bits = cfg.readNumEntry("auth_peer_bits", 24);
164 uint mask = auth_peer_bits >= 32 // shifting by 32 is not defined 156 uint mask = auth_peer_bits >= 32 // shifting by 32 is not defined
165 ? 0xffffffff : (((1 << auth_peer_bits) - 1) << (32 - auth_peer_bits)); 157 ? 0xffffffff : (((1 << auth_peer_bits) - 1) << (32 - auth_peer_bits));
158
166 return (peeraddress.ip4Addr() & mask) == auth_peer; 159 return (peeraddress.ip4Addr() & mask) == auth_peer;
167} 160}
168 161
169bool SyncAuthentication::checkUser( const QString& user ) 162bool SyncAuthentication::checkUser( const QString& user )
170{ 163{
171 if ( user.isEmpty() ) 164 if ( user.isEmpty() ) return FALSE;
172 return FALSE;
173 QString euser = loginName(); 165 QString euser = loginName();
174 return user == euser; 166 return user == euser;
175} 167}
176 168
177bool SyncAuthentication::checkPassword( const QString& password ) 169bool SyncAuthentication::checkPassword( const QString& password )
178{ 170{
@@ -199,88 +191,122 @@ bool SyncAuthentication::checkPassword( const QString& password )
199 static int denials = 0; 191 static int denials = 0;
200 int now = time(0); 192 int now = time(0);
201 193
202 // Detect old Qtopia Desktop (no password) 194 // Detect old Qtopia Desktop (no password)
203 if ( password.isEmpty() ) { 195 if ( password.isEmpty() ) {
204 if ( denials < 1 || now > lastdenial + 600 ) { 196 if ( denials < 1 || now > lastdenial + 600 ) {
205 QMessageBox::warning( 0, tr("Sync Connection"), 197 QMessageBox unauth(
198 tr("Sync Connection"),
206 tr("<p>An unauthorized system is requesting access to this device." 199 tr("<p>An unauthorized system is requesting access to this device."
207 "<p>If you are using a version of Qtopia Desktop older than 1.5.1, " 200 "<p>If you are using a version of Qtopia Desktop older than 1.5.1, "
208 "please upgrade."), 201 "please upgrade."),
209 tr("Deny") ); 202 QMessageBox::Warning,
203 QMessageBox::Cancel, QMessageBox::NoButton, QMessageBox::NoButton,
204 0, QString::null, TRUE, WStyle_StaysOnTop);
205 unauth.setButtonText(QMessageBox::Cancel, tr("Deny"));
206 unauth.exec();
207
210 denials++; 208 denials++;
211 lastdenial = now; 209 lastdenial = now;
212 } 210 }
213 return FALSE; 211 return FALSE;
214 } 212 }
215 213
216 // Second, check sync password... 214 // Second, check sync password...
217 QString pass = password.left(6); 215
218 /* old QtopiaDesktops are sending 216 static int lock=0;
219 * rootme newer versions got a Qtopia 217 if ( lock ) return FALSE;
220 * prefixed. Qtopia prefix will suceed 218
221 * until the sync software syncs up 219 ++lock;
222 * FIXME 220 if ( password.left(6) == "Qtopia" ) {
223 */ 221 Config cfg( QPEApplication::qpeDir()+"/etc/Security.conf", Config::File );
224 if ( pass == "rootme" || pass == "Qtopia") {
225
226 QString cpassword = QString::fromLocal8Bit( crypt( password.mid(8).local8Bit(), "qp" ) );
227 Config cfg("Security");
228 cfg.setGroup("Sync"); 222 cfg.setGroup("Sync");
229 QString pwds = cfg.readEntry("Passwords"); 223 QStringList pwds = cfg.readListEntry("Passwords",' ');
230 if ( QStringList::split(QChar(' '), pwds).contains(cpassword) ) 224 for (QStringList::ConstIterator it=pwds.begin(); it!=pwds.end(); ++it) {
225#ifndef Q_OS_WIN32
226 QString cpassword = QString::fromLocal8Bit(
227 crypt( password.mid(8).local8Bit(), (*it).left(2).latin1() ) );
228#else
229 // ### revise
230 QString cpassword("");
231#endif
232 if ( *it == cpassword ) {
233 lock--;
231 return TRUE; 234 return TRUE;
235 }
236 }
232 237
233 // Unrecognized system. Be careful... 238 // Unrecognized system. Be careful...
234 239 QMessageBox unrecbox(
235 if ( (denials > 2 && now < lastdenial + 600) 240 tr("Sync Connection"),
236 || QMessageBox::warning(0, tr("Sync Connection"),
237 tr("<p>An unrecognized system is requesting access to this device." 241 tr("<p>An unrecognized system is requesting access to this device."
238 "<p>If you have just initiated a Sync for the first time, this is normal."), 242 "<p>If you have just initiated a Sync for the first time, this is normal."),
239 tr("Allow"), tr("Deny"), 0, 1, 1 ) == 1 ) { 243 QMessageBox::Warning,
244 QMessageBox::Cancel, QMessageBox::Yes, QMessageBox::NoButton,
245 0, QString::null, TRUE, WStyle_StaysOnTop);
246 unrecbox.setButtonText(QMessageBox::Cancel, tr("Deny"));
247 unrecbox.setButtonText(QMessageBox::Yes, tr("Allow"));
248
249 if ( (denials > 2 && now < lastdenial+600)
250 || unrecbox.exec() != QMessageBox::Yes)
251 {
240 denials++; 252 denials++;
241 lastdenial = now; 253 lastdenial = now;
254 lock--;
242 return FALSE; 255 return FALSE;
243 } 256 } else {
244 else { 257 const char salty[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/.";
258 char salt[2];
259 salt[0]= salty[rand() % (sizeof(salty)-1)];
260 salt[1]= salty[rand() % (sizeof(salty)-1)];
261#ifndef Q_OS_WIN32
262 QString cpassword = QString::fromLocal8Bit(
263 crypt( password.mid(8).local8Bit(), salt ) );
264#else
265 //### revise
266 QString cpassword("");
267#endif
245 denials = 0; 268 denials = 0;
246 cfg.writeEntry("Passwords", pwds + " " + cpassword); 269 pwds.prepend(cpassword);
270 cfg.writeEntry("Passwords",pwds,' ');
271 lock--;
247 return TRUE; 272 return TRUE;
248 } 273 }
249 } 274 }
275 lock--;
250 276
251 return FALSE; 277 return FALSE;
252} 278}
253 279
280
254ServerPI::ServerPI( int socket, QObject *parent , const char* name ) 281ServerPI::ServerPI( int socket, QObject *parent , const char* name )
255 : QSocket( parent, name ) , dtp( 0 ), serversocket( 0 ), waitsocket( 0 ) 282 : QSocket( parent, name ) , dtp( 0 ), serversocket( 0 ), waitsocket( 0 ),
283 storFileSize(-1)
256{ 284{
257 state = Connected; 285 state = Connected;
258 286
259 setSocket( socket ); 287 setSocket( socket );
260 288
261 peerport = peerPort(); 289 peerport = peerPort();
262 peeraddress = peerAddress(); 290 peeraddress = peerAddress();
263 291
264#ifndef INSECURE 292#ifndef INSECURE
265
266 if ( !SyncAuthentication::isAuthorized(peeraddress) ) { 293 if ( !SyncAuthentication::isAuthorized(peeraddress) ) {
267 state = Forbidden; 294 state = Forbidden;
268 startTimer( 0 ); 295 startTimer( 0 );
269 } 296 } else
270 else
271#endif 297#endif
272 { 298 {
273 connect( this, SIGNAL( readyRead() ), SLOT( read() ) ); 299 connect( this, SIGNAL( readyRead() ), SLOT( read() ) );
274 connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); 300 connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) );
275 301
276 passiv = FALSE; 302 passiv = FALSE;
277 for ( int i = 0; i < 4; i++ ) 303 for ( int i = 0; i < 4; i++ )
278 wait[i] = FALSE; 304 wait[i] = FALSE;
279 305
280 send( "220 Qtopia " QPE_VERSION " FTP Server" ); 306 send( "220 Qtopia " QPE_VERSION " FTP Server" ); // No tr
281 state = Wait_USER; 307 state = Wait_USER;
282 308
283 dtp = new ServerDTP( this ); 309 dtp = new ServerDTP( this );
284 connect( dtp, SIGNAL( completed() ), SLOT( dtpCompleted() ) ); 310 connect( dtp, SIGNAL( completed() ), SLOT( dtpCompleted() ) );
285 connect( dtp, SIGNAL( failed() ), SLOT( dtpFailed() ) ); 311 connect( dtp, SIGNAL( failed() ), SLOT( dtpFailed() ) );
286 connect( dtp, SIGNAL( error( int ) ), SLOT( dtpError( int ) ) ); 312 connect( dtp, SIGNAL( error( int ) ), SLOT( dtpError( int ) ) );
@@ -298,18 +324,31 @@ ServerPI::ServerPI( int socket, QObject *parent , const char* name )
298 SLOT( newConnection( int ) ) ); 324 SLOT( newConnection( int ) ) );
299 } 325 }
300} 326}
301 327
302ServerPI::~ServerPI() 328ServerPI::~ServerPI()
303{ 329{
330 close();
331 dtp->close();
332 delete dtp;
333 delete serversocket;
334}
335
336bool ServerPI::verifyAuthorised()
337{
338 if ( !SyncAuthentication::isAuthorized(peerAddress()) ) {
339 state = Forbidden;
340 return FALSE;
341 }
342 return TRUE;
304} 343}
305 344
306void ServerPI::connectionClosed() 345void ServerPI::connectionClosed()
307{ 346{
308 // qDebug( "Debug: Connection closed" ); 347 // qDebug( "Debug: Connection closed" );
309 delete this; 348 emit connectionClosed(this);
310} 349}
311 350
312void ServerPI::send( const QString& msg ) 351void ServerPI::send( const QString& msg )
313{ 352{
314 QTextStream os( this ); 353 QTextStream os( this );
315 os << msg << endl; 354 os << msg << endl;
@@ -355,14 +394,13 @@ bool ServerPI::checkWriteFile( const QString& file )
355void ServerPI::process( const QString& message ) 394void ServerPI::process( const QString& message )
356{ 395{
357 //qDebug( "Command: %s", message.latin1() ); 396 //qDebug( "Command: %s", message.latin1() );
358 397
359 // split message using "," as separator 398 // split message using "," as separator
360 QStringList msg = QStringList::split( " ", message ); 399 QStringList msg = QStringList::split( " ", message );
361 if ( msg.isEmpty() ) 400 if ( msg.isEmpty() ) return;
362 return ;
363 401
364 // command token 402 // command token
365 QString cmd = msg[0].upper(); 403 QString cmd = msg[0].upper();
366 404
367 // argument token 405 // argument token
368 QString arg; 406 QString arg;
@@ -380,282 +418,285 @@ void ServerPI::process( const QString& message )
380 } 418 }
381 419
382 //qDebug( "args: %s", args.latin1() ); 420 //qDebug( "args: %s", args.latin1() );
383 421
384 // we always respond to QUIT, regardless of state 422 // we always respond to QUIT, regardless of state
385 if ( cmd == "QUIT" ) { 423 if ( cmd == "QUIT" ) {
386 send( "211 Good bye!" ); 424 send( "211 Good bye!" ); // No tr
387 delete this; 425 close();
388 return ; 426 return ;
389 } 427 }
390 428
391 // connected to client 429 // connected to client
392 if ( Connected == state ) 430 if ( Connected == state )
393 return ; 431 return ;
394 432
395 // waiting for user name 433 // waiting for user name
396 if ( Wait_USER == state ) { 434 if ( Wait_USER == state ) {
397 435
398 if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) { 436 if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) {
399 send( "530 Please login with USER and PASS" ); 437 send( "530 Please login with USER and PASS" ); // No tr
400 return ; 438 return ;
401 } 439 }
402 send( "331 User name ok, need password" ); 440 send( "331 User name ok, need password" ); // No tr
403 state = Wait_PASS; 441 state = Wait_PASS;
404 return ; 442 return ;
405 } 443 }
406 444
407 // waiting for password 445 // waiting for password
408 if ( Wait_PASS == state ) { 446 if ( Wait_PASS == state ) {
409 447
410 if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) { 448 if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) {
411 send( "530 Please login with USER and PASS" ); 449 send( "530 Please login with USER and PASS" ); // No tr
412 return ; 450 return ;
413 } 451 }
414 send( "230 User logged in, proceed" ); 452 send( "230 User logged in, proceed" ); // No tr
415 state = Ready; 453 state = Ready;
416 return ; 454 return ;
417 } 455 }
418 456
419 // ACCESS CONTROL COMMANDS 457 // ACCESS CONTROL COMMANDS
420 458
459 // Only an ALLO sent immediately before STOR is valid.
460 if ( cmd != "STOR" )
461 storFileSize = -1;
421 462
422 // account (ACCT) 463 // account (ACCT)
423 if ( cmd == "ACCT" ) { 464 if ( cmd == "ACCT" ) {
424 // even wu-ftp does not support it 465 // even wu-ftp does not support it
425 send( "502 Command not implemented" ); 466 send( "502 Command not implemented" ); // No tr
426 } 467 }
427 468
428 // change working directory (CWD) 469 // change working directory (CWD)
429 else if ( cmd == "CWD" ) { 470 else if ( cmd == "CWD" ) {
430 471
431 if ( !args.isEmpty() ) { 472 if ( !args.isEmpty() ) {
432 if ( directory.cd( args, TRUE ) ) 473 if ( directory.cd( args, TRUE ) )
433 send( "250 Requested file action okay, completed" ); 474 send( "250 Requested file action okay, completed" ); // No tr
434 else 475 else
435 send( "550 Requested action not taken" ); 476 send( "550 Requested action not taken" ); // No tr
436 } 477 }
437 else 478 else
438 send( "500 Syntax error, command unrecognized" ); 479 send( "500 Syntax error, command unrecognized" ); // No tr
439 } 480 }
440 481
441 // change to parent directory (CDUP) 482 // change to parent directory (CDUP)
442 else if ( cmd == "CDUP" ) { 483 else if ( cmd == "CDUP" ) {
443 if ( directory.cdUp() ) 484 if ( directory.cdUp() )
444 send( "250 Requested file action okay, completed" ); 485 send( "250 Requested file action okay, completed" ); // No tr
445 else 486 else
446 send( "550 Requested action not taken" ); 487 send( "550 Requested action not taken" ); // No tr
447 } 488 }
448 489
449 // structure mount (SMNT) 490 // structure mount (SMNT)
450 else if ( cmd == "SMNT" ) { 491 else if ( cmd == "SMNT" ) {
451 // even wu-ftp does not support it 492 // even wu-ftp does not support it
452 send( "502 Command not implemented" ); 493 send( "502 Command not implemented" ); // No tr
453 } 494 }
454 495
455 // reinitialize (REIN) 496 // reinitialize (REIN)
456 else if ( cmd == "REIN" ) { 497 else if ( cmd == "REIN" ) {
457 // even wu-ftp does not support it 498 // even wu-ftp does not support it
458 send( "502 Command not implemented" ); 499 send( "502 Command not implemented" ); // No tr
459 } 500 }
460 501
461 502
462 // TRANSFER PARAMETER COMMANDS 503 // TRANSFER PARAMETER COMMANDS
463 504
464 505
465 // data port (PORT) 506 // data port (PORT)
466 else if ( cmd == "PORT" ) { 507 else if ( cmd == "PORT" ) {
467 if ( parsePort( arg ) ) 508 if ( parsePort( arg ) )
468 send( "200 Command okay" ); 509 send( "200 Command okay" ); // No tr
469 else 510 else
470 send( "500 Syntax error, command unrecognized" ); 511 send( "500 Syntax error, command unrecognized" ); // No tr
471 } 512 }
472 513
473 // passive (PASV) 514 // passive (PASV)
474 else if ( cmd == "PASV" ) { 515 else if ( cmd == "PASV" ) {
475 passiv = TRUE; 516 passiv = TRUE;
476 send( "227 Entering Passive Mode (" 517 send( "227 Entering Passive Mode (" // No tr
477 + address().toString().replace( QRegExp( "\\." ), "," ) + "," 518 + address().toString().replace( QRegExp( "\\." ), "," ) + ","
478 + QString::number( ( serversocket->port() ) >> 8 ) + "," 519 + QString::number( ( serversocket->port() ) >> 8 ) + ","
479 + QString::number( ( serversocket->port() ) & 0xFF ) + ")" ); 520 + QString::number( ( serversocket->port() ) & 0xFF ) + ")" );
480 } 521 }
481 522
482 // representation type (TYPE) 523 // representation type (TYPE)
483 else if ( cmd == "TYPE" ) { 524 else if ( cmd == "TYPE" ) {
484 if ( arg.upper() == "A" || arg.upper() == "I" ) 525 if ( arg.upper() == "A" || arg.upper() == "I" )
485 send( "200 Command okay" ); 526 send( "200 Command okay" ); // No tr
486 else 527 else
487 send( "504 Command not implemented for that parameter" ); 528 send( "504 Command not implemented for that parameter" ); // No tr
488 } 529 }
489 530
490 // file structure (STRU) 531 // file structure (STRU)
491 else if ( cmd == "STRU" ) { 532 else if ( cmd == "STRU" ) {
492 if ( arg.upper() == "F" ) 533 if ( arg.upper() == "F" )
493 send( "200 Command okay" ); 534 send( "200 Command okay" ); // No tr
494 else 535 else
495 send( "504 Command not implemented for that parameter" ); 536 send( "504 Command not implemented for that parameter" ); // No tr
496 } 537 }
497 538
498 // transfer mode (MODE) 539 // transfer mode (MODE)
499 else if ( cmd == "MODE" ) { 540 else if ( cmd == "MODE" ) {
500 if ( arg.upper() == "S" ) 541 if ( arg.upper() == "S" )
501 send( "200 Command okay" ); 542 send( "200 Command okay" ); // No tr
502 else 543 else
503 send( "504 Command not implemented for that parameter" ); 544 send( "504 Command not implemented for that parameter" ); // No tr
504 } 545 }
505 546
506 547
507 // FTP SERVICE COMMANDS 548 // FTP SERVICE COMMANDS
508 549
509 550
510 // retrieve (RETR) 551 // retrieve (RETR)
511 else if ( cmd == "RETR" ) 552 else if ( cmd == "RETR" )
512 if ( !args.isEmpty() && checkReadFile( absFilePath( args ) ) 553 if ( !args.isEmpty() && checkReadFile( absFilePath( args ) )
513 || backupRestoreGzip( absFilePath( args ) ) ) { 554 || backupRestoreGzip( absFilePath( args ) ) ) {
514 send( "150 File status okay" ); 555 send( "150 File status okay" ); // No tr
515 sendFile( absFilePath( args ) ); 556 sendFile( absFilePath( args ) );
516 } 557 }
517 else { 558 else {
518 qDebug("550 Requested action not taken"); 559 qDebug("550 Requested action not taken");
519 send( "550 Requested action not taken" ); 560 send( "550 Requested action not taken" ); // No tr
520 } 561 }
521 562
522 // store (STOR) 563 // store (STOR)
523 else if ( cmd == "STOR" ) 564 else if ( cmd == "STOR" )
524 if ( !args.isEmpty() && checkWriteFile( absFilePath( args ) ) ) { 565 if ( !args.isEmpty() && checkWriteFile( absFilePath( args ) ) ) {
525 send( "150 File status okay" ); 566 send( "150 File status okay" ); // No tr
526 retrieveFile( absFilePath( args ) ); 567 retrieveFile( absFilePath( args ) );
527 } 568 }
528 else 569 else
529 send( "550 Requested action not taken" ); 570 send( "550 Requested action not taken" ); // No tr
530 571
531 // store unique (STOU) 572 // store unique (STOU)
532 else if ( cmd == "STOU" ) { 573 else if ( cmd == "STOU" ) {
533 send( "502 Command not implemented" ); 574 send( "502 Command not implemented" ); // No tr
534 } 575 }
535 576
536 // append (APPE) 577 // append (APPE)
537 else if ( cmd == "APPE" ) { 578 else if ( cmd == "APPE" ) {
538 send( "502 Command not implemented" ); 579 send( "502 Command not implemented" ); // No tr
539 } 580 }
540 581
541 // allocate (ALLO) 582 // allocate (ALLO)
542 else if ( cmd == "ALLO" ) { 583 else if ( cmd == "ALLO" ) {
543 send( "200 Command okay" ); 584 storFileSize = args.toInt();
585 send( "200 Command okay" ); // No tr
544 } 586 }
545 587
546 // restart (REST) 588 // restart (REST)
547 else if ( cmd == "REST" ) { 589 else if ( cmd == "REST" ) {
548 send( "502 Command not implemented" ); 590 send( "502 Command not implemented" ); // No tr
549 } 591 }
550 592
551 // rename from (RNFR) 593 // rename from (RNFR)
552 else if ( cmd == "RNFR" ) { 594 else if ( cmd == "RNFR" ) {
553 renameFrom = QString::null; 595 renameFrom = QString::null;
554 if ( args.isEmpty() ) 596 if ( args.isEmpty() )
555 send( "500 Syntax error, command unrecognized" ); 597 send( "500 Syntax error, command unrecognized" ); // No tr
556 else { 598 else {
557 QFile file( absFilePath( args ) ); 599 QFile file( absFilePath( args ) );
558 if ( file.exists() ) { 600 if ( file.exists() ) {
559 send( "350 File exists, ready for destination name" ); 601 send( "350 File exists, ready for destination name" ); // No tr
560 renameFrom = absFilePath( args ); 602 renameFrom = absFilePath( args );
561 } 603 }
562 else 604 else
563 send( "550 Requested action not taken" ); 605 send( "550 Requested action not taken" ); // No tr
564 } 606 }
565 } 607 }
566 608
567 // rename to (RNTO) 609 // rename to (RNTO)
568 else if ( cmd == "RNTO" ) { 610 else if ( cmd == "RNTO" ) {
569 if ( lastCommand != "RNFR" ) 611 if ( lastCommand != "RNFR" )
570 send( "503 Bad sequence of commands" ); 612 send( "503 Bad sequence of commands" ); // No tr
571 else if ( args.isEmpty() ) 613 else if ( args.isEmpty() )
572 send( "500 Syntax error, command unrecognized" ); 614 send( "500 Syntax error, command unrecognized" ); // No tr
573 else { 615 else {
574 QDir dir( absFilePath( args ) ); 616 QDir dir( absFilePath( args ) );
575 if ( dir.rename( renameFrom, absFilePath( args ), TRUE ) ) 617 if ( dir.rename( renameFrom, absFilePath( args ), TRUE ) )
576 send( "250 Requested file action okay, completed." ); 618 send( "250 Requested file action okay, completed." ); // No tr
577 else 619 else
578 send( "550 Requested action not taken" ); 620 send( "550 Requested action not taken" ); // No tr
579 } 621 }
580 } 622 }
581 623
582 // abort (ABOR) 624 // abort (ABOR)
583 else if ( cmd.contains( "ABOR" ) ) { 625 else if ( cmd.contains( "ABOR" ) ) {
584 dtp->close(); 626 dtp->close();
585 if ( dtp->dtpMode() != ServerDTP::Idle ) 627 if ( dtp->dtpMode() != ServerDTP::Idle )
586 send( "426 Connection closed; transfer aborted" ); 628 send( "426 Connection closed; transfer aborted" ); // No tr
587 else 629 else
588 send( "226 Closing data connection" ); 630 send( "226 Closing data connection" ); // No tr
589 } 631 }
590 632
591 // delete (DELE) 633 // delete (DELE)
592 else if ( cmd == "DELE" ) { 634 else if ( cmd == "DELE" ) {
593 if ( args.isEmpty() ) 635 if ( args.isEmpty() )
594 send( "500 Syntax error, command unrecognized" ); 636 send( "500 Syntax error, command unrecognized" ); // No tr
595 else { 637 else {
596 QFile file( absFilePath( args ) ) ; 638 QFile file( absFilePath( args ) ) ;
597 if ( file.remove() ) { 639 if ( file.remove() ) {
598 send( "250 Requested file action okay, completed" ); 640 send( "250 Requested file action okay, completed" ); // No tr
599 QCopEnvelope e("QPE/System", "linkChanged(QString)" ); 641 QCopEnvelope e("QPE/System", "linkChanged(QString)" );
600 e << file.name(); 642 e << file.name();
601 } 643 } else {
602 else { 644 send( "550 Requested action not taken" ); // No tr
603 send( "550 Requested action not taken" );
604 } 645 }
605 } 646 }
606 } 647 }
607 648
608 // remove directory (RMD) 649 // remove directory (RMD)
609 else if ( cmd == "RMD" ) { 650 else if ( cmd == "RMD" ) {
610 if ( args.isEmpty() ) 651 if ( args.isEmpty() )
611 send( "500 Syntax error, command unrecognized" ); 652 send( "500 Syntax error, command unrecognized" ); // No tr
612 else { 653 else {
613 QDir dir; 654 QDir dir;
614 if ( dir.rmdir( absFilePath( args ), TRUE ) ) 655 if ( dir.rmdir( absFilePath( args ), TRUE ) )
615 send( "250 Requested file action okay, completed" ); 656 send( "250 Requested file action okay, completed" ); // No tr
616 else 657 else
617 send( "550 Requested action not taken" ); 658 send( "550 Requested action not taken" ); // No tr
618 } 659 }
619 } 660 }
620 661
621 // make directory (MKD) 662 // make directory (MKD)
622 else if ( cmd == "MKD" ) { 663 else if ( cmd == "MKD" ) {
623 if ( args.isEmpty() ) { 664 if ( args.isEmpty() ) {
624 qDebug(" Error: no arg"); 665 qDebug(" Error: no arg");
625 send( "500 Syntax error, command unrecognized" ); 666 send( "500 Syntax error, command unrecognized" ); // No tr
626 } 667 }
627 else { 668 else {
628 QDir dir; 669 QDir dir;
629 if ( dir.mkdir( absFilePath( args ), TRUE ) ) 670 if ( dir.mkdir( absFilePath( args ), TRUE ) )
630 send( "250 Requested file action okay, completed." ); 671 send( "250 Requested file action okay, completed." ); // No tr
631 else 672 else
632 send( "550 Requested action not taken" ); 673 send( "550 Requested action not taken" ); // No tr
633 } 674 }
634 } 675 }
635 676
636 // print working directory (PWD) 677 // print working directory (PWD)
637 else if ( cmd == "PWD" ) { 678 else if ( cmd == "PWD" ) {
638 send( "257 \"" + directory.path() + "\"" ); 679 send( "257 \"" + directory.path() + "\"" );
639 } 680 }
640 681
641 // list (LIST) 682 // list (LIST)
642 else if ( cmd == "LIST" ) { 683 else if ( cmd == "LIST" ) {
643 if ( sendList( absFilePath( args ) ) ) 684 if ( sendList( absFilePath( args ) ) )
644 send( "150 File status okay" ); 685 send( "150 File status okay" ); // No tr
645 else 686 else
646 send( "500 Syntax error, command unrecognized" ); 687 send( "500 Syntax error, command unrecognized" ); // No tr
647 } 688 }
648 689
649 // size (SIZE) 690 // size (SIZE)
650 else if ( cmd == "SIZE" ) { 691 else if ( cmd == "SIZE" ) {
651 QString filePath = absFilePath( args ); 692 QString filePath = absFilePath( args );
652 QFileInfo fi( filePath ); 693 QFileInfo fi( filePath );
653 bool gzipfile = backupRestoreGzip( filePath ); 694 bool gzipfile = backupRestoreGzip( filePath );
654 if ( !fi.exists() && !gzipfile ) 695 if ( !fi.exists() && !gzipfile )
655 send( "500 Syntax error, command unrecognized" ); 696 send( "500 Syntax error, command unrecognized" ); // No tr
656 else { 697 else {
657 if ( !gzipfile ) 698 if ( !gzipfile )
658 send( "213 " + QString::number( fi.size() ) ); 699 send( "213 " + QString::number( fi.size() ) );
659 else { 700 else {
660 Process duproc( QString("du") ); 701 Process duproc( QString("du") );
661 duproc.addArgument("-s"); 702 duproc.addArgument("-s");
@@ -664,66 +705,66 @@ void ServerPI::process( const QString& message )
664 qDebug("du process failed; just sending back 1K"); 705 qDebug("du process failed; just sending back 1K");
665 send( "213 1024"); 706 send( "213 1024");
666 } 707 }
667 else { 708 else {
668 QString size = out.left( out.find("\t") ); 709 QString size = out.left( out.find("\t") );
669 int guess = size.toInt() / 5; 710 int guess = size.toInt() / 5;
670 if ( filePath.contains("doc") ) 711 if ( filePath.contains("doc") ) // No tr
671 guess *= 1000; 712 guess *= 1000;
672 qDebug("sending back gzip guess of %d", guess); 713 qDebug("sending back gzip guess of %d", guess);
673 send( "213 " + QString::number(guess) ); 714 send( "213 " + QString::number(guess) );
674 } 715 }
675 } 716 }
676 } 717 }
677 } 718 }
678 // name list (NLST) 719 // name list (NLST)
679 else if ( cmd == "NLST" ) { 720 else if ( cmd == "NLST" ) {
680 send( "502 Command not implemented" ); 721 send( "502 Command not implemented" ); // No tr
681 } 722 }
682 723
683 // site parameters (SITE) 724 // site parameters (SITE)
684 else if ( cmd == "SITE" ) { 725 else if ( cmd == "SITE" ) {
685 send( "502 Command not implemented" ); 726 send( "502 Command not implemented" ); // No tr
686 } 727 }
687 728
688 // system (SYST) 729 // system (SYST)
689 else if ( cmd == "SYST" ) { 730 else if ( cmd == "SYST" ) {
690 send( "215 UNIX Type: L8" ); 731 send( "215 UNIX Type: L8" ); // No tr
691 } 732 }
692 733
693 // status (STAT) 734 // status (STAT)
694 else if ( cmd == "STAT" ) { 735 else if ( cmd == "STAT" ) {
695 send( "502 Command not implemented" ); 736 send( "502 Command not implemented" ); // No tr
696 } 737 }
697 738
698 // help (HELP ) 739 // help (HELP )
699 else if ( cmd == "HELP" ) { 740 else if ( cmd == "HELP" ) {
700 send( "502 Command not implemented" ); 741 send( "502 Command not implemented" ); // No tr
701 } 742 }
702 743
703 // noop (NOOP) 744 // noop (NOOP)
704 else if ( cmd == "NOOP" ) { 745 else if ( cmd == "NOOP" ) {
705 send( "200 Command okay" ); 746 send( "200 Command okay" ); // No tr
706 } 747 }
707 748
708 // not implemented 749 // not implemented
709 else 750 else
710 send( "502 Command not implemented" ); 751 send( "502 Command not implemented" ); // No tr
711 752
712 lastCommand = cmd; 753 lastCommand = cmd;
713} 754}
714 755
715bool ServerPI::backupRestoreGzip( const QString &file ) 756bool ServerPI::backupRestoreGzip( const QString &file )
716{ 757{
717 return (file.find( "backup" ) != -1 && 758 return (file.find( "backup" ) != -1 && // No tr
718 file.findRev( ".tgz" ) == (int)file.length() - 4 ); 759 file.findRev( ".tgz" ) == (int)file.length() - 4 );
719} 760}
720 761
721bool ServerPI::backupRestoreGzip( const QString &file, QStringList &targets ) 762bool ServerPI::backupRestoreGzip( const QString &file, QStringList &targets )
722{ 763{
723 if ( file.find( "backup" ) != -1 && 764 if ( file.find( "backup" ) != -1 && // No tr
724 file.findRev( ".tgz" ) == (int)file.length() - 4 ) { 765 file.findRev( ".tgz" ) == (int)file.length() - 4 ) {
725 QFileInfo info( file ); 766 QFileInfo info( file );
726 targets = info.dirPath( TRUE ); 767 targets = info.dirPath( TRUE );
727 qDebug("ServerPI::backupRestoreGzip for %s = %s", file.latin1(), 768 qDebug("ServerPI::backupRestoreGzip for %s = %s", file.latin1(),
728 targets.join(" ").latin1() ); 769 targets.join(" ").latin1() );
729 return true; 770 return true;
@@ -740,14 +781,13 @@ void ServerPI::sendFile( const QString& file )
740 newConnection( waitsocket ); 781 newConnection( waitsocket );
741 } 782 }
742 else { 783 else {
743 QStringList targets; 784 QStringList targets;
744 if ( backupRestoreGzip( file, targets ) ) 785 if ( backupRestoreGzip( file, targets ) )
745 dtp->sendGzipFile( file, targets, peeraddress, peerport ); 786 dtp->sendGzipFile( file, targets, peeraddress, peerport );
746 else 787 else dtp->sendFile( file, peeraddress, peerport );
747 dtp->sendFile( file, peeraddress, peerport );
748 } 788 }
749} 789}
750 790
751void ServerPI::retrieveFile( const QString& file ) 791void ServerPI::retrieveFile( const QString& file )
752{ 792{
753 if ( passiv ) { 793 if ( passiv ) {
@@ -758,55 +798,57 @@ void ServerPI::retrieveFile( const QString& file )
758 } 798 }
759 else { 799 else {
760 QStringList targets; 800 QStringList targets;
761 if ( backupRestoreGzip( file, targets ) ) 801 if ( backupRestoreGzip( file, targets ) )
762 dtp->retrieveGzipFile( file, peeraddress, peerport ); 802 dtp->retrieveGzipFile( file, peeraddress, peerport );
763 else 803 else
764 dtp->retrieveFile( file, peeraddress, peerport ); 804 dtp->retrieveFile( file, peeraddress, peerport, storFileSize );
765 } 805 }
766} 806}
767 807
768bool ServerPI::parsePort( const QString& pp ) 808bool ServerPI::parsePort( const QString& pp )
769{ 809{
770 QStringList p = QStringList::split( ",", pp ); 810 QStringList p = QStringList::split( ",", pp );
771 if ( p.count() != 6 ) 811 if ( p.count() != 6 ) return FALSE;
772 return FALSE;
773 812
774 // h1,h2,h3,h4,p1,p2 813 // h1,h2,h3,h4,p1,p2
775 peeraddress = QHostAddress( ( p[0].toInt() << 24 ) + ( p[1].toInt() << 16 ) + 814 peeraddress = QHostAddress( ( p[0].toInt() << 24 ) + ( p[1].toInt() << 16 ) +
776 ( p[2].toInt() << 8 ) + p[3].toInt() ); 815 ( p[2].toInt() << 8 ) + p[3].toInt() );
777 peerport = ( p[4].toInt() << 8 ) + p[5].toInt(); 816 peerport = ( p[4].toInt() << 8 ) + p[5].toInt();
778 return TRUE; 817 return TRUE;
779} 818}
780 819
781void ServerPI::dtpCompleted() 820void ServerPI::dtpCompleted()
782{ 821{
783 send( "226 Closing data connection, file transfer successful" ); 822 send( "226 Closing data connection, file transfer successful" ); // No tr
784 if ( dtp->dtpMode() == ServerDTP::RetrieveFile ) { 823 if ( dtp->dtpMode() == ServerDTP::RetrieveFile ) {
785 QString fn = dtp->fileName(); 824 QString fn = dtp->fileName();
786 if ( fn.right(8) == ".desktop" && fn.find("/Documents/") >= 0 ) { 825 if ( fn.right(8) == ".desktop" && fn.find("/Documents/") >= 0 ) {
787 QCopEnvelope e("QPE/System", "linkChanged(QString)" ); 826 QCopEnvelope e("QPE/System", "linkChanged(QString)" );
788 e << fn; 827 e << fn;
789 } 828 }
790 } 829 }
791 waitsocket = 0; 830 waitsocket = 0;
792 dtp->close(); 831 dtp->close();
832 storFileSize = -1;
793} 833}
794 834
795void ServerPI::dtpFailed() 835void ServerPI::dtpFailed()
796{ 836{
797 dtp->close(); 837 dtp->close();
798 waitsocket = 0; 838 waitsocket = 0;
799 send( "451 Requested action aborted: local error in processing" ); 839 send( "451 Requested action aborted: local error in processing" ); // No tr
840 storFileSize = -1;
800} 841}
801 842
802void ServerPI::dtpError( int ) 843void ServerPI::dtpError( int )
803{ 844{
804 dtp->close(); 845 dtp->close();
805 waitsocket = 0; 846 waitsocket = 0;
806 send( "451 Requested action aborted: local error in processing" ); 847 send( "451 Requested action aborted: local error in processing" ); // No tr
848 storFileSize = -1;
807} 849}
808 850
809bool ServerPI::sendList( const QString& arg ) 851bool ServerPI::sendList( const QString& arg )
810{ 852{
811 QByteArray listing; 853 QByteArray listing;
812 QBuffer buffer( listing ); 854 QBuffer buffer( listing );
@@ -818,14 +860,13 @@ bool ServerPI::sendList( const QString& arg )
818 QString fn = arg; 860 QString fn = arg;
819 861
820 if ( fn.isEmpty() ) 862 if ( fn.isEmpty() )
821 fn = directory.path(); 863 fn = directory.path();
822 864
823 QFileInfo fi( fn ); 865 QFileInfo fi( fn );
824 if ( !fi.exists() ) 866 if ( !fi.exists() ) return FALSE;
825 return FALSE;
826 867
827 // return file listing 868 // return file listing
828 if ( fi.isFile() ) { 869 if ( fi.isFile() ) {
829 ts << fileListing( &fi ) << endl; 870 ts << fileListing( &fi ) << endl;
830 } 871 }
831 872
@@ -841,13 +882,13 @@ bool ServerPI::sendList( const QString& arg )
841 while ( ( info = it.current() ) ) { 882 while ( ( info = it.current() ) ) {
842 if ( info->fileName() != "." && info->fileName() != ".." ) 883 if ( info->fileName() != "." && info->fileName() != ".." )
843 total += info->size(); 884 total += info->size();
844 ++it; 885 ++it;
845 } 886 }
846 887
847 ts << "total " << QString::number( total / 1024 ) << endl; 888 ts << "total " << QString::number( total / 1024 ) << endl; // No tr
848 889
849 it.toFirst(); 890 it.toFirst();
850 while ( ( info = it.current() ) ) { 891 while ( ( info = it.current() ) ) {
851 if ( info->fileName() == "." || info->fileName() == ".." ) { 892 if ( info->fileName() == "." || info->fileName() == ".." ) {
852 ++it; 893 ++it;
853 continue; 894 continue;
@@ -867,14 +908,13 @@ bool ServerPI::sendList( const QString& arg )
867 dtp->sendByteArray( buffer.buffer(), peeraddress, peerport ); 908 dtp->sendByteArray( buffer.buffer(), peeraddress, peerport );
868 return TRUE; 909 return TRUE;
869} 910}
870 911
871QString ServerPI::fileListing( QFileInfo *info ) 912QString ServerPI::fileListing( QFileInfo *info )
872{ 913{
873 if ( !info ) 914 if ( !info ) return QString::null;
874 return QString::null;
875 QString s; 915 QString s;
876 916
877 // type char 917 // type char
878 if ( info->isDir() ) 918 if ( info->isDir() )
879 s += "d"; 919 s += "d";
880 else if ( info->isSymLink() ) 920 else if ( info->isSymLink() )
@@ -894,16 +934,22 @@ QString ServerPI::fileListing( QFileInfo *info )
894 //if ( info->isDir() ) 934 //if ( info->isDir() )
895 //subdirs = QDir( info->absFilePath() ).entryList( QDir::Dirs ).count(); 935 //subdirs = QDir( info->absFilePath() ).entryList( QDir::Dirs ).count();
896 936
897 s += QString::number( subdirs ).rightJustify( 3, ' ', TRUE ) + " "; 937 s += QString::number( subdirs ).rightJustify( 3, ' ', TRUE ) + " ";
898 938
899 // owner 939 // owner
900 s += info->owner().leftJustify( 8, ' ', TRUE ) + " "; 940 QString o = info->owner();
941 if ( o.isEmpty() )
942 o = QString::number(info->ownerId());
943 s += o.leftJustify( 8, ' ', TRUE ) + " ";
901 944
902 // group 945 // group
903 s += info->group().leftJustify( 8, ' ', TRUE ) + " "; 946 QString g = info->group();
947 if ( g.isEmpty() )
948 g = QString::number(info->groupId());
949 s += g.leftJustify( 8, ' ', TRUE ) + " ";
904 950
905 // file size in bytes 951 // file size in bytes
906 s += QString::number( info->size() ).rightJustify( 9, ' ', TRUE ) + " "; 952 s += QString::number( info->size() ).rightJustify( 9, ' ', TRUE ) + " ";
907 953
908 // last modified date 954 // last modified date
909 QDate date = info->lastModified().date(); 955 QDate date = info->lastModified().date();
@@ -918,67 +964,47 @@ QString ServerPI::fileListing( QFileInfo *info )
918 964
919 return s; 965 return s;
920} 966}
921 967
922QString ServerPI::permissionString( QFileInfo *info ) 968QString ServerPI::permissionString( QFileInfo *info )
923{ 969{
924 if ( !info ) 970 if ( !info ) return QString( "---------" );
925 return QString( "---------" );
926 QString s; 971 QString s;
927 972
928 // user 973 // user
929 if ( info->permission( QFileInfo::ReadUser ) ) 974 if ( info->permission( QFileInfo::ReadUser ) ) s += "r";
930 s += "r"; 975 else s += "-";
931 else 976 if ( info->permission( QFileInfo::WriteUser ) ) s += "w";
932 s += "-"; 977 else s += "-";
933 if ( info->permission( QFileInfo::WriteUser ) ) 978 if ( info->permission( QFileInfo::ExeUser ) ) s += "x";
934 s += "w"; 979 else s += "-";
935 else
936 s += "-";
937 if ( info->permission( QFileInfo::ExeUser ) )
938 s += "x";
939 else
940 s += "-";
941 980
942 // group 981 // group
943 if ( info->permission( QFileInfo::ReadGroup ) ) 982 if ( info->permission( QFileInfo::ReadGroup ) ) s += "r";
944 s += "r"; 983 else s += "-";
945 else 984 if ( info->permission( QFileInfo::WriteGroup ) )s += "w";
946 s += "-"; 985 else s += "-";
947 if ( info->permission( QFileInfo::WriteGroup ) ) 986 if ( info->permission( QFileInfo::ExeGroup ) ) s += "x";
948 s += "w"; 987 else s += "-";
949 else
950 s += "-";
951 if ( info->permission( QFileInfo::ExeGroup ) )
952 s += "x";
953 else
954 s += "-";
955 988
956 // exec 989 // exec
957 if ( info->permission( QFileInfo::ReadOther ) ) 990 if ( info->permission( QFileInfo::ReadOther ) ) s += "r";
958 s += "r"; 991 else s += "-";
959 else 992 if ( info->permission( QFileInfo::WriteOther ) ) s += "w";
960 s += "-"; 993 else s += "-";
961 if ( info->permission( QFileInfo::WriteOther ) ) 994 if ( info->permission( QFileInfo::ExeOther ) ) s += "x";
962 s += "w"; 995 else s += "-";
963 else
964 s += "-";
965 if ( info->permission( QFileInfo::ExeOther ) )
966 s += "x";
967 else
968 s += "-";
969 996
970 return s; 997 return s;
971} 998}
972 999
973void ServerPI::newConnection( int socket ) 1000void ServerPI::newConnection( int socket )
974{ 1001{
975 //qDebug( "New incomming connection" ); 1002 //qDebug( "New incomming connection" );
976 1003
977 if ( !passiv ) 1004 if ( !passiv ) return;
978 return ;
979 1005
980 if ( wait[SendFile] ) { 1006 if ( wait[SendFile] ) {
981 QStringList targets; 1007 QStringList targets;
982 if ( backupRestoreGzip( waitfile, targets ) ) 1008 if ( backupRestoreGzip( waitfile, targets ) )
983 dtp->sendGzipFile( waitfile, targets ); 1009 dtp->sendGzipFile( waitfile, targets );
984 else 1010 else
@@ -987,13 +1013,13 @@ void ServerPI::newConnection( int socket )
987 } 1013 }
988 else if ( wait[RetrieveFile] ) { 1014 else if ( wait[RetrieveFile] ) {
989 qDebug("check retrieve file"); 1015 qDebug("check retrieve file");
990 if ( backupRestoreGzip( waitfile ) ) 1016 if ( backupRestoreGzip( waitfile ) )
991 dtp->retrieveGzipFile( waitfile ); 1017 dtp->retrieveGzipFile( waitfile );
992 else 1018 else
993 dtp->retrieveFile( waitfile ); 1019 dtp->retrieveFile( waitfile, storFileSize );
994 dtp->setSocket( socket ); 1020 dtp->setSocket( socket );
995 } 1021 }
996 else if ( wait[SendByteArray] ) { 1022 else if ( wait[SendByteArray] ) {
997 dtp->sendByteArray( waitarray ); 1023 dtp->sendByteArray( waitarray );
998 dtp->setSocket( socket ); 1024 dtp->setSocket( socket );
999 } 1025 }
@@ -1008,14 +1034,13 @@ void ServerPI::newConnection( int socket )
1008 for ( int i = 0; i < 4; i++ ) 1034 for ( int i = 0; i < 4; i++ )
1009 wait[i] = FALSE; 1035 wait[i] = FALSE;
1010} 1036}
1011 1037
1012QString ServerPI::absFilePath( const QString& file ) 1038QString ServerPI::absFilePath( const QString& file )
1013{ 1039{
1014 if ( file.isEmpty() ) 1040 if ( file.isEmpty() ) return file;
1015 return file;
1016 1041
1017 QString filepath( file ); 1042 QString filepath( file );
1018 if ( file[0] != "/" ) 1043 if ( file[0] != "/" )
1019 filepath = directory.path() + "/" + file; 1044 filepath = directory.path() + "/" + file;
1020 1045
1021 return filepath; 1046 return filepath;
@@ -1027,49 +1052,55 @@ void ServerPI::timerEvent( QTimerEvent * )
1027 connectionClosed(); 1052 connectionClosed();
1028} 1053}
1029 1054
1030 1055
1031ServerDTP::ServerDTP( QObject *parent, const char* name) 1056ServerDTP::ServerDTP( QObject *parent, const char* name)
1032 : QSocket( parent, name ), mode( Idle ), createTargzProc( 0 ), 1057 : QSocket( parent, name ), mode( Idle ), createTargzProc( 0 ),
1033 retrieveTargzProc( 0 ), gzipProc( 0 ) 1058 retrieveTargzProc( 0 )
1034{ 1059{
1035 1060
1036 connect( this, SIGNAL( connected() ), SLOT( connected() ) ); 1061 connect( this, SIGNAL( connected() ), SLOT( connected() ) );
1037 connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); 1062 connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) );
1038 connect( this, SIGNAL( bytesWritten( int ) ), SLOT( bytesWritten( int ) ) ); 1063 connect( this, SIGNAL( bytesWritten( int ) ), SLOT( bytesWritten( int ) ) );
1039 connect( this, SIGNAL( readyRead() ), SLOT( readyRead() ) ); 1064 connect( this, SIGNAL( readyRead() ), SLOT( readyRead() ) );
1040 1065
1041 gzipProc = new OProcess( this, "gzipProc" ); 1066 createTargzProc = new QProcess( QString("tar"), this, "createTargzProc"); // No tr
1042 1067 createTargzProc->setCommunication( QProcess::Stdout );
1043 createTargzProc = new OProcess( QString("tar"), this, "createTargzProc");
1044 createTargzProc->setWorkingDirectory( QDir::rootDirPath() ); 1068 createTargzProc->setWorkingDirectory( QDir::rootDirPath() );
1045 connect( createTargzProc, SIGNAL( processExited(OProcess *) ), SLOT( targzDone() ) ); 1069 connect( createTargzProc, SIGNAL( processExited() ), SLOT( targzDone() ) );
1046 1070
1047 QStringList args = "tar"; 1071 retrieveTargzProc = new QProcess( this, "retrieveTargzProc" );
1048 args += "-xv"; 1072 retrieveTargzProc->setCommunication( QProcess::Stdin );
1049 retrieveTargzProc = new OProcess( args, this, "retrieveTargzProc" );
1050 retrieveTargzProc->setWorkingDirectory( QDir::rootDirPath() ); 1073 retrieveTargzProc->setWorkingDirectory( QDir::rootDirPath() );
1051 connect( retrieveTargzProc, SIGNAL( processExited(OProcess *) ), 1074 connect( retrieveTargzProc, SIGNAL( processExited() ),
1052 SIGNAL( completed() ) ); 1075 SIGNAL( completed() ) );
1053 connect( retrieveTargzProc, SIGNAL( processExited(OProcess *) ), 1076 connect( retrieveTargzProc, SIGNAL( processExited() ),
1054 SLOT( extractTarDone() ) ); 1077 SLOT( extractTarDone() ) );
1055} 1078}
1056 1079
1057ServerDTP::~ServerDTP() 1080ServerDTP::~ServerDTP()
1058{ 1081{
1059 buf.close(); 1082 buf.close();
1083 if ( RetrieveFile == mode && file.isOpen() ) {
1084 // We're being shutdown before the client closed.
1085 file.close();
1086 if ( recvFileSize >= 0 && (int)file.size() != recvFileSize ) {
1087 qDebug( "STOR incomplete" );
1088 file.remove();
1089 }
1090 } else {
1060 file.close(); 1091 file.close();
1092 }
1061 createTargzProc->kill(); 1093 createTargzProc->kill();
1062} 1094}
1063 1095
1064void ServerDTP::extractTarDone() 1096void ServerDTP::extractTarDone()
1065{ 1097{
1066 qDebug("extract done"); 1098 qDebug("extract done");
1067#ifndef QT_NO_COP 1099#ifndef QT_NO_COP
1068 1100 QCopEnvelope e( "QPE/System", "restoreDone(QString)" );
1069 QCopEnvelope e( "QPE/Desktop", "restoreDone(QString)" );
1070 e << file.name(); 1101 e << file.name();
1071#endif 1102#endif
1072} 1103}
1073 1104
1074void ServerDTP::connected() 1105void ServerDTP::connected()
1075{ 1106{
@@ -1087,16 +1118,15 @@ void ServerDTP::connected()
1087 bytes_written = 0; 1118 bytes_written = 0;
1088 if ( file.size() == 0 ) { 1119 if ( file.size() == 0 ) {
1089 //make sure it doesn't hang on empty files 1120 //make sure it doesn't hang on empty files
1090 file.close(); 1121 file.close();
1091 emit completed(); 1122 emit completed();
1092 mode = Idle; 1123 mode = Idle;
1093 } 1124 } else {
1094 else { 1125 // Don't write more if there is plenty buffered already.
1095 1126 if ( bytesToWrite() <= block_size && !file.atEnd() ) {
1096 if ( !file.atEnd() ) {
1097 QCString s; 1127 QCString s;
1098 s.resize( block_size ); 1128 s.resize( block_size );
1099 int bytes = file.readBlock( s.data(), block_size ); 1129 int bytes = file.readBlock( s.data(), block_size );
1100 writeBlock( s.data(), bytes ); 1130 writeBlock( s.data(), bytes );
1101 } 1131 }
1102 } 1132 }
@@ -1107,16 +1137,15 @@ void ServerDTP::connected()
1107 qWarning("Previous tar --gzip process is still running; killing it..."); 1137 qWarning("Previous tar --gzip process is still running; killing it...");
1108 createTargzProc->kill(); 1138 createTargzProc->kill();
1109 } 1139 }
1110 1140
1111 bytes_written = 0; 1141 bytes_written = 0;
1112 qDebug("==>start send tar process"); 1142 qDebug("==>start send tar process");
1113 if ( !createTargzProc->start(OProcess::NotifyOnExit, OProcess::Stdout) ) 1143 if ( !createTargzProc->start() )
1114 qWarning("Error starting %s or %s", 1144 qWarning("Error starting %s",
1115 createTargzProc->args()[0].data(), 1145 createTargzProc->arguments().join(" ").latin1());
1116 gzipProc->args()[0].data());
1117 break; 1146 break;
1118 case SendBuffer: 1147 case SendBuffer:
1119 if ( !buf.open( IO_ReadOnly) ) { 1148 if ( !buf.open( IO_ReadOnly) ) {
1120 emit failed(); 1149 emit failed();
1121 mode = Idle; 1150 mode = Idle;
1122 return ; 1151 return ;
@@ -1181,19 +1210,25 @@ void ServerDTP::connectionClosed()
1181 emit failed(); 1210 emit failed();
1182 } 1211 }
1183 1212
1184 // retrieve file mode 1213 // retrieve file mode
1185 else if ( RetrieveFile == mode ) { 1214 else if ( RetrieveFile == mode ) {
1186 file.close(); 1215 file.close();
1216 if ( recvFileSize >= 0 && (int)file.size() != recvFileSize ) {
1217 qDebug( "STOR incomplete" );
1218 file.remove();
1219 emit failed();
1220 } else {
1187 emit completed(); 1221 emit completed();
1188 } 1222 }
1223 }
1189 1224
1190 else if ( RetrieveGzipFile == mode ) { 1225 else if ( RetrieveGzipFile == mode ) {
1191 qDebug("Done writing ungzip file; closing input"); 1226 qDebug("Done writing ungzip file; closing input");
1192 gzipProc->flushStdin(); 1227 retrieveTargzProc->flushStdin();
1193 gzipProc->closeStdin(); 1228 retrieveTargzProc->closeStdin();
1194 } 1229 }
1195 1230
1196 // retrieve buffer mode 1231 // retrieve buffer mode
1197 else if ( RetrieveBuffer == mode ) { 1232 else if ( RetrieveBuffer == mode ) {
1198 buf.close(); 1233 buf.close();
1199 emit completed(); 1234 emit completed();
@@ -1241,59 +1276,44 @@ void ServerDTP::readyRead()
1241 QCString s; 1276 QCString s;
1242 s.resize( bytesAvailable() ); 1277 s.resize( bytesAvailable() );
1243 readBlock( s.data(), bytesAvailable() ); 1278 readBlock( s.data(), bytesAvailable() );
1244 file.writeBlock( s.data(), s.size() ); 1279 file.writeBlock( s.data(), s.size() );
1245 } 1280 }
1246 else if ( RetrieveGzipFile == mode ) { 1281 else if ( RetrieveGzipFile == mode ) {
1247 if ( !gzipProc->isRunning() ) 1282 if ( !retrieveTargzProc->isRunning() )
1248 gzipProc->start(OProcess::NotifyOnExit, (OProcess::Communication) ( OProcess::Stdin | OProcess::Stdout )); 1283 retrieveTargzProc->start();
1249 1284
1250 QByteArray s; 1285 QByteArray s;
1251 s.resize( bytesAvailable() ); 1286 s.resize( bytesAvailable() );
1252 readBlock( s.data(), bytesAvailable() ); 1287 readBlock( s.data(), bytesAvailable() );
1253 gzipProc->writeStdin( s.data(), s.size() ); 1288 retrieveTargzProc->writeToStdin( s );
1254 qDebug("wrote %d bytes to ungzip ", s.size() ); 1289 qDebug("wrote %d bytes to ungzip ", s.size() );
1255 } 1290 }
1256 // retrieve buffer mode 1291 // retrieve buffer mode
1257 else if ( RetrieveBuffer == mode ) { 1292 else if ( RetrieveBuffer == mode ) {
1258 QCString s; 1293 QCString s;
1259 s.resize( bytesAvailable() ); 1294 s.resize( bytesAvailable() );
1260 readBlock( s.data(), bytesAvailable() ); 1295 readBlock( s.data(), bytesAvailable() );
1261 buf.writeBlock( s.data(), s.size() ); 1296 buf.writeBlock( s.data(), s.size() );
1262 } 1297 }
1263} 1298}
1264 1299
1265void ServerDTP::writeTargzBlock(OProcess *, char *buffer, int buflen) 1300void ServerDTP::writeTargzBlock()
1266{ 1301{
1267 writeBlock( buffer, buflen ); 1302 QByteArray block = createTargzProc->readStdout();
1268 qDebug("writeTargzBlock %d", buflen); 1303 writeBlock( block.data(), block.size() );
1269 if ( !createTargzProc->isRunning() ) { 1304 qDebug("writeTargzBlock %d", block.size());
1270 qDebug("tar and gzip done");
1271 emit completed();
1272 mode = Idle;
1273 disconnect( gzipProc, SIGNAL( receivedStdout(OProcess *, char *, int ) ),
1274 this, SLOT( writeTargzBlock(OProcess *, char *, int) ) );
1275 }
1276} 1305}
1277 1306
1278void ServerDTP::targzDone() 1307void ServerDTP::targzDone()
1279{ 1308{
1280 //qDebug("targz done"); 1309 qDebug("tar and gzip done");
1281 disconnect( createTargzProc, SIGNAL( receivedStdout(OProcess *, char *, int) ), 1310 emit completed();
1282 this, SLOT( gzipTarBlock(OProcess *, char *, int) ) ); 1311 mode = Idle;
1283 gzipProc->closeStdin(); 1312 disconnect( createTargzProc, SIGNAL( readyReadStdout() ),
1284} 1313 this, SLOT( writeTargzBlock() ) );
1285
1286void ServerDTP::gzipTarBlock(OProcess *, char *buffer, int buflen)
1287{
1288 //qDebug("gzipTarBlock");
1289 if ( !gzipProc->isRunning() ) {
1290 //qDebug("auto start gzip proc");
1291 gzipProc->start(OProcess::NotifyOnExit, (OProcess::Communication) ( OProcess::Stdin | OProcess::Stdout ));
1292 }
1293 gzipProc->writeStdin( buffer, buflen );
1294} 1314}
1295 1315
1296void ServerDTP::sendFile( const QString fn, const QHostAddress& host, Q_UINT16 port ) 1316void ServerDTP::sendFile( const QString fn, const QHostAddress& host, Q_UINT16 port )
1297{ 1317{
1298 file.setName( fn ); 1318 file.setName( fn );
1299 mode = SendFile; 1319 mode = SendFile;
@@ -1317,74 +1337,45 @@ void ServerDTP::sendGzipFile( const QString &fn,
1317void ServerDTP::sendGzipFile( const QString &fn, 1337void ServerDTP::sendGzipFile( const QString &fn,
1318 const QStringList &archiveTargets ) 1338 const QStringList &archiveTargets )
1319{ 1339{
1320 mode = SendGzipFile; 1340 mode = SendGzipFile;
1321 file.setName( fn ); 1341 file.setName( fn );
1322 1342
1323 QStringList args = "tar"; 1343 QStringList args = "targzip";
1324 args += "-cv"; 1344 //args += "-cv";
1325 args += archiveTargets; 1345 args += archiveTargets;
1326 qDebug("sendGzipFile %s", args.join(" ").latin1() ); 1346 qDebug("sendGzipFile %s", args.join(" ").latin1() );
1327 createTargzProc->clearArguments( ); 1347 createTargzProc->setArguments( args );
1328 *createTargzProc << args;
1329 connect( createTargzProc, 1348 connect( createTargzProc,
1330 SIGNAL( receivedStdout(OProcess *, char *, int) ), SLOT( gzipTarBlock(OProcess *, char *, int) ) ); 1349 SIGNAL( readyReadStdout() ), SLOT( writeTargzBlock() ) );
1331
1332 gzipProc->clearArguments( );
1333 *gzipProc << "gzip";
1334 connect( gzipProc, SIGNAL( receivedStdout(OProcess *, char *, int) ),
1335 SLOT( writeTargzBlock(OProcess *, char *, int) ) );
1336} 1350}
1337 1351
1338void ServerDTP::gunzipDone() 1352void ServerDTP::retrieveFile( const QString fn, const QHostAddress& host, Q_UINT16 port, int fileSize )
1339{
1340 qDebug("gunzipDone");
1341 disconnect( gzipProc, SIGNAL( processExited() ),
1342 this, SLOT( gunzipDone() ) );
1343 retrieveTargzProc->closeStdin();
1344 disconnect( gzipProc, SIGNAL( receivedStdout(OProcess *, char *, int) ),
1345 this, SLOT( tarExtractBlock(OProcess *, char *, int) ) );
1346}
1347
1348void ServerDTP::tarExtractBlock(OProcess *, char *buffer, int buflen)
1349{
1350 qDebug("tarExtractBlock");
1351 if ( !retrieveTargzProc->isRunning() ) {
1352 qDebug("auto start ungzip proc");
1353 if ( !retrieveTargzProc->start(OProcess::NotifyOnExit, OProcess::Stdin) )
1354 qWarning(" failed to start tar -x process");
1355 }
1356 retrieveTargzProc->writeStdin( buffer, buflen );
1357}
1358
1359
1360void ServerDTP::retrieveFile( const QString fn, const QHostAddress& host, Q_UINT16 port )
1361{ 1353{
1354 recvFileSize = fileSize;
1362 file.setName( fn ); 1355 file.setName( fn );
1363 mode = RetrieveFile; 1356 mode = RetrieveFile;
1364 connectToHost( host.toString(), port ); 1357 connectToHost( host.toString(), port );
1365} 1358}
1366 1359
1367void ServerDTP::retrieveFile( const QString fn ) 1360void ServerDTP::retrieveFile( const QString fn, int fileSize )
1368{ 1361{
1362 recvFileSize = fileSize;
1369 file.setName( fn ); 1363 file.setName( fn );
1370 mode = RetrieveFile; 1364 mode = RetrieveFile;
1371} 1365}
1372 1366
1373void ServerDTP::retrieveGzipFile( const QString &fn ) 1367void ServerDTP::retrieveGzipFile( const QString &fn )
1374{ 1368{
1375 qDebug("retrieveGzipFile %s", fn.latin1()); 1369 qDebug("retrieveGzipFile %s", fn.latin1());
1376 file.setName( fn ); 1370 file.setName( fn );
1377 mode = RetrieveGzipFile; 1371 mode = RetrieveGzipFile;
1378 1372
1379 gzipProc->clearArguments(); 1373 retrieveTargzProc->setArguments( "targunzip" );
1380 *gzipProc << "gunzip"; 1374 connect( retrieveTargzProc, SIGNAL( processExited() ),
1381 connect( gzipProc, SIGNAL( readyReadStdout() ), 1375 SLOT( extractTarDone() ) );
1382 SLOT( tarExtractBlock() ) );
1383 connect( gzipProc, SIGNAL( processExited() ),
1384 SLOT( gunzipDone() ) );
1385} 1376}
1386 1377
1387void ServerDTP::retrieveGzipFile( const QString &fn, const QHostAddress& host, Q_UINT16 port ) 1378void ServerDTP::retrieveGzipFile( const QString &fn, const QHostAddress& host, Q_UINT16 port )
1388{ 1379{
1389 retrieveGzipFile( fn ); 1380 retrieveGzipFile( fn );
1390 connectToHost( host.toString(), port ); 1381 connectToHost( host.toString(), port );