Ticket #1471: pam_auth.patch

File pam_auth.patch, 3.6 KB (added by v-for-vandal, 11 years ago)

Patch that implement changes

  • deluge/core/authmanager.py

    From afff1ee9b00cfac01932d15a5f54ad787a16c115 Mon Sep 17 00:00:00 2001
    From: Artem Serebriyskiy <v.for.vandal@gmail.com>
    Date: Sun, 2 Jan 2011 21:39:00 +0300
    Subject: [PATCH] Add PAM authentication to Deluged
    
    ---
     deluge/core/authmanager.py |   63 ++++++++++++++++++++++++++++++++++++++++++++
     1 files changed, 63 insertions(+), 0 deletions(-)
    
    diff --git a/deluge/core/authmanager.py b/deluge/core/authmanager.py
    index 106351b..d78e51f 100644
    a b class AuthManager(component.Component): 
    5858    def __init__(self): 
    5959        component.Component.__init__(self, "AuthManager") 
    6060        self.__auth = {} 
     61        # Map of the supported schemas 
     62        self.__supported_schemas = {} 
     63        self.__auth_methods = [] 
    6164 
    6265    def start(self): 
    6366        self.__load_auth_file() 
     67        self.__prepare_pam() 
     68        self.__load_auth_methods() 
    6469 
    6570    def stop(self): 
    6671        self.__auth = {} 
     72        self.__supported_schemas = {} 
     73        self.__auth_methods = [] 
    6774 
    6875    def shutdown(self): 
    6976        pass 
    def authorize(self, username, password): 
    8087        :raises BadLoginError: if the username does not exist or password does not match 
    8188 
    8289        """ 
     90        success = False 
     91        level = 0 
     92        for m in self.__auth_methods: 
     93            try: 
     94                level = m(username,password) 
     95                success = True 
     96                break 
     97            except BadLoginError,e: 
     98                log.debug("Auth method failed with error:%s"%(e.message,)) 
     99                pass 
     100 
     101        if not success: 
     102            raise BadLoginError("Authentification failed") 
     103 
     104        return level 
     105 
    83106 
     107 
     108    def authorize_file(self, username, password): 
    84109        if username not in self.__auth: 
    85110            # Let's try to re-load the file.. Maybe it's been updated 
    86111            self.__load_auth_file() 
    def authorize(self, username, password): 
    93118        else: 
    94119            raise BadLoginError("Password does not match") 
    95120 
     121    def authorize_pam(self,username,password): 
     122        if self.pam.authenticate(username, password, service='login'):  
     123            return AUTH_LEVEL_DEFAULT 
     124        else : 
     125            log.info("PAM authentification failed") 
     126            raise BadLoginError("Can't authentificate with PAM") 
     127 
    96128    def __create_localclient_account(self): 
    97129        """ 
    98130        Returns the string. 
    def __load_auth_file(self): 
    145177 
    146178        if "localclient" not in self.__auth: 
    147179            open(auth_file, "a").write(self.__create_localclient_account()) 
     180 
     181        self.__supported_schemas['file'] = self.authorize_file 
     182 
     183    def __prepare_pam(self): 
     184        try: 
     185            self.pam = __import__('pam') 
     186            self.__supported_schemas['pam'] = self.authorize_pam 
     187        except ImportError: 
     188            pass 
     189     
     190    def __load_auth_methods(self): 
     191        config = configmanager.ConfigManager("core.conf").config 
     192        auth_list = [] 
     193        ## Load desired authentification methods  
     194        try: 
     195            auth_list = config['auth_methods'] 
     196        except KeyError: 
     197            auth_list = ['file'] 
     198 
     199        log.info("Desired auth methods order: %s"%(auth_list,)) 
     200         
     201        ## Remove unsupported methods from this list 
     202        auth_list = filter(lambda x: x in self.__supported_schemas, auth_list) 
     203 
     204        if ( len(auth_list) == 0 ): 
     205            log.error("None of the selected authentification methods is supported on this system") 
     206        else: 
     207            log.debug("Available authentification methods: %s" %(auth_list,)) 
     208 
     209        self.__auth_methods = map(lambda x: self.__supported_schemas[x], auth_list) 
     210