2009-01-21 14:39:29 +01:00
|
|
|
# -*- python -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
2009-01-21 16:11:39 +01:00
|
|
|
#
|
|
|
|
# DDPortfolio service key finder module
|
2012-01-07 01:46:57 +01:00
|
|
|
# Copyright (c) 2009, 2010, 2011, 2012 Jan Dittberner <jan@dittberner.info>
|
2009-01-21 16:11:39 +01:00
|
|
|
#
|
|
|
|
# This file is part of DDPortfolio service.
|
|
|
|
#
|
|
|
|
# DDPortfolio service is free software: you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Affero General Public License
|
|
|
|
# as published by the Free Software Foundation, either version 3 of
|
|
|
|
# the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# DDPortfolio service is distributed in the hope that it will be
|
|
|
|
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
|
|
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public
|
|
|
|
# License along with this program. If not, see
|
|
|
|
# <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
2009-01-21 14:39:29 +01:00
|
|
|
"""
|
|
|
|
This module provides tools for finding PGP key information from a
|
|
|
|
given keyring.
|
|
|
|
"""
|
2010-10-26 15:40:04 +02:00
|
|
|
import logging
|
|
|
|
import time
|
2013-11-15 14:34:23 +01:00
|
|
|
import sys
|
2009-01-21 14:39:29 +01:00
|
|
|
|
|
|
|
db = None
|
2010-10-26 15:40:04 +02:00
|
|
|
cachetimestamp = 0
|
2009-01-21 14:39:29 +01:00
|
|
|
|
2012-01-07 01:46:57 +01:00
|
|
|
|
2009-01-21 14:39:29 +01:00
|
|
|
def _get_keyring_cache():
|
2010-10-26 15:40:04 +02:00
|
|
|
global db, cachetimestamp
|
|
|
|
if db is None or (time.time() - cachetimestamp) > 86300:
|
2009-01-21 14:39:29 +01:00
|
|
|
import anydbm
|
|
|
|
import pkg_resources
|
|
|
|
import os.path
|
|
|
|
filename = pkg_resources.resource_filename(__name__,
|
|
|
|
'keyringcache')
|
2010-10-26 15:40:04 +02:00
|
|
|
logging.debug('reading cache data from %s', filename)
|
2009-01-21 14:39:29 +01:00
|
|
|
assert os.path.exists(filename) and os.path.isfile(filename)
|
|
|
|
db = anydbm.open(filename, 'r')
|
2010-10-26 15:40:04 +02:00
|
|
|
cachetimestamp = time.time()
|
2009-01-21 14:39:29 +01:00
|
|
|
return db
|
|
|
|
|
2012-01-07 01:46:57 +01:00
|
|
|
|
2009-01-21 14:39:29 +01:00
|
|
|
def _get_cached(cachekey):
|
|
|
|
cache = _get_keyring_cache()
|
2010-10-26 15:40:04 +02:00
|
|
|
logging.debug('cache lookup for %s', cachekey)
|
2009-01-21 14:39:29 +01:00
|
|
|
if cachekey in cache:
|
2010-10-26 15:40:04 +02:00
|
|
|
logging.debug('found entry %s', cache[cachekey])
|
2009-02-17 23:30:03 +01:00
|
|
|
return cache[cachekey]
|
2009-01-21 14:39:29 +01:00
|
|
|
return None
|
|
|
|
|
2012-01-07 01:46:57 +01:00
|
|
|
|
2009-01-21 14:39:29 +01:00
|
|
|
def getFingerprintByEmail(email):
|
2012-01-07 01:46:57 +01:00
|
|
|
"""
|
|
|
|
Gets the fingerprints associated with the given email address if
|
|
|
|
available.
|
|
|
|
"""
|
2009-01-21 14:39:29 +01:00
|
|
|
return _get_cached('fpr:email:%s' % email)
|
|
|
|
|
|
|
|
|
|
|
|
def getRealnameByEmail(email):
|
2012-01-07 01:46:57 +01:00
|
|
|
"""
|
|
|
|
Gets the real names associated with the given email address if
|
|
|
|
available.
|
|
|
|
"""
|
2009-01-21 14:39:29 +01:00
|
|
|
return _get_cached('name:email:%s' % email)
|
|
|
|
|
|
|
|
|
|
|
|
def getLoginByEmail(email):
|
2012-01-07 01:46:57 +01:00
|
|
|
"""
|
|
|
|
Gets the logins associated with the given email address if
|
|
|
|
available.
|
|
|
|
"""
|
2009-01-21 14:39:29 +01:00
|
|
|
return _get_cached('login:email:%s' % email)
|
|
|
|
|
|
|
|
|
|
|
|
def getLoginByFingerprint(fpr):
|
2012-01-07 01:46:57 +01:00
|
|
|
"""
|
|
|
|
Gets the login associated with the given fingerprint if available.
|
|
|
|
"""
|
2009-01-21 14:39:29 +01:00
|
|
|
return _get_cached('login:fpr:%s' % fpr)
|
2013-11-15 14:34:23 +01:00
|
|
|
|
|
|
|
def _dump_cache():
|
|
|
|
cache = _get_keyring_cache()
|
|
|
|
fprs = []
|
|
|
|
for key in cache.keys():
|
|
|
|
if key.startswith('email:fpr:'):
|
|
|
|
fpr = key.replace('email:fpr:', '')
|
|
|
|
if not fpr in fprs:
|
|
|
|
fprs.append(fpr)
|
|
|
|
|
|
|
|
for fpr in fprs:
|
|
|
|
login = getLoginByFingerprint(fpr)
|
|
|
|
email = _get_cached('email:fpr:%s' % fpr)
|
|
|
|
name = _get_cached('name:fpr:%s' % fpr)
|
|
|
|
|
|
|
|
print fpr, login, ':'
|
|
|
|
print ' ', name, email
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
logging.basicConfig(stream=sys.stderr, level=logging.WARNING)
|
|
|
|
_dump_cache()
|