diff --git a/src/ioslaves/http/CMakeLists.txt b/src/ioslaves/http/CMakeLists.txt index 3ae45a28..acfbb744 100644 --- a/src/ioslaves/http/CMakeLists.txt +++ b/src/ioslaves/http/CMakeLists.txt @@ -1,95 +1,93 @@ project(kioslave-http) include(ECMMarkNonGuiExecutable) include(ConfigureChecks.cmake) configure_file(config-kioslave-http.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-kioslave-http.h ) find_package(X11) set(HAVE_X11 ${X11_FOUND}) if(GSSAPI_FOUND) set(HAVE_LIBGSSAPI 1) if(GSSAPI_FLAVOR STREQUAL "MIT") set(GSSAPI_MIT 1) else() set(GSSAPI_MIT 0) endif() include_directories( ${GSSAPI_INCS} ) else() set(HAVE_LIBGSSAPI 0) set(GSSAPI_MIT 0) endif() configure_file(config-gssapi.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-gssapi.h ) include_directories(${ZLIB_INCLUDE_DIR}) -remove_definitions(-DQT_NO_CAST_FROM_ASCII) - if (NOT KIOCORE_ONLY) add_subdirectory( kcookiejar ) endif() ########### next target ############### set(kio_http_cache_cleaner_SRCS http_cache_cleaner.cpp ) add_executable(kio_http_cache_cleaner ${kio_http_cache_cleaner_SRCS}) # Mark it as non-gui so we won't create an app bundle on Mac OS X ecm_mark_nongui_executable(kio_http_cache_cleaner) target_link_libraries(kio_http_cache_cleaner Qt5::DBus Qt5::Network # QLocalSocket ${ZLIB_LIBRARY} KF5::KIOCore # KProtocolManager KF5::I18n) install(TARGETS kio_http_cache_cleaner DESTINATION ${KDE_INSTALL_LIBEXECDIR_KF5} ) ########### next target ############### # kio/httpfilter/Makefile.am: httpfilter set(kio_http_PART_SRCS http.cpp httpauthentication.cpp httpfilter.cpp ) add_library(kio_http MODULE ${kio_http_PART_SRCS}) target_link_libraries(kio_http Qt5::DBus Qt5::Network # QLocalSocket etc. Qt5::Xml # QDom KF5::KIOCore KF5::KIONTLM KF5::Archive ${ZLIB_LIBRARY} KF5::I18n ) if(GSSAPI_FOUND) target_link_libraries(kio_http ${GSSAPI_LIBS} ) endif() set_target_properties(kio_http PROPERTIES OUTPUT_NAME "http") set_target_properties(kio_http PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/kf5/kio") install(TARGETS kio_http DESTINATION ${KDE_INSTALL_PLUGINDIR}/kf5/kio) ########### install files ############### install( FILES http_cache_cleaner.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR} ) diff --git a/src/ioslaves/http/http_cache_cleaner.cpp b/src/ioslaves/http/http_cache_cleaner.cpp index 3687dda2..d46b0b2b 100644 --- a/src/ioslaves/http/http_cache_cleaner.cpp +++ b/src/ioslaves/http/http_cache_cleaner.cpp @@ -1,850 +1,850 @@ /* This file is part of KDE Copyright (C) 1999-2000 Waldo Bastian (bastian@kde.org) Copyright (C) 2009 Andreas Hartmetz (ahartmetz@gmail.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ //---------------------------------------------------------------------------- // // KDE HTTP Cache cleanup tool #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include QDateTime g_currentDate; int g_maxCacheAge; qint64 g_maxCacheSize; static const char appFullName[] = "org.kio5.kio_http_cache_cleaner"; static const char appName[] = "kio_http_cache_cleaner"; // !START OF SYNC! // Keep the following in sync with the cache code in http.cpp static const int s_hashedUrlBits = 160; // this number should always be divisible by eight static const int s_hashedUrlNibbles = s_hashedUrlBits / 4; static const int s_hashedUrlBytes = s_hashedUrlBits / 8; static const char version[] = "A\n"; // never instantiated, on-disk / wire format only struct SerializedCacheFileInfo { // from http.cpp quint8 version[2]; quint8 compression; // for now fixed to 0 quint8 reserved; // for now; also alignment static const int useCountOffset = 4; qint32 useCount; qint64 servedDate; qint64 lastModifiedDate; qint64 expireDate; qint32 bytesCached; static const int size = 36; QString url; QString etag; QString mimeType; QStringList responseHeaders; // including status response like "HTTP 200 OK" }; struct MiniCacheFileInfo { // data from cache entry file, or from scoreboard file qint32 useCount; // from filesystem QDateTime lastUsedDate; qint64 sizeOnDisk; // we want to delete the least "useful" files and we'll have to sort a list for that... bool operator<(const MiniCacheFileInfo &other) const; void debugPrint() const { // qDebug() << "useCount:" << useCount // << "\nlastUsedDate:" << lastUsedDate.toString(Qt::ISODate) // << "\nsizeOnDisk:" << sizeOnDisk << '\n'; } }; struct CacheFileInfo : MiniCacheFileInfo { quint8 version[2]; quint8 compression; // for now fixed to 0 quint8 reserved; // for now; also alignment QDateTime servedDate; QDateTime lastModifiedDate; QDateTime expireDate; qint32 bytesCached; QString baseName; QString url; QString etag; QString mimeType; QStringList responseHeaders; // including status response like "HTTP 200 OK" void prettyPrint() const { QTextStream out(stdout, QIODevice::WriteOnly); out << "File " << baseName << " version " << version[0] << version[1]; out << "\n cached bytes " << bytesCached << " useCount " << useCount; out << "\n servedDate " << servedDate.toString(Qt::ISODate); out << "\n lastModifiedDate " << lastModifiedDate.toString(Qt::ISODate); out << "\n expireDate " << expireDate.toString(Qt::ISODate); out << "\n entity tag " << etag; out << "\n encoded URL " << url; out << "\n mimetype " << mimeType; out << "\nResponse headers follow...\n"; Q_FOREACH (const QString &h, responseHeaders) { out << h << '\n'; } } }; bool MiniCacheFileInfo::operator<(const MiniCacheFileInfo &other) const { const int thisUseful = useCount / qMax(lastUsedDate.secsTo(g_currentDate), qint64(1)); const int otherUseful = other.useCount / qMax(other.lastUsedDate.secsTo(g_currentDate), qint64(1)); return thisUseful < otherUseful; } bool CacheFileInfoPtrLessThan(const CacheFileInfo *cf1, const CacheFileInfo *cf2) { return *cf1 < *cf2; } enum OperationMode { CleanCache = 0, DeleteCache, FileInfo }; static bool readBinaryHeader(const QByteArray &d, CacheFileInfo *fi) { if (d.size() < SerializedCacheFileInfo::size) { // qDebug() << "readBinaryHeader(): file too small?"; return false; } QDataStream stream(d); stream.setVersion(QDataStream::Qt_4_5); stream >> fi->version[0]; stream >> fi->version[1]; if (fi->version[0] != version[0] || fi->version[1] != version[1]) { // qDebug() << "readBinaryHeader(): wrong magic bytes"; return false; } stream >> fi->compression; stream >> fi->reserved; stream >> fi->useCount; stream >> fi->servedDate; stream >> fi->lastModifiedDate; stream >> fi->expireDate; stream >> fi->bytesCached; return true; } static QString filenameFromUrl(const QByteArray &url) { QCryptographicHash hash(QCryptographicHash::Sha1); hash.addData(url); return QString::fromLatin1(hash.result().toHex()); } static QString cacheDir() { - return QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/kio_http"; + return QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1String("/kio_http"); } static QString filePath(const QString &baseName) { QString cacheDirName = cacheDir(); - if (!cacheDirName.endsWith('/')) { - cacheDirName.append('/'); + if (!cacheDirName.endsWith(QLatin1Char('/'))) { + cacheDirName.append(QLatin1Char('/')); } return cacheDirName + baseName; } static bool readLineChecked(QIODevice *dev, QByteArray *line) { *line = dev->readLine(8192); // if nothing read or the line didn't fit into 8192 bytes(!) if (line->isEmpty() || !line->endsWith('\n')) { return false; } // we don't actually want the newline! line->chop(1); return true; } static bool readTextHeader(QFile *file, CacheFileInfo *fi, OperationMode mode) { bool ok = true; QByteArray readBuf; ok = ok && readLineChecked(file, &readBuf); fi->url = QString::fromLatin1(readBuf); if (filenameFromUrl(readBuf) != QFileInfo(*file).baseName()) { // qDebug() << "You have witnessed a very improbable hash collision!"; return false; } // only read the necessary info for cache cleaning. Saves time and (more importantly) memory. if (mode != FileInfo) { return true; } ok = ok && readLineChecked(file, &readBuf); fi->etag = QString::fromLatin1(readBuf); ok = ok && readLineChecked(file, &readBuf); fi->mimeType = QString::fromLatin1(readBuf); // read as long as no error and no empty line found while (true) { ok = ok && readLineChecked(file, &readBuf); if (ok && !readBuf.isEmpty()) { fi->responseHeaders.append(QString::fromLatin1(readBuf)); } else { break; } } return ok; // it may still be false ;) } // TODO common include file with http.cpp? enum CacheCleanerCommand { InvalidCommand = 0, CreateFileNotificationCommand, UpdateFileCommand }; static bool readCacheFile(const QString &baseName, CacheFileInfo *fi, OperationMode mode) { QFile file(filePath(baseName)); if (!file.open(QIODevice::ReadOnly)) { return false; } fi->baseName = baseName; QByteArray header = file.read(SerializedCacheFileInfo::size); // do *not* modify/delete the file if we're in file info mode. if (!(readBinaryHeader(header, fi) && readTextHeader(&file, fi, mode)) && mode != FileInfo) { // qDebug() << "read(Text|Binary)Header() returned false, deleting file" << baseName; file.remove(); return false; } // get meta-information from the filesystem QFileInfo fileInfo(file); fi->lastUsedDate = fileInfo.lastModified(); fi->sizeOnDisk = fileInfo.size(); return true; } class Scoreboard; class CacheIndex { public: explicit CacheIndex(const QString &baseName) { QByteArray ba = baseName.toLatin1(); const int sz = ba.size(); const char *input = ba.constData(); Q_ASSERT(sz == s_hashedUrlNibbles); int translated = 0; for (int i = 0; i < sz; i++) { int c = input[i]; if (c >= '0' && c <= '9') { translated |= c - '0'; } else if (c >= 'a' && c <= 'f') { translated |= c - 'a' + 10; } else { Q_ASSERT(false); } if (i & 1) { // odd index m_index[i >> 1] = translated; translated = 0; } else { translated = translated << 4; } } computeHash(); } bool operator==(const CacheIndex &other) const { const bool isEqual = memcmp(m_index, other.m_index, s_hashedUrlBytes) == 0; if (isEqual) { Q_ASSERT(m_hash == other.m_hash); } return isEqual; } private: explicit CacheIndex(const QByteArray &index) { Q_ASSERT(index.length() >= s_hashedUrlBytes); memcpy(m_index, index.constData(), s_hashedUrlBytes); computeHash(); } void computeHash() { uint hash = 0; const int ints = s_hashedUrlBytes / sizeof(uint); for (int i = 0; i < ints; i++) { hash ^= reinterpret_cast(&m_index[0])[i]; } if (const int bytesLeft = s_hashedUrlBytes % sizeof(uint)) { // dead code until a new url hash algorithm or architecture with sizeof(uint) != 4 appears. // we have the luxury of ignoring endianness because the hash is never written to disk. // just merge the bits into the hash in some way. const int offset = ints * sizeof(uint); for (int i = 0; i < bytesLeft; i++) { hash ^= static_cast(m_index[offset + i]) << (i * 8); } } m_hash = hash; } friend uint qHash(const CacheIndex &); friend class Scoreboard; quint8 m_index[s_hashedUrlBytes]; // packed binary version of the hexadecimal name uint m_hash; }; uint qHash(const CacheIndex &ci) { return ci.m_hash; } static CacheCleanerCommand readCommand(const QByteArray &cmd, CacheFileInfo *fi) { readBinaryHeader(cmd, fi); QDataStream stream(cmd); stream.skipRawData(SerializedCacheFileInfo::size); quint32 ret; stream >> ret; QByteArray baseName; baseName.resize(s_hashedUrlNibbles); stream.readRawData(baseName.data(), s_hashedUrlNibbles); Q_ASSERT(stream.atEnd()); fi->baseName = QString::fromLatin1(baseName); Q_ASSERT(ret == CreateFileNotificationCommand || ret == UpdateFileCommand); return static_cast(ret); } // never instantiated, on-disk format only struct ScoreboardEntry { // from scoreboard file quint8 index[s_hashedUrlBytes]; static const int indexSize = s_hashedUrlBytes; qint32 useCount; // from scoreboard file, but compared with filesystem to see if scoreboard has current data qint64 lastUsedDate; qint32 sizeOnDisk; static const int size = 36; // we want to delete the least "useful" files and we'll have to sort a list for that... bool operator<(const MiniCacheFileInfo &other) const; }; class Scoreboard { public: Scoreboard() { // read in the scoreboard... QFile sboard(filePath(QStringLiteral("scoreboard"))); if (sboard.open(QIODevice::ReadOnly)) { while (true) { QByteArray baIndex = sboard.read(ScoreboardEntry::indexSize); QByteArray baRest = sboard.read(ScoreboardEntry::size - ScoreboardEntry::indexSize); if (baIndex.size() + baRest.size() != ScoreboardEntry::size) { break; } const QString entryBasename = QString::fromLatin1(baIndex.toHex()); MiniCacheFileInfo mcfi; if (readAndValidateMcfi(baRest, entryBasename, &mcfi)) { m_scoreboard.insert(CacheIndex(baIndex), mcfi); } } } } void writeOut() { // write out the scoreboard QFile sboard(filePath(QStringLiteral("scoreboard"))); if (!sboard.open(QIODevice::WriteOnly | QIODevice::Truncate)) { return; } QDataStream stream(&sboard); QHash::ConstIterator it = m_scoreboard.constBegin(); for (; it != m_scoreboard.constEnd(); ++it) { const char *indexData = reinterpret_cast(it.key().m_index); stream.writeRawData(indexData, s_hashedUrlBytes); stream << it.value().useCount; stream << it.value().lastUsedDate; stream << it.value().sizeOnDisk; } } bool fillInfo(const QString &baseName, MiniCacheFileInfo *mcfi) { QHash::ConstIterator it = m_scoreboard.constFind(CacheIndex(baseName)); if (it == m_scoreboard.constEnd()) { return false; } *mcfi = it.value(); return true; } qint64 runCommand(const QByteArray &cmd) { // execute the command; return number of bytes if a new file was created, zero otherwise. Q_ASSERT(cmd.size() == 80); CacheFileInfo fi; const CacheCleanerCommand ccc = readCommand(cmd, &fi); QString fileName = filePath(fi.baseName); switch (ccc) { case CreateFileNotificationCommand: // qDebug() << "CreateNotificationCommand for" << fi.baseName; if (!readBinaryHeader(cmd, &fi)) { return 0; } break; case UpdateFileCommand: { // qDebug() << "UpdateFileCommand for" << fi.baseName; QFile file(fileName); file.open(QIODevice::ReadWrite); CacheFileInfo fiFromDisk; QByteArray header = file.read(SerializedCacheFileInfo::size); if (!readBinaryHeader(header, &fiFromDisk) || fiFromDisk.bytesCached != fi.bytesCached) { return 0; } // adjust the use count, to make sure that we actually count up. (slaves read the file // asynchronously...) const quint32 newUseCount = fiFromDisk.useCount + 1; QByteArray newHeader = cmd.mid(0, SerializedCacheFileInfo::size); { QDataStream stream(&newHeader, QIODevice::WriteOnly); stream.skipRawData(SerializedCacheFileInfo::useCountOffset); stream << newUseCount; } file.seek(0); file.write(newHeader); file.close(); if (!readBinaryHeader(newHeader, &fi)) { return 0; } break; } default: // qDebug() << "received invalid command"; return 0; } QFileInfo fileInfo(fileName); fi.lastUsedDate = fileInfo.lastModified(); fi.sizeOnDisk = fileInfo.size(); fi.debugPrint(); // a CacheFileInfo is-a MiniCacheFileInfo which enables the following assignment... add(fi); // finally, return cache dir growth (only relevant if a file was actually created!) return ccc == CreateFileNotificationCommand ? fi.sizeOnDisk : 0; } void add(const CacheFileInfo &fi) { m_scoreboard[CacheIndex(fi.baseName)] = fi; } void remove(const QString &basename) { m_scoreboard.remove(CacheIndex(basename)); } // keep memory usage reasonably low - otherwise entries of nonexistent files don't hurt. void maybeRemoveStaleEntries(const QList &fiList) { // don't bother when there are a few bogus entries if (m_scoreboard.count() < fiList.count() + 100) { return; } // qDebug() << "we have too many fake/stale entries, cleaning up..."; QSet realFiles; Q_FOREACH (CacheFileInfo *fi, fiList) { realFiles.insert(CacheIndex(fi->baseName)); } QHash::Iterator it = m_scoreboard.begin(); while (it != m_scoreboard.end()) { if (realFiles.contains(it.key())) { ++it; } else { it = m_scoreboard.erase(it); } } } private: bool readAndValidateMcfi(const QByteArray &rawData, const QString &basename, MiniCacheFileInfo *mcfi) { QDataStream stream(rawData); stream >> mcfi->useCount; // check those against filesystem stream >> mcfi->lastUsedDate; stream >> mcfi->sizeOnDisk; QFileInfo fileInfo(filePath(basename)); if (!fileInfo.exists()) { return false; } bool ok = true; ok = ok && fileInfo.lastModified() == mcfi->lastUsedDate; ok = ok && fileInfo.size() == mcfi->sizeOnDisk; if (!ok) { // size or last-modified date not consistent with entry file; reload useCount // note that avoiding to open the file is the whole purpose of the scoreboard - we only // open the file if we really have to. QFile entryFile(fileInfo.absoluteFilePath()); if (!entryFile.open(QIODevice::ReadOnly)) { return false; } if (entryFile.size() < SerializedCacheFileInfo::size) { return false; } QDataStream stream(&entryFile); stream.skipRawData(SerializedCacheFileInfo::useCountOffset); stream >> mcfi->useCount; mcfi->lastUsedDate = fileInfo.lastModified(); mcfi->sizeOnDisk = fileInfo.size(); ok = true; } return ok; } QHash m_scoreboard; }; // Keep the above in sync with the cache code in http.cpp // !END OF SYNC! // remove files and directories used by earlier versions of the HTTP cache. static void removeOldFiles() { const char *oldDirs = "0abcdefghijklmnopqrstuvwxyz"; const int n = strlen(oldDirs); QDir cacheRootDir(filePath(QString())); for (int i = 0; i < n; i++) { QString dirName = QString::fromLatin1(&oldDirs[i], 1); // delete files in directory... Q_FOREACH (const QString &baseName, QDir(filePath(dirName)).entryList()) { - QFile::remove(filePath(dirName + '/' + baseName)); + QFile::remove(filePath(dirName + QLatin1Char('/') + baseName)); } // delete the (now hopefully empty!) directory itself cacheRootDir.rmdir(dirName); } QFile::remove(filePath(QStringLiteral("cleaned"))); } class CacheCleaner { public: CacheCleaner(const QDir &cacheDir) : m_totalSizeOnDisk(0) { // qDebug(); m_fileNameList = cacheDir.entryList(); } // Delete some of the files that need to be deleted. Return true when done, false otherwise. // This makes interleaved cleaning / serving ioslaves possible. bool processSlice(Scoreboard *scoreboard = nullptr) { QTime t; t.start(); // phase one: gather information about cache files if (!m_fileNameList.isEmpty()) { while (t.elapsed() < 100 && !m_fileNameList.isEmpty()) { QString baseName = m_fileNameList.takeFirst(); // check if the filename is of the $s_hashedUrlNibbles letters, 0...f type if (baseName.length() < s_hashedUrlNibbles) { continue; } bool nameOk = true; for (int i = 0; i < s_hashedUrlNibbles && nameOk; i++) { QChar c = baseName[i]; - nameOk = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); + nameOk = (c >= QLatin1Char('0') && c <= QLatin1Char('9')) || (c >= QLatin1Char('a') && c <= QLatin1Char('f')); } if (!nameOk) { continue; } if (baseName.length() > s_hashedUrlNibbles) { if (QFileInfo(filePath(baseName)).lastModified().secsTo(g_currentDate) > 15 * 60) { // it looks like a temporary file that hasn't been touched in > 15 minutes... QFile::remove(filePath(baseName)); } // the temporary file might still be written to, leave it alone continue; } CacheFileInfo *fi = new CacheFileInfo(); fi->baseName = baseName; bool gotInfo = false; if (scoreboard) { gotInfo = scoreboard->fillInfo(baseName, fi); } if (!gotInfo) { gotInfo = readCacheFile(baseName, fi, CleanCache); if (gotInfo && scoreboard) { scoreboard->add(*fi); } } if (gotInfo) { m_fiList.append(fi); m_totalSizeOnDisk += fi->sizeOnDisk; } else { delete fi; } } // qDebug() << "total size of cache files is" << m_totalSizeOnDisk; if (m_fileNameList.isEmpty()) { // final step of phase one std::sort(m_fiList.begin(), m_fiList.end(), CacheFileInfoPtrLessThan); } return false; } // phase two: delete files until cache is under maximum allowed size // TODO: delete files larger than allowed for a single file while (t.elapsed() < 100) { if (m_totalSizeOnDisk <= g_maxCacheSize || m_fiList.isEmpty()) { // qDebug() << "total size of cache files after cleaning is" << m_totalSizeOnDisk; if (scoreboard) { scoreboard->maybeRemoveStaleEntries(m_fiList); scoreboard->writeOut(); } qDeleteAll(m_fiList); m_fiList.clear(); return true; } CacheFileInfo *fi = m_fiList.takeFirst(); QString filename = filePath(fi->baseName); if (QFile::remove(filename)) { m_totalSizeOnDisk -= fi->sizeOnDisk; if (scoreboard) { scoreboard->remove(fi->baseName); } } delete fi; } return false; } private: QStringList m_fileNameList; QList m_fiList; qint64 m_totalSizeOnDisk; }; int main(int argc, char **argv) { QCoreApplication app(argc, argv); app.setApplicationVersion(QStringLiteral("5.0")); KLocalizedString::setApplicationDomain("kio5"); QCommandLineParser parser; parser.addVersionOption(); parser.setApplicationDescription(QCoreApplication::translate("main", "KDE HTTP cache maintenance tool")); parser.addHelpOption(); parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("clear-all"), QCoreApplication::translate("main", "Empty the cache"))); parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("file-info"), QCoreApplication::translate("main", "Display information about cache file"), QStringLiteral("filename"))); parser.process(app); OperationMode mode = CleanCache; if (parser.isSet(QStringLiteral("clear-all"))) { mode = DeleteCache; } else if (parser.isSet(QStringLiteral("file-info"))) { mode = FileInfo; } // file info mode: no scanning of directories, just output info and exit. if (mode == FileInfo) { CacheFileInfo fi; if (!readCacheFile(parser.value(QStringLiteral("file-info")), &fi, mode)) { return 1; } fi.prettyPrint(); return 0; } // make sure we're the only running instance of the cleaner service if (mode == CleanCache) { if (!QDBusConnection::sessionBus().isConnected()) { QDBusError error(QDBusConnection::sessionBus().lastError()); fprintf(stderr, "%s: Could not connect to D-Bus! (%s: %s)\n", appName, qPrintable(error.name()), qPrintable(error.message())); return 1; } - if (!QDBusConnection::sessionBus().registerService(appFullName)) { + if (!QDBusConnection::sessionBus().registerService(QString::fromLatin1(appFullName))) { fprintf(stderr, "%s: Already running!\n", appName); return 0; } } g_currentDate = QDateTime::currentDateTime(); g_maxCacheAge = KProtocolManager::maxCacheAge(); g_maxCacheSize = mode == DeleteCache ? -1 : KProtocolManager::maxCacheSize() * 1024; QString cacheDirName = cacheDir(); QDir().mkpath(cacheDirName); QDir cacheDir(cacheDirName); if (!cacheDir.exists()) { fprintf(stderr, "%s: '%s' does not exist.\n", appName, qPrintable(cacheDirName)); return 0; } removeOldFiles(); if (mode == DeleteCache) { QTime t; t.start(); cacheDir.refresh(); //qDebug() << "time to refresh the cacheDir QDir:" << t.elapsed(); CacheCleaner cleaner(cacheDir); while (!cleaner.processSlice()) { } return 0; } QLocalServer lServer; - QString socketFileName = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation) + QLatin1Char('/') + "kio_http_cache_cleaner"; + const QString socketFileName = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation) + QLatin1String("/kio_http_cache_cleaner"); // we need to create the file by opening the socket, otherwise it won't work QFile::remove(socketFileName); if (!lServer.listen(socketFileName)) { qWarning() << "Error listening on" << socketFileName; } QList sockets; qint64 newBytesCounter = LLONG_MAX; // force cleaner run on startup Scoreboard scoreboard; CacheCleaner *cleaner = nullptr; while (true) { g_currentDate = QDateTime::currentDateTime(); if (cleaner) { QCoreApplication::processEvents(QEventLoop::AllEvents, 100); } else { // We will not immediately know when a socket was disconnected. Causes: // - WaitForMoreEvents does not make processEvents() return when a socket disconnects // - WaitForMoreEvents *and* a timeout is not possible. QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents); } if (!lServer.isListening()) { return 1; } lServer.waitForNewConnection(1); while (QLocalSocket *sock = lServer.nextPendingConnection()) { sock->waitForConnected(); sockets.append(sock); } for (int i = 0; i < sockets.size(); i++) { QLocalSocket *sock = sockets[i]; if (sock->state() != QLocalSocket::ConnectedState) { if (sock->state() != QLocalSocket::UnconnectedState) { sock->waitForDisconnected(); } delete sock; sockets.removeAll(sock); i--; continue; } sock->waitForReadyRead(0); while (true) { QByteArray recv = sock->read(80); if (recv.isEmpty()) { break; } Q_ASSERT(recv.size() == 80); newBytesCounter += scoreboard.runCommand(recv); } } // interleave cleaning with serving ioslaves to reduce "garbage collection pauses" if (cleaner) { if (cleaner->processSlice(&scoreboard)) { // that was the last slice, done delete cleaner; cleaner = nullptr; } } else if (newBytesCounter > (g_maxCacheSize / 8)) { cacheDir.refresh(); cleaner = new CacheCleaner(cacheDir); newBytesCounter = 0; } } return 0; } diff --git a/src/ioslaves/http/httpauthentication.cpp b/src/ioslaves/http/httpauthentication.cpp index 56805657..eddf95cc 100644 --- a/src/ioslaves/http/httpauthentication.cpp +++ b/src/ioslaves/http/httpauthentication.cpp @@ -1,928 +1,928 @@ /* This file is part of the KDE libraries Copyright (C) 2008, 2009 Andreas Hartmetz This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "httpauthentication.h" #if HAVE_LIBGSSAPI #if GSSAPI_MIT #include #else #include #endif /* GSSAPI_MIT */ // Catch uncompatible crap (BR86019) #if defined(GSS_RFC_COMPLIANT_OIDS) && (GSS_RFC_COMPLIANT_OIDS == 0) #include #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name #endif #endif /* HAVE_LIBGSSAPI */ #include #include #include #include #include #include #include Q_LOGGING_CATEGORY(KIO_HTTP_AUTH, "kf5.kio.kio_http.auth") static bool isWhiteSpace(char ch) { return (ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f'); } static bool isWhiteSpaceOrComma(char ch) { return (ch == ',' || isWhiteSpace(ch)); } static bool containsScheme(const char input[], int start, int end) { // skip any comma or white space while (start < end && isWhiteSpaceOrComma(input[start])) { start++; } while (start < end) { if (isWhiteSpace(input[start])) { return true; } start++; } return false; } // keys on even indexes, values on odd indexes. Reduces code expansion for the templated // alternatives. // If "ba" starts with empty content it will be removed from ba to simplify later calls static QList parseChallenge(QByteArray &ba, QByteArray *scheme, QByteArray *nextAuth = nullptr) { QList values; const char *b = ba.constData(); int len = ba.count(); int start = 0, end = 0, pos = 0, pos2 = 0; // parse scheme while (start < len && isWhiteSpaceOrComma(b[start])) { start++; } end = start; while (end < len && !isWhiteSpace(b[end])) { end++; } // drop empty stuff from the given string, it would have to be skipped over and over again if (start != 0) { ba = ba.mid(start); end -= start; len -= start; start = 0; b = ba.constData(); } Q_ASSERT(scheme); *scheme = ba.left(end); while (end < len) { start = end; while (end < len && b[end] != '=') { end++; } pos = end; // save the end position while (end - 1 > start && isWhiteSpace(b[end - 1])) { // trim whitespace end--; } pos2 = start; while (pos2 < end && isWhiteSpace(b[pos2])) { // skip whitespace pos2++; } if (containsScheme(b, start, end) || (b[pos2] == ',' && b[pos] != '=' && pos == len)) { if (nextAuth) { *nextAuth = QByteArray(b + start); } break; // break on start of next scheme. } while (start < len && isWhiteSpaceOrComma(b[start])) { start++; } values.append(QByteArray(b + start, end - start)); end = pos; // restore the end position if (end == len) { break; } // parse value start = end + 1; //skip '=' while (start < len && isWhiteSpace(b[start])) { start++; } if (b[start] == '"') { //quoted string bool hasBs = false; bool hasErr = false; end = ++start; while (end < len) { if (b[end] == '\\') { end++; if (end + 1 >= len) { hasErr = true; break; } else { hasBs = true; end++; } } else if (b[end] == '"') { break; } else { end++; } } if (hasErr || (end == len)) { // remove the key we already inserted // qDebug() << "error in quoted text for key" << values.last(); values.removeLast(); break; } QByteArray value = QByteArray(b + start, end - start); if (hasBs) { // skip over the next character, it might be an escaped backslash int i = -1; while ((i = value.indexOf('\\', i + 1)) >= 0) { value.remove(i, 1); } } values.append(value); end++; } else { //unquoted string end = start; while (end < len && b[end] != ',' && !isWhiteSpace(b[end])) { end++; } values.append(QByteArray(b + start, end - start)); } //the quoted string has ended, but only a comma ends a key-value pair while (end < len && isWhiteSpace(b[end])) { end++; } // garbage, here should be end or field delimiter (comma) if (end < len && b[end] != ',') { // qDebug() << "unexpected character" << b[end] << "found in WWW-authentication header where token boundary (,) was expected"; break; } } // ensure every key has a value // WARNING: Do not remove the > 1 check or parsing a Type 1 NTLM // authentication challenge will surely fail. if (values.count() > 1 && values.count() % 2) { values.removeLast(); } return values; } static QByteArray valueForKey(const QList &ba, const QByteArray &key) { for (int i = 0, count = ba.count(); (i + 1) < count; i += 2) { if (ba[i] == key) { return ba[i + 1]; } } return QByteArray(); } KAbstractHttpAuthentication::KAbstractHttpAuthentication(KConfigGroup *config) : m_config(config), m_finalAuthStage(false) { reset(); } KAbstractHttpAuthentication::~KAbstractHttpAuthentication() { } QByteArray KAbstractHttpAuthentication::bestOffer(const QList &offers) { // choose the most secure auth scheme offered QByteArray negotiateOffer; QByteArray digestOffer; QByteArray ntlmOffer; QByteArray basicOffer; Q_FOREACH (const QByteArray &offer, offers) { const QByteArray scheme = offer.mid(0, offer.indexOf(' ')).toLower(); #if HAVE_LIBGSSAPI if (scheme == "negotiate") { // krazy:exclude=strings negotiateOffer = offer; } else #endif if (scheme == "digest") { // krazy:exclude=strings digestOffer = offer; } else if (scheme == "ntlm") { // krazy:exclude=strings ntlmOffer = offer; } else if (scheme == "basic") { // krazy:exclude=strings basicOffer = offer; } } if (!negotiateOffer.isEmpty()) { return negotiateOffer; } if (!digestOffer.isEmpty()) { return digestOffer; } if (!ntlmOffer.isEmpty()) { return ntlmOffer; } return basicOffer; //empty or not... } KAbstractHttpAuthentication *KAbstractHttpAuthentication::newAuth(const QByteArray &offer, KConfigGroup *config) { const QByteArray scheme = offer.mid(0, offer.indexOf(' ')).toLower(); #if HAVE_LIBGSSAPI if (scheme == "negotiate") { // krazy:exclude=strings return new KHttpNegotiateAuthentication(config); } else #endif if (scheme == "digest") { // krazy:exclude=strings return new KHttpDigestAuthentication(); } else if (scheme == "ntlm") { // krazy:exclude=strings return new KHttpNtlmAuthentication(config); } else if (scheme == "basic") { // krazy:exclude=strings return new KHttpBasicAuthentication(); } return nullptr; } QList< QByteArray > KAbstractHttpAuthentication::splitOffers(const QList< QByteArray > &offers) { // first detect if one entry may contain multiple offers QList alloffers; foreach (QByteArray offer, offers) { QByteArray scheme, cont; parseChallenge(offer, &scheme, &cont); while (!cont.isEmpty()) { offer.chop(cont.length()); alloffers << offer; offer = cont; cont.clear(); parseChallenge(offer, &scheme, &cont); } alloffers << offer; } return alloffers; } void KAbstractHttpAuthentication::reset() { m_scheme.clear(); m_challenge.clear(); m_challengeText.clear(); m_resource.clear(); m_httpMethod.clear(); m_isError = false; m_needCredentials = true; m_forceKeepAlive = false; m_forceDisconnect = false; m_keepPassword = false; m_headerFragment.clear(); m_username.clear(); m_password.clear(); } void KAbstractHttpAuthentication::setChallenge(const QByteArray &c, const QUrl &resource, const QByteArray &httpMethod) { reset(); m_challengeText = c.trimmed(); m_challenge = parseChallenge(m_challengeText, &m_scheme); Q_ASSERT(m_scheme.toLower() == scheme().toLower()); m_resource = resource; m_httpMethod = httpMethod; } QString KAbstractHttpAuthentication::realm() const { const QByteArray realm = valueForKey(m_challenge, "realm"); // TODO: Find out what this is supposed to address. The site mentioned below does not exist. if (QLocale().uiLanguages().contains(QStringLiteral("ru"))) { //for sites like lib.homelinux.org return QTextCodec::codecForName("CP1251")->toUnicode(realm); } return QString::fromLatin1(realm.constData(), realm.length()); } void KAbstractHttpAuthentication::authInfoBoilerplate(KIO::AuthInfo *a) const { a->url = m_resource; a->username = m_username; a->password = m_password; a->verifyPath = supportsPathMatching(); a->realmValue = realm(); a->digestInfo = QLatin1String(authDataToCache()); a->keepPassword = m_keepPassword; } void KAbstractHttpAuthentication::generateResponseCommon(const QString &user, const QString &password) { if (m_scheme.isEmpty() || m_httpMethod.isEmpty()) { m_isError = true; return; } if (m_needCredentials) { m_username = user; m_password = password; } m_isError = false; m_forceKeepAlive = false; m_forceDisconnect = false; m_finalAuthStage = true; } QByteArray KHttpBasicAuthentication::scheme() const { return "Basic"; } void KHttpBasicAuthentication::fillKioAuthInfo(KIO::AuthInfo *ai) const { authInfoBoilerplate(ai); } void KHttpBasicAuthentication::generateResponse(const QString &user, const QString &password) { generateResponseCommon(user, password); if (m_isError) { return; } m_headerFragment = "Basic "; m_headerFragment += QByteArray(m_username.toLatin1() + ':' + m_password.toLatin1()).toBase64(); m_headerFragment += "\r\n"; } QByteArray KHttpDigestAuthentication::scheme() const { return "Digest"; } void KHttpDigestAuthentication::setChallenge(const QByteArray &c, const QUrl &resource, const QByteArray &httpMethod) { QString oldUsername; QString oldPassword; if (valueForKey(m_challenge, "stale").toLower() == "true") { // stale nonce: the auth failure that triggered this round of authentication is an artifact // of digest authentication. the credentials are probably still good, so keep them. oldUsername = m_username; oldPassword = m_password; } KAbstractHttpAuthentication::setChallenge(c, resource, httpMethod); if (!oldUsername.isEmpty() && !oldPassword.isEmpty()) { // keep credentials *and* don't ask for new ones m_needCredentials = false; m_username = oldUsername; m_password = oldPassword; } } void KHttpDigestAuthentication::fillKioAuthInfo(KIO::AuthInfo *ai) const { authInfoBoilerplate(ai); } struct DigestAuthInfo { QByteArray nc; QByteArray qop; QByteArray realm; QByteArray nonce; QByteArray method; QByteArray cnonce; QByteArray username; QByteArray password; QList digestURIs; QByteArray algorithm; QByteArray entityBody; }; //calculateResponse() from the original HTTPProtocol static QByteArray calculateResponse(const DigestAuthInfo &info, const QUrl &resource) { QCryptographicHash md(QCryptographicHash::Md5); QByteArray HA1; QByteArray HA2; // Calculate H(A1) QByteArray authStr = info.username; authStr += ':'; authStr += info.realm; authStr += ':'; authStr += info.password; md.addData(authStr); if (info.algorithm.toLower() == "md5-sess") { authStr = md.result().toHex(); authStr += ':'; authStr += info.nonce; authStr += ':'; authStr += info.cnonce; md.reset(); md.addData(authStr); } HA1 = md.result().toHex(); // qDebug() << "A1 => " << HA1; // Calculate H(A2) authStr = info.method; authStr += ':'; authStr += resource.path(QUrl::FullyEncoded).toLatin1(); if (resource.hasQuery()) { authStr += '?' + resource.query(QUrl::FullyEncoded).toLatin1(); } if (info.qop == "auth-int") { authStr += ':'; md.reset(); md.addData(info.entityBody); authStr += md.result().toHex(); } md.reset(); md.addData(authStr); HA2 = md.result().toHex(); // qDebug() << "A2 => " << HA2; // Calculate the response. authStr = HA1; authStr += ':'; authStr += info.nonce; authStr += ':'; if (!info.qop.isEmpty()) { authStr += info.nc; authStr += ':'; authStr += info.cnonce; authStr += ':'; authStr += info.qop; authStr += ':'; } authStr += HA2; md.reset(); md.addData(authStr); const QByteArray response = md.result().toHex(); // qDebug() << "Response =>" << response; return response; } void KHttpDigestAuthentication::generateResponse(const QString &user, const QString &password) { generateResponseCommon(user, password); if (m_isError) { return; } // magic starts here (this part is slightly modified from the original in HTTPProtocol) DigestAuthInfo info; info.username = m_username.toLatin1(); //### charset breakage info.password = m_password.toLatin1(); //### // info.entityBody = p; // FIXME: send digest of data for POST action ?? info.realm = ""; info.nonce = ""; info.qop = ""; // cnonce is recommended to contain about 64 bits of entropy #ifdef ENABLE_HTTP_AUTH_NONCE_SETTER info.cnonce = m_nonce; #else info.cnonce = KRandom::randomString(16).toLatin1(); #endif // HACK: Should be fixed according to RFC 2617 section 3.2.2 info.nc = "00000001"; // Set the method used... info.method = m_httpMethod; // Parse the Digest response.... info.realm = valueForKey(m_challenge, "realm"); info.algorithm = valueForKey(m_challenge, "algorithm"); if (info.algorithm.isEmpty()) { info.algorithm = valueForKey(m_challenge, "algorith"); } if (info.algorithm.isEmpty()) { info.algorithm = "MD5"; } Q_FOREACH (const QByteArray &path, valueForKey(m_challenge, "domain").split(' ')) { - QUrl u = m_resource.resolved(QUrl(path)); + QUrl u = m_resource.resolved(QUrl(QString::fromUtf8(path))); if (u.isValid()) { info.digestURIs.append(u); } } info.nonce = valueForKey(m_challenge, "nonce"); QByteArray opaque = valueForKey(m_challenge, "opaque"); info.qop = valueForKey(m_challenge, "qop"); // NOTE: Since we do not have access to the entity body, we cannot support // the "auth-int" qop value ; so if the server returns a comma separated // list of qop values, prefer "auth".See RFC 2617 sec 3.2.2 for the details. // If "auth" is not present or it is set to "auth-int" only, then we simply // print a warning message and disregard the qop option altogether. if (info.qop.contains(',')) { const QList values = info.qop.split(','); if (info.qop.contains("auth")) { info.qop = "auth"; } else { qCWarning(KIO_HTTP_AUTH) << "Unsupported digest authentication qop parameters:" << values; info.qop.clear(); } } else if (info.qop == "auth-int") { qCWarning(KIO_HTTP_AUTH) << "Unsupported digest authentication qop parameter:" << info.qop; info.qop.clear(); } if (info.realm.isEmpty() || info.nonce.isEmpty()) { // ### proper error return m_isError = true; return; } // If the "domain" attribute was not specified and the current response code // is authentication needed, add the current request url to the list over which // this credential can be automatically applied. if (info.digestURIs.isEmpty() /*###&& (m_request.responseCode == 401 || m_request.responseCode == 407)*/) { info.digestURIs.append(m_resource); } else { // Verify whether or not we should send a cached credential to the // server based on the stored "domain" attribute... bool send = true; // Determine the path of the request url... QString requestPath = m_resource.adjusted(QUrl::RemoveFilename).path(); if (requestPath.isEmpty()) { requestPath = QLatin1Char('/'); } Q_FOREACH (const QUrl &u, info.digestURIs) { send &= (m_resource.scheme().toLower() == u.scheme().toLower()); send &= (m_resource.host().toLower() == u.host().toLower()); if (m_resource.port() > 0 && u.port() > 0) { send &= (m_resource.port() == u.port()); } QString digestPath = u.adjusted(QUrl::RemoveFilename).path(); if (digestPath.isEmpty()) { digestPath = QLatin1Char('/'); } send &= (requestPath.startsWith(digestPath)); if (send) { break; } } if (!send) { m_isError = true; return; } } // qDebug() << "RESULT OF PARSING:"; // qDebug() << " algorithm: " << info.algorithm; // qDebug() << " realm: " << info.realm; // qDebug() << " nonce: " << info.nonce; // qDebug() << " opaque: " << opaque; // qDebug() << " qop: " << info.qop; // Calculate the response... const QByteArray response = calculateResponse(info, m_resource); QByteArray auth = "Digest username=\""; auth += info.username; auth += "\", realm=\""; auth += info.realm; auth += "\""; auth += ", nonce=\""; auth += info.nonce; auth += "\", uri=\""; auth += m_resource.path(QUrl::FullyEncoded).toLatin1(); if (m_resource.hasQuery()) { auth += '?' + m_resource.query(QUrl::FullyEncoded).toLatin1(); } if (!info.algorithm.isEmpty()) { auth += "\", algorithm="; auth += info.algorithm; } if (!info.qop.isEmpty()) { auth += ", qop="; auth += info.qop; auth += ", cnonce=\""; auth += info.cnonce; auth += "\", nc="; auth += info.nc; } auth += ", response=\""; auth += response; if (!opaque.isEmpty()) { auth += "\", opaque=\""; auth += opaque; } auth += "\"\r\n"; // magic ends here // note that auth already contains \r\n m_headerFragment = auth; } #ifdef ENABLE_HTTP_AUTH_NONCE_SETTER void KHttpDigestAuthentication::setDigestNonceValue(const QByteArray &nonce) { m_nonce = nonce; } #endif QByteArray KHttpNtlmAuthentication::scheme() const { return "NTLM"; } void KHttpNtlmAuthentication::setChallenge(const QByteArray &c, const QUrl &resource, const QByteArray &httpMethod) { QString oldUsername, oldPassword; if (!m_finalAuthStage && !m_username.isEmpty() && !m_password.isEmpty()) { oldUsername = m_username; oldPassword = m_password; } KAbstractHttpAuthentication::setChallenge(c, resource, httpMethod); if (!oldUsername.isEmpty() && !oldPassword.isEmpty()) { m_username = oldUsername; m_password = oldPassword; } // The type 1 message we're going to send needs no credentials; // they come later in the type 3 message. m_needCredentials = !m_challenge.isEmpty(); } void KHttpNtlmAuthentication::fillKioAuthInfo(KIO::AuthInfo *ai) const { authInfoBoilerplate(ai); // Every auth scheme is supposed to supply a realm according to the RFCs. Of course this doesn't // prevent Microsoft from not doing it... Dummy value! // we don't have the username yet which may (may!) contain a domain, so we really have no choice ai->realmValue = QStringLiteral("NTLM"); } void KHttpNtlmAuthentication::generateResponse(const QString &_user, const QString &password) { generateResponseCommon(_user, password); if (m_isError) { return; } QByteArray buf; if (m_challenge.isEmpty()) { m_finalAuthStage = false; // first, send type 1 message (with empty domain, workstation..., but it still works) switch (m_stage1State) { case Init: if (!KNTLM::getNegotiate(buf)) { qCWarning(KIO_HTTP_AUTH) << "Error while constructing Type 1 NTLMv1 authentication request"; m_isError = true; return; } m_stage1State = SentNTLMv1; break; case SentNTLMv1: if (!KNTLM::getNegotiate(buf, QString(), QString(), KNTLM::Negotiate_NTLM2_Key | KNTLM::Negotiate_Always_Sign | KNTLM::Negotiate_Unicode | KNTLM::Request_Target | KNTLM::Negotiate_NTLM)) { qCWarning(KIO_HTTP_AUTH) << "Error while constructing Type 1 NTLMv2 authentication request"; m_isError = true; return; } m_stage1State = SentNTLMv2; break; default: qCWarning(KIO_HTTP_AUTH) << "Error - Type 1 NTLM already sent - no Type 2 response received."; m_isError = true; return; } } else { m_finalAuthStage = true; // we've (hopefully) received a valid type 2 message: send type 3 message as last step QString user, domain; if (m_username.contains(QLatin1Char('\\'))) { domain = m_username.section(QLatin1Char('\\'), 0, 0); user = m_username.section(QLatin1Char('\\'), 1); } else { user = m_username; } m_forceKeepAlive = true; const QByteArray challenge = QByteArray::fromBase64(m_challenge[0]); KNTLM::AuthFlags flags = KNTLM::Add_LM; if ((!m_config || !m_config->readEntry("EnableNTLMv2Auth", false)) && (m_stage1State != SentNTLMv2)) { flags |= KNTLM::Force_V1; } if (!KNTLM::getAuth(buf, challenge, user, m_password, domain, QStringLiteral("WORKSTATION"), flags)) { qCWarning(KIO_HTTP_AUTH) << "Error while constructing Type 3 NTLM authentication request"; m_isError = true; return; } } m_headerFragment = "NTLM "; m_headerFragment += buf.toBase64(); m_headerFragment += "\r\n"; return; } ////////////////////////// #if HAVE_LIBGSSAPI // just an error message formatter static QByteArray gssError(int major_status, int minor_status) { OM_uint32 new_status; OM_uint32 msg_ctx = 0; gss_buffer_desc major_string; gss_buffer_desc minor_string; OM_uint32 ret; QByteArray errorstr; do { ret = gss_display_status(&new_status, major_status, GSS_C_GSS_CODE, GSS_C_NULL_OID, &msg_ctx, &major_string); errorstr += (const char *)major_string.value; errorstr += ' '; ret = gss_display_status(&new_status, minor_status, GSS_C_MECH_CODE, GSS_C_NULL_OID, &msg_ctx, &minor_string); errorstr += (const char *)minor_string.value; errorstr += ' '; } while (!GSS_ERROR(ret) && msg_ctx != 0); return errorstr; } QByteArray KHttpNegotiateAuthentication::scheme() const { return "Negotiate"; } void KHttpNegotiateAuthentication::setChallenge(const QByteArray &c, const QUrl &resource, const QByteArray &httpMethod) { KAbstractHttpAuthentication::setChallenge(c, resource, httpMethod); // GSSAPI knows how to get the credentials on its own m_needCredentials = false; } void KHttpNegotiateAuthentication::fillKioAuthInfo(KIO::AuthInfo *ai) const { authInfoBoilerplate(ai); //### does GSSAPI supply anything realm-like? dummy value for now. ai->realmValue = QStringLiteral("Negotiate"); } void KHttpNegotiateAuthentication::generateResponse(const QString &user, const QString &password) { generateResponseCommon(user, password); if (m_isError) { return; } OM_uint32 major_status, minor_status; gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER; gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER; gss_name_t server; gss_ctx_id_t ctx; gss_OID mech_oid; static gss_OID_desc krb5_oid_desc = {9, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"}; static gss_OID_desc spnego_oid_desc = {6, (void *) "\x2b\x06\x01\x05\x05\x02"}; gss_OID_set mech_set; gss_OID tmp_oid; ctx = GSS_C_NO_CONTEXT; mech_oid = &krb5_oid_desc; // see whether we can use the SPNEGO mechanism major_status = gss_indicate_mechs(&minor_status, &mech_set); if (GSS_ERROR(major_status)) { qCDebug(KIO_HTTP_AUTH) << "gss_indicate_mechs failed:" << gssError(major_status, minor_status); } else { for (uint i = 0; i < mech_set->count; i++) { tmp_oid = &mech_set->elements[i]; if (tmp_oid->length == spnego_oid_desc.length && !memcmp(tmp_oid->elements, spnego_oid_desc.elements, tmp_oid->length)) { // qDebug() << "found SPNEGO mech"; mech_oid = &spnego_oid_desc; break; } } gss_release_oid_set(&minor_status, &mech_set); } // the service name is "HTTP/f.q.d.n" QByteArray servicename = "HTTP@"; servicename += m_resource.host().toLatin1(); input_token.value = (void *)servicename.data(); input_token.length = servicename.length() + 1; major_status = gss_import_name(&minor_status, &input_token, GSS_C_NT_HOSTBASED_SERVICE, &server); input_token.value = nullptr; input_token.length = 0; if (GSS_ERROR(major_status)) { qCDebug(KIO_HTTP_AUTH) << "gss_import_name failed:" << gssError(major_status, minor_status); m_isError = true; return; } OM_uint32 req_flags; if (m_config && m_config->readEntry("DelegateCredentialsOn", false)) { req_flags = GSS_C_DELEG_FLAG; } else { req_flags = 0; } // GSSAPI knows how to get the credentials its own way, so don't ask for any major_status = gss_init_sec_context(&minor_status, GSS_C_NO_CREDENTIAL, &ctx, server, mech_oid, req_flags, GSS_C_INDEFINITE, GSS_C_NO_CHANNEL_BINDINGS, GSS_C_NO_BUFFER, nullptr, &output_token, nullptr, nullptr); if (GSS_ERROR(major_status) || (output_token.length == 0)) { qCDebug(KIO_HTTP_AUTH) << "gss_init_sec_context failed:" << gssError(major_status, minor_status); gss_release_name(&minor_status, &server); if (ctx != GSS_C_NO_CONTEXT) { gss_delete_sec_context(&minor_status, &ctx, GSS_C_NO_BUFFER); ctx = GSS_C_NO_CONTEXT; } m_isError = true; return; } m_headerFragment = "Negotiate "; m_headerFragment += QByteArray::fromRawData(static_cast(output_token.value), output_token.length).toBase64(); m_headerFragment += "\r\n"; // free everything gss_release_name(&minor_status, &server); if (ctx != GSS_C_NO_CONTEXT) { gss_delete_sec_context(&minor_status, &ctx, GSS_C_NO_BUFFER); ctx = GSS_C_NO_CONTEXT; } gss_release_buffer(&minor_status, &output_token); } #endif // HAVE_LIBGSSAPI diff --git a/src/ioslaves/http/kcookiejar/kcookiejar.cpp b/src/ioslaves/http/kcookiejar/kcookiejar.cpp index bc5adbf0..37c37d00 100644 --- a/src/ioslaves/http/kcookiejar/kcookiejar.cpp +++ b/src/ioslaves/http/kcookiejar/kcookiejar.cpp @@ -1,1612 +1,1612 @@ /* This file is part of the KDE File Manager Copyright (C) 1998-2000 Waldo Bastian (bastian@kde.org) Copyright (C) 2000,2001 Dawit Alemayehu (adawit@kde.org) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ //---------------------------------------------------------------------------- // // KDE File Manager -- HTTP Cookies // // The cookie protocol is a mess. RFC2109 is a joke since nobody seems to // use it. Apart from that it is badly written. // We try to implement Netscape Cookies and try to behave us according to // RFC2109 as much as we can. // // We assume cookies do not contain any spaces (Netscape spec.) // According to RFC2109 this is allowed though. // #include "kcookiejar.h" #include #include #include #include #include #include #include #include #include #include #include Q_LOGGING_CATEGORY(KIO_COOKIEJAR, "kf5.kio.cookiejar") // BR87227 // Waba: Should the number of cookies be limited? // I am not convinced of the need of such limit // Mozilla seems to limit to 20 cookies / domain // but it is unclear which policy it uses to expire // cookies when it exceeds that amount #undef MAX_COOKIE_LIMIT #define MAX_COOKIES_PER_HOST 25 #define READ_BUFFER_SIZE 8192 #define IP_ADDRESS_EXPRESSION "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" // Note with respect to QLatin1String( ).... // Cookies are stored as 8 bit data and passed to kio_http as Latin1 // regardless of their actual encoding. #define QL1S(x) QLatin1String(x) #define QL1C(x) QLatin1Char(x) static QString removeWeekday(const QString &value) { const int index = value.indexOf(QL1C(' ')); if (index > -1) { int pos = 0; const QString weekday = value.left(index); const QLocale cLocale = QLocale::c(); for (int i = 1; i < 8; ++i) { // No need to check for long names since the short names are // prefixes of the long names if (weekday.startsWith(cLocale.dayName(i, QLocale::ShortFormat), Qt::CaseInsensitive)) { pos = index + 1; break; } } if (pos > 0) { return value.mid(pos); } } return value; } static QDateTime parseDate(const QString &_value) { // Handle sites sending invalid weekday as part of the date. #298660 const QString value(removeWeekday(_value)); // Check if expiration date matches RFC dates as specified under // RFC 2616 sec 3.3.1 & RFC 6265 sec 4.1.1 QDateTime dt = QDateTime::fromString(value, Qt::RFC2822Date); if (!dt.isValid()) { static const char *const date_formats[] = { // Other formats documented in RFC 2616 sec 3.3.1 // Note: the RFC says timezone information MUST be "GMT", hence the hardcoded timezone string "MMM dd HH:mm:ss yyyy", /* ANSI C's asctime() format (#145244): Jan 01 00:00:00 1970 GMT */ "dd-MMM-yy HH:mm:ss 'GMT'", /* RFC 850 date: 06-Dec-39 00:30:42 GMT */ // Non-standard formats "MMM dd yyyy HH:mm:ss", /* A variation on ANSI C format seen @ amazon.com: Jan 01 1970 00:00:00 GMT */ "dd-MMM-yyyy HH:mm:ss 'GMT'", /* cookies.test: Y2K38 problem: 06-Dec-2039 00:30:42 GMT */ "MMM dd HH:mm:ss yyyy 'GMT'", /* cookies.test: Non-standard expiration dates: Sep 12 07:00:00 2020 GMT */ "MMM dd yyyy HH:mm:ss 'GMT'", /* cookies.test: Non-standard expiration dates: Sep 12 2020 07:00:00 GMT */ nullptr }; // Only English month names are allowed, thus use the C locale. const QLocale cLocale = QLocale::c(); for (int i = 0; date_formats[i]; ++i) { dt = cLocale.toDateTime(value, QL1S(date_formats[i])); if (dt.isValid()) { dt.setTimeSpec(Qt::UTC); break; } } } return dt.toUTC(); // Per RFC 2616 sec 3.3.1 always convert to UTC. } static qint64 toEpochSecs(const QDateTime &dt) { return (dt.toMSecsSinceEpoch() / 1000); // convert to seconds... } static qint64 epoch() { return toEpochSecs(QDateTime::currentDateTimeUtc()); } QString KCookieJar::adviceToStr(KCookieAdvice _advice) { switch (_advice) { case KCookieAccept: return QStringLiteral("Accept"); case KCookieAcceptForSession: return QStringLiteral("AcceptForSession"); case KCookieReject: return QStringLiteral("Reject"); case KCookieAsk: return QStringLiteral("Ask"); default: return QStringLiteral("Dunno"); } } KCookieAdvice KCookieJar::strToAdvice(const QString &_str) { if (_str.isEmpty()) { return KCookieDunno; } QString advice = _str.toLower(); if (advice == QL1S("accept")) { return KCookieAccept; } else if (advice == QL1S("acceptforsession")) { return KCookieAcceptForSession; } else if (advice == QL1S("reject")) { return KCookieReject; } else if (advice == QL1S("ask")) { return KCookieAsk; } return KCookieDunno; } // KHttpCookie /////////////////////////////////////////////////////////////////////////// // // Cookie constructor // KHttpCookie::KHttpCookie(const QString &_host, const QString &_domain, const QString &_path, const QString &_name, const QString &_value, qint64 _expireDate, int _protocolVersion, bool _secure, bool _httpOnly, bool _explicitPath) : mHost(_host), mDomain(_domain), mPath(_path.isEmpty() ? QString() : _path), mName(_name), mValue(_value), mExpireDate(_expireDate), mProtocolVersion(_protocolVersion), mSecure(_secure), mCrossDomain(false), mHttpOnly(_httpOnly), mExplicitPath(_explicitPath), mUserSelectedAdvice(KCookieDunno) { } // // Checks if a cookie has been expired // bool KHttpCookie::isExpired(qint64 currentDate) const { if (currentDate == -1) { currentDate = epoch(); } return (mExpireDate != 0) && (mExpireDate < currentDate); } // // Returns a string for a HTTP-header // QString KHttpCookie::cookieStr(bool useDOMFormat) const { QString result; if (useDOMFormat || (mProtocolVersion == 0)) { if (mName.isEmpty()) { result = mValue; } else { result = mName + QL1C('=') + mValue; } } else { result = mName + QL1C('=') + mValue; if (mExplicitPath) { result += QL1S("; $Path=\"") + mPath + QL1C('"'); } if (!mDomain.isEmpty()) { result += QL1S("; $Domain=\"") + mDomain + QL1C('"'); } if (!mPorts.isEmpty()) { if (mPorts.length() == 2 && mPorts.at(0) == -1) { result += QL1S("; $Port"); } else { QString portNums; Q_FOREACH (int port, mPorts) { portNums += QString::number(port) + QL1C(' '); } result += QL1S("; $Port=\"") + portNums.trimmed() + QL1C('"'); } } } return result; } // // Returns whether this cookie should be send to this location. bool KHttpCookie::match(const QString &fqdn, const QStringList &domains, const QString &path, int port) const { // Cookie domain match check if (mDomain.isEmpty()) { if (fqdn != mHost) { return false; } } else if (!domains.contains(mDomain)) { - if (mDomain[0] == '.') { + if (mDomain[0] == QL1C('.')) { return false; } // Maybe the domain needs an extra dot. const QString domain = QL1C('.') + mDomain; if (!domains.contains(domain)) if (fqdn != mDomain) { return false; } } else if (mProtocolVersion != 0 && port != -1 && !mPorts.isEmpty() && !mPorts.contains(port)) { return false; } // Cookie path match check if (mPath.isEmpty()) { return true; } // According to the netscape spec http://www.acme.com/foobar, // http://www.acme.com/foo.bar and http://www.acme.com/foo/bar // should all match http://www.acme.com/foo... // We only match http://www.acme.com/foo/bar if (path.startsWith(mPath) && ( (path.length() == mPath.length()) || // Paths are exact match mPath.endsWith(QL1C('/')) || // mPath ended with a slash (path[mPath.length()] == QL1C('/')) // A slash follows )) { return true; // Path of URL starts with cookie-path } return false; } // KCookieJar /////////////////////////////////////////////////////////////////////////// // // Constructs a new cookie jar // // One jar should be enough for all cookies. // KCookieJar::KCookieJar() { m_globalAdvice = KCookieDunno; m_configChanged = false; m_cookiesChanged = false; KConfig cfg(QStringLiteral("kf5/kcookiejar/domain_info"), KConfig::NoGlobals, QStandardPaths::GenericDataLocation); KConfigGroup group(&cfg, QString()); m_gTLDs = QSet::fromList(group.readEntry("gTLDs", QStringList())); m_twoLevelTLD = QSet::fromList(group.readEntry("twoLevelTLD", QStringList())); } // // Destructs the cookie jar // // Poor little cookies, they will all be eaten by the cookie monster! // KCookieJar::~KCookieJar() { qDeleteAll(m_cookieDomains); // Not much to do here } // cookiePtr is modified: the window ids of the existing cookie in the list are added to it static void removeDuplicateFromList(KHttpCookieList *list, KHttpCookie &cookiePtr, bool nameMatchOnly = false, bool updateWindowId = false) { QString domain1 = cookiePtr.domain(); if (domain1.isEmpty()) { domain1 = cookiePtr.host(); } QMutableListIterator cookieIterator(*list); while (cookieIterator.hasNext()) { const KHttpCookie &cookie = cookieIterator.next(); QString domain2 = cookie.domain(); if (domain2.isEmpty()) { domain2 = cookie.host(); } if (cookiePtr.name() == cookie.name() && (nameMatchOnly || (domain1 == domain2 && cookiePtr.path() == cookie.path()))) { if (updateWindowId) { Q_FOREACH (WId windowId, cookie.windowIds()) { if (windowId && (!cookiePtr.windowIds().contains(windowId))) { cookiePtr.windowIds().append(windowId); } } } cookieIterator.remove(); break; } } } // // Looks for cookies in the cookie jar which are appropriate for _url. // Returned is a string containing all appropriate cookies in a format // which can be added to a HTTP-header without any additional processing. // QString KCookieJar::findCookies(const QString &_url, bool useDOMFormat, WId windowId, KHttpCookieList *pendingCookies) { QString cookieStr, fqdn, path; QStringList domains; int port = -1; if (!parseUrl(_url, fqdn, path, &port)) { return cookieStr; } const bool secureRequest = (_url.startsWith(QL1S("https://"), Qt::CaseInsensitive) || _url.startsWith(QL1S("webdavs://"), Qt::CaseInsensitive)); if (port == -1) { port = (secureRequest ? 443 : 80); } extractDomains(fqdn, domains); KHttpCookieList allCookies; for (QStringList::ConstIterator it = domains.constBegin(), itEnd = domains.constEnd();; ++it) { KHttpCookieList *cookieList = nullptr; if (it == itEnd) { cookieList = pendingCookies; // Add pending cookies pendingCookies = nullptr; if (!cookieList) { break; } } else { if ((*it).isNull()) { cookieList = m_cookieDomains.value(QL1S("")); } else { cookieList = m_cookieDomains.value(*it); } if (!cookieList) { continue; // No cookies for this domain } } QMutableListIterator cookieIt(*cookieList); while (cookieIt.hasNext()) { KHttpCookie &cookie = cookieIt.next(); if (cookieAdvice(cookie) == KCookieReject) { continue; } if (!cookie.match(fqdn, domains, path, port)) { continue; } if (cookie.isSecure() && !secureRequest) { continue; } if (cookie.isHttpOnly() && useDOMFormat) { continue; } // Do not send expired cookies. if (cookie.isExpired()) { // NOTE: there is no need to delete the cookie here because the // cookieserver will invoke its saveCookieJar function as a result // of the state change below. This will then result in the cookie // being deleting at that point. m_cookiesChanged = true; continue; } if (windowId && (cookie.windowIds().indexOf(windowId) == -1)) { cookie.windowIds().append(windowId); } if (it == itEnd) { // Only needed when processing pending cookies removeDuplicateFromList(&allCookies, cookie); } allCookies.append(cookie); } if (it == itEnd) { break; // Finished. } } int protVersion = 0; Q_FOREACH (const KHttpCookie &cookie, allCookies) { if (cookie.protocolVersion() > protVersion) { protVersion = cookie.protocolVersion(); } } if (!allCookies.isEmpty()) { if (!useDOMFormat) { cookieStr = QStringLiteral("Cookie: "); } if (protVersion > 0) { cookieStr = cookieStr + QStringLiteral("$Version=") + QString::number(protVersion) + QStringLiteral("; "); } Q_FOREACH (const KHttpCookie &cookie, allCookies) { cookieStr = cookieStr + cookie.cookieStr(useDOMFormat) + QStringLiteral("; "); } cookieStr.truncate(cookieStr.length() - 2); // Remove the trailing ';' } return cookieStr; } // // This function parses a string like 'my_name="my_value";' and returns // 'my_name' in Name and 'my_value' in Value. // // A pointer to the end of the parsed part is returned. // This pointer points either to: // '\0' - The end of the string has reached. // ';' - Another my_name="my_value" pair follows // ',' - Another cookie follows // '\n' - Another header follows static const char *parseNameValue(const char *header, QString &Name, QString &Value, bool keepQuotes = false, bool rfcQuotes = false) { const char *s = header; // Parse 'my_name' part for (; (*s != '='); s++) { if ((*s == '\0') || (*s == ';') || (*s == '\n')) { // No '=' sign -> use string as the value, name is empty // (behavior found in Mozilla and IE) Name = QL1S(""); Value = QL1S(header); Value.truncate(s - header); Value = Value.trimmed(); return s; } } Name = QL1S(header); Name.truncate(s - header); Name = Name.trimmed(); // *s == '=' s++; // Skip any whitespace for (; (*s == ' ') || (*s == '\t'); s++) { if ((*s == '\0') || (*s == ';') || (*s == '\n')) { // End of Name Value = QLatin1String(""); return s; } } if ((rfcQuotes || !keepQuotes) && (*s == '\"')) { // Parse '"my_value"' part (quoted value) if (keepQuotes) { header = s++; } else { header = ++s; // skip " } for (; (*s != '\"'); s++) { if ((*s == '\0') || (*s == '\n')) { // End of Name Value = QL1S(header); Value.truncate(s - header); return s; } } Value = QL1S(header); // *s == '\"'; if (keepQuotes) { Value.truncate(++s - header); } else { Value.truncate(s++ - header); } // Skip any remaining garbage for (;; s++) { if ((*s == '\0') || (*s == ';') || (*s == '\n')) { break; } } } else { // Parse 'my_value' part (unquoted value) header = s; while ((*s != '\0') && (*s != ';') && (*s != '\n')) { s++; } // End of Name Value = QL1S(header); Value.truncate(s - header); Value = Value.trimmed(); } return s; } void KCookieJar::stripDomain(const QString &_fqdn, QString &_domain) const { QStringList domains; extractDomains(_fqdn, domains); if (domains.count() > 3) { _domain = domains[3]; } else if (domains.count() > 0) { _domain = domains[0]; } else { _domain = QL1S(""); } } QString KCookieJar::stripDomain(const KHttpCookie &cookie) const { QString domain; // We file the cookie under this domain. if (cookie.domain().isEmpty()) { stripDomain(cookie.host(), domain); } else { domain = cookie.domain(); } return domain; } bool KCookieJar::parseUrl(const QString &_url, QString &_fqdn, QString &_path, int *port) { QUrl kurl(_url); if (!kurl.isValid() || kurl.scheme().isEmpty()) { return false; } _fqdn = kurl.host().toLower(); // Cookie spoofing protection. Since there is no way a path separator, // a space or the escape encoding character is allowed in the hostname // according to RFC 2396, reject attempts to include such things there! if (_fqdn.contains(QL1C('/')) || _fqdn.contains(QL1C('%'))) { return false; // deny everything!! } // Set the port number from the protocol when one is found... if (port) { *port = kurl.port(); } _path = kurl.path(); if (_path.isEmpty()) { _path = QStringLiteral("/"); } return true; } // not static because it uses m_twoLevelTLD void KCookieJar::extractDomains(const QString &_fqdn, QStringList &_domains) const { if (_fqdn.isEmpty()) { _domains.append(QStringLiteral("localhost")); return; } // Return numeric IPv6 addresses as is... - if (_fqdn[0] == '[') { + if (_fqdn[0] == QL1C('[')) { _domains.append(_fqdn); return; } // Return numeric IPv4 addresses as is... - if (_fqdn[0] >= '0' && _fqdn[0] <= '9' && _fqdn.indexOf(QRegExp(IP_ADDRESS_EXPRESSION)) > -1) { + if (_fqdn[0] >= QL1C('0') && _fqdn[0] <= QL1C('9') && _fqdn.indexOf(QRegExp(QStringLiteral(IP_ADDRESS_EXPRESSION))) > -1) { _domains.append(_fqdn); return; } // Always add the FQDN at the start of the list for // hostname == cookie-domainname checks! _domains.append(_fqdn); _domains.append(QL1C('.') + _fqdn); QStringList partList = _fqdn.split(QL1C('.'), QString::SkipEmptyParts); if (partList.count()) { partList.erase(partList.begin()); // Remove hostname } while (partList.count()) { if (partList.count() == 1) { break; // We only have a TLD left. } if ((partList.count() == 2) && m_twoLevelTLD.contains(partList[1].toLower())) { // This domain uses two-level TLDs in the form xxxx.yy break; } if ((partList.count() == 2) && (partList[1].length() == 2)) { // If this is a TLD, we should stop. (e.g. co.uk) // We assume this is a TLD if it ends with .xx.yy or .x.yy if (partList[0].length() <= 2) { break; // This is a TLD. } // Catch some TLDs that we miss with the previous check // e.g. com.au, org.uk, mil.co if (m_gTLDs.contains(partList[0].toLower())) { break; } } QString domain = partList.join(QLatin1Char('.')); _domains.append(domain); _domains.append(QL1C('.') + domain); partList.erase(partList.begin()); // Remove part } } // // This function parses cookie_headers and returns a linked list of // KHttpCookie objects for all cookies found in cookie_headers. // If no cookies could be found 0 is returned. // // cookie_headers should be a concatenation of all lines of a HTTP-header // which start with "Set-Cookie". The lines should be separated by '\n's. // KHttpCookieList KCookieJar::makeCookies(const QString &_url, const QByteArray &cookie_headers, WId windowId) { QString fqdn, path; if (!parseUrl(_url, fqdn, path)) { return KHttpCookieList(); // Error parsing _url } QString Name, Value; KHttpCookieList cookieList, cookieList2; bool isRFC2965 = false; bool crossDomain = false; const char *cookieStr = cookie_headers.constData(); QString defaultPath; const int index = path.lastIndexOf(QL1C('/')); if (index > 0) { defaultPath = path.left(index); } // Check for cross-domain flag from kio_http if (qstrncmp(cookieStr, "Cross-Domain\n", 13) == 0) { cookieStr += 13; crossDomain = true; } // The hard stuff :) for (;;) { // check for "Set-Cookie" if (qstrnicmp(cookieStr, "Set-Cookie:", 11) == 0) { cookieStr = parseNameValue(cookieStr + 11, Name, Value, true); // Host = FQDN // Default domain = "" // Default path according to rfc2109 KHttpCookie cookie(fqdn, QL1S(""), defaultPath, Name, Value); if (windowId) { cookie.mWindowIds.append(windowId); } cookie.mCrossDomain = crossDomain; // Insert cookie in chain cookieList.append(cookie); } else if (qstrnicmp(cookieStr, "Set-Cookie2:", 12) == 0) { // Attempt to follow rfc2965 isRFC2965 = true; cookieStr = parseNameValue(cookieStr + 12, Name, Value, true, true); // Host = FQDN // Default domain = "" // Default path according to rfc2965 KHttpCookie cookie(fqdn, QL1S(""), defaultPath, Name, Value); if (windowId) { cookie.mWindowIds.append(windowId); } cookie.mCrossDomain = crossDomain; // Insert cookie in chain cookieList2.append(cookie); } else { // This is not the start of a cookie header, skip till next line. while (*cookieStr && *cookieStr != '\n') { cookieStr++; } if (*cookieStr == '\n') { cookieStr++; } if (!*cookieStr) { break; // End of cookie_headers } else { continue; // end of this header, continue with next. } } while ((*cookieStr == ';') || (*cookieStr == ' ')) { cookieStr++; // Name-Value pair follows cookieStr = parseNameValue(cookieStr, Name, Value); KHttpCookie &lastCookie = (isRFC2965 ? cookieList2.last() : cookieList.last()); if (Name.compare(QL1S("domain"), Qt::CaseInsensitive) == 0) { QString dom = Value.toLower(); // RFC2965 3.2.2: If an explicitly specified value does not // start with a dot, the user agent supplies a leading dot - if (dom.length() && dom[0] != '.') { - dom.prepend("."); + if (dom.length() > 0 && dom[0] != QL1C('.')) { + dom.prepend(QL1C('.')); } // remove a trailing dot - if (dom.length() > 2 && dom[dom.length() - 1] == '.') { + if (dom.length() > 2 && dom[dom.length() - 1] == QL1C('.')) { dom = dom.left(dom.length() - 1); } if (dom.count(QL1C('.')) > 1 || dom == QLatin1String(".local")) { lastCookie.mDomain = dom; } } else if (Name.compare(QL1S("max-age"), Qt::CaseInsensitive) == 0) { int max_age = Value.toInt(); if (max_age == 0) { lastCookie.mExpireDate = 1; } else { lastCookie.mExpireDate = toEpochSecs(QDateTime::currentDateTimeUtc().addSecs(max_age)); } } else if (Name.compare(QL1S("expires"), Qt::CaseInsensitive) == 0) { const QDateTime dt = parseDate(Value); if (dt.isValid()) { lastCookie.mExpireDate = toEpochSecs(dt); if (lastCookie.mExpireDate == 0) { lastCookie.mExpireDate = 1; } } } else if (Name.compare(QL1S("path"), Qt::CaseInsensitive) == 0) { if (Value.isEmpty()) { lastCookie.mPath.clear(); // Catch "" <> QString() } else { lastCookie.mPath = QUrl::fromPercentEncoding(Value.toLatin1()); } lastCookie.mExplicitPath = true; } else if (Name.compare(QL1S("version"), Qt::CaseInsensitive) == 0) { lastCookie.mProtocolVersion = Value.toInt(); } else if (Name.compare(QL1S("secure"), Qt::CaseInsensitive) == 0 || (Name.isEmpty() && Value.compare(QL1S("secure"), Qt::CaseInsensitive) == 0)) { lastCookie.mSecure = true; } else if (Name.compare(QL1S("httponly"), Qt::CaseInsensitive) == 0 || (Name.isEmpty() && Value.compare(QL1S("httponly"), Qt::CaseInsensitive) == 0)) { lastCookie.mHttpOnly = true; } else if (isRFC2965 && (Name.compare(QL1S("port"), Qt::CaseInsensitive) == 0 || (Name.isEmpty() && Value.compare(QL1S("port"), Qt::CaseInsensitive) == 0))) { // Based on the port selection rule of RFC 2965 section 3.3.4... if (Name.isEmpty()) { // We intentionally append a -1 first in order to distinguish // between only a 'Port' vs a 'Port="80 443"' in the sent cookie. lastCookie.mPorts.append(-1); const bool secureRequest = (_url.startsWith(QL1S("https://"), Qt::CaseInsensitive) || _url.startsWith(QL1S("webdavs://"), Qt::CaseInsensitive)); if (secureRequest) { lastCookie.mPorts.append(443); } else { lastCookie.mPorts.append(80); } } else { bool ok; const QStringList portNums = Value.split(QL1C(' '), QString::SkipEmptyParts); Q_FOREACH (const QString &portNum, portNums) { const int port = portNum.toInt(&ok); if (ok) { lastCookie.mPorts.append(port); } } } } } if (*cookieStr == '\0') { break; // End of header } // Skip ';' or '\n' cookieStr++; } // RFC2965 cookies come last so that they override netscape cookies. while (!cookieList2.isEmpty()) { KHttpCookie &lastCookie = cookieList2.first(); removeDuplicateFromList(&cookieList, lastCookie, true); cookieList.append(lastCookie); cookieList2.removeFirst(); } return cookieList; } /** * Parses cookie_domstr and returns a linked list of KHttpCookie objects. * cookie_domstr should be a semicolon-delimited list of "name=value" * pairs. Any whitespace before "name" or around '=' is discarded. * If no cookies are found, 0 is returned. */ KHttpCookieList KCookieJar::makeDOMCookies(const QString &_url, const QByteArray &cookie_domstring, WId windowId) { // A lot copied from above KHttpCookieList cookieList; const char *cookieStr = cookie_domstring.data(); QString fqdn; QString path; if (!parseUrl(_url, fqdn, path)) { // Error parsing _url return KHttpCookieList(); } QString Name; QString Value; // This time it's easy while (*cookieStr) { cookieStr = parseNameValue(cookieStr, Name, Value); // Host = FQDN // Default domain = "" // Default path = "" KHttpCookie cookie(fqdn, QString(), QString(), Name, Value); if (windowId) { cookie.mWindowIds.append(windowId); } cookieList.append(cookie); if (*cookieStr != '\0') { cookieStr++; // Skip ';' or '\n' } } return cookieList; } // KHttpCookieList sorting /////////////////////////////////////////////////////////////////////////// // We want the longest path first static bool compareCookies(const KHttpCookie &item1, const KHttpCookie &item2) { return item1.path().length() > item2.path().length(); } #ifdef MAX_COOKIE_LIMIT static void makeRoom(KHttpCookieList *cookieList, KHttpCookiePtr &cookiePtr) { // Too many cookies: throw one away, try to be somewhat clever KHttpCookiePtr lastCookie = 0; for (KHttpCookiePtr cookie = cookieList->first(); cookie; cookie = cookieList->next()) { if (compareCookies(cookie, cookiePtr)) { break; } lastCookie = cookie; } if (!lastCookie) { lastCookie = cookieList->first(); } cookieList->removeRef(lastCookie); } #endif // // This function hands a KHttpCookie object over to the cookie jar. // void KCookieJar::addCookie(KHttpCookie &cookie) { QStringList domains; // We always need to do this to make sure that the // that cookies of type hostname == cookie-domainname // are properly removed and/or updated as necessary! extractDomains(cookie.host(), domains); // If the cookie specifies a domain, check whether it is valid. Otherwise, // accept the cookie anyways but removes the domain="" value to prevent // cross-site cookie injection. if (!cookie.domain().isEmpty()) { if (!domains.contains(cookie.domain()) && !cookie.domain().endsWith(QL1C('.') + cookie.host())) { cookie.fixDomain(QString()); } } QStringListIterator it(domains); while (it.hasNext()) { const QString &key = it.next(); KHttpCookieList *list; if (key.isNull()) { list = m_cookieDomains.value(QL1S("")); } else { list = m_cookieDomains.value(key); } if (list) { removeDuplicateFromList(list, cookie, false, true); } } const QString domain = stripDomain(cookie); KHttpCookieList *cookieList; if (domain.isNull()) { cookieList = m_cookieDomains.value(QL1S("")); } else { cookieList = m_cookieDomains.value(domain); } if (!cookieList) { // Make a new cookie list cookieList = new KHttpCookieList(); // All cookies whose domain is not already // known to us should be added with KCookieDunno. // KCookieDunno means that we use the global policy. cookieList->setAdvice(KCookieDunno); m_cookieDomains.insert(domain, cookieList); // Update the list of domains m_domainList.append(domain); } // Add the cookie to the cookie list // The cookie list is sorted 'longest path first' if (!cookie.isExpired()) { #ifdef MAX_COOKIE_LIMIT if (cookieList->count() >= MAX_COOKIES_PER_HOST) { makeRoom(cookieList, cookie); // Delete a cookie } #endif cookieList->push_back(cookie); // Use a stable sort so that unit tests are reliable. // In practice it doesn't matter though. qStableSort(cookieList->begin(), cookieList->end(), compareCookies); m_cookiesChanged = true; } } // // This function advises whether a single KHttpCookie object should // be added to the cookie jar. // KCookieAdvice KCookieJar::cookieAdvice(const KHttpCookie &cookie) const { if (m_rejectCrossDomainCookies && cookie.isCrossDomain()) { return KCookieReject; } if (cookie.getUserSelectedAdvice() != KCookieDunno) { return cookie.getUserSelectedAdvice(); } if (m_autoAcceptSessionCookies && cookie.expireDate() == 0) { return KCookieAccept; } QStringList domains; extractDomains(cookie.host(), domains); KCookieAdvice advice = KCookieDunno; QStringListIterator it(domains); while (advice == KCookieDunno && it.hasNext()) { const QString &domain = it.next(); if (domain.startsWith(QL1C('.')) || cookie.host() == domain) { KHttpCookieList *cookieList = m_cookieDomains.value(domain); if (cookieList) { advice = cookieList->getAdvice(); } } } if (advice == KCookieDunno) { advice = m_globalAdvice; } return advice; } // // This function tells whether a single KHttpCookie object should // be considered persistent. Persistent cookies do not get deleted // at the end of the session and are saved on disk. // bool KCookieJar::cookieIsPersistent(const KHttpCookie &cookie) const { if (cookie.expireDate() == 0) { return false; } KCookieAdvice advice = cookieAdvice(cookie); if (advice == KCookieReject || advice == KCookieAcceptForSession) { return false; } return true; } // // This function gets the advice for all cookies originating from // _domain. // KCookieAdvice KCookieJar::getDomainAdvice(const QString &_domain) const { KHttpCookieList *cookieList = m_cookieDomains.value(_domain); KCookieAdvice advice; if (cookieList) { advice = cookieList->getAdvice(); } else { advice = KCookieDunno; } return advice; } // // This function sets the advice for all cookies originating from // _domain. // void KCookieJar::setDomainAdvice(const QString &_domain, KCookieAdvice _advice) { QString domain(_domain); KHttpCookieList *cookieList = m_cookieDomains.value(domain); if (cookieList) { if (cookieList->getAdvice() != _advice) { m_configChanged = true; // domain is already known cookieList->setAdvice(_advice); } if ((cookieList->isEmpty()) && (_advice == KCookieDunno)) { // This deletes cookieList! delete m_cookieDomains.take(domain); m_domainList.removeAll(domain); } } else { // domain is not yet known if (_advice != KCookieDunno) { // We should create a domain entry m_configChanged = true; // Make a new cookie list cookieList = new KHttpCookieList(); cookieList->setAdvice(_advice); m_cookieDomains.insert(domain, cookieList); // Update the list of domains m_domainList.append(domain); } } } // // This function sets the advice for all cookies originating from // the same domain as _cookie // void KCookieJar::setDomainAdvice(const KHttpCookie &cookie, KCookieAdvice _advice) { QString domain; stripDomain(cookie.host(), domain); // We file the cookie under this domain. setDomainAdvice(domain, _advice); } // // This function sets the global advice for cookies // void KCookieJar::setGlobalAdvice(KCookieAdvice _advice) { if (m_globalAdvice != _advice) { m_configChanged = true; } m_globalAdvice = _advice; } // // Get a list of all domains known to the cookie jar. // const QStringList &KCookieJar::getDomainList() { return m_domainList; } // // Get a list of all cookies in the cookie jar originating from _domain. // KHttpCookieList *KCookieJar::getCookieList(const QString &_domain, const QString &_fqdn) { QString domain; if (_domain.isEmpty()) { stripDomain(_fqdn, domain); } else { domain = _domain; } return m_cookieDomains.value(domain); } // // Eat a cookie out of the jar. // cookieIterator should be one of the cookies returned by getCookieList() // void KCookieJar::eatCookie(const KHttpCookieList::iterator &cookieIterator) { const KHttpCookie &cookie = *cookieIterator; const QString domain = stripDomain(cookie); // We file the cookie under this domain. KHttpCookieList *cookieList = m_cookieDomains.value(domain); if (cookieList) { // This deletes cookie! cookieList->erase(cookieIterator); if ((cookieList->isEmpty()) && (cookieList->getAdvice() == KCookieDunno)) { // This deletes cookieList! delete m_cookieDomains.take(domain); m_domainList.removeAll(domain); } } } void KCookieJar::eatCookiesForDomain(const QString &domain) { KHttpCookieList *cookieList = m_cookieDomains.value(domain); if (!cookieList || cookieList->isEmpty()) { return; } cookieList->clear(); if (cookieList->getAdvice() == KCookieDunno) { // This deletes cookieList! delete m_cookieDomains.take(domain); m_domainList.removeAll(domain); } m_cookiesChanged = true; } void KCookieJar::eatSessionCookies(long windowId) { if (!windowId) { return; } Q_FOREACH (const QString &domain, m_domainList) { eatSessionCookies(domain, windowId, false); } } void KCookieJar::eatAllCookies() { Q_FOREACH (const QString &domain, m_domainList) { eatCookiesForDomain(domain); // This might remove domain from m_domainList! } } void KCookieJar::eatSessionCookies(const QString &fqdn, WId windowId, bool isFQDN) { KHttpCookieList *cookieList; if (!isFQDN) { cookieList = m_cookieDomains.value(fqdn); } else { QString domain; stripDomain(fqdn, domain); cookieList = m_cookieDomains.value(domain); } if (cookieList) { QMutableListIterator cookieIterator(*cookieList); while (cookieIterator.hasNext()) { KHttpCookie &cookie = cookieIterator.next(); if (cookieIsPersistent(cookie)) { continue; } QList &ids = cookie.windowIds(); #ifndef NDEBUG if (ids.contains(windowId)) { if (ids.count() > 1) { qCDebug(KIO_COOKIEJAR) << "removing window id" << windowId << "from session cookie"; } else { qCDebug(KIO_COOKIEJAR) << "deleting session cookie"; } } #endif if (!ids.removeAll(windowId) || !ids.isEmpty()) { continue; } cookieIterator.remove(); } } } static QString hostWithPort(const KHttpCookie *cookie) { const QList &ports = cookie->ports(); if (ports.isEmpty()) { return cookie->host(); } QStringList portList; Q_FOREACH (int port, ports) { portList << QString::number(port); } return (cookie->host() + QL1C(':') + portList.join(QLatin1Char(','))); } // // Saves all cookies to the file '_filename'. // On success 'true' is returned. // On failure 'false' is returned. bool KCookieJar::saveCookies(const QString &_filename) { QSaveFile cookieFile(_filename); if (!cookieFile.open(QIODevice::WriteOnly)) { return false; } QTextStream ts(&cookieFile); ts << "# KDE Cookie File v2\n#\n"; QString s; s.sprintf("%-20s %-20s %-12s %-10s %-4s %-20s %-4s %s\n", "# Host", "Domain", "Path", "Exp.date", "Prot", "Name", "Sec", "Value"); ts << s.toLatin1().constData(); QStringListIterator it(m_domainList); while (it.hasNext()) { const QString &domain = it.next(); bool domainPrinted = false; KHttpCookieList *cookieList = m_cookieDomains.value(domain); if (!cookieList) { continue; } QMutableListIterator cookieIterator(*cookieList); while (cookieIterator.hasNext()) { const KHttpCookie &cookie = cookieIterator.next(); if (cookie.isExpired()) { // Delete expired cookies cookieIterator.remove(); continue; } if (cookieIsPersistent(cookie)) { // Only save cookies that are not "session-only cookies" if (!domainPrinted) { domainPrinted = true; ts << '[' << domain.toLocal8Bit().data() << "]\n"; } // Store persistent cookies const QString path = QL1S("\"") + cookie.path() + QL1C('"'); const QString domain = QL1S("\"") + cookie.domain() + QL1C('"'); const QString host = hostWithPort(&cookie); // TODO: replace with direct QTextStream output ? s.sprintf("%-20s %-20s %-12s %10lld %3d %-20s %-4i %s\n", host.toLatin1().constData(), domain.toLatin1().constData(), path.toLatin1().constData(), cookie.expireDate(), cookie.protocolVersion(), cookie.name().isEmpty() ? cookie.value().toLatin1().constData() : cookie.name().toLatin1().constData(), (cookie.isSecure() ? 1 : 0) + (cookie.isHttpOnly() ? 2 : 0) + (cookie.hasExplicitPath() ? 4 : 0) + (cookie.name().isEmpty() ? 8 : 0), cookie.value().toLatin1().constData()); ts << s.toLatin1().constData(); } } } if (cookieFile.commit()) { QFile::setPermissions(_filename, QFile::ReadUser | QFile::WriteUser); return true; } return false; } static const char *parseField(char *&buffer, bool keepQuotes = false) { char *result; if (!keepQuotes && (*buffer == '\"')) { // Find terminating " buffer++; result = buffer; while ((*buffer != '\"') && (*buffer)) { buffer++; } } else { // Find first white space result = buffer; while ((*buffer != ' ') && (*buffer != '\t') && (*buffer != '\n') && (*buffer)) { buffer++; } } if (!*buffer) { return result; // } *buffer++ = '\0'; // Skip white-space while ((*buffer == ' ') || (*buffer == '\t') || (*buffer == '\n')) { buffer++; } return result; } static QString extractHostAndPorts(const QString &str, QList *ports = nullptr) { if (str.isEmpty()) { return str; } const int index = str.indexOf(QL1C(':')); if (index == -1) { return str; } const QString host = str.left(index); if (ports) { bool ok; QStringList portList = str.mid(index + 1).split(QL1C(',')); Q_FOREACH (const QString &portStr, portList) { const int portNum = portStr.toInt(&ok); if (ok) { ports->append(portNum); } } } return host; } // // Reloads all cookies from the file '_filename'. // On success 'true' is returned. // On failure 'false' is returned. bool KCookieJar::loadCookies(const QString &_filename) { QFile cookieFile(_filename); if (!cookieFile.open(QIODevice::ReadOnly)) { return false; } int version = 1; bool success = false; char *buffer = new char[READ_BUFFER_SIZE]; qint64 len = cookieFile.readLine(buffer, READ_BUFFER_SIZE - 1); if (len != -1) { if (qstrcmp(buffer, "# KDE Cookie File\n") == 0) { success = true; } else if (qstrcmp(buffer, "# KDE Cookie File v") > 0) { bool ok = false; const int verNum = QByteArray(buffer + 19, len - 19).trimmed().toInt(&ok); if (ok) { version = verNum; success = true; } } } if (success) { const qint64 currentTime = epoch(); QList ports; while (cookieFile.readLine(buffer, READ_BUFFER_SIZE - 1) != -1) { char *line = buffer; // Skip lines which begin with '#' or '[' if ((line[0] == '#') || (line[0] == '[')) { continue; } const QString host = extractHostAndPorts(QL1S(parseField(line)), &ports); const QString domain = QL1S(parseField(line)); if (host.isEmpty() && domain.isEmpty()) { continue; } const QString path = QL1S(parseField(line)); const QString expStr = QL1S(parseField(line)); if (expStr.isEmpty()) { continue; } const qint64 expDate = expStr.toLongLong(); const QString verStr = QL1S(parseField(line)); if (verStr.isEmpty()) { continue; } int protVer = verStr.toInt(); QString name = QL1S(parseField(line)); bool keepQuotes = false; bool secure = false; bool httpOnly = false; bool explicitPath = false; const char *value = nullptr; if ((version == 2) || (protVer >= 200)) { if (protVer >= 200) { protVer -= 200; } int i = atoi(parseField(line)); secure = i & 1; httpOnly = i & 2; explicitPath = i & 4; if (i & 8) { name = QLatin1String(""); } line[strlen(line) - 1] = '\0'; // Strip LF. value = line; } else { if (protVer >= 100) { protVer -= 100; keepQuotes = true; } value = parseField(line, keepQuotes); secure = QByteArray(parseField(line)).toShort(); } // Expired or parse error if (!value || expDate == 0 || expDate < currentTime) { continue; } - KHttpCookie cookie(host, domain, path, name, value, expDate, + KHttpCookie cookie(host, domain, path, name, QString::fromUtf8(value), expDate, protVer, secure, httpOnly, explicitPath); if (ports.count()) { cookie.mPorts = ports; } addCookie(cookie); } } delete [] buffer; m_cookiesChanged = false; return success; } // // Save the cookie configuration // void KCookieJar::saveConfig(KConfig *_config) { if (!m_configChanged) { return; } KConfigGroup dlgGroup(_config, "Cookie Dialog"); dlgGroup.writeEntry("PreferredPolicy", static_cast(m_preferredPolicy)); dlgGroup.writeEntry("ShowCookieDetails", m_showCookieDetails); KConfigGroup policyGroup(_config, "Cookie Policy"); policyGroup.writeEntry("CookieGlobalAdvice", adviceToStr(m_globalAdvice)); QStringList domainSettings; QStringListIterator it(m_domainList); while (it.hasNext()) { const QString &domain = it.next(); KCookieAdvice advice = getDomainAdvice(domain); if (advice != KCookieDunno) { const QString value = domain + QL1C(':') + adviceToStr(advice); domainSettings.append(value); } } policyGroup.writeEntry("CookieDomainAdvice", domainSettings); _config->sync(); m_configChanged = false; } // // Load the cookie configuration // void KCookieJar::loadConfig(KConfig *_config, bool reparse) { if (reparse) { _config->reparseConfiguration(); } KConfigGroup dlgGroup(_config, "Cookie Dialog"); m_showCookieDetails = dlgGroup.readEntry("ShowCookieDetails", false); m_preferredPolicy = static_cast(dlgGroup.readEntry("PreferredPolicy", 0)); KConfigGroup policyGroup(_config, "Cookie Policy"); const QStringList domainSettings = policyGroup.readEntry("CookieDomainAdvice", QStringList()); // Warning: those default values are duplicated in the kcm (kio/kcookiespolicies.cpp) m_rejectCrossDomainCookies = policyGroup.readEntry("RejectCrossDomainCookies", true); m_autoAcceptSessionCookies = policyGroup.readEntry("AcceptSessionCookies", true); m_globalAdvice = strToAdvice(policyGroup.readEntry("CookieGlobalAdvice", QStringLiteral("Accept"))); // Reset current domain settings first. Q_FOREACH (const QString &domain, m_domainList) { setDomainAdvice(domain, KCookieDunno); } // Now apply the domain settings read from config file... for (QStringList::ConstIterator it = domainSettings.constBegin(), itEnd = domainSettings.constEnd(); it != itEnd; ++it) { const QString &value = *it; const int sepPos = value.lastIndexOf(QL1C(':')); if (sepPos <= 0) { continue; } const QString domain(value.left(sepPos)); KCookieAdvice advice = strToAdvice(value.mid(sepPos + 1)); setDomainAdvice(domain, advice); } } QDebug operator<<(QDebug dbg, const KHttpCookie &cookie) { dbg.nospace() << cookie.cookieStr(false); return dbg.space(); } QDebug operator<<(QDebug dbg, const KHttpCookieList &list) { Q_FOREACH (const KHttpCookie &cookie, list) { dbg << cookie; } return dbg; } diff --git a/src/ioslaves/http/kcookiejar/kcookieserver.cpp b/src/ioslaves/http/kcookiejar/kcookieserver.cpp index 3de5b276..ef421038 100644 --- a/src/ioslaves/http/kcookiejar/kcookieserver.cpp +++ b/src/ioslaves/http/kcookiejar/kcookieserver.cpp @@ -1,602 +1,602 @@ /* This file is part of KDE Copyright (C) 1998-2000 Waldo Bastian (bastian@kde.org) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ //---------------------------------------------------------------------------- // // KDE Cookie Server #include "kcookieserver.h" #define SAVE_DELAY 3 // Save after 3 minutes #include #include #include #include #include #include #include #include #include #include #include #include Q_DECLARE_LOGGING_CATEGORY(KIO_COOKIEJAR) #include "kcookiejar.h" #include "kcookiewin.h" #include "kcookieserveradaptor.h" K_PLUGIN_FACTORY_WITH_JSON(KdedCookieServerFactory, "kcookiejar.json", registerPlugin();) static QDir getOrCreateCookieJarDir() { const QDir dataDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)); const QString kcookiejarDirName = dataDir.absoluteFilePath(QStringLiteral("kcookiejar")); if (dataDir.exists(QStringLiteral("kcookiejar"))) { const QFileInfo cookiejarDirInfo(kcookiejarDirName); if (!cookiejarDirInfo.isDir()) { QFile kcookieBogusFile(kcookiejarDirName); if (!kcookieBogusFile.remove()) { QMessageBox::warning(nullptr, i18n("Cannot Save Cookies"), i18n("Could not remove %1, check permissions", cookiejarDirInfo.absoluteFilePath())); } } else { return QDir(kcookiejarDirName); } } if (!dataDir.mkpath(QStringLiteral("kcookiejar"))) { QMessageBox::warning(nullptr, i18n("Cannot Save Cookies"), i18n("Could not create directory %1", kcookiejarDirName)); } return QDir(kcookiejarDirName); } // Cookie field indexes enum CookieDetails { CF_DOMAIN = 0, CF_PATH, CF_NAME, CF_HOST, CF_VALUE, CF_EXPIRE, CF_PROVER, CF_SECURE }; class CookieRequest { public: QDBusMessage reply; QString url; bool DOM; qlonglong windowId; }; template class QList; class RequestList : public QList { public: RequestList() : QList() { } }; KCookieServer::KCookieServer(QObject *parent, const QList &) : KDEDModule(parent) { (void)new KCookieServerAdaptor(this); mCookieJar = new KCookieJar; mPendingCookies = new KHttpCookieList; mRequestList = new RequestList; mAdvicePending = false; mTimer = new QTimer(); mTimer->setSingleShot(true); connect(mTimer, &QTimer::timeout, this, &KCookieServer::slotSave); mConfig = new KConfig(QStringLiteral("kcookiejarrc")); mCookieJar->loadConfig(mConfig); mFilename = getOrCreateCookieJarDir().absoluteFilePath(QStringLiteral("cookies")); mCookieJar->loadCookies(mFilename); connect(this, &KDEDModule::windowUnregistered, this, &KCookieServer::slotDeleteSessionCookies); } KCookieServer::~KCookieServer() { slotSave(); delete mCookieJar; delete mTimer; delete mPendingCookies; delete mConfig; } bool KCookieServer::cookiesPending(const QString &url, KHttpCookieList *cookieList) { QString fqdn; QString path; // Check whether 'url' has cookies on the pending list if (mPendingCookies->isEmpty()) { return false; } if (!KCookieJar::parseUrl(url, fqdn, path)) { return false; } QStringList domains; mCookieJar->extractDomains(fqdn, domains); Q_FOREACH (const KHttpCookie &cookie, *mPendingCookies) { if (cookie.match(fqdn, domains, path)) { if (!cookieList) { return true; } cookieList->append(cookie); } } if (!cookieList) { return false; } return cookieList->isEmpty(); } void KCookieServer::addCookies(const QString &url, const QByteArray &cookieHeader, qlonglong windowId, bool useDOMFormat) { KHttpCookieList cookieList; if (useDOMFormat) { cookieList = mCookieJar->makeDOMCookies(url, cookieHeader, windowId); } else { cookieList = mCookieJar->makeCookies(url, cookieHeader, windowId); } checkCookies(&cookieList, windowId); *mPendingCookies += cookieList; if (!mAdvicePending) { mAdvicePending = true; while (!mPendingCookies->isEmpty()) { checkCookies(nullptr, windowId); } mAdvicePending = false; } } void KCookieServer::checkCookies(KHttpCookieList *cookieList) { checkCookies(cookieList, 0); } void KCookieServer::checkCookies(KHttpCookieList *cookieList, qlonglong windowId) { KHttpCookieList *list; if (cookieList) { list = cookieList; } else { list = mPendingCookies; } QMutableListIterator cookieIterator(*list); while (cookieIterator.hasNext()) { KHttpCookie &cookie = cookieIterator.next(); const KCookieAdvice advice = mCookieJar->cookieAdvice(cookie); switch (advice) { case KCookieAccept: case KCookieAcceptForSession: mCookieJar->addCookie(cookie); cookieIterator.remove(); break; case KCookieReject: cookieIterator.remove(); break; case KCookieDunno: case KCookieAsk: break; } } if (cookieList || list->isEmpty()) { return; } // Collect all pending cookies with the same host as the first pending cookie const KHttpCookie ¤tCookie = mPendingCookies->first(); KHttpCookieList currentList; currentList.append(currentCookie); const QString currentHost = currentCookie.host(); QList shownCookies; shownCookies << 0; for (int i = 1 /*first already done*/; i < mPendingCookies->count(); ++i) { const KHttpCookie &cookie = (*mPendingCookies)[i]; if (cookie.host() == currentHost) { currentList.append(cookie); shownCookies << i; } } //qDebug() << shownCookies; KCookieWin *kw = new KCookieWin(nullptr, currentList, mCookieJar->preferredDefaultPolicy(), mCookieJar->showCookieDetails()); if (windowId > 0) { KWindowSystem::setMainWindow(kw, windowId); } KCookieAdvice userAdvice = kw->advice(mCookieJar, currentCookie); delete kw; // Save the cookie config if it has changed mCookieJar->saveConfig(mConfig); // Apply the user's choice to all cookies that are currently // queued for this host (or just the first one, if the user asks for that). QMutableListIterator cookieIterator2(*mPendingCookies); int pendingCookieIndex = -1; while (cookieIterator2.hasNext()) { ++pendingCookieIndex; KHttpCookie &cookie = cookieIterator2.next(); if (cookie.host() != currentHost) { continue; } if (mCookieJar->preferredDefaultPolicy() == KCookieJar::ApplyToShownCookiesOnly && !shownCookies.contains(pendingCookieIndex)) { // User chose "only those cookies", and this one was added while the dialog was up -> skip break; } switch (userAdvice) { case KCookieAccept: case KCookieAcceptForSession: // Store the user's choice for the cookie. // This is only used to check later if the cookie should expire // at the end of the session. The choice is not saved on disk. cookie.setUserSelectedAdvice(userAdvice); mCookieJar->addCookie(cookie); cookieIterator2.remove(); break; case KCookieReject: cookieIterator2.remove(); break; case KCookieDunno: case KCookieAsk: qCWarning(KIO_COOKIEJAR) << "userAdvice not accept or reject, this should never happen!"; break; } } // Check if we can handle any request QMutableListIterator requestIterator(*mRequestList); while (requestIterator.hasNext()) { CookieRequest *request = requestIterator.next(); if (!cookiesPending(request->url)) { const QString res = mCookieJar->findCookies(request->url, request->DOM, request->windowId); QDBusConnection::sessionBus().send(request->reply.createReply(res)); delete request; requestIterator.remove(); } } saveCookieJar(); } void KCookieServer::slotSave() { if (mCookieJar->changed()) { mCookieJar->saveCookies(mFilename); } } void KCookieServer::saveCookieJar() { if (mTimer->isActive()) { return; } mTimer->start(1000 * 60 * SAVE_DELAY); } void KCookieServer::putCookie(QStringList &out, const KHttpCookie &cookie, const QList &fields) { foreach (int i, fields) { switch (i) { case CF_DOMAIN : out << cookie.domain(); break; case CF_NAME : out << cookie.name(); break; case CF_PATH : out << cookie.path(); break; case CF_HOST : out << cookie.host(); break; case CF_VALUE : out << cookie.value(); break; case CF_EXPIRE : out << QString::number(cookie.expireDate()); break; case CF_PROVER : out << QString::number(cookie.protocolVersion()); break; case CF_SECURE : out << QString::number(cookie.isSecure() ? 1 : 0); break; default : out << QString(); } } } bool KCookieServer::cookieMatches(const KHttpCookie &c, const QString &domain, const QString &fqdn, const QString &path, const QString &name) { const bool hasDomain = !domain.isEmpty(); return (((hasDomain && c.domain() == domain) || fqdn == c.host()) && (c.path() == path) && (c.name() == name) && (!c.isExpired())); } // DBUS function QString KCookieServer::listCookies(const QString &url) { return findCookies(url, 0); } // DBUS function QString KCookieServer::findCookies(const QString &url, qlonglong windowId) { if (cookiesPending(url)) { CookieRequest *request = new CookieRequest; message().setDelayedReply(true); request->reply = message(); request->url = url; request->DOM = false; request->windowId = windowId; mRequestList->append(request); return QString(); // Talk to you later :-) } QString cookies = mCookieJar->findCookies(url, false, windowId); saveCookieJar(); return cookies; } // DBUS function QStringList KCookieServer::findDomains() { QStringList result; Q_FOREACH (const QString &domain, mCookieJar->getDomainList()) { // Ignore domains that have policy set for but contain // no cookies whatsoever... const KHttpCookieList *list = mCookieJar->getCookieList(domain, QString()); if (list && !list->isEmpty()) { result << domain; } } return result; } // DBUS function QStringList KCookieServer::findCookies(const QList &fields, const QString &_domain, const QString &fqdn, const QString &path, const QString &name) { QStringList result; const bool allCookies = name.isEmpty(); const QStringList domainList = _domain.split(QLatin1Char(' ')); if (allCookies) { Q_FOREACH (const QString &domain, domainList) { const KHttpCookieList *list = mCookieJar->getCookieList(domain, fqdn); if (!list) { continue; } Q_FOREACH (const KHttpCookie &cookie, *list) { if (cookie.isExpired()) { continue; } putCookie(result, cookie, fields); } } } else { Q_FOREACH (const QString &domain, domainList) { const KHttpCookieList *list = mCookieJar->getCookieList(domain, fqdn); if (!list) { continue; } Q_FOREACH (const KHttpCookie &cookie, *list) { if (cookie.isExpired()) { continue; } if (cookieMatches(cookie, domain, fqdn, path, name)) { putCookie(result, cookie, fields); break; } } } } return result; } // DBUS function QString KCookieServer::findDOMCookies(const QString &url) { return findDOMCookies(url, 0); } // DBUS function QString KCookieServer::findDOMCookies(const QString &url, qlonglong windowId) { // We don't wait for pending cookies because it locks up konqueror // which can cause a deadlock if it happens to have a popup-menu up. // Instead we just return pending cookies as if they had been accepted already. KHttpCookieList pendingCookies; cookiesPending(url, &pendingCookies); return mCookieJar->findCookies(url, true, windowId, &pendingCookies); } // DBUS function void KCookieServer::addCookies(const QString &arg1, const QByteArray &arg2, qlonglong arg3) { addCookies(arg1, arg2, arg3, false); } // DBUS function void KCookieServer::deleteCookie(const QString &domain, const QString &fqdn, const QString &path, const QString &name) { KHttpCookieList *cookieList = mCookieJar->getCookieList(domain, fqdn); if (cookieList && !cookieList->isEmpty()) { KHttpCookieList::Iterator itEnd = cookieList->end(); for (KHttpCookieList::Iterator it = cookieList->begin(); it != itEnd; ++it) { if (cookieMatches(*it, domain, fqdn, path, name)) { mCookieJar->eatCookie(it); saveCookieJar(); break; } } } } // DBUS function void KCookieServer::deleteCookiesFromDomain(const QString &domain) { mCookieJar->eatCookiesForDomain(domain); saveCookieJar(); } // Qt function void KCookieServer::slotDeleteSessionCookies(qlonglong windowId) { deleteSessionCookies(windowId); } // DBUS function void KCookieServer::deleteSessionCookies(qlonglong windowId) { mCookieJar->eatSessionCookies(windowId); saveCookieJar(); } void KCookieServer::deleteSessionCookiesFor(const QString &fqdn, qlonglong windowId) { mCookieJar->eatSessionCookies(fqdn, windowId); saveCookieJar(); } // DBUS function void KCookieServer::deleteAllCookies() { mCookieJar->eatAllCookies(); saveCookieJar(); } // DBUS function void KCookieServer::addDOMCookies(const QString &url, const QByteArray &cookieHeader, qlonglong windowId) { addCookies(url, cookieHeader, windowId, true); } // DBUS function bool KCookieServer::setDomainAdvice(const QString &url, const QString &advice) { QString fqdn; QString dummy; if (KCookieJar::parseUrl(url, fqdn, dummy)) { QStringList domains; mCookieJar->extractDomains(fqdn, domains); mCookieJar->setDomainAdvice(domains[domains.count() > 3 ? 3 : 0], KCookieJar::strToAdvice(advice)); // Save the cookie config if it has changed mCookieJar->saveConfig(mConfig); return true; } return false; } // DBUS function QString KCookieServer::getDomainAdvice(const QString &url) { KCookieAdvice advice = KCookieDunno; QString fqdn; QString dummy; if (KCookieJar::parseUrl(url, fqdn, dummy)) { QStringList domains; mCookieJar->extractDomains(fqdn, domains); QStringListIterator it(domains); while ((advice == KCookieDunno) && it.hasNext()) { // Always check advice in both ".domain" and "domain". Note // that we only want to check "domain" if it matches the // fqdn of the requested URL. const QString &domain = it.next(); - if (domain.at(0) == '.' || domain == fqdn) { + if (domain.at(0) == QLatin1Char('.') || domain == fqdn) { advice = mCookieJar->getDomainAdvice(domain); } } if (advice == KCookieDunno) { advice = mCookieJar->getGlobalAdvice(); } } return KCookieJar::adviceToStr(advice); } // DBUS function void KCookieServer::reloadPolicy() { mCookieJar->loadConfig(mConfig, true); } // DBUS function void KCookieServer::shutdown() { deleteLater(); } #include "kcookieserver.moc" diff --git a/src/ioslaves/http/kcookiejar/kcookiewin.cpp b/src/ioslaves/http/kcookiejar/kcookiewin.cpp index 07b50a43..10123456 100644 --- a/src/ioslaves/http/kcookiejar/kcookiewin.cpp +++ b/src/ioslaves/http/kcookiejar/kcookiewin.cpp @@ -1,387 +1,387 @@ /* This file is part of KDE Copyright (C) 2000- Waldo Bastian Copyright (C) 2000- Dawit Alemayehu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ //---------------------------------------------------------------------------- // // KDE File Manager -- HTTP Cookie Dialogs // The purpose of the QT_NO_TOOLTIP and QT_NO_WHATSTHIS ifdefs is because // this file is also used in Konqueror/Embedded. One of the aims of // Konqueror/Embedded is to be a small as possible to fit on embedded // devices. For this it's also useful to strip out unneeded features of // Qt, like for example QToolTip or QWhatsThis. The availability (or the // lack thereof) can be determined using these preprocessor defines. // The same applies to the QT_NO_ACCEL ifdef below. I hope it doesn't make // too much trouble... (Simon) #include "kcookiewin.h" #include "kcookiejar.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include enum { AcceptedForSession = QDialog::Accepted + 1 }; KCookieWin::KCookieWin(QWidget *parent, KHttpCookieList cookieList, int defaultButton, bool showDetails) : QDialog(parent) { setModal(true); setObjectName(QStringLiteral("cookiealert")); setWindowTitle(i18n("Cookie Alert")); setWindowIcon(QIcon::fromTheme(QStringLiteral("preferences-web-browser-cookies"))); // all cookies in the list should have the same window at this time, so let's take the first if (cookieList.first().windowIds().count() > 0) { KWindowSystem::setMainWindow(this, cookieList.first().windowIds().first()); } else { // No window associated... make sure the user notices our dialog. KWindowSystem::setState(winId(), NET::KeepAbove); KUserTimestamp::updateUserTimestamp(); } const int count = cookieList.count(); const KHttpCookie &cookie = cookieList.first(); QString host(cookie.host()); - int pos = host.indexOf(':'); + const int pos = host.indexOf(QLatin1Char(':')); if (pos > 0) { QString portNum = host.left(pos); host.remove(0, pos + 1); - host += ':'; + host += QLatin1Char(':'); host += portNum; } QString txt = QStringLiteral(""); txt += i18ncp("%2 hostname, %3 optional cross domain suffix (translated below)", "

You received a cookie from
" "%2%3
" "Do you want to accept or reject this cookie?

", "

You received %1 cookies from
" "%2%3
" "Do you want to accept or reject these cookies?

", count, QUrl::fromAce(host.toLatin1()), cookie.isCrossDomain() ? i18nc("@item:intext cross domain cookie", " [Cross Domain]") : QString()); txt += QLatin1String(""); QVBoxLayout *topLayout = new QVBoxLayout; // This may look wrong, but it makes the dialogue automatically // shrink when the details are shown and then hidden again. topLayout->setSizeConstraint(QLayout::SetFixedSize); setLayout(topLayout); QFrame *vBox1 = new QFrame(this); topLayout->addWidget(vBox1); m_detailsButton = new QPushButton; - m_detailsButton->setText(i18n("Details") + " >>"); + m_detailsButton->setText(i18n("Details") + QLatin1String(" >>")); m_detailsButton->setIcon(QIcon::fromTheme(QStringLiteral("dialog-information"))); #ifndef QT_NO_TOOLTIP m_detailsButton->setToolTip(i18n("See or modify the cookie information")); #endif connect(m_detailsButton, &QAbstractButton::clicked, this, &KCookieWin::slotToggleDetails); QPushButton *sessionOnlyButton = new QPushButton; sessionOnlyButton->setText(i18n("Accept for this &session")); sessionOnlyButton->setIcon(QIcon::fromTheme(QStringLiteral("chronometer"))); #ifndef QT_NO_TOOLTIP sessionOnlyButton->setToolTip(i18n("Accept cookie(s) until the end of the current session")); #endif connect(sessionOnlyButton, &QAbstractButton::clicked, this, &KCookieWin::slotSessionOnlyClicked); QDialogButtonBox *buttonBox = new QDialogButtonBox(this); buttonBox->addButton(m_detailsButton, QDialogButtonBox::ActionRole); buttonBox->addButton(sessionOnlyButton, QDialogButtonBox::ActionRole); buttonBox->setStandardButtons(QDialogButtonBox::Yes | QDialogButtonBox::No); QPushButton *but = buttonBox->button(QDialogButtonBox::Yes); but->setText(i18n("&Accept")); connect(but, &QAbstractButton::clicked, this, &QDialog::accept); but = buttonBox->button(QDialogButtonBox::No); but->setText(i18n("&Reject")); connect(but, &QAbstractButton::clicked, this, &QDialog::reject); topLayout->addWidget(buttonBox); QVBoxLayout *vBox1Layout = new QVBoxLayout(vBox1); vBox1Layout->setSpacing(-1); vBox1Layout->setMargin(0); // Cookie image and message to user QFrame *hBox = new QFrame(vBox1); vBox1Layout->addWidget(hBox); QHBoxLayout *hBoxLayout = new QHBoxLayout(hBox); hBoxLayout->setSpacing(0); hBoxLayout->setMargin(0); QLabel *icon = new QLabel(hBox); hBoxLayout->addWidget(icon); icon->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-warning")).pixmap(style()->pixelMetric(QStyle::PM_LargeIconSize))); icon->setAlignment(Qt::AlignCenter); icon->setFixedSize(2 * icon->sizeHint()); QFrame *vBox = new QFrame(hBox); QVBoxLayout *vBoxLayout = new QVBoxLayout(vBox); vBoxLayout->setSpacing(0); vBoxLayout->setMargin(0); hBoxLayout->addWidget(vBox); QLabel *lbl = new QLabel(txt, vBox); vBoxLayout->addWidget(lbl); lbl->setAlignment(Qt::AlignCenter); // Cookie Details dialog... m_detailView = new KCookieDetail(cookieList, count, vBox1); vBox1Layout->addWidget(m_detailView); m_detailView->hide(); // Cookie policy choice... QGroupBox *m_btnGrp = new QGroupBox(i18n("Apply Choice To"), vBox1); vBox1Layout->addWidget(m_btnGrp); QVBoxLayout *vbox = new QVBoxLayout; txt = (count == 1) ? i18n("&Only this cookie") : i18n("&Only these cookies"); m_onlyCookies = new QRadioButton(txt, m_btnGrp); vbox->addWidget(m_onlyCookies); #ifndef QT_NO_WHATSTHIS m_onlyCookies->setWhatsThis(i18n("Select this option to only accept or reject this cookie. " "You will be prompted again if you receive another cookie.")); #endif m_allCookiesDomain = new QRadioButton(i18n("All cookies from this do&main"), m_btnGrp); vbox->addWidget(m_allCookiesDomain); #ifndef QT_NO_WHATSTHIS m_allCookiesDomain->setWhatsThis(i18n("Select this option to accept or reject all cookies from " "this site. Choosing this option will add a new policy for " "the site this cookie originated from. This policy will be " "permanent until you manually change it from the System Settings.")); #endif m_allCookies = new QRadioButton(i18n("All &cookies"), m_btnGrp); vbox->addWidget(m_allCookies); #ifndef QT_NO_WHATSTHIS m_allCookies->setWhatsThis(i18n("Select this option to accept/reject all cookies from " "anywhere. Choosing this option will change the global " "cookie policy for all cookies until you manually change " "it from the System Settings.")); #endif m_btnGrp->setLayout(vbox); switch (defaultButton) { case KCookieJar::ApplyToShownCookiesOnly: m_onlyCookies->setChecked(true); break; case KCookieJar::ApplyToCookiesFromDomain: m_allCookiesDomain->setChecked(true); break; case KCookieJar::ApplyToAllCookies: m_allCookies->setChecked(true); break; default: m_onlyCookies->setChecked(true); break; } if (showDetails) { slotToggleDetails(); } } KCookieWin::~KCookieWin() { } KCookieAdvice KCookieWin::advice(KCookieJar *cookiejar, const KHttpCookie &cookie) { const int result = exec(); cookiejar->setShowCookieDetails(!m_detailView->isHidden()); KCookieAdvice advice; switch (result) { case QDialog::Accepted: advice = KCookieAccept; break; case AcceptedForSession: advice = KCookieAcceptForSession; break; default: advice = KCookieReject; break; } KCookieJar::KCookieDefaultPolicy preferredPolicy = KCookieJar::ApplyToShownCookiesOnly; if (m_allCookiesDomain->isChecked()) { preferredPolicy = KCookieJar::ApplyToCookiesFromDomain; cookiejar->setDomainAdvice(cookie, advice); } else if (m_allCookies->isChecked()) { preferredPolicy = KCookieJar::ApplyToAllCookies; cookiejar->setGlobalAdvice(advice); } cookiejar->setPreferredDefaultPolicy(preferredPolicy); return advice; } KCookieDetail::KCookieDetail(const KHttpCookieList &cookieList, int cookieCount, QWidget *parent) : QGroupBox(parent) { setTitle(i18n("Cookie Details")); QGridLayout *grid = new QGridLayout(this); grid->addItem(new QSpacerItem(0, fontMetrics().lineSpacing()), 0, 0); grid->setColumnStretch(1, 3); QLabel *label = new QLabel(i18n("Name:"), this); grid->addWidget(label, 1, 0); m_name = new QLineEdit(this); m_name->setReadOnly(true); m_name->setMaximumWidth(fontMetrics().maxWidth() * 25); grid->addWidget(m_name, 1, 1); //Add the value label = new QLabel(i18n("Value:"), this); grid->addWidget(label, 2, 0); m_value = new QLineEdit(this); m_value->setReadOnly(true); m_value->setMaximumWidth(fontMetrics().maxWidth() * 25); grid->addWidget(m_value, 2, 1); label = new QLabel(i18n("Expires:"), this); grid->addWidget(label, 3, 0); m_expires = new QLineEdit(this); m_expires->setReadOnly(true); m_expires->setMaximumWidth(fontMetrics().maxWidth() * 25); grid->addWidget(m_expires, 3, 1); label = new QLabel(i18n("Path:"), this); grid->addWidget(label, 4, 0); m_path = new QLineEdit(this); m_path->setReadOnly(true); m_path->setMaximumWidth(fontMetrics().maxWidth() * 25); grid->addWidget(m_path, 4, 1); label = new QLabel(i18n("Domain:"), this); grid->addWidget(label, 5, 0); m_domain = new QLineEdit(this); m_domain->setReadOnly(true); m_domain->setMaximumWidth(fontMetrics().maxWidth() * 25); grid->addWidget(m_domain, 5, 1); label = new QLabel(i18n("Exposure:"), this); grid->addWidget(label, 6, 0); m_secure = new QLineEdit(this); m_secure->setReadOnly(true); m_secure->setMaximumWidth(fontMetrics().maxWidth() * 25); grid->addWidget(m_secure, 6, 1); if (cookieCount > 1) { QPushButton *btnNext = new QPushButton(i18nc("Next cookie", "&Next >>"), this); btnNext->setFixedSize(btnNext->sizeHint()); grid->addWidget(btnNext, 8, 0, 1, 2); connect(btnNext, &QAbstractButton::clicked, this, &KCookieDetail::slotNextCookie); #ifndef QT_NO_TOOLTIP btnNext->setToolTip(i18n("Show details of the next cookie")); #endif } m_cookieList = cookieList; m_cookieNumber = 0; slotNextCookie(); } KCookieDetail::~KCookieDetail() { } void KCookieDetail::slotNextCookie() { if (m_cookieNumber == m_cookieList.count() - 1) { m_cookieNumber = 0; } else { ++m_cookieNumber; } displayCookieDetails(); } void KCookieDetail::displayCookieDetails() { const KHttpCookie &cookie = m_cookieList.at(m_cookieNumber); m_name->setText(cookie.name()); m_value->setText((cookie.value())); if (cookie.domain().isEmpty()) { m_domain->setText(i18n("Not specified")); } else { m_domain->setText(cookie.domain()); } m_path->setText(cookie.path()); QDateTime cookiedate; cookiedate.setTime_t(cookie.expireDate()); if (cookie.expireDate()) { m_expires->setText(cookiedate.toString()); } else { m_expires->setText(i18n("End of Session")); } QString sec; if (cookie.isSecure()) { if (cookie.isHttpOnly()) { sec = i18n("Secure servers only"); } else { sec = i18n("Secure servers, page scripts"); } } else { if (cookie.isHttpOnly()) { sec = i18n("Servers"); } else { sec = i18n("Servers, page scripts"); } } m_secure->setText(sec); } void KCookieWin::slotSessionOnlyClicked() { done(AcceptedForSession); } void KCookieWin::slotToggleDetails() { const QString baseText = i18n("Details"); if (!m_detailView->isHidden()) { - m_detailsButton->setText(baseText + " >>"); + m_detailsButton->setText(baseText + QLatin1String(" >>")); m_detailView->hide(); } else { - m_detailsButton->setText(baseText + " <<"); + m_detailsButton->setText(baseText + QLatin1String(" <<")); m_detailView->show(); } }