From 975913e03b8a018b3db5a7b65e7d3af61aead421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D1=80=D0=BE=D1=81=D0=BA=D1=83=D1=80=D0=BD=D1=91?= =?UTF-8?q?=D0=B2=20=D0=98=D0=BB=D1=8C=D1=8F?= Date: Tue, 9 Dec 2025 14:49:46 +0300 Subject: [PATCH 1/2] The check of polkit rules has been added If formatting and writing are permitted, a dialogue box will appear --- lib/mintstick.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/lib/mintstick.py b/lib/mintstick.py index 24ba055..03e912d 100755 --- a/lib/mintstick.py +++ b/lib/mintstick.py @@ -2,6 +2,7 @@ from unidecode import unidecode from subprocess import Popen, PIPE +import dbus import getopt import gettext import gi @@ -313,8 +314,56 @@ def update_format_button(self): else: self.go_button.set_sensitive(False) + def has_polkit_rule_format(self): + return self.check_polkit_permission('com.linuxmint.mintstick-format') + + def has_polkit_rule_write(self): + return self.check_polkit_permission('com.linuxmint.mintstick-write') + + def check_polkit_permission(self, action_id): + try: + bus = dbus.SystemBus() + polkit = bus.get_object('org.freedesktop.PolicyKit1', + '/org/freedesktop/PolicyKit1/Authority') + authority = dbus.Interface(polkit, 'org.freedesktop.PolicyKit1.Authority') + + subject = ('unix-process', { + 'pid': dbus.UInt32(os.getpid()), + 'start-time': dbus.UInt64(0) + }) + + granted, _, _ = authority.CheckAuthorization( + subject, + action_id, + {}, + 0, + '' + ) + return granted + except: + return False + + def confirm_action(self, title, question): + dialog = Gtk.MessageDialog( + parent=self.window, + flags=Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT, + type=Gtk.MessageType.QUESTION, + buttons=Gtk.ButtonsType.YES_NO, + message_format=f"{title}\n\n{question}" + ) + + response = dialog.run() + dialog.destroy() + return response == Gtk.ResponseType.YES + def do_format(self, widget): + if self.has_polkit_rule_format(): + if not self.confirm_action( + "Confirm formatting", + "Do you really want to format the device?" + ): + return if self.debug: print("DEBUG: Format %s as %s" % (self.dev, self.filesystem)) return @@ -376,6 +425,12 @@ def format_job_done(self, rc): return False def do_write(self, widget): + if self.has_polkit_rule_write(): + if not self.confirm_action( + "Confirm ISO-writing", + "Do you really want to write the ISO to the device?" + ): + return if self.debug: print("DEBUG: Write %s to %s" % (self.chooser.get_filename(), self.dev)) return From dd1c084f7de561490e6434a7cb13d2d930584719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D1=80=D0=BE=D1=81=D0=BA=D1=83=D1=80=D0=BD=D1=91?= =?UTF-8?q?=D0=B2=20=D0=98=D0=BB=D1=8C=D1=8F=20=D0=A1=D0=B5=D1=80=D0=B3?= =?UTF-8?q?=D0=B5=D0=B5=D0=B2=D0=B8=D1=87?= Date: Tue, 18 Aug 2026 19:46:22 +0300 Subject: [PATCH 2/2] Use the Polkit library instead of python-dbus check_polkit_permission() now uses gi.repository Polkit (Polkit.Authority.get_sync + check_authorization_sync) rather than calling CheckAuthorization over python-dbus manually. --- lib/mintstick.py | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/lib/mintstick.py b/lib/mintstick.py index 03e912d..9f8c674 100755 --- a/lib/mintstick.py +++ b/lib/mintstick.py @@ -2,7 +2,6 @@ from unidecode import unidecode from subprocess import Popen, PIPE -import dbus import getopt import gettext import gi @@ -322,24 +321,11 @@ def has_polkit_rule_write(self): def check_polkit_permission(self, action_id): try: - bus = dbus.SystemBus() - polkit = bus.get_object('org.freedesktop.PolicyKit1', - '/org/freedesktop/PolicyKit1/Authority') - authority = dbus.Interface(polkit, 'org.freedesktop.PolicyKit1.Authority') - - subject = ('unix-process', { - 'pid': dbus.UInt32(os.getpid()), - 'start-time': dbus.UInt64(0) - }) - - granted, _, _ = authority.CheckAuthorization( - subject, - action_id, - {}, - 0, - '' - ) - return granted + authority = Polkit.Authority.get_sync() + subject = Polkit.UnixProcess.new_for_owner(os.getpid(), 0, os.getuid()) + result = authority.check_authorization_sync( + subject, action_id, None, Polkit.CheckAuthorizationFlags.NONE, None) + return result.get_is_authorized() except: return False