3
_                 @   s   d Z dZd1Z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d2d)dZd3d*dZd4d+d,Zd5d-dZed6d.dZed7d/d0ZdS )8zHProvides 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.cleanQ c             C   s   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subject r%   6/usr/lib/python3/dist-packages/aptdaemon/policykit1.pyr   @   s    c             C   s   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pid)r   )r'   r    r!   r"   r#   r$   r%   r%   r&   r   Q   s    c                s`    fdd}|st j }|d kr(t}t |jdd}i }|j ||dd||jd	 S )Nc                sD   | \}}}|rj | n&|r0jt  njt  d S )N)callbackerrbackr   r   )Zxxx_todo_changemeZ
authorizedZ
challengedZauth_details)r    deferredr$   r%   r&   policykit_donec   s    
z,_check_authorization.<locals>.policykit_donezorg.freedesktop.PolicyKit1z%/org/freedesktop/PolicyKit1/Authority z$org.freedesktop.PolicyKit1.Authority)dbus_interfacer!   reply_handlererror_handler)dbus	SystemBusr   r   
get_objectZCheckAuthorizationr)   )r$   r    r!   r"   r#   r+   ZpkZdetailsr%   )r    r*   r$   r&   r   b   s     

r   c             C   s8   |st j }t }|jdd}|j| d|j|jd |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)r-   r.   r/   )r0   r1   r   r2   ZGetConnectionUnixProcessIDr(   r)   )r   r"   r*   Zbus_objr%   r%   r&   r   }   s    
c          
   c   sb   |st j }t| |V }td| }dd |j D }W dQ R X t|d j d }t| dS )z\Return a deferred that gets the uid of the user owning the given
    system D-Bus name.
    z/proc/%s/statusc             S   s   g | ]}|j d r|qS )zUid:)
startswith).0vr%   r%   r&   
<listcomp>   s    z*get_uid_from_dbus_name.<locals>.<listcomp>Nr   r   )r0   r1   r   open	readlinesintsplitr   )r   r"   r'   procvaluesuidr%   r%   r&   r      s    c             c   s   |st j }t| |V }td| *}|j }dd |D }dd |D }W dQ R X td| d}|j jd}W dQ R X t|d	 j d
 }	t|d	 j d
 }
t	||	|
|f dS )zzReturn a deferred that gets the pid, the uid of the user owning the
    given system D-Bus name and its command line.
    z/proc/%s/statusc             S   s   g | ]}|j d r|qS )zUid:)r3   )r4   r5   r%   r%   r&   r6      s    z0get_proc_info_from_dbus_name.<locals>.<listcomp>c             S   s   g | ]}|j d r|qS )zGid:)r3   )r4   r5   r%   r%   r&   r6      s    Nz/proc/%s/cmdlinerbzutf-8r   r   )
r0   r1   r   r7   r8   readdecoder9   r:   r   )r   r"   r'   r;   linesZ
uid_valuesZ
gid_valuesZcmdline_fileZcmdliner=   gidr%   r%   r&   get_proc_info_from_dbus_name   s    rC   )r   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   )r   NN)r   NN)N)N)N)N) __doc__
__author____all__r0   Zdeferr   r   r   errorsr   r   r   r   r   r   r   r   r   r   r   r	   ZPK_ACTION_CHANGE_CONFIGr   r   r   r   r   r   r   r   r   rC   r%   r%   r%   r&   <module>   sV                 
 


