a
    )&iÈ                     @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlZd dl	m
Z
 d dlmZmZ d dlmZ d dlmZmZ d dlmZmZ d d	lmZ d dlmZ d dlZd d
lmZmZmZ d dlm Z m!Z! dZ"h dZ#dZ$dZ%dZ&ej'Z(ej)Z*ej+Z,ej-Z.ej/Z0ej1Z2ej3Z4ze5 W n e6y2   dd Z5Y n0 dd Z7e7  d&ddZ8d'ddZ9d(ddZ:G dd deZ;eG dd de;Z<eG d d! d!e;Z=eG d"d# d#e;Z>d$d% Z?e?  dS ))    N)SamDB)system_session)SDUtils)DONT_USE_KERBEROSCredentials)FEATURE_SEAL)SubunitOptionsTestProgram)TestCaseldb_err)DynamicTestCase)c_REDc_GREENc_DARK_YELLOW)UF_SERVER_TRUST_ACCOUNTUF_TRUSTED_FOR_DELEGATIONz$f3a64788-5306-11d1-a9c5-0000f80367c1>   dNSHostNameservicePrincipalNamednsAMAccountNameTFreportc                  C   s   dd l } |   d S )Nr   )pdbZ	set_trace)r    r   6/usr/lib/python3/dist-packages/samba/tests/ldap_spn.py
breakpointA   s    r   c                  C   s   t d} t| }| | t| }| | t| a| t | jdddt	j
 d | jddd |  \}}t|d	kr|   t	d	 | a|ta|d
 at a|ja|jad S )Nz&python3 ldap_spn.py <server> [options]z--colour
store_truezuse colour text)actionhelpdefaultz--filterz"only run tests matching this regex)r      r   )optparseZOptionParseroptionsZSambaOptionsZadd_option_groupZCredentialsOptionsr   subunitoptsZ
add_optionsysstdoutisatty
parse_argslenZprint_usageexitZget_loadparmLPZget_credentialsCREDSSERVER	get_realmREALMZcolourCOLOUR_TEXTfilterFILTER)parserZ	sambaoptsZcredoptsoptsargsr   r   r   initF   s0    






r4   c                 C   s0   t s| S |dkrt| S |dkr(t| S t| S )Nerrorpass)r.   r   r   r   )xstater   r   r   colour_textl   s    r9   c                 C   s.   | d u rt } t }nd }tdt t|| dS )Nzldap://)ZurlZlpZsession_infoZcredentials)r*   r   r   r+   r)   )credsZsessionr   r   r   	get_samdbw   s    
r;   	samba123@c                 C   s   t  }|| || |t  |t  |t	  |
t tB  |t d| d| }|ddd }| j|||d |rt| }||}	|D ]"}
dt d|	 d}||
| qt|d	}|S )
NCN=,r   r   )Zuserouz
(OA;CI;WP;z;;))r:   )r   Zset_usernameZset_passwordZ
set_domainr*   Z
get_domainZ	set_realmr,   Zset_workstationZget_workstationZset_gensec_featuresZget_gensec_featuresr   Zset_kerberos_stater   splitZnewuserr   Zget_object_sidSPN_GUIDZdacl_add_acer;   )samdbouZusernamewriteable_objectsZpasswordr:   ZdnstrZshort_ouZsd_utilsZsidobjmodZunpriv_samdbr   r   r   add_unpriv_user   s&    




rG   c                   @   sL   e Zd ZdZedd Zdd Zdd Zdd	 Zd
d Z	dd Z
dd ZdS )LdapSpnTestBaseFc                 C   sT   t | ddrd S | jD ]8^}}tr0tt|s0qtdd|}| d||| qd S )N	_disabledFz\W+_Ztest_spn)getattrcasesr0   researchsubZgenerate_dynamic_test)clsdocrowsnamer   r   r   setUpDynamicTestCases   s    z%LdapSpnTestBase.setUpDynamicTestCasesc                 C   sR   t dd |D }|D ]6}d|v r4|dd\}}nd}t| d| | qd S )Nc                 s   s   | ]}|d  V  qdS )r   Nr   .0rr   r   r   	<genexpr>       z0LdapSpnTestBase.setup_objects.<locals>.<genexpr>:r   dcZadd_)setr@   rK   )selfrR   objectsrS   objtyper   r   r   setup_objects   s    zLdapSpnTestBase.setup_objectsc                    s   d j i _tdd |D }|D ]`}|dkr0q"|dkrBd}d }n(d|dd } fd	d
|dD }t j  j|| j|< q"d S )N*c                 s   s   | ]}|d  V  qdS )   Nr   rU   r   r   r   rX      rY   z.LdapSpnTestBase.setup_users.<locals>.<genexpr> ZnobodyZwrites_r>   rJ   c                    s   g | ]} j | d  qS )r   )r^   rV   r7   r]   r   r   
<listcomp>   rY   z/LdapSpnTestBase.setup_users.<locals>.<listcomp>)rB   userdbsr\   replacer@   rG   rC   )r]   rR   ZpermissionspuserrD   r   re   r   setup_users   s    zLdapSpnTestBase.setup_usersc                 C   sX  t |}t |d}t |d}trLtj  tdtdd| d tj  t | _	| j	
 | _|  ddd d d	 | _i | _d
| j d| j | _| | j	j| jdg | j	| j | | | | t|D ]v\}}t|dkr|\}}	}
}}n|\}}	}
}tj}| j|
 }d|v r:|dd\}}nd}| j| \}}d|i}t|	trl||	 n|	|d< t|  }|!t"st#d|t"  ddD ]\}t|$|t%r|| j&dt' d||< n(t|$|t(rdd || D ||< qtj)*|||}|t+u rz|,| W nX tj-y } z<td|d  d| dt.| d W Y d }~qW Y d }~n
d }~0 0 | /d|d  d|
 dt01| d| d| d| d  q|t2u rFz|,| W nj tj-yB } zN| /d|d  d!| d"t.| d#|
 dt01| d| d|  W Y d }~n
d }~0 0 q|t3u rzF| j	,| td|d  d| d$t d%d d#t01| d| 
 W nh tj-y } zLtd|d  d| d$t d&d d't.| d#t01| d|  W Y d }~n
d }~0 0 q|t4u rpzt4  |,| W nL tj-yl } z0td|d  d| d(t.| d W Y d }~n
d }~0 0 qz|,| W n tj-y } zt|j5d) |krW Y d }~q| /d|d  d| d*t.| d+t.| d#|
 dt01| d| d|  W Y d }~n
d }~0 0 | /d|d  d| d*t.| d#|
 dt01| d| d|  qd S ),Nr5   r6   
z
##########u   starting «u   »
.r   ?   zOU=r>   ztree_delete:1   rZ   r[   r   r   zunexpected attr z. Casefold typo?)r   r   x.dnsnamec                 S   s   g | ]}|j d t dqS )rp   rq   )formatr-   rd   r   r   r   rf     rY   z7LdapSpnTestBase._test_spn_with_args.<locals>.<listcomp>zrow z of 'z' failed as expected with z:  z on z should fail (r?   z of z failed with z:
z' Z	SUCCEEDEDZFAILEDz with z' FAILED with r   z' should have failed with z:
not )6r9   r.   r#   stderrflushprintr   r$   r;   rB   Zget_default_basednbase_dnidrsplitZshort_idr^   rC   
addCleanupdeleteZ	create_our`   rk   	enumerater'   ldbFLAG_MOD_REPLACErg   r@   
isinstancedictupdater\   keysissubsetRELEVANT_ATTRS
ValueErrorgetstrrs   r-   listZMessageZ	from_dictbadZmodifyZLdbErrorr   ZfailpprintZpformatokr   r   r3   )r]   rR   rQ   ZcdocZedocZpdocirowrE   dataZrightsZexpectedoprB   r_   r   rr   mr   kmsger   r   r   _test_spn_with_args   s   









"
  
 
<z#LdapSpnTestBase._test_spn_with_argsc                 C   sh   d| d| j  }| dt  }| j|dtttB ||  d | 	| j
| ||f| j|< d S )Nr=   z,OU=Domain Controllers,rm   Zcomputer)r   objectclassZuserAccountControlZdnsHostName
carLicense)rx   r-   lowerrB   addr   r   r   ry   r{   remove_objectr^   r]   rS   r   rr   r   r   r   add_dcF  s    zLdapSpnTestBase.add_dcc                 C   sN   d| d| j  }| j|||d|  d | | j| |d f| j|< d S )Nr=   r>   rj   )r   rS   ZsamAccountNamer   r   )rC   rB   r   ry   r{   r   r^   )r]   rS   r   r   r   r   add_userT  s    zLdapSpnTestBase.add_userc                 C   s    | j |\}}| j| d S )N)r^   poprB   r|   r   r   r   r   r   `  s    zLdapSpnTestBase.remove_objectN)__name__
__module____qualname__rI   classmethodrT   r`   rk   r   r   r   r   r   r   r   r   rH      s   

	!mrH   c                d   @   s  e Zd ZdZddddeffddddeffddddeffd	dddefdd
deffddddefdd
deffddddefdd
deffddddefdd
deeffddddefdd
deeffddddefdd
deeffddddefdddeeffddddefdddeeffddddefdddeeffddddefdddeffddddefdddeffddddefdddefdddeffddddefdddefdddeffddddefdddefdddeffd dddefdddefddd!effd"dddefddd#efddd#effd$dddefdddefdddeffd%dddefdd&defdddeffd'dddefdd&defddd#effd(dddefdd&defdddeffd)dddefdddeffd*dddefdddeffd+dddefdddeeffd,dddefdddeeffd-dddefdddeeffd.dddefdddeeffd/d0ddefdddeffd1d0ddefdddeffd2d0ddefddd3effd4d0ddefd5d&deffd6d0ddefd5d&deffd7d0ddefd5d&d8effd9d0ddefd5d&deffd:dddefddd#effd;dddefd5ddeffd<dddefd5dd=effd>dd?defdd@deffdAdd?defdd@deffdBdd?defdd@deffdCddDdefddEdeffdFd0dEdefd5dDdeffdGddHdIidefddJdefddJdefddHdKidefddJdefddLdeffdMddHdNidefddOdefddOdefddHdPidefddOdefddQdefddRdeffdSddIdefddTdefddUdefddVdefd0dWdeffdXddYdefddZdefdd[defdd\defdd]defdd^deefdd_deefdd`defddadefddbdefddcdefddddeefddedeefddfdefddgdefddhdefddidefddjdefddkdefddldefddmdefd0dndefd0dodefddodefddpdefddqdefddrdefddsdefddtdefddudefddvdefddwdeefddxdefddydefddydefddzdefdd{defdd|deefdd}deefdd~defdddefdddefdddefdddefdddefdddefdddefdddefdddeff2ddddefdddefdddefdddeffddddefddHdKidefdddefdddefdddeffddddefdddeffddddefdddeffddddefdddeffddddefddHdKidefdddefdddefdddeffdddHdIidefdddefddHdKidefdddefdddefdddeffg7Z	dS )LdapSpnTestu   Make sure we can't add clashing servicePrincipalNames.

    This would be possible using sPNMappings aliases — for example, if
    the mapping maps host/ to cifs/, we should not be able to add
    different addresses for each.
    zadd one as adminAhost/{dnsname}ra   zadd one as rightful userzattempt to add one as nobodyrc   zadd and replace as adminzhost/x.{dnsname}zreplace as rightful userz attempt to replace one as nobodyzadd second as adminzadd second as rightful userzattempt to add second as nobodyz.add the same one twice, simple duplicate errorz)simple duplicate attributes, as non-adminz+add the same one twice, identical duplicatez%add a conflict, host first, as nobodyhost/z.{dnsname}Bcifs/z.{dnsname}z(add a conflict, service first, as nobodycifs/{dnsname}z(three way conflict, host first, as adminCwww/z.{dnsname}z6three way conflict, host first, with sufficient rightszB,AC,Az0three way conflict, host first, adding duplicatez=three way conflict, host first, adding duplicate, full rightszC,B,Az7three way conflict, host first, with other write rightsA,Bz)three way conflict, host first, as nobodyz,three way conflict, services first, as adminwww/{dnsname}z=three way conflict, services first, with service write rightsz,three way conflict, service first, as nobodyzreplace host before specificz&replace host after specific, as nobodyz!non-conflict host before specificz non-conflict host after specificz,non-conflict host before specific, non-adminz+non-conflict host after specific, as nobodyz,add a conflict, host first on user, as adminuser:Cz/add a conflict, host first on user, host rightsz/add a conflict, host first on user, both rightsB,Cz'add a conflict, host first both on useruser:Dz4add a conflict, host first both on user, host rightsz4add a conflict, host first both on user, both rightszC,Dz2add a conflict, host first both on user, as nobodyz2add a conflict, host first, with both write rightsz4add a conflict, host first, second on user, as adminz7add a conflict, host first, second on user, with rightszA,Dznonsense SPNs, part 1, as adminza-b-c/{dnsname}zrrrrrrrrrrrrr /{dnsname}znonsense SPNs, part 1, as userz nonsense SPNs, part 1, as nobodyzadd a conflict, using portzdns/{dnsname}zdns/{dnsname}:53z&add a conflict, using port, port firstzthree part spnsr   	{dnsname}'cifs/{dnsname}/DomainDNSZones.{dnsname}y.{dnsname})cifs/y.{dnsname}/DomainDNSZones.{dnsname}zthree part nonsense spnsZbeanzcifs/bean/DomainDNSZones.beanzy.beanzcifs/y.bean/DomainDNSZones.beanzhost/bean/beanzone part spns (no slashes)Zcifszcifs/rt   Zhostz
dodgy spnsz\/{dnsname}zcifs/\\{dnsname}zcifs/\\\{dnsname}zcifs/\\\{dnsname}/u   cīfs/\\\{dnsname}/u   cifs／sficu   cifs／\\\{dnsname}rl   z
/
z
/
/
z
/
/
/
z /* and so on */ u   ¯\_(ツ)_/¯u   ¯\_(つ)_/¯u   ¯\_(㋡)_/¯z//z //z/host/{dnsname}z/host/x.y.zz/ /x.y.zz / / s   host/z /hostu    /HōSTu	    /ħØştz /H0STu    /НoSTz  /hostu    /hostu    2/HōST/⌷[ ][]¨(z (//)z ///z /\//z\//z\\/\\/z|//|z\/\/\\rZ   z:/:z:/:80u   :/:( ツz:/:/:s   cifs/\example.coms   :/s
   :/b /b/b/bs   a@b/a@b/a@bs	   a/a@b/a@bz%empty part spns (consecutive slashes)zcifs//{dnsname}zcifs/zzzy.{dnsname}/z/host/zzzy.{dnsname}ztoo many spn partsz"cifs/{dnsname}/{dnsname}/{dnsname}zcifs/{dnsname}/{dnsname}/zcifs/y.{dnsname}/{dnsname}/toopzhost/{dnsname}/a/b/cz$add a conflict, host first, as adminz2add a conflict, host first, with host write rightsz8add a conflict, service first, with service write rightsz5adding dNSHostName after cifs with no old dNSHostNamecifs/y.{dnsname}host/y.{dnsname}zchanging dNSHostName after cifsN)
r   r   r   __doc__r   deniedr   r   
constraintrL   r   r   r   r   r   e  sZ  












































































































































<





















  r   c                '   @   s  e Zd ZdejvZddddefdddeffddd	defdd
deffddd	defdd
deffddd	defdd
deffddd
defdddeffddd	defdddefddd
gdeffddddidefdd
defdddidefdd	defddde	fddde	ffddddefdddeffddddefdddeffddd	defdd
defdd	deffd dd	defdd
defdd	deffd!dd	defdd
defdd
dee
fdd	deeffd"dd#defdd	defdd
defdd	deffd$dd#defdd	defdd
d%efdd	d%effd&dd	defdd
deefdddeefdd'deefdd(deefdddee
fdd
dee
fdd
deefdddeefdd(dee
ffd)dd	defdd
deefdddeefdd'deefdd(deefdddee
fdd
dee
fdd
deefdddeefdd(dee
ffd*dddefdddefd+d,d+effd-dddefddde	fd+d,d.effd/dd
defdd	defdd	defdd
defd+dd0efd+dd1efdd
defdd2defd+dd+efdd	defdd
de	fdd
d0e	fd+dd+ee
fdd	dee
fdd
defd+dd1eefdd	deffd3dd	defddd0efd+d
d0e	ffd4dd	defdddefd+d
d0e	ffd5dd	defdddefd+d
d+e	ffd6dd	defdd	dee
fddd0efdd
de	fdddee
fdd
defd+d	d+e	fd+d	d1eff	d7dd	d8efdddefd+d
d8e	ffgZd9S ):LdapSpnSambaOnlyTestZSAMBA_SELFTESTz5add a conflict, host first, with service write rightsr   r   ra   r   r   z5add a conflict, service first, with host write rightsr   r   z'add a conflict, service first, as adminz5add a conflict, service first, with both write rightsr   z7add a conflict, host first both on user, service rightsr   r   r   Dz)add a conflict, along with a re-added SPNzcifs/heeble.example.netzchanging dNSHostName after hostr   r   r   r   r   z!mystery dnsname clash, host firstzhost/heeble.example.netzwww/heeble.example.netz mystery dnsname clash, www firstzreplace as adminz replace as non-admin with rightsz,replace vial delete as non-admin with rightsz#replace as non-admin without rightszcifs/bzreplace as nobodyrc   zaccumulate and delete as adminzwww/...zhost/...z&accumulate and delete with user rightsz9three way conflict, host first, with partial write rightsr   r   z;three way conflict, host first, with partial write rights 2r   z.three way conflict sandwich, sufficient rightszA,B,Cr   zldap/{dnsname}z8three way conflict, service first, with all write rightsz9three way conflict, service first, just sufficient rightsz9three way conflict, service first, with host write rightsz9three way conflict, service first, with both write rightsz7three way conflict, services first, with partial rightszA,CN)r   r   r   osenvironrI   r   r   r   r   r|   r   rL   r   r   r   r   r     s6  
















	




























































 r   c                   @   s   e Zd ZdZddddefdddeffdd	ddefd	d
deeffdd	ddeffddd	d	ddej	fdddidefdddidefdddidefdddideffdd	ddidefd	ddefd	ddidefdddefdddefdddefd	ddeffgZ
dS )LdapSpnAmbitiousTestTz1add a conflict with port, host first both on userr   r   ra   r   zwww/{dnsname}:80z2add the same one twice, case-insensitive duplicater   zHost/{dnsname}zspecial SPNzSE3514235-4B06-11D1-AB04-00C04FC2DCD2/75b84f00-a81b-4a19-8ef2-8e483cccff11/{dnsname}z(single part SPNs matching sAMAccountNamezuser:A)r   r   zuser:Br   r   r   r   z three part spns with dnsHostNamer   r   r   r   r   zhost/{y.dnsname}/{y.dnsname}zhost/y.{dnsname}/{dnsname}N)r   r   r   rI   r   r   r   r   r~   ZERR_NO_SUCH_OBJECTrL   r   r   r   r   r   p  sF   








r   c                   C   s   t ttd d S )N)moduler2   )r	   r   r"   r   r   r   r   main  s    r   )N)N)Nr<   )@r#   r   r   rM   Zsamba.samdbr   Z
samba.authr   r~   Zsamba.sd_utilsr   Zsamba.credentialsr   r   Zsamba.gensecr   Zsamba.tests.subunitrunr   r	   Zsamba.testsr
   r   r   Zsamba.getoptZgetoptr!   r    Zsamba.colourr   r   r   Z
samba.dsdbr   r   rA   r   r   r   r   ZERR_OPERATIONS_ERRORZoperrZERR_INSUFFICIENT_ACCESS_RIGHTSr   ZERR_CONSTRAINT_VIOLATIONr   ZERR_ENTRY_ALREADY_EXISTSexistsZFLAG_MOD_ADDr   r   rh   ZFLAG_MOD_DELETEr|   r   	NameErrorr4   r9   r;   rG   rH   r   r   r   r   r   r   r   r   <module>   sh   #

  
 E  ^ .(