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):
|
58 | 58 | def __init__(self): |
59 | 59 | component.Component.__init__(self, "AuthManager") |
60 | 60 | self.__auth = {} |
| 61 | # Map of the supported schemas |
| 62 | self.__supported_schemas = {} |
| 63 | self.__auth_methods = [] |
61 | 64 | |
62 | 65 | def start(self): |
63 | 66 | self.__load_auth_file() |
| 67 | self.__prepare_pam() |
| 68 | self.__load_auth_methods() |
64 | 69 | |
65 | 70 | def stop(self): |
66 | 71 | self.__auth = {} |
| 72 | self.__supported_schemas = {} |
| 73 | self.__auth_methods = [] |
67 | 74 | |
68 | 75 | def shutdown(self): |
69 | 76 | pass |
… |
… |
def authorize(self, username, password):
|
80 | 87 | :raises BadLoginError: if the username does not exist or password does not match |
81 | 88 | |
82 | 89 | """ |
| 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 | |
83 | 106 | |
| 107 | |
| 108 | def authorize_file(self, username, password): |
84 | 109 | if username not in self.__auth: |
85 | 110 | # Let's try to re-load the file.. Maybe it's been updated |
86 | 111 | self.__load_auth_file() |
… |
… |
def authorize(self, username, password):
|
93 | 118 | else: |
94 | 119 | raise BadLoginError("Password does not match") |
95 | 120 | |
| 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 | |
96 | 128 | def __create_localclient_account(self): |
97 | 129 | """ |
98 | 130 | Returns the string. |
… |
… |
def __load_auth_file(self):
|
145 | 177 | |
146 | 178 | if "localclient" not in self.__auth: |
147 | 179 | 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 | |