Advertisement
Guest User

Untitled

a guest
Nov 9th, 2019
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. def _check_polkit_privilege(self, sender, conn, privilege):
  2.     # Get Peer PID
  3.     if self.dbus_info is None:
  4.         # Get DBus Interface and get info thru that
  5.         self.dbus_info = dbus.Interface(conn.get_object("org.freedesktop.DBus",
  6.                                                         "/org/freedesktop/DBus/Bus", False),
  7.                                         "org.freedesktop.DBus")
  8.     pid = self.dbus_info.GetConnectionUnixProcessID(sender)
  9.  
  10.     # Query polkit
  11.     if self.polkit is None:
  12.         self.polkit = dbus.Interface(dbus.SystemBus().get_object(
  13.         "org.freedesktop.PolicyKit1",
  14.         "/org/freedesktop/PolicyKit1/Authority", False),
  15.                                      "org.freedesktop.PolicyKit1.Authority")
  16.  
  17.     # Check auth against polkit; if it times out, try again
  18.     try:
  19.         auth_response = self.polkit.CheckAuthorization(
  20.             ("unix-process", {"pid": dbus.UInt32(pid, variant_level=1),
  21.                               "start-time": dbus.UInt64(0, variant_level=1)}),
  22.             privilege, {"AllowUserInteraction": "true"}, dbus.UInt32(1), "", timeout=600)
  23.         print(auth_response)
  24.         (is_auth, _, details) = auth_response
  25.     except dbus.DBusException as e:
  26.         if e._dbus_error_name == "org.freedesktop.DBus.Error.ServiceUnknown":
  27.             # polkitd timeout, retry
  28.             self.polkit = None
  29.             return self._check_polkit_privilege(sender, conn, privilege)
  30.         else:
  31.             # it's another error, propagate it
  32.             raise
  33.  
  34.     if not is_auth:
  35.         # Aww, not authorized :(
  36.         print(":(")
  37.         return False
  38.  
  39.     print("Successful authorization!")
  40.     return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement