
    qZh'O                        d Z dZg dZddlZddlmZmZ ddlmZ ddl	Z	ddl
mZ ddlmZ d	Zd
Zedz   ez   Zedz   ez   dz   Zedz   ez   dz   Z ej        dez            Zd e            z  ZdddddZdddddZg dZi ZeD ]Zdee<   d ZdZ ej        e          Zd Z G d d          Z d)d!Z! G d" d#          Z" G d$ d%e"          Z# G d& d'e"          Z$d*d(Z%dS )+zc
ldif - generate and parse LDIF data (see RFC 2849)

See https://www.python-ldap.org/ for details.
z3.4.4)ldif_pattern
CreateLDIF	ParseLDIF
LDIFWriter
LDIFParserLDIFRecordListLDIFCopy    N)	b64encode	b64decode)StringIO)urlparse)urlopenz[\w;.-]+(;[\w_-]+)*z(([^,]|\\,)+|".*?")z	[ ]*=[ ]*z([ ]*\+[ ]*z)*[ ]*z
([ ]*,[ ]*z^%s$z>^((dn(:|::) %(dn_pattern)s)|(%(attrtype_pattern)s(:|::) .*)$)+         )adddeletereplace	incrementr   r   r   r   )r	   r   r   r   )r   r   modifymodrdnc                     | dk    rdS t                               |           }|dk    o|                    d          | k    S )z!
  returns 1 if s is a LDAP DN
   r   Nr	   )dn_regexmatchgroup)srms     P/var/www/lab.imftr.de/alumni/alumnizer_venv/lib/python3.11/site-packages/ldif.pyis_dnr    6   s@     UU1~~a"	T	$bhhqkk1n$    s    (^( |
|| |:|<)|[ 
-]+|[ ]+$)c                     d | D             S )zN
  return a dictionary with all items of l being the keys of the dictionary
  c                     i | ]}|d S N ).0is     r   
<dictcomp>zlist_dict.<locals>.<dictcomp>G   s    			a!T			r!   r%   )ls    r   	list_dictr*   C   s     
	1			r!   c                   >    e Zd ZdZddZd Zd Zd Zd	 Zd
 Z	d Z
dS )r   z
  Write LDIF entry or change records to file object
  Copy LDIF input to a file output object containing all data retrieved
  via URLs
  NL   
c                 ~    || _         t          d |pg D                       | _        || _        || _        d| _        dS )aB  
    output_file
        file object for output; should be opened in *text* mode
    base64_attrs
        list of attribute types to be base64-encoded in any case
    cols
        Specifies how many columns a line may have before it's
        folded into many lines.
    line_sep
        String used as line separator
    c                 6    g | ]}|                                 S r%   lowerr&   as     r   
<listcomp>z'LDIFWriter.__init__.<locals>.<listcomp>^   s     #L#L#L!AGGII#L#L#Lr!   r	   N)_output_filer*   _base64_attrs_cols_last_line_seprecords_written)selfoutput_filebase64_attrscolsline_seps        r   __init__zLDIFWriter.__init__Q   sM     $D"#L#L8J#L#L#LMMDDJ"DDr!   c           	      ~   t          |          }|| j        k    r:| j                            |           | j                            | j                   n| j        }| j                            |dt          || j                                      | j                            | j                   ||k     r| j                            d           | j                            ||t          ||| j        z   dz
                                | j                            | j                   || j        z   dz
  }||k     dS )z7
    Write string line as one or more folded lines
    r	    r   N)lenr7   r5   writer8   min)r:   lineline_lenposs       r   _unfold_lineszLDIFWriter._unfold_linesc   s2   
 4yyH
d###
d12222 Jc
d1S$*%=%=#=>???
d1222LL$$$SXc$*nQ6F)G)G%G HIII 3444$*nQ	 LL
 Fr!   c                 p    |                                 | j        v pt                              |          duS )z
    returns 1 if attr_value has to be base-64 encoded because
    of special chars or because attr_type is in self._base64_attrs
    N)r1   r6   safe_string_research)r:   	attr_type
