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