<s> import coro <EOL> from coro . asn1 . ber import * <EOL> from coro . ldap . query import * <EOL> import re <EOL> from coro . log import Facility <EOL> LOG = Facility ( '<STR_LIT>' ) <EOL> FLAGS_STRUCTURED = <NUM_LIT> <EOL> FLAGS_APPLICATION = <NUM_LIT> <EOL> FLAGS_CONTEXT = <NUM_LIT> <EOL> W = coro . write_stderr <EOL> re_dn = re . compile ( r'<STR_LIT>' ) <EOL> re_dn_attr = re . compile ( r'<STR_LIT>' ) <EOL> class ProtocolError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Exit_Recv_Thread ( Exception ) : <EOL> "<STR_LIT>" <EOL> pass <EOL> class LDAP : <EOL> BindRequest = <NUM_LIT:0> <EOL> BindResponse = <NUM_LIT:1> <EOL> UnbindRequest = <NUM_LIT:2> <EOL> SearchRequest = <NUM_LIT:3> <EOL> SearchResultEntry = <NUM_LIT:4> <EOL> SearchResultDone = <NUM_LIT:5> <EOL> SearchResultReference = <NUM_LIT> <EOL> ModifyRequest = <NUM_LIT:6> <EOL> ModifyResponse = <NUM_LIT:7> <EOL> AddRequest = <NUM_LIT:8> <EOL> AddResponse = <NUM_LIT:9> <EOL> DelRequest = <NUM_LIT:10> <EOL> DelResponse = <NUM_LIT:11> <EOL> ModifyDNRequest = <NUM_LIT:12> <EOL> ModifyDNResponse = <NUM_LIT> <EOL> CompareRequest = <NUM_LIT> <EOL> CompareResponse = <NUM_LIT:15> <EOL> AbandonRequest = <NUM_LIT:16> <EOL> ExtendedRequest = <NUM_LIT> <EOL> ExtendedResponse = <NUM_LIT> <EOL> class SCOPE : <EOL> BASE = <NUM_LIT:0> <EOL> ONELEVEL = <NUM_LIT:1> <EOL> SUBTREE = <NUM_LIT:2> <EOL> class DEREF : <EOL> NEVER = <NUM_LIT:0> <EOL> SEARCHING = <NUM_LIT:1> <EOL> FINDING = <NUM_LIT:2> <EOL> ALWAYS = <NUM_LIT:3> <EOL> def encode_search_request ( <EOL> base_object , <EOL> scope , <EOL> deref_aliases , <EOL> size_limit , <EOL> time_limit , <EOL> types_only , <EOL> filter , <EOL> which_attrs = None , <EOL> compatibility = { } <EOL> ) : <EOL> if scope is None : <EOL> scope = compatibility . get ( '<STR_LIT>' , SCOPE . SUBTREE ) <EOL> if which_attrs is None : <EOL> which_attrs = SEQUENCE ( ) <EOL> elif len ( which_attrs ) == <NUM_LIT:0> : <EOL> which_attrs = SEQUENCE ( <EOL> OCTET_STRING ( compatibility . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> ) <EOL> else : <EOL> which_attrs = SEQUENCE ( * [ OCTET_STRING ( x ) for x in which_attrs ] ) <EOL> return TLV ( <EOL> LDAP . SearchRequest , <EOL> FLAGS_APPLICATION | FLAGS_STRUCTURED , <EOL> OCTET_STRING ( base_object ) , <EOL> ENUMERATED ( scope ) , <EOL> ENUMERATED ( deref_aliases ) , <EOL> INTEGER ( size_limit ) , <EOL> INTEGER ( time_limit ) , <EOL> BOOLEAN ( types_only ) , <EOL> parse_query ( filter ) , <EOL> which_attrs , <EOL> ) <EOL> class AUTH : <EOL> simple = <NUM_LIT> <EOL> sasl = <NUM_LIT> <EOL> class RESULT : <EOL> success = <NUM_LIT:0> <EOL> operationsError = <NUM_LIT:1> <EOL> protocolError = <NUM_LIT:2> <EOL> timeLimitExceeded = <NUM_LIT:3> <EOL> sizeLimitExceeded = <NUM_LIT:4> <EOL> compareFalse = <NUM_LIT:5> <EOL> compareTrue = <NUM_LIT:6> <EOL> authMethodNotSupported = <NUM_LIT:7> <EOL> strongAuthRequired = <NUM_LIT:8> <EOL> referral = <NUM_LIT:10> <EOL> adminLimitExceeded = <NUM_LIT:11> <EOL> unavailableCriticalExtension = <NUM_LIT:12> <EOL> confidentialityRequired = <NUM_LIT> <EOL> saslBindInProgress = <NUM_LIT> <EOL> noSuchAttribute = <NUM_LIT:16> <EOL> undefinedAttributeType = <NUM_LIT> <EOL> inappropriateMatching = <NUM_LIT> <EOL> constraintViolation = <NUM_LIT> <EOL> attributeOrValueExists = <NUM_LIT:20> <EOL> invalidAttributeSyntax = <NUM_LIT> <EOL> noSuchObject = <NUM_LIT:32> <EOL> aliasProblem = <NUM_LIT> <EOL> invalidDNSyntax = <NUM_LIT> <EOL> aliasDereferencingProblem = <NUM_LIT> <EOL> inappropriateAuthentication = <NUM_LIT> <EOL> invalidCredentials = <NUM_LIT> <EOL> insufficientAccessRights = <NUM_LIT:50> <EOL> busy = <NUM_LIT> <EOL> unavailable = <NUM_LIT> <EOL> unwillingToPerform = <NUM_LIT> <EOL> loopDetect = <NUM_LIT> <EOL> namingViolation = <NUM_LIT:64> <EOL> objectClassViolation = <NUM_LIT> <EOL> notAllowedOnNonLeaf = <NUM_LIT> <EOL> notAllowedOnRDN = <NUM_LIT> <EOL> entryAlreadyExists = <NUM_LIT> <EOL> objectClassModsProhibited = <NUM_LIT> <EOL> affectsMultipleDSAs = <NUM_LIT> <EOL> other = <NUM_LIT> <EOL> class Error ( Exception ) : <EOL> def __init__ ( self , answer ) : <EOL> Exception . __init__ ( self ) <EOL> self . code = answer [ <NUM_LIT:0> ] <EOL> self . answer = answer <EOL> self . error_string = result_string ( answer [ <NUM_LIT:0> ] ) <EOL> def __str__ ( self ) : <EOL> if len ( self . answer ) == <NUM_LIT:3> : <EOL> if self . answer [ <NUM_LIT:2> ] : <EOL> parenthesize_got_dn = <NUM_LIT:1> <EOL> err_msg = "<STR_LIT>" % ( self . answer [ <NUM_LIT:2> ] , ) <EOL> else : <EOL> parenthesize_got_dn = <NUM_LIT:0> <EOL> err_msg = "<STR_LIT>" <EOL> if self . answer [ <NUM_LIT:1> ] : <EOL> err_msg += "<STR_LIT:U+0020>" <EOL> if parenthesize_got_dn : <EOL> err_msg += "<STR_LIT:(>" <EOL> err_msg += "<STR_LIT>" % ( self . answer [ <NUM_LIT:1> ] , ) <EOL> if parenthesize_got_dn : <EOL> err_msg += "<STR_LIT:)>" <EOL> else : <EOL> err_msg = "<STR_LIT>" % ( self . answer , ) <EOL> return '<STR_LIT>' % ( self . error_string , self . code , <EOL> err_msg ) <EOL> __repr__ = __str__ <EOL> RESULT . _reverse_map = r = { } <EOL> for attr in dir ( RESULT ) : <EOL> value = getattr ( RESULT , attr ) <EOL> if isinstance ( value , type ( <NUM_LIT:0> ) ) : <EOL> r [ value ] = attr <EOL> def result_string ( result ) : <EOL> try : <EOL> return RESULT . _reverse_map [ result ] <EOL> except KeyError : <EOL> return "<STR_LIT>" % ( result , ) <EOL> def encode_bind_request ( version , name , auth_data ) : <EOL> assert ( <NUM_LIT:1> <= version <= <NUM_LIT> ) <EOL> return TLV ( <EOL> LDAP . BindRequest , <EOL> FLAGS_APPLICATION | FLAGS_STRUCTURED , <EOL> INTEGER ( version ) , <EOL> OCTET_STRING ( name ) , <EOL> auth_data <EOL> ) <EOL> def encode_simple_bind ( version , name , login ) : <EOL> return encode_bind_request ( <EOL> version , <EOL> name , <EOL> TLV ( AUTH . simple , FLAGS_CONTEXT , login ) <EOL> ) <EOL> def encode_sasl_bind ( version , name , mechanism , credentials = '<STR_LIT>' ) : <EOL> if credentials : <EOL> cred = OCTET_STRING ( credentials ) <EOL> else : <EOL> cred = '<STR_LIT>' <EOL> return encode_bind_request ( <EOL> version , <EOL> name , <EOL> TLV ( <EOL> AUTH . sasl , <EOL> FLAGS_CONTEXT | FLAGS_STRUCTURED , <EOL> OCTET_STRING ( mechanism ) , <EOL> cred <EOL> ) <EOL> ) <EOL> def encode_starttls ( ) : <EOL> return TLV ( <EOL> LDAP . ExtendedRequest , <EOL> FLAGS_APPLICATION , <EOL> TLV ( <NUM_LIT:0> , FLAGS_CONTEXT , '<STR_LIT>' ) <EOL> ) <EOL> class ReadyQueue : <EOL> "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> self . q = [ ] <EOL> self . cv = coro . condition_variable ( ) <EOL> def __len__ ( self ) : <EOL> return len ( self . q ) <EOL> def push ( self , item ) : <EOL> if not self . cv . wake_one ( item ) : <EOL> self . q . insert ( <NUM_LIT:0> , item ) <EOL> def pop ( self ) : <EOL> if len ( self . q ) : <EOL> return self . q . pop ( ) <EOL> else : <EOL> return self . cv . wait ( ) <EOL> def pop_all ( self ) : <EOL> if not ( len ( self . q ) ) : <EOL> return [ self . cv . wait ( ) ] <EOL> else : <EOL> result , self . q = self . q , [ ] <EOL> return result <EOL> class client : <EOL> def __init__ ( self , addr ) : <EOL> self . msgid = <NUM_LIT:1> <EOL> self . addr = addr <EOL> if isinstance ( addr , tuple ) : <EOL> self . sock = coro . tcp_sock ( ) <EOL> else : <EOL> self . sock = coro . unix_sock ( ) <EOL> self . sock . connect ( addr ) <EOL> self . pending = { } <EOL> self . recv_thread_ob = coro . spawn ( self . recv_thread ) <EOL> def recv_exact ( self , size ) : <EOL> try : <EOL> return self . sock . recv_exact ( size ) <EOL> except AttributeError : <EOL> left = size <EOL> r = [ ] <EOL> while left : <EOL> block = self . sock . recv ( left ) <EOL> if not block : <EOL> break <EOL> else : <EOL> r . append ( block ) <EOL> left -= len ( block ) <EOL> return '<STR_LIT>' . join ( r ) <EOL> def _recv_packet ( self ) : <EOL> tl = self . recv_exact ( <NUM_LIT:2> ) <EOL> if not tl : <EOL> return [ None , None ] <EOL> tag = tl [ <NUM_LIT:0> ] <EOL> if tag != '<STR_LIT:0>' : <EOL> raise ProtocolError ( '<STR_LIT>' % ( tag , ) ) <EOL> l = ord ( tl [ <NUM_LIT:1> ] ) <EOL> p = [ tl ] <EOL> if l & <NUM_LIT> : <EOL> ll = l & <NUM_LIT> <EOL> len_bytes = self . recv_exact ( ll ) <EOL> p . append ( len_bytes ) <EOL> n = <NUM_LIT:0> <EOL> for i in xrange ( ll ) : <EOL> n = ( n << <NUM_LIT:8> ) | ord ( len_bytes [ i ] ) <EOL> if ( n < <NUM_LIT:0> ) or ( n > <NUM_LIT> ) : <EOL> raise ProtocolError ( '<STR_LIT>' % ( n , ) ) <EOL> need = n <EOL> else : <EOL> need = l <EOL> p . append ( self . recv_exact ( need ) ) <EOL> packet = '<STR_LIT>' . join ( p ) <EOL> reply , plen = decode ( packet ) <EOL> return reply <EOL> def recv_thread ( self ) : <EOL> while not self . exit_recv_thread : <EOL> [ msgid , reply ] = self . _recv_packet ( ) <EOL> if msgid is None : <EOL> break <EOL> else : <EOL> probe = self . pending . get ( msgid , None ) <EOL> if probe is None : <EOL> raise ProtocolError ( '<STR_LIT>' % ( msgid , ) ) <EOL> else : <EOL> probe . push ( reply ) <EOL> default_timeout = <NUM_LIT:10> <EOL> def send_message ( self , msg ) : <EOL> msgid = self . msgid <EOL> self . msgid += <NUM_LIT:1> <EOL> self . sock . send ( SEQUENCE ( INTEGER ( msgid ) , msg ) ) <EOL> try : <EOL> q = ReadyQueue ( ) <EOL> self . pending [ msgid ] = q <EOL> reply = coro . with_timeout ( self . default_timeout , q . pop ) <EOL> return reply <EOL> finally : <EOL> del self . pending [ msgid ] <EOL> def search ( self , * args , ** kwargs ) : <EOL> msg = encode_search_request ( * args , ** kwargs ) <EOL> msgid = self . msgid <EOL> self . msgid += <NUM_LIT:1> <EOL> self . sock . send ( SEQUENCE ( INTEGER ( msgid ) , msg ) ) <EOL> result = [ ] <EOL> q = ReadyQueue ( ) <EOL> self . pending [ msgid ] = q <EOL> try : <EOL> while <NUM_LIT:1> : <EOL> reply = coro . with_timeout ( self . default_timeout , q . pop ) <EOL> kind , tag , data = reply <EOL> if kind == '<STR_LIT>' : <EOL> if tag == LDAP . SearchResultDone : <EOL> break <EOL> else : <EOL> result . append ( reply [ <NUM_LIT:2> ] ) <EOL> else : <EOL> raise ProtocolError ( '<STR_LIT>' , reply ) <EOL> return result <EOL> finally : <EOL> del self . pending [ msgid ] <EOL> exit_recv_thread = False <EOL> def starttls ( self , * future_cert_params ) : <EOL> import tlslite <EOL> self . exit_recv_thread = True <EOL> reply = self . send_message ( encode_starttls ( ) ) <EOL> if reply [ <NUM_LIT:2> ] == <NUM_LIT:0> : <EOL> conn = tlslite . TLSConnection ( self . sock ) <EOL> conn . handshakeClientCert ( ) <EOL> self . osock = self . sock <EOL> self . sock = conn <EOL> self . exit_recv_thread = False <EOL> self . recv_thread_ob = coro . spawn ( self . recv_thread ) <EOL> return reply <EOL> ldap_protocol_version = <NUM_LIT:3> <EOL> def simple_bind ( self , name , login ) : <EOL> return self . send_message ( encode_simple_bind ( self . ldap_protocol_version , name , login ) ) <EOL> def sasl_bind ( self , name , mechanism , credentials ) : <EOL> return self . send_message ( encode_sasl_bind ( self . ldap_protocol_version , name , mechanism , credentials ) ) <EOL> def unbind ( self ) : <EOL> msgid = self . msgid <EOL> self . msgid += <NUM_LIT:1> <EOL> self . sock . send ( SEQUENCE ( INTEGER ( msgid ) , TLV ( LDAP . UnbindRequest , FLAGS_APPLICATION , '<STR_LIT>' ) ) ) <EOL> def t0 ( ) : <EOL> return encode_search_request ( <EOL> '<STR_LIT>' , <EOL> SCOPE . SUBTREE , <EOL> DEREF . NEVER , <EOL> <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <EOL> '<STR_LIT>' , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> ) <EOL> def t1 ( ) : <EOL> LOG ( '<STR_LIT>' ) <EOL> c = client ( ( '<STR_LIT:127.0.0.1>' , <NUM_LIT> ) ) <EOL> LOG ( '<STR_LIT>' ) <EOL> c . simple_bind ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> LOG ( '<STR_LIT>' ) <EOL> r = c . search ( <EOL> '<STR_LIT>' , <EOL> SCOPE . SUBTREE , <EOL> DEREF . NEVER , <EOL> <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <EOL> '<STR_LIT>' , <EOL> [ ] <EOL> ) <EOL> LOG ( '<STR_LIT>' ) <EOL> c . unbind ( ) <EOL> from pprint import pprint <EOL> pprint ( r ) <EOL> coro . set_exit ( ) <EOL> return r <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import coro . backdoor <EOL> coro . spawn ( coro . backdoor . serve , unix_path = '<STR_LIT>' ) <EOL> coro . spawn ( t1 ) <EOL> coro . event_loop ( ) </s>
