
    Id("                         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y)    N)FLAG_MOD_ADDFLAG_MOD_REPLACELdbError   )SiteNotFoundExceptionc                       e Zd ZdZy)SubnetExceptionzBase element for Subnet errorsN__name__
__module____qualname____doc__     //usr/lib/python3/dist-packages/samba/subnets.pyr	   r	      s    (r   r	   c                       e Zd ZdZy)SubnetNotFoundz$The subnet requested does not exist.Nr
   r   r   r   r   r   !   s    .r   r   c                       e Zd ZdZy)SubnetAlreadyExistsz&The subnet being added already exists.Nr
   r   r   r   r   r   &   s    0r   r   c                       e Zd ZdZy)SubnetInvalidzThe subnet CIDR is invalid.Nr
   r   r   r   r   r   +   s    %r   r   c                       e Zd ZdZy)SiteNotFoundz2The site to be used for the subnet does not exist.Nr
   r   r   r   r   r   0   s    <r   r   c                    | j                  |t        j                  dt        j                  |      z        }t	        |      dk7  rt        d|z        |d   j                  }t        |t              st        d|z        t        j                  | d      }	 |j                  |       	 |j                  d	       |j                  dd|       	 t        j                          }||_        t        j"                  dt$        d      |d<   t        j"                  t        |      t$        d      |d<   | j'                  |       y# t        j                  $ r t        d      w xY w# t        j                  $ r t        d
      w xY w# t        j                  $ rY}|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=Sites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   r	   	add_childset_componentMessageMessageElementr   add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#4  5C 3x1}C$% & 	&!fiiGk3'ESTTvve23H<(#=6" 1dK0KKM--h.;=-,,S\<-9;,		! << <:;;< << =;<<= << vvt3,,,+t TUUS111 &&P(3'4 5 5 s2   E +E$ A1F E!$FG2AG--G2c                 T   t        j                  | d      }	 |j                  |       	 |j                  d       |j                  dd|       	 | j                  |t         j                  d      }t        |      d	k7  rt        d
|z        	 | j                  |       y# t         j                  $ r t	        d      w xY w# t         j                  $ r t	        d      w xY w# t        $ r:}|j                  \  }}|t         j                  k(  rt        d
|z        Y d}~d}~ww xY w)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   r    r!   r"   r   r#   objectClass=subnetr   r   Subnet %s does not existN)r*   r1   r2   r   r	   r3   r4   r)   
SCOPE_BASEr-   r   r8   ERR_NO_SUCH_OBJECTdelete)r;   r<   r=   rA   r?   e1rD   rE   s           r   delete_subnetrN   m   s'    vve23H<(#=6" 1dK0Kll&:  <s8q= !;k!IJJ  
LL% << <:;;< << =;<<=  Kwwt3))) !;k!IJJ *Ks.   B  C >C$  B?C!$	D'-0D""D'c                    t        j                  | d      }	 |j                  |       	 |j                  d       |j                  dd|       t        j                  | t        |            }|j                  dd|       	 | j                  ||       y
# t         j                  $ r t	        d      w xY w# t         j                  $ r t	        d      w xY w# t        $ rz}|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   r    r!   r"   r   r#   rI   r(   r'   N)r*   r1   r2   r   r	   r3   r4   r0   renamer8   rK   r   r:   r   r9   r   )	r;   r<   r=   new_namerA   newdnsubnete2rD   rE   s	            r   rename_subnetrT      sY    vve23H<(#=6" 1dK0&&H.Kax0X{+ << <:;;< << =;<<=  wwt3))) !;h!FGGS111%&P(0'1 2 2S...(BF!H I I s/   B B6 C B36C	E!A5EEc                 v   t        j                  | d      }	 |j                  |       	 |j                  d       |j                  dd|       	 | j                  |t         j                  d      }t        |      d	k7  rt        d
|z        	 t        j                  | d      }		 |	j                  |       	 |	j                  d       |	j                  dd|       t        j                  | d      }
|
j                  |	       	 | j                  |	t         j                  d      }t        |      d	k7  rt        d|z        	 t        d   j                        }t        j                          }||_        t        j"                  |t$        d      |d<   | j'                  |       y# t         j                  $ r t	        d      w xY w# t         j                  $ r t	        d      w xY w# t        $ r;}|j                  \  }}|t         j                  k(  rt        d
|z        Y d}~d}~ww xY w# t         j                  $ r t	        d      w xY w# t         j                  $ r t	        d      w xY w# t        $ r;}|j                  \  }}|t         j                  k(  rt        d|z        Y d}~qd}~ww xY w)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   r    r!   r"   r   r#   rH   r   r   rI   NzCN=Siteszdnsite.add_base() failedzdnsite.add_child() failedz
CN=ServerszobjectClass=sitezSite %s does not existr&   )r*   r1   r2   r   r	   r3   r4   r)   rJ   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    vve23H<(#=6" 1dK0Kll&:  <s8q= !;k!IJJ  VVE:&F:!;  D),ul+IvNllcnn&8  :s8q='(@9(LMM  Q^FAAD((1A)57AlO	LLOa << <:;;< << =;<<=  Kwwt3))) !;k!IJJ *K << :899: << ;9::;  Nwwt3)))'(@9(LMM *Ns^   F% G >G) %H0 7I >I4 %GG&)	H-20H((H-0II14	J8=0J33J8)r*   r   r   r   sitesr   	Exceptionr	   r   r   r   r   rF   rN   rT   r\   r   r   r   <module>r_      sg   .  8 8 )	i 	
	_ 	
	/ 	
	O 	
	? 	
5pB%PAr   