
    b0!                         d dl Z d dl mZmZmZ ddlmZ  G d de          Z G d de          Z G d	 d
e          Z	 G d de          Z
 G d de          Zd Zd Zd Zd ZdS )    N)FLAG_MOD_ADDFLAG_MOD_REPLACELdbError   )SiteNotFoundExceptionc                       e Zd ZdZdS )SubnetExceptionzBase element for Subnet errorsN__name__
__module____qualname____doc__     //usr/lib/python3/dist-packages/samba/subnets.pyr	   r	      s        ((Dr   r	   c                       e Zd ZdZdS )SubnetNotFoundz$The subnet requested does not exist.Nr
   r   r   r   r   r   !   s        ..Dr   r   c                       e Zd ZdZdS )SubnetAlreadyExistsz&The subnet being added already exists.Nr
   r   r   r   r   r   &   s        00Dr   r   c                       e Zd ZdZdS )SubnetInvalidzThe subnet CIDR is invalid.Nr
   r   r   r   r   r   +   s        %%Dr   r   c                       e Zd ZdZdS )SiteNotFoundz2The site to be used for the subnet does not exist.Nr
   r   r   r   r   r   0   s        <<Dr   r   c                    |                      |t          j        dt          j        |          z            }t	          |          dk    rt          d|z            |d         j        }t          |t                    st          d|z            t          j
        | d          }|                    |          dk    rt          d	          |                    d
          dk    rt          d          |                    dd|           	 t          j                    }||_        t          j        dt"          d          |d<   t          j        t          |          t"          d          |d<   |                     |           dS # t          j        $ rV}|j        \  }	}
|	t          j        k    rt          |d|
          |	t          j        k    rt/          d|z             d}~ww xY w)aj  Create a subnet and associate it with a site.

    :param samdb: A samdb connection
    :param configDn: The DN of the configuration partition
    :param subnet_name: name of the subnet to create (a CIDR range)
    :return: None
    :raise SubnetAlreadyExists: if the subnet to be created already exists.
    :raise SiteNotFound: if the site does not exist.
    z(&(objectclass=Site)(cn=%s))basescope
expressionr   z&A site with the name %s does not existr   z'%s is not a valid subnet (not a string)CN=Subnets,CN=SitesFdnsubnet.add_base() failedCN=Xdnsubnet.add_child() failedCNsubnetobjectclass
siteObject is not a valid subnet: (A subnet with the CIDR %s already existsN)searchldbSCOPE_SUBTREEbinary_encodelenr   dn
isinstancestrr   Dnadd_baser	   	add_childset_componentMessageMessageElementr   addr   argsERR_INVALID_DN_SYNTAXERR_ENTRY_ALREADY_EXISTSr   )samdbconfigDnsubnet_name	site_nameretdn_sitednsubnetmeenumestrs              r   create_subnetrF   5   s    ,,HC,="@(33#4  5 5C 3xx1}}C$% & & 	&!fiGk3'' UESTTTve233H""e++:;;;&!!U**;<<<1dK000KMM-h.;= =-,S\\<-9; ;,		!<   vt3,,,+++tt TUUUS111 &&P(3'4 5 5 5 s   A8F G-AG((G-c                 @   t          j        | d          }|                    |          dk    rt          d          |                    d          dk    rt          d          |                    dd|           	 |                     |t           j        d	          }t          |          d
k    rt          d|z            nC# t          $ r6}|j        \  }}|t           j        k    rt          d|z            Y d}~nd}~ww xY w|                     |           dS )a  Delete a subnet.

    :param samdb: A samdb connection
    :param configDn: The DN of the configuration partition
    :param subnet_name: Name of the subnet to delete
    :return: None
    :raise SubnetNotFound: if the subnet to be deleted does not exist.
    r   Fr    r!   r"   r   r#   objectClass=subnetr   r   Subnet %s does not existN)r*   r1   r2   r	   r3   r4   r)   
SCOPE_BASEr-   r   r   r8   ERR_NO_SUCH_OBJECTdelete)r;   r<   r=   rA   r?   e1rD   rE   s           r   delete_subnetrN   i   sH    ve233H""e++:;;;&!!U**;<<<1dK000Kll&:  < <s88q== !;k!IJJJ  K K Kwt3))) !;k!IJJJ *))))K
 
LLs   >AC 
D,DDc                    t          j        | d          }|                    |          dk    rt          d          |                    d          dk    rt          d          |                    dd|           t          j        | t          |                    }|                    dd|           	 |                     ||           dS # t          $ rx}|j	        \  }}|t           j
        k    rt          d|z            |t           j        k    rt          d	|z            |t           j        k    rt          |d
|           d}~ww xY w)av  Rename a subnet.

    :param samdb: A samdb connection
    :param configDn: The DN of the configuration partition
    :param subnet_name: Name of the subnet to rename
    :param new_name: New name for the subnet
    :return: None
    :raise SubnetNotFound: if the subnet to be renamed does not exist.
    :raise SubnetExists: if the subnet to be created already exists.
    r   Fr    r!   r"   r   r#   rI   r(   r'   N)r*   r1   r2   r	   r3   r4   r0   renamer   r8   rK   r   r:   r   r9   r   )	r;   r<   r=   new_namerA   newdnsubnete2rD   rE   s	            r   rename_subnetrT      s|    ve233H""e++:;;;&!!U**;<<<1dK000&H..Kax000X{+++++   wt3))) !;h!FGGGS111%&P(0'1 2 2 2S...(((BF$!H I I I s   7C 
EA3EEc                 H   t          j        | d          }|                    |          dk    rt          d          |                    d          dk    rt          d          |                    dd|           	 |                     |t           j        d	          }t          |          d
k    rt          d|z            nC# t          $ r6}|j        \  }}|t           j        k    rt          d|z            Y d}~nd}~ww xY wt          j        | d          }	|	                    |          dk    rt          d          |	                    d          dk    rt          d          |	                    dd|           t          j        | d          }
|
                    |	           	 |                     |	t           j        d	          }t          |          d
k    rt          d|z            nC# t          $ r6}|j        \  }}|t           j        k    rt          d|z            Y d}~nd}~ww xY wt          |d         j                  }t          j                    }||_        t          j        |t$          d          |d<   |                     |           dS )a  Assign a subnet to a site.

    This dissociates the subnet from its previous site.

    :param samdb: A samdb connection
    :param configDn: The DN of the configuration partition
    :param subnet_name: Name of the subnet
    :param site_name: Name of the site
    :return: None
    :raise SubnetNotFound: if the subnet does not exist.
    :raise SiteNotFound: if the site does not exist.
    r   Fr    r!   r"   r   r#   rH   r   r   rI   NzCN=Siteszdnsites.add_base() failedzdnsites.add_child() failedz
CN=ServerszobjectClass=sitezSite %s does not existr&   )r*   r1   r2   r	   r3   r4   r)   rJ   r-   r   r   r8   rK   r   r0   r.   r5   r6   r   modify)r;   r<   r=   r>   rA   r?   e3rD   rE   dnsite	dnserverse4siteDnrB   s                 r   set_subnet_siter\      s    ve233H""e++:;;;&!!U**;<<<1dK000Kll&:  < <s88q== !;k!IJJJ  K K Kwt3))) !;k!IJJJ *))))K
 VE:&&Fx  E))9:::5((:;;;
D),,,ul++IvNllcn&8  : :s88q=='(@9(LMMM  N N Nwt3)))'(@9(LMMM *))))N
 Q^^FAAD(1A)57 7AlO	LLOOOOOs2   >AC 
D,DD0AG8 8
H8,H33H8)r*   r   r   r   sitesr   	Exceptionr	   r   r   r   r   rF   rN   rT   r\   r   r   r   <module>r_      sZ  . 


 8 8 8 8 8 8 8 8 8 8 ) ) ) ) ) )	 	 	 	 	i 	 	 	
	 	 	 	 	_ 	 	 	
	 	 	 	 	/ 	 	 	
	 	 	 	 	O 	 	 	
	 	 	 	 	? 	 	 	
1 1 1h  :! ! !H9 9 9 9 9r   