ó
Á£ô_c           @   sÆ  d  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 Z d d l	 Z
 d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z d d l m Z d d l m Z e j e ƒ Z d „  Z d „  Z e j d ƒ Z  e j d e j! ƒ Z" e
 j# j$ e j% ƒ d e& f d „  ƒ  Yƒ Z' d e' f d „  ƒ  YZ( d e& f d „  ƒ  YZ) d e& f d „  ƒ  YZ* d „  Z+ d „  Z, d S(   s   Plugin common functions.iÿÿÿÿN(   t   util(   t   List(   t   achallenges(   t   crypto_util(   t   errors(   t
   interfaces(   t   reverter(   t	   constants(   t
   filesystem(   t   os(   t   PluginStoragec         C   s   |  d S(   s9   ArgumentParser options namespace (prefix of all options).t   -(    (   t   name(    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   option_namespace   s    c         C   s   |  j  d d ƒ d S(   s;   ArgumentParser dest namespace (prefix of all destinations).R   t   _(   t   replace(   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   dest_namespace   s    sX   (^127\.0\.0\.1)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)s3   ^(([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*[a-z]+$t   Pluginc           B   sq   e  Z d  Z d „  Z e j d „  ƒ Z e d „  ƒ Z e	 d „  ƒ Z
 d „  Z e	 d „  ƒ Z d „  Z d „  Z RS(	   s   Generic plugin.c         C   s   | |  _  | |  _ d  S(   N(   t   configR   (   t   selfR   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   __init__0   s    	c         C   s   d S(   sé   Add plugin arguments to the CLI argument parser.

        :param callable add: Function that proxies calls to
            `argparse.ArgumentParser.add_argument` prepending options
            with unique plugin name prefix.

        N(    (   t   clst   add(    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   add_parser_arguments4   s    c            s   ‡  ‡ f d †  } |  j  | ƒ S(   sY   Inject parser options.

        See `~.IPlugin.inject_parser_options` for docs.

        c            s%   ˆ j  d j t ˆ  ƒ |  ƒ | | Ž S(   Ns   --{0}{1}(   t   add_argumentt   formatR   (   t   arg_name_no_prefixt   argst   kwargs(   R   t   parser(    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyR   F   s    (   R   (   R   R   R   R   (    (   R   R   s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   inject_parser_options>   s    c         C   s   t  |  j ƒ S(   s9   ArgumentParser options namespace (prefix of all options).(   R   R   (   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyR   L   s    c         C   s   |  j  | S(   s'   Option name (include plugin namespace).(   R   (   R   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   option_nameQ   s    c         C   s   t  |  j ƒ S(   s;   ArgumentParser dest namespace (prefix of all destinations).(   R   R   (   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyR   U   s    c         C   s   |  j  | j d d ƒ S(   s.   Find a destination for given variable ``var``.R   R   (   R   R   (   R   t   var(    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   destZ   s    c         C   s   t  |  j |  j | ƒ ƒ S(   s0   Find a configuration value for variable ``var``.(   t   getattrR   R!   (   R   R    (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   conf`   s    (   t   __name__t
   __module__t   __doc__R   t	   jose_utilt   abstractclassmethodR   t   classmethodR   t   propertyR   R   R   R!   R#   (    (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyR   *   s   	
		t	   Installerc           B   sq   e  Z d  Z d „  Z e d „ Z d „  Z d „  Z d „  Z d d „ Z	 e
 d „  ƒ Z e
 d	 „  ƒ Z d
 „  Z RS(   s‹   An installer base class with reverter and ssl_dhparam methods defined.

    Installer plugins do not have to inherit from this class.

    c         O   sJ   t  t |  ƒ j | | Ž  t |  j |  j ƒ |  _ t j |  j ƒ |  _ d  S(   N(	   t   superR+   R   R
   R   R   t   storageR   t   Reverter(   R   R   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyR   k   s    c         C   sd   | r |  j  j } n |  j  j } y | | | ƒ Wn+ t j k
 r_ } t j t | ƒ ƒ ‚ n Xd S(   s´  Add files to a checkpoint.

        :param set save_files: set of filepaths to save
        :param str save_notes: notes about changes during the save
        :param bool temporary: True if the files should be added to a
            temporary checkpoint rather than a permanent one. This is
            usually used for changes that will soon be reverted.

        :raises .errors.PluginError: when unable to add to checkpoint

        N(   R   t   add_to_temp_checkpointt   add_to_checkpointR   t   ReverterErrort   PluginErrort   str(   R   t
   save_filest
   save_notest	   temporaryt   checkpoint_funct   err(    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyR0   p   s    c         C   sF   y |  j  j | ƒ Wn+ t j k
 rA } t j t | ƒ ƒ ‚ n Xd S(   s±   Timestamp and save changes made through the reverter.

        :param str title: Title describing checkpoint

        :raises .errors.PluginError: when an error occurs

        N(   R   t   finalize_checkpointR   R1   R2   R3   (   R   t   titleR8   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyR9   †   s    c         C   sC   y |  j  j ƒ  Wn+ t j k
 r> } t j t | ƒ ƒ ‚ n Xd S(   sÉ   Revert all previously modified files.

        Reverts all modified files that have not been saved as a checkpoint

        :raises .errors.PluginError: If unable to recover the configuration

        N(   R   t   recovery_routineR   R1   R2   R3   (   R   R8   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyR;   “   s    c         C   sC   y |  j  j ƒ  Wn+ t j k
 r> } t j t | ƒ ƒ ‚ n Xd S(   sk   Rollback temporary checkpoint.

        :raises .errors.PluginError: when unable to revert config

        N(   R   t   revert_temporary_configR   R1   R2   R3   (   R   R8   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyR<       s    i   c         C   sF   y |  j  j | ƒ Wn+ t j k
 rA } t j t | ƒ ƒ ‚ n Xd S(   sú   Rollback saved checkpoints.

        :param int rollback: Number of checkpoints to revert

        :raises .errors.PluginError: If there is a problem with the input or
            the function is unable to correctly revert the configuration

        N(   R   t   rollback_checkpointsR   R1   R2   R3   (   R   t   rollbackR8   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyR=   «   s    	c         C   s   t  j j |  j j t j ƒ S(   s(   Full absolute path to ssl_dhparams file.(   R	   t   patht   joinR   t
   config_dirR   t   SSL_DHPARAMS_DEST(   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   ssl_dhparams¹   s    c         C   s   t  j j |  j j t j ƒ S(   s:   Full absolute path to digest of updated ssl_dhparams file.(   R	   R?   R@   R   RA   R   t   UPDATED_SSL_DHPARAMS_DIGEST(   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   updated_ssl_dhparams_digest¾   s    c         C   s   t  |  j |  j t j t j ƒ S(   sJ   Copy Certbot's ssl_dhparams file into the system's config dir if required.(   t   install_version_controlled_fileRC   RE   R   t   SSL_DHPARAMS_SRCt   ALL_SSL_DHPARAMS_HASHES(   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   install_ssl_dhparamsÃ   s
    (   R$   R%   R&   R   t   FalseR0   R9   R;   R<   R=   R*   RC   RE   RI   (    (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyR+   e   s   				t   Addrc           B   sƒ   e  Z d  Z e d „ Z e d „  ƒ Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z RS(   sˆ   Represents an virtual host address.

    :param str addr: addr part of vhost address
    :param str port: port number or \*, or ""

    c         C   s   | |  _  | |  _ d  S(   N(   t   tupt   ipv6(   R   RL   RM   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyR   Ó   s    	c         C   s®   | j  d ƒ rƒ | j d ƒ } | | d  } d } t | ƒ | d k rm | | d d k rm | | d } n  |  | | f d t ƒS| j d ƒ } |  | d | d f ƒ Sd	 S(
   s   Initialize Addr from string.t   [t   ]i   t    i   t   :RM   i    N(   t
   startswitht   rfindt   lent   Truet	   partition(   R   t   str_addrt   endIndext   hostt   portRL   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt
   fromstring×   s    *c         C   s#   |  j  d r d |  j  S|  j  d S(   Ni   s   %s:%si    (   RL   (   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   __str__æ   s    c         C   s'   |  j  r  |  j ƒ  |  j d f S|  j S(   s5   Normalized representation of addr/port tuple
        i   (   RM   t   get_ipv6_explodedRL   (   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   normalized_tupleë   s    	c         C   s,   t  | |  j ƒ r( |  j ƒ  | j ƒ  k St S(   N(   t
   isinstancet	   __class__R^   RJ   (   R   t   other(    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   __eq__ò   s    c         C   s   t  |  j ƒ S(   N(   t   hashRL   (   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   __hash__ú   s    c         C   s   |  j  d S(   s    Return addr part of Addr object.i    (   RL   (   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   get_addrý   s    c         C   s   |  j  d S(   s   Return port.i   (   RL   (   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   get_port  s    c         C   s    |  j  |  j d | f |  j ƒ S(   s6   Return new address object with same addr and new port.i    (   R`   RL   RM   (   R   RZ   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   get_addr_obj  s    c         C   s+   | j  d ƒ } | j d ƒ } |  j | ƒ S(   s7   Return IPv6 address in normalized form, helper functionRN   RO   (   t   lstript   rstript   _explode_ipv6(   R   t   addr(    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   _normalize_ipv6	  s    c         C   s*   |  j  r& d j |  j |  j d ƒ ƒ Sd S(   s   Return IPv6 in normalized formRQ   i    RP   (   RM   R@   Rl   RL   (   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyR]     s    	c         C   së   d d d d d d d d g } | j  d ƒ } t | ƒ t | ƒ k r[ | d t | ƒ !} n  t } xƒ t | ƒ D]u \ } } | sŒ t } qn n  t | ƒ d k r° | j d ƒ } n  | sÉ t | ƒ | | <qn t | ƒ | | t | ƒ <qn W| S(   s#   Explode IPv6 address for comparisont   0RQ   i    i   (   t   splitRT   RJ   t	   enumerateRU   Rh   R3   (   R   Rk   t   resultt	   addr_listt   append_to_endt   it   block(    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyRj     s    (   R$   R%   R&   RJ   R   R)   R[   R\   R^   Rb   Rd   Re   Rf   Rg   Rl   R]   Rj   (    (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyRK   Ì   s   									t   ChallengePerformerc           B   s,   e  Z d  Z d „  Z d d „ Z d „  Z RS(   sv  Abstract base for challenge performers.

    :ivar configurator: Authenticator and installer plugin
    :ivar achalls: Annotated challenges
    :vartype achalls: `list` of `.KeyAuthorizationAnnotatedChallenge`
    :ivar indices: Holds the indices of challenges from a larger array
        so the user of the class doesn't have to.
    :vartype indices: `list` of `int`

    c         C   s   | |  _  g  |  _ g  |  _ d  S(   N(   t   configuratort   achallst   indices(   R   Rv   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyR   :  s    		c         C   s3   |  j  j | ƒ | d k	 r/ |  j j | ƒ n  d S(   sÝ   Store challenge to be performed when perform() is called.

        :param .KeyAuthorizationAnnotatedChallenge achall: Annotated
            challenge.
        :param int idx: index to challenge in a larger array

        N(   Rw   t   appendt   NoneRx   (   R   t   achallt   idx(    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt	   add_chall?  s    c         C   s   t  ƒ  ‚ d S(   s   Perform all added challenges.

        :returns: challenge responses
        :rtype: `list` of `acme.challenges.KeyAuthorizationChallengeResponse`


        N(   t   NotImplementedError(   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   performK  s    N(   R$   R%   R&   R   Rz   R}   R   (    (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyRu   .  s   
	c            sò   t  j ˆ ƒ ‰ ‡ ‡ f d †  ‰  ‡  ‡ ‡ f d †  } t j j ˆ ƒ sS | ƒ  d St  j ˆ ƒ } | ˆ k rr d S| | k rˆ | ƒ  nf t j j ˆ ƒ rÑ t ˆ d ƒ  } | j ƒ  } Wd QX| ˆ k rÑ d Sn  ˆ  ƒ  t j d ˆ ˆ ˆ ƒ d S(   sƒ  Copy a file into an active location (likely the system's config dir) if required.

       :param str dest_path: destination path for version controlled file
       :param str digest_path: path to save a digest of the file in
       :param str src_path: path to version controlled file found in distribution
       :param list all_hashes: hashes of every released version of the file
    c             s)   t  ˆ d ƒ  }  |  j ˆ  ƒ Wd  QXd  S(   Nt   w(   t   opent   write(   t   f(   t   current_hasht   digest_path(    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   _write_current_hash`  s    c              s   t  j ˆ ˆ ƒ ˆ  ƒ  d  S(   N(   t   shutilt   copyfile(    (   R†   t	   dest_patht   src_path(    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   _install_current_filed  s    Nt   rsh   %s has been manually modified; updated file saved to %s. We recommend updating %s for security purposes.(	   R   t	   sha256sumR	   R?   t   isfileR   t   readt   loggert   warning(   R‰   R…   RŠ   t
   all_hashesR‹   t   active_file_digestRƒ   t   saved_digest(    (   R†   R„   R‰   R…   RŠ   s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyRF   V  s&    
	c         C   s¹   d „  } | d ƒ } | d ƒ } | d ƒ } t  j | t j ƒ t  j | t j ƒ t  j | t j ƒ t j | t j j d |  ƒ ƒ } t	 j
 | t j j | |  ƒ d t ƒ| | | f S(   s5   Setup the directories necessary for the configurator.c         S   s   t  j t j |  ƒ ƒ S(   s  Return the real path of a temp directory with the specified prefix

        Some plugins rely on real paths of symlinks for working correctly. For
        example, certbot-apache uses real paths of configuration files to tell
        a virtual host from another. On systems where TMP itself is a symbolic
        link, (ex: OS X) such plugins will be confused. This function prevents
        such a case.
        (   R   t   realpatht   tempfilet   mkdtemp(   t   prefix(    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   expanded_tempdirŠ  s    	t   tempR   t   workt   testdatat   symlinks(   R   t   chmodR   t   CONFIG_DIRS_MODEt   pkg_resourcest   resource_filenameR	   R?   R@   R‡   t   copytreeRU   (   t   test_dirt   pkgR™   t   temp_dirRA   t   work_dirt   test_configs(    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt	   dir_setupˆ  s    	(-   R&   t   loggingt   reR‡   R–   t   josepyR    R'   R    t   zope.interfacet   zopet   acme.magic_typingR   t   certbotR   R   R   R   R   t   certbot._internalR   t   certbot.compatR   R	   t   certbot.plugins.storageR
   t	   getLoggerR$   R   R   R   t   compilet   private_ips_regext
   IGNORECASEt   hostname_regext	   interfacet   implementert   IPlugint   objectR   R+   RK   Ru   RF   R¨   (    (    (    s:   /usr/lib/python2.7/site-packages/certbot/plugins/common.pyt   <module>   s>   			:gb(	2