
    =OOfAM                         d Z ddlmZmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZmZmZmZ d	d
lmZmZ ddlmZmZmZmZ d Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'y)
    )a2b_hexhexlify)datetime)timegm)UUID)pack   )SEQUENCE_TYPESSTRING_TYPESNUMERIC_TYPESINTEGER_TYPES   )format_timeformat_ad_timestamp)to_raw
to_unicodeldap_escape_to_bytesescape_bytesc                     t        | t        t        f      rd| v r| j                  dd      } | S t        | t              rd| v r| j                  dd      } | S )N   \s   \5C\z\5C)
isinstance	bytearraybytesreplacer   )values    e/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/ldap3/protocol/formatters/validators.pycheck_backslashr   (   sW    %)U+,E>MM%1E L 
E<	(5=MM$/EL    c                 j    t        | |      ryt        | t              r| D ]  }t        ||      r y yy)NTF)r   r   )input_value
value_typer   s      r   
check_typer$   2   s=    +z*+~.  	EeZ0	 r    c                      y)NT r"   s    r   always_validr(   @   s    r    c                 f    t        | t              sy	 t        |       dk(  ry	 y# t        $ r Y yw xY w)NTr   F)r   r   len	Exceptionr'   s    r   validate_generic_single_valuer,   D   sE    k>2{q  !
   s   $ 	00c                    t        | t              s3t        | t              st        | t              rt	        |       dk\  rdS dS yt        |       dk(  r;t        | d   t              st        | d   t              rt	        | d         dk\  rdS dS y)z4Accept -1 and 0 only (used by pwdLastSet in AD)
    TFr   r   )r   r   r   r   intr*   r'   s    r   ,validate_zero_and_minus_one_and_positive_intr0   Q   s     k>2k=1Z\5Z{+r14<u<{q jQ&OS]^ijk^lnzS{{1~."44?%?r    c                 x   t        | t        t        f      ryt        | t              ryt	        | t
              sd}| g} nd}g }ddlm}m} | D ]L  }	 t	        |t              rt        |      n|} ||      }t        |      }||k(  r|j                  |       n yN |r|S |d   S # t        t        |f$ r Y  yw xY w)NFTr   )DecimalInvalidOperation)r$   floatboolr   r   r   decimalr2   r3   r   r   r/   append
ValueError	TypeError)	r"   sequencevalid_valuesr2   r3   elementr   decimal_value	int_values	            r   validate_integerr?   _   s    +t}-+}-k>2"mL1 
		+5gu+EJw'7E#ENME
I	)##I. /
 A I'78 		s   AB%%B98B9c                 "    t        | t              S )N)r$   r   r'   s    r   validate_bytesrA      s    k5))r    c                 *   t        |       rt        | t              r| d   } t        | t              r| ryyt        t
        urt        | t
              rt        |       } t        | t              r(| j                         dk(  ry| j                         dk(  ryy)Nr   TRUEFALSEtruefalseF)	r,   r   r   r5   strr   r   r   lowerr'   s    r   validate_booleanrI      s    $[1k>2%a.Kk4(e
; >$[1Kk<0  "f,""$/r    c                 z   t        | t              sd}| g} nd}g }d}| D ]  }t        t        urt        |t              rt	        |      }t        |t
              rG|j                  d      s"t        t        t        |            t              r|j                  |        yt        |t              r}d}|j                  r!|j                  |j                  d             t        j                         t        j                         z
  }|j                  ||z
  j                  d              y |r	|r|S |d   S y)NFT0000%Y%m%d%H%M%S%z%Y%m%d%H%M%SZr   )r   r   rG   r   r   r   
startswithr   r   r   r7   tzinfostrftimenowutcnowr"   r:   r;   changedr<   offsets         r   validate_time_with_0_yearrV      s    k>2"mLG e
7E : )Gg|,!!&)ZF7O8TV^-_##G,*G~~##G$4$45E$FG!(//*;;##Wv%5$?$?$PQ!$ ?"r    c                 T   t        | t              sd}| g} nd}g }d}| D ]  }t        t        urt        |t              rt	        |      }t        |t
              r6t        t        t        |            t              r|j                  |       n yt        |t              r|d}|j                  r!|j                  |j                  d             t        j                         t        j                         z
  }|j                  ||z
  j                  d              y |r	|r|S |d   S y)NFTrL   rM   r   )r   r   rG   r   r   r   r   r   r   r7   rO   rP   rQ   rR   rS   s         r   validate_timerX      s   k>2"mLG e
