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