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