/************************************************************************* * Copyright (C) 2008, 2009, 2010 by Volker Lanz * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 3 of * * the License, or (at your option) any later version. * * * * This program 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 General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see .* *************************************************************************/ #include "util/guihelpers.h" #include #include #include #include #include #include #include #include #include #include #include bool checkPermissions() { if (geteuid() != 0) { // only try to gain root privileges if we have a valid (kde|gk)su(do) command and // we did not try so before: the dontsu-option is there to make sure there are no // endless loops of calling the same non-working (kde|gk)su(do) binary again and again. if (!suCommand().isEmpty() && !QCoreApplication::arguments().contains(QLatin1String("--dontsu"))) { QString argList; const QString suCmd = suCommand(); // kdesu broke backward compatibility at some point and now only works with "-c"; // kdesudo accepts either (with or without "-c"), but the gk* helpers only work // without. kdesu maintainers won't fix their app, so we need to work around that here. if (suCmd.indexOf(QStringLiteral("kdesu")) != -1) argList = QStringLiteral("-c "); argList += QCoreApplication::arguments().join(QStringLiteral(" ")) + QStringLiteral(" --dontsu"); if (QProcess::execute(suCmd, QStringList(argList)) == 0) return false; } return KMessageBox::warningContinueCancel(NULL, xi18nc("@info", "You do not have administrative privileges." "It is possible to run %1 without these privileges. " "You will, however, not be allowed to apply operations." "Do you want to continue running %1?", QGuiApplication::applicationDisplayName()), i18nc("@title:window", "No administrative privileges"), KGuiItem(i18nc("@action:button", "Run without administrative privileges"), QStringLiteral("arrow-right")), KStandardGuiItem::cancel(), QStringLiteral("runWithoutRootPrivileges")) == KMessageBox::Continue; } return true; } bool loadBackend() { if (CoreBackendManager::self()->load(Config::backend()) == false) { if (CoreBackendManager::self()->load(CoreBackendManager::defaultBackendName())) { KMessageBox::sorry(NULL, xi18nc("@info", "The configured backend plugin \"%1\" could not be loaded." "Loading the default backend plugin \"%2\" instead.", Config::backend(), CoreBackendManager::defaultBackendName()), i18nc("@title:window", "Error: Could Not Load Backend Plugin")); Config::setBackend(CoreBackendManager::defaultBackendName()); } else { KMessageBox::error(NULL, xi18nc("@info", "Neither the configured (\"%1\") nor the default (\"%2\") backend " "plugin could be loaded.Please check your installation.", Config::backend(), CoreBackendManager::defaultBackendName()), i18nc("@title:window", "Error: Could Not Load Backend Plugin")); return false; } } return true; } QString suCommand() { const QString candidates[] = { QStringLiteral("kdesu"), QStringLiteral("kdesudo"), QStringLiteral("gksudo"), QStringLiteral("gksu") }; QString rval; for (quint32 i = 0; i < sizeof(candidates) / sizeof(candidates[0]); i++) { rval = QStandardPaths::findExecutable(candidates[i]); if (QFileInfo(rval).isExecutable()) return rval; } return QString(); }