o
    rag)                     @   s,  d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddl
mZ ddlmZmZmZ G dd deZG dd	 d	eZG d
d deZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Ze d krd!d"  d#d"  e
!  e D ]Z"e#e" qdS dS )$z4Handle GnuPG keys used to trust signed repositories.    )print_functionN)gettext)ListOptionalTuplec                   @   s   e Zd ZdS )AptKeyErrorN)__name__
__module____qualname__ r   r   */usr/lib/python3/dist-packages/apt/auth.pyr   +   s    r   c                   @   s   e Zd ZdZdS )AptKeyIDTooShortErrorz!Internal class do not rely on it.N)r   r	   r
   __doc__r   r   r   r   r   /   s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )
TrustedKeyzRepresents a trusted key.c                 C   s    || _ t|| _|| _|| _d S )N)Zraw_name_namekeyiddate)selfr   r   r   r   r   r   __init__7   s   

zTrustedKey.__init__c                 C   s   d| j | j| jf S )Nz%s
%s %s)r   r   r   )r   r   r   r   __str__?   s   zTrustedKey.__str__N)r   r	   r
   r   r   r   r   r   r   r   r   3   s    r   c            	      O   s  d}t jddg}||  tj }d|d< d|d< zet jdd	kr@tj	d
dd}|
t j d |  |j|d< tj||dtjtjtjd}|dd}||\}}|jrltd|jd|||f |rttj
| | W |dur|  S S |dur|  w w )z0Run the apt-key script with the given arguments.NzDir::Bin::Apt-Keyz/usr/bin/apt-keyCLANG1Z$APT_KEY_DONT_WARN_ON_DANGEROUS_USAGEZDir/zapt-keyz.conf)prefixsuffixzUTF-8Z
APT_CONFIGT)envuniversal_newlinesstdinstdoutstderrr   zGThe apt-key script failed with return code %s:
%s
stdout: %s
stderr: %s )apt_pkgZconfigZ	find_fileextendosenvironcopyZfind_dirtempfileZNamedTemporaryFilewritedumpencodeflushr   
subprocessPopenPIPEgetcommunicate
returncoder   joinsysr!   stripclose)	argskwargsconfcmdr   procr   outputr!   r   r   r   _call_apt_key_scriptD   sH   






r=   c                 C   s@   t j| std|  t | t jstd|  td|  dS )zImport a GnuPG key file to trust repositores signed by it.

    Keyword arguments:
    filename -- the absolute path to the public GnuPG key file
    z An absolute path is required: %szKey file cannot be accessed: %saddN)r%   pathabspathr   accessR_OKr=   )filenamer   r   r   add_key_from_fileq   s
   rD   c              
   C   s`   t  }zzt| || W n ty    w W dd }tj||d dS dd }tj||d w )zImport a GnuPG key file to trust repositores signed by it.

    Keyword arguments:
    keyid -- the long keyid (fingerprint) of the key, e.g.
             A1BD8E9D78F7FE5C3E65D8AF8B48AD6246925553
    keyserver -- the URL or hostname of the key server
    c                 S   s$   t |d tr|d jtjkrd S  )N   )
isinstanceOSErrorerrnoENOENT)funcr?   exc_infor   r   r   onerror   s
   z'add_key_from_keyserver.<locals>.onerror)rL   N)r(   Zmkdtemp_add_key_from_keyserver	Exceptionshutilrmtree)r   	keyservertmp_keyring_dirrL   r   r   r   add_key_from_keyserver   s   	rS   c                 C   sJ  t | dddddk rtdtj|d}tj|d}dd	d
d|g}t|d|d|d|d| g }|dkrBtd|| f tj|d}t|d|d|d| g }|dkr_td| tj	|d|ddddg tj
dd d }d }	| D ]}
|
dr|
dd }	 nqz| dd }|	|krtd||f t| d S )Nr"    0xg      D@z,Only fingerprints (v4, 160bit) are supportedzsecring.gpgzpubring.gpgZgpgz--no-default-keyringz--no-optionsz	--homedirz--secret-keyringz	--keyringz--keyserverz--recvr   zrecv from '%s' failed for '%s'zexport-keyring.gpgz--outputz--exportzexport of '%s' failedz--fingerprint--batch--fixed-list-mode--with-colonsT)r    r   zfpr::	   )lenreplacer   r%   r?   r3   r-   callr   r.   r/   r1   
splitlines
startswithsplitupperrD   )r   rQ   rR   Ztmp_secret_keyringZtmp_keyringZgpg_default_optionsresZtmp_export_keyringr<   Zgot_fingerprintlineZsigning_key_fingerprintr   r   r   rM      sn   

	

rM   c                 C   s   t ddddd| d dS )zImport a GnuPG key to trust repositores signed by it.

    Keyword arguments:
    content -- the content of the GnuPG public key
    advz--quietrV   z--import-)r   Nr=   )Zcontentr   r   r   add_key   s   
rg   c                 C   s   t d|  dS )zRemove a GnuPG key to no longer trust repositores signed by it.

    Keyword arguments:
    fingerprint -- the fingerprint identifying the key
    ZrmNrf   Zfingerprintr   r   r   
remove_key   s   ri   c                 C   s
   t d| S )zxReturn the GnuPG key in text format.

    Keyword arguments:
    fingerprint -- the fingerprint identifying the key
    Zexportrf   rh   r   r   r   
export_key   s   
rj   c                   C      t dS )a  Update the local keyring with the archive keyring and remove from
    the local keyring the archive keys which are no longer valid. The
    archive keyring is shipped in the archive-keyring package of your
    distribution, e.g. the debian-archive-keyring package in Debian.
    updaterf   r   r   r   r   rl     s   rl   c                   C   rk   )ay  Work similar to the update command above, but get the archive
    keyring from an URI instead and validate it against a master key.
    This requires an installed wget(1) and an APT build configured to
    have a server to fetch from and a master keyring to validate. APT
    in Debian does not support this command and relies on update
    instead, but Ubuntu's APT does.
    z
net-updaterf   r   r   r   r   
net_update  s   	rm   c                  C   sx   t ddddd} g }| dD ]*}|d}|d d	kr |d
 }|d dkr9|d }|d }t|||}|| q|S )zaReturns a list of TrustedKey instances for each key which is
    used to trust repositories.
    rd   rX   rV   rW   z--list-keys
rY   r   Zpub   uidrZ      )r=   r`   r   append)r<   rb   rc   Zfieldsr   rp   Zcreation_datekeyr   r   r   	list_keys  s   

rt   __main__c                   C   rk   )Nz;Ubuntu Archive Automatic Signing Key <ftpmaster@ubuntu.com>r   r   r   r   r   <lambda>0      rw   c                   C   rk   )Nz:Ubuntu CD Image Automatic Signing Key <cdimage@ubuntu.com>rv   r   r   r   r   rw   1  rx   )$r   Z
__future__r   rH   r%   os.pathrO   r-   r4   r(   r#   r   r   typingr   r   r   rN   r   r   objectr   r=   rD   rS   rM   rg   ri   rj   rl   rm   rt   r   ZinitZtrusted_keyprintr   r   r   r   <module>   sB   -H




