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