-rw-r--r-- | core/launcher/qcopbridge.cpp | 100 |
1 files changed, 52 insertions, 48 deletions
diff --git a/core/launcher/qcopbridge.cpp b/core/launcher/qcopbridge.cpp index 2d084fc..85993ee 100644 --- a/core/launcher/qcopbridge.cpp +++ b/core/launcher/qcopbridge.cpp | |||
@@ -1,106 +1,115 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of 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 | 20 | ||
21 | #include "qcopbridge.h" | 21 | #include "qcopbridge.h" |
22 | #include "transferserver.h" | 22 | #include "transferserver.h" |
23 | 23 | ||
24 | #ifdef QWS | ||
24 | #include <qpe/qcopenvelope_qws.h> | 25 | #include <qpe/qcopenvelope_qws.h> |
26 | #endif | ||
25 | #include <qpe/qpeapplication.h> | 27 | #include <qpe/qpeapplication.h> |
28 | #include <qpe/version.h> | ||
26 | 29 | ||
27 | #include <qdir.h> | 30 | #include <qdir.h> |
28 | #include <qfile.h> | 31 | #include <qfile.h> |
29 | #include <qtextstream.h> | 32 | #include <qtextstream.h> |
30 | #include <qdatastream.h> | 33 | #include <qdatastream.h> |
31 | #include <qstringlist.h> | 34 | #include <qstringlist.h> |
32 | #include <qfileinfo.h> | 35 | #include <qfileinfo.h> |
33 | #include <qregexp.h> | 36 | #include <qregexp.h> |
37 | #ifdef QWS | ||
34 | #include <qcopchannel_qws.h> | 38 | #include <qcopchannel_qws.h> |
39 | #endif | ||
35 | 40 | ||
36 | // actually this is wrong, _XOPEN_SOURCE should get defined on the commandline | ||
37 | // and it should have a proper value assigned. (Simon) | ||
38 | #if !defined(_XOPEN_SOURCE) | ||
39 | #define _XOPEN_SOURCE | 41 | #define _XOPEN_SOURCE |
40 | #endif | ||
41 | #include <pwd.h> | 42 | #include <pwd.h> |
42 | #include <sys/types.h> | 43 | #include <sys/types.h> |
43 | #include <unistd.h> | 44 | #include <unistd.h> |
44 | 45 | ||
45 | #if defined(_OS_LINUX_) | 46 | #if defined(_OS_LINUX_) |
46 | #include <shadow.h> | 47 | #include <shadow.h> |
47 | #endif | 48 | #endif |
48 | 49 | ||
49 | //#define INSECURE | 50 | //#define INSECURE |
50 | 51 | ||
51 | const int block_size = 51200; | 52 | const int block_size = 51200; |
52 | 53 | ||
53 | QCopBridge::QCopBridge( Q_UINT16 port, QObject *parent, | 54 | QCopBridge::QCopBridge( Q_UINT16 port, QObject *parent, |
54 | const char* name ) | 55 | const char* name ) |
55 | : QServerSocket( port, 1, parent, name ), | 56 | : QServerSocket( port, 1, parent, name ), |
56 | desktopChannel( 0 ), | 57 | desktopChannel( 0 ), |
57 | cardChannel( 0 ) | 58 | cardChannel( 0 ) |
58 | { | 59 | { |
59 | if ( !ok() ) | 60 | if ( !ok() ) |
60 | qWarning( "Failed to bind to port %d", port ); | 61 | qWarning( "Failed to bind to port %d", port ); |
61 | else { | 62 | else { |
63 | #ifndef QT_NO_COP | ||
62 | desktopChannel = new QCopChannel( "QPE/Desktop", this ); | 64 | desktopChannel = new QCopChannel( "QPE/Desktop", this ); |
63 | connect( desktopChannel, SIGNAL(received(const QCString &, const QByteArray &)), | 65 | connect( desktopChannel, SIGNAL(received(const QCString &, const QByteArray &)), |
64 | this, SLOT(desktopMessage( const QCString &, const QByteArray &)) ); | 66 | this, SLOT(desktopMessage( const QCString &, const QByteArray &)) ); |
65 | cardChannel = new QCopChannel( "QPE/Card", this ); | 67 | cardChannel = new QCopChannel( "QPE/Card", this ); |
66 | connect( cardChannel, SIGNAL(received(const QCString &, const QByteArray &)), | 68 | connect( cardChannel, SIGNAL(received(const QCString &, const QByteArray &)), |
67 | this, SLOT(desktopMessage( const QCString &, const QByteArray &)) ); | 69 | this, SLOT(desktopMessage( const QCString &, const QByteArray &)) ); |
70 | #endif | ||
68 | } | 71 | } |
69 | sendSync = FALSE; | 72 | sendSync = FALSE; |
70 | } | 73 | } |
71 | 74 | ||
72 | QCopBridge::~QCopBridge() | 75 | QCopBridge::~QCopBridge() |
73 | { | 76 | { |
77 | #ifndef QT_NO_COP | ||
74 | delete desktopChannel; | 78 | delete desktopChannel; |
79 | #endif | ||
75 | } | 80 | } |
76 | 81 | ||
77 | void QCopBridge::newConnection( int socket ) | 82 | void QCopBridge::newConnection( int socket ) |
78 | { | 83 | { |
79 | QCopBridgePI *pi = new QCopBridgePI( socket, this ); | 84 | QCopBridgePI *pi = new QCopBridgePI( socket, this ); |
80 | openConnections.append( pi ); | 85 | openConnections.append( pi ); |
81 | connect ( pi, SIGNAL( connectionClosed( QCopBridgePI *) ), this, SLOT( connectionClosed( QCopBridgePI *) ) ); | 86 | connect ( pi, SIGNAL( connectionClosed( QCopBridgePI *) ), this, SLOT( connectionClosed( QCopBridgePI *) ) ); |
87 | #ifndef QT_NO_COP | ||
82 | QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::DisableSuspend; | 88 | QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::DisableSuspend; |
89 | #endif | ||
83 | 90 | ||
84 | if ( sendSync ) { | 91 | if ( sendSync ) { |
85 | pi ->startSync(); | 92 | pi ->startSync(); |
86 | sendSync = FALSE; | 93 | sendSync = FALSE; |
87 | } | 94 | } |
88 | } | 95 | } |
89 | 96 | ||
90 | void QCopBridge::connectionClosed( QCopBridgePI *pi ) | 97 | void QCopBridge::connectionClosed( QCopBridgePI *pi ) |
91 | { | 98 | { |
92 | openConnections.remove( pi ); | 99 | openConnections.remove( pi ); |
93 | if ( openConnections.count() == 0 ) { | 100 | if ( openConnections.count() == 0 ) { |
101 | #ifndef QT_NO_COP | ||
94 | QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::Enable; | 102 | QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::Enable; |
103 | #endif | ||
95 | } | 104 | } |
96 | } | 105 | } |
97 | 106 | ||
98 | void QCopBridge::closeOpenConnections() | 107 | void QCopBridge::closeOpenConnections() |
99 | { | 108 | { |
100 | QCopBridgePI *pi; | 109 | QCopBridgePI *pi; |
101 | for ( pi = openConnections.first(); pi != 0; pi = openConnections.next() ) | 110 | for ( pi = openConnections.first(); pi != 0; pi = openConnections.next() ) |
102 | pi->close(); | 111 | pi->close(); |
103 | } | 112 | } |
104 | 113 | ||
105 | 114 | ||
106 | void QCopBridge::desktopMessage( const QCString &command, const QByteArray &args ) | 115 | void QCopBridge::desktopMessage( const QCString &command, const QByteArray &args ) |
@@ -133,32 +142,50 @@ void QCopBridge::desktopMessage( const QCString &command, const QByteArray &args | |||
133 | QCString cstr; | 142 | QCString cstr; |
134 | stream >> cstr; | 143 | stream >> cstr; |
135 | str = QString::fromLocal8Bit( cstr ); | 144 | str = QString::fromLocal8Bit( cstr ); |
136 | } else if ( *it == "int" ) { | 145 | } else if ( *it == "int" ) { |
137 | int i; | 146 | int i; |
138 | stream >> i; | 147 | stream >> i; |
139 | str = QString::number( i ); | 148 | str = QString::number( i ); |
140 | } else if ( *it == "bool" ) { | 149 | } else if ( *it == "bool" ) { |
141 | int i; | 150 | int i; |
142 | stream >> i; | 151 | stream >> i; |
143 | str = QString::number( i ); | 152 | str = QString::number( i ); |
144 | } else { | 153 | } else { |
145 | qDebug(" cannot route the argument type %s through the qcop bridge", (*it).latin1() ); | 154 | qDebug(" cannot route the argument type %s throught the qcop bridge", (*it).latin1() ); |
146 | return; | 155 | return; |
147 | } | 156 | } |
148 | str.replace( QRegExp("&"), "&" ); | 157 | QString estr; |
149 | str.replace( QRegExp(" "), "&0x20;" ); | 158 | for (int i=0; i<(int)str.length(); i++) { |
150 | str.replace( QRegExp("\n"), "&0x0d;" ); | 159 | QChar ch = str[i]; |
151 | str.replace( QRegExp("\r"), "&0x0a;" ); | 160 | if ( ch.row() ) |
152 | data += " " + str; | 161 | goto quick; |
162 | switch (ch.cell()) { | ||
163 | case '&': | ||
164 | estr.append( "&" ); | ||
165 | break; | ||
166 | case ' ': | ||
167 | estr.append( "&0x20;" ); | ||
168 | break; | ||
169 | case '\n': | ||
170 | estr.append( "&0x0d;" ); | ||
171 | break; | ||
172 | case '\r': | ||
173 | estr.append( "&0x0a;" ); | ||
174 | break; | ||
175 | default: quick: | ||
176 | estr.append(ch); | ||
177 | } | ||
178 | } | ||
179 | data += " " + estr; | ||
153 | } | 180 | } |
154 | } | 181 | } |
155 | QString sendCommand = QString(command.data()) + data; | 182 | QString sendCommand = QString(command.data()) + data; |
156 | // send the command to all open connections | 183 | // send the command to all open connections |
157 | if ( command == "startSync()" ) { | 184 | if ( command == "startSync()" ) { |
158 | // we need to buffer it a bit | 185 | // we need to buffer it a bit |
159 | sendSync = TRUE; | 186 | sendSync = TRUE; |
160 | startTimer( 20000 ); | 187 | startTimer( 20000 ); |
161 | } | 188 | } |
162 | 189 | ||
163 | QCopBridgePI *pi; | 190 | QCopBridgePI *pi; |
164 | for ( pi = openConnections.first(); pi != 0; pi = openConnections.next() ) { | 191 | for ( pi = openConnections.first(); pi != 0; pi = openConnections.next() ) { |
@@ -173,36 +200,41 @@ void QCopBridge::timerEvent( QTimerEvent * ) | |||
173 | } | 200 | } |
174 | 201 | ||
175 | 202 | ||
176 | QCopBridgePI::QCopBridgePI( int socket, QObject *parent, const char* name ) | 203 | QCopBridgePI::QCopBridgePI( int socket, QObject *parent, const char* name ) |
177 | : QSocket( parent, name ) | 204 | : QSocket( parent, name ) |
178 | { | 205 | { |
179 | setSocket( socket ); | 206 | setSocket( socket ); |
180 | 207 | ||
181 | peerport = peerPort(); | 208 | peerport = peerPort(); |
182 | peeraddress = peerAddress(); | 209 | peeraddress = peerAddress(); |
183 | 210 | ||
184 | #ifndef INSECURE | 211 | #ifndef INSECURE |
185 | if ( !accessAuthorized(peeraddress) ) { | 212 | if ( !SyncAuthentication::isAuthorized(peeraddress) ) { |
186 | state = Forbidden; | 213 | state = Forbidden; |
187 | startTimer( 0 ); | 214 | startTimer( 0 ); |
188 | } else | 215 | } else |
189 | #endif | 216 | #endif |
190 | { | 217 | { |
191 | state = Connected; | 218 | state = Connected; |
192 | sendSync = FALSE; | 219 | sendSync = FALSE; |
193 | connect( this, SIGNAL( readyRead() ), SLOT( read() ) ); | 220 | connect( this, SIGNAL( readyRead() ), SLOT( read() ) ); |
194 | connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); | 221 | connect( this, SIGNAL( connectionClosed() ), SLOT( connectionClosed() ) ); |
195 | 222 | ||
196 | send( "220 Qtopia QCop bridge ready!" ); | 223 | QString intro="220 Qtopia "; |
224 | intro += QPE_VERSION; intro += ";"; | ||
225 | intro += "challenge="; intro += SyncAuthentication::serverId(); intro += ";"; | ||
226 | intro += "loginname="; intro += SyncAuthentication::loginName(); intro += ";"; | ||
227 | intro += "displayname="; intro += SyncAuthentication::ownerName(); intro += ";"; | ||
228 | send( intro ); | ||
197 | state = Wait_USER; | 229 | state = Wait_USER; |
198 | 230 | ||
199 | // idle timer to close connections when not used anymore | 231 | // idle timer to close connections when not used anymore |
200 | startTimer( 60000 ); | 232 | startTimer( 60000 ); |
201 | connected = TRUE; | 233 | connected = TRUE; |
202 | } | 234 | } |
203 | } | 235 | } |
204 | 236 | ||
205 | 237 | ||
206 | QCopBridgePI::~QCopBridgePI() | 238 | QCopBridgePI::~QCopBridgePI() |
207 | { | 239 | { |
208 | 240 | ||
@@ -226,55 +258,24 @@ void QCopBridgePI::send( const QString& msg ) | |||
226 | { | 258 | { |
227 | QTextStream os( this ); | 259 | QTextStream os( this ); |
228 | os << msg << endl; | 260 | os << msg << endl; |
229 | //qDebug( "sending qcop message: %s", msg.latin1() ); | 261 | //qDebug( "sending qcop message: %s", msg.latin1() ); |
230 | } | 262 | } |
231 | 263 | ||
232 | void QCopBridgePI::read() | 264 | void QCopBridgePI::read() |
233 | { | 265 | { |
234 | while ( canReadLine() ) | 266 | while ( canReadLine() ) |
235 | process( readLine().stripWhiteSpace() ); | 267 | process( readLine().stripWhiteSpace() ); |
236 | } | 268 | } |
237 | 269 | ||
238 | bool QCopBridgePI::checkUser( const QString& user ) | ||
239 | { | ||
240 | if ( user.isEmpty() ) return FALSE; | ||
241 | |||
242 | struct passwd *pw; | ||
243 | pw = getpwuid( geteuid() ); | ||
244 | QString euser = QString::fromLocal8Bit( pw->pw_name ); | ||
245 | return user == euser; | ||
246 | } | ||
247 | |||
248 | bool QCopBridgePI::checkPassword( const QString& password ) | ||
249 | { | ||
250 | // ### HACK for testing on local host | ||
251 | return true; | ||
252 | |||
253 | /* | ||
254 | struct passwd *pw = 0; | ||
255 | struct spwd *spw = 0; | ||
256 | |||
257 | pw = getpwuid( geteuid() ); | ||
258 | spw = getspnam( pw->pw_name ); | ||
259 | |||
260 | QString cpwd = QString::fromLocal8Bit( pw->pw_passwd ); | ||
261 | if ( cpwd == "x" && spw ) | ||
262 | cpwd = QString::fromLocal8Bit( spw->sp_pwdp ); | ||
263 | |||
264 | QString cpassword = QString::fromLocal8Bit( crypt( password.local8Bit(), cpwd.local8Bit() ) ); | ||
265 | return cpwd == cpassword; | ||
266 | */ | ||
267 | } | ||
268 | |||
269 | void QCopBridgePI::process( const QString& message ) | 270 | void QCopBridgePI::process( const QString& message ) |
270 | { | 271 | { |
271 | //qDebug( "Command: %s", message.latin1() ); | 272 | //qDebug( "Command: %s", message.latin1() ); |
272 | 273 | ||
273 | // split message using "," as separator | 274 | // split message using "," as separator |
274 | QStringList msg = QStringList::split( " ", message ); | 275 | QStringList msg = QStringList::split( " ", message ); |
275 | if ( msg.isEmpty() ) return; | 276 | if ( msg.isEmpty() ) return; |
276 | 277 | ||
277 | // command token | 278 | // command token |
278 | QString cmd = msg[0].upper(); | 279 | QString cmd = msg[0].upper(); |
279 | 280 | ||
280 | // argument token | 281 | // argument token |
@@ -287,38 +288,37 @@ void QCopBridgePI::process( const QString& message ) | |||
287 | send( "211 Have a nice day!" ); | 288 | send( "211 Have a nice day!" ); |
288 | delete this; | 289 | delete this; |
289 | return; | 290 | return; |
290 | } | 291 | } |
291 | 292 | ||
292 | // connected to client | 293 | // connected to client |
293 | if ( Connected == state ) | 294 | if ( Connected == state ) |
294 | return; | 295 | return; |
295 | 296 | ||
296 | // waiting for user name | 297 | // waiting for user name |
297 | if ( Wait_USER == state ) { | 298 | if ( Wait_USER == state ) { |
298 | 299 | ||
299 | if ( cmd != "USER" || msg.count() < 2 || !checkUser( arg ) ) { | 300 | if ( cmd != "USER" || msg.count() < 2 || !SyncAuthentication::checkUser( arg ) ) { |
300 | send( "530 Please login with USER and PASS" ); | 301 | send( "530 Please login with USER and PASS" ); |
301 | return; | 302 | return; |
302 | } | 303 | } |
303 | send( "331 User name ok, need password" ); | 304 | send( "331 User name ok, need password" ); |
304 | state = Wait_PASS; | 305 | state = Wait_PASS; |
305 | return; | 306 | return; |
306 | } | 307 | } |
307 | 308 | ||
308 | // waiting for password | 309 | // waiting for password |
309 | if ( Wait_PASS == state ) { | 310 | if ( Wait_PASS == state ) { |
310 | 311 | ||
311 | if ( cmd != "PASS" || !checkPassword( arg ) ) { | 312 | if ( cmd != "PASS" || !SyncAuthentication::checkPassword( arg ) ) { |
312 | //if ( cmd != "PASS" || msg.count() < 2 || !checkPassword( arg ) ) { | ||
313 | send( "530 Please login with USER and PASS" ); | 313 | send( "530 Please login with USER and PASS" ); |
314 | return; | 314 | return; |
315 | } | 315 | } |
316 | send( "230 User logged in, proceed" ); | 316 | send( "230 User logged in, proceed" ); |
317 | state = Ready; | 317 | state = Ready; |
318 | if ( sendSync ) { | 318 | if ( sendSync ) { |
319 | sendDesktopMessage( "startSync()" ); | 319 | sendDesktopMessage( "startSync()" ); |
320 | sendSync = FALSE; | 320 | sendSync = FALSE; |
321 | } | 321 | } |
322 | return; | 322 | return; |
323 | } | 323 | } |
324 | 324 | ||
@@ -380,37 +380,41 @@ void QCopBridgePI::process( const QString& message ) | |||
380 | ds << arg.local8Bit(); | 380 | ds << arg.local8Bit(); |
381 | else if ( *it == "int" ) | 381 | else if ( *it == "int" ) |
382 | ds << arg.toInt(); | 382 | ds << arg.toInt(); |
383 | else if ( *it == "bool" ) | 383 | else if ( *it == "bool" ) |
384 | ds << arg.toInt(); | 384 | ds << arg.toInt(); |
385 | else { | 385 | else { |
386 | send( "500 Syntax error, command unrecognized" ); | 386 | send( "500 Syntax error, command unrecognized" ); |
387 | return; | 387 | return; |
388 | } | 388 | } |
389 | msgId++; | 389 | msgId++; |
390 | } | 390 | } |
391 | 391 | ||
392 | #ifndef QT_NO_COP | ||
392 | if ( !QCopChannel::isRegistered( channel.latin1() ) ) { | 393 | if ( !QCopChannel::isRegistered( channel.latin1() ) ) { |
393 | // send message back about it | 394 | // send message back about it |
394 | QString answer = "599 ChannelNotRegistered " + channel; | 395 | QString answer = "599 ChannelNotRegistered " + channel; |
395 | send( answer ); | 396 | send( answer ); |
396 | return; | 397 | return; |
397 | } | 398 | } |
399 | #endif | ||
398 | 400 | ||
401 | #ifndef QT_NO_COP | ||
399 | if ( paramList.count() ) | 402 | if ( paramList.count() ) |
400 | QCopChannel::send( channel.latin1(), command.latin1(), buffer ); | 403 | QCopChannel::send( channel.latin1(), command.latin1(), buffer ); |
401 | else | 404 | else |
402 | QCopChannel::send( channel.latin1(), command.latin1() ); | 405 | QCopChannel::send( channel.latin1(), command.latin1() ); |
403 | 406 | ||
404 | send( "200 Command okay" ); | 407 | send( "200 Command okay" ); |
408 | #endif | ||
405 | } | 409 | } |
406 | } | 410 | } |
407 | // not implemented | 411 | // not implemented |
408 | else | 412 | else |
409 | send( "502 Command not implemented" ); | 413 | send( "502 Command not implemented" ); |
410 | } | 414 | } |
411 | 415 | ||
412 | 416 | ||
413 | 417 | ||
414 | void QCopBridgePI::timerEvent( QTimerEvent * ) | 418 | void QCopBridgePI::timerEvent( QTimerEvent * ) |
415 | { | 419 | { |
416 | if ( connected ) | 420 | if ( connected ) |