7E : )Gg|,+fWo6A##G,*G~~##G$4$45E$FG!(//*;;##Wv%5$?$?$PQ!$ ?"r    c           	      "   t        | t              sd}| g} nd}g }d}| D ]b  }t        t        urt        |t              rt	        |      }t        |t
              r#d|cxk  rdk  rn  y|j                  |       \ yt        |t              r6t        t        t        |            t              r|j                  |        yt        |t              rd}|j                  r:|j                  t        t        |j                               dz   dz  d             t        j                         t        j                         z
  }|j                  t        t        ||z
  j!                               dz   dz  d             c y |r	|r|S |d   S y)	a  
    Active Directory stores date/time values as the number of 100-nanosecond intervals
    that have elapsed since the 0 hour on January 1, 1601 till the date/time that is being stored.
    The time is always stored in Greenwich Mean Time (GMT) in the Active Directory.
    FTr   l    l    !l
 i ascii)encoding)r   r   rG   r   r   r   r7   r   r   r   r   rO   r   utctimetuplerQ   rR   	timetuplerS   s         r   validate_ad_timestampr^      sd    k>2"mLG e
7E : )Gg}-G222  ##G,.-fWo>I##G,*G~~##FF73G3G3I,J[,X\d+dov$wx!(//*;;##FFGf4D3O3O3Q,RU`,`dl+lw~$  A+. ?"r    c                 J    t        | t              r| dkD  ryt        | dz        S )z
    Should be validated like an AD timestamp except that since it is a time
    delta, it is stored as a negative number.
    r   Fr.   )r   r   r^   r'   s    r   validate_ad_timedeltar`     s'    
 k=1[1_ r!122r    c                 
   t        | t              sd}| g} nd}g }d}| D ]c  }t        |t              r(	 |j                  t	        |      j
                         d};t        |t
        t        f      r|j                  |       c y |r	|r|S |d   S y# t        $ rh 	 t        |      }|j                  t	        |      j
                         d}Y # t        $ r& t        t
        urn|j                  |       Y Y Y Y  yw xY ww xY w)z8
    object guid in uuid format (Novell eDirectory)
    FT)r   r   )
r   r   r   r7   r   r   r8   r   rG   r   )r"   r:   r;   rT   r<   xs         r   validate_guidrc     s    k>2"mLG g-##DM$7$78 %!34(+. ?"3  
!,W5A ''1(;(;<"G! !%'$++G4   !s/   &B	D2C'C>6D9D=C>>Dc                    t        | t              sd}| g} nd}g }d}| D ]b  }t        |t              r'	 |j                  t	        t        |                   d}:t        |t        t        f      r|j                  |       b y |r	|r|S |d   S y# t        $ rk 	 |j                  t	        t        |j                  dd                         d}Y # t        $ r& t        t        urn|j                  |       Y Y Y Y  yw xY ww xY w)z)
    object entryUUID in uuid format
    FTr    r   )
r   r   r   r7   rG   r   r8   r   r   r   )r"   r:   r;   rT   r<   s        r   validate_uuidrf   G  s    k>2"mLG g-##CW$67 %!34(), ?"1  	! ''Dr1J,K(LM"G! !%'$++G4   !s/   %B	D5C'D 8D;D?D  Dc                 J   t        | t              sd}| g} nd}g }d}| D ]2  }d}t        |t              r|d   dk(  r1|d   dk(  r)	 |j                  t	        |      j
                         d}nd|v r)	 |j                  t	        |      j
                         d}nkd	|v r2	 |j                  t	        t        |      
      j
                         d}n5d|vr1	 |j                  t	        t        |      
      j
                         d}|st        t        u s|j                  |       