attr_values      r   _needs_base64_encodingz!LDIFWriter._needs_base64_encodingx   s9    
 ?? 22 9$$Z00D89r!   c                 r   |                      ||          rct          |                              d          }|                    dd          }|                     d                    ||g                     n=|                     d                    ||                    d          g                     dS )z
    Write a single attribute type/value pair

    attr_type
          attribute type (text)
    attr_value
          attribute value (bytes)
    asciir-   r   z:: : N)rN   r
   decoder   rH   join)r:   rL   rM   encodeds       r   _unparseAttrTypeandValuez#LDIFWriter._unparseAttrTypeandValue   s     ""9Z88 M*%%,,W55gR((g
Y$899::::
Iz/@/@/I/I#JKKLLL
Fr!   c                     t          |                                          D ] \  }}|D ]}|                     ||           !dS )z3
    entry
        dictionary holding an entry
    N)sorteditemsrU   )r:   entryrL   valuesrM   s        r   _unparseEntryRecordzLDIFWriter._unparseEntryRecord   s_    
 $EKKMM22 < <	6 < <*%%i
;;;;<< <r!   c                    t          |d                   }|dk    rd}n|dk    rd}nt          d|z            |                     d|                    d                     |D ]}|dk    r|\  }}nP|dk    r;|\  }}}|                     t          |         |                    d                     nt          d	          |r|D ]}|                     ||           |dk    r"| j                            d
| j        z              dS )zP
    modlist
        list of additions (2-tuple) or modifications (3-tuple)
    r	   r   r   r   r   z modlist item of wrong length: %d
changetyperP   z'Subsequent modlist item of wrong length-N)rB   
ValueErrorrU   encode
MOD_OP_STRr5   rC   r8   )	r:   modlistmod_lenr]   modmod_typemod_valsmod_opmod_vals	            r   _unparseChangeRecordzLDIFWriter._unparseChangeRecord   sO   
 '!*ooGzzjj	!jj9WEFFF!!,z/@/@/I/IJJJ 9 9	!A::#& x%%j&8&.oog&>&>	@ 	@ 	@ 	@ BCCC	 : 	: 	:G

'
'
9
9
9
9	!D$7 78889 9r!   c                    |                     d          }|                     d|           t          |t                    r|                     |           nJt          |t
                    r|                     |           nt          dt          |          z            | j	        
                    | j                   | j        dz   | _        dS )z
    dn
          string-representation of distinguished name
    record
          Either a dictionary holding the LDAP entry {attrtype:record}
          or a list with a modify list like for LDAPObject.modify().
    utf-8dnz8Argument record must be dictionary or list instead of %sr   N)r`   rU   
isinstancedictr[   listri   r_   reprr5   rC   r8   r9   )r:   rl   records      r   unparsezLDIFWriter.unparse   s     
7		B!!$+++& d
v&&&&	F4	 	  d
''''QUYZ`UaUabcccD/000/1D
Fr!   )Nr,   r-   )__name__
__module____qualname____doc__r?   rH   rN   rU   r[   ri   rr   r%   r!   r   r   r   J   s            $  *9 9 9  $< < <9 9 98    r!   r   r,   c                     t          j        dt          d           t                      }t	          |||d          }|                    | |           |                                }|                                 |S )a  
  Create LDIF single formatted record including trailing empty line.
  This is a compatibility function.

  dn
        string-representation of distinguished name
  record
        Either a dictionary holding the LDAP entry {attrtype:record}
        or a list with a modify list like for LDAPObject.modify().
  base64_attrs
        list of attribute types to be base64-encoded in any case
  cols
        Specifies how many columns a line may have before it's
        folded into many lines.
  zhldif.CreateLDIF() is deprecated. Use LDIFWriter.unparse() instead. It will be removed in python-ldap 3.1r   category
