3 uAcI$@sdZddlmZddlZddlZddlmZddlmZddlm Z dd d d d d gZ ddZ dZ Gdd d e Zdeddfdd ZGdd d ejZGddde ZeZdd Zdifdd ZdS)zmThis module contains convenience decorators and functions for using PolicyKit with dbus services and clients.)absolute_importN) decorator)reduce)method_call_no_timeout require_auth enable_proxyAUTHFAIL_DONTCATCHNotAuthorizedExceptionAreAuthorizationsObtainableIsSystemBusNameAuthorizedAsynccsfdd}|S)uDecorator for DBus service methods. Specify that a user needs a specific PolicyKit authorization `polkit_auth´ to execute it.cst|d|S)NZ_slip_polkit_auth_required)setattr)method) polkit_auth/usr/lib/python3.6/polkit.pyrequire_auth_decorator/s z,require_auth..require_auth_decoratorr)rrr)rrr)s zBorg.fedoraproject.slip.dbus.service.PolKit.NotAuthorizedException.c@s eZdZdS)r N)__name__ __module__ __qualname__rrrrr <scs6fdd|dk r"t|Sfdd}|SdS)uDecorator for DBus proxy methods. Let's you (optionally) specify either a result value or an exception type and a callback which is returned, thrown or called respectively if a PolicyKit authorization doesn't exist or can't be obtained in the DBus mechanism, i.e. an appropriate DBus exception is thrown. An exception constructor may and a callback must accept an `action_id´ parameter which will be set to the id of the PolicyKit action for which authorization could not be obtained. Examples: 1) Return `False´ in the event of an authorization problem, and call `error_handler´: def error_handler(action_id=None): print "Authorization problem:", action_id class MyProxy(object): @polkit.enable_proxy(authfail_result=False, authfail_callback=error_handler) def some_method(self, ...): ... 2) Throw a `MyAuthError´ instance in the event of an authorization problem: class MyAuthError(Exception): def __init__(self, *args, **kwargs): action_id = kwargs.pop("action_id") super(MyAuthError, self).__init__(*args, **kwargs) self.action_id = action_id class MyProxy(object): @polkit.enable_proxy(authfail_exception=MyAuthError) def some_method(self, ...): ...csy |||Stjk r}zr|j}|jts2|ttd}dk rT|ddk ry|d}Wn}YnX|tkrSd}~XnXdS)N) action_id)dbus DBusExceptionZ get_dbus_name startswithAUTH_EXC_PREFIXlenr )funcpkeZexc_namerZaf_exc)authfail_callbackauthfail_exceptionauthfail_resultrr _enable_proxyvs$    z#enable_proxy.._enable_proxyNcs t|S)N)r)r)r#rrdecorateszenable_proxy..decorate)r)rr"r!r r$r)r#r r!r"rr@s 6  cs$eZdZdZdZfddZZS)r zqException which a DBus service method throws if an authorization required for executing it can't be obtained.zAorg.fedoraproject.slip.dbus.service.PolKit.NotAuthorizedExceptioncs(|jjd||_tt|j||dS)N.) __class___dbus_error_namesuperr __init__)selfrrr)r&rrr)s zNotAuthorizedException.__init__)rrr__doc__r'r) __classcell__rr)r&rr sc@seZdZdZdZdZdZdZdZdZ dZ e ddZ e dd Ze d d Ze d d Ze ddZddZddZddZdifddZdS)PolKitz"Convenience wrapper around polkit.zorg.freedesktop.PolicyKit1z%/org/freedesktop/PolicyKit1/Authorityz$org.freedesktop.PolicyKit1.AuthorityNcCs4||jkr0tjr0tjjtjdt_dt_dt_dS)N) _dbus_namer- _PolKit__busZremove_signal_receiver_PolKit__signal_receiver_PolKit__interface)clsnameZ old_ownerZ new_ownerrrr_on_name_owner_changeds zPolKit._on_name_owner_changedcCs0tjs*tjt_tjj|jdd|jdt_tjS)NZNameOwnerChangedzorg.freedesktop.DBus)Zhandler_functionZ signal_nameZdbus_interfaceZarg0)r-r/rZ SystemBusZadd_signal_receiverr4r.r0)r*rrr_buss  z PolKit._buscCstjs|jjt_tjS)N)r-_PolKit__bus_namer5Zget_unique_name)r*rrr _bus_names zPolKit._bus_namec CsFtjs@y"tj|jj|j|j|jt_Wntj k r>YnXtjS)N) r-r1r Interfacer5 get_objectr. _dbus_path_dbus_interfacer)r*rrr _interfaces  zPolKit._interfacecCs t|jS)N)boolr<)r*rrr_polkit_presentszPolKit._polkit_presentc Cs>|jjdd}tj|d}y|j|}Wnd}YnX|S)Nzorg.freedesktop.DBusz/org/freedesktop/DBus)r5r9rr8ZGetConnectionUnixUser)r*system_bus_nameZ bus_objectZ bus_interfaceZuidrrrZ__dbus_system_bus_name_uids  z!PolKit.__dbus_system_bus_name_uidcCs6|js dS|jjdd|jif|idd\}}}|p4|S)NTzsystem-bus-namer3r)r>r<CheckAuthorizationr7)r*Z authorization is_authorized is_challengedetailsrrrZ__authorization_is_obtainables  z$PolKit.__authorization_is_obtainablecs8js dSt|tttfs |f}tfdd|d}|S)NTcs|o j|S)N)$_PolKit__authorization_is_obtainable)xy)r*rrsz4PolKit.AreAuthorizationsObtainable..)r> isinstancetuplelistsetr)r*authorizationsZ obtainabler)r*rr s z"PolKit.AreAuthorizationsObtainableTc sd|js |dkp|j|dkSd}|r0|dO}fdd}|jjdd|if|||d||tddS) Nrrcs|\}}}|dS)Nr)argsrBrCrD) reply_handlerrrreply_cb s z7PolKit.IsSystemBusNameAuthorizedAsync..reply_cbzsystem-bus-namer3r@)rO error_handlerZtimeout)r>!_PolKit__dbus_system_bus_name_uidr<rAr) r*r?rrOrQ challengerDflagsrPr)rOrr s   z%PolKit.IsSystemBusNameAuthorizedAsync)rrrr+r.r:r;r1r/r6r0 classmethodr4propertyr5r7r<r>rRrEr r rrrrr-s"     r-cCs tj|S)N)__polkitr )rMrrrr sTcCstj||||||S)N)rWr )r?rrOrQrSrDrrrr s )r+Z __future__r collectionsrr functoolsrZ constantsr__all__rrobjectr rrr r-rWr r rrrrs(     Wo