ó
Á£ô_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	 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 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# e j$ j% e j& ƒ e j$ j' e j( ƒ d e j) f d „  ƒ  Yƒ ƒ Z* d e j+ f d „  ƒ  YZ, d e j+ f d „  ƒ  YZ- d „  Z. d S(   s   Webroot plugin.iÿÿÿÿN(   t
   challenges(   t   DefaultDict(   t   Dict(   t   List(   t   Set(   t   achallenges(   t   errors(   t
   interfaces(   t   cli(   t
   filesystem(   t   os(   t   ops(   t   util(   t   common(   t	   safe_opent   Authenticatorc           B   s¡   e  Z d  Z d Z d Z d „  Z e d „  ƒ Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z e d „ Z d „  Z d „  Z d „  Z d „  Z RS(   s   Webroot Authenticator.s    Place files in webroot directorysô   Authenticator plugin that performs http-01 challenge by saving
necessary validation resources to appropriate paths on the file
system. It expects that there is some other HTTP server configured
to serve all files under specified web root ({0}).c         C   s   |  j  j |  j d ƒ ƒ S(   Nt   path(   t	   MORE_INFOt   formatt   conf(   t   self(    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyt	   more_info,   s    c      	   C   s?   | d d d g  d t  d d ƒ| d d i  d t d d ƒd  S(	   NR   s   -wt   defaultt   actiont   helps  public_html / webroot path. This can be specified multiple times to handle different domains; each domain will have the webroot path that preceded it.  For instance: `-w /var/www/example -d example.com -d www.example.com -w /var/www/thing -d thing.net -d m.thing.net` (default: Ask)t   maps—  JSON dictionary mapping domains to webroot paths; this implies -d for each entry. You may need to escape this from your shell. E.g.: --webroot-map '{"eg1.is,m.eg1.is":"/www/eg1/", "eg2.is":"/www/eg2"}' This option is merged with, but takes precedence over, -w / -d entries. At present, if you put webroot-map in a config file, it needs to be on a single line, like: webroot-map = {"example.com":"/var/www"}.(   t   _WebrootPathActiont   _WebrootMapAction(   t   clst   add(    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyt   add_parser_arguments/   s    c         C   s
   t  j g S(   N(   R    t   HTTP01(   R   t   domain(    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyt   get_chall_prefA   s    c         O   sA   t  t |  ƒ j | | Ž  i  |  _ t j t ƒ |  _ g  |  _ d  S(   N(	   t   superR   t   __init__t
   full_rootst   collectionst   defaultdictt   sett	   performedt   _created_dirs(   R   t   argst   kwargs(    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyR#   E   s    	c         C   s   d  S(   N(    (   R   (    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyt   prepareM   s    c         C   s7   |  j  | ƒ |  j ƒ  g  | D] } |  j | ƒ ^ q S(   N(   t   _set_webrootst   _create_challenge_dirst   _perform_single(   R   t   achallst   achall(    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyt   performP   s    
c         C   s  |  j  d ƒ rb |  j  d ƒ d } t j d | ƒ xÝ | D]" } |  j  d ƒ j | j | ƒ q9 Wn° t t t j |  j  d ƒ ƒ ƒ ƒ } x‰ | D] } | j |  j  d ƒ k r |  j	 | j | ƒ } y | j
 | ƒ Wn t k
 rä n X| j d | ƒ | |  j  d ƒ | j <q q Wd  S(   NR   iÿÿÿÿs4   Using the webroot path %s for all unmatched domains.R   i    (   R   t   loggert   infot
   setdefaultR    t   listR'   t   sixt
   itervaluest   _prompt_for_webroott   removet
   ValueErrort   insert(   R   R0   t   webroot_pathR1   t   known_webrootst   new_webroot(    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyR-   W   s"    	#$	c         C   sh   d  } x[ | d  k rc | rN |  j | | ƒ } | d  k r` |  j | ƒ } q` q	 |  j | t ƒ } q	 W| S(   N(   t   Nonet   _prompt_with_webroot_listt   _prompt_for_new_webroott   True(   R   R    R>   t   webroot(    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyR9   m   s    c         C   s§   t  j j t j ƒ } d |  j d ƒ } xx t r¢ | j d j | ƒ d g | d | d t ƒ\ } } | t	 j
 k r† t j d ƒ ‚ n  | d k r– d  S| | d	 SWd  S(
   Ns   --R   s   Select the webroot for {0}:s   Enter a new webroott   cli_flagt   force_interactivesI   Every requested domain must have a webroot when using the webroot plugin.i    i   (   t   zopet	   componentt
   getUtilityR   t   IDisplayt   option_nameRC   t   menuR   t   display_utilt   CANCELR   t   PluginErrorR@   (   R   R    R>   t   displayt	   path_flagt   codet   index(    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyRA   |   s    	c         C   s\   t  j t d j | ƒ d t ƒ\ } } | t j k rR | s@ d  St j	 d ƒ ‚ n  t | ƒ S(   Ns   Input the webroot for {0}:RF   sI   Every requested domain must have a webroot when using the webroot plugin.(
   R   t   validated_directoryt   _validate_webrootR   RC   RM   RN   R@   R   RO   (   R   R    t
   allowraiseRR   RD   (    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyRB   ‹   s    c      
   C   sŸ  |  j  d ƒ } | s' t j d ƒ ‚ n  xq| j ƒ  D]c\ } } t j j | t j j	 ƒ |  j
 | <t j d |  j
 | ƒ t j d ƒ } zþ x÷ t t j |  j
 | ƒ d  d t ƒD]Ï } t j j | ƒ rÐ q² n  y€ t j | d ƒ |  j j | ƒ y# t j | | d d t d	 t ƒWn6 t t f k
 rN} t j d
 ƒ t j d | ƒ n XWq² t k
 r€} t j d j | | ƒ ƒ ‚ q² Xq² WWd  t j | ƒ Xq4 Wd  S(   NR   s   Missing parts of webroot configuration; please set either --webroot-path and --domains, or --webroot-map. Run with  --help webroot for examples.s-   Creating root challenges validation dir at %si   iÿÿÿÿt   keyií  t	   copy_usert
   copy_groups3   Unable to change owner and uid of webroot directorys   Error was: %ss=   Couldn't create root for {0} http-01 challenge responses: {1}(   R   R   RO   t   itemsR
   R   t   joinR    R   t   URI_ROOT_PATHR$   R3   t   debugR	   t   umaskt   sortedR   t   get_prefixest   lent   isdirt   mkdirR)   t   appendt   copy_ownership_and_apply_modeRC   t   OSErrort   AttributeErrorR4   R   (   R   t   path_mapt   nameR   t	   old_umaskt   prefixt	   exception(    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyR.   ˜   s6    "	-c         C   s   t  j j | | j j d ƒ ƒ S(   Nt   token(   R
   R   R[   t   challt   encode(   R   t	   root_pathR1   (    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyt   _get_validation_pathÄ   s    c         C   s´   | j  ƒ  \ } } |  j | j } |  j | | ƒ } t j d | ƒ t j d ƒ } z8 t | d d d d ƒ } | j	 | j
 ƒ  ƒ Wd  QXWd  t j | ƒ X|  j | j | ƒ | S(   Ns#   Attempting to save validation to %si   t   modet   wbt   chmodi¤  (   t   response_and_validationR$   R    Rq   R3   R]   R	   R^   R   t   writeRo   R(   R   (   R   R1   t   responset
   validationRp   t   validation_pathRj   t   validation_file(    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyR/   Ç   s    c         C   s  xx | D]p } |  j  j | j d  ƒ } | d  k	 r |  j | | ƒ } t j d | ƒ t j | ƒ |  j	 | j | ƒ q q Wg  } xs |  j
 rö |  j
 j ƒ  } y t j | ƒ Wq„ t k
 rò } | j d | ƒ t j d | ƒ t j d | ƒ q„ Xq„ W| |  _
 t j d ƒ d  S(   Ns   Removing %si    s3   Challenge directory %s was not empty, didn't removes   Error was: %ss   All challenges cleaned up(   R$   t   getR    R@   Rq   R3   R]   R
   R:   R(   R)   t   popt   rmdirRf   R<   R4   (   R   R0   R1   Rp   Ry   t   not_removedR   t   exc(    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyt   cleanupÚ   s$    	(   t   __name__t
   __module__t   __doc__t   descriptionR   R   t   classmethodR   R!   R#   R,   R2   R-   R9   RA   t   FalseRB   R.   Rq   R/   R€   (    (    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyR      s"   									,		R   c           B   s   e  Z d  Z d d „ Z RS(   s%   Action class for parsing webroot_map.c            se   x^ t  j t j | ƒ ƒ D]D \ } ‰  t ˆ  ƒ ‰  | j j ‡  f d †  t j | | ƒ Dƒ ƒ q Wd  S(   Nc         3   s   |  ] } | ˆ  f Vq d  S(   N(    (   t   .0t   d(   R=   (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pys	   <genexpr>÷   s    (	   R7   t	   iteritemst   jsont   loadsRU   t   webroot_mapt   updateR   t   add_domains(   R   t   parsert	   namespaceRŒ   t   option_stringt   domains(    (   R=   sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyt   __call__ó   s    %	N(   R   R‚   Rƒ   R@   R“   (    (    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyR   ð   s   R   c           B   s#   e  Z d  Z d „  Z d d „ Z RS(   s&   Action class for parsing webroot_path.c         O   s&   t  t |  ƒ j | | Ž  t |  _ d  S(   N(   R"   R   R#   R†   t   _domain_before_webroot(   R   R*   R+   (    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyR#   ý   s    c         C   sŠ   |  j  r t j d ƒ ‚ n  | j r[ | j d } x< | j D] } | j j | | ƒ q; Wn | j rp t |  _  n  | j j t	 | ƒ ƒ d  S(   NsP   If you specify multiple webroot paths, one of them must precede all domain flagsiÿÿÿÿ(
   R”   R   RO   R=   R’   RŒ   R5   RC   Rd   RU   (   R   R   R   R=   R‘   t   prev_webrootR    (    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyR“     s    			N(   R   R‚   Rƒ   R#   R@   R“   (    (    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyR   ú   s   	c         C   s8   t  j j |  ƒ s( t j |  d ƒ ‚ n  t  j j |  ƒ S(   s·   Validates and returns the absolute path of webroot_path.

    :param str webroot_path: path to the webroot directory

    :returns: absolute path of webroot_path
    :rtype: str

    s%    does not exist or is not a directory(   R
   R   Rb   R   RO   t   abspath(   R=   (    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyRU     s    	(/   Rƒ   t   argparseR%   RŠ   t   loggingR7   t   zope.componentRG   t   zope.interfacet   acmeR    t   acme.magic_typingR   R   R   R   t   certbotR   R   R   t   certbot._internalR   t   certbot.compatR	   R
   t   certbot.displayR   R   RM   t   certbot.pluginsR   t   certbot.utilR   t	   getLoggerR   R3   t	   interfacet   implementert   IAuthenticatort   providert   IPluginFactoryt   PluginR   t   ActionR   R   RU   (    (    (    sE   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/webroot.pyt   <module>   s<   Ï