t        |t        t        f      r|j                  |       3 y |r	|r|S |d   S y# t        $ r d}Y hw xY w# t        $ r d}Y yw xY w# t        $ r d}Y w xY w# t        $ r d}Y w xY w)a  
    Active Directory stores objectGUID in uuid_le format, follows RFC4122 and MS-DTYP:
    "{07039e68-4373-264d-a0a7-07039e684373}": string representation big endian, converted to little endian (with or without brace curles)
    "689e030773434d26a7a007039e684373": packet representation, already in little endian
    "\68\9e\03\07\73\43\4d\26\a7\a0\07\03\9e\68\43\73": bytes representation, already in little endian
    byte sequence: already in little endian

    FTr   {r.   })hex-r   )bytes_le)r   r   r   r7   r   rl   r8   r   r   rG   r   r   )r"   r:   r;   rT   r<   errors         r   validate_uuid_lern   s  s    k>2"mLG  g|,qzS WR[C%7! ''(9(B(BC"G ! ''(9(B(BC"G ! ''6J76S(T(](]^"G G#! ''gg6F(G(P(PQ"G #,##G,%!34(A D ?"E " ! E! " ! E! " ! E! " ! E!sH   'E!3'E2 0F0F!E/.E/2F ?F FFF"!F"c           
      H   t        | t              sd}| g} nd}g }d}| D ]  }t        |t              s|j                  d      s&|j	                  d      }t        dt        |d               dd }|t        dt        |dd	             dd z  }t        |d
         dk  r|t        dt        |d
               d
d	 z  }n|t        dt        |d
   d            d
d	 z  }|dd	 D ]  }|t        dt        |            dd z  } |j                  |       d} |r	|r|S |d   S y)a*
  
        SID= "S-1-" IdentifierAuthority 1*SubAuthority
               IdentifierAuthority= IdentifierAuthorityDec / IdentifierAuthorityHex
                  ; If the identifier authority is < 2^32, the
                  ; identifier authority is represented as a decimal
                  ; number
                  ; If the identifier authority is >= 2^32,
                  ; the identifier authority is represented in
                  ; hexadecimal
                IdentifierAuthorityDec =  1*10DIGIT
                  ; IdentifierAuthorityDec, top level authority of a
                  ; security identifier is represented as a decimal number
                IdentifierAuthorityHex = "0x" 12HEXDIG
                  ; IdentifierAuthorityHex, the top-level authority of a
                  ; security identifier is represented as a hexadecimal number
                SubAuthority= "-" 1*10DIGIT
                  ; Sub-Authority is always represented as a decimal number
                  ; No leading "0" characters are allowed when IdentifierAuthority
                  ; or SubAuthority is represented as a decimal number
                  ; All hexadecimal digits must be output in string format,
                  ; pre-pended by "0x"

        Revision (1 byte): An 8-bit unsigned integer that specifies the revision level of the SID. This value MUST be set to 0x01.
        SubAuthorityCount (1 byte): An 8-bit unsigned integer that specifies the number of elements in the SubAuthority array. The maximum number of elements allowed is 15.
        IdentifierAuthority (6 bytes): A SID_IDENTIFIER_AUTHORITY structure that indicates the authority under which the SID was created. It describes the entity that created the SID. The Identifier Authority value {0,0,0,0,0,5} denotes SIDs created by the NT SID authority.
        SubAuthority (variable): A variable length array of unsigned 32-bit integers that uniquely identifies a principal relative to the IdentifierAuthority. Its length is determined by SubAuthorityCount.

        If you have a SID like S-a-b-c-d-e-f-g-...

        Then the bytes are
        a 	(revision)
        N 	(number of dashes minus two)
        bbbbbb 	(six bytes of "b" treated as a 48-bit number in big-endian format)
        cccc 	(four bytes of "c" treated as a 32-bit number in little-endian format)
        dddd 	(four bytes of "d" treated as a 32-bit number in little-endian format)
        eeee 	(four bytes of "e" treated as a 32-bit number in little-endian format)
        ffff 	(four bytes of "f" treated as a 32-bit number in little-endian format)

    FTzS-rk   z<qr   r   r
   N   
   z>q      )	r   r   r   rN   splitr	   r/   r*   r7   )r"   r:   r;   rT   r<   parts	sid_bytessub_auths           r   validate_sidrx     sP   P k>2"mLG g|,!!$'c* s58}5a:	T$E!"I7!<<	uQx=B&dCaM!:12!>>IdCa",=!>qr!BBI %ab	 @HdCM!:1Q!??I@##I. ?"r    N)(__doc__binasciir   r   r   calendarr   uuidr   structr	   re   r   r   r   r   
formattersr   r   
utils.convr   r   r   r   r   r$   r(   r,   r0   r?   rA   rI   rV   rX   r^   r`   rc   rf   rn   rx   r&   r    r   <module>r      s   0 &     K J 8 P P

@*(#L"J+\3*Z)X:zFr    