Unbreak getting password from UI

Commit 9239a2d326 broke getting the password from the UI. This
unbreaks the change and adds some extended documentation and cleanups in
the functino en-passent.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
This commit is contained in:
Sebastian 2010-12-13 11:45:42 -06:00 committed by Nicolas Sebrecht
parent 62712cbe15
commit b853b58578
2 changed files with 23 additions and 9 deletions

View File

@ -176,7 +176,7 @@ class IMAPServer:
challenge = response.strip() challenge = response.strip()
ui.debug('imap', 'md5handler: got challenge %s' % challenge) ui.debug('imap', 'md5handler: got challenge %s' % challenge)
passwd = self.repos.getpassword() passwd = self.getpassword()
retval = self.username + ' ' + hmac.new(passwd, challenge).hexdigest() retval = self.username + ' ' + hmac.new(passwd, challenge).hexdigest()
ui.debug('imap', 'md5handler: returning %s' % retval) ui.debug('imap', 'md5handler: returning %s' % retval)
return retval return retval
@ -184,7 +184,7 @@ class IMAPServer:
def plainauth(self, imapobj): def plainauth(self, imapobj):
UIBase.getglobalui().debug('imap', UIBase.getglobalui().debug('imap',
'Attempting plain authentication') 'Attempting plain authentication')
imapobj.login(self.username, self.repos.getpassword()) imapobj.login(self.username, self.getpassword())
def gssauth(self, response): def gssauth(self, response):
data = base64.b64encode(response) data = base64.b64encode(response)

View File

@ -152,24 +152,35 @@ class IMAPRepository(BaseRepository):
return self.getconfboolean('expunge', 1) return self.getconfboolean('expunge', 1)
def getpassword(self): def getpassword(self):
passwd = None """Return the IMAP password for this repository.
localeval = self.localeval
if self.config.has_option(self.getsection(), 'remotepasseval'): It tries to get passwords in the following order:
passwd = self.getconf('remotepasseval')
1. evaluate Repository 'remotepasseval'
2. read password from Repository 'remotepass'
3. read password from file specified in Repository 'remotepassfile'
4. read password from ~/.netrc
5. read password from /etc/netrc
On success we return the password.
If all strategies fail we return None.
"""
# 1. evaluate Repository 'remotepasseval'
passwd = self.getconf('remotepasseval', None)
if passwd != None: if passwd != None:
return localeval.eval(passwd) return self.localeval.eval(passwd)
# 2. read password from Repository 'remotepass'
password = self.getconf('remotepass', None) password = self.getconf('remotepass', None)
if password != None: if password != None:
return password return password
# 3. read password from file specified in Repository 'remotepassfile'
passfile = self.getconf('remotepassfile', None) passfile = self.getconf('remotepassfile', None)
if passfile != None: if passfile != None:
fd = open(os.path.expanduser(passfile)) fd = open(os.path.expanduser(passfile))
password = fd.readline().strip() password = fd.readline().strip()
fd.close() fd.close()
return password return password
# 4. read password from ~/.netrc
try: try:
netrcentry = netrc.netrc().authenticators(self.gethost()) netrcentry = netrc.netrc().authenticators(self.gethost())
except IOError, inst: except IOError, inst:
@ -180,6 +191,7 @@ class IMAPRepository(BaseRepository):
user = self.getconf('remoteuser') user = self.getconf('remoteuser')
if user == None or user == netrcentry[0]: if user == None or user == netrcentry[0]:
return netrcentry[2] return netrcentry[2]
# 5. read password from /etc/netrc
try: try:
netrcentry = netrc.netrc('/etc/netrc').authenticators(self.gethost()) netrcentry = netrc.netrc('/etc/netrc').authenticators(self.gethost())
except IOError, inst: except IOError, inst:
@ -190,8 +202,10 @@ class IMAPRepository(BaseRepository):
user = self.getconf('remoteuser') user = self.getconf('remoteuser')
if user == None or user == netrcentry[0]: if user == None or user == netrcentry[0]:
return netrcentry[2] return netrcentry[2]
# no strategy yielded a password!
return None return None
def getfolder(self, foldername): def getfolder(self, foldername):
return self.getfoldertype()(self.imapserver, foldername, return self.getfoldertype()(self.imapserver, foldername,
self.nametrans(foldername), self.nametrans(foldername),