I am using Cinnamon 6.0.4. Please rewrite the polkit rules with JavaScript instead of…
- User
I am using Cinnamon 6.0.4. Please rewrite the polkit rules with JavaScript instead of the legacy pkla format.
Assistant
Alright, let's transition from the legacy
.pklaformat to the modern JavaScript.rulesformat for PolicyKit. This is a much cleaner and more powerful way to define authorization rules, giving you more granular control.PolicyKit JavaScript rules are placed in
/etc/polkit-1/rules.d/and are processed in alphanumeric order. This means files named50-my-rule.ruleswill be processed before60-another-rule.rules.Here's how to rewrite the previous rules using JavaScript.
Important Considerations for JavaScript Rules:
- File Location: All these files will go into
/etc/polkit-1/rules.d/. - Naming: Use a two-digit prefix (e.g.,
50-,60-) for ordering and a descriptive name ending in.rules. polkit.addRule(function(action, subject) { ... });: This is the core function.action: An object representing the action being performed (e.g.,action.idfor "org.freedesktop.login1.power-off").subject: An object representing the user trying to perform the action (e.g.,subject.isInGroup("sudo")).
- Return Values:
polkit.Result.YES: Allow the action.polkit.Result.NO: Deny the action.polkit.Result.AUTH_ADMIN: Require an administrator's password.polkit.Result.AUTH_SELF: Require the user's own password (useful for things like changing their own settings).
!subject.isInGroup("sudo"): This is your primary condition to identify non-admin users.
Prerequisites (Review)
- You have
xrdpandcinnamon-desktop-environmentinstalled. - Your users are added with
sudo adduser <username>and are NOT in thesudogroup. - The
/etc/xrdp/startwm.shis configured for Cinnamon.
How to Implement
-
Remove Old
.pklaFiles: First, clean up the old rules to avoid conflicts.sudo rm /etc/polkit-1/localauthority/50-local.d/01-no-power-actions.pkla sudo rm /etc/polkit-1/localauthority/50-local.d/02-no-network-changes.pkla sudo rm /etc/polkit-1/localauthority/50-local.d/03-no-user-management.pkla sudo rm /etc/polkit-1/localauthority/50-local.d/04-no-software-management.pkla sudo rm /etc/polkit-1/localauthority/50-local.d/05-no-time-changes.pkla sudo rm /etc/polkit-1/localauthority/50-local.d/06-no-drive-mounts.pkla sudo rm /etc/polkit-1/localauthority/50-local.d/07-no-printer-config.pkla -
Create New
.rulesFiles: Usesudo nanoorsudo vimto create these files in/etc/polkit-1/rules.d/. -
Restart Polkit Service (or Logout/Login): After creating/modifying rules, refresh the PolicyKit daemon:
sudo systemctl restart polkit.serviceOr simply have the affected users log out of their
xrdpsession and log back in.
The JavaScript PolicyKit Rules
1. Power Management
- File:
/etc/polkit-1/rules.d/50-no-power-actions.rules - Content:
polkit.addRule(function(action, subject) { // Deny power management actions for non-sudo users if (!subject.isInGroup("sudo")) { var powerActions = [ "org.freedesktop.login1.power-off", "org.freedesktop.login1.power-off-multiple-sessions", "org.freedesktop.login1.reboot", "org.freedesktop.login1.reboot-multiple-sessions", "org.freedesktop.login1.suspend", "org.freedesktop.login1.suspend-multiple-sessions", "org.freedesktop.login1.hibernate", "org.freedesktop.login1.hibernate-multiple-sessions" ]; if (powerActions.indexOf(action.id) !== -1) { polkit.log("Denied power action '" + action.id + "' for user " + subject.user + "."); return polkit.Result.NO; } } });
2. Network Configuration
- File:
/etc/polkit-1/rules.d/51-no-network-changes.rules - Content:
polkit.addRule(function(action, subject) { // Deny NetworkManager system settings changes for non-sudo users if (!subject.isInGroup("sudo")) { var networkActions = [ "org.freedesktop.NetworkManager.settings.modify.system", "org.freedesktop.NetworkManager.settings.modify.own", // Can affect shared settings if user creates a "system" connection "org.freedesktop.NetworkManager.settings.modify.hostname", "org.freedesktop.NetworkManager.network-control", "org.freedesktop.NetworkManager.enable-disable-wifi", "org.freedesktop.NetworkManager.enable-disable-network", "org.freedesktop.NetworkManager.sleep-wake", "org.freedesktop.NetworkManager.vpn.control", "org.freedesktop.NetworkManager.connection.new", "org.freedesktop.NetworkManager.connection.delete", "org.freedesktop.NetworkManager.connection.modify" ]; if (networkActions.indexOf(action.id) !== -1) { polkit.log("Denied network action '" + action.id + "' for user " + subject.user + "."); return polkit.Result.NO; } } });
3. User & Group Management
- File:
/etc/polkit-1/rules.d/52-no-user-management.rules - Content:
polkit.addRule(function(action, subject) { // Deny user management actions for non-sudo users if (!subject.isInGroup("sudo")) { var userManagementActions = [ "org.freedesktop.Accounts.set-enabled", "org.freedesktop.Accounts.set-locked", "org.freedesktop.Accounts.set-user-attribute", "org.freedesktop.Accounts.create-user", "org.freedesktop.Accounts.delete-user", "org.freedesktop.Accounts.modify-user-privileges", // "org.freedesktop.Accounts.change-own-password" // Uncomment if you want to prevent users from changing their OWN password ]; if (userManagementActions.indexOf(action.id) !== -1) { polkit.log("Denied user management action '" + action.id + "' for user " + subject.user + "."); return polkit.Result.NO; } } });- Note: I've commented out
org.freedesktop.Accounts.change-own-passwordby default. Most environments allow users to change their own password. If you truly want to prevent even that, uncomment the line.
- Note: I've commented out
4. Software Installation & Updates
- File:
/etc/polkit-1/rules.d/53-no-software-management.rules - Content:
polkit.addRule(function(action, subject) { // Deny software management actions for non-sudo users if (!subject.isInGroup("sudo")) { var softwareActions = [ "org.debian.apt.install-or-remove-packages", "org.debian.apt.update-cache", "org.debian.apt.upgrade-packages", "org.freedesktop.packagekit.install-packages", "org.freedesktop.packagekit.remove-packages", "org.freedesktop.packagekit.update-packages", "org.freedesktop.packagekit.repair-system", "org.freedesktop.packagekit.change-locale", "org.freedesktop.software-center.install", "org.freedesktop.software-center.remove" ]; if (softwareActions.indexOf(action.id) !== -1) { polkit.log("Denied software action '" + action.id + "' for user " + subject.user + "."); return polkit.Result.NO; } } });
5. System Time & Date
- File:
/etc/polkit-1/rules.d/54-no-time-changes.rules - Content:
polkit.addRule(function(action, subject) { // Deny time and date changes for non-sudo users if (!subject.isInGroup("sudo")) { var timeActions = [ "org.freedesktop.timedate1.set-time", "org.freedesktop.timedate1.set-timezone", "org.freedesktop.timedate1.set-ntp", "org.freedesktop.timedate1.set-local-rtc" ]; if (timeActions.indexOf(action.id) !== -1) { polkit.log("Denied time action '" + action.id + "' for user " + subject.user + "."); return polkit.Result.NO; } } });
6. Mounting/Unmounting Filesystems
- File:
/etc/polkit-1/rules.d/55-no-drive-mounts.rules - Content:
polkit.addRule(function(action, subject) { // Deny system disk mounting/unmounting actions for non-sudo users if (!subject.isInGroup("sudo")) { var mountActions = [ "org.freedesktop.udisks2.filesystem-mount-system", "org.freedesktop.udisks2.filesystem-mount-other-seat", "org.freedesktop.udisks2.filesystem-mount-options", "org.freedesktop.udisks2.filesystem-unmount-others", "org.freedesktop.udisks2.loop-setup", "org.freedesktop.udisks2.change-system-power", "org.freedesktop.udisks2.authorize" // "org.freedesktop.udisks2.filesystem-mount" // This can prevent mounting *any* drive, including personal USBs. Use with caution. ]; if (mountActions.indexOf(action.id) !== -1) { polkit.log("Denied mount action '" + action.id + "' for user " + subject.user + "."); return polkit.Result.NO; } } });- Note: This is a strong restriction. If you want users to be able to mount their own USB drives, you might need to adjust or remove
org.freedesktop.udisks2.filesystem-mount-other-seatororg.freedesktop.udisks2.filesystem-mount. The default PolicyKit rules often alloworg.freedesktop.udisks2.filesystem-mountfor active users if the device is not "system" owned. This rule explicitly tries to block broader mounting actions.
- Note: This is a strong restriction. If you want users to be able to mount their own USB drives, you might need to adjust or remove
7. Printing & Printer Configuration
- File:
/etc/polkit-1/rules.d/56-no-printer-config.rules - Content:
polkit.addRule(function(action, subject) { // Deny printer configuration actions for non-sudo users if (!subject.isInGroup("sudo")) { var printerActions = [ "org.cups.cupsd.admin", "org.cups.cupsd.set-default", "org.cups.cupsd.shutdown" // "org.cups.cupsd.print" // Uncomment if you want to prevent printing altogether. ]; if (printerActions.indexOf(action.id) !== -1) { polkit.log("Denied printer action '" + action.id + "' for user " + subject.user + "."); return polkit.Result.NO; } } });- Note:
org.cups.cupsd.printis commented out by default. This allows users to print to existing configured printers. If you want to disable printing entirely, uncomment that line.
- Note:
Important Reminder
- Testing: After applying these rules and restarting
polkit.service(or logging out/in as the test user), thoroughly test each restricted action while logged in as a normal user. - Logs: Check
journalctl -for/var/log/auth.logwhile testing. You should seepolkit.logmessages indicating that the actions were denied by your custom rules.
This set of JavaScript rules provides the same level of robust restriction as the
.pklafiles, but in a more modern, flexible, and maintainable format.- File Location: All these files will go into
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.