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