<s> """<STR_LIT>""" <EOL> from twisted . python import log <EOL> from twisted . python . filepath import FilePath <EOL> from twisted . conch . error import ConchError <EOL> from twisted . conch . ssh import common , keys , userauth <EOL> from twisted . internet import defer , protocol , reactor <EOL> from twisted . conch . client . knownhosts import KnownHostsFile , ConsoleUI <EOL> from twisted . conch . client import agent <EOL> import os , sys , base64 , getpass <EOL> _open = open <EOL> def verifyHostKey ( transport , host , pubKey , fingerprint ) : <EOL> """<STR_LIT>""" <EOL> actualHost = transport . factory . options [ '<STR_LIT:host>' ] <EOL> actualKey = keys . Key . fromString ( pubKey ) <EOL> kh = KnownHostsFile . fromPath ( FilePath ( <EOL> transport . factory . options [ '<STR_LIT>' ] <EOL> or os . path . expanduser ( "<STR_LIT>" ) <EOL> ) ) <EOL> ui = ConsoleUI ( lambda : _open ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> return kh . verifyHostKey ( ui , actualHost , host , actualKey ) <EOL> def isInKnownHosts ( host , pubKey , options ) : <EOL> """<STR_LIT>""" <EOL> keyType = common . getNS ( pubKey ) [ <NUM_LIT:0> ] <EOL> retVal = <NUM_LIT:0> <EOL> if not options [ '<STR_LIT>' ] and not os . path . exists ( os . path . expanduser ( '<STR_LIT>' ) ) : <EOL> print '<STR_LIT>' <EOL> os . mkdir ( os . path . expanduser ( '<STR_LIT>' ) ) <EOL> kh_file = options [ '<STR_LIT>' ] or '<STR_LIT>' <EOL> try : <EOL> known_hosts = open ( os . path . expanduser ( kh_file ) ) <EOL> except IOError : <EOL> return <NUM_LIT:0> <EOL> for line in known_hosts . xreadlines ( ) : <EOL> split = line . split ( ) <EOL> if len ( split ) < <NUM_LIT:3> : <EOL> continue <EOL> hosts , hostKeyType , encodedKey = split [ : <NUM_LIT:3> ] <EOL> if host not in hosts . split ( '<STR_LIT:U+002C>' ) : <EOL> continue <EOL> if hostKeyType != keyType : <EOL> continue <EOL> try : <EOL> decodedKey = base64 . decodestring ( encodedKey ) <EOL> except : <EOL> continue <EOL> if decodedKey == pubKey : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> retVal = <NUM_LIT:2> <EOL> return retVal <EOL> class SSHUserAuthClient ( userauth . SSHUserAuthClient ) : <EOL> def __init__ ( self , user , options , * args ) : <EOL> userauth . SSHUserAuthClient . __init__ ( self , user , * args ) <EOL> self . keyAgent = None <EOL> self . options = options <EOL> self . usedFiles = [ ] <EOL> if not options . identitys : <EOL> options . identitys = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def serviceStarted ( self ) : <EOL> if '<STR_LIT>' in os . environ and not self . options [ '<STR_LIT>' ] : <EOL> log . msg ( '<STR_LIT>' ) <EOL> cc = protocol . ClientCreator ( reactor , agent . SSHAgentClient ) <EOL> d = cc . connectUNIX ( os . environ [ '<STR_LIT>' ] ) <EOL> d . addCallback ( self . _setAgent ) <EOL> d . addErrback ( self . _ebSetAgent ) <EOL> else : <EOL> userauth . SSHUserAuthClient . serviceStarted ( self ) <EOL> def serviceStopped ( self ) : <EOL> if self . keyAgent : <EOL> self . keyAgent . transport . loseConnection ( ) <EOL> self . keyAgent = None <EOL> def _setAgent ( self , a ) : <EOL> self . keyAgent = a <EOL> d = self . keyAgent . getPublicKeys ( ) <EOL> d . addBoth ( self . _ebSetAgent ) <EOL> return d <EOL> def _ebSetAgent ( self , f ) : <EOL> userauth . SSHUserAuthClient . serviceStarted ( self ) <EOL> def _getPassword ( self , prompt ) : <EOL> try : <EOL> oldout , oldin = sys . stdout , sys . stdin <EOL> sys . stdin = sys . stdout = open ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> p = getpass . getpass ( prompt ) <EOL> sys . stdout , sys . stdin = oldout , oldin <EOL> return p <EOL> except ( KeyboardInterrupt , IOError ) : <EOL> print <EOL> raise ConchError ( '<STR_LIT>' ) <EOL> def getPassword ( self , prompt = None ) : <EOL> if not prompt : <EOL> prompt = "<STR_LIT>" % ( self . user , self . transport . transport . getPeer ( ) . host ) <EOL> try : <EOL> p = self . _getPassword ( prompt ) <EOL> return defer . succeed ( p ) <EOL> except ConchError : <EOL> return defer . fail ( ) <EOL> def getPublicKey ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . keyAgent : <EOL> key = self . keyAgent . getPublicKey ( ) <EOL> if key is not None : <EOL> return key <EOL> files = [ x for x in self . options . identitys if x not in self . usedFiles ] <EOL> log . msg ( str ( self . options . identitys ) ) <EOL> log . msg ( str ( files ) ) <EOL> if not files : <EOL> return None <EOL> file = files [ <NUM_LIT:0> ] <EOL> log . msg ( file ) <EOL> self . usedFiles . append ( file ) <EOL> file = os . path . expanduser ( file ) <EOL> file += '<STR_LIT>' <EOL> if not os . path . exists ( file ) : <EOL> return self . getPublicKey ( ) <EOL> try : <EOL> return keys . Key . fromFile ( file ) <EOL> except keys . BadKeyError : <EOL> return self . getPublicKey ( ) <EOL> def signData ( self , publicKey , signData ) : <EOL> """<STR_LIT>""" <EOL> if not self . usedFiles : <EOL> return self . keyAgent . signData ( publicKey . blob ( ) , signData ) <EOL> else : <EOL> return userauth . SSHUserAuthClient . signData ( self , publicKey , signData ) <EOL> def getPrivateKey ( self ) : <EOL> """<STR_LIT>""" <EOL> file = os . path . expanduser ( self . usedFiles [ - <NUM_LIT:1> ] ) <EOL> if not os . path . exists ( file ) : <EOL> return None <EOL> try : <EOL> return defer . succeed ( keys . Key . fromFile ( file ) ) <EOL> except keys . EncryptedKeyError : <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> prompt = "<STR_LIT>" % self . usedFiles [ - <NUM_LIT:1> ] <EOL> try : <EOL> p = self . _getPassword ( prompt ) <EOL> return defer . succeed ( keys . Key . fromFile ( file , passphrase = p ) ) <EOL> except ( keys . BadKeyError , ConchError ) : <EOL> pass <EOL> return defer . fail ( ConchError ( '<STR_LIT>' ) ) <EOL> raise <EOL> except KeyboardInterrupt : <EOL> print <EOL> reactor . stop ( ) <EOL> def getGenericAnswers ( self , name , instruction , prompts ) : <EOL> responses = [ ] <EOL> try : <EOL> oldout , oldin = sys . stdout , sys . stdin <EOL> sys . stdin = sys . stdout = open ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if name : <EOL> print name <EOL> if instruction : <EOL> print instruction <EOL> for prompt , echo in prompts : <EOL> if echo : <EOL> responses . append ( raw_input ( prompt ) ) <EOL> else : <EOL> responses . append ( getpass . getpass ( prompt ) ) <EOL> finally : <EOL> sys . stdout , sys . stdin = oldout , oldin <EOL> return defer . succeed ( responses ) </s>