stacklevelr-   )warningswarnDeprecationWarningr   r   rr   getvalueclose)rl   rq   r<   r=   fldif_writerr   s          r   r   r      s{      -)	    jj!1\$t44+b   jjll!'')))	
(r!   c                   Z    e Zd ZdZ	 	 	 	 ddZd Zd Zd Zd	 Zd
 Z	d Z
d ZddZd ZdS )r   z
  Base class for a LDIF parser. Applications should sub-class this
  class and override method handle() to implement something meaningful.

  Public class attributes:

  records_read
        Counter for records processed so far
  Nr	   r-   c                    || _         t          | j                             d          t                    | _        || _        t          d |pg D                       | _        t          d |pg D                       | _        || _	        d| _
        d| _        d| _        d| _        i                     t          d          | _        t"          | _        	 |                                 | _        dS # t*          $ r d| _        Y dS w xY w)a  
    Parameters:
    input_file
        File-object to read the LDIF input from
    ignored_attr_types
        Attributes with these attribute type names will be ignored.
    max_entries
        If non-zero specifies the maximum number of entries to be
        read from f.
    process_url_schemes
        List containing strings with URLs schemes to process with urllib.
        An empty list turns off all URL processing and the attribute
        is ignored completely.
    line_sep
        String used as line separator
    r	   c                 6    g | ]}|                                 S r%   r0   )r&   r   s     r   r4   z'LDIFParser.__init__.<locals>.<listcomp>  s     *Z*Z*Z17799*Z*Z*Zr!   c                 6    g | ]}|                                 S r%   r0   r2   s     r   r4   z'LDIFParser.__init__.<locals>.<listcomp>  s     )X)X)X!'')))X)X)Xr!   Nr   )_input_filerm   readbytes_file_sends_bytes_max_entriesr*   _process_url_schemes_ignored_attr_typesr8   versionline_counterbyte_counterrecords_readfromkeysCHANGE_TYPESchangetype_counterr   
_b64decode	_readline
_last_lineEOFError)r:   
input_fileignored_attr_typesmax_entriesprocess_url_schemesr>   s         r   r?   zLDIFParser.__init__   s	   0 "D'(8(=(=a(@(@%HHD#D )*Z*Z?R?XVX*Z*Z*Z [ [D()X)X>P>VTV)X)X)XYYD"DDLDDD kk,q99DDO((dooo   doooos   C, ,D Dc                     dS )zw
    Process a single content LDIF record. This method should be
    implemented by applications using LDIFParser.
    Nr%   r:   rl   rY   s      r   handlezLDIFParser.handle&  s	    
 	Dr!   c                 .   | j                                         }| j        r|                    d          }| j        dz   | _        | j        t          |          z   | _        |sd S |dd          dk    r
|d d         S |dd          dk    r
|d d         S |S )Nrk   r   z
r-   )r   readliner   rR   r   r   rB   )r:   r   s     r   r   zLDIFParser._readline-  s    !!##A  ((7

a)A-D)CFF2D T	
233ssVm	
233ssVmhr!   c                 V   | j         t          d| j        | j        fz            | j         g}|                                 }|rK|d         dk    r?|                    |dd                    |                                 }|r|d         dk    ?|| _         d                    |          S )zG
    Unfold several folded lines with trailing space into one line
    Nz%EOF reached after %d lines (%d bytes)r	   rA   r   r   )r   r   r   r   r   appendrS   )r:   unfolded_lines	next_lines      r   rH   zLDIFParser._unfold_lines>  s     <@  	 	 	 (N  I
 #	!c))IabbM***..""i  #	!c))  DO77>"""r!   c                 v   |                                  }|r.|d         dk    r"|                                  }|r|d         dk    "|sdS |dk    rdS 	 |                    d          }n1# t          $ r$}t          dt          |          z            d}~ww xY w|d|         }|||d	z            }|d
k    r5||d	z   d                                         }|                    d          }n|dk    r8||d	z   d         }|                    d          }|                     |          }n|dk    rh||d	z   d                                         }d}| j        r?t          |          }|d         | j        v r!t          |                                          }n ||dz   d                             d          }||fS )z
    Parse a single attribute type and value pair from one or
    more lines of LDIF data

    Returns attr_type (text) and attr_value (bytes)
    r	   #NNr^   )r^   N:zno value-spec in %sNr   rQ   rk   z::rP   z:<r   )rH   indexr_   rp   lstripr`   r   stripr   r   r   r   )	r:   unfolded_line	colon_poserL   
value_specrM   urlus	            r   _next_key_and_valuezLDIFParser._next_key_and_valueO  s    &&((M
 +M!,c11((**m  +M!,c11 YcXF%%c**ii F F F,]0C0CDEEEFa	k*Iy145J4 1.5577j $$W--jj	T		 !1.j$$W--j??:..jj	T		)A+,,'--//cj		" +SMMQ44,,,s||((*** !1.55g>>jZs   A( (
B2BBc                     | j         }	  |            \  }}|| |            \  }}||n# t          $ r d\  }}Y nw xY w||fS )z
    Consume empty lines until first non-empty line.
    Must only be used between full records!

    Returns non-empty key-value-tuple.
    Nr   )r   r   )r:   next_key_and_valuekvs       r   _consume_empty_lineszLDIFParser._consume_empty_lines  s|     1  caI!)  ""! I!)   caQ3Js   ", >>c                    | j         }	 |                                 \  }}|dk    r>t          |                    d                    | _        |                                 \  }}n# t
          $ r Y dS w xY w|dk    rx| j        r| j        | j        k     r`|dk    r&t          d| j	        t          |          fz            |                    d          }t          |          s&t          d| j	        t          |          fz            |}i } |            \  }}|dk    rx|                                | j        vr3	 ||                             |           n# t          $ r	 |g||<   Y nw xY w	  |            \  }}n# t
          $ r d\  }}Y nw xY w|dk    x|                     ||           | j        d	z   | _        |                                 \  }}|dk    r| j        O| j        | j        k     `dS )
z8
    Continuously read and parse LDIF entry records
    r   rP   Nrl   ;Line %d: First line of record does not start with "dn:": %srk   6Line %d: Not a valid string-representation for dn: %s.r   r   )r   r   intrR   r   r   r   r   r_   r   rp   r    r1   r   r   KeyErrorr   )r:   r   r   r   rl   rY   s         r   parse_entry_recordszLDIFParser.parse_entry_records  s_   
 1%%''ca	
I188G,,--''))!   ff T''  $($5d6G$G$G	
DVZ^ZklpqrlslsYttuuu ((7

a1XX qQUYUfgklmgngnToopppbe  ca tGGwwyyD444!HOOA   SE!HHH	""$$#!AA 	 	 	#!AAA	 tGG kk"U+a/d%%''caC T''  $($5d6G$G$GB Fs6   AA% %
A32A3>E E-,E-1E? ?FFc                 *    |                                  S )zM
    Invokes LDIFParser.parse_entry_records() for backward compatibility
    )r   )r:   s    r   parsezLDIFParser.parse  s     ##%%%r!   c                     g pd}dS z
    Process a single LDIF record representing a single modify operation.
    This method should be implemented by applications using LDIFParser.
    Nr%   r:   rl   modopscontrolss       r   handle_modifyzLDIFParser.handle_modify  s    
 zTHDr!   c                 |   | j         }|                                 \  }}|dk    r+t          |          | _        |                                 \  }}|d k    re| j        r| j        | j        k     rM|dk    r&t          d| j        t          |          fz            |	                    d          }t          |          s&t          d| j        t          |          fz            |} |            \  }}g }|d k    r|dk    r|	                    d          }	 |                    dd          \  }}}n+# t          $ r d }|                    dd	          \  }}Y nw xY w|                    |||f            |            \  }}|d k    r|dk    d }	|d
k    rL|	                    d          }|t          vrt          dt          |          z            |}	 |            \  }}|	dk    r@g }
	 |d k    r	 t          |         }n4# t          $ r' t          d| j        t          |          fz            w xY w|	                    d          }|}g }	  |            \  }}n# t           $ r d\  }}Y nw xY w||k    r?|                    |           	  |            \  }}n# t           $ r d\  }}Y nw xY w||k    ?|
                    |||pd f            |            \  }}|dk    r |            \  }}|d k    n# t           $ r d\  }}Y nw xY w|
r|                     ||
|           n|d k    r |            \  }}|d k    |                                 \  }}	 | j        |	         d	z   | j        |	<   n# t          $ r d	| j        |	<   Y nw xY w| j        d	z   | _        |d k    r| j        <| j        | j        k     Md S )Nr   rl   r   rk   r   controlrA   r   r   r]   zInvalid changetype: %sr   z"Line %d: Invalid mod-op string: %sr   r^   )r   r   r   r   r   r   r_   r   rp   rR   r    splitr   valid_changetype_dictMOD_OP_INTEGERr   r   r   r   )r:   r   r   r   rl   r   control_typecriticalitycontrol_valuer]   r   modopmodattr	modvaluess                 r   parse_change_recordszLDIFParser.parse_change_records  s   1

