2
0
Эх сурвалжийг харах

Added optional polkit rules file to avoid authorization.

Malte Veerman 6 жил өмнө
parent
commit
1d64c8be37

+ 10 - 0
CMakeLists.txt

@@ -10,11 +10,13 @@ option(BUILD_KCM "Build the KCM" OFF)
 option(BUILD_PLASMOID "Build the plasmoid" OFF)
 option(BUILD_HELPER "Build the KHelper" ON)
 option(INSTALL_SHARED "Install the shared parts" ON)
+option(INSTALL_POLKIT "Install polkit files and rules" OFF)
 
 #variables
 set(STANDARD_SERVICE_NAME "fancontrol" CACHE STRING "The name of the systemd service for the fancontrol script")
 set(STANDARD_CONFIG_FILE "/etc/fancontrol" CACHE STRING "The location of the standard config file for the fancontrol script")
 set(STANDARD_HELPER_ID "fancontrol.gui.helper" CACHE STRING "The standard id for the KAuth helper")
+set(POLKIT_GROUP_NAME "fancontrol" CACHE STRING "The group which is granted elevated permissions by polkit to manipulate fancontrol")
 add_definitions(-DSTANDARD_SERVICE_NAME="${STANDARD_SERVICE_NAME}")
 add_definitions(-DSTANDARD_CONFIG_FILE="${STANDARD_CONFIG_FILE}")
 add_definitions(-DSTANDARD_HELPER_ID="${STANDARD_HELPER_ID}")
@@ -127,5 +129,13 @@ if(INSTALL_SHARED)
 endif(INSTALL_SHARED)
 
 
+#install polkit files
+if(INSTALL_POLKIT)
+
+    add_subdirectory(polkit)
+
+endif(INSTALL_POLKIT)
+
+
 #summary
 feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)

+ 1 - 0
README.md

@@ -5,6 +5,7 @@ Furthermore it communicates with systemd via dbus to control the fancontrol serv
 
 KAuth currently doesn't support install prefixes other than where KAuth itself was installed.
 If you want to use another install prefix, you have to run the application as root or another user with the necessary privileges to avoid the KAuth helper.
+If you want to avoid authorizing yourself when using the helper you can set the option -DINSTALL_POLKIT=true. This will install a polkit rules file allowing members of the group 'fancontrol' to edit the config file and manipulate the systemd service. You can change the group name with the -DPOLKIT_GROUP_NAME option. Service name and config file can be set with the options -DSTANDARD_SERVICE_NAME and -DSTANDARD_CONFIG_FILE.
 
 If you want to compile without systemd support set the option -DNO_SYSTEMD=true.
 

+ 32 - 2
import/src/systemdcommunicator.cpp

@@ -26,7 +26,9 @@
 #include <QtCore/QTimer>
 #include <QtDBus/QDBusArgument>
 #include <QtDBus/QDBusInterface>
+#include <QtDBus/QDBusMetaType>
 #include <QtDBus/QDBusReply>
+#include <QtDBus/QDBusVariant>
 
 #include <KAuth/KAuthExecuteJob>
 #include <KI18n/KLocalizedString>
@@ -261,6 +263,34 @@ bool SystemdCommunicator::dbusAction(const QString &method, const QVariantList &
         return false;
     }
 
+    const auto systembus = QDBusConnection::systemBus();
+    QScopedPointer<QDBusInterface> iface(new QDBusInterface (QStringLiteral("org.freedesktop.systemd1"),
+                                         QStringLiteral("/org/freedesktop/systemd1"),
+                                         QStringLiteral("org.freedesktop.systemd1.Manager"),
+                                         systembus,
+                                         this));
+    QDBusMessage dbusmessage;
+    bool success = false;
+    QString error;
+
+    if (iface->isValid())
+    {
+        if (arguments.isEmpty())
+            dbusmessage = iface->call(QDBus::AutoDetect, method);
+        else
+            dbusmessage = iface->callWithArgumentList(QDBus::AutoDetect, method, arguments);
+
+        if (dbusmessage.type() == QDBusMessage::ErrorMessage)
+        {
+            success = false;
+            error = dbusmessage.errorMessage();
+            emit this->error("DBus error: " + error);
+        }
+    }
+
+    if (success)
+        return true;
+
     auto action = newFancontrolAction();
     QVariantMap map;
     map[QStringLiteral("action")] = "dbusaction";
@@ -269,7 +299,7 @@ bool SystemdCommunicator::dbusAction(const QString &method, const QVariantList &
     action.setArguments(map);
 
     const auto job = action.execute();
-    bool success = job->exec();
+    success = job->exec();
     if (success)
     {
         if (method == QStringLiteral("EnableUnitFiles") || method == QStringLiteral("DisableUnitFiles"))
@@ -284,7 +314,7 @@ bool SystemdCommunicator::dbusAction(const QString &method, const QVariantList &
         }
     }
     else
-        emit error(i18n("KAuth::ExecuteJob error! Code: %1\nAdditional Info: %2", job->error(), job->errorString()), true);
+        emit this->error(i18n("KAuth::ExecuteJob error! Code: %1\nAdditional Info: %2", job->error(), job->errorString()), true);
 
     return success;
 }

+ 3 - 0
polkit/CMakeLists.txt

@@ -0,0 +1,3 @@
+configure_file(org.kde.fancontrol.rules.in org.kde.fancontrol.rules @ONLY)
+
+install(FILES "${CMAKE_CURRENT_BINARY_DIR}/org.kde.fancontrol.rules" DESTINATION "/etc/polkit-1/rules.d")

+ 14 - 0
polkit/org.kde.fancontrol.rules.in

@@ -0,0 +1,14 @@
+polkit.addRule(function(action, subject) {
+    if (action.id == "@STANDARD_HELPER_ID@.action" && subject.isInGroup("@POLKIT_GROUP_NAME@")) {
+        return polkit.Result.YES;
+    }
+
+    if (action.id == "org.freedesktop.systemd1.manage-units") {
+        if (action.lookup("unit") == "@STANDARD_SERVICE_NAME@.service" && subject.isInGroup("@POLKIT_GROUP_NAME@")) {
+            var verb = action.lookup("verb");
+            if (verb == "start" || verb == "stop" || verb == "restart") {
+                return polkit.Result.YES;
+            }
+        }
+    }
+});