U
    ̋_                     @   s   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dZd#ddZd$ddZd%ddZed&ddZed'd d!ZdS )(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_NONEZPK_ACTION_ADD_REMOVE_VENDOR_KEYPK_ACTION_CANCEL_FOREIGNPK_ACTION_CHANGE_REPOSITORYZ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    r   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pidr   )r&   r   r    r!   r"   r#   r$   r$   r%   r   Q   s    r   c                    s`    fdd}|st  }|d kr(t}t |dd}i }|j ||dd||jd	 S )Nc                    sD   | \}}}|r | n&|r0t  n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   b   s,        r   c                 C   s8   |st  }t }|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    r   c              	   c   sb   |st  }t| |V }td| }dd | D }W 5 Q R X t|d  d }t| dS )z\Return a deferred that gets the uid of the user owning the given
    system D-Bus name.
    /proc/%s/statusc                 S   s   g | ]}| d r|qS zUid:
startswith.0vr$   r$   r%   
<listcomp>   s     
 z*get_uid_from_dbus_name.<locals>.<listcomp>r   r   N)r0   r1   r   open	readlinesintsplitr   )r   r!   r&   procvaluesuidr$   r$   r%   r      s    r   c              	   c   s   |st  }t| |V }td| *}| }dd |D }dd |D }W 5 Q R X td| d}| d}W 5 Q R X t|d  d	 }	t|d  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.
    r3   c                 S   s   g | ]}| d r|qS r4   r5   r7   r$   r$   r%   r:      s     
 z0get_proc_info_from_dbus_name.<locals>.<listcomp>c                 S   s   g | ]}| d r|qS )zGid:r5   r7   r$   r$   r%   r:      s     
 z/proc/%s/cmdlinerbzutf-8r   r   N)
r0   r1   r   r;   r<   readdecoder=   r>   r   )r   r!   r&   r?   linesZ
uid_valuesZ
gid_valuesZcmdline_fileZcmdlinerA   gidr$   r$   r%   get_proc_info_from_dbus_name   s    rG   )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   rG   r$   r$   r$   r%   <module>   sH     
  


