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|dstt|d|S)NZ_dbus_is_methodZ_slip_polkit_auth_required)hasattrAssertionErrorsetattr)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 <scs|dkst|tjstdtfks0dks0tdksHttjsHtdks^tts^tfdd|dk rt|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, ...): ...Ncsy |||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_proxycs t|S)N)r)r)r%rrdecorateszenable_proxy..decorate) isinstance collectionsCallablerr issubclass Exceptionr)rr$r#r"r&r)r%r"r#r$rr@s*    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-rrr0s zNotAuthorizedException.__init__)rrr__doc__r.r0 __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_namer4 _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)r4r6rZ SystemBusZadd_signal_receiverr;r5r7)r1rrr_buss  z PolKit._buscCstjs|jjt_tjS)N)r4_PolKit__bus_namer<Zget_unique_name)r1rrr _bus_names zPolKit._bus_namec CsFtjs@y"tj|jj|j|j|jt_Wntj k r>YnXtjS)N) r4r8r Interfacer< get_objectr5 _dbus_path_dbus_interfacer)r1rrr _interfaces  zPolKit._interfacecCs t|jS)N)boolrC)r1rrr_polkit_presentszPolKit._polkit_presentc Cs>|jjdd}tj|d}y|j|}Wnd}YnX|S)Nzorg.freedesktop.DBusz/org/freedesktop/DBus)r<r@rr?ZGetConnectionUnixUser)r1system_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-namer:r)rErCCheckAuthorizationr>)r1Z 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)r1rrsz4PolKit.AreAuthorizationsObtainable..)rEr'tuplelistsetr)r1authorizationsZ obtainabler)r1rr s z"PolKit.AreAuthorizationsObtainableTc sd|js |dkp|j|dkSd}|r0|dO}fdd}|jjdd|if|||d||tddS) Nrrcs|\}}}|dS)Nr)argsrIrJrK) reply_handlerrrreply_cb s z7PolKit.IsSystemBusNameAuthorizedAsync..reply_cbzsystem-bus-namer:rG)rU error_handlerZtimeout)rE!_PolKit__dbus_system_bus_name_uidrCrHr) r1rFrrUrW challengerKflagsrVr)rUrr s   z%PolKit.IsSystemBusNameAuthorizedAsync)rrrr2r5rArBr8r6r=r7 classmethodr;propertyr<r>rCrErXrLr r rrrrr4s"     r4cCs tj|S)N)__polkitr )rSrrrr sTcCstj||||||S)N)r]r )rFrrUrWrYrKrrrr s )r2Z __future__rr(rr functoolsrZ constantsr__all__rrobjectr rrr r4r]r r rrrrs(     Wo