#
#
%
%CAa)||VVdl%%''ca T''  $($5d6G$G$G	
DVZ^ZklpqrlslsYttuuu ((7

a1XX qQUYUfgklmgngnToopppb  cahtGG9HHW	434773q>>
0,{== 	4 	4 	4-%&WWS^^
",{{{	4 	k-@AAA  ""! tGG9 j	
LHHW)))3d1gg=>>
>
  ""!	X		 	4c$Q'ee c c cCtGXY]^_Y`Y`Faabbbc !!AGI&&((ca   caW**q!!! ((**!!      !!! 	 W** MM5):d;<<<$$&&CAa#vv&&((ca3 44  	 	 	#!AAA	  	1


Rx
0
0
0
 gg""$$#!A gg %%''ca0.2.Ej.QTU.U
++ 0 0 0./
+++0+a/dw T''  $($5d6G$G$Gx Fs   D3 3%EE-K; 5H K; 1H44K; I K; I1.K; 0I11K; J K; J0-K; /J00A
K; ;LLM4 4N
N)Nr	   Nr-   r$   )rs   rt   ru   rv   r?   r   r   rH   r   r   r   r   r   r   r%   r!   r   r   r      s          + + + +Z	 	 	  "# # #".  .  . `  $4 4 4l& & &	 	 	 	h h h h hr!   r   c                   *    e Zd ZdZ	 ddZd ZddZdS )	r   z
  Collect all records of a LDIF file. It can be a memory hog!

  Records are stored in :attr:`.all_records` as a single list
  of 2-tuples (dn, entry), after calling :meth:`.parse`.
  Nr	   c                 ^    t                               | ||||           g | _        g | _        d S r$   )r   r?   all_recordsall_modify_changes)r:   r   r   r   r   s        r   r?   zLDIFRecordList.__init__H  s:    
 Z(:;GZ[[[ D Dr!   c                 >    | j                             ||f           dS )zS
    Append a single record to the list of all records (:attr:`.all_records`).
    N)r   r   r   s      r   r   zLDIFRecordList.handleS  s%     	RJ'''''r!   c                 H    g pd}| j                             |||f           dS r   )r   r   r   s       r   r   zLDIFRecordList.handle_modifyY  s1    
 zTH""Bvh#788888r!   )Nr	   Nr$   )rs   rt   ru   rv   r?   r   r   r%   r!   r   r   r   @  s[          ?C	! 	! 	! 	!( ( (9 9 9 9 9 9r!   r   c                   $    e Zd ZdZ	 	 ddZd ZdS )	r   zM
  Copy LDIF input to LDIF output containing all data retrieved
  via URLs
  Nr	   r,   r-   c	                 p    t                               | ||||           t          ||||          | _        dS )z=
    See LDIFParser.__init__() and LDIFWriter.__init__()
    N)r   r?   r   _output_ldif)	r:   r   r;   r   r   r   r<   r=   r>   s	            r   r?   zLDIFCopy.__init__h  s>     Z(:;GZ[[[";|DJJDr!   c                 <    | j                             ||           dS )z2
    Write single LDIF record to output file.
    N)r   rr   r   s      r   r   zLDIFCopy.handlet  s#     	b'''''r!   )Nr	   NNr,   r-   )rs   rt   ru   rv   r?   r   r%   r!   r   r   r   b  sN          ?C'+	
K 
K 
K 
K( ( ( ( (r!   r   c                     t          j        dt          d           t          | ||d          }|                                 |j        S )zL
  Parse LDIF records read from file.
  This is a compatibility function.
  zildif.ParseLDIF() is deprecated. Use LDIFRecordList.parse() instead. It will be removed in python-ldap 3.1r   rx   r	   )r   r   r   )r{   r|   r}   r   r   r   )r   ignore_attrs
maxentriesldif_parsers       r   r   r   {  se    
 -)	    *QR  + 		  r!   )Nr,   )Nr	   )&rv   __version____all__rebase64r
   r   ior   r{   urllib.parser   urllib.requestr   attrtype_patternattrvalue_patternattrtypeandvalue_patternrdn_pattern
dn_patterncompiler   varsr   r   ra   r   r   cr    SAFE_STRING_PATTERNrJ   r*   r   r   r   r   r   r   r%   r!   r   <module>r      s}   
 
 
 
 
			 ' ' ' ' ' ' ' '        ! ! ! ! ! ! " " " " " ") * +l:=NN (>9<TTW``]*[89D
RZ+,,ORVRVRXRXX 	
	  
Hy; 
 211 	    A% % % J /00  C C C C C C C CL   <O O O O O O O Od
9 9 9 9 9Z 9 9 9D( ( ( ( (z ( ( (2! ! ! ! ! !r!   