<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division <EOL> from zope . interface import implementer <EOL> from twisted import plugin <EOL> from twisted . cred . strcred import ICheckerFactory <EOL> from twisted . cred . checkers import ICredentialsChecker <EOL> from twisted . cred . credentials import IUsernamePassword <EOL> from twisted . cred . error import UnauthorizedLogin <EOL> from twisted . internet import defer <EOL> def verifyCryptedPassword ( crypted , pw ) : <EOL> if crypted [ <NUM_LIT:0> ] == '<STR_LIT:$>' : <EOL> salt = '<STR_LIT>' + crypted . split ( '<STR_LIT:$>' ) [ <NUM_LIT:2> ] <EOL> else : <EOL> salt = crypted [ : <NUM_LIT:2> ] <EOL> try : <EOL> import crypt <EOL> except ImportError : <EOL> crypt = None <EOL> if crypt is None : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> return crypt . crypt ( pw , salt ) == crypted <EOL> @ implementer ( ICredentialsChecker ) <EOL> class UNIXChecker ( object ) : <EOL> """<STR_LIT>""" <EOL> credentialInterfaces = ( IUsernamePassword , ) <EOL> def checkPwd ( self , pwd , username , password ) : <EOL> try : <EOL> cryptedPass = pwd . getpwnam ( username ) [ <NUM_LIT:1> ] <EOL> except KeyError : <EOL> return defer . fail ( UnauthorizedLogin ( ) ) <EOL> else : <EOL> if cryptedPass in ( '<STR_LIT:*>' , '<STR_LIT:x>' ) : <EOL> return None <EOL> elif verifyCryptedPassword ( cryptedPass , password ) : <EOL> return defer . succeed ( username ) <EOL> def checkSpwd ( self , spwd , username , password ) : <EOL> try : <EOL> cryptedPass = spwd . getspnam ( username ) [ <NUM_LIT:1> ] <EOL> except KeyError : <EOL> return defer . fail ( UnauthorizedLogin ( ) ) <EOL> else : <EOL> if verifyCryptedPassword ( cryptedPass , password ) : <EOL> return defer . succeed ( username ) <EOL> def requestAvatarId ( self , credentials ) : <EOL> username , password = credentials . username , credentials . password <EOL> try : <EOL> import pwd <EOL> except ImportError : <EOL> pwd = None <EOL> if pwd is not None : <EOL> checked = self . checkPwd ( pwd , username , password ) <EOL> if checked is not None : <EOL> return checked <EOL> try : <EOL> import spwd <EOL> except ImportError : <EOL> spwd = None <EOL> if spwd is not None : <EOL> checked = self . checkSpwd ( spwd , username , password ) <EOL> if checked is not None : <EOL> return checked <EOL> return defer . fail ( UnauthorizedLogin ( ) ) <EOL> unixCheckerFactoryHelp = """<STR_LIT>""" <EOL> @ implementer ( ICheckerFactory , plugin . IPlugin ) <EOL> class UNIXCheckerFactory ( object ) : <EOL> """<STR_LIT>""" <EOL> authType = '<STR_LIT>' <EOL> authHelp = unixCheckerFactoryHelp <EOL> argStringFormat = '<STR_LIT>' <EOL> credentialInterfaces = UNIXChecker . credentialInterfaces <EOL> def generateChecker ( self , argstring ) : <EOL> """<STR_LIT>""" <EOL> return UNIXChecker ( ) <EOL> theUnixCheckerFactory = UNIXCheckerFactory ( ) </s>
