<s> import logging <EOL> import syslog <EOL> from passlib . hash import sha512_crypt <EOL> import ajenti <EOL> import ajenti . usersync <EOL> from ajenti . api import * <EOL> def restrict ( permission ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( fx ) : <EOL> def wrapper ( * args , ** kwargs ) : <EOL> UserManager . get ( ) . require_permission ( extract_context ( ) , permission ) <EOL> return fx ( * args , ** kwargs ) <EOL> return wrapper <EOL> return decorator <EOL> class SecurityError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , permission ) : <EOL> self . permission = permission <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % self . permission <EOL> @ plugin <EOL> @ persistent <EOL> @ rootcontext <EOL> class UserManager ( BasePlugin ) : <EOL> default_classconfig = { '<STR_LIT>' : '<STR_LIT>' } <EOL> classconfig_root = True <EOL> def check_password ( self , username , password , env = None ) : <EOL> """<STR_LIT>""" <EOL> if not username or not password : <EOL> return False <EOL> provider = self . get_sync_provider ( fallback = True ) <EOL> if username == '<STR_LIT:root>' and not provider . syncs_root : <EOL> provider = ajenti . usersync . AjentiSyncProvider . get ( ) <EOL> if not username in ajenti . config . tree . users : <EOL> return False <EOL> try : <EOL> provider . sync ( ) <EOL> except Exception as e : <EOL> logging . error ( str ( e ) ) <EOL> result = provider . check_password ( username , password ) <EOL> provider_name = type ( provider ) . __name__ <EOL> ip_notion = '<STR_LIT>' <EOL> ip = env . get ( '<STR_LIT>' , None ) if env else None <EOL> if ip : <EOL> ip_notion = '<STR_LIT>' % ip <EOL> if not result : <EOL> msg = '<STR_LIT>' % ( username , password , provider_name , ip_notion ) <EOL> syslog . syslog ( syslog . LOG_WARNING , msg ) <EOL> logging . warn ( msg ) <EOL> else : <EOL> msg = '<STR_LIT>' % ( username , provider_name , ip_notion ) <EOL> syslog . syslog ( syslog . LOG_INFO , msg ) <EOL> logging . info ( msg ) <EOL> return result <EOL> def hash_password ( self , password ) : <EOL> """<STR_LIT>""" <EOL> if not password . startswith ( '<STR_LIT>' ) : <EOL> password = '<STR_LIT>' % sha512_crypt . encrypt ( password ) <EOL> return password <EOL> def hash_passwords ( self ) : <EOL> for user in ajenti . config . tree . users . values ( ) : <EOL> if not user . password . startswith ( '<STR_LIT>' ) : <EOL> user . password = self . hash_password ( user . password ) <EOL> def has_permission ( self , context , permission ) : <EOL> """<STR_LIT>""" <EOL> if context . user . name == '<STR_LIT:root>' : <EOL> return True <EOL> if not permission in context . user . permissions : <EOL> return False <EOL> return True <EOL> def require_permission ( self , context , permission ) : <EOL> """<STR_LIT>""" <EOL> if not self . has_permission ( context , permission ) : <EOL> raise SecurityError ( permission ) <EOL> def get_sync_provider ( self , fallback = False ) : <EOL> """<STR_LIT>""" <EOL> for p in ajenti . usersync . UserSyncProvider . get_classes ( ) : <EOL> p . get ( ) <EOL> if p . id == self . classconfig [ '<STR_LIT>' ] : <EOL> try : <EOL> p . get ( ) . test ( ) <EOL> except : <EOL> if fallback : <EOL> return ajenti . usersync . AjentiSyncProvider . get ( ) <EOL> return p . get ( ) <EOL> def set_sync_provider ( self , provider_id ) : <EOL> self . classconfig [ '<STR_LIT>' ] = provider_id <EOL> self . save_classconfig ( ) <EOL> def set_password ( self , username , password ) : <EOL> ajenti . config . tree . users [ username ] . password = self . hash_password ( password ) <EOL> @ interface <EOL> class PermissionProvider ( object ) : <EOL> """<STR_LIT>""" <EOL> def get_permissions ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ ] <EOL> def get_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] </s>
