ó
Y[c           @   sä  d  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 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 e( f d „  ƒ  YZ) d „  Z* d „  Z+ d S(   s   Plugin common functions.iÿÿÿÿN(   t   util(   t   List(   t   achallenges(   t	   constants(   t   crypto_util(   t   errors(   t
   interfaces(   t   reverter(   t   PluginStoragec         C   s   |  d S(   s9   ArgumentParser options namespace (prefix of all options).t   -(    (   t   name(    (    s:   /usr/lib/python2.7/dist-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/dist-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/dist-packages/certbot/plugins/common.pyt   __init__2   s    	c         C   s   d S(   s°  Add plugin arguments to the CLI argument parser.

        NOTE: If some of your flags interact with others, you can
        use cli.report_config_interaction to register this to ensure
        values are correctly saved/overridable during renewal.

        :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/dist-packages/certbot/plugins/common.pyt   add_parser_arguments6   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/dist-packages/certbot/plugins/common.pyR   L   s    (   R   (   R   R   R
   R   (    (   R
   R   s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyt   inject_parser_optionsD   s    c         C   s   t  |  j ƒ S(   s9   ArgumentParser options namespace (prefix of all options).(   R   R
   (   R   (    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyR   S   s    c         C   s   |  j  | S(   s'   Option name (include plugin namespace).(   R   (   R   R
   (    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyt   option_nameX   s    c         C   s   t  |  j ƒ S(   s;   ArgumentParser dest namespace (prefix of all destinations).(   R   R
   (   R   (    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyR   \   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/dist-packages/certbot/plugins/common.pyt   desta   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/dist-packages/certbot/plugins/common.pyt   confg   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/dist-packages/certbot/plugins/common.pyR   ,   s   			t	   Installerc           B   sz   e  Z d  Z d „  Z e d „ Z d „  Z d „  Z d „  Z d d „ Z	 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/dist-packages/certbot/plugins/common.pyR   r   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/dist-packages/certbot/plugins/common.pyR.   w   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   R/   R0   R1   (   R   t   titleR6   (    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyR7      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   R/   R0   R1   (   R   R6   (    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyR9   š   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   R/   R0   R1   (   R   R6   (    (    s:   /usr/lib/python2.7/dist-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   R/   R0   R1   (   R   t   rollbackR6   (    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyR;   ²   s    	c         C   sC   y |  j  j ƒ  Wn+ t j k
 r> } t j t | ƒ ƒ ‚ n Xd S(   s¼   Show all of the configuration changes that have taken place.

        :raises .errors.PluginError: If there is a problem while processing
            the checkpoints directories.

        N(   R   t   view_config_changesR   R/   R0   R1   (   R   R6   (    (    s:   /usr/lib/python2.7/dist-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.(   t   ost   patht   joinR   t
   config_dirR   t   SSL_DHPARAMS_DEST(   R   (    (    s:   /usr/lib/python2.7/dist-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/dist-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/dist-packages/certbot/plugins/common.pyt   install_ssl_dhparamsÖ   s
    (   R"   R#   R$   R   t   FalseR.   R7   R9   R:   R;   R=   R(   RC   RE   RI   (    (    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyR)   l   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/dist-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/dist-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/dist-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/dist-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/dist-packages/certbot/plugins/common.pyt   __eq__  s    c         C   s   t  |  j ƒ S(   N(   t   hashRL   (   R   (    (    s:   /usr/lib/python2.7/dist-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/dist-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/dist-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/dist-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/dist-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/dist-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 t | ƒ ƒ D]… } | | } t | ƒ d k r¥ t } qw n$ t | ƒ d k rÉ | j d ƒ } n  | sâ t | ƒ | | <qw t | ƒ | | t | ƒ <qw W| S(   s#   Explode IPv6 address for comparisont   0RQ   i    i   (   t   splitRT   RJ   t   rangeRU   Rh   R1   (   R   Rk   t   resultt	   addr_listt   append_to_endt   it   block(    (    s:   /usr/lib/python2.7/dist-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/dist-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/dist-packages/certbot/plugins/common.pyR   N  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/dist-packages/certbot/plugins/common.pyt	   add_challS  s    c         C   s   t  ƒ  ‚ d S(   sœ   Perform all added challenges.

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


        N(   t   NotImplementedError(   R   (    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyt   perform_  s    N(   R"   R#   R$   R   Rz   R}   R   (    (    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyRu   B  s   
	t   TLSSNI01c           B   s>   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d d „ Z RS(   s1   Abstract base for TLS-SNI-01 challenge performersc         C   s8   t  t |  ƒ j | ƒ t j j | j j d ƒ |  _ d  S(   Ns!   le_tls_sni_01_cert_challenge.conf(	   R*   R€   R   R>   R?   R@   R   RA   t   challenge_conf(   R   Rv   (    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyR   n  s    	c         C   s,   t  j j |  j j j | j j d ƒ d ƒ S(   sã   Returns standardized name for challenge certificate.

        :param .KeyAuthorizationAnnotatedChallenge achall: Annotated
            tls-sni-01 challenge.

        :returns: certificate file name
        :rtype: str

        t   tokens   .crt(   R>   R?   R@   Rv   R   t   work_dirt   challt   encode(   R   R{   (    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyt   get_cert_patht  s    
c         C   s,   t  j j |  j j j | j j d ƒ d ƒ S(   s'   Get standardized path to challenge key.R‚   s   .pem(   R>   R?   R@   Rv   R   Rƒ   R„   R…   (   R   R{   (    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyt   get_key_path  s    c         C   s   | j  | j ƒ j j d ƒ S(   s.   Returns z_domain (SNI) name for the challenge.s   utf-8(   t   responset   account_keyt   z_domaint   decode(   R   R{   (    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyt   get_z_domain†  s    c         C   sõ   |  j  | ƒ } |  j | ƒ } |  j j j t | ƒ |  j j j t | ƒ | j d | ƒ \ } \ } } t j j	 t j j
 | ƒ } t j j t j j
 | ƒ }	 t | d ƒ  }
 |
 j | ƒ Wd QXt j | d d d ƒ } | j |	 ƒ Wd QX| S(   s-   Generate and write out challenge certificate.t   cert_keyt   wbNt   chmodi   (   R†   R‡   Rv   R   t   register_file_creationRU   t   response_and_validationt   OpenSSLt   cryptot   dump_certificatet   FILETYPE_PEMt   dump_privatekeyt   opent   writeR    t	   safe_open(   R   R{   R   t	   cert_patht   key_pathRˆ   t   certt   keyt   cert_pemt   key_pemt   cert_chall_fdt   key_file(    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyt   _setup_challenge_certŠ  s    			N(	   R"   R#   R$   R   R†   R‡   RŒ   Rz   R¢   (    (    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyR€   j  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(   R—   R˜   (   t   f(   t   current_hasht   digest_path(    s:   /usr/lib/python2.7/dist-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/dist-packages/certbot/plugins/common.pyt   _install_current_file±  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/dist-packages/certbot/plugins/common.pyRF   £  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 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>   R?   t   realpatht   tempfilet   mkdtemp(   t   prefix(    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyt   expanded_tempdir×  s    	t   tempR   t   workt   testdatat   symlinks(   R>   R   R   t   CONFIG_DIRS_MODEt   pkg_resourcest   resource_filenameR?   R@   R¨   t   copytreeRU   (   t   test_dirt   pkgRº   t   temp_dirRA   Rƒ   t   test_configs(    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyt	   dir_setupÕ  s    	(,   R$   t   loggingR>   t   reR¨   R·   R’   RÀ   t   zope.interfacet   zopet   josepyR    R%   t   acme.magic_typingR   t   certbotR   R   R   R   R   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   R€   RF   RÇ   (    (    (    s:   /usr/lib/python2.7/dist-packages/certbot/plugins/common.pyt   <module>   sB   			?sc(9	2