
    kd                         d Z dZdZddlZddlmZmZmZ ddlm	Z	m
Z
 dZd	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZ	 	 ddZ	 	 ddZddZddZedd       Zedd       Zy)zHProvides access to PolicyKit privilege mangement using gdefer Deferreds.z%Sebastian Heinlein <devel@glatzor.de>)check_authorization_by_namecheck_authorization_by_pidget_pid_from_dbus_nameget_uid_from_dbus_name!CHECK_AUTH_ALLOW_USER_INTERACTIONCHECK_AUTH_NONEPK_ACTION_ADD_REMOVE_VENDOR_KEYPK_ACTION_CANCEL_FOREIGNPK_ACTION_CHANGE_REPOSITORYPK_ACTION_CHANGE_CONIFG!PK_ACTION_GET_TRUSTED_VENDOR_KEYSPK_ACTION_INSTALL_FILE$PK_ACTION_INSTALL_OR_REMOVE_PACKAGES(PK_ACTION_INSTALL_PACKAGES_FROM_NEW_REPO/PK_ACTION_INSTALL_PACKAGES_FROM_HIGH_TRUST_REPO$PK_ACTION_INSTALL_PURCHASED_PACKAGESPK_ACTION_UPDATE_CACHEPK_ACTION_UPGRADE_PACKAGESPK_ACTION_SET_PROXYPK_ACTION_CLEAN    N)Deferredinline_callbacksreturn_value   )NotAuthorizedErrorAuthorizationFailedz)org.debian.apt.install-or-remove-packagesz)org.debian.apt.install-purchased-packagesz-org.debian.apt.install-packages-from-new-repoz/org.debian.apt.install-packages.high-trust-repozorg.debian.apt.install-filezorg.debian.apt.upgrade-packageszorg.debian.apt.update-cachezorg.debian.apt.cancel-foreignz&org.debian.apt.get-trusted-vendor-keysz org.debian.apt.change-repositoryzorg.debian.apt.change-configzorg.debian.apt.set-proxyzorg.debian.apt.cleanc                 ,    dd| if}t        |||||      S )a  Check if the given sender is authorized for the specified action.

    If the sender is not authorized raise NotAuthorizedError.

    Keyword arguments:
    dbus_name -- D-Bus name of the subject
    action_id -- the PolicyKit policy name of the action
    timeout -- time in seconds for the user to authenticate
    bus -- the D-Bus connection (defaults to the system bus)
    flags -- optional flags to control the authentication process
    zsystem-bus-namename_check_authorization)	dbus_name	action_idtimeoutbusflagssubjects         6/usr/lib/python3/dist-packages/aptdaemon/policykit1.pyr   r   @   s&     !69"56GGS%HH    c                 ,    dd| if}t        |||||      S )a  Check if the given process is authorized for the specified action.

    If the sender is not authorized raise NotAuthorizedError.

    Keyword arguments:
    pid -- id of the process
    action_id -- the PolicyKit policy name of the action
    timeout -- time in seconds for the user to authenticate
    bus -- the D-Bus connection (defaults to the system bus)
    flags -- optional flags to control the authentication process
    zunix-processpidr   )r*   r"   r#   r$   r%   r&   s         r'   r   r   Q   s%     s|,GGS%HHr(   c                       fd}|st        j                         }|t        }t               |j	                  dd      }i }|j                   ||dd||j                  	       S )Nc                     | \  }}}|rj                  |       y |rj                  t                     y j                  t                     y N)callbackerrbackr   r   )xxx_todo_changeme
authorized
challengedauth_detailsr"   deferredr&   s       r'   policykit_donez,_check_authorization.<locals>.policykit_donec   sO    1B.Zl+0)DE/CDr(   zorg.freedesktop.PolicyKit1z%/org/freedesktop/PolicyKit1/Authority z$org.freedesktop.PolicyKit1.Authority)dbus_interfacer#   reply_handlererror_handler)dbus	SystemBusr   r   
get_objectCheckAuthorizationr/   )	r&   r"   r#   r$   r%   r5   pkdetailsr4   s	   ``      @r'   r    r    b   s|    E nn}1zH	4?
ABGGUB=$&&  ( Or(   c                     |st        j                         }t               }|j                  dd      }|j	                  | d|j
                  |j                         |S )zZReturn a deferred that gets the id of process owning the given
    system D-Bus name.
    zorg.freedesktop.DBusz/org/freedesktop/DBus/Bus)r7   r8   r9   )r:   r;   r   r<   GetConnectionUnixProcessIDr.   r/   )r!   r$   r4   bus_objs       r'   r   r   }   s`     nnzHnn38:G&&y6L5=5F5F5=5E5E ' G Or(   c              #   V  K   |st        j                         }t        | |      }t        d|z        5 }|j	                         D cg c]  }|j                  d      s| }}ddd       t        d   j                         d         }t        |       yc c}w # 1 sw Y   9xY ww)z\Return a deferred that gets the uid of the user owning the given
    system D-Bus name.
    /proc/%s/statusUid:Nr   r   )	r:   r;   r   open	readlines
startswithintsplitr   )r!   r$   r*   procvvaluesuids          r'   r   r      s     
 nn&y#6
6C	#%	& G$!^^-Ff1E!FFG
fQioo"
#C GG Gs4   3B)BBB#B%3B)BB&"B)c              #   r  K   |st        j                         }t        | |      }t        d|z        5 }|j	                         }|D cg c]  }|j                  d      s| }}|D cg c]  }|j                  d      s| }}ddd       t        d|z  d      5 }|j                         j                  d      }	ddd       t        d   j                         d	         }
t        d   j                         d	         }t        ||
|	f       yc c}w c c}w # 1 sw Y   xY w# 1 sw Y   mxY ww)
zzReturn a deferred that gets the pid, the uid of the user owning the
    given system D-Bus name and its command line.
    rD   rE   zGid:Nz/proc/%s/cmdlinerbzutf-8r   r   )r:   r;   r   rF   rG   rH   readdecoderI   rJ   r   )r!   r$   r*   rK   linesrL   
uid_values
gid_valuescmdline_filecmdlinerN   gids               r'   get_proc_info_from_dbus_namerY      s*    
 nn&y#6
6C	#%	& @$ !&?A!,,v*>a?
?!&?A!,,v*>a?
?@ 
 3&	- 6##%,,W56
jm!!#A&
'C
jm!!#A&
'C#sC)* @?@ @6 6s_   3D7D
D!D%D+DDDD7 D+?AD7
DD($D7+D40D7)iQ NNr-   ) __doc__
__author____all__r:   deferr   r   r   errorsr   r   r   r   r   r   r   r   r   r	   r   r
   PK_ACTION_CHANGE_CONFIGr   r   r   r   r   r   r    r   r   rY    r(   r'   <module>ra      s    O" 5
5  : : ; 0 % 0 % 4 ) 6 06 > 6 : $L !@ 8 0 ($% ! JN&*I" CG%)I"6  
 
 + +r(   