author | zecke <zecke> | 2002-10-04 21:16:03 (UTC) |
---|---|---|
committer | zecke <zecke> | 2002-10-04 21:16:03 (UTC) |
commit | 10048c2dd7f7cd26c582d64b5e255b00b47de304 (patch) (unidiff) | |
tree | 3b2a79d8218efe5f80d1b2e79e77b22cbc27c2ba | |
parent | dc0344e987ba9b386056dd7bab7c5e34922eff5c (diff) | |
download | opie-10048c2dd7f7cd26c582d64b5e255b00b47de304.zip opie-10048c2dd7f7cd26c582d64b5e255b00b47de304.tar.gz opie-10048c2dd7f7cd26c582d64b5e255b00b47de304.tar.bz2 |
Weird for some of you
_OS_LINUX_ does not get defined
so now it's a if !define(_OS_LINUX)
but this means you need to libuuid again
I don't know how to fix that right. I should ask tt
-rw-r--r-- | core/launcher/transferserver.cpp | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/core/launcher/transferserver.cpp b/core/launcher/transferserver.cpp index a20df2f..f9204ab 100644 --- a/core/launcher/transferserver.cpp +++ b/core/launcher/transferserver.cpp | |||
@@ -1,494 +1,494 @@ | |||
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 | #ifndef _OS_LINUX_ | 28 | #ifndef _OS_LINUX_ |
29 | 29 | ||
30 | extern "C" { | 30 | extern "C" { |
31 | #include <uuid/uuid.h> | 31 | #include <uuid/uuid.h> |
32 | #define UUID_H_INCLUDED | 32 | #define UUID_H_INCLUDED |
33 | } | 33 | } |
34 | 34 | ||
35 | #endif // not defined linux | 35 | #endif // not defined linux |
36 | 36 | ||
37 | #if defined(_OS_LINUX_) | 37 | #if defined(_OS_LINUX_) |
38 | #include <shadow.h> | 38 | #include <shadow.h> |
39 | #endif | 39 | #endif |
40 | 40 | ||
41 | #include <qdir.h> | 41 | #include <qdir.h> |
42 | #include <qfile.h> | 42 | #include <qfile.h> |
43 | #include <qtextstream.h> | 43 | #include <qtextstream.h> |
44 | #include <qdatastream.h> | 44 | #include <qdatastream.h> |
45 | #include <qmessagebox.h> | 45 | #include <qmessagebox.h> |
46 | #include <qstringlist.h> | 46 | #include <qstringlist.h> |
47 | #include <qfileinfo.h> | 47 | #include <qfileinfo.h> |
48 | #include <qregexp.h> | 48 | #include <qregexp.h> |
49 | //#include <qpe/qcopchannel_qws.h> | 49 | //#include <qpe/qcopchannel_qws.h> |
50 | #include <qpe/process.h> | 50 | #include <qpe/process.h> |
51 | #include <qpe/global.h> | 51 | #include <qpe/global.h> |
52 | #include <qpe/config.h> | 52 | #include <qpe/config.h> |
53 | #include <qpe/contact.h> | 53 | #include <qpe/contact.h> |
54 | #include <qpe/quuid.h> | 54 | #include <qpe/quuid.h> |
55 | #include <qpe/version.h> | 55 | #include <qpe/version.h> |
56 | #ifdef QWS | 56 | #ifdef QWS |
57 | #include <qpe/qcopenvelope_qws.h> | 57 | #include <qpe/qcopenvelope_qws.h> |
58 | #endif | 58 | #endif |
59 | 59 | ||
60 | #include "transferserver.h" | 60 | #include "transferserver.h" |
61 | #include "qprocess.h" | 61 | #include "qprocess.h" |
62 | 62 | ||
63 | const int block_size = 51200; | 63 | const int block_size = 51200; |
64 | 64 | ||
65 | TransferServer::TransferServer( Q_UINT16 port, QObject *parent , | 65 | TransferServer::TransferServer( Q_UINT16 port, QObject *parent , |
66 | const char* name ) | 66 | const char* name ) |
67 | : QServerSocket( port, 1, parent, name ) | 67 | : QServerSocket( port, 1, parent, name ) |
68 | { | 68 | { |
69 | if ( !ok() ) | 69 | if ( !ok() ) |
70 | qWarning( "Failed to bind to port %d", port ); | 70 | qWarning( "Failed to bind to port %d", port ); |
71 | } | 71 | } |
72 | 72 | ||
73 | TransferServer::~TransferServer() | 73 | TransferServer::~TransferServer() |
74 | { | 74 | { |
75 | 75 | ||
76 | } | 76 | } |
77 | 77 | ||
78 | void TransferServer::newConnection( int socket ) | 78 | void TransferServer::newConnection( int socket ) |
79 | { | 79 | { |
80 | (void) new ServerPI( socket, this ); | 80 | (void) new ServerPI( socket, this ); |
81 | } | 81 | } |
82 | 82 | ||
83 | /* | 83 | /* |
84 | * small class in anonymous namespace | 84 | * small class in anonymous namespace |
85 | * to generate a QUUid for us | 85 | * to generate a QUUid for us |
86 | */ | 86 | */ |
87 | namespace { | 87 | namespace { |
88 | struct UidGen { | 88 | struct UidGen { |
89 | QString uuid(); | 89 | QString uuid(); |
90 | }; | 90 | }; |
91 | #if defined(_OS_LINUX_) | 91 | #if !defined(_OS_LINUX_) |
92 | QString UidGen::uuid() { | ||
93 | uuid_t uuid; | ||
94 | uuid_generate( uuid ); | ||
95 | return QUUid( uuid ).toString(); | ||
96 | } | ||
97 | #else | ||
92 | /* | 98 | /* |
93 | * linux got a /proc/sys/kernel/random/uuid file | 99 | * linux got a /proc/sys/kernel/random/uuid file |
94 | * it'll generate the uuids for us | 100 | * it'll generate the uuids for us |
95 | */ | 101 | */ |
96 | QString UidGen::uuid() { | 102 | QString UidGen::uuid() { |
97 | QFile file( "/proc/sys/kernel/random/uuid" ); | 103 | QFile file( "/proc/sys/kernel/random/uuid" ); |
98 | if (!file.open(IO_ReadOnly ) ) | 104 | if (!file.open(IO_ReadOnly ) ) |
99 | return QString::null; | 105 | return QString::null; |
100 | 106 | ||
101 | QTextStream stream(&file); | 107 | QTextStream stream(&file); |
102 | 108 | ||
103 | return "{" + stream.read().stripWhiteSpace() + "}"; | 109 | return "{" + stream.read().stripWhiteSpace() + "}"; |
104 | } | 110 | } |
105 | #else | ||
106 | QString UidGen::uuid() { | ||
107 | uuid_t uuid; | ||
108 | uuid_generate( uuid ); | ||
109 | return QUUid( uuid ).toString(); | ||
110 | } | ||
111 | #endif | 111 | #endif |
112 | } | 112 | } |
113 | 113 | ||
114 | QString SyncAuthentication::serverId() | 114 | QString SyncAuthentication::serverId() |
115 | { | 115 | { |
116 | Config cfg("Security"); | 116 | Config cfg("Security"); |
117 | cfg.setGroup("Sync"); | 117 | cfg.setGroup("Sync"); |
118 | QString r=cfg.readEntry("serverid"); | 118 | QString r=cfg.readEntry("serverid"); |
119 | if ( r.isEmpty() ) { | 119 | if ( r.isEmpty() ) { |
120 | UidGen gen; | 120 | UidGen gen; |
121 | r = gen.uuid(); | 121 | r = gen.uuid(); |
122 | cfg.writeEntry("serverid", r ); | 122 | cfg.writeEntry("serverid", r ); |
123 | } | 123 | } |
124 | return r; | 124 | return r; |
125 | } | 125 | } |
126 | 126 | ||
127 | QString SyncAuthentication::ownerName() | 127 | QString SyncAuthentication::ownerName() |
128 | { | 128 | { |
129 | QString vfilename = Global::applicationFileName("addressbook", | 129 | QString vfilename = Global::applicationFileName("addressbook", |
130 | "businesscard.vcf"); | 130 | "businesscard.vcf"); |
131 | if (QFile::exists(vfilename)) { | 131 | if (QFile::exists(vfilename)) { |
132 | Contact c; | 132 | Contact c; |
133 | c = Contact::readVCard( vfilename )[0]; | 133 | c = Contact::readVCard( vfilename )[0]; |
134 | return c.fullName(); | 134 | return c.fullName(); |
135 | } | 135 | } |
136 | 136 | ||
137 | return ""; | 137 | return ""; |
138 | } | 138 | } |
139 | 139 | ||
140 | QString SyncAuthentication::loginName() | 140 | QString SyncAuthentication::loginName() |
141 | { | 141 | { |
142 | struct passwd *pw; | 142 | struct passwd *pw; |
143 | pw = getpwuid( geteuid() ); | 143 | pw = getpwuid( geteuid() ); |
144 | return QString::fromLocal8Bit( pw->pw_name ); | 144 | return QString::fromLocal8Bit( pw->pw_name ); |
145 | } | 145 | } |
146 | 146 | ||
147 | int SyncAuthentication::isAuthorized(QHostAddress peeraddress) | 147 | int SyncAuthentication::isAuthorized(QHostAddress peeraddress) |
148 | { | 148 | { |
149 | Config cfg("Security"); | 149 | Config cfg("Security"); |
150 | cfg.setGroup("Sync"); | 150 | cfg.setGroup("Sync"); |
151 | // QString allowedstr = cfg.readEntry("auth_peer","192.168.1.0"); | 151 | // QString allowedstr = cfg.readEntry("auth_peer","192.168.1.0"); |
152 | uint auth_peer = cfg.readNumEntry("auth_peer",0xc0a80100); | 152 | uint auth_peer = cfg.readNumEntry("auth_peer",0xc0a80100); |
153 | 153 | ||
154 | // QHostAddress allowed; | 154 | // QHostAddress allowed; |
155 | // allowed.setAddress(allowedstr); | 155 | // allowed.setAddress(allowedstr); |
156 | // uint auth_peer = allowed.ip4Addr(); | 156 | // uint auth_peer = allowed.ip4Addr(); |
157 | uint auth_peer_bits = cfg.readNumEntry("auth_peer_bits",24); | 157 | uint auth_peer_bits = cfg.readNumEntry("auth_peer_bits",24); |
158 | uint mask = auth_peer_bits >= 32 // shifting by 32 is not defined | 158 | uint mask = auth_peer_bits >= 32 // shifting by 32 is not defined |
159 | ? 0xffffffff : (((1<<auth_peer_bits)-1)<<(32-auth_peer_bits)); | 159 | ? 0xffffffff : (((1<<auth_peer_bits)-1)<<(32-auth_peer_bits)); |
160 | return (peeraddress.ip4Addr() & mask) == auth_peer; | 160 | return (peeraddress.ip4Addr() & mask) == auth_peer; |
161 | } | 161 | } |
162 | 162 | ||
163 | bool SyncAuthentication::checkUser( const QString& user ) | 163 | bool SyncAuthentication::checkUser( const QString& user ) |
164 | { | 164 | { |
165 | if ( user.isEmpty() ) return FALSE; | 165 | if ( user.isEmpty() ) return FALSE; |
166 | QString euser = loginName(); | 166 | QString euser = loginName(); |
167 | return user == euser; | 167 | return user == euser; |
168 | } | 168 | } |
169 | 169 | ||
170 | bool SyncAuthentication::checkPassword( const QString& password ) | 170 | bool SyncAuthentication::checkPassword( const QString& password ) |
171 | { | 171 | { |
172 | #ifdef ALLOW_UNIX_USER_FTP | 172 | #ifdef ALLOW_UNIX_USER_FTP |
173 | // First, check system password... | 173 | // First, check system password... |
174 | 174 | ||
175 | struct passwd *pw = 0; | 175 | struct passwd *pw = 0; |
176 | struct spwd *spw = 0; | 176 | struct spwd *spw = 0; |
177 | 177 | ||
178 | pw = getpwuid( geteuid() ); | 178 | pw = getpwuid( geteuid() ); |
179 | spw = getspnam( pw->pw_name ); | 179 | spw = getspnam( pw->pw_name ); |
180 | 180 | ||
181 | QString cpwd = QString::fromLocal8Bit( pw->pw_passwd ); | 181 | QString cpwd = QString::fromLocal8Bit( pw->pw_passwd ); |
182 | if ( cpwd == "x" && spw ) | 182 | if ( cpwd == "x" && spw ) |
183 | cpwd = QString::fromLocal8Bit( spw->sp_pwdp ); | 183 | cpwd = QString::fromLocal8Bit( spw->sp_pwdp ); |
184 | 184 | ||
185 | // Note: some systems use more than crypt for passwords. | 185 | // Note: some systems use more than crypt for passwords. |
186 | QString cpassword = QString::fromLocal8Bit( crypt( password.local8Bit(), cpwd.local8Bit() ) ); | 186 | QString cpassword = QString::fromLocal8Bit( crypt( password.local8Bit(), cpwd.local8Bit() ) ); |
187 | if ( cpwd == cpassword ) | 187 | if ( cpwd == cpassword ) |
188 | return TRUE; | 188 | return TRUE; |
189 | #endif | 189 | #endif |
190 | 190 | ||
191 | static int lastdenial=0; | 191 | static int lastdenial=0; |
192 | static int denials=0; | 192 | static int denials=0; |
193 | int now = time(0); | 193 | int now = time(0); |
194 | 194 | ||
195 | // Detect old Qtopia Desktop (no password) | 195 | // Detect old Qtopia Desktop (no password) |
196 | if ( password.isEmpty() ) { | 196 | if ( password.isEmpty() ) { |
197 | if ( denials < 1 || now > lastdenial+600 ) { | 197 | if ( denials < 1 || now > lastdenial+600 ) { |
198 | QMessageBox::warning( 0,tr("Sync Connection"), | 198 | QMessageBox::warning( 0,tr("Sync Connection"), |
199 | tr("<p>An unauthorized system is requesting access to this device." | 199 | 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, " | 200 | "<p>If you are using a version of Qtopia Desktop older than 1.5.1, " |
201 | "please upgrade."), | 201 | "please upgrade."), |
202 | tr("Deny") ); | 202 | tr("Deny") ); |
203 | denials++; | 203 | denials++; |
204 | lastdenial=now; | 204 | lastdenial=now; |
205 | } | 205 | } |
206 | return FALSE; | 206 | return FALSE; |
207 | } | 207 | } |
208 | 208 | ||
209 | // Second, check sync password... | 209 | // Second, check sync password... |
210 | QString pass = password.left(6); | 210 | QString pass = password.left(6); |
211 | /* old QtopiaDesktops are sending | 211 | /* old QtopiaDesktops are sending |
212 | * rootme newer versions got a Qtopia | 212 | * rootme newer versions got a Qtopia |
213 | * prefixed. Qtopia prefix will suceed | 213 | * prefixed. Qtopia prefix will suceed |
214 | * until the sync software syncs up | 214 | * until the sync software syncs up |
215 | * FIXME | 215 | * FIXME |
216 | */ | 216 | */ |
217 | if ( pass == "rootme" || pass == "Qtopia") { | 217 | if ( pass == "rootme" || pass == "Qtopia") { |
218 | 218 | ||
219 | QString cpassword = QString::fromLocal8Bit( crypt( password.mid(8).local8Bit(), "qp" ) ); | 219 | QString cpassword = QString::fromLocal8Bit( crypt( password.mid(8).local8Bit(), "qp" ) ); |
220 | Config cfg("Security"); | 220 | Config cfg("Security"); |
221 | cfg.setGroup("Sync"); | 221 | cfg.setGroup("Sync"); |
222 | QString pwds = cfg.readEntry("Passwords"); | 222 | QString pwds = cfg.readEntry("Passwords"); |
223 | if ( QStringList::split(QChar(' '),pwds).contains(cpassword) ) | 223 | if ( QStringList::split(QChar(' '),pwds).contains(cpassword) ) |
224 | return TRUE; | 224 | return TRUE; |
225 | 225 | ||
226 | // Unrecognized system. Be careful... | 226 | // Unrecognized system. Be careful... |
227 | 227 | ||
228 | if ( (denials > 2 && now < lastdenial+600) | 228 | if ( (denials > 2 && now < lastdenial+600) |
229 | || QMessageBox::warning(0,tr("Sync Connection"), | 229 | || QMessageBox::warning(0,tr("Sync Connection"), |
230 | tr("<p>An unrecognized system is requesting access to this device." | 230 | 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."), | 231 | "<p>If you have just initiated a Sync for the first time, this is normal."), |
232 | tr("Allow"),tr("Deny"))==1 ) | 232 | tr("Allow"),tr("Deny"))==1 ) |
233 | { | 233 | { |
234 | denials++; | 234 | denials++; |
235 | lastdenial=now; | 235 | lastdenial=now; |
236 | return FALSE; | 236 | return FALSE; |
237 | } else { | 237 | } else { |
238 | denials=0; | 238 | denials=0; |
239 | cfg.writeEntry("Passwords",pwds+" "+cpassword); | 239 | cfg.writeEntry("Passwords",pwds+" "+cpassword); |
240 | return TRUE; | 240 | return TRUE; |
241 | } | 241 | } |
242 | } | 242 | } |
243 | 243 | ||
244 | return FALSE; | 244 | return FALSE; |
245 | } | 245 | } |
246 | 246 | ||
247 | ServerPI::ServerPI( int socket, QObject *parent , const char* name ) | 247 | ServerPI::ServerPI( int socket, QObject *parent , const char* name ) |
248 | : QSocket( parent, name ) , dtp( 0 ), serversocket( 0 ), waitsocket( 0 ) | 248 | : QSocket( parent, name ) , dtp( 0 ), serversocket( 0 ), waitsocket( 0 ) |
249 | { | 249 | { |
250 | state = Connected; | 250 | state = Connected; |
251 | 251 | ||
252 | setSocket( socket ); | 252 | setSocket( socket ); |
253 | 253 | ||
254 | peerport = peerPort(); | 254 | peerport = peerPort(); |
255 | peeraddress = peerAddress(); | 255 | peeraddress = peerAddress(); |
256 | 256 | ||
257 | #ifndef INSECURE | 257 | #ifndef INSECURE |
258 | if ( !SyncAuthentication::isAuthorized(peeraddress) ) { | 258 | if ( !SyncAuthentication::isAuthorized(peeraddress) ) { |
259 | state = Forbidden; | 259 | state = Forbidden; |
260 | startTimer( 0 ); | 260 | startTimer( 0 ); |
261 | } else | 261 | } else |
262 | #endif | 262 | #endif |
263 | { | 263 | { |
264 | connect( this, SIGNAL( readyRead() ), SLOT( read() ) ); | 264 | connect( this, SIGNAL( readyRead() ), SLOT( read() ) ); |
265 | connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); | 265 | connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); |
266 | 266 | ||
267 | passiv = FALSE; | 267 | passiv = FALSE; |
268 | for( int i = 0; i < 4; i++ ) | 268 | for( int i = 0; i < 4; i++ ) |
269 | wait[i] = FALSE; | 269 | wait[i] = FALSE; |
270 | 270 | ||
271 | send( "220 Qtopia " QPE_VERSION " FTP Server" ); | 271 | send( "220 Qtopia " QPE_VERSION " FTP Server" ); |
272 | state = Wait_USER; | 272 | state = Wait_USER; |
273 | 273 | ||
274 | dtp = new ServerDTP( this ); | 274 | dtp = new ServerDTP( this ); |
275 | connect( dtp, SIGNAL( completed() ), SLOT( dtpCompleted() ) ); | 275 | connect( dtp, SIGNAL( completed() ), SLOT( dtpCompleted() ) ); |
276 | connect( dtp, SIGNAL( failed() ), SLOT( dtpFailed() ) ); | 276 | connect( dtp, SIGNAL( failed() ), SLOT( dtpFailed() ) ); |
277 | connect( dtp, SIGNAL( error( int ) ), SLOT( dtpError( int ) ) ); | 277 | connect( dtp, SIGNAL( error( int ) ), SLOT( dtpError( int ) ) ); |
278 | 278 | ||
279 | 279 | ||
280 | directory = QDir::currentDirPath(); | 280 | directory = QDir::currentDirPath(); |
281 | 281 | ||
282 | static int p = 1024; | 282 | static int p = 1024; |
283 | 283 | ||
284 | while ( !serversocket || !serversocket->ok() ) { | 284 | while ( !serversocket || !serversocket->ok() ) { |
285 | delete serversocket; | 285 | delete serversocket; |
286 | serversocket = new ServerSocket( ++p, this ); | 286 | serversocket = new ServerSocket( ++p, this ); |
287 | } | 287 | } |
288 | connect( serversocket, SIGNAL( newIncomming( int ) ), | 288 | connect( serversocket, SIGNAL( newIncomming( int ) ), |
289 | SLOT( newConnection( int ) ) ); | 289 | SLOT( newConnection( int ) ) ); |
290 | } | 290 | } |
291 | } | 291 | } |
292 | 292 | ||
293 | ServerPI::~ServerPI() | 293 | ServerPI::~ServerPI() |
294 | { | 294 | { |
295 | 295 | ||
296 | } | 296 | } |
297 | 297 | ||
298 | void ServerPI::connectionClosed() | 298 | void ServerPI::connectionClosed() |
299 | { | 299 | { |
300 | // qDebug( "Debug: Connection closed" ); | 300 | // qDebug( "Debug: Connection closed" ); |
301 | delete this; | 301 | delete this; |
302 | } | 302 | } |
303 | 303 | ||
304 | void ServerPI::send( const QString& msg ) | 304 | void ServerPI::send( const QString& msg ) |
305 | { | 305 | { |
306 | QTextStream os( this ); | 306 | QTextStream os( this ); |
307 | os << msg << endl; | 307 | os << msg << endl; |
308 | //qDebug( "Reply: %s", msg.latin1() ); | 308 | //qDebug( "Reply: %s", msg.latin1() ); |
309 | } | 309 | } |
310 | 310 | ||
311 | void ServerPI::read() | 311 | void ServerPI::read() |
312 | { | 312 | { |
313 | while ( canReadLine() ) | 313 | while ( canReadLine() ) |
314 | process( readLine().stripWhiteSpace() ); | 314 | process( readLine().stripWhiteSpace() ); |
315 | } | 315 | } |
316 | 316 | ||
317 | bool ServerPI::checkReadFile( const QString& file ) | 317 | bool ServerPI::checkReadFile( const QString& file ) |
318 | { | 318 | { |
319 | QString filename; | 319 | QString filename; |
320 | 320 | ||
321 | if ( file[0] != "/" ) | 321 | if ( file[0] != "/" ) |
322 | filename = directory.path() + "/" + file; | 322 | filename = directory.path() + "/" + file; |
323 | else | 323 | else |
324 | filename = file; | 324 | filename = file; |
325 | 325 | ||
326 | QFileInfo fi( filename ); | 326 | QFileInfo fi( filename ); |
327 | return ( fi.exists() && fi.isReadable() ); | 327 | return ( fi.exists() && fi.isReadable() ); |
328 | } | 328 | } |
329 | 329 | ||
330 | bool ServerPI::checkWriteFile( const QString& file ) | 330 | bool ServerPI::checkWriteFile( const QString& file ) |
331 | { | 331 | { |
332 | QString filename; | 332 | QString filename; |
333 | 333 | ||
334 | if ( file[0] != "/" ) | 334 | if ( file[0] != "/" ) |
335 | filename = directory.path() + "/" + file; | 335 | filename = directory.path() + "/" + file; |
336 | else | 336 | else |
337 | filename = file; | 337 | filename = file; |
338 | 338 | ||
339 | QFileInfo fi( filename ); | 339 | QFileInfo fi( filename ); |
340 | 340 | ||
341 | if ( fi.exists() ) | 341 | if ( fi.exists() ) |
342 | if ( !QFile( filename ).remove() ) | 342 | if ( !QFile( filename ).remove() ) |
343 | return FALSE; | 343 | return FALSE; |
344 | return TRUE; | 344 | return TRUE; |
345 | } | 345 | } |
346 | 346 | ||
347 | void ServerPI::process( const QString& message ) | 347 | void ServerPI::process( const QString& message ) |
348 | { | 348 | { |
349 | //qDebug( "Command: %s", message.latin1() ); | 349 | //qDebug( "Command: %s", message.latin1() ); |
350 | 350 | ||
351 | // split message using "," as separator | 351 | // split message using "," as separator |
352 | QStringList msg = QStringList::split( " ", message ); | 352 | QStringList msg = QStringList::split( " ", message ); |
353 | if ( msg.isEmpty() ) return; | 353 | if ( msg.isEmpty() ) return; |
354 | 354 | ||
355 | // command token | 355 | // command token |
356 | QString cmd = msg[0].upper(); | 356 | QString cmd = msg[0].upper(); |
357 | 357 | ||
358 | // argument token | 358 | // argument token |
359 | QString arg; | 359 | QString arg; |
360 | if ( msg.count() >= 2 ) | 360 | if ( msg.count() >= 2 ) |
361 | arg = msg[1]; | 361 | arg = msg[1]; |
362 | 362 | ||
363 | // full argument string | 363 | // full argument string |
364 | QString args; | 364 | QString args; |
365 | if ( msg.count() >= 2 ) { | 365 | if ( msg.count() >= 2 ) { |
366 | QStringList copy( msg ); | 366 | QStringList copy( msg ); |
367 | // FIXME: for Qt3 | 367 | // FIXME: for Qt3 |
368 | // copy.pop_front() | 368 | // copy.pop_front() |
369 | copy.remove( copy.begin() ); | 369 | copy.remove( copy.begin() ); |
370 | args = copy.join( " " ); | 370 | args = copy.join( " " ); |
371 | } | 371 | } |
372 | 372 | ||
373 | //qDebug( "args: %s", args.latin1() ); | 373 | //qDebug( "args: %s", args.latin1() ); |
374 | 374 | ||
375 | // we always respond to QUIT, regardless of state | 375 | // we always respond to QUIT, regardless of state |
376 | if ( cmd == "QUIT" ) { | 376 | if ( cmd == "QUIT" ) { |
377 | send( "211 Good bye!" ); | 377 | send( "211 Good bye!" ); |
378 | delete this; | 378 | delete this; |
379 | return; | 379 | return; |
380 | } | 380 | } |
381 | 381 | ||
382 | // connected to client | 382 | // connected to client |
383 | if ( Connected == state ) | 383 | if ( Connected == state ) |
384 | return; | 384 | return; |
385 | 385 | ||
386 | // waiting for user name | 386 | // waiting for user name |
387 | if ( Wait_USER == state ) { | 387 | if ( Wait_USER == state ) { |
388 | 388 | ||
389 | if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) { | 389 | if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) { |
390 | send( "530 Please login with USER and PASS" ); | 390 | send( "530 Please login with USER and PASS" ); |
391 | return; | 391 | return; |
392 | } | 392 | } |
393 | send( "331 User name ok, need password" ); | 393 | send( "331 User name ok, need password" ); |
394 | state = Wait_PASS; | 394 | state = Wait_PASS; |
395 | return; | 395 | return; |
396 | } | 396 | } |
397 | 397 | ||
398 | // waiting for password | 398 | // waiting for password |
399 | if ( Wait_PASS == state ) { | 399 | if ( Wait_PASS == state ) { |
400 | 400 | ||
401 | if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) { | 401 | if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) { |
402 | send( "530 Please login with USER and PASS" ); | 402 | send( "530 Please login with USER and PASS" ); |
403 | return; | 403 | return; |
404 | } | 404 | } |
405 | send( "230 User logged in, proceed" ); | 405 | send( "230 User logged in, proceed" ); |
406 | state = Ready; | 406 | state = Ready; |
407 | return; | 407 | return; |
408 | } | 408 | } |
409 | 409 | ||
410 | // ACCESS CONTROL COMMANDS | 410 | // ACCESS CONTROL COMMANDS |
411 | 411 | ||
412 | 412 | ||
413 | // account (ACCT) | 413 | // account (ACCT) |
414 | if ( cmd == "ACCT" ) { | 414 | if ( cmd == "ACCT" ) { |
415 | // even wu-ftp does not support it | 415 | // even wu-ftp does not support it |
416 | send( "502 Command not implemented" ); | 416 | send( "502 Command not implemented" ); |
417 | } | 417 | } |
418 | 418 | ||
419 | // change working directory (CWD) | 419 | // change working directory (CWD) |
420 | else if ( cmd == "CWD" ) { | 420 | else if ( cmd == "CWD" ) { |
421 | 421 | ||
422 | if ( !args.isEmpty() ) { | 422 | if ( !args.isEmpty() ) { |
423 | if ( directory.cd( args, TRUE ) ) | 423 | if ( directory.cd( args, TRUE ) ) |
424 | send( "250 Requested file action okay, completed" ); | 424 | send( "250 Requested file action okay, completed" ); |
425 | else | 425 | else |
426 | send( "550 Requested action not taken" ); | 426 | send( "550 Requested action not taken" ); |
427 | } | 427 | } |
428 | else | 428 | else |
429 | send( "500 Syntax error, command unrecognized" ); | 429 | send( "500 Syntax error, command unrecognized" ); |
430 | } | 430 | } |
431 | 431 | ||
432 | // change to parent directory (CDUP) | 432 | // change to parent directory (CDUP) |
433 | else if ( cmd == "CDUP" ) { | 433 | else if ( cmd == "CDUP" ) { |
434 | if ( directory.cdUp() ) | 434 | if ( directory.cdUp() ) |
435 | send( "250 Requested file action okay, completed" ); | 435 | send( "250 Requested file action okay, completed" ); |
436 | else | 436 | else |
437 | send( "550 Requested action not taken" ); | 437 | send( "550 Requested action not taken" ); |
438 | } | 438 | } |
439 | 439 | ||
440 | // structure mount (SMNT) | 440 | // structure mount (SMNT) |
441 | else if ( cmd == "SMNT" ) { | 441 | else if ( cmd == "SMNT" ) { |
442 | // even wu-ftp does not support it | 442 | // even wu-ftp does not support it |
443 | send( "502 Command not implemented" ); | 443 | send( "502 Command not implemented" ); |
444 | } | 444 | } |
445 | 445 | ||
446 | // reinitialize (REIN) | 446 | // reinitialize (REIN) |
447 | else if ( cmd == "REIN" ) { | 447 | else if ( cmd == "REIN" ) { |
448 | // even wu-ftp does not support it | 448 | // even wu-ftp does not support it |
449 | send( "502 Command not implemented" ); | 449 | send( "502 Command not implemented" ); |
450 | } | 450 | } |
451 | 451 | ||
452 | 452 | ||
453 | // TRANSFER PARAMETER COMMANDS | 453 | // TRANSFER PARAMETER COMMANDS |
454 | 454 | ||
455 | 455 | ||
456 | // data port (PORT) | 456 | // data port (PORT) |
457 | else if ( cmd == "PORT" ) { | 457 | else if ( cmd == "PORT" ) { |
458 | if ( parsePort( arg ) ) | 458 | if ( parsePort( arg ) ) |
459 | send( "200 Command okay" ); | 459 | send( "200 Command okay" ); |
460 | else | 460 | else |
461 | send( "500 Syntax error, command unrecognized" ); | 461 | send( "500 Syntax error, command unrecognized" ); |
462 | } | 462 | } |
463 | 463 | ||
464 | // passive (PASV) | 464 | // passive (PASV) |
465 | else if ( cmd == "PASV" ) { | 465 | else if ( cmd == "PASV" ) { |
466 | passiv = TRUE; | 466 | passiv = TRUE; |
467 | send( "227 Entering Passive Mode (" | 467 | send( "227 Entering Passive Mode (" |
468 | + address().toString().replace( QRegExp( "\\." ), "," ) + "," | 468 | + address().toString().replace( QRegExp( "\\." ), "," ) + "," |
469 | + QString::number( ( serversocket->port() ) >> 8 ) + "," | 469 | + QString::number( ( serversocket->port() ) >> 8 ) + "," |
470 | + QString::number( ( serversocket->port() ) & 0xFF ) +")" ); | 470 | + QString::number( ( serversocket->port() ) & 0xFF ) +")" ); |
471 | } | 471 | } |
472 | 472 | ||
473 | // representation type (TYPE) | 473 | // representation type (TYPE) |
474 | else if ( cmd == "TYPE" ) { | 474 | else if ( cmd == "TYPE" ) { |
475 | if ( arg.upper() == "A" || arg.upper() == "I" ) | 475 | if ( arg.upper() == "A" || arg.upper() == "I" ) |
476 | send( "200 Command okay" ); | 476 | send( "200 Command okay" ); |
477 | else | 477 | else |
478 | send( "504 Command not implemented for that parameter" ); | 478 | send( "504 Command not implemented for that parameter" ); |
479 | } | 479 | } |
480 | 480 | ||
481 | // file structure (STRU) | 481 | // file structure (STRU) |
482 | else if ( cmd == "STRU" ) { | 482 | else if ( cmd == "STRU" ) { |
483 | if ( arg.upper() == "F" ) | 483 | if ( arg.upper() == "F" ) |
484 | send( "200 Command okay" ); | 484 | send( "200 Command okay" ); |
485 | else | 485 | else |
486 | send( "504 Command not implemented for that parameter" ); | 486 | send( "504 Command not implemented for that parameter" ); |
487 | } | 487 | } |
488 | 488 | ||
489 | // transfer mode (MODE) | 489 | // transfer mode (MODE) |
490 | else if ( cmd == "MODE" ) { | 490 | else if ( cmd == "MODE" ) { |
491 | if ( arg.upper() == "S" ) | 491 | if ( arg.upper() == "S" ) |
492 | send( "200 Command okay" ); | 492 | send( "200 Command okay" ); |
493 | else | 493 | else |
494 | send( "504 Command not implemented for that parameter" ); | 494 | send( "504 Command not implemented for that parameter" ); |