
    M/eS$                         d 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  G d de
j"                        Z G d d      Zy)z8Module contains classes used by the Apache Configurator.    N)Any)Iterable)Optional)Pattern)Set)Union)common)ApacheBlockNode)AugeasBlockNode)DualBlockNodec                        e Zd ZdZdedefdZdefdZde	f fdZ
dd defdZde	fd	Zdd defd
ZdefdZdedej                   fdZ xZS )AddrzRepresents an Apache address.otherreturnc                     t        || j                        r^| j                  |j                  k(  xsC | j                  d   |j                  d   k(  xr" | j                         xr |j                         S y)zTThis is defined as equivalent within Apache.

        ip_addr:* == ip_addr

        r   F)
isinstance	__class__tupis_wildcardselfr   s     >/usr/lib/python3/dist-packages/certbot_apache/_internal/obj.py__eq__zAddr.__eq__   so     eT^^,XX* AXXa[EIIaL0 @%%'@,1,=,=,?B     c                 4    dt        | j                         dS )Nz"certbot_apache._internal.obj.Addr())reprr   r   s    r   __repr__zAddr.__repr__   s    3DN3C1EEr   c                      t         |          S N)super__hash__)r   r   s    r   r#   zAddr.__hash__"   s     w!!r   addrc                 D    |j                         | j                         kD  S )zAReturns if addr.get_addr() is more specific than self.get_addr().)_rank_specific_addrr   r$   s     r   _addr_less_specificzAddr._addr_less_specific'   s!     '')D,D,D,FFFr   c                 T    | j                         dk(  ry| j                         dk(  ryy)zzReturns numerical rank for get_addr()

        :returns: 2 - FQ, 1 - wildcard, 0 - _default_
        :rtype: int

        	_default_r   *      )get_addrr   s    r   r&   zAddr._rank_specific_addr,   s)     ==?k)]]_#r   c                     | j                  |      ry| j                         |j                         k(  r2| j                         s!| j                         |j                         k(  ryy)a  Returns if address could conflict with correct function of self.

        Could addr take away service provided by self within Apache?

        .. note::IP Address is more important than wildcard.
            Connection from 127.0.0.1:80 with choices of *:80 and 127.0.0.1:*
            chooses 127.0.0.1:\*

        .. todo:: Handle domain name addrs...

        Examples:

        =========================================  =====
        ``127.0.0.1:\*.conflicts(127.0.0.1:443)``  True
        ``127.0.0.1:443.conflicts(127.0.0.1:\*)``  False
        ``\*:443.conflicts(\*:80)``                False
        ``_default_:443.conflicts(\*:443)``        True
        =========================================  =====

        TF)r(   r.   r   get_portr'   s     r   	conflictszAddr.conflicts9   sN    * ##D)]]_/!T]]_%Gr   c                 J    | j                   d   dk(  xs | j                   d    S )z'Returns if address has a wildcard port.r,   r+   )r   r   s    r   r   zAddr.is_wildcardU   s$    xx{c!4!_4r   portc                 H    | j                         r| S | j                  |      S )zReturns the least specific address that resolves on the port.

        Examples:

        - ``1.2.3.4:443`` -> ``1.2.3.4:<port>``
        - ``1.2.3.4:*`` -> ``1.2.3.4:*``

        :param str port: Desired port

        )r   get_addr_obj)r   r3   s     r   get_sni_addrzAddr.get_sni_addrY   s&     K  &&r   )__name__
__module____qualname____doc__r   boolr   strr   intr#   r(   r&   r1   r   r	   r   r6   __classcell__)r   s   @r   r   r      s    '
C 
D 
F# F"# "
G G4 G
S f  85T 5' ' 'r   r   c                      e Zd ZU dZ ej
                  d      Zeed<   	 	 	 dde	de	de
d   d	ed
edee	   dee
e	      deded    deeeeef      ddfdZde
e	   fdZde	fdZde	fdZdedefdZdefdZdee   defdZddd dedefdZy)VirtualHosta  Represents an Apache Virtualhost.

    :ivar str filep: file path of VH
    :ivar str path: Augeas path to virtual host
    :ivar set addrs: Virtual Host addresses (:class:`set` of
        :class:`common.Addr`)
    :ivar str name: ServerName of VHost
    :ivar list aliases: Server aliases of vhost
        (:class:`list` of :class:`str`)

    :ivar bool ssl: SSLEngine on in vhost
    :ivar bool enabled: Virtual host is enabled
    :ivar bool modmacro: VirtualHost is using mod_macro
    :ivar VirtualHost ancestor: A non-SSL VirtualHost this is based on

    https://httpd.apache.org/docs/2.4/vhosts/details.html

    .. todo:: Any vhost that includes the magic _default_ wildcard is given the
              same ServerName as the main server.

    z^(?:.+://)?([^ :$]*)
strip_nameNfilepathpathaddrsr   sslenablednamealiasesmodmacroancestornoder   c                     || _         || _        || _        || _        ||n	t	               | _        || _        || _        || _        |	| _	        |
| _
        y)zInitialize a VH.N)fileprC   rD   rG   setrH   rE   rF   rI   rJ   rK   )r   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   s              r   __init__zVirtualHost.__init__   sS     
	
	")"5w35  	r   c                     t               }|j                  | j                         | j                  ;|j	                  t
        j                  j                  | j                        d          |S )zReturn a set of all names.r   )rN   updaterH   rG   addr@   rA   findall)r   	all_namess     r   	get_nameszVirtualHost.get_names   sQ    !e	&99 MM+0088CAFGr   c                 f   d| j                    d| j                   ddj                  d | j                  D               d| j                  | j                  nd ddj                  d	 | j
                  D               d
