00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <time.h>
00026
00027 #include <qtimer.h>
00028
00029 #include <kapplication.h>
00030 #include <klocale.h>
00031 #include <kmessagebox.h>
00032 #include <kdebug.h>
00033 #include <kio/passdlg.h>
00034
00035 #ifdef Q_WS_X11
00036 #include <X11/X.h>
00037 #include <X11/Xlib.h>
00038 #endif
00039
00040 #include "kpasswdserver.h"
00041
00042 extern "C" {
00043 KDEDModule *create_kpasswdserver(const QCString &name)
00044 {
00045 return new KPasswdServer(name);
00046 }
00047 };
00048
00049 int
00050 KPasswdServer::AuthInfoList::compareItems(QPtrCollection::Item n1, QPtrCollection::Item n2)
00051 {
00052 if (!n1 || !n2)
00053 return 0;
00054
00055 AuthInfo *i1 = (AuthInfo *) n1;
00056 AuthInfo *i2 = (AuthInfo *) n2;
00057
00058 int l1 = i1->directory.length();
00059 int l2 = i2->directory.length();
00060
00061 if (l1 > l2)
00062 return -1;
00063 if (l1 < l2)
00064 return 1;
00065 return 0;
00066 }
00067
00068
00069 KPasswdServer::KPasswdServer(const QCString &name)
00070 : KDEDModule(name)
00071 {
00072 m_authDict.setAutoDelete(true);
00073 m_authPending.setAutoDelete(true);
00074 m_seqNr = 0;
00075 connect(this, SIGNAL(windowUnregistered(long)),
00076 this, SLOT(removeAuthForWindowId(long)));
00077 }
00078
00079 KPasswdServer::~KPasswdServer()
00080 {
00081 }
00082
00083 KIO::AuthInfo
00084 KPasswdServer::checkAuthInfo(KIO::AuthInfo info, long windowId)
00085 {
00086 kdDebug() << "KPasswdServer::checkAuthInfo: User= " << info.username
00087 << ", WindowId = " << windowId << endl;
00088
00089 QString key = createCacheKey(info);
00090
00091 Request *request = m_authPending.first();
00092 QString path2 = info.url.directory(false, false);
00093 for(; request; request = m_authPending.next())
00094 {
00095 if (request->key != key)
00096 continue;
00097
00098 if (info.verifyPath)
00099 {
00100 QString path1 = request->info.url.directory(false, false);
00101 if (!path2.startsWith(path1))
00102 continue;
00103 }
00104
00105 request = new Request;
00106 request->client = callingDcopClient();
00107 request->transaction = request->client->beginTransaction();
00108 request->key = key;
00109 request->info = info;
00110 m_authWait.append(request);
00111 return info;
00112 }
00113
00114 const AuthInfo *result = findAuthInfoItem(key, info);
00115 if (!result || result->isCanceled)
00116 {
00117 info.setModified(false);
00118 return info;
00119 }
00120
00121 updateAuthExpire(key, result, windowId, false);
00122
00123 return copyAuthInfo(result);
00124 }
00125
00126 KIO::AuthInfo
00127 KPasswdServer::queryAuthInfo(KIO::AuthInfo info, QString errorMsg, long windowId, long seqNr)
00128 {
00129 kdDebug() << "KPasswdServer::queryAuthInfo: User= " << info.username
00130 << ", Message= " << info.prompt << ", WindowId = " << windowId << endl;
00131 QString key = createCacheKey(info);
00132 Request *request = new Request;
00133 request->client = callingDcopClient();
00134 request->transaction = request->client->beginTransaction();
00135 request->key = key;
00136 request->info = info;
00137 request->windowId = windowId;
00138 request->seqNr = seqNr;
00139 request->errorMsg = errorMsg;
00140 m_authPending.append(request);
00141
00142 if (m_authPending.count() == 1)
00143 QTimer::singleShot(0, this, SLOT(processRequest()));
00144
00145 return info;
00146 }
00147
00148 void
00149 KPasswdServer::addAuthInfo(KIO::AuthInfo info, long windowId)
00150 {
00151 kdDebug() << "KPasswdServer::addAuthInfo: User= " << info.username
00152 << ", RealmValue= " << info.realmValue << ", WindowId = " << windowId << endl;
00153 QString key = createCacheKey(info);
00154
00155 m_seqNr++;
00156
00157 addAuthInfoItem(key, info, windowId, m_seqNr, false);
00158 }
00159
00160 void
00161 KPasswdServer::processRequest()
00162 {
00163 Request *request = m_authPending.first();
00164 if (!request)
00165 return;
00166
00167 KIO::AuthInfo &info = request->info;
00168
00169 kdDebug() << "KPasswdServer::processRequest: User= " << info.username
00170 << ", Message= " << info.prompt << endl;
00171
00172 const AuthInfo *result = findAuthInfoItem(request->key, request->info);
00173
00174 if (result && (request->seqNr < result->seqNr))
00175 {
00176 kdDebug() << "KPasswdServer::processRequest: auto retry!" << endl;
00177 if (result->isCanceled)
00178 {
00179 info.setModified(false);
00180 }
00181 else
00182 {
00183 updateAuthExpire(request->key, result, request->windowId, false);
00184 info = copyAuthInfo(result);
00185 }
00186 }
00187 else
00188 {
00189 m_seqNr++;
00190 bool askPw = true;
00191 if (result && !info.username.isEmpty() &&
00192 !request->errorMsg.isEmpty())
00193 {
00194 QString prompt = request->errorMsg;
00195 prompt += i18n(" Do you want to retry?");
00196 int dlgResult = KMessageBox::warningContinueCancel(0, prompt,
00197 i18n("Authentication"), i18n("Retry"));
00198 if (dlgResult != KMessageBox::Continue)
00199 askPw = false;
00200 }
00201
00202 int dlgResult = QDialog::Rejected;
00203 if (askPw)
00204 {
00205 KIO::PasswordDialog dlg( info.prompt, info.username, info.keepPassword );
00206 if (info.caption.isEmpty())
00207 dlg.setPlainCaption( i18n("Authorization Dialog") );
00208 else
00209 dlg.setPlainCaption( info.caption );
00210
00211 if ( !info.comment.isEmpty() )
00212 dlg.addCommentLine( info.commentLabel, info.comment );
00213
00214 if ( !info.password.isEmpty() )
00215 dlg.setPassword( info.password );
00216
00217 if (info.readOnly)
00218 dlg.setUserReadOnly( true );
00219
00220 XSetTransientForHint( qt_xdisplay(), dlg.winId(), request->windowId);
00221
00222 dlgResult = dlg.exec();
00223
00224 if (dlgResult == QDialog::Accepted)
00225 {
00226 info.username = dlg.username();
00227 info.password = dlg.password();
00228 info.keepPassword = dlg.keepPassword();
00229 }
00230 }
00231 if ( dlgResult != QDialog::Accepted )
00232 {
00233 addAuthInfoItem(request->key, info, 0, m_seqNr, true);
00234 info.setModified( false );
00235 }
00236 else
00237 {
00238 addAuthInfoItem(request->key, info, request->windowId, m_seqNr, false);
00239 info.setModified( true );
00240 }
00241 }
00242
00243 QCString replyType;
00244 QByteArray replyData;
00245
00246 QDataStream stream2(replyData, IO_WriteOnly);
00247 stream2 << info << m_seqNr;
00248 replyType = "KIO::AuthInfo";
00249 request->client->endTransaction( request->transaction,
00250 replyType, replyData);
00251
00252 m_authPending.remove((unsigned int) 0);
00253
00254
00255 for(Request *waitRequest = m_authWait.first();
00256 waitRequest; )
00257 {
00258 bool keepQueued = false;
00259 QString key = waitRequest->key;
00260
00261 request = m_authPending.first();
00262 QString path2 = waitRequest->info.url.directory(false, false);
00263 for(; request; request = m_authPending.next())
00264 {
00265 if (request->key != key)
00266 continue;
00267
00268 if (info.verifyPath)
00269 {
00270 QString path1 = request->info.url.directory(false, false);
00271 if (!path2.startsWith(path1))
00272 continue;
00273 }
00274
00275 keepQueued = true;
00276 break;
00277 }
00278 if (keepQueued)
00279 {
00280 waitRequest = m_authWait.next();
00281 }
00282 else
00283 {
00284 const AuthInfo *result = findAuthInfoItem(waitRequest->key, waitRequest->info);
00285
00286 QCString replyType;
00287 QByteArray replyData;
00288
00289 QDataStream stream2(replyData, IO_WriteOnly);
00290
00291 if (!result || result->isCanceled)
00292 {
00293 waitRequest->info.setModified(false);
00294 stream2 << waitRequest->info;
00295 }
00296 else
00297 {
00298 updateAuthExpire(waitRequest->key, result, waitRequest->windowId, false);
00299 KIO::AuthInfo info = copyAuthInfo(result);
00300 stream2 << info;
00301 }
00302
00303 replyType = "KIO::AuthInfo";
00304 waitRequest->client->endTransaction( waitRequest->transaction,
00305 replyType, replyData);
00306
00307 m_authWait.remove();
00308 waitRequest = m_authWait.current();
00309 }
00310 }
00311
00312 if (m_authPending.count())
00313 QTimer::singleShot(0, this, SLOT(processRequest()));
00314
00315 }
00316
00317 QString KPasswdServer::createCacheKey( const KIO::AuthInfo &info )
00318 {
00319 if( info.url.isMalformed() )
00320 return QString::null;
00321
00322
00323 QString key = info.url.protocol();
00324 key += '-';
00325 if (!info.url.user().isEmpty())
00326 {
00327 key += info.url.user();
00328 key += "@";
}
key += info.url.host();
int port = info.url.port();
if( port )
{
key += ':';
key += QString::number(port);
}
return key;
}
KIO::AuthInfo
KPasswdServer::copyAuthInfo(const AuthInfo *i)
{
KIO::AuthInfo result;
result.url = i->url;
result.username = i->username;
result.password = i->password;
result.realmValue = i->realmValue;
result.digestInfo = i->digestInfo;
result.setModified(true);
return result;
}
const KPasswdServer::AuthInfo *
KPasswdServer::findAuthInfoItem(const QString &key, const KIO::AuthInfo &info)
{
AuthInfoList *authList = m_authDict.find(key);
if (!authList)
return 0;
QString path2 = info.url.directory(false, false);
for(AuthInfo *current = authList->first();
current; )
{
if ((current->expire == AuthInfo::expTime) &&
(difftime(time(0), current->expireTime) > 0))
{
authList->remove();
current = authList->current();
continue;
}
if (info.verifyPath)
{
QString path1 = current->directory;
if (path2.startsWith(path1))
return current;
}
else
{
if (current->realmValue == info.realmValue)
return current; // TODO: Update directory info,
}
current = authList->next();
}
return 0;
}
void
KPasswdServer::removeAuthInfoItem(const QString &key, const KIO::AuthInfo &info)
{
AuthInfoList *authList = m_authDict.find(key);
if (!authList)
return;
for(AuthInfo *current = authList->first();
current; )
{
if (current->realmValue == info.realmValue)
{
authList->remove();
current = authList->current();
}
else
{
current = authList->next();
}
}
if (authList->isEmpty())
{
m_authDict.remove(key);
}
}
void
KPasswdServer::addAuthInfoItem(const QString &key, const KIO::AuthInfo &info, long windowId, long seqNr, bool canceled)
{
AuthInfoList *authList = m_authDict.find(key);
if (!authList)
{
authList = new AuthInfoList;
m_authDict.insert(key, authList);
}
AuthInfo *current = authList->first();
for(; current; current = authList->next())
{
if (current->realmValue == info.realmValue)
{
authList->take();
break;
}
}
if (!current)
{
current = new AuthInfo;
current->expire = AuthInfo::expTime;
kdDebug() << "Creating AuthInfo" << endl;
00329 }
00330 else
00331 {
00332 kdDebug() << "Updating AuthInfo" << endl;
00333 }
00334
00335 current->url = info.url;
00336 current->directory = info.url.directory(false, false);
00337 current->username = info.username;
00338 current->password = info.password;
00339 current->realmValue = info.realmValue;
00340 current->digestInfo = info.digestInfo;
00341 current->seqNr = seqNr;
00342 current->isCanceled = canceled;
00343
00344 updateAuthExpire(key, current, windowId, info.keepPassword && !canceled);
00345
00346
00347 authList->inSort(current);
00348 }
00349
00350 void
00351 KPasswdServer::updateAuthExpire(const QString &key, const AuthInfo *auth, long windowId, bool keep)
00352 {
00353 AuthInfo *current = const_cast<AuthInfo *>(auth);
00354 if (keep)
00355 {
00356 current->expire = AuthInfo::expNever;
00357 }
00358 else if (windowId && (current->expire != AuthInfo::expNever))
00359 {
00360 current->expire = AuthInfo::expWindowClose;
00361 if (!current->windowList.contains(windowId))
00362 current->windowList.append(windowId);
00363 }
00364 else if (current->expire == AuthInfo::expTime)
00365 {
00366 current->expireTime = time(0)+10;
00367 }
00368
00369
00370 if (windowId)
00371 {
00372 QStringList *keysChanged = mWindowIdList.find(windowId);
00373 if (!keysChanged)
00374 {
00375 keysChanged = new QStringList;
00376 mWindowIdList.insert(windowId, keysChanged);
00377 }
00378 if (!keysChanged->contains(key))
00379 keysChanged->append(key);
00380 }
00381 }
00382
00383 void
00384 KPasswdServer::removeAuthForWindowId(long windowId)
00385 {
00386 QStringList *keysChanged = mWindowIdList.find(windowId);
00387 if (!keysChanged) return;
00388
00389 for(QStringList::ConstIterator it = keysChanged->begin();
00390 it != keysChanged->end(); ++it)
00391 {
00392 QString key = *it;
00393 AuthInfoList *authList = m_authDict.find(key);
00394 if (!authList)
00395 continue;
00396
00397 AuthInfo *current = authList->first();
00398 for(; current; )
00399 {
00400 if (current->expire == AuthInfo::expWindowClose)
00401 {
00402 if (current->windowList.remove(windowId) && current->windowList.isEmpty())
00403 {
00404 authList->remove();
00405 current = authList->current();
00406 continue;
00407 }
00408 }
00409 current = authList->next();
00410 }
00411 }
00412 }
00413
00414 #include "kpasswdserver.moc"
00415