1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
/*
SPDX-FileCopyrightText: 2019 Filip Fila <filipfila.kde@gmail.com>
SPDX-FileCopyrightText: 2013 Reza Fatahilah Shah <rshah0385@kireihana.com>
SPDX-FileCopyrightText: 2011, 2012 David Edmundson <kde@davidedmundson.co.uk>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "sddmauthhelper.h"
#include "src/config.h"
#include <unistd.h>
#include <QBuffer>
#include <QDBusUnixFileDescriptor>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QMimeDatabase>
#include <QMimeType>
#include <QSharedPointer>
#include <KArchive>
#include <KCompressionDevice>
#include <KConfig>
#include <KConfigGroup>
#include <KLazyLocalizedString>
#include <KLocalizedString>
#include <KTar>
#include <KUser>
#include <KZip>
static QSharedPointer<KConfig> openConfig(const QString &filePath)
{
// if the sddm.conf.d folder doesn't exist we fail to set the right permissions for kde_settings.conf
QFileInfo fileLocation(filePath);
QDir dir(fileLocation.absolutePath());
if (!dir.exists()) {
QDir().mkpath(dir.path());
}
QFile file(filePath);
if (!file.exists()) {
// If we are creating the config file, ensure it is world-readable: if
// we don't do that, KConfig will create a file which is only readable
// by root
file.open(QIODevice::WriteOnly);
file.close();
file.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::ReadOther);
}
// in case the file has already been created with wrong permissions
else if (!(file.permissions() & QFile::ReadOwner & QFile::WriteOwner & QFile::ReadGroup & QFile::ReadOther)) {
file.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::ReadOther);
}
return QSharedPointer<KConfig>(new KConfig(file.fileName(), KConfig::SimpleConfig));
}
static QString SddmUserCheck()
{
// check for sddm user; return empty string if user not present
// we have to check with QString and isEmpty() instead of QDir and exists() because
// QDir returns "." and true for exists() in the case of a non-existent user;
const QString sddmHomeDirPath = KUser("sddm").homeDir();
if (sddmHomeDirPath.isEmpty()) {
qDebug() << "Cannot proceed, user 'sddm' does not exist. Please check your SDDM install.";
return QString();
} else {
return sddmHomeDirPath;
}
}
void SddmAuthHelper::copyDirectoryRecursively(const QString &source, const QString &destination, QSet<QString> &done)
{
if (done.contains(source)) {
return;
}
done.insert(source);
const QDir sourceDir(source);
const auto entries = sourceDir.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files);
for (const auto &entry : entries) {
const QString destinationPath = destination + QLatin1Char('/') + entry.fileName();
if (entry.isFile()) {
copyFile(entry.absoluteFilePath(), destinationPath);
} else {
QDir().mkpath(destinationPath);
copyDirectoryRecursively(entry.absoluteFilePath(), destinationPath, done);
}
}
}
void SddmAuthHelper::copyFile(const QString &source, const QString &destination)
{
KUser sddmUser(QStringLiteral("sddm"));
if (QFile::exists(destination)) {
QFile::remove(destination);
}
if (!QFile::copy(source, destination)) {
qWarning() << "Could not copy" << source << "to" << destination;
}
const char *destinationConverted = destination.toLocal8Bit().data();
if (chown(destinationConverted, sddmUser.userId().nativeId(), sddmUser.groupId().nativeId())) {
return;
}
}
ActionReply SddmAuthHelper::sync(const QVariantMap &args)
{
// abort if user not present
const QString sddmHomeDirPath = SddmUserCheck();
if (sddmHomeDirPath.isEmpty()) {
return ActionReply::HelperErrorReply();
}
// In plasma-framework, ThemePrivate::useCache documents the requirement to
// clear the cache when colors change while the app that uses them isn't running;
// that condition applies to the SDDM greeter here, so clear the cache if it
// exists to make sure SDDM has a fresh state
QDir sddmCacheLocation(sddmHomeDirPath + QStringLiteral("/.cache"));
if (sddmCacheLocation.exists()) {
sddmCacheLocation.removeRecursively();
}
// create SDDM config directory if it does not exist
QDir sddmConfigLocation(sddmHomeDirPath + QStringLiteral("/.config"));
if (!sddmConfigLocation.exists()) {
QDir().mkpath(sddmConfigLocation.path());
}
// copy fontconfig (font, font rendering)
if (!args[QStringLiteral("fontconfig")].isNull()) {
QDir fontconfigSource(args[QStringLiteral("fontconfig")].toString());
QStringList sourceFileEntries = fontconfigSource.entryList(QDir::Files);
QStringList sourceDirEntries = fontconfigSource.entryList(QDir::AllDirs);
QDir fontconfigDestination(sddmConfigLocation.path() + QStringLiteral("/fontconfig"));
if (!fontconfigDestination.exists()) {
QDir().mkpath(fontconfigDestination.path());
}
if (sourceDirEntries.count() != 0) {
for (int i = 0; i < sourceDirEntries.count(); i++) {
QString directoriesSource = fontconfigSource.path() + QDir::separator() + sourceDirEntries[i];
QString directoriesDestination = fontconfigDestination.path() + QDir::separator() + sourceDirEntries[i];
fontconfigSource.mkpath(directoriesDestination);
copyFile(directoriesSource, directoriesDestination);
}
}
if (sourceFileEntries.count() != 0) {
for (int i = 0; i < sourceFileEntries.count(); i++) {
QString filesSource = fontconfigSource.path() + QDir::separator() + sourceFileEntries[i];
QString filesDestination = fontconfigDestination.path() + QDir::separator() + sourceFileEntries[i];
copyFile(filesSource, filesDestination);
}
}
}
// copy kdeglobals (color scheme)
if (!args[QStringLiteral("kdeglobals")].isNull()) {
QDir kdeglobalsSource(args[QStringLiteral("kdeglobals")].toString());
QDir kdeglobalsDestination(sddmConfigLocation.path() + QStringLiteral("/kdeglobals"));
copyFile(kdeglobalsSource.path(), kdeglobalsDestination.path());
}
// copy plasmarc (icons, UI style)
if (!args[QStringLiteral("plasmarc")].isNull()) {
QDir plasmarcSource(args[QStringLiteral("plasmarc")].toString());
QDir plasmarcDestination(sddmConfigLocation.path() + QStringLiteral("/plasmarc"));
copyFile(plasmarcSource.path(), plasmarcDestination.path());
}
// If SDDM is set up to use Wayland with KWin, NumLock is controlled by this
if (!args[QStringLiteral("kcminputrc")].isNull()) {
QDir kcminputrcSource(args[QStringLiteral("kcminputrc")].toString());
QDir kcminputrcDestination(sddmConfigLocation.path() + QStringLiteral("/kcminputrc"));
copyFile(kcminputrcSource.path(), kcminputrcDestination.path());
}
// copy kscreen config
if (!args[QStringLiteral("kscreen-config")].isNull()) {
const QString destinationDir = sddmHomeDirPath + QStringLiteral("/.local/share/kscreen/");
QSet<QString> done;
copyDirectoryRecursively(args[QStringLiteral("kscreen-config")].toString(), destinationDir, done);
}
// copy KWin config
if (!args[QStringLiteral("kwinoutputconfig")].isNull()) {
QDir source(args[QStringLiteral("kwinoutputconfig")].toString());
QDir destination(sddmConfigLocation.path() + QStringLiteral("/kwinoutputconfig.json"));
copyFile(source.path(), destination.path());
}
// write cursor theme, NumLock preference, and scaling DPI to config file
ActionReply reply = ActionReply::HelperErrorReply();
QSharedPointer<KConfig> sddmConfig = openConfig(args[QStringLiteral("kde_settings.conf")].toString());
QSharedPointer<KConfig> sddmOldConfig = openConfig(args[QStringLiteral("sddm.conf")].toString());
QMap<QString, QVariant>::const_iterator iterator;
for (iterator = args.constBegin(); iterator != args.constEnd(); ++iterator) {
if (iterator.key() == QLatin1String("kde_settings.conf")) {
continue;
}
QStringList configFields = iterator.key().split(QLatin1Char('/'));
if (configFields.size() != 3) {
continue;
}
QSharedPointer<KConfig> config;
QString fileName = configFields[0];
QString groupName = configFields[1];
QString keyName = configFields[2];
if (fileName == QLatin1String("kde_settings.conf")) {
if (iterator.value().isValid()) {
sddmConfig->group(groupName).writeEntry(keyName, iterator.value());
} else {
sddmConfig->group(groupName).deleteEntry(keyName);
}
sddmOldConfig->group(groupName).deleteEntry(keyName);
}
}
sddmOldConfig->sync();
sddmConfig->sync();
return ActionReply::SuccessReply();
}
ActionReply SddmAuthHelper::reset(const QVariantMap &args)
{
// abort if user not present
const QString sddmHomeDirPath = SddmUserCheck();
if (sddmHomeDirPath.isEmpty()) {
return ActionReply::HelperErrorReply();
}
QDir sddmConfigLocation(sddmHomeDirPath + QStringLiteral("/.config"));
QDir fontconfigDir(args[QStringLiteral("sddmUserConfig")].toString() + QStringLiteral("/fontconfig"));
fontconfigDir.removeRecursively();
QFile::remove(sddmConfigLocation.path() + QStringLiteral("/kdeglobals"));
QFile::remove(sddmConfigLocation.path() + QStringLiteral("/plasmarc"));
QDir(sddmHomeDirPath + QStringLiteral("/.local/share/kscreen/")).removeRecursively();
// remove cursor theme, NumLock preference, and scaling DPI from config file
ActionReply reply = ActionReply::HelperErrorReply();
QSharedPointer<KConfig> sddmConfig = openConfig(args[QStringLiteral("kde_settings.conf")].toString());
QSharedPointer<KConfig> sddmOldConfig = openConfig(args[QStringLiteral("sddm.conf")].toString());
QMap<QString, QVariant>::const_iterator iterator;
for (iterator = args.constBegin(); iterator != args.constEnd(); ++iterator) {
if (iterator.key() == QLatin1String("kde_settings.conf")) {
continue;
}
QStringList configFields = iterator.key().split(QLatin1Char('/'));
if (configFields.size() != 3) {
continue;
}
QSharedPointer<KConfig> config;
QString fileName = configFields[0];
QString groupName = configFields[1];
QString keyName = configFields[2];
if (fileName == QLatin1String("kde_settings.conf")) {
sddmConfig->group(groupName).deleteEntry(keyName);
sddmOldConfig->group(groupName).deleteEntry(keyName);
}
}
sddmOldConfig->sync();
sddmConfig->sync();
return ActionReply::SuccessReply();
}
ActionReply SddmAuthHelper::save(const QVariantMap &args)
{
ActionReply reply = ActionReply::HelperErrorReply();
QSharedPointer<KConfig> sddmConfig = openConfig(QString{QLatin1String(SDDM_CONFIG_DIR "/") + QStringLiteral("kde_settings.conf")});
QSharedPointer<KConfig> sddmOldConfig = openConfig(QStringLiteral(SDDM_CONFIG_FILE));
QSharedPointer<KConfig> themeConfig;
QString themeConfigFile = args[QStringLiteral("theme.conf.user")].toString();
if (!themeConfigFile.isEmpty()) {
themeConfig = openConfig(themeConfigFile);
}
QMap<QString, QVariant>::const_iterator iterator;
for (iterator = args.constBegin(); iterator != args.constEnd(); ++iterator) {
if (iterator.key() == QLatin1String("kde_settings.conf") || iterator.key() == QLatin1String("theme.conf.user")) {
continue;
}
QStringList configFields = iterator.key().split(QLatin1Char('/'));
if (configFields.size() != 3) {
continue;
}
QString fileName = configFields[0];
QString groupName = configFields[1];
QString keyName = configFields[2];
// if there is an identical keyName in "sddm.conf" we want to delete it so SDDM doesn't read from the old file
// hierarchically SDDM prefers "etc/sddm.conf" to "/etc/sddm.conf.d/some_file.conf"
if (fileName == QLatin1String("kde_settings.conf")) {
sddmConfig->group(groupName).writeEntry(keyName, iterator.value());
sddmOldConfig->group(groupName).deleteEntry(keyName);
} else if (fileName == QLatin1String("theme.conf.user") && !themeConfig.isNull()) {
QFileInfo themeConfigFileInfo(themeConfigFile);
QDir configRootDirectory = themeConfigFileInfo.absoluteDir();
if (keyName == QLatin1String("background")) {
QFileInfo newBackgroundFileInfo(iterator.value().toString());
QString previousBackground = themeConfig->group(groupName).readEntry(keyName);
bool backgroundChanged = newBackgroundFileInfo.fileName() != previousBackground;
if (backgroundChanged) {
if (!previousBackground.isEmpty()) {
QString previousBackgroundPath = configRootDirectory.filePath(previousBackground);
if (QFile::remove(previousBackgroundPath)) {
qDebug() << "Removed previous background " << previousBackgroundPath;
}
}
if (newBackgroundFileInfo.exists()) {
QString newBackgroundPath = configRootDirectory.filePath(newBackgroundFileInfo.fileName());
qDebug() << "Copying background from " << newBackgroundFileInfo.absoluteFilePath() << " to " << newBackgroundPath;
if (QFile::copy(newBackgroundFileInfo.absoluteFilePath(), newBackgroundPath)) {
QFile::setPermissions(newBackgroundPath, QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::ReadOther);
themeConfig->group(groupName).writeEntry(keyName, newBackgroundFileInfo.fileName());
}
} else {
themeConfig->group(groupName).deleteEntry(keyName);
}
}
} else {
themeConfig->group(groupName).writeEntry(keyName, iterator.value());
}
}
}
sddmOldConfig->sync();
sddmConfig->sync();
if (!themeConfig.isNull()) {
themeConfig->sync();
}
return ActionReply::SuccessReply();
}
ActionReply SddmAuthHelper::installtheme(const QVariantMap &args)
{
const QDBusUnixFileDescriptor themefileFd = args[QStringLiteral("filedescriptor")].value<QDBusUnixFileDescriptor>();
QFile theme;
if (!themefileFd.isValid()) {
return ActionReply::HelperErrorReply();
}
if (!theme.open(themefileFd.fileDescriptor(), QIODevice::ReadOnly)) {
return ActionReply::HelperErrorReply();
}
const QString themesBaseDir = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("sddm/themes"), QStandardPaths::LocateDirectory);
QDir dir(themesBaseDir);
if (!dir.exists()) {
return ActionReply::HelperErrorReply();
}
QByteArray themeData = theme.readAll();
QBuffer themeBuffer(&themeData);
QMimeDatabase db;
QMimeType mimeType = db.mimeTypeForData(themeData);
qWarning() << "Postinstallation: uncompress the file";
QScopedPointer<QIODevice> filterDev;
KCompressionDevice::CompressionType compressionType = KCompressionDevice::compressionTypeForMimeType(mimeType.name());
if (compressionType == KCompressionDevice::None) {
// KCompressionDevice::None may indicate an uncompressed Tar or any other archive format
filterDev.reset(new QBuffer(&themeBuffer.buffer()));
} else {
filterDev.reset(new KCompressionDevice(&themeBuffer, false, compressionType));
}
QStringList installedPaths;
{
QScopedPointer<KArchive> archive;
// there must be a better way to do this? If not, make a static bool KZip::supportsMimeType(const QMimeType &type);?
// or even a factory class in KArchive
if (mimeType.inherits(QStringLiteral("application/zip"))) {
archive.reset(new KZip(filterDev.data()));
} else if (mimeType.inherits(QStringLiteral("application/tar")) || mimeType.inherits(QStringLiteral("application/x-gzip"))
|| mimeType.inherits(QStringLiteral("application/x-bzip")) || mimeType.inherits(QStringLiteral("application/x-lzma"))
|| mimeType.inherits(QStringLiteral("application/x-xz")) || mimeType.inherits(QStringLiteral("application/x-bzip-compressed-tar"))
|| mimeType.inherits(QStringLiteral("application/x-compressed-tar"))) {
archive.reset(new KTar(filterDev.data()));
} else {
auto e = ActionReply::HelperErrorReply();
e.setErrorDescription(QString::fromUtf8(kli18n("Invalid theme package").untranslatedText()));
return e;
}
if (!archive->open(QIODevice::ReadOnly)) {
auto e = ActionReply::HelperErrorReply();
e.setErrorDescription(QString::fromUtf8(kli18n("Could not open file").untranslatedText()));
return e;
}
auto directory = archive->directory();
// some basic validation
// the top level should only have folders, and those folders should contain a valid metadata.desktop file
// if we get anything else, abort everything before copying
const auto entries = directory->entries();
for (const QString &name : entries) {
auto entry = directory->entry(name);
if (!entry->isDirectory()) {
auto e = ActionReply::HelperErrorReply();
e.setErrorDescription(QString::fromUtf8(kli18n("Invalid theme package").untranslatedText()));
return e;
}
auto subDirectory = static_cast<const KArchiveDirectory *>(entry);
auto metadataFile = subDirectory->file(QStringLiteral("metadata.desktop"));
if (!metadataFile || !metadataFile->data().contains("[SddmGreeterTheme]")) {
auto e = ActionReply::HelperErrorReply();
e.setErrorDescription(QString::fromUtf8(kli18n("Invalid theme package").untranslatedText()));
return e;
}
installedPaths.append(themesBaseDir + QLatin1Char('/') + name);
}
if (!directory->copyTo(themesBaseDir)) {
auto e = ActionReply::HelperErrorReply();
e.setErrorDescription(QString::fromUtf8(kli18n("Could not decompress archive").untranslatedText()));
return e;
}
}
auto rc = ActionReply::SuccessReply();
rc.addData(QStringLiteral("installedPaths"), installedPaths);
return rc;
}
ActionReply SddmAuthHelper::uninstalltheme(const QVariantMap &args)
{
const QString themePath = args[QStringLiteral("filePath")].toString();
const QString themesBaseDir = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("sddm/themes"), QStandardPaths::LocateDirectory);
QDir dir(themePath);
if (!dir.exists()) {
return ActionReply::HelperErrorReply();
}
// validate the themePath is directly inside the themesBaseDir
QDir baseDir(themesBaseDir);
if (baseDir.absoluteFilePath(dir.dirName()) != dir.absolutePath()) {
return ActionReply::HelperErrorReply();
}
if (!dir.removeRecursively()) {
return ActionReply::HelperErrorReply();
}
return ActionReply::SuccessReply();
}
KAUTH_HELPER_MAIN("org.kde.kcontrol.kcmsddm", SddmAuthHelper)
#include "moc_sddmauthhelper.cpp"
|