
    y6hFa                        S SK r S SKrS SKrS SKJrJrJr  S SKJr  S SK	J
r
  S SKJr  S SKJrJr  S SKJr  S SKJr  S SKrS S	KJr  S S
KJrJr  S SKrS SKrS SKJr  S SKr S SKJr  S SK"J#r#  S SK$J%r%  S SK&J'r'  S SK(J)r)  Sr*Sr+/ SQr,S r-S r.S r/S$S jr0S r1S r2S r3S r4S r5S r6S  r7S%S! jr8 " S" S#\\5      r9g! \  a    S r\RB                  " S5         Nrf = f)&    N)
gp_pol_ext
gp_applierGPOSTATE)Ldb)misc)
ndr_unpack)SCOPE_SUBTREE
SCOPE_BASE)system_session)get_dc_hostnamewhich)PopenPIPE)log)load_der_pkcs7_certificatesc                     / $ N )xs    B/usr/lib/python3/dist-packages/samba/gp/gp_cert_auto_enroll_ext.pyr   r   '   s    r	    zNpython cryptography missing pkcs7 support. Certificate chain parsing will fail)Encoding)load_der_x509_certificate)default_backend)
get_strings9   
-----BEGIN CERTIFICATE-----
%s
-----END CERTIFICATE-----zc(https|HTTPS)://(?P<server>[a-zA-Z0-9.-]+)/ADPolicyProvider_CEP_(?P<auth>[a-zA-Z]+)/service.svc/CEP)z/etc/pki/trust/anchorsz /etc/pki/ca-trust/source/anchorsz /usr/local/share/ca-certificatesc                    0 nU  H7  nUS   UR                  5       ;  a  / XS   '   XS      R                  U5        M9     UR                  5        H  nUR                  S S9  U Vs/ s H  o"S   PM	     nn[	        U5      nU Ha  nUR                  U5      n[        U5      [        R                  " [        U5      U5      -
  S-
  nXx:X  a  MJ  S n	[        X7US-    U	S9X7US-   & Mc     M     [        UR                  5       5      $ s  snf )zGroup and Sort End Point Information.

[MS-CAESO] 4.4.5.3.2.3
In this step autoenrollment processes the end point information by grouping
it by CEP ID and sorting in the order with which it will use the end point
to access the CEP information.
PolicyIDc                     U S   $ )NCostr   es    r   <lambda>6group_and_sort_end_point_information.<locals>.<lambda>O   s    1V9r   )keyr       c                 ,    U S   S:X  a  gU S   S:X  a  gg)N	AuthFlags   r   r&   r   r!   s    r   	sort_auth7group_and_sort_end_point_information.<locals>.sort_auth^   s#    [>S({^s*r   )keysappendvaluessortsetindexlenoperatorindexOfreversedsortedlist)
end_point_informationend_point_groupsr"   end_point_group	cost_listcostscostijr*   s
             r   $group_and_sort_end_point_informationr@   ;   s    "Z= 0 5 5 77.0z]+:'..q1 # ,224 	!45 )881vY	8ID%AIx//0CTJJ1LAv &,Oac,B09&;Oac"'  5:  '')**/ 9s   'Dc                    0 nSnU  Hs  nUR                   R                  U5      (       d  M%  UR                   R                  US5      nXAR                  5       ;  a  0 X'   UR                  X   UR
                  '   Mu     UR                  5        H  n[        R                  " [        US   5      nU(       aR  SUR                  S5      R                  SS5      -  nXES'   UR                  S5      US	'   UR                  S
5      US
'   Mz  US   R                  5       S:w  d  M  SUS   0n[        R                  " SU5        0 s  $    [        UR                  5       5      nU$ )zObtain End Point Information.

[MS-CAESO] 4.4.5.3.2.2
In this step autoenrollment initializes the
CertificateEnrollmentPolicyEndPoints table.
z7Software\Policies\Microsoft\Cryptography\PolicyServers\ URLz%s-CAserver.-namehostnameauthzldap:endpointzFailed to parse the endpoint)keyname
startswithreplacer,   data	valuenamer.   rematchendpoint_regrouplowerr   errorr@   )entriesr8   sectionr"   rG   camedatas           r   obtain_end_point_informationr[   k   s?    LGyy##G,,yy  "-1133*,!'3466#AKK0  $**,HH["U),QWWX.66sC@@DvJWWX.BzNBvJY__') "U)-EII4e<I - 	--B-I-I-KL   r   c           	      R   / nU R                  5       nSU-  n/ SQnSnU R                  U[        XT5      n[        U5      S:X  a  U$ U H[  n[	        US   S   5      [	        US   S   5      [	        [
        R                  " US   S   5      5      S.nUR                  U5        M]     U$ )	z(Initialize CAs.

[MS-CAESO] 4.4.5.3.1.2
zMCN=Enrollment Services,CN=Public Key Services,CN=Services,CN=Configuration,%s)cACertificatecndNSHostNamez"(objectClass=pKIEnrollmentService)r   r^   r_   r]   )rG   rH   r]   )get_default_basednsearchr	   r2   r   base64	b64encoder-   )	ldbresultbasedndnattrsexprresesrN   s	            r   fetch_certification_authoritiesrl      s    
 F##%F 
Y[a	aB2E/D
**R
4C
3x1}#BtHQK0'=(9!(<=",V-=-=b>QRS>T-U"V 	d  Mr   c                     Uc  S/nU R                  5       nSU-  nSU-  nU R                  U[        XR5      n[        U5      S:X  a  SUS   ;   a  [	        US   5      $ SS/0$ )NmsPKI-Minimal-Key-SizezOCN=Certificate Templates,CN=Public Key Services,CN=Services,CN=Configuration,%sz(cn=%s)r&   r   2048)r`   ra   r	   r2   dict)rd   rG   rh   rf   rg   ri   rj   s          r   fetch_template_attrsrq      sv    })*##%F	Z]c	cBtD
**R
4C
3x1}1SV;CF|(6(33r   c                 |    [         [        R                  " SSU R                  5       S[        R                  5      -  $ )Ns   (.{64})s   \1
r   )	cert_wraprP   subencodeDOTALL)certs    r   format_root_certrx      s(    rvvj(DKKM1biiPPPr   c                  x    [         R                  R                  S5      SS/n [        SSR	                  U 5      S9$ )NPATHz/usr/lib/certmongerz/usr/libexec/certmongerzcepces-submit:)path)osenvirongetr   join)certmonger_dirss    r   find_cepces_submitr      s5    zz~~f-/D02Osxx'@AAr   c                    [        5       nU(       d  [        R                  " S5        / $ [        R                  nSUS'   [        USU -  S/U[        [        S9nUR                  5       u  pEUR                  S:w  a)  SUR                  5       0n[        R                  " S	U5        UR                  5       R                  5       $ )
NzFailed to find cepces-submitzGET-SUPPORTED-TEMPLATESCERTMONGER_OPERATIONz--server=%sz--auth=Kerberos)envstdoutstderrr   Errorz0Failed to fetch the list of supported templates.)r   r   rU   r}   r~   r   r   communicate
returncodedecodestripsplit)rD   cepces_submitr   pouterrrN   s          r   get_supported_templatesr      s    &(M		01	
**C";C}mf46GHd4	1A}}HC||q&		DdK99;r   c                 ^   [         R                  R                  USU S   -  5      n/ n [        R                  " USSS.S9nUb#  UR                  S	:X  d  UR                  S
   S:X  a  [        R                  " S5        SU ;   a  [        R                  " S5        [        R                  " U S   5      n [        U5      nUR!                  ["        R$                  5      n['        US5       n	U	R)                  U5        SSS5        UR+                  U5        U$ UR                  S
   S:X  an   [        UR                  5      nUR!                  ["        R$                  5      n['        US5       n	U	R)                  U5        SSS5        UR+                  U5        U$ UR                  S
   S:X  a  [-        UR                  5      n
[/        S[1        U
5      5       Hv  nX   R!                  ["        R$                  5      nUR3                  SS5      u  pSXU4-  n['        US5       n	U	R)                  U5        SSS5        UR+                  U5        Mx     U$ [        R                  " S5        U$ ! [        R
                  R                   a    [        R                  " S5        Sn GN`f = f! [         a    [        U[        5       5      n GNf = f! , (       d  f       GN= f! [         a"    [        UR                  [        5       5      n GNf = f! , (       d  f       GN= f! , (       d  f       N= f)z$Fetch Certificate Chain from the CA.%s.crtrG   	GetCACertCAIdentifier)	operationmessage)urlparamsz7Could not connect to Network Device Enrollment Service.Nr   zContent-Typez	text/htmlz2Unable to fetch root certificates (requires NDES).r]   z'Installing the server certificate only.wbzapplication/x-x509-ca-certzapplication/x-x509-ca-ra-certr   rE   r&   z%s.%d.%sz+getca: Wrong (or missing) MIME content type)r}   r|   r   requestsr   
exceptionsConnectionErrorr   warncontentheadersrb   	b64decoder   	TypeErrorr   public_bytesr   PEMopenwriter-   r   ranger2   rsplit)rX   r   	trust_dir	root_cert
root_certsrder_certificaterw   	cert_datawcertsr>   filename	extensiondests                  r   getcar      s   Y2f:(=>IJLLS{5C*E F
 	yAII$		.(A[(PEFb HH>?$..r//BCOD0A ))(,,7Ii&!	" 'i(yy $@@	K,QYY7D %%hll3	)T"aGGI #)$  
>	"&E	E+AII6q#e*%A8((6D"+"2"23":Hi 88DdD!Q "d# &  	>?S .. JK  D01@1BDD '&  	K,QYY8IJD	K #" "!sY   I) 4J& *K+K +L L)6J#"J#&KK
K(L	L	
L
L,	c                  ~    [          H+  n [        R                  R                  U 5      (       d  M)  U s  $    [         S   $ )zIReturn the global trust dir using known paths from various Linux distros.r   )global_trust_dirsr}   r|   isdir)r   s    r   find_global_trust_dirr      s2    &	77==## ' Qr   c                  <    [        S5      =(       d    [        S5      $ )z0Return the command to update the CA trust store.zupdate-ca-certificateszupdate-ca-trustr   r   r   r   update_ca_commandr     s    )*Fe4E.FFr   c                 N   ^ ^ [        U U4S jT R                  5        5       5      $ )z9Return True if any key present in both dicts has changed.c              3   J   >#    U  H  nUT;   a  TU   TU   :g  OS v   M     g7f)FNr   ).0knew_dataold_datas     r   	<genexpr>changed.<locals>.<genexpr>  s0      &$ /08mx{*F$s    #)anyr,   )r   r   s   ``r   changedr     s#     &]]_& & &r   c                 	   [        / / S.40 U D6nSU S   -  n[        R                  " S5        [        XU5      nUS   R	                  U5        [        5       nU H  n	[        R                  R                  U[        R                  R                  U	5      5      n
 [        R                  " X5        US   R                  U
5        [        R                  " SU	< SU
< 35        M     [!        5       n[        R                  " S
U-  5        Ub9  [#        U/5      R%                  5       nUS:w  a  [        R&                  " SU-  5        [)        S5      n[+        5       nUGb  UGb  [#        USSU S   SU< SU S   < SU< 3/[,        [,        S9nUR/                  5       u  nn[        R0                  " UR3                  5       5        UR4                  S:w  aZ  UR4                  S:X  a  [        R                  " SU S   -  5        O-UR3                  5       U S   S.n[        R&                  " SU5        [7        U S   5      nU GHm  n[9        UU5      nU S   < SUR3                  5       < 3n[        R                  R                  USU-  5      n[        R                  R                  USU-  5      n[#        USSU S   SUR3                  5       SUSUS US!US"   S   /[,        [,        S9nUR/                  5       u  nn[        R0                  " UR3                  5       5        UR4                  S:w  aT  UR4                  S:X  a  [        R                  " S#U-  5        O*UR3                  5       US$.n[        R&                  " S%U5        US   R	                  UU/5        US&   R                  U5        GMp     Ub9  [#        U/5      R%                  5       nUS:w  a  [        R&                  " SU-  5        O[        R                  " S'5        [:        R<                  " U5      $ ! [         a    [        R                  " S5         GM  [         a    [        R                  " S	U5         GM  [         a    US   R                  U
5         GM(  f = f)(z#Install the root certificate chain.)files	templatesz0http://%s/CertSrv/mscep/mscep.dll/pkiclient.exe?rH   z&Try to get root or server certificatesr   zCreated symlink: z -> z=Failed to symlink root certificate to the admin trust anchorszZFailed to symlink root certificate to the admin trust anchors. The directory was not foundz
Running %sr   zFailed to run %sgetcertzadd-ca-crG   z-ez
 --server=z --auth=)r   r   r)   zThe CA [%s] already exists)r   CAz#Failed to add Certificate AuthorityrE   z%s.keyr   requestz-Tz-Iz-kz-fz-grn   z The template [%s] already exists)r   CertificatezFailed to request certificater   zOcertmonger and cepces must be installed for certificate auto enrollment to work)rp   r   infor   extendr   r}   r|   r   basenamesymlinkr-   PermissionErrorr   FileNotFoundErrorFileExistsErrorr   r   waitrU   r   r   r   r   debugr   r   r   rq   jsondumps)rX   rd   r   private_dirrI   rN   r   r   global_trust_dirsrcdstupdateretr   r   r   r   r   supported_templatestemplaterh   nicknamekeyfilecertfiles                           r   cert_enrollr   
  s   "2.5"5D
<r*~
MCHH56r	*JM$,.ggll+RWW-=-=c-BC	&JJs M  %HHC=> (  FHH\V$%VHo""$!8II(F34 IG&(M}87HdBvJ0=Z.$() d, ==?S		#**,<<1||q 56
BC!$RZ@		?F5bnE+H(h7E"$V*hoo.?@Hggll;80CDGww||Ix(/BCHw	4FX__.XtWdHU#;<Q?A #4	1A
 }}HCIIcjjl#||q <<1$HH?8LM%(ZZ\(KDII=tDM  '8!45$$X.) ,* /&&(Cax		,78 7 	8::dE  	-HH , - -  	GHH 45EG G  	& M  %%		&s   AP R= R!RRc                   D    \ rS rSrS rS rS r SS jrS rS r	S	 r
S
rg)gp_cert_auto_enroll_exti_  c                     g)NzCryptography\AutoEnrollmentr   )selfs    r   __str__gp_cert_auto_enroll_ext.__str__`  s    -r   c                    [         R                  " U5      n[        R                  " U5      n[	        S5      nUbF  [        USSU/5      R                  5         US    H   n[        USSU/5      R                  5         M"     US    He  n[        R                  R                  U5      (       d  M)  [        R                  R                  U5      (       d  MO  [        R                  " U5        Mg     U R                  X5        g )Nr   z	remove-car   r   zstop-trackingz-ir   )rb   r   r   loadsr   r   r   r}   r|   existsunlinkcache_remove_attribute)	r   guid	attributevalueca_cnrN   r   r   fs	            r   unapplygp_cert_auto_enroll_ext.unapplyc  s      +zz% 	"7Ku56;;= -wx@AFFH .gAww~~a  77>>!$$IIaL  	##D4r   c                 z   [         R                  " US   R                  5       5      R                  5       nU R	                  X5      nUb  [
        R                  " U5      O0 nUb6  [        US   5       V	s/ s H  oS   < SU	R                  5       < 3PM     sn	O/ n
SU
0UEn[        X5      (       d"  U R                  5       [        R                  :X  a  U R                  XU5        Ub3  [        X5      (       d#  U R                  5       [        R                  :w  a  g U" U0 UD6nU R                  XU5        g s  sn	f )NrG   rH   rE   r   )rb   rc   ru   r   cache_get_attribute_valuer   r   r   r   cache_get_apply_stater   ENFORCEr   cache_add_attribute)r   r   rX   applier_funcargskwargsr   old_valr   tr   r   rN   s                r   applygp_cert_auto_enroll_ext.applyq  s   $$RZ%6%6%89@@B	00A*1*=4::g&2" BYY[\fYgAhiAhA6
AHHJ7Ahi(* 	 )3r38&&$*D*D*F(JZJZ*ZLL'2wx'B'B**,0@0@@ T,V,  $7 js   5#D8Nc           	         Uc  U R                   R                  S5      nUc  U R                   R                  S5      n[        R                  R                  U5      (       d  [        R                  " USS9  [        R                  R                  U5      (       d  [        R                  " USS9  U HM  u  pV[        U 5      U;   d  M  U[        U 5         R                  5        H  u  pxU R                  XWU5        M     MO     U GH  n	U	R                  (       d  M  Sn
Sn[        R                  R                  U	R                  U5      nU R                  U5      nU(       d  M_  UR                   GHM  nUR                  U
:X  d  M  UR                  S:X  d  M(  UR                   S-  (       a  M>  UR                   S	-  S	:H  nUR                   S
-  S
:H  nUR                   S-  S:H  nU(       a  U R#                  U	R$                  UR                  X45      nU Vs/ s H5  n[&        R(                  " UR+                  5       5      R-                  5       PM7     nnU R/                  U	R$                  US9  GM  U R1                  U	R$                  5      nU R/                  U	R$                  [3        UR5                  5       5      S9  GMP     GM     g s  snf )Nr   i  )modei  7Software\Policies\Microsoft\Cryptography\AutoEnrollmentMACHINE/Registry.polAEPolicy   r&   r)      )keep)remove)lp
cache_pathprivate_pathr}   r|   r   mkdirstritemsr   file_sys_pathr   parserV   rK   rO   rN    _gp_cert_auto_enroll_ext__enrollrG   rb   rc   ru   r   cleancache_get_all_attribute_valuesr7   r,   )r   deleted_gpo_listchanged_gpo_listr   r   r   settings	ca_cn_encrN   gporW   pol_filer|   pol_confr"   enrollmanageretrive_pendingca_namesnca_attrss                        r   process_group_policy,gp_cert_auto_enroll_ext.process_group_policy  s)   **73I''..w7Kww~~i((HHYU+ww~~k**HH[u-.ND4yH$'/D	':'@'@'BOILL$7 (C /
 $C   T1ww||C$5$5x@::d+!))AyyG+z0I66F?$!"#!4!"#!4*+&&3,#*=!'+}}SXX5=5E5E5>(MH .6(7-5 )/(8(8(D(K(K(M-5 % (7 JJsxxhJ?
 !% C CCHH M % JJsxxX]]_8MJN- * $,(7s   <Kc                 |   U GH  nUS   nUS   S-  (       d  M  [        U Vs/ s H
  owS   S:H  PM     sn5      (       a  UR                  S[        SS/5      n[        U5      S	:w  a  Mh  UR                  US   S   S   [        SS
/5      n	[        U	5      S	:w  a  M  S[	        [        [        R                  U	S   S
   S   5      5      R                  5       -  n
U
WS   :w  a  M  / nU H  nUS   S:X  aC  [        U5      nU H0  nU R                  X[        XUU5        UR                  US   5        M2     MO  US   R                  5       R                  S5      (       a0  U R                  X[        XUX\S   S9  UR                  US   5        M  SUS   0n[        R                   " SU5        M     Us  $    gs  snf )zRead CEP Data.

[MS-CAESO] 4.4.5.3.2.4
In this step autoenrollment initializes instances of the
CertificateEnrollmentPolicy by accessing end points associated with CEP
groups created in the previous step.
r   Flags   rC   LDAP:rB   z(objectClass=*)rootDomainNamingContextr&   
objectGUIDz{%s}r   rG   zhttps://rI   )rI   rJ   zUnrecognized endpointN)r   ra   r
   r2   r  r   r   GUIDupperrl   r  r   r-   rT   rL   r   rU   )r   r   rd   r8   r   r   r:   r"   rj   res2r+  r!  rX   cas_carZ   s                   r   __read_cep_data'gp_cert_auto_enroll_ext.__read_cep_data  s     5O  "A W:$
 AAeH'ABB jjZ1B";!<>s8q=zz#a&)B"CA"F",.?#/.2 t9> $
499d1gl.CA.FGHNNPQ
:. H%e9'9#>C"

4k3Y#.0 F4  #
 Y__&11*==JJtby*F  =OOBvJ/("U)5EII5u=! &" Oq  5 Bs   F9
c           
         S[        U R                  U R                  5      -  n[        U[	        5       U R                  U R                  S9n/ n[        U5      n[        U5      S:  a$  UR                  U R                  XUX45      5        U$ [        U5      n	U	 H0  n
U R                  X[        XUU5        UR                  U
S   5        M2     U$ )N	ldap://%sr   session_infor  credentialsr   rG   )r   credsr  r   r   r[   r2   r   '_gp_cert_auto_enroll_ext__read_cep_datarl   r  r   r-   )r   r   rV   r   r   r   rd   r!  r8   r/  rX   s              r   __enroll gp_cert_auto_enroll_ext.__enroll  s    ODJJ@@c(8WW$**6  <W E$%)OOD001F1:I J  2#6C

4["9&(6
+  r   c                    0 nSnSnUR                   (       Ga'  [        R                  R                  UR                   U5      nU R	                  U5      nU(       d  U$ UR
                   GH  nUR                  U:X  d  M  UR                  S:X  d  M(  UR                  S-  S:H  nUR                  S-  (       d  U(       d  MW  0 US'   S[        U R                  U R                  5      -  n	[        U	[        5       U R                  U R                  S9n
[        UR
                  5      n[        U
5      n[!        U5      S	:  aW  U VVs/ s H  o  H  oPM     M     nnn[#        U Vs/ s H  nUS
   S:H  PM     sn5      (       a  UR%                  U5        OUnU H  nS
U;   a  US
   S:X  a  M  SnUS   nUU;  a  0 UU'   0 UU   U'   SU;   a%  ['        US   5      R)                  5       UU   U   S'   US   UU   U   S'   [+        US   5      nU Vs/ s H  nUR)                  5       PM     snUU   U   S'   M     GM     U$ s  snnf s  snf s  snf )Nr  r  r  r&   r  zAuto Enrollment Policyr4  r5  r   rC   r)  rG   r]   zCA CertificaterH   zAuto Enrollment Server	Templates)r  r}   r|   r   r  rV   rK   rO   rN   r   r8  r  r   r   r[   rl   r2   r   r   rx   r   r   )r   r  outputr  rW   r|   r  r"   r  r   rd   r8   r/  slepcas2rX   policyr^   r   r   s                        r   rsopgp_cert_auto_enroll_ext.rsop  sM   )L77<< 1 18<Dzz$'H%%99'AKK:,EVVc\S0Fvvf 79F34%

DGG(LLC#N4D!%djjBC 5X5E5EF *9#>C01A5-BP-BrRrR-BPtDt5	W 4tDEEJJt,"&C! B;2e9+?$!9Z!/-/F6N-/vr**b0 0O1D E L L N #6N2./?@ zN vr*+CD 4BzNC , 2EE1DAQXXZ1DE vr*;7 "% &F -  QD( Fs   I$I
Ir   )NN)__name__
__module____qualname____firstlineno__r   r   r  r$  r9  r  rC  __static_attributes__r   r   r   r   r   _  s.    .58( :>.O`BH&,r   r   r   )Kerberos):r}   r3   r   samba.gp.gpclassr   r   r   sambar   samba.dcerpcr   	samba.ndrr   rd   r	   r
   
samba.authr   r   rb   shutilr   
subprocessr   r   rP   r   samba.gp.util.loggingr   struct2cryptography.hazmat.primitives.serialization.pkcs7r   ModuleNotFoundErrorrU   ,cryptography.hazmat.primitives.serializationr   cryptography.x509r   cryptography.hazmat.backendsr   samba.commonr   rs   rR   r   r@   r[   rl   rq   rx   r   r   r   r   r   r   r   r   r   r   r   <module>rZ     s   " 
   = =     ) % ,   " 	  % 5$ B 7 8 #	99 
.+`!>.
4QB
"1h G&
SjXj* Xs	  51II 4 55s   "B= =CC