| j                  rdnd d| j                  rdnd d| j                  rd S d S )NFile: z
Vhost path: 
Addresses: , c              3   2   K   | ]  }t        |        y wr!   r<   .0r$   s     r   	<genexpr>z&VirtualHost.__str__.<locals>.<genexpr>        #E$CI#E   z
Name:  z

Aliases: c              3       K   | ]  }|  y wr!    )r]   rG   s     r   r^   z&VirtualHost.__str__.<locals>.<genexpr>   s     !@4$!@s   z
TLS Enabled: YesNoz
Site Enabled: z
mod_macro Vhost: )	rM   rC   joinrD   rG   rH   rE   rF   rI   r   s    r   __str__zVirtualHost.__str__   s    TZZL !99+ &))#E$**#EEF G"&))"7TYYR@ A		!@4<<!@@A B%)XXE48 9&*llU= > )-AC		
 =AAC		
r   c           	          d| j                    ddj                  d | j                  D               ddj                  | j                                d| j                  rd d		S d d		S )
z5Return a representation of VHost to be used in dialogrW   rX   rY   c              3   2   K   | ]  }t        |        y wr!   r[   r\   s     r   r^   z+VirtualHost.display_repr.<locals>.<genexpr>   r_   r`   z
Names: z
HTTPS: rd   re   
)rM   rf   rD   rU   rE   r   s    r   display_reprzVirtualHost.display_repr   sz     TZZL !))#E$**#EEF Gii 012 3#xxe2"6	
 .22"6	
r   r   c                    t        || j                        r| j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xrr | j                         |j                         k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S y)NF)	r   r   rM   rC   rD   rU   rE   rF   rI   r   s     r   r   zVirtualHost.__eq__   s    eT^^,JJ%++- 4$))uzz2I 4JJ%++-4NN$(994 HH		)4 LLEMM1	4
 MMU^^35 r   c           	          t        | j                  | j                  t        | j                        t        | j                               | j                  | j                  | j                  f      S r!   )	hashrM   rC   tuplerD   rU   rE   rF   rI   r   s    r   r#   zVirtualHost.__hash__   sM    TZZ4::&dnn.>(?XXt||T]]< = 	=r   c                 \    |D ]'  }| j                   D ]  }|j                  |      s  y ) y)aE  See if vhost conflicts with any of the addrs.

        This determines whether or not these addresses would/could overwrite
        the vhost addresses.

        :param addrs: Iterable Addresses
        :type addrs: Iterable :class:~obj.Addr

        :returns: If addresses conflicts with vhost
        :rtype: bool

        TF)rD   r1   )r   rD   pot_addrr$   s       r   r1   zVirtualHost.conflicts   s:      	 H

  >>(+ 	  r   vhostgenericc                     |s;|j                         | j                         k7  ry| j                  | j                  ry| j                  ryt        |j                        t        | j                        k7  ryt               }|j                  D ]o  }| j                  D ]]  }|j                         |j                         k(  s%||k7  s+|j                         |vs>|j                  |j                                 n  y y)aX  Determines if the vhost is the same 'server'.

        Used in redirection - indicates whether or not the two virtual hosts
        serve on the exact same IP combinations, but different ports.
        The generic flag indicates that that we're trying to match to a
        default or generic vhost

        .. todo:: Handle _default_

        FT)rU   rG   rH   lenrD   rN   r.   rR   )r   rr   rs   already_foundr$   
local_addrs         r   same_serverzVirtualHost.same_server   s      DNN$44 yy$ YY u{{s4::. #&%KK 	D"jj 
'')T]]_<"d*"++-]B
 "%%j&9&9&;< 	 r   )NNFNN)F)r7   r8   r9   r:   recompilerA   r   __annotations__r<   r   r;   r   r   r
   r   r   rO   rU   rg   rk   r   r   r=   r#   r   r   r1   rx   rc   r   r   r@   r@   j   s)   , %"**%<=J= [_MQZ^ C F $ &.smEMcRUhEW3;M3J  o&U VW 	$3s8 

 


c 
	C 	D 	=# =
x~ $ &1 1 1$ 1r   r@   )r:   ry   typingr   r   r   r   r   r   certbot.pluginsr	   %certbot_apache._internal.apacheparserr
   %certbot_apache._internal.augeasparserr   #certbot_apache._internal.dualparserr   r   r@   rc   r   r   <module>r      sH    > 	       " A A =W'6;; W't^ ^r   