
¡\\®#  ã               @   se   d  Z  d d l Z d d l m Z d d l m Z Gd d „  d e j ƒ Z Gd d „  d e ƒ Z d S)	z8Module contains classes used by the Apache Configurator.é    N)ÚSet)Úcommonc                   sˆ   e  Z d  Z d Z d d „  Z d d „  Z d d „  Z ‡  f d d	 †  Z d
 d „  Z d d „  Z	 d d „  Z
 d d „  Z d d „  Z ‡  S)ÚAddrzRepresents an Apache address.c             C   sX   t  | |  j ƒ rT |  j | j k pS |  j d | j d k oS |  j ƒ  oS | j ƒ  Sd S)zTThis is defined as equivalent within Apache.

        ip_addr:* == ip_addr

        r   F)Ú
isinstanceÚ	__class__ÚtupÚis_wildcard)ÚselfÚother© r   ú4/usr/lib/python3/dist-packages/certbot_apache/obj.pyÚ__eq__   s
    zAddr.__eq__c             C   s   |  j  | ƒ S)N)r   )r	   r
   r   r   r   Ú__ne__   s    zAddr.__ne__c             C   s   d t  |  j ƒ d S)Nzcertbot_apache.obj.Addr(ú))Úreprr   )r	   r   r   r   Ú__repr__   s    zAddr.__repr__c                s   t  t |  ƒ j ƒ  S)N)Úsuperr   Ú__hash__)r	   )r   r   r   r      s    zAddr.__hash__c             C   s   | j  ƒ  |  j  ƒ  k S)zAReturns if addr.get_addr() is more specific than self.get_addr().)Ú_rank_specific_addr)r	   Úaddrr   r   r   Ú_addr_less_specific"   s    zAddr._addr_less_specificc             C   s4   |  j  ƒ  d k r d S|  j  ƒ  d k r, d Sd Sd S)zzReturns numerical rank for get_addr()

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

        Z	_default_r   Ú*é   é   N)Úget_addr)r	   r   r   r   r   '   s
    zAddr._rank_specific_addrc             C   sW   |  j  | ƒ r d S|  j ƒ  | j ƒ  k rS |  j ƒ  sO |  j ƒ  | j ƒ  k rS d Sd S)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   Zget_port)r	   r   r   r   r   Ú	conflicts5   s    $zAddr.conflictsc             C   s   |  j  d d k p |  j  d S)z'Returns if address has a wildcard port.r   r   )r   )r	   r   r   r   r   Q   s    zAddr.is_wildcardc             C   s   |  j  ƒ  r |  S|  j | ƒ S)zÛReturns 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   Zget_addr_obj)r	   Zportr   r   r   Úget_sni_addrU   s    zAddr.get_sni_addr)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r   r   )r   r   r      s   r   c               @   s    e  Z d  Z d Z e j d ƒ Z d d d d 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 d „ Z d S)Ú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^(?:.+://)?([^ :$]*)NFc
       
      C   sj   | |  _  | |  _ | |  _ | |  _ | d k	 r6 | n t ƒ  |  _ | |  _ | |  _ | |  _ |	 |  _	 d S)zInitialize a VH.N)
ÚfilepÚpathÚaddrsÚnameÚsetÚaliasesÚsslÚenabledÚmodmacroÚancestor)
r	   r"   r#   r$   r(   r)   r%   r'   r*   r+   r   r   r   Ú__init__   s    							zVirtualHost.__init__c             C   sL   t  ƒ  } | j |  j ƒ |  j d k	 rH | j t j j |  j ƒ d ƒ | S)zReturn a set of all names.Nr   )r&   Úupdater'   r%   Úaddr!   Ú
strip_nameÚfindall)r	   Z	all_namesr   r   r   Ú	get_namesŽ   s
    	 zVirtualHost.get_namesc             C   s·   d j  d |  j d |  j d d j d d „  |  j Dƒ ƒ d |  j d  k	 rR |  j n d	 d
 d j d d „  |  j Dƒ ƒ d |  j r† d n d d |  j r› d n d d |  j	 r° d n d ƒ S)NzžFile: {filename}
Vhost path: {vhpath}
Addresses: {addrs}
Name: {name}
Aliases: {aliases}
TLS Enabled: {tls}
Site Enabled: {active}
mod_macro Vhost: {modmacro}ÚfilenameZvhpathr$   z, c             s   s   |  ] } t  | ƒ Vq d  S)N)Ústr)Ú.0r   r   r   r   ú	<genexpr>¤   s    z&VirtualHost.__str__.<locals>.<genexpr>r%   Ú r'   c             s   s   |  ] } | Vq d  S)Nr   )r4   r%   r   r   r   r5   ¦   s    ZtlsÚYesÚNoZactiver*   )
Úformatr"   r#   Újoinr$   r%   r'   r(   r)   r*   )r	   r   r   r   Ú__str__˜   s    			zVirtualHost.__str__c          	   C   s\   d j  d |  j d d j d d „  |  j Dƒ ƒ d d j |  j ƒ  ƒ d |  j rU d	 n d
 ƒ S)z5Return a representation of VHost to be used in dialogzBFile: {filename}
Addresses: {addrs}
Names: {names}
HTTPS: {https}
r2   r$   z, c             s   s   |  ] } t  | ƒ Vq d  S)N)r3   )r4   r   r   r   r   r5   ³   s    z+VirtualHost.display_repr.<locals>.<genexpr>ÚnamesZhttpsr7   r8   )r9   r"   r:   r$   r1   r(   )r	   r   r   r   Údisplay_repr«   s
    		zVirtualHost.display_reprc             C   s˜   t  | |  j ƒ r” |  j | j k o“ |  j | j k o“ |  j | j k o“ |  j ƒ  | j ƒ  k o“ |  j | j k o“ |  j | j k o“ |  j | j k Sd S)NF)	r   r   r"   r#   r$   r1   r(   r)   r*   )r	   r
   r   r   r   r   ¸   s    $zVirtualHost.__eq__c             C   s   |  j  | ƒ S)N)r   )r	   r
   r   r   r   r   Ã   s    zVirtualHost.__ne__c             C   sC   t  |  j |  j t |  j ƒ t |  j ƒ  ƒ |  j |  j |  j f ƒ S)N)	Úhashr"   r#   Útupler$   r1   r(   r)   r*   )r	   r   r   r   r   Æ   s    zVirtualHost.__hash__c             C   s<   x5 | D]- } x$ |  j  D] } | j | ƒ r d Sq Wq Wd S)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)r$   r   )r	   r$   Zpot_addrr   r   r   r   r   Ë   s
    zVirtualHost.conflictsc             C   só   | sA | j  ƒ  |  j  ƒ  k r" d S|  j d k	 s: |  j rN d Sn |  j rN d St | j ƒ t |  j ƒ k rp d St ƒ  } xs | j D]h } x_ |  j D]P } | j ƒ  | j ƒ  k r“ | | k r“ | j ƒ  | k r“ | j | j ƒ  ƒ Pq“ Wd Sqƒ Wd S)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_

        FNT)r1   r%   r'   Úlenr$   r&   r   r.   )r	   ZvhostZgenericZalready_foundr   Z
local_addrr   r   r   Úsame_serverÞ   s&    		zVirtualHost.same_server)r   r   r   r    ÚreÚcompiler/   r,   r1   r;   r=   r   r   r   r   rA   r   r   r   r   r!   f   s   
r!   )	r    rB   Zacme.magic_typingr   Zcertbot.pluginsr   r   Úobjectr!   r   r   r   r   Ú<module>   s
   ^