author | zecke <zecke> | 2005-03-02 19:42:04 (UTC) |
---|---|---|
committer | zecke <zecke> | 2005-03-02 19:42:04 (UTC) |
commit | a349b5d092c392aba28c6fbd99221559c33bc5e1 (patch) (unidiff) | |
tree | 56a809f42d20c6f2fe7a60f243dd1aebca359b39 | |
parent | 9f3e0913c7b01c1e14f907e756999b9d9bf9ceef (diff) | |
download | opie-a349b5d092c392aba28c6fbd99221559c33bc5e1.zip opie-a349b5d092c392aba28c6fbd99221559c33bc5e1.tar.gz opie-a349b5d092c392aba28c6fbd99221559c33bc5e1.tar.bz2 |
Close the Filedescriptor for the QCopBridge and Transferserver on exit
-rw-r--r-- | core/launcher/qcopbridge.cpp | 4 | ||||
-rw-r--r-- | core/launcher/transferserver.cpp | 5 |
2 files changed, 7 insertions, 2 deletions
diff --git a/core/launcher/qcopbridge.cpp b/core/launcher/qcopbridge.cpp index e339dc7..64eb096 100644 --- a/core/launcher/qcopbridge.cpp +++ b/core/launcher/qcopbridge.cpp | |||
@@ -1,257 +1,259 @@ | |||
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 | 20 | ||
21 | #include "qcopbridge.h" | 21 | #include "qcopbridge.h" |
22 | #include "transferserver.h" | 22 | #include "transferserver.h" |
23 | 23 | ||
24 | /* OPIE */ | 24 | /* OPIE */ |
25 | #include <opie2/odebug.h> | 25 | #include <opie2/odebug.h> |
26 | #include <opie2/oglobal.h> | 26 | #include <opie2/oglobal.h> |
27 | #ifdef Q_WS_QWS | 27 | #ifdef Q_WS_QWS |
28 | #include <qtopia/qcopenvelope_qws.h> | 28 | #include <qtopia/qcopenvelope_qws.h> |
29 | #endif | 29 | #endif |
30 | #include <qtopia/qpeapplication.h> | 30 | #include <qtopia/qpeapplication.h> |
31 | #include <qtopia/version.h> | 31 | #include <qtopia/version.h> |
32 | using namespace Opie::Core; | 32 | using namespace Opie::Core; |
33 | 33 | ||
34 | /* QT */ | 34 | /* QT */ |
35 | #include <qtextstream.h> | 35 | #include <qtextstream.h> |
36 | #include <qtimer.h> | 36 | #include <qtimer.h> |
37 | #ifdef Q_WS_QWS | 37 | #ifdef Q_WS_QWS |
38 | #include <qcopchannel_qws.h> | 38 | #include <qcopchannel_qws.h> |
39 | #endif | 39 | #endif |
40 | 40 | ||
41 | /* STD */ | 41 | /* STD */ |
42 | #ifndef _XOPEN_SOURCE | 42 | #ifndef _XOPEN_SOURCE |
43 | #define _XOPEN_SOURCE | 43 | #define _XOPEN_SOURCE |
44 | #endif | 44 | #endif |
45 | #ifndef Q_OS_WIN32 | 45 | #ifndef Q_OS_WIN32 |
46 | #include <pwd.h> | 46 | #include <pwd.h> |
47 | #include <unistd.h> | 47 | #include <unistd.h> |
48 | #include <fcntl.h> | ||
48 | #include <sys/types.h> | 49 | #include <sys/types.h> |
49 | #endif | 50 | #endif |
50 | 51 | ||
51 | #if defined(_OS_LINUX_) | 52 | #if defined(_OS_LINUX_) |
52 | #include <shadow.h> | 53 | #include <shadow.h> |
53 | #endif | 54 | #endif |
54 | 55 | ||
55 | const int block_size = 51200; | 56 | const int block_size = 51200; |
56 | 57 | ||
57 | QCopBridge::QCopBridge( Q_UINT16 port, QObject *parent, | 58 | QCopBridge::QCopBridge( Q_UINT16 port, QObject *parent, |
58 | const char* name ) | 59 | const char* name ) |
59 | : QServerSocket( port, 1, parent, name ), | 60 | : QServerSocket( port, 1, parent, name ), |
60 | desktopChannel( 0 ), | 61 | desktopChannel( 0 ), |
61 | cardChannel( 0 ) | 62 | cardChannel( 0 ) |
62 | { | 63 | { |
63 | if ( !ok() ) | 64 | if ( !ok() ) |
64 | owarn << "Failed to bind to port " << port << "" << oendl; | 65 | owarn << "Failed to bind to port " << port << "" << oendl; |
65 | else { | 66 | else { |
67 | ::fcntl( socket(), F_SETFD, FD_CLOEXEC ); | ||
66 | #ifndef QT_NO_COP | 68 | #ifndef QT_NO_COP |
67 | desktopChannel = new QCopChannel( "QPE/Desktop", this ); | 69 | desktopChannel = new QCopChannel( "QPE/Desktop", this ); |
68 | connect( desktopChannel, SIGNAL(received(const QCString&,const QByteArray&)), | 70 | connect( desktopChannel, SIGNAL(received(const QCString&,const QByteArray&)), |
69 | this, SLOT(desktopMessage(const QCString&,const QByteArray&)) ); | 71 | this, SLOT(desktopMessage(const QCString&,const QByteArray&)) ); |
70 | cardChannel = new QCopChannel( "QPE/Card", this ); | 72 | cardChannel = new QCopChannel( "QPE/Card", this ); |
71 | connect( cardChannel, SIGNAL(received(const QCString&,const QByteArray&)), | 73 | connect( cardChannel, SIGNAL(received(const QCString&,const QByteArray&)), |
72 | this, SLOT(desktopMessage(const QCString&,const QByteArray&)) ); | 74 | this, SLOT(desktopMessage(const QCString&,const QByteArray&)) ); |
73 | #endif | 75 | #endif |
74 | } | 76 | } |
75 | sendSync = FALSE; | 77 | sendSync = FALSE; |
76 | openConnections.setAutoDelete( TRUE ); | 78 | openConnections.setAutoDelete( TRUE ); |
77 | authorizeConnections(); | 79 | authorizeConnections(); |
78 | } | 80 | } |
79 | 81 | ||
80 | QCopBridge::~QCopBridge() | 82 | QCopBridge::~QCopBridge() |
81 | { | 83 | { |
82 | #ifndef QT_NO_COP | 84 | #ifndef QT_NO_COP |
83 | delete desktopChannel; | 85 | delete desktopChannel; |
84 | #endif | 86 | #endif |
85 | } | 87 | } |
86 | 88 | ||
87 | void QCopBridge::authorizeConnections() | 89 | void QCopBridge::authorizeConnections() |
88 | { | 90 | { |
89 | Config cfg("Security"); | 91 | Config cfg("Security"); |
90 | cfg.setGroup("SyncMode"); | 92 | cfg.setGroup("SyncMode"); |
91 | m_mode = Mode(cfg.readNumEntry("Mode", Sharp )); | 93 | m_mode = Mode(cfg.readNumEntry("Mode", Sharp )); |
92 | QListIterator<QCopBridgePI> it(openConnections); | 94 | QListIterator<QCopBridgePI> it(openConnections); |
93 | while ( it.current() ) { | 95 | while ( it.current() ) { |
94 | if ( !it.current()->verifyAuthorised() ) { | 96 | if ( !it.current()->verifyAuthorised() ) { |
95 | disconnect ( it.current(), SIGNAL( connectionClosed(QCopBridgePI*) ), this, SLOT( closed(QCopBridgePI*) ) ); | 97 | disconnect ( it.current(), SIGNAL( connectionClosed(QCopBridgePI*) ), this, SLOT( closed(QCopBridgePI*) ) ); |
96 | openConnections.removeRef( it.current() ); | 98 | openConnections.removeRef( it.current() ); |
97 | } else | 99 | } else |
98 | ++it; | 100 | ++it; |
99 | } | 101 | } |
100 | } | 102 | } |
101 | 103 | ||
102 | void QCopBridge::newConnection( int socket ) | 104 | void QCopBridge::newConnection( int socket ) |
103 | { | 105 | { |
104 | QCopBridgePI *pi = new QCopBridgePI( socket, this ); | 106 | QCopBridgePI *pi = new QCopBridgePI( socket, this ); |
105 | openConnections.append( pi ); | 107 | openConnections.append( pi ); |
106 | connect ( pi, SIGNAL( connectionClosed(QCopBridgePI*) ), this, SLOT( closed(QCopBridgePI*) ) ); | 108 | connect ( pi, SIGNAL( connectionClosed(QCopBridgePI*) ), this, SLOT( closed(QCopBridgePI*) ) ); |
107 | 109 | ||
108 | /* ### libqtopia merge FIXME */ | 110 | /* ### libqtopia merge FIXME */ |
109 | #if 0 | 111 | #if 0 |
110 | QPEApplication::setTempScreenSaverMode( QPEApplication::DisableSuspend ); | 112 | QPEApplication::setTempScreenSaverMode( QPEApplication::DisableSuspend ); |
111 | #endif | 113 | #endif |
112 | #ifndef QT_NO_COP | 114 | #ifndef QT_NO_COP |
113 | QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::DisableSuspend; | 115 | QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::DisableSuspend; |
114 | #endif | 116 | #endif |
115 | 117 | ||
116 | if ( sendSync ) { | 118 | if ( sendSync ) { |
117 | pi ->startSync(); | 119 | pi ->startSync(); |
118 | sendSync = FALSE; | 120 | sendSync = FALSE; |
119 | } | 121 | } |
120 | } | 122 | } |
121 | 123 | ||
122 | void QCopBridge::closed( QCopBridgePI *pi ) | 124 | void QCopBridge::closed( QCopBridgePI *pi ) |
123 | { | 125 | { |
124 | emit connectionClosed( pi->peerAddress() ); | 126 | emit connectionClosed( pi->peerAddress() ); |
125 | openConnections.removeRef( pi ); | 127 | openConnections.removeRef( pi ); |
126 | if ( openConnections.count() == 0 ) { | 128 | if ( openConnections.count() == 0 ) { |
127 | /* ### FIXME libqtopia merge */ | 129 | /* ### FIXME libqtopia merge */ |
128 | #if 0 | 130 | #if 0 |
129 | QPEApplication::setTempScreenSaverMode( QPEApplication::Enable ); | 131 | QPEApplication::setTempScreenSaverMode( QPEApplication::Enable ); |
130 | #endif | 132 | #endif |
131 | #ifndef QT_NO_COP | 133 | #ifndef QT_NO_COP |
132 | QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::Enable; | 134 | QCopEnvelope( "QPE/System", "setScreenSaverMode(int)" ) << QPEApplication::Enable; |
133 | #endif | 135 | #endif |
134 | } | 136 | } |
135 | } | 137 | } |
136 | 138 | ||
137 | void QCopBridge::closeOpenConnections() | 139 | void QCopBridge::closeOpenConnections() |
138 | { | 140 | { |
139 | QCopBridgePI *pi; | 141 | QCopBridgePI *pi; |
140 | for ( pi = openConnections.first(); pi != 0; pi = openConnections.next() ) | 142 | for ( pi = openConnections.first(); pi != 0; pi = openConnections.next() ) |
141 | pi->close(); | 143 | pi->close(); |
142 | } | 144 | } |
143 | 145 | ||
144 | 146 | ||
145 | void QCopBridge::desktopMessage( const QCString &command, const QByteArray &data ) | 147 | void QCopBridge::desktopMessage( const QCString &command, const QByteArray &data ) |
146 | { | 148 | { |
147 | if ( command == "startSync()" ) { | 149 | if ( command == "startSync()" ) { |
148 | // we need to buffer it a bit | 150 | // we need to buffer it a bit |
149 | sendSync = TRUE; | 151 | sendSync = TRUE; |
150 | startTimer( 20000 ); | 152 | startTimer( 20000 ); |
151 | } | 153 | } |
152 | 154 | ||
153 | if ( m_mode & Qtopia1_7 ) { | 155 | if ( m_mode & Qtopia1_7 ) { |
154 | // send the command to all open connections | 156 | // send the command to all open connections |
155 | QCopBridgePI *pi; | 157 | QCopBridgePI *pi; |
156 | for ( pi = openConnections.first(); pi != 0; pi = openConnections.next() ) { | 158 | for ( pi = openConnections.first(); pi != 0; pi = openConnections.next() ) { |
157 | pi->sendDesktopMessage( command, data ); | 159 | pi->sendDesktopMessage( command, data ); |
158 | } | 160 | } |
159 | } | 161 | } |
160 | if ( ( m_mode & Sharp ) || (m_mode & IntelliSync) ) | 162 | if ( ( m_mode & Sharp ) || (m_mode & IntelliSync) ) |
161 | sendDesktopMessageOld( command, data ); | 163 | sendDesktopMessageOld( command, data ); |
162 | } | 164 | } |
163 | 165 | ||
164 | #ifndef OPIE_NO_OLD_SYNC_CODE | 166 | #ifndef OPIE_NO_OLD_SYNC_CODE |
165 | /* | 167 | /* |
166 | * Old compat mode | 168 | * Old compat mode |
167 | */ | 169 | */ |
168 | void QCopBridge::sendDesktopMessageOld( const QCString& command, const QByteArray& args) { | 170 | void QCopBridge::sendDesktopMessageOld( const QCString& command, const QByteArray& args) { |
169 | command.stripWhiteSpace(); | 171 | command.stripWhiteSpace(); |
170 | 172 | ||
171 | int paren = command.find( "(" ); | 173 | int paren = command.find( "(" ); |
172 | if ( paren <= 0 ) { | 174 | if ( paren <= 0 ) { |
173 | odebug << "DesktopMessage: bad qcop syntax" << oendl; | 175 | odebug << "DesktopMessage: bad qcop syntax" << oendl; |
174 | return; | 176 | return; |
175 | } | 177 | } |
176 | 178 | ||
177 | QString params = command.mid( paren + 1 ); | 179 | QString params = command.mid( paren + 1 ); |
178 | if ( params[params.length()-1] != ')' ) { | 180 | if ( params[params.length()-1] != ')' ) { |
179 | odebug << "DesktopMessage: bad qcop syntax" << oendl; | 181 | odebug << "DesktopMessage: bad qcop syntax" << oendl; |
180 | return; | 182 | return; |
181 | } | 183 | } |
182 | 184 | ||
183 | params.truncate( params.length()-1 ); | 185 | params.truncate( params.length()-1 ); |
184 | 186 | ||
185 | QStringList paramList = QStringList::split( ",", params ); | 187 | QStringList paramList = QStringList::split( ",", params ); |
186 | QString data; | 188 | QString data; |
187 | if ( paramList.count() ) { | 189 | if ( paramList.count() ) { |
188 | QDataStream stream( args, IO_ReadOnly ); | 190 | QDataStream stream( args, IO_ReadOnly ); |
189 | for ( QStringList::Iterator it = paramList.begin(); it != paramList.end(); ++it ) { | 191 | for ( QStringList::Iterator it = paramList.begin(); it != paramList.end(); ++it ) { |
190 | QString str; | 192 | QString str; |
191 | if ( *it == "QString" ) { | 193 | if ( *it == "QString" ) { |
192 | stream >> str; | 194 | stream >> str; |
193 | } else if ( *it == "QCString" ) { | 195 | } else if ( *it == "QCString" ) { |
194 | QCString cstr; | 196 | QCString cstr; |
195 | stream >> cstr; | 197 | stream >> cstr; |
196 | str = QString::fromLocal8Bit( cstr ); | 198 | str = QString::fromLocal8Bit( cstr ); |
197 | } else if ( *it == "int" ) { | 199 | } else if ( *it == "int" ) { |
198 | int i; | 200 | int i; |
199 | stream >> i; | 201 | stream >> i; |
200 | str = QString::number( i ); | 202 | str = QString::number( i ); |
201 | } else if ( *it == "bool" ) { | 203 | } else if ( *it == "bool" ) { |
202 | int i; | 204 | int i; |
203 | stream >> i; | 205 | stream >> i; |
204 | str = QString::number( i ); | 206 | str = QString::number( i ); |
205 | } else { | 207 | } else { |
206 | odebug << " cannot route the argument type " << (*it) << " throught the qcop bridge" << oendl; | 208 | odebug << " cannot route the argument type " << (*it) << " throught the qcop bridge" << oendl; |
207 | return; | 209 | return; |
208 | } | 210 | } |
209 | QString estr; | 211 | QString estr; |
210 | for (int i=0; i<(int)str.length(); i++) { | 212 | for (int i=0; i<(int)str.length(); i++) { |
211 | QChar ch = str[i]; | 213 | QChar ch = str[i]; |
212 | if ( ch.row() ) | 214 | if ( ch.row() ) |
213 | goto quick; | 215 | goto quick; |
214 | switch (ch.cell()) { | 216 | switch (ch.cell()) { |
215 | case '&': | 217 | case '&': |
216 | estr.append( "&" ); | 218 | estr.append( "&" ); |
217 | break; | 219 | break; |
218 | case ' ': | 220 | case ' ': |
219 | estr.append( "&0x20;" ); | 221 | estr.append( "&0x20;" ); |
220 | break; | 222 | break; |
221 | case '\n': | 223 | case '\n': |
222 | estr.append( "&0x0d;" ); | 224 | estr.append( "&0x0d;" ); |
223 | break; | 225 | break; |
224 | case '\r': | 226 | case '\r': |
225 | estr.append( "&0x0a;" ); | 227 | estr.append( "&0x0a;" ); |
226 | break; | 228 | break; |
227 | default: quick: | 229 | default: quick: |
228 | estr.append(ch); | 230 | estr.append(ch); |
229 | } | 231 | } |
230 | } | 232 | } |
231 | data += " " + estr; | 233 | data += " " + estr; |
232 | } | 234 | } |
233 | } | 235 | } |
234 | QString sendCommand = QString(command.data()) + data; | 236 | QString sendCommand = QString(command.data()) + data; |
235 | 237 | ||
236 | 238 | ||
237 | // send the command to all open connections | 239 | // send the command to all open connections |
238 | QCopBridgePI *pi; | 240 | QCopBridgePI *pi; |
239 | for ( pi = openConnections.first(); pi != 0; pi = openConnections.next() ) | 241 | for ( pi = openConnections.first(); pi != 0; pi = openConnections.next() ) |
240 | pi->sendDesktopMessage( sendCommand ); | 242 | pi->sendDesktopMessage( sendCommand ); |
241 | 243 | ||
242 | } | 244 | } |
243 | #endif | 245 | #endif |
244 | 246 | ||
245 | 247 | ||
246 | void QCopBridge::timerEvent( QTimerEvent * ) | 248 | void QCopBridge::timerEvent( QTimerEvent * ) |
247 | { | 249 | { |
248 | sendSync = FALSE; | 250 | sendSync = FALSE; |
249 | killTimers(); | 251 | killTimers(); |
250 | } | 252 | } |
251 | 253 | ||
252 | 254 | ||
253 | QCopBridgePI::QCopBridgePI( int socket, QObject *parent, const char* name ) | 255 | QCopBridgePI::QCopBridgePI( int socket, QObject *parent, const char* name ) |
254 | : QSocket( parent, name ) | 256 | : QSocket( parent, name ) |
255 | { | 257 | { |
256 | setSocket( socket ); | 258 | setSocket( socket ); |
257 | 259 | ||
diff --git a/core/launcher/transferserver.cpp b/core/launcher/transferserver.cpp index 2b2e435..a219c0f 100644 --- a/core/launcher/transferserver.cpp +++ b/core/launcher/transferserver.cpp | |||
@@ -1,250 +1,253 @@ | |||
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 | #include "transferserver.h" | 20 | #include "transferserver.h" |
21 | 21 | ||
22 | /* OPIE */ | 22 | /* OPIE */ |
23 | #include <opie2/odebug.h> | 23 | #include <opie2/odebug.h> |
24 | #include <opie2/oglobal.h> | 24 | #include <opie2/oglobal.h> |
25 | #include <qtopia/qprocess.h> | 25 | #include <qtopia/qprocess.h> |
26 | #include <qtopia/process.h> | 26 | #include <qtopia/process.h> |
27 | #include <qtopia/private/contact.h> | 27 | #include <qtopia/private/contact.h> |
28 | #include <qtopia/version.h> | 28 | #include <qtopia/version.h> |
29 | #ifdef Q_WS_QWS | 29 | #ifdef Q_WS_QWS |
30 | #include <qtopia/qcopenvelope_qws.h> | 30 | #include <qtopia/qcopenvelope_qws.h> |
31 | #endif | 31 | #endif |
32 | using namespace Opie::Core; | 32 | using namespace Opie::Core; |
33 | 33 | ||
34 | /* QT */ | 34 | /* QT */ |
35 | #include <qtextstream.h> | 35 | #include <qtextstream.h> |
36 | #include <qmessagebox.h> | 36 | #include <qmessagebox.h> |
37 | 37 | ||
38 | /* STD */ | 38 | /* STD */ |
39 | #include <pwd.h> | 39 | #include <pwd.h> |
40 | #include <sys/types.h> | 40 | #include <sys/types.h> |
41 | #include <unistd.h> | 41 | #include <unistd.h> |
42 | #include <fcntl.h> | ||
42 | #include <stdlib.h> | 43 | #include <stdlib.h> |
43 | #include <time.h> | 44 | #include <time.h> |
44 | 45 | ||
45 | #ifndef Q_OS_MACX | 46 | #ifndef Q_OS_MACX |
46 | #include <shadow.h> | 47 | #include <shadow.h> |
47 | #include <crypt.h> | 48 | #include <crypt.h> |
48 | #endif /* Q_OS_MACX */ | 49 | #endif /* Q_OS_MACX */ |
49 | 50 | ||
50 | const int block_size = 51200; | 51 | const int block_size = 51200; |
51 | 52 | ||
52 | TransferServer::TransferServer( Q_UINT16 port, QObject *parent, | 53 | TransferServer::TransferServer( Q_UINT16 port, QObject *parent, |
53 | const char* name) | 54 | const char* name) |
54 | : QServerSocket( port, 1, parent, name ) | 55 | : QServerSocket( port, 1, parent, name ) |
55 | { | 56 | { |
56 | connections.setAutoDelete( TRUE ); | 57 | connections.setAutoDelete( TRUE ); |
57 | if ( !ok() ) | 58 | if ( !ok() ) |
58 | owarn << "Failed to bind to port " << port << "" << oendl; | 59 | owarn << "Failed to bind to port " << port << "" << oendl; |
60 | else | ||
61 | ::fcntl( socket(), F_SETFD, FD_CLOEXEC ); | ||
59 | } | 62 | } |
60 | 63 | ||
61 | void TransferServer::authorizeConnections() | 64 | void TransferServer::authorizeConnections() |
62 | { | 65 | { |
63 | QListIterator<ServerPI> it(connections); | 66 | QListIterator<ServerPI> it(connections); |
64 | while ( it.current() ) { | 67 | while ( it.current() ) { |
65 | if ( !it.current()->verifyAuthorised() ) { | 68 | if ( !it.current()->verifyAuthorised() ) { |
66 | disconnect( it.current(), SIGNAL(connectionClosed(ServerPI*)), this, SLOT( closed(ServerPI*)) ); | 69 | disconnect( it.current(), SIGNAL(connectionClosed(ServerPI*)), this, SLOT( closed(ServerPI*)) ); |
67 | connections.removeRef( it.current() ); | 70 | connections.removeRef( it.current() ); |
68 | } else | 71 | } else |
69 | ++it; | 72 | ++it; |
70 | } | 73 | } |
71 | } | 74 | } |
72 | 75 | ||
73 | void TransferServer::closed(ServerPI *item) | 76 | void TransferServer::closed(ServerPI *item) |
74 | { | 77 | { |
75 | connections.removeRef(item); | 78 | connections.removeRef(item); |
76 | } | 79 | } |
77 | 80 | ||
78 | TransferServer::~TransferServer() | 81 | TransferServer::~TransferServer() |
79 | { | 82 | { |
80 | } | 83 | } |
81 | 84 | ||
82 | void TransferServer::newConnection( int socket ) | 85 | void TransferServer::newConnection( int socket ) |
83 | { | 86 | { |
84 | ServerPI *ptr = new ServerPI( socket, this ); | 87 | ServerPI *ptr = new ServerPI( socket, this ); |
85 | connect( ptr, SIGNAL(connectionClosed(ServerPI*)), this, SLOT( closed(ServerPI*)) ); | 88 | connect( ptr, SIGNAL(connectionClosed(ServerPI*)), this, SLOT( closed(ServerPI*)) ); |
86 | connections.append( ptr ); | 89 | connections.append( ptr ); |
87 | } | 90 | } |
88 | 91 | ||
89 | QString SyncAuthentication::serverId() | 92 | QString SyncAuthentication::serverId() |
90 | { | 93 | { |
91 | Config cfg("Security"); | 94 | Config cfg("Security"); |
92 | cfg.setGroup("Sync"); | 95 | cfg.setGroup("Sync"); |
93 | QString r = cfg.readEntry("serverid"); | 96 | QString r = cfg.readEntry("serverid"); |
94 | 97 | ||
95 | if ( r.isEmpty() ) { | 98 | if ( r.isEmpty() ) { |
96 | r = OGlobal::generateUuid(); | 99 | r = OGlobal::generateUuid(); |
97 | cfg.writeEntry("serverid", r ); | 100 | cfg.writeEntry("serverid", r ); |
98 | } | 101 | } |
99 | return r; | 102 | return r; |
100 | } | 103 | } |
101 | 104 | ||
102 | QString SyncAuthentication::ownerName() | 105 | QString SyncAuthentication::ownerName() |
103 | { | 106 | { |
104 | QString vfilename = Global::applicationFileName("addressbook", | 107 | QString vfilename = Global::applicationFileName("addressbook", |
105 | "businesscard.vcf"); | 108 | "businesscard.vcf"); |
106 | if (QFile::exists(vfilename)) { | 109 | if (QFile::exists(vfilename)) { |
107 | Contact c; | 110 | Contact c; |
108 | c = Contact::readVCard( vfilename )[0]; | 111 | c = Contact::readVCard( vfilename )[0]; |
109 | return c.fullName(); | 112 | return c.fullName(); |
110 | } | 113 | } |
111 | 114 | ||
112 | return QString::null; | 115 | return QString::null; |
113 | } | 116 | } |
114 | 117 | ||
115 | QString SyncAuthentication::loginName() | 118 | QString SyncAuthentication::loginName() |
116 | { | 119 | { |
117 | struct passwd *pw = 0L; | 120 | struct passwd *pw = 0L; |
118 | #ifndef Q_OS_WIN32 | 121 | #ifndef Q_OS_WIN32 |
119 | pw = getpwuid( geteuid() ); | 122 | pw = getpwuid( geteuid() ); |
120 | return QString::fromLocal8Bit( pw->pw_name ); | 123 | return QString::fromLocal8Bit( pw->pw_name ); |
121 | #else | 124 | #else |
122 | //### revise | 125 | //### revise |
123 | return QString(); | 126 | return QString(); |
124 | #endif | 127 | #endif |
125 | } | 128 | } |
126 | 129 | ||
127 | int SyncAuthentication::isAuthorized(QHostAddress peeraddress) | 130 | int SyncAuthentication::isAuthorized(QHostAddress peeraddress) |
128 | { | 131 | { |
129 | Config cfg("Security"); | 132 | Config cfg("Security"); |
130 | cfg.setGroup("Sync"); | 133 | cfg.setGroup("Sync"); |
131 | // QString allowedstr = cfg.readEntry("auth_peer","192.168.1.0"); | 134 | // QString allowedstr = cfg.readEntry("auth_peer","192.168.1.0"); |
132 | uint auth_peer = cfg.readNumEntry("auth_peer", 0xc0a80100); | 135 | uint auth_peer = cfg.readNumEntry("auth_peer", 0xc0a80100); |
133 | 136 | ||
134 | // QHostAddress allowed; | 137 | // QHostAddress allowed; |
135 | // allowed.setAddress(allowedstr); | 138 | // allowed.setAddress(allowedstr); |
136 | // uint auth_peer = allowed.ip4Addr(); | 139 | // uint auth_peer = allowed.ip4Addr(); |
137 | uint auth_peer_bits = cfg.readNumEntry("auth_peer_bits", 24); | 140 | uint auth_peer_bits = cfg.readNumEntry("auth_peer_bits", 24); |
138 | uint mask = auth_peer_bits >= 32 // shifting by 32 is not defined | 141 | uint mask = auth_peer_bits >= 32 // shifting by 32 is not defined |
139 | ? 0xffffffff : (((1 << auth_peer_bits) - 1) << (32 - auth_peer_bits)); | 142 | ? 0xffffffff : (((1 << auth_peer_bits) - 1) << (32 - auth_peer_bits)); |
140 | 143 | ||
141 | return (peeraddress.ip4Addr() & mask) == auth_peer; | 144 | return (peeraddress.ip4Addr() & mask) == auth_peer; |
142 | } | 145 | } |
143 | 146 | ||
144 | bool SyncAuthentication::checkUser( const QString& user ) | 147 | bool SyncAuthentication::checkUser( const QString& user ) |
145 | { | 148 | { |
146 | if ( user.isEmpty() ) return FALSE; | 149 | if ( user.isEmpty() ) return FALSE; |
147 | QString euser = loginName(); | 150 | QString euser = loginName(); |
148 | return user == euser; | 151 | return user == euser; |
149 | } | 152 | } |
150 | 153 | ||
151 | bool SyncAuthentication::checkPassword( const QString& password ) | 154 | bool SyncAuthentication::checkPassword( const QString& password ) |
152 | { | 155 | { |
153 | #ifdef ALLOW_UNIX_USER_FTP | 156 | #ifdef ALLOW_UNIX_USER_FTP |
154 | // First, check system password... | 157 | // First, check system password... |
155 | 158 | ||
156 | struct passwd *pw = 0; | 159 | struct passwd *pw = 0; |
157 | struct spwd *spw = 0; | 160 | struct spwd *spw = 0; |
158 | 161 | ||
159 | pw = getpwuid( geteuid() ); | 162 | pw = getpwuid( geteuid() ); |
160 | spw = getspnam( pw->pw_name ); | 163 | spw = getspnam( pw->pw_name ); |
161 | 164 | ||
162 | QString cpwd = QString::fromLocal8Bit( pw->pw_passwd ); | 165 | QString cpwd = QString::fromLocal8Bit( pw->pw_passwd ); |
163 | if ( cpwd == "x" && spw ) | 166 | if ( cpwd == "x" && spw ) |
164 | cpwd = QString::fromLocal8Bit( spw->sp_pwdp ); | 167 | cpwd = QString::fromLocal8Bit( spw->sp_pwdp ); |
165 | 168 | ||
166 | // Note: some systems use more than crypt for passwords. | 169 | // Note: some systems use more than crypt for passwords. |
167 | QString cpassword = QString::fromLocal8Bit( crypt( password.local8Bit(), cpwd.local8Bit() ) ); | 170 | QString cpassword = QString::fromLocal8Bit( crypt( password.local8Bit(), cpwd.local8Bit() ) ); |
168 | if ( cpwd == cpassword ) | 171 | if ( cpwd == cpassword ) |
169 | return TRUE; | 172 | return TRUE; |
170 | #endif | 173 | #endif |
171 | 174 | ||
172 | static int lastdenial=0; | 175 | static int lastdenial=0; |
173 | static int denials=0; | 176 | static int denials=0; |
174 | int now = time(0); | 177 | int now = time(0); |
175 | 178 | ||
176 | Config cfg("Security"); | 179 | Config cfg("Security"); |
177 | cfg.setGroup("SyncMode"); | 180 | cfg.setGroup("SyncMode"); |
178 | int mode = cfg.readNumEntry("Mode", 0x02 ); | 181 | int mode = cfg.readNumEntry("Mode", 0x02 ); |
179 | 182 | ||
180 | //No pass word needed if the user really needs it | 183 | //No pass word needed if the user really needs it |
181 | if (mode & 0x04) { | 184 | if (mode & 0x04) { |
182 | QMessageBox unauth( | 185 | QMessageBox unauth( |
183 | tr("Sync Connection"), | 186 | tr("Sync Connection"), |
184 | tr("<qt><p>An unauthorized system is requesting access to this device." | 187 | tr("<qt><p>An unauthorized system is requesting access to this device." |
185 | "<p>You chose IntelliSync so you may I allow or deny this connection.</qt>" ), | 188 | "<p>You chose IntelliSync so you may I allow or deny this connection.</qt>" ), |
186 | QMessageBox::Warning, | 189 | QMessageBox::Warning, |
187 | QMessageBox::Ok, QMessageBox::Cancel|QMessageBox::Default, QMessageBox::NoButton, | 190 | QMessageBox::Ok, QMessageBox::Cancel|QMessageBox::Default, QMessageBox::NoButton, |
188 | 0, QString::null, TRUE, WStyle_StaysOnTop); | 191 | 0, QString::null, TRUE, WStyle_StaysOnTop); |
189 | unauth.setButtonText(QMessageBox::Ok, tr("Allow" ) ); | 192 | unauth.setButtonText(QMessageBox::Ok, tr("Allow" ) ); |
190 | unauth.setButtonText(QMessageBox::Cancel, tr("Deny")); | 193 | unauth.setButtonText(QMessageBox::Cancel, tr("Deny")); |
191 | switch( unauth.exec() ) { | 194 | switch( unauth.exec() ) { |
192 | case QMessageBox::Ok: | 195 | case QMessageBox::Ok: |
193 | return TRUE; | 196 | return TRUE; |
194 | break; | 197 | break; |
195 | case QMessageBox::Cancel: | 198 | case QMessageBox::Cancel: |
196 | default: | 199 | default: |
197 | denials++; | 200 | denials++; |
198 | lastdenial=now; | 201 | lastdenial=now; |
199 | return FALSE; | 202 | return FALSE; |
200 | } | 203 | } |
201 | } | 204 | } |
202 | 205 | ||
203 | // Detect old Qtopia Desktop (no password) and fail | 206 | // Detect old Qtopia Desktop (no password) and fail |
204 | if ( password.isEmpty() ) { | 207 | if ( password.isEmpty() ) { |
205 | if ( denials < 3 || now > lastdenial+600 ) { | 208 | if ( denials < 3 || now > lastdenial+600 ) { |
206 | QMessageBox unauth( | 209 | QMessageBox unauth( |
207 | tr("Sync Connection"), | 210 | tr("Sync Connection"), |
208 | tr("<p>An unauthorized system is requesting access to this device." | 211 | tr("<p>An unauthorized system is requesting access to this device." |
209 | "<p>If you are using a version of Qtopia Desktop older than 1.5.1, " | 212 | "<p>If you are using a version of Qtopia Desktop older than 1.5.1, " |
210 | "please upgrade or change the security setting to use IntelliSync." ), | 213 | "please upgrade or change the security setting to use IntelliSync." ), |
211 | QMessageBox::Warning, | 214 | QMessageBox::Warning, |
212 | QMessageBox::Cancel, QMessageBox::NoButton, QMessageBox::NoButton, | 215 | QMessageBox::Cancel, QMessageBox::NoButton, QMessageBox::NoButton, |
213 | 0, QString::null, TRUE, WStyle_StaysOnTop); | 216 | 0, QString::null, TRUE, WStyle_StaysOnTop); |
214 | unauth.setButtonText(QMessageBox::Cancel, tr("Deny")); | 217 | unauth.setButtonText(QMessageBox::Cancel, tr("Deny")); |
215 | unauth.exec(); | 218 | unauth.exec(); |
216 | 219 | ||
217 | denials++; | 220 | denials++; |
218 | lastdenial=now; | 221 | lastdenial=now; |
219 | } | 222 | } |
220 | return FALSE; | 223 | return FALSE; |
221 | 224 | ||
222 | } | 225 | } |
223 | 226 | ||
224 | // Second, check sync password... | 227 | // Second, check sync password... |
225 | 228 | ||
226 | static int lock=0; | 229 | static int lock=0; |
227 | if ( lock ) return FALSE; | 230 | if ( lock ) return FALSE; |
228 | 231 | ||
229 | ++lock; | 232 | ++lock; |
230 | 233 | ||
231 | /* | 234 | /* |
232 | * we need to support old Sync software and QtopiaDesktop | 235 | * we need to support old Sync software and QtopiaDesktop |
233 | */ | 236 | */ |
234 | if ( password.left(6) == "Qtopia" || password.left(6) == "rootme" ) { | 237 | if ( password.left(6) == "Qtopia" || password.left(6) == "rootme" ) { |
235 | cfg.setGroup("Sync"); | 238 | cfg.setGroup("Sync"); |
236 | QStringList pwds = cfg.readListEntry("Passwords",' '); | 239 | QStringList pwds = cfg.readListEntry("Passwords",' '); |
237 | for (QStringList::ConstIterator it=pwds.begin(); it!=pwds.end(); ++it) { | 240 | for (QStringList::ConstIterator it=pwds.begin(); it!=pwds.end(); ++it) { |
238 | #ifndef Q_OS_WIN32 | 241 | #ifndef Q_OS_WIN32 |
239 | QString cpassword = QString::fromLocal8Bit( | 242 | QString cpassword = QString::fromLocal8Bit( |
240 | crypt( password.mid(8).local8Bit(), (*it).left(2).latin1() ) ); | 243 | crypt( password.mid(8).local8Bit(), (*it).left(2).latin1() ) ); |
241 | #else | 244 | #else |
242 | // ### revise | 245 | // ### revise |
243 | QString cpassword(""); | 246 | QString cpassword(""); |
244 | #endif | 247 | #endif |
245 | if ( *it == cpassword ) { | 248 | if ( *it == cpassword ) { |
246 | lock--; | 249 | lock--; |
247 | return TRUE; | 250 | return TRUE; |
248 | } | 251 | } |
249 | } | 252 | } |
250 | 253 | ||