<s> from fnmatch import fnmatch <EOL> import xmlrpclib <EOL> import httplib <EOL> import socket <EOL> import ssl <EOL> import os . path <EOL> import os <EOL> def get_host_list ( cert ) : <EOL> if '<STR_LIT>' in cert : <EOL> return [ x [ <NUM_LIT:1> ] for x in cert [ '<STR_LIT>' ] if x [ <NUM_LIT:0> ] == '<STR_LIT>' ] <EOL> else : <EOL> return [ x [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] for x in cert [ '<STR_LIT>' ] if x [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT>' ] <EOL> def validate ( cert , hostname ) : <EOL> hosts = get_host_list ( cert ) <EOL> for host in hosts : <EOL> if fnmatch ( host , hostname ) : <EOL> return True <EOL> return False <EOL> class DebileHTTPSConnection ( httplib . HTTPSConnection ) : <EOL> def __init__ ( <EOL> self , host , port = None , <EOL> key_file = None , cert_file = None , ca_certs = None , <EOL> strict = None , timeout = socket . _GLOBAL_DEFAULT_TIMEOUT , <EOL> source_address = None <EOL> ) : <EOL> if not ( os . path . isfile ( cert_file ) and os . access ( cert_file , os . R_OK ) ) : <EOL> raise Exception ( "<STR_LIT>" + cert_file ) <EOL> if not ( os . path . isfile ( key_file ) and os . access ( key_file , os . R_OK ) ) : <EOL> raise Exception ( "<STR_LIT>" + key_file ) <EOL> if not ( os . path . isfile ( ca_certs ) and os . access ( ca_certs , os . R_OK ) ) : <EOL> raise Exception ( "<STR_LIT>" + ca_certs ) <EOL> httplib . HTTPSConnection . __init__ ( <EOL> self , host = host , port = port , <EOL> key_file = key_file , cert_file = cert_file , <EOL> strict = strict , timeout = timeout , <EOL> source_address = source_address <EOL> ) <EOL> self . ca_certs = ca_certs <EOL> def connect ( self ) : <EOL> sock = socket . create_connection ( <EOL> ( self . host , self . port ) , <EOL> self . timeout , self . source_address <EOL> ) <EOL> if self . _tunnel_host : <EOL> self . sock = sock <EOL> self . _tunnel ( ) <EOL> cert_reqs = ( ssl . CERT_NONE if self . ca_certs is None <EOL> else ssl . CERT_REQUIRED ) <EOL> self . sock = ssl . wrap_socket ( <EOL> sock , self . key_file , self . cert_file , <EOL> ca_certs = self . ca_certs , cert_reqs = cert_reqs , <EOL> do_handshake_on_connect = True , ssl_version = ssl . PROTOCOL_TLSv1 , <EOL> ) <EOL> if not validate ( self . sock . getpeercert ( ) , self . host ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % self . host ) <EOL> class DebileSafeTransport ( xmlrpclib . Transport ) : <EOL> def __init__ ( self , key_file = None , cert_file = None , ca_certs = None ) : <EOL> xmlrpclib . Transport . __init__ ( self ) <EOL> self . key_file = key_file <EOL> self . cert_file = cert_file <EOL> self . ca_certs = ca_certs <EOL> def make_connection ( self , host ) : <EOL> host = ( host , { <EOL> '<STR_LIT>' : self . key_file , <EOL> '<STR_LIT>' : self . cert_file , <EOL> '<STR_LIT>' : self . ca_certs , <EOL> } ) <EOL> if self . _connection and host == self . _connection [ <NUM_LIT:0> ] : <EOL> return self . _connection [ <NUM_LIT:1> ] <EOL> chost , self . _extra_headers , x509 = self . get_host_info ( host ) <EOL> self . _connection = host , DebileHTTPSConnection ( chost , None , timeout = <NUM_LIT> , ** ( x509 or { } ) ) <EOL> return self . _connection [ <NUM_LIT:1> ] <EOL> def get_proxy ( config , auth_method ) : <EOL> xml = config . get ( "<STR_LIT>" , None ) <EOL> if xml is None : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> proxy = None <EOL> if auth_method == '<STR_LIT>' : <EOL> proxy = xmlrpclib . ServerProxy ( <EOL> "<STR_LIT>" . format ( <EOL> host = xml [ '<STR_LIT:host>' ] , <EOL> port = xml [ '<STR_LIT:port>' ] , <EOL> ) , <EOL> allow_none = True ) <EOL> else : <EOL> proxy = xmlrpclib . ServerProxy ( <EOL> "<STR_LIT>" . format ( <EOL> host = xml [ '<STR_LIT:host>' ] , <EOL> port = xml [ '<STR_LIT:port>' ] , <EOL> ) , transport = DebileSafeTransport ( <EOL> key_file = xml . get ( '<STR_LIT>' , None ) , <EOL> cert_file = xml . get ( '<STR_LIT>' , None ) , <EOL> ca_certs = xml . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> ) , allow_none = True ) <EOL> return proxy </s>
