U
    &c*                     @   s  d Z ddlmZ zddlmZ W n ek
r4   Y nX 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 ejjdkreZz$ddlmZmZmZ e e e W n ek
r   Y nX 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( qtdS )&z4Handle GnuPG keys used to trust signed repositories.    )print_function)OptionalN)gettext   )ListTupleUnionc                   @   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__F   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__N   s    zTrustedKey.__str__N)r
   r   r   r   r   r   r   r   r   r   r   B   s   r   c            	      O   s0  d}t jddg}||  tj }d|d< d|d< zt jdd	krt	j
d
dd}|t j d |  |j|d< tj||dtjtjtjd}|dd}tjjdk rt|tr|d}||\}}|jrtd|jd|||f n|rtj| | W S |dk	r*|  X dS )z0Run the apt-key script with the given arguments.NzDir::Bin::Apt-Keyz/usr/bin/apt-keyCZ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       zutf-8zGThe apt-key script failed with return code %s:
%s
stdout: %s
stderr: %s )apt_pkgZconfigZ	find_fileextendosenvironcopycloseZfind_dirtempfileZNamedTemporaryFilewritedumpencodeflushr   
subprocessPopenPIPEgetsysversion_infomajor
isinstanceunicodecommunicate
returncoder	   joinr"   strip)	argskwargsconfcmdr   procr    outputr"   r   r   r   _call_apt_key_scriptS   sN    

 


   
rC   c                 C   s@   t j| std|  t | t js2t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_OKrC   )filenamer   r   r   add_key_from_file   s
    rJ   c              
   C   sR   t  }z,zt| || W n tk
r0    Y nX W 5 dd }tj||d X dS )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  d S )N   )r7   OSErrorerrnoZENOENT)funcrE   exc_infor   r   r   onerror   s
    z'add_key_from_keyserver.<locals>.onerror)rP   N)r+   ZmkdtempshutilZrmtree_add_key_from_keyserver	Exception)r   	keyservertmp_keyring_dirrP   r   r   r   add_key_from_keyserver   s    	rV   c                 C   sN  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rtd|| f tj|d}t|d|d|d| g }|dkrtd| tj	|d|ddddg tj
dd d }d }	| D ]"}
|
dr|
dd }	 qq| dd }|	|krBtd||f t| d S )Nr$    Z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'   rE   r;   r0   callr	   r1   r2   r9   
splitlines
startswithsplitupperrJ   )r   rT   rU   Ztmp_secret_keyringZtmp_keyringZgpg_default_optionsresZtmp_export_keyringrB   Zgot_fingerprintlineZsigning_key_fingerprintr   r   r   rR      s           
   
 	


 rR   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--quietrX   z--import-)r    NrC   )Zcontentr   r   r   add_key   s
      ri   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rmNrh   Zfingerprintr   r   r   
remove_key   s    rk   c                 C   s
   t d| S )zxReturn the GnuPG key in text format.

    Keyword arguments:
    fingerprint -- the fingerprint identifying the key
    Zexportrh   rj   r   r   r   
export_key	  s    rl   c                   C   s   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.
    updaterh   r   r   r   r   rm     s    rm   c                   C   s   t dS )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-updaterh   r   r   r   r   
net_update  s    	rn   c                  C   sx   t ddddd} g }| dD ]T}|d}|d d	kr@|d
 }|d dkr|d }|d }t|||}|| q|S )zaReturns a list of TrustedKey instances for each key which is
    used to trust repositories.
    rf   rZ   rX   rY   z--list-keys
r[   r   Zpub   uidr\      )rC   rb   r   append)rB   rd   re   Zfieldsr   rq   Zcreation_datekeyr   r   r   	list_keys)  s     
ru   __main__c                   C   s   t dS )Nz;Ubuntu Archive Automatic Signing Key <ftpmaster@ubuntu.com>r   r   r   r   r   <lambda>B      rx   c                   C   s   t dS )Nz:Ubuntu CD Image Automatic Signing Key <cdimage@ubuntu.com>rw   r   r   r   r   rx   C  ry   )*r   Z
__future__r   typingr   ImportErrorrM   r'   os.pathrQ   r0   r4   r+   r%   r   r   r5   r6   strr8   r   r   r   rS   r	   r   objectr   rC   rJ   rV   rR   ri   rk   rl   rm   rn   ru   r
   ZinitZtrusted_keyprintr   r   r   r   <module>   sT   0H




