<s> from mammon . events import eventmgr_core , eventmgr_rfc1459 <EOL> from mammon . capability import Capability , caplist <EOL> import base64 <EOL> import binascii <EOL> valid_mechanisms = [ '<STR_LIT>' ] <EOL> cap_sasl = Capability ( '<STR_LIT>' , value = '<STR_LIT:U+002C>' . join ( valid_mechanisms ) ) <EOL> @ eventmgr_core . handler ( '<STR_LIT>' ) <EOL> def m_sasl_start ( info ) : <EOL> ctx = info [ '<STR_LIT>' ] <EOL> if not ctx . hashing . enabled : <EOL> ctx . logger . info ( '<STR_LIT>' ) <EOL> valid_mechanisms . remove ( '<STR_LIT>' ) <EOL> if len ( valid_mechanisms ) == <NUM_LIT:0> : <EOL> ctx . logger . info ( '<STR_LIT>' ) <EOL> del caplist [ '<STR_LIT>' ] <EOL> @ eventmgr_rfc1459 . message ( '<STR_LIT>' , min_params = <NUM_LIT:1> , allow_unregistered = True ) <EOL> def m_AUTHENTICATE ( cli , ev_msg ) : <EOL> if len ( ev_msg [ '<STR_LIT>' ] ) == <NUM_LIT:1> and ev_msg [ '<STR_LIT>' ] [ <NUM_LIT:0> ] == '<STR_LIT:*>' : <EOL> if getattr ( cli , '<STR_LIT>' , None ) : <EOL> cli . dump_numeric ( '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> else : <EOL> cli . dump_numeric ( '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> cli . sasl = None <EOL> cli . sasl_tmp = '<STR_LIT>' <EOL> return <EOL> if getattr ( cli , '<STR_LIT>' , None ) : <EOL> raw_data = ev_msg [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> if len ( raw_data ) > <NUM_LIT> : <EOL> cli . dump_numeric ( '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> cli . sasl = None <EOL> cli . sasl_tmp = '<STR_LIT>' <EOL> return <EOL> elif len ( raw_data ) == <NUM_LIT> : <EOL> if not hasattr ( cli , '<STR_LIT>' ) : <EOL> cli . sasl_tmp = '<STR_LIT>' <EOL> cli . sasl_tmp += raw_data <EOL> if len ( cli . sasl_tmp ) > <NUM_LIT> * <NUM_LIT:4> : <EOL> cli . dump_numeric ( '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> cli . sasl = None <EOL> cli . sasl_tmp = '<STR_LIT>' <EOL> return <EOL> elif getattr ( cli , '<STR_LIT>' , None ) : <EOL> if raw_data != '<STR_LIT:+>' : <EOL> cli . sasl_tmp += raw_data <EOL> try : <EOL> if hasattr ( cli , '<STR_LIT>' ) : <EOL> data = base64 . b64decode ( cli . sasl_tmp ) <EOL> else : <EOL> data = base64 . b64decode ( raw_data ) <EOL> except binascii . Error : <EOL> cli . dump_numeric ( '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> return <EOL> cli . sasl_tmp = '<STR_LIT>' <EOL> eventmgr_core . dispatch ( '<STR_LIT>' . format ( cli . sasl . casefold ( ) ) , { <EOL> '<STR_LIT:source>' : cli , <EOL> '<STR_LIT>' : cli . sasl , <EOL> '<STR_LIT:data>' : data , <EOL> } ) <EOL> else : <EOL> mechanism = ev_msg [ '<STR_LIT>' ] [ <NUM_LIT:0> ] . upper ( ) <EOL> if mechanism in valid_mechanisms : <EOL> cli . sasl = mechanism <EOL> cli . dump_verb ( '<STR_LIT>' , '<STR_LIT:+>' ) <EOL> else : <EOL> cli . dump_numeric ( '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> return <EOL> @ eventmgr_core . handler ( '<STR_LIT>' ) <EOL> def m_sasl_unreglocked ( info ) : <EOL> cli = info [ '<STR_LIT>' ] <EOL> if getattr ( cli , '<STR_LIT>' , None ) : <EOL> cli . sasl = None <EOL> cli . dump_numeric ( '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> @ eventmgr_core . handler ( '<STR_LIT>' ) <EOL> def m_sasl_plain ( info ) : <EOL> cli = info [ '<STR_LIT:source>' ] <EOL> data = info [ '<STR_LIT:data>' ] <EOL> try : <EOL> authorization_id , account , passphrase = data . split ( b'<STR_LIT:\x00>' ) <EOL> except ValueError : <EOL> cli . dump_numeric ( '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> cli . sasl = None <EOL> return <EOL> account = str ( account , '<STR_LIT:utf8>' ) <EOL> passphrase = str ( passphrase , '<STR_LIT:utf8>' ) <EOL> authorization_id = str ( authorization_id , '<STR_LIT:utf8>' ) <EOL> authorization_id = authorization_id or account <EOL> account_info = cli . ctx . data . get ( '<STR_LIT>' . format ( account ) , None ) <EOL> if ( account_info and '<STR_LIT>' in account_info [ '<STR_LIT>' ] and <EOL> account_info [ '<STR_LIT>' ] and authorization_id == account ) : <EOL> passphrase_hash = account_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if cli . ctx . hashing . verify ( passphrase , passphrase_hash ) : <EOL> cli . account = account <EOL> eventmgr_core . dispatch ( '<STR_LIT>' , { <EOL> '<STR_LIT:source>' : cli , <EOL> '<STR_LIT>' : account , <EOL> } ) <EOL> cli . sasl = None <EOL> hostmask = cli . hostmask <EOL> if hostmask is None : <EOL> hostmask = '<STR_LIT:*>' <EOL> cli . dump_numeric ( '<STR_LIT>' , [ hostmask , account , '<STR_LIT>' . format ( account ) ] ) <EOL> cli . dump_numeric ( '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> return <EOL> cli . dump_numeric ( '<STR_LIT>' , [ '<STR_LIT>' ] ) </s>
