a
    I_0!                     @   s   d dl Z d dl mZmZmZ ddlmZ G dd deZG dd deZG d	d
 d
eZ	G dd deZ
G dd deZdd Zdd Zdd Zdd ZdS )    N)FLAG_MOD_ADDFLAG_MOD_REPLACELdbError   )SiteNotFoundExceptionc                   @   s   e Zd ZdZdS )SubnetExceptionzBase element for Subnet errorsN__name__
__module____qualname____doc__ r   r   //usr/lib/python3/dist-packages/samba/subnets.pyr      s   r   c                   @   s   e Zd ZdZdS )SubnetNotFoundz$The subnet requested does not exist.Nr   r   r   r   r   r   !   s   r   c                   @   s   e Zd ZdZdS )SubnetAlreadyExistsz&The subnet being added already exists.Nr   r   r   r   r   r   &   s   r   c                   @   s   e Zd ZdZdS )SubnetInvalidzThe subnet CIDR is invalid.Nr   r   r   r   r   r   +   s   r   c                   @   s   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   r   c              
   C   sP  | j |tjdt| d}t|dkr4td| |d j}t|tsTt	d| t
| d}||dkrvtd	|d
dkrtd|dd| zDt }||_tdtd|d< tt|td|d< | | W nl tjyJ } zP|j\}	}
|	tjkrt	d||
f n|	tjkr4td| n W Y d}~n
d}~0 0 dS )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))baseZscopeZ
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CNZsubnetZobjectclass
siteObject%s is not a valid subnet: %s(A subnet with the CIDR %s already existsN)searchldbZSCOPE_SUBTREEZ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retZdn_sitednsubnetmeenumestrr   r   r   create_subnet5   sJ    




r7   c              
   C   s   t | d}||dkr"td|ddkr8td|dd| z.| j|t jdd	}t|d
krrt	d| W nD t
y } z,|j\}}|t jkrt	d| W Y d}~n
d}~0 0 | | 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   r#   r$   r   r%   r&   r   
SCOPE_BASEr   r   r   r*   ERR_NO_SUCH_OBJECTdelete)r-   r.   r/   r2   r1   Ze1r5   r6   r   r   r   delete_subneti   s"    	


"r=   c           	   
   C   s   t | d}||dkr"td|ddkr8td|dd| t | t|}|dd| z| || W n| ty } zd|j	\}}|t j
krtd| n6|t jkrtd	| n|t jkrtd
||f n W Y d}~n
d}~0 0 dS )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   r9   r   r   N)r   r#   r$   r   r%   r&   r"   renamer   r*   r;   r   r,   r   r+   r   )	r-   r.   r/   new_namer2   ZnewdnsubnetZe2r5   r6   r   r   r   rename_subnet   s.    




r@   c              
   C   s  t | d}||dkr"td|ddkr8td|dd| z.| j|t jdd	}t|d
krrt	d| W nD t
y } z,|j\}}|t jkrt	d| W Y d}~n
d}~0 0 t | d}	|	|dkrtd|	ddkrtd|	dd| t | d}
|
|	 z0| j|	t jdd	}t|d
krDtd| W nH t
y } z.|j\}}|t jkrztd| W Y d}~n
d}~0 0 t|d j}t  }||_t |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   r8   r   r   r9   NzCN=Siteszdnsites.add_base() failedzdnsites.add_child() failedz
CN=ServerszobjectClass=sitezSite %s does not existr   )r   r#   r$   r   r%   r&   r   r:   r   r   r   r*   r;   r   r"   r    r'   r(   r   Zmodify)r-   r.   r/   r0   r2   r1   Ze3r5   r6   ZdnsiteZ	dnserversZe4ZsiteDnr3   r   r   r   set_subnet_site   sR    


"


"rA   )r   r   r   r   Zsitesr   	Exceptionr   r   r   r   r   r7   r=   r@   rA   r   r   r   r   <module>   s   4$