

`k%                 @   sj  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	 m
 Z Gd d   d e  Z Gd 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 rfd d   d  d   e	 j   x e   D] Z e e  qRWd S)!z4Handle GnuPG keys used to trust signed repositories.    )print_functionN)gettextc               @   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 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__0   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__7   s    zTrustedKey.__str__N)r   r   r   __doc__r   r   r   r   r   r	   r
   ,   s   r
   c        	      O   s  d } t  j j d d  g } | j |   t j j   } d | d <zKt  j j d  d k r t j	 d d	 d
 d  } | j
 t  j j   j d   | j   | j | d <t j | d | d d d t j d t j d t j } | j d d  } t j j d k  r%t | t  r%| j d  } | j |  \ } } | j rnt d | j d j |  | | f   n | rt j j
 |  | j   SWd | d k	 r| j   Xd S)z0Run the apt-key script with the given arguments.NzDir::Bin::Apt-Keyz/usr/bin/apt-keyCZLANGZDir/prefixzapt-keysuffixz.confzUTF-8Z
APT_CONFIGenvuniversal_newlinesTstdinstdoutstderr   zutf-8zGThe apt-key script failed with return code %s:
%s
stdout: %s
stderr: %s )apt_pkgconfigZ	find_fileextendosenvironcopyZfind_dirtempfileZNamedTemporaryFilewritedumpencodeflushr   
subprocessPopenPIPEgetsysversion_infomajor
isinstanceZunicodecommunicate
returncoder   joinr   stripclose)	argskwargsconfcmdr   proccontentoutputr   r   r   r	   _call_apt_key_script;   s:    
	
		!	%r=   c             C   sX   t  j j |   s" t d |    t  j |  t  j  sG 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_filei   s
    rD   c             C   sG   t  j   } z& y t |  | |  Wn   Yn XWd t j |  Xd 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
    N)r$   Zmkdtemp_add_key_from_keyservershutilZrmtree)r   	keyservertmp_keyring_dirr   r   r	   add_key_from_keyserverv   s    rI   c          
   C   s  t  |   d k  r t d   t j j | d  } t j j | d  } d d d d	 | g } t j | d
 | d | d | d |  g  } | d k r t d | |  f   t j j | d  } t j | d | d | d |  g  } | d k r t d |    t j | d | d d d g d t j d d j	   d } d  }	 x7 | j
   D]) }
 |
 j d  rM|
 j d  d }	 PqMW|  j d d  j   } |	 | k rt d | | f   t |  d  S) N      z+Only long keyids (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--fingerprintz--batchz--with-colonsr   r   Tzfpr::	   Z0x g      4@)lenr   r!   r?   r3   r)   callr*   r+   r1   
splitlines
startswithsplitreplaceupperrD   )r   rG   rH   Ztmp_secret_keyringZtmp_keyringZgpg_default_optionsresZtmp_export_keyringr<   Zgot_fingerprintlineZsigning_key_fingerprintr   r   r	   rE      sR    			
	rE   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--quietz--batchz--import-r   N)r=   )r;   r   r   r	   add_key   s    rZ   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rmN)r=   )fingerprintr   r   r	   
remove_key   s    r\   c             C   s   t  d |   S)zxReturn the GnuPG key in text format.

    Keyword arguments:
    fingerprint -- the fingerprint identifying the key
    Zexport)r=   )r[   r   r   r	   
export_key   s    r]   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.
    update)r=   r   r   r   r	   r^      s    r^   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-update)r=   r   r   r   r	   
net_update   s    r_   c              C   s   t  d d d d  }  g  } xk |  j d  D]Z } | j d  } | d d k r+ t | d	 | d
 d d  | d  } | j |  q+ W| S)zaReturns a list of TrustedKey instances for each key which is
    used to trust repositories.
    rX   z--with-colonsz--batchz--list-keys
rL   r   ZpubrM      rK   N   i)r=   rS   r
   append)r<   rV   rW   Zfieldskeyr   r   r	   	list_keys   s    	(re   __main__c               C   s
   t  d  S)Nz;Ubuntu Archive Automatic Signing Key <ftpmaster@ubuntu.com>)r   r   r   r   r	   <lambda>  s    rg   c               C   s
   t  d  S)Nz:Ubuntu CD Image Automatic Signing Key <cdimage@ubuntu.com>)r   r   r   r   r	   rg     s    )r   Z
__future__r   r!   os.pathrF   r)   r-   r$   r   r   r   	Exceptionr   objectr
   r=   rD   rI   rE   rZ   r\   r]   r^   r_   re   r   ZinitZtrusted_keyprintr   r   r   r	   <module>   s6   .E
			


