summaryrefslogtreecommitdiff
authorzecke <zecke>2002-10-04 21:32:08 (UTC)
committer zecke <zecke>2002-10-04 21:32:08 (UTC)
commitadc64501e1ce7789ce7915eebb6ebc0c318f5133 (patch) (unidiff)
tree30b769f7464facd4f5b6d7e270490992cc72b086
parent10048c2dd7f7cd26c582d64b5e255b00b47de304 (diff)
downloadopie-adc64501e1ce7789ce7915eebb6ebc0c318f5133.zip
opie-adc64501e1ce7789ce7915eebb6ebc0c318f5133.tar.gz
opie-adc64501e1ce7789ce7915eebb6ebc0c318f5133.tar.bz2
Next try
include <qglobal.h> to get _OS_LINUX from tt
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--core/launcher/transferserver.cpp3
1 files changed, 3 insertions, 0 deletions
diff --git a/core/launcher/transferserver.cpp b/core/launcher/transferserver.cpp
index f9204ab..d2f5501 100644
--- a/core/launcher/transferserver.cpp
+++ b/core/launcher/transferserver.cpp
@@ -1,795 +1,798 @@
1/********************************************************************** 1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. 2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3** 3**
4** This file is part of the Qtopia Environment. 4** This file is part of the Qtopia Environment.
5** 5**
6** This file may be distributed and/or modified under the terms of the 6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software 7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the 8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file. 9** packaging of this file.
10** 10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20#define _XOPEN_SOURCE 20#define _XOPEN_SOURCE
21#include <pwd.h> 21#include <pwd.h>
22#include <sys/types.h> 22#include <sys/types.h>
23#include <unistd.h> 23#include <unistd.h>
24#include <stdlib.h> 24#include <stdlib.h>
25#include <time.h> 25#include <time.h>
26#include <shadow.h> 26#include <shadow.h>
27 27
28/* we need the _OS_LINUX stuff first ! */
29#include <qglobal.h>
30
28#ifndef _OS_LINUX_ 31#ifndef _OS_LINUX_
29 32
30extern "C" { 33extern "C" {
31#include <uuid/uuid.h> 34#include <uuid/uuid.h>
32#define UUID_H_INCLUDED 35#define UUID_H_INCLUDED
33} 36}
34 37
35#endif // not defined linux 38#endif // not defined linux
36 39
37#if defined(_OS_LINUX_) 40#if defined(_OS_LINUX_)
38#include <shadow.h> 41#include <shadow.h>
39#endif 42#endif
40 43
41#include <qdir.h> 44#include <qdir.h>
42#include <qfile.h> 45#include <qfile.h>
43#include <qtextstream.h> 46#include <qtextstream.h>
44#include <qdatastream.h> 47#include <qdatastream.h>
45#include <qmessagebox.h> 48#include <qmessagebox.h>
46#include <qstringlist.h> 49#include <qstringlist.h>
47#include <qfileinfo.h> 50#include <qfileinfo.h>
48#include <qregexp.h> 51#include <qregexp.h>
49//#include <qpe/qcopchannel_qws.h> 52//#include <qpe/qcopchannel_qws.h>
50#include <qpe/process.h> 53#include <qpe/process.h>
51#include <qpe/global.h> 54#include <qpe/global.h>
52#include <qpe/config.h> 55#include <qpe/config.h>
53#include <qpe/contact.h> 56#include <qpe/contact.h>
54#include <qpe/quuid.h> 57#include <qpe/quuid.h>
55#include <qpe/version.h> 58#include <qpe/version.h>
56#ifdef QWS 59#ifdef QWS
57#include <qpe/qcopenvelope_qws.h> 60#include <qpe/qcopenvelope_qws.h>
58#endif 61#endif
59 62
60#include "transferserver.h" 63#include "transferserver.h"
61#include "qprocess.h" 64#include "qprocess.h"
62 65
63const int block_size = 51200; 66const int block_size = 51200;
64 67
65TransferServer::TransferServer( Q_UINT16 port, QObject *parent , 68TransferServer::TransferServer( Q_UINT16 port, QObject *parent ,
66 const char* name ) 69 const char* name )
67 : QServerSocket( port, 1, parent, name ) 70 : QServerSocket( port, 1, parent, name )
68{ 71{
69 if ( !ok() ) 72 if ( !ok() )
70 qWarning( "Failed to bind to port %d", port ); 73 qWarning( "Failed to bind to port %d", port );
71} 74}
72 75
73TransferServer::~TransferServer() 76TransferServer::~TransferServer()
74{ 77{
75 78
76} 79}
77 80
78void TransferServer::newConnection( int socket ) 81void TransferServer::newConnection( int socket )
79{ 82{
80 (void) new ServerPI( socket, this ); 83 (void) new ServerPI( socket, this );
81} 84}
82 85
83/* 86/*
84 * small class in anonymous namespace 87 * small class in anonymous namespace
85 * to generate a QUUid for us 88 * to generate a QUUid for us
86 */ 89 */
87namespace { 90namespace {
88 struct UidGen { 91 struct UidGen {
89 QString uuid(); 92 QString uuid();
90 }; 93 };
91#if !defined(_OS_LINUX_) 94#if !defined(_OS_LINUX_)
92 QString UidGen::uuid() { 95 QString UidGen::uuid() {
93 uuid_t uuid; 96 uuid_t uuid;
94 uuid_generate( uuid ); 97 uuid_generate( uuid );
95 return QUUid( uuid ).toString(); 98 return QUUid( uuid ).toString();
96 } 99 }
97#else 100#else
98 /* 101 /*
99 * linux got a /proc/sys/kernel/random/uuid file 102 * linux got a /proc/sys/kernel/random/uuid file
100 * it'll generate the uuids for us 103 * it'll generate the uuids for us
101 */ 104 */
102 QString UidGen::uuid() { 105 QString UidGen::uuid() {
103 QFile file( "/proc/sys/kernel/random/uuid" ); 106 QFile file( "/proc/sys/kernel/random/uuid" );
104 if (!file.open(IO_ReadOnly ) ) 107 if (!file.open(IO_ReadOnly ) )
105 return QString::null; 108 return QString::null;
106 109
107 QTextStream stream(&file); 110 QTextStream stream(&file);
108 111
109 return "{" + stream.read().stripWhiteSpace() + "}"; 112 return "{" + stream.read().stripWhiteSpace() + "}";
110 } 113 }
111#endif 114#endif
112} 115}
113 116
114QString SyncAuthentication::serverId() 117QString SyncAuthentication::serverId()
115{ 118{
116 Config cfg("Security"); 119 Config cfg("Security");
117 cfg.setGroup("Sync"); 120 cfg.setGroup("Sync");
118 QString r=cfg.readEntry("serverid"); 121 QString r=cfg.readEntry("serverid");
119 if ( r.isEmpty() ) { 122 if ( r.isEmpty() ) {
120 UidGen gen; 123 UidGen gen;
121 r = gen.uuid(); 124 r = gen.uuid();
122 cfg.writeEntry("serverid", r ); 125 cfg.writeEntry("serverid", r );
123 } 126 }
124 return r; 127 return r;
125} 128}
126 129
127QString SyncAuthentication::ownerName() 130QString SyncAuthentication::ownerName()
128{ 131{
129 QString vfilename = Global::applicationFileName("addressbook", 132 QString vfilename = Global::applicationFileName("addressbook",
130 "businesscard.vcf"); 133 "businesscard.vcf");
131 if (QFile::exists(vfilename)) { 134 if (QFile::exists(vfilename)) {
132 Contact c; 135 Contact c;
133 c = Contact::readVCard( vfilename )[0]; 136 c = Contact::readVCard( vfilename )[0];
134 return c.fullName(); 137 return c.fullName();
135 } 138 }
136 139
137 return ""; 140 return "";
138} 141}
139 142
140QString SyncAuthentication::loginName() 143QString SyncAuthentication::loginName()
141{ 144{
142 struct passwd *pw; 145 struct passwd *pw;
143 pw = getpwuid( geteuid() ); 146 pw = getpwuid( geteuid() );
144 return QString::fromLocal8Bit( pw->pw_name ); 147 return QString::fromLocal8Bit( pw->pw_name );
145} 148}
146 149
147int SyncAuthentication::isAuthorized(QHostAddress peeraddress) 150int SyncAuthentication::isAuthorized(QHostAddress peeraddress)
148{ 151{
149 Config cfg("Security"); 152 Config cfg("Security");
150 cfg.setGroup("Sync"); 153 cfg.setGroup("Sync");
151// QString allowedstr = cfg.readEntry("auth_peer","192.168.1.0"); 154// QString allowedstr = cfg.readEntry("auth_peer","192.168.1.0");
152 uint auth_peer = cfg.readNumEntry("auth_peer",0xc0a80100); 155 uint auth_peer = cfg.readNumEntry("auth_peer",0xc0a80100);
153 156
154// QHostAddress allowed; 157// QHostAddress allowed;
155// allowed.setAddress(allowedstr); 158// allowed.setAddress(allowedstr);
156// uint auth_peer = allowed.ip4Addr(); 159// uint auth_peer = allowed.ip4Addr();
157 uint auth_peer_bits = cfg.readNumEntry("auth_peer_bits",24); 160 uint auth_peer_bits = cfg.readNumEntry("auth_peer_bits",24);
158 uint mask = auth_peer_bits >= 32 // shifting by 32 is not defined 161 uint mask = auth_peer_bits >= 32 // shifting by 32 is not defined
159 ? 0xffffffff : (((1<<auth_peer_bits)-1)<<(32-auth_peer_bits)); 162 ? 0xffffffff : (((1<<auth_peer_bits)-1)<<(32-auth_peer_bits));
160 return (peeraddress.ip4Addr() & mask) == auth_peer; 163 return (peeraddress.ip4Addr() & mask) == auth_peer;
161} 164}
162 165
163bool SyncAuthentication::checkUser( const QString& user ) 166bool SyncAuthentication::checkUser( const QString& user )
164{ 167{
165 if ( user.isEmpty() ) return FALSE; 168 if ( user.isEmpty() ) return FALSE;
166 QString euser = loginName(); 169 QString euser = loginName();
167 return user == euser; 170 return user == euser;
168} 171}
169 172
170bool SyncAuthentication::checkPassword( const QString& password ) 173bool SyncAuthentication::checkPassword( const QString& password )
171{ 174{
172#ifdef ALLOW_UNIX_USER_FTP 175#ifdef ALLOW_UNIX_USER_FTP
173 // First, check system password... 176 // First, check system password...
174 177
175 struct passwd *pw = 0; 178 struct passwd *pw = 0;
176 struct spwd *spw = 0; 179 struct spwd *spw = 0;
177 180
178 pw = getpwuid( geteuid() ); 181 pw = getpwuid( geteuid() );
179 spw = getspnam( pw->pw_name ); 182 spw = getspnam( pw->pw_name );
180 183
181 QString cpwd = QString::fromLocal8Bit( pw->pw_passwd ); 184 QString cpwd = QString::fromLocal8Bit( pw->pw_passwd );
182 if ( cpwd == "x" && spw ) 185 if ( cpwd == "x" && spw )
183 cpwd = QString::fromLocal8Bit( spw->sp_pwdp ); 186 cpwd = QString::fromLocal8Bit( spw->sp_pwdp );
184 187
185 // Note: some systems use more than crypt for passwords. 188 // Note: some systems use more than crypt for passwords.
186 QString cpassword = QString::fromLocal8Bit( crypt( password.local8Bit(), cpwd.local8Bit() ) ); 189 QString cpassword = QString::fromLocal8Bit( crypt( password.local8Bit(), cpwd.local8Bit() ) );
187 if ( cpwd == cpassword ) 190 if ( cpwd == cpassword )
188 return TRUE; 191 return TRUE;
189#endif 192#endif
190 193
191 static int lastdenial=0; 194 static int lastdenial=0;
192 static int denials=0; 195 static int denials=0;
193 int now = time(0); 196 int now = time(0);
194 197
195 // Detect old Qtopia Desktop (no password) 198 // Detect old Qtopia Desktop (no password)
196 if ( password.isEmpty() ) { 199 if ( password.isEmpty() ) {
197 if ( denials < 1 || now > lastdenial+600 ) { 200 if ( denials < 1 || now > lastdenial+600 ) {
198 QMessageBox::warning( 0,tr("Sync Connection"), 201 QMessageBox::warning( 0,tr("Sync Connection"),
199 tr("<p>An unauthorized system is requesting access to this device." 202 tr("<p>An unauthorized system is requesting access to this device."
200 "<p>If you are using a version of Qtopia Desktop older than 1.5.1, " 203 "<p>If you are using a version of Qtopia Desktop older than 1.5.1, "
201 "please upgrade."), 204 "please upgrade."),
202 tr("Deny") ); 205 tr("Deny") );
203 denials++; 206 denials++;
204 lastdenial=now; 207 lastdenial=now;
205 } 208 }
206 return FALSE; 209 return FALSE;
207 } 210 }
208 211
209 // Second, check sync password... 212 // Second, check sync password...
210 QString pass = password.left(6); 213 QString pass = password.left(6);
211 /* old QtopiaDesktops are sending 214 /* old QtopiaDesktops are sending
212 * rootme newer versions got a Qtopia 215 * rootme newer versions got a Qtopia
213 * prefixed. Qtopia prefix will suceed 216 * prefixed. Qtopia prefix will suceed
214 * until the sync software syncs up 217 * until the sync software syncs up
215 * FIXME 218 * FIXME
216 */ 219 */
217 if ( pass == "rootme" || pass == "Qtopia") { 220 if ( pass == "rootme" || pass == "Qtopia") {
218 221
219 QString cpassword = QString::fromLocal8Bit( crypt( password.mid(8).local8Bit(), "qp" ) ); 222 QString cpassword = QString::fromLocal8Bit( crypt( password.mid(8).local8Bit(), "qp" ) );
220 Config cfg("Security"); 223 Config cfg("Security");
221 cfg.setGroup("Sync"); 224 cfg.setGroup("Sync");
222 QString pwds = cfg.readEntry("Passwords"); 225 QString pwds = cfg.readEntry("Passwords");
223 if ( QStringList::split(QChar(' '),pwds).contains(cpassword) ) 226 if ( QStringList::split(QChar(' '),pwds).contains(cpassword) )
224 return TRUE; 227 return TRUE;
225 228
226 // Unrecognized system. Be careful... 229 // Unrecognized system. Be careful...
227 230
228 if ( (denials > 2 && now < lastdenial+600) 231 if ( (denials > 2 && now < lastdenial+600)
229 || QMessageBox::warning(0,tr("Sync Connection"), 232 || QMessageBox::warning(0,tr("Sync Connection"),
230 tr("<p>An unrecognized system is requesting access to this device." 233 tr("<p>An unrecognized system is requesting access to this device."
231 "<p>If you have just initiated a Sync for the first time, this is normal."), 234 "<p>If you have just initiated a Sync for the first time, this is normal."),
232 tr("Allow"),tr("Deny"))==1 ) 235 tr("Allow"),tr("Deny"))==1 )
233 { 236 {
234 denials++; 237 denials++;
235 lastdenial=now; 238 lastdenial=now;
236 return FALSE; 239 return FALSE;
237 } else { 240 } else {
238 denials=0; 241 denials=0;
239 cfg.writeEntry("Passwords",pwds+" "+cpassword); 242 cfg.writeEntry("Passwords",pwds+" "+cpassword);
240 return TRUE; 243 return TRUE;
241 } 244 }
242 } 245 }
243 246
244 return FALSE; 247 return FALSE;
245} 248}
246 249
247ServerPI::ServerPI( int socket, QObject *parent , const char* name ) 250ServerPI::ServerPI( int socket, QObject *parent , const char* name )
248 : QSocket( parent, name ) , dtp( 0 ), serversocket( 0 ), waitsocket( 0 ) 251 : QSocket( parent, name ) , dtp( 0 ), serversocket( 0 ), waitsocket( 0 )
249{ 252{
250 state = Connected; 253 state = Connected;
251 254
252 setSocket( socket ); 255 setSocket( socket );
253 256
254 peerport = peerPort(); 257 peerport = peerPort();
255 peeraddress = peerAddress(); 258 peeraddress = peerAddress();
256 259
257#ifndef INSECURE 260#ifndef INSECURE
258 if ( !SyncAuthentication::isAuthorized(peeraddress) ) { 261 if ( !SyncAuthentication::isAuthorized(peeraddress) ) {
259 state = Forbidden; 262 state = Forbidden;
260 startTimer( 0 ); 263 startTimer( 0 );
261 } else 264 } else
262#endif 265#endif
263 { 266 {
264 connect( this, SIGNAL( readyRead() ), SLOT( read() ) ); 267 connect( this, SIGNAL( readyRead() ), SLOT( read() ) );
265 connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); 268 connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) );
266 269
267 passiv = FALSE; 270 passiv = FALSE;
268 for( int i = 0; i < 4; i++ ) 271 for( int i = 0; i < 4; i++ )
269 wait[i] = FALSE; 272 wait[i] = FALSE;
270 273
271 send( "220 Qtopia " QPE_VERSION " FTP Server" ); 274 send( "220 Qtopia " QPE_VERSION " FTP Server" );
272 state = Wait_USER; 275 state = Wait_USER;
273 276
274 dtp = new ServerDTP( this ); 277 dtp = new ServerDTP( this );
275 connect( dtp, SIGNAL( completed() ), SLOT( dtpCompleted() ) ); 278 connect( dtp, SIGNAL( completed() ), SLOT( dtpCompleted() ) );
276 connect( dtp, SIGNAL( failed() ), SLOT( dtpFailed() ) ); 279 connect( dtp, SIGNAL( failed() ), SLOT( dtpFailed() ) );
277 connect( dtp, SIGNAL( error( int ) ), SLOT( dtpError( int ) ) ); 280 connect( dtp, SIGNAL( error( int ) ), SLOT( dtpError( int ) ) );
278 281
279 282
280 directory = QDir::currentDirPath(); 283 directory = QDir::currentDirPath();
281 284
282 static int p = 1024; 285 static int p = 1024;
283 286
284 while ( !serversocket || !serversocket->ok() ) { 287 while ( !serversocket || !serversocket->ok() ) {
285 delete serversocket; 288 delete serversocket;
286 serversocket = new ServerSocket( ++p, this ); 289 serversocket = new ServerSocket( ++p, this );
287 } 290 }
288 connect( serversocket, SIGNAL( newIncomming( int ) ), 291 connect( serversocket, SIGNAL( newIncomming( int ) ),
289 SLOT( newConnection( int ) ) ); 292 SLOT( newConnection( int ) ) );
290 } 293 }
291} 294}
292 295
293ServerPI::~ServerPI() 296ServerPI::~ServerPI()
294{ 297{
295 298
296} 299}
297 300
298void ServerPI::connectionClosed() 301void ServerPI::connectionClosed()
299{ 302{
300 // qDebug( "Debug: Connection closed" ); 303 // qDebug( "Debug: Connection closed" );
301 delete this; 304 delete this;
302} 305}
303 306
304void ServerPI::send( const QString& msg ) 307void ServerPI::send( const QString& msg )
305{ 308{
306 QTextStream os( this ); 309 QTextStream os( this );
307 os << msg << endl; 310 os << msg << endl;
308 //qDebug( "Reply: %s", msg.latin1() ); 311 //qDebug( "Reply: %s", msg.latin1() );
309} 312}
310 313
311void ServerPI::read() 314void ServerPI::read()
312{ 315{
313 while ( canReadLine() ) 316 while ( canReadLine() )
314 process( readLine().stripWhiteSpace() ); 317 process( readLine().stripWhiteSpace() );
315} 318}
316 319
317bool ServerPI::checkReadFile( const QString& file ) 320bool ServerPI::checkReadFile( const QString& file )
318{ 321{
319 QString filename; 322 QString filename;
320 323
321 if ( file[0] != "/" ) 324 if ( file[0] != "/" )
322 filename = directory.path() + "/" + file; 325 filename = directory.path() + "/" + file;
323 else 326 else
324 filename = file; 327 filename = file;
325 328
326 QFileInfo fi( filename ); 329 QFileInfo fi( filename );
327 return ( fi.exists() && fi.isReadable() ); 330 return ( fi.exists() && fi.isReadable() );
328} 331}
329 332
330bool ServerPI::checkWriteFile( const QString& file ) 333bool ServerPI::checkWriteFile( const QString& file )
331{ 334{
332 QString filename; 335 QString filename;
333 336
334 if ( file[0] != "/" ) 337 if ( file[0] != "/" )
335 filename = directory.path() + "/" + file; 338 filename = directory.path() + "/" + file;
336 else 339 else
337 filename = file; 340 filename = file;
338 341
339 QFileInfo fi( filename ); 342 QFileInfo fi( filename );
340 343
341 if ( fi.exists() ) 344 if ( fi.exists() )
342 if ( !QFile( filename ).remove() ) 345 if ( !QFile( filename ).remove() )
343 return FALSE; 346 return FALSE;
344 return TRUE; 347 return TRUE;
345} 348}
346 349
347void ServerPI::process( const QString& message ) 350void ServerPI::process( const QString& message )
348{ 351{
349 //qDebug( "Command: %s", message.latin1() ); 352 //qDebug( "Command: %s", message.latin1() );
350 353
351 // split message using "," as separator 354 // split message using "," as separator
352 QStringList msg = QStringList::split( " ", message ); 355 QStringList msg = QStringList::split( " ", message );
353 if ( msg.isEmpty() ) return; 356 if ( msg.isEmpty() ) return;
354 357
355 // command token 358 // command token
356 QString cmd = msg[0].upper(); 359 QString cmd = msg[0].upper();
357 360
358 // argument token 361 // argument token
359 QString arg; 362 QString arg;
360 if ( msg.count() >= 2 ) 363 if ( msg.count() >= 2 )
361 arg = msg[1]; 364 arg = msg[1];
362 365
363 // full argument string 366 // full argument string
364 QString args; 367 QString args;
365 if ( msg.count() >= 2 ) { 368 if ( msg.count() >= 2 ) {
366 QStringList copy( msg ); 369 QStringList copy( msg );
367 // FIXME: for Qt3 370 // FIXME: for Qt3
368 // copy.pop_front() 371 // copy.pop_front()
369 copy.remove( copy.begin() ); 372 copy.remove( copy.begin() );
370 args = copy.join( " " ); 373 args = copy.join( " " );
371 } 374 }
372 375
373 //qDebug( "args: %s", args.latin1() ); 376 //qDebug( "args: %s", args.latin1() );
374 377
375 // we always respond to QUIT, regardless of state 378 // we always respond to QUIT, regardless of state
376 if ( cmd == "QUIT" ) { 379 if ( cmd == "QUIT" ) {
377 send( "211 Good bye!" ); 380 send( "211 Good bye!" );
378 delete this; 381 delete this;
379 return; 382 return;
380 } 383 }
381 384
382 // connected to client 385 // connected to client
383 if ( Connected == state ) 386 if ( Connected == state )
384 return; 387 return;
385 388
386 // waiting for user name 389 // waiting for user name
387 if ( Wait_USER == state ) { 390 if ( Wait_USER == state ) {
388 391
389 if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) { 392 if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) {
390 send( "530 Please login with USER and PASS" ); 393 send( "530 Please login with USER and PASS" );
391 return; 394 return;
392 } 395 }
393 send( "331 User name ok, need password" ); 396 send( "331 User name ok, need password" );
394 state = Wait_PASS; 397 state = Wait_PASS;
395 return; 398 return;
396 } 399 }
397 400
398 // waiting for password 401 // waiting for password
399 if ( Wait_PASS == state ) { 402 if ( Wait_PASS == state ) {
400 403
401 if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) { 404 if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) {
402 send( "530 Please login with USER and PASS" ); 405 send( "530 Please login with USER and PASS" );
403 return; 406 return;
404 } 407 }
405 send( "230 User logged in, proceed" ); 408 send( "230 User logged in, proceed" );
406 state = Ready; 409 state = Ready;
407 return; 410 return;
408 } 411 }
409 412
410 // ACCESS CONTROL COMMANDS 413 // ACCESS CONTROL COMMANDS
411 414
412 415
413 // account (ACCT) 416 // account (ACCT)
414 if ( cmd == "ACCT" ) { 417 if ( cmd == "ACCT" ) {
415 // even wu-ftp does not support it 418 // even wu-ftp does not support it
416 send( "502 Command not implemented" ); 419 send( "502 Command not implemented" );
417 } 420 }
418 421
419 // change working directory (CWD) 422 // change working directory (CWD)
420 else if ( cmd == "CWD" ) { 423 else if ( cmd == "CWD" ) {
421 424
422 if ( !args.isEmpty() ) { 425 if ( !args.isEmpty() ) {
423 if ( directory.cd( args, TRUE ) ) 426 if ( directory.cd( args, TRUE ) )
424 send( "250 Requested file action okay, completed" ); 427 send( "250 Requested file action okay, completed" );
425 else 428 else
426 send( "550 Requested action not taken" ); 429 send( "550 Requested action not taken" );
427 } 430 }
428 else 431 else
429 send( "500 Syntax error, command unrecognized" ); 432 send( "500 Syntax error, command unrecognized" );
430 } 433 }
431 434
432 // change to parent directory (CDUP) 435 // change to parent directory (CDUP)
433 else if ( cmd == "CDUP" ) { 436 else if ( cmd == "CDUP" ) {
434 if ( directory.cdUp() ) 437 if ( directory.cdUp() )
435 send( "250 Requested file action okay, completed" ); 438 send( "250 Requested file action okay, completed" );
436 else 439 else
437 send( "550 Requested action not taken" ); 440 send( "550 Requested action not taken" );
438 } 441 }
439 442
440 // structure mount (SMNT) 443 // structure mount (SMNT)
441 else if ( cmd == "SMNT" ) { 444 else if ( cmd == "SMNT" ) {
442 // even wu-ftp does not support it 445 // even wu-ftp does not support it
443 send( "502 Command not implemented" ); 446 send( "502 Command not implemented" );
444 } 447 }
445 448
446 // reinitialize (REIN) 449 // reinitialize (REIN)
447 else if ( cmd == "REIN" ) { 450 else if ( cmd == "REIN" ) {
448 // even wu-ftp does not support it 451 // even wu-ftp does not support it
449 send( "502 Command not implemented" ); 452 send( "502 Command not implemented" );
450 } 453 }
451 454
452 455
453 // TRANSFER PARAMETER COMMANDS 456 // TRANSFER PARAMETER COMMANDS
454 457
455 458
456 // data port (PORT) 459 // data port (PORT)
457 else if ( cmd == "PORT" ) { 460 else if ( cmd == "PORT" ) {
458 if ( parsePort( arg ) ) 461 if ( parsePort( arg ) )
459 send( "200 Command okay" ); 462 send( "200 Command okay" );
460 else 463 else
461 send( "500 Syntax error, command unrecognized" ); 464 send( "500 Syntax error, command unrecognized" );
462 } 465 }
463 466
464 // passive (PASV) 467 // passive (PASV)
465 else if ( cmd == "PASV" ) { 468 else if ( cmd == "PASV" ) {
466 passiv = TRUE; 469 passiv = TRUE;
467 send( "227 Entering Passive Mode (" 470 send( "227 Entering Passive Mode ("
468 + address().toString().replace( QRegExp( "\\." ), "," ) + "," 471 + address().toString().replace( QRegExp( "\\." ), "," ) + ","
469 + QString::number( ( serversocket->port() ) >> 8 ) + "," 472 + QString::number( ( serversocket->port() ) >> 8 ) + ","
470 + QString::number( ( serversocket->port() ) & 0xFF ) +")" ); 473 + QString::number( ( serversocket->port() ) & 0xFF ) +")" );
471 } 474 }
472 475
473 // representation type (TYPE) 476 // representation type (TYPE)
474 else if ( cmd == "TYPE" ) { 477 else if ( cmd == "TYPE" ) {
475 if ( arg.upper() == "A" || arg.upper() == "I" ) 478 if ( arg.upper() == "A" || arg.upper() == "I" )
476 send( "200 Command okay" ); 479 send( "200 Command okay" );
477 else 480 else
478 send( "504 Command not implemented for that parameter" ); 481 send( "504 Command not implemented for that parameter" );
479 } 482 }
480 483
481 // file structure (STRU) 484 // file structure (STRU)
482 else if ( cmd == "STRU" ) { 485 else if ( cmd == "STRU" ) {
483 if ( arg.upper() == "F" ) 486 if ( arg.upper() == "F" )
484 send( "200 Command okay" ); 487 send( "200 Command okay" );
485 else 488 else
486 send( "504 Command not implemented for that parameter" ); 489 send( "504 Command not implemented for that parameter" );
487 } 490 }
488 491
489 // transfer mode (MODE) 492 // transfer mode (MODE)
490 else if ( cmd == "MODE" ) { 493 else if ( cmd == "MODE" ) {
491 if ( arg.upper() == "S" ) 494 if ( arg.upper() == "S" )
492 send( "200 Command okay" ); 495 send( "200 Command okay" );
493 else 496 else
494 send( "504 Command not implemented for that parameter" ); 497 send( "504 Command not implemented for that parameter" );
495 } 498 }
496 499
497 500
498 // FTP SERVICE COMMANDS 501 // FTP SERVICE COMMANDS
499 502
500 503
501 // retrieve (RETR) 504 // retrieve (RETR)
502 else if ( cmd == "RETR" ) 505 else if ( cmd == "RETR" )
503 if ( !args.isEmpty() && checkReadFile( absFilePath( args ) ) 506 if ( !args.isEmpty() && checkReadFile( absFilePath( args ) )
504 || backupRestoreGzip( absFilePath( args ) ) ) { 507 || backupRestoreGzip( absFilePath( args ) ) ) {
505 send( "150 File status okay" ); 508 send( "150 File status okay" );
506 sendFile( absFilePath( args ) ); 509 sendFile( absFilePath( args ) );
507 } 510 }
508 else { 511 else {
509 qDebug("550 Requested action not taken"); 512 qDebug("550 Requested action not taken");
510 send( "550 Requested action not taken" ); 513 send( "550 Requested action not taken" );
511 } 514 }
512 515
513 // store (STOR) 516 // store (STOR)
514 else if ( cmd == "STOR" ) 517 else if ( cmd == "STOR" )
515 if ( !args.isEmpty() && checkWriteFile( absFilePath( args ) ) ) { 518 if ( !args.isEmpty() && checkWriteFile( absFilePath( args ) ) ) {
516 send( "150 File status okay" ); 519 send( "150 File status okay" );
517 retrieveFile( absFilePath( args ) ); 520 retrieveFile( absFilePath( args ) );
518 } 521 }
519 else 522 else
520 send( "550 Requested action not taken" ); 523 send( "550 Requested action not taken" );
521 524
522 // store unique (STOU) 525 // store unique (STOU)
523 else if ( cmd == "STOU" ) { 526 else if ( cmd == "STOU" ) {
524 send( "502 Command not implemented" ); 527 send( "502 Command not implemented" );
525 } 528 }
526 529
527 // append (APPE) 530 // append (APPE)
528 else if ( cmd == "APPE" ) { 531 else if ( cmd == "APPE" ) {
529 send( "502 Command not implemented" ); 532 send( "502 Command not implemented" );
530 } 533 }
531 534
532 // allocate (ALLO) 535 // allocate (ALLO)
533 else if ( cmd == "ALLO" ) { 536 else if ( cmd == "ALLO" ) {
534 send( "200 Command okay" ); 537 send( "200 Command okay" );
535 } 538 }
536 539
537 // restart (REST) 540 // restart (REST)
538 else if ( cmd == "REST" ) { 541 else if ( cmd == "REST" ) {
539 send( "502 Command not implemented" ); 542 send( "502 Command not implemented" );
540 } 543 }
541 544
542 // rename from (RNFR) 545 // rename from (RNFR)
543 else if ( cmd == "RNFR" ) { 546 else if ( cmd == "RNFR" ) {
544 renameFrom = QString::null; 547 renameFrom = QString::null;
545 if ( args.isEmpty() ) 548 if ( args.isEmpty() )
546 send( "500 Syntax error, command unrecognized" ); 549 send( "500 Syntax error, command unrecognized" );
547 else { 550 else {
548 QFile file( absFilePath( args ) ); 551 QFile file( absFilePath( args ) );
549 if ( file.exists() ) { 552 if ( file.exists() ) {
550 send( "350 File exists, ready for destination name" ); 553 send( "350 File exists, ready for destination name" );
551 renameFrom = absFilePath( args ); 554 renameFrom = absFilePath( args );
552 } 555 }
553 else 556 else
554 send( "550 Requested action not taken" ); 557 send( "550 Requested action not taken" );
555 } 558 }
556 } 559 }
557 560
558 // rename to (RNTO) 561 // rename to (RNTO)
559 else if ( cmd == "RNTO" ) { 562 else if ( cmd == "RNTO" ) {
560 if ( lastCommand != "RNFR" ) 563 if ( lastCommand != "RNFR" )
561 send( "503 Bad sequence of commands" ); 564 send( "503 Bad sequence of commands" );
562 else if ( args.isEmpty() ) 565 else if ( args.isEmpty() )
563 send( "500 Syntax error, command unrecognized" ); 566 send( "500 Syntax error, command unrecognized" );
564 else { 567 else {
565 QDir dir( absFilePath( args ) ); 568 QDir dir( absFilePath( args ) );
566 if ( dir.rename( renameFrom, absFilePath( args ), TRUE ) ) 569 if ( dir.rename( renameFrom, absFilePath( args ), TRUE ) )
567 send( "250 Requested file action okay, completed." ); 570 send( "250 Requested file action okay, completed." );
568 else 571 else
569 send( "550 Requested action not taken" ); 572 send( "550 Requested action not taken" );
570 } 573 }
571 } 574 }
572 575
573 // abort (ABOR) 576 // abort (ABOR)
574 else if ( cmd.contains( "ABOR" ) ) { 577 else if ( cmd.contains( "ABOR" ) ) {
575 dtp->close(); 578 dtp->close();
576 if ( dtp->dtpMode() != ServerDTP::Idle ) 579 if ( dtp->dtpMode() != ServerDTP::Idle )
577 send( "426 Connection closed; transfer aborted" ); 580 send( "426 Connection closed; transfer aborted" );
578 else 581 else
579 send( "226 Closing data connection" ); 582 send( "226 Closing data connection" );
580 } 583 }
581 584
582 // delete (DELE) 585 // delete (DELE)
583 else if ( cmd == "DELE" ) { 586 else if ( cmd == "DELE" ) {
584 if ( args.isEmpty() ) 587 if ( args.isEmpty() )
585 send( "500 Syntax error, command unrecognized" ); 588 send( "500 Syntax error, command unrecognized" );
586 else { 589 else {
587 QFile file( absFilePath( args ) ) ; 590 QFile file( absFilePath( args ) ) ;
588 if ( file.remove() ) { 591 if ( file.remove() ) {
589 send( "250 Requested file action okay, completed" ); 592 send( "250 Requested file action okay, completed" );
590 QCopEnvelope e("QPE/System", "linkChanged(QString)" ); 593 QCopEnvelope e("QPE/System", "linkChanged(QString)" );
591 e << file.name(); 594 e << file.name();
592 } else { 595 } else {
593 send( "550 Requested action not taken" ); 596 send( "550 Requested action not taken" );
594 } 597 }
595 } 598 }
596 } 599 }
597 600
598 // remove directory (RMD) 601 // remove directory (RMD)
599 else if ( cmd == "RMD" ) { 602 else if ( cmd == "RMD" ) {
600 if ( args.isEmpty() ) 603 if ( args.isEmpty() )
601 send( "500 Syntax error, command unrecognized" ); 604 send( "500 Syntax error, command unrecognized" );
602 else { 605 else {
603 QDir dir; 606 QDir dir;
604 if ( dir.rmdir( absFilePath( args ), TRUE ) ) 607 if ( dir.rmdir( absFilePath( args ), TRUE ) )
605 send( "250 Requested file action okay, completed" ); 608 send( "250 Requested file action okay, completed" );
606 else 609 else
607 send( "550 Requested action not taken" ); 610 send( "550 Requested action not taken" );
608 } 611 }
609 } 612 }
610 613
611 // make directory (MKD) 614 // make directory (MKD)
612 else if ( cmd == "MKD" ) { 615 else if ( cmd == "MKD" ) {
613 if ( args.isEmpty() ) { 616 if ( args.isEmpty() ) {
614 qDebug(" Error: no arg"); 617 qDebug(" Error: no arg");
615 send( "500 Syntax error, command unrecognized" ); 618 send( "500 Syntax error, command unrecognized" );
616 } 619 }
617 else { 620 else {
618 QDir dir; 621 QDir dir;
619 if ( dir.mkdir( absFilePath( args ), TRUE ) ) 622 if ( dir.mkdir( absFilePath( args ), TRUE ) )
620 send( "250 Requested file action okay, completed." ); 623 send( "250 Requested file action okay, completed." );
621 else 624 else
622 send( "550 Requested action not taken" ); 625 send( "550 Requested action not taken" );
623 } 626 }
624 } 627 }
625 628
626 // print working directory (PWD) 629 // print working directory (PWD)
627 else if ( cmd == "PWD" ) { 630 else if ( cmd == "PWD" ) {
628 send( "257 \"" + directory.path() +"\"" ); 631 send( "257 \"" + directory.path() +"\"" );
629 } 632 }
630 633
631 // list (LIST) 634 // list (LIST)
632 else if ( cmd == "LIST" ) { 635 else if ( cmd == "LIST" ) {
633 if ( sendList( absFilePath( args ) ) ) 636 if ( sendList( absFilePath( args ) ) )
634 send( "150 File status okay" ); 637 send( "150 File status okay" );
635 else 638 else
636 send( "500 Syntax error, command unrecognized" ); 639 send( "500 Syntax error, command unrecognized" );
637 } 640 }
638 641
639 // size (SIZE) 642 // size (SIZE)
640 else if ( cmd == "SIZE" ) { 643 else if ( cmd == "SIZE" ) {
641 QString filePath = absFilePath( args ); 644 QString filePath = absFilePath( args );
642 QFileInfo fi( filePath ); 645 QFileInfo fi( filePath );
643 bool gzipfile = backupRestoreGzip( filePath ); 646 bool gzipfile = backupRestoreGzip( filePath );
644 if ( !fi.exists() && !gzipfile ) 647 if ( !fi.exists() && !gzipfile )
645 send( "500 Syntax error, command unrecognized" ); 648 send( "500 Syntax error, command unrecognized" );
646 else { 649 else {
647 if ( !gzipfile ) 650 if ( !gzipfile )
648 send( "213 " + QString::number( fi.size() ) ); 651 send( "213 " + QString::number( fi.size() ) );
649 else { 652 else {
650 Process duproc( QString("du") ); 653 Process duproc( QString("du") );
651 duproc.addArgument("-s"); 654 duproc.addArgument("-s");
652 QString in, out; 655 QString in, out;
653 if ( !duproc.exec(in, out) ) { 656 if ( !duproc.exec(in, out) ) {
654 qDebug("du process failed; just sending back 1K"); 657 qDebug("du process failed; just sending back 1K");
655 send( "213 1024"); 658 send( "213 1024");
656 } 659 }
657 else { 660 else {
658 QString size = out.left( out.find("\t") ); 661 QString size = out.left( out.find("\t") );
659 int guess = size.toInt()/5; 662 int guess = size.toInt()/5;
660 if ( filePath.contains("doc") ) 663 if ( filePath.contains("doc") )
661 guess *= 1000; 664 guess *= 1000;
662 qDebug("sending back gzip guess of %d", guess); 665 qDebug("sending back gzip guess of %d", guess);
663 send( "213 " + QString::number(guess) ); 666 send( "213 " + QString::number(guess) );
664 } 667 }
665 } 668 }
666 } 669 }
667 } 670 }
668 // name list (NLST) 671 // name list (NLST)
669 else if ( cmd == "NLST" ) { 672 else if ( cmd == "NLST" ) {
670 send( "502 Command not implemented" ); 673 send( "502 Command not implemented" );
671 } 674 }
672 675
673 // site parameters (SITE) 676 // site parameters (SITE)
674 else if ( cmd == "SITE" ) { 677 else if ( cmd == "SITE" ) {
675 send( "502 Command not implemented" ); 678 send( "502 Command not implemented" );
676 } 679 }
677 680
678 // system (SYST) 681 // system (SYST)
679 else if ( cmd == "SYST" ) { 682 else if ( cmd == "SYST" ) {
680 send( "215 UNIX Type: L8" ); 683 send( "215 UNIX Type: L8" );
681 } 684 }
682 685
683 // status (STAT) 686 // status (STAT)
684 else if ( cmd == "STAT" ) { 687 else if ( cmd == "STAT" ) {
685 send( "502 Command not implemented" ); 688 send( "502 Command not implemented" );
686 } 689 }
687 690
688 // help (HELP ) 691 // help (HELP )
689 else if ( cmd == "HELP" ) { 692 else if ( cmd == "HELP" ) {
690 send( "502 Command not implemented" ); 693 send( "502 Command not implemented" );
691 } 694 }
692 695
693 // noop (NOOP) 696 // noop (NOOP)
694 else if ( cmd == "NOOP" ) { 697 else if ( cmd == "NOOP" ) {
695 send( "200 Command okay" ); 698 send( "200 Command okay" );
696 } 699 }
697 700
698 // not implemented 701 // not implemented
699 else 702 else
700 send( "502 Command not implemented" ); 703 send( "502 Command not implemented" );
701 704
702 lastCommand = cmd; 705 lastCommand = cmd;
703} 706}
704 707
705bool ServerPI::backupRestoreGzip( const QString &file ) 708bool ServerPI::backupRestoreGzip( const QString &file )
706{ 709{
707 return (file.find( "backup" ) != -1 && 710 return (file.find( "backup" ) != -1 &&
708 file.findRev( ".tgz" ) == (int)file.length()-4 ); 711 file.findRev( ".tgz" ) == (int)file.length()-4 );
709} 712}
710 713
711bool ServerPI::backupRestoreGzip( const QString &file, QStringList &targets ) 714bool ServerPI::backupRestoreGzip( const QString &file, QStringList &targets )
712{ 715{
713 if ( file.find( "backup" ) != -1 && 716 if ( file.find( "backup" ) != -1 &&
714 file.findRev( ".tgz" ) == (int)file.length()-4 ) { 717 file.findRev( ".tgz" ) == (int)file.length()-4 ) {
715 QFileInfo info( file ); 718 QFileInfo info( file );
716 targets = info.dirPath( TRUE ); 719 targets = info.dirPath( TRUE );
717 qDebug("ServerPI::backupRestoreGzip for %s = %s", file.latin1(), 720 qDebug("ServerPI::backupRestoreGzip for %s = %s", file.latin1(),
718 targets.join(" ").latin1() ); 721 targets.join(" ").latin1() );
719 return true; 722 return true;
720 } 723 }
721 return false; 724 return false;
722} 725}
723 726
724void ServerPI::sendFile( const QString& file ) 727void ServerPI::sendFile( const QString& file )
725{ 728{
726 if ( passiv ) { 729 if ( passiv ) {
727 wait[SendFile] = TRUE; 730 wait[SendFile] = TRUE;
728 waitfile = file; 731 waitfile = file;
729 if ( waitsocket ) 732 if ( waitsocket )
730 newConnection( waitsocket ); 733 newConnection( waitsocket );
731 } 734 }
732 else { 735 else {
733 QStringList targets; 736 QStringList targets;
734 if ( backupRestoreGzip( file, targets ) ) 737 if ( backupRestoreGzip( file, targets ) )
735 dtp->sendGzipFile( file, targets, peeraddress, peerport ); 738 dtp->sendGzipFile( file, targets, peeraddress, peerport );
736 else dtp->sendFile( file, peeraddress, peerport ); 739 else dtp->sendFile( file, peeraddress, peerport );
737 } 740 }
738} 741}
739 742
740void ServerPI::retrieveFile( const QString& file ) 743void ServerPI::retrieveFile( const QString& file )
741{ 744{
742 if ( passiv ) { 745 if ( passiv ) {
743 wait[RetrieveFile] = TRUE; 746 wait[RetrieveFile] = TRUE;
744 waitfile = file; 747 waitfile = file;
745 if ( waitsocket ) 748 if ( waitsocket )
746 newConnection( waitsocket ); 749 newConnection( waitsocket );
747 } 750 }
748 else { 751 else {
749 QStringList targets; 752 QStringList targets;
750 if ( backupRestoreGzip( file, targets ) ) 753 if ( backupRestoreGzip( file, targets ) )
751 dtp->retrieveGzipFile( file, peeraddress, peerport ); 754 dtp->retrieveGzipFile( file, peeraddress, peerport );
752 else 755 else
753 dtp->retrieveFile( file, peeraddress, peerport ); 756 dtp->retrieveFile( file, peeraddress, peerport );
754 } 757 }
755} 758}
756 759
757bool ServerPI::parsePort( const QString& pp ) 760bool ServerPI::parsePort( const QString& pp )
758{ 761{
759 QStringList p = QStringList::split( ",", pp ); 762 QStringList p = QStringList::split( ",", pp );
760 if ( p.count() != 6 ) return FALSE; 763 if ( p.count() != 6 ) return FALSE;
761 764
762 // h1,h2,h3,h4,p1,p2 765 // h1,h2,h3,h4,p1,p2
763 peeraddress = QHostAddress( ( p[0].toInt() << 24 ) + ( p[1].toInt() << 16 ) + 766 peeraddress = QHostAddress( ( p[0].toInt() << 24 ) + ( p[1].toInt() << 16 ) +
764 ( p[2].toInt() << 8 ) + p[3].toInt() ); 767 ( p[2].toInt() << 8 ) + p[3].toInt() );
765 peerport = ( p[4].toInt() << 8 ) + p[5].toInt(); 768 peerport = ( p[4].toInt() << 8 ) + p[5].toInt();
766 return TRUE; 769 return TRUE;
767} 770}
768 771
769void ServerPI::dtpCompleted() 772void ServerPI::dtpCompleted()
770{ 773{
771 send( "226 Closing data connection, file transfer successful" ); 774 send( "226 Closing data connection, file transfer successful" );
772 if ( dtp->dtpMode() == ServerDTP::RetrieveFile ) { 775 if ( dtp->dtpMode() == ServerDTP::RetrieveFile ) {
773 QString fn = dtp->fileName(); 776 QString fn = dtp->fileName();
774 if ( fn.right(8)==".desktop" && fn.find("/Documents/")>=0 ) { 777 if ( fn.right(8)==".desktop" && fn.find("/Documents/")>=0 ) {
775 QCopEnvelope e("QPE/System", "linkChanged(QString)" ); 778 QCopEnvelope e("QPE/System", "linkChanged(QString)" );
776 e << fn; 779 e << fn;
777 } 780 }
778 } 781 }
779 waitsocket = 0; 782 waitsocket = 0;
780 dtp->close(); 783 dtp->close();
781} 784}
782 785
783void ServerPI::dtpFailed() 786void ServerPI::dtpFailed()
784{ 787{
785 dtp->close(); 788 dtp->close();
786 waitsocket = 0; 789 waitsocket = 0;
787 send( "451 Requested action aborted: local error in processing" ); 790 send( "451 Requested action aborted: local error in processing" );
788} 791}
789 792
790void ServerPI::dtpError( int ) 793void ServerPI::dtpError( int )
791{ 794{
792 dtp->close(); 795 dtp->close();
793 waitsocket = 0; 796 waitsocket = 0;
794 send( "451 Requested action aborted: local error in processing" ); 797 send( "451 Requested action aborted: local error in processing" );
795} 798}