<s> """<STR_LIT>""" <EOL> import PAM <EOL> import getpass , threading , os <EOL> from twisted . internet import threads , defer <EOL> def pamAuthenticateThread ( service , user , conv ) : <EOL> def _conv ( items ) : <EOL> from twisted . internet import reactor <EOL> try : <EOL> d = conv ( items ) <EOL> except : <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> return <EOL> ev = threading . Event ( ) <EOL> def cb ( r ) : <EOL> ev . r = ( <NUM_LIT:1> , r ) <EOL> ev . set ( ) <EOL> def eb ( e ) : <EOL> ev . r = ( <NUM_LIT:0> , e ) <EOL> ev . set ( ) <EOL> reactor . callFromThread ( d . addCallbacks , cb , eb ) <EOL> ev . wait ( ) <EOL> done = ev . r <EOL> if done [ <NUM_LIT:0> ] : <EOL> return done [ <NUM_LIT:1> ] <EOL> else : <EOL> raise done [ <NUM_LIT:1> ] . type , done [ <NUM_LIT:1> ] . value <EOL> return callIntoPAM ( service , user , _conv ) <EOL> def callIntoPAM ( service , user , conv ) : <EOL> """<STR_LIT>""" <EOL> pam = PAM . pam ( ) <EOL> pam . start ( service ) <EOL> pam . set_item ( PAM . PAM_USER , user ) <EOL> pam . set_item ( PAM . PAM_CONV , conv ) <EOL> gid = os . getegid ( ) <EOL> uid = os . geteuid ( ) <EOL> os . setegid ( <NUM_LIT:0> ) <EOL> os . seteuid ( <NUM_LIT:0> ) <EOL> try : <EOL> pam . authenticate ( ) <EOL> pam . acct_mgmt ( ) <EOL> return <NUM_LIT:1> <EOL> finally : <EOL> os . setegid ( gid ) <EOL> os . seteuid ( uid ) <EOL> def defConv ( items ) : <EOL> resp = [ ] <EOL> for i in range ( len ( items ) ) : <EOL> message , kind = items [ i ] <EOL> if kind == <NUM_LIT:1> : <EOL> p = getpass . getpass ( message ) <EOL> resp . append ( ( p , <NUM_LIT:0> ) ) <EOL> elif kind == <NUM_LIT:2> : <EOL> p = raw_input ( message ) <EOL> resp . append ( ( p , <NUM_LIT:0> ) ) <EOL> elif kind in ( <NUM_LIT:3> , <NUM_LIT:4> ) : <EOL> print message <EOL> resp . append ( ( "<STR_LIT>" , <NUM_LIT:0> ) ) <EOL> else : <EOL> return defer . fail ( '<STR_LIT:foo>' ) <EOL> d = defer . succeed ( resp ) <EOL> return d <EOL> def pamAuthenticate ( service , user , conv ) : <EOL> return threads . deferToThread ( pamAuthenticateThread , service , user , conv ) </s>
