2009-01-21 14:39:29 +01:00
|
|
|
# -*- python -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
2009-01-21 16:11:39 +01:00
|
|
|
#
|
2014-01-11 01:03:09 +01:00
|
|
|
# Debian Member Portfolio service application key ring analyzer tool
|
2009-01-21 16:11:39 +01:00
|
|
|
#
|
2014-01-11 01:03:09 +01:00
|
|
|
# Copyright © 2009-2014 Jan Dittberner <jan@dittberner.info>
|
2009-01-21 16:11:39 +01:00
|
|
|
#
|
2014-01-11 01:03:09 +01:00
|
|
|
# This file is part of the Debian Member Portfolio service.
|
|
|
|
#
|
|
|
|
# Debian Member Portfolio 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
|
2009-01-21 16:11:39 +01:00
|
|
|
# the License, or (at your option) any later version.
|
|
|
|
#
|
2014-01-11 01:03:09 +01:00
|
|
|
# Debian Member Portfolio 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.
|
2009-01-21 16:11:39 +01:00
|
|
|
#
|
|
|
|
# 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 is a tool that analyzes GPG and PGP keyrings and stores the
|
|
|
|
retrieved data in a file database. The tool was inspired by Debian
|
|
|
|
qa's carnivore.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import anydbm
|
|
|
|
import pkg_resources
|
|
|
|
import glob
|
|
|
|
import ConfigParser
|
|
|
|
import os
|
2010-06-06 00:31:41 +02:00
|
|
|
import os.path
|
2010-10-26 15:40:04 +02:00
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2013-11-15 14:31:46 +01:00
|
|
|
import email.utils
|
2009-01-21 14:39:29 +01:00
|
|
|
|
|
|
|
|
2014-01-11 01:03:09 +01:00
|
|
|
CONFIG = ConfigParser.SafeConfigParser()
|
|
|
|
|
|
|
|
|
2009-01-21 14:39:29 +01:00
|
|
|
def _get_keyrings():
|
2012-01-07 01:46:57 +01:00
|
|
|
"""
|
|
|
|
Gets the available keyring files from the keyring directory
|
|
|
|
configured in ddportfolio.ini.
|
|
|
|
"""
|
2014-01-11 01:03:09 +01:00
|
|
|
keyringdir = os.path.expanduser(CONFIG.get('DEFAULT', 'keyring.dir'))
|
2010-10-26 15:40:04 +02:00
|
|
|
logging.debug("keyring dir is %s", keyringdir)
|
2010-06-06 00:31:41 +02:00
|
|
|
keyrings = glob.glob(os.path.join(keyringdir, '*.gpg'))
|
|
|
|
keyrings.extend(glob.glob(os.path.join(keyringdir, '*.pgp')))
|
2009-01-21 14:39:29 +01:00
|
|
|
keyrings.sort()
|
|
|
|
return keyrings
|
|
|
|
|
2014-01-11 01:03:09 +01:00
|
|
|
|
2009-01-21 14:39:29 +01:00
|
|
|
def _parse_uid(uid):
|
2012-01-07 01:46:57 +01:00
|
|
|
"""
|
|
|
|
Parse a uid of the form 'Real Name <email@example.com>' into email
|
|
|
|
and realname parts.
|
|
|
|
"""
|
2014-01-11 01:03:09 +01:00
|
|
|
|
2013-11-18 13:59:49 +01:00
|
|
|
# First try with the Python library, but it doesn't always catch everything
|
|
|
|
(name, mail) = email.utils.parseaddr(uid)
|
|
|
|
if (not name) and (not mail):
|
|
|
|
logging.warning("malformed uid %s", uid)
|
|
|
|
if (not name) or (not mail):
|
|
|
|
logging.debug("strange uid %s: '%s' - <%s>", uid, name, mail)
|
|
|
|
# Try and do better than the python library
|
|
|
|
if not '@' in mail:
|
|
|
|
uid = uid.strip()
|
|
|
|
# First, strip comment
|
|
|
|
s = uid.find('(')
|
|
|
|
e = uid.find(')')
|
|
|
|
if s >= 0 and e >= 0:
|
|
|
|
uid = uid[:s] + uid[e + 1:]
|
|
|
|
s = uid.find('<')
|
|
|
|
e = uid.find('>')
|
|
|
|
mail = None
|
|
|
|
if s >= 0 and e >= 0:
|
|
|
|
mail = uid[s + 1:e]
|
|
|
|
uid = uid[:s] + uid[e + 1:]
|
|
|
|
uid = uid.strip()
|
|
|
|
if not mail and uid.find('@') >= 0:
|
|
|
|
mail, uid = uid, mail
|
|
|
|
|
|
|
|
name = uid
|
|
|
|
logging.debug("corrected: '%s' - <%s>", name, mail)
|
|
|
|
return (name, mail)
|
2009-01-21 14:39:29 +01:00
|
|
|
|
|
|
|
resultdict = {}
|
|
|
|
|
2012-01-07 01:46:57 +01:00
|
|
|
|
2009-01-21 14:39:29 +01:00
|
|
|
def _get_canonical(key):
|
|
|
|
if not key in resultdict:
|
|
|
|
resultdict[key] = []
|
|
|
|
return key
|
|
|
|
|
|
|
|
|
|
|
|
def _add_to_result(key, newvalue):
|
2013-11-15 14:31:46 +01:00
|
|
|
logging.debug("adding %s: %s", key, newvalue)
|
2009-01-21 14:39:29 +01:00
|
|
|
thekey = _get_canonical(key)
|
|
|
|
if newvalue not in resultdict[thekey]:
|
|
|
|
resultdict[thekey].append(newvalue)
|
|
|
|
|
|
|
|
|
2014-01-11 01:03:09 +01:00
|
|
|
def _handle_mail(mail, fpr):
|
|
|
|
if mail.endswith('@debian.org'):
|
|
|
|
login = mail[0:-len('@debian.org')]
|
|
|
|
_add_to_result('login:email:%s' % mail, login)
|
|
|
|
_add_to_result('login:fpr:%s' % fpr, login)
|
|
|
|
_add_to_result('fpr:login:%s' % login, fpr)
|
|
|
|
_add_to_result('fpr:email:%s' % mail, fpr)
|
|
|
|
_add_to_result('email:fpr:%s' % fpr, mail)
|
|
|
|
|
|
|
|
|
|
|
|
def _handle_uid(uid, fpr):
|
|
|
|
# Do stuff with 'uid'
|
|
|
|
if uid:
|
|
|
|
(uid, mail) = _parse_uid(uid)
|
|
|
|
if mail:
|
|
|
|
_handle_mail(mail, fpr)
|
|
|
|
if uid:
|
|
|
|
_add_to_result('name:fpr:%s' % fpr, uid)
|
|
|
|
if mail:
|
|
|
|
_add_to_result('name:email:%s' % mail, uid)
|
|
|
|
return fpr
|
|
|
|
|
|
|
|
|
|
|
|
def process_gpg_list_keys_line(line, fpr):
|
|
|
|
"""
|
|
|
|
Process a line of gpg --list-keys --with-colon output.
|
|
|
|
"""
|
|
|
|
items = line.split(':')
|
|
|
|
if items[0] == 'pub':
|
|
|
|
return None
|
|
|
|
if items[0] == 'fpr':
|
|
|
|
return items[9].strip()
|
|
|
|
if items[0] == 'uid':
|
|
|
|
if items[1] == 'r':
|
|
|
|
return fpr
|
|
|
|
return _handle_uid(items[9].strip(), fpr)
|
|
|
|
else:
|
|
|
|
return fpr
|
|
|
|
|
|
|
|
|
2009-01-21 14:39:29 +01:00
|
|
|
def process_keyrings():
|
|
|
|
"""Process the keyrings and store the extracted data in an anydbm
|
|
|
|
file."""
|
|
|
|
for keyring in _get_keyrings():
|
2010-10-26 15:40:04 +02:00
|
|
|
logging.debug("get data from %s", keyring)
|
2014-01-11 01:03:09 +01:00
|
|
|
proc = subprocess.Popen([
|
|
|
|
"gpg", "--no-options", "--no-default-keyring",
|
|
|
|
"--homedir", os.path.expanduser(
|
|
|
|
CONFIG.get('DEFAULT', 'gnupghome')),
|
2010-10-26 15:40:04 +02:00
|
|
|
"--no-expensive-trust-checks",
|
|
|
|
"--keyring", keyring, "--list-keys",
|
2014-01-11 01:03:09 +01:00
|
|
|
"--with-colons", "--fixed-list-mode", "--with-fingerprint",
|
|
|
|
"--with-fingerprint"],
|
2010-10-26 15:40:04 +02:00
|
|
|
stdout=subprocess.PIPE)
|
2009-01-21 14:39:29 +01:00
|
|
|
fpr = None
|
2010-10-26 15:40:04 +02:00
|
|
|
for line in proc.stdout.readlines():
|
2014-01-11 01:03:09 +01:00
|
|
|
fpr = process_gpg_list_keys_line(line, fpr)
|
2010-10-26 15:40:04 +02:00
|
|
|
retcode = proc.wait()
|
|
|
|
if retcode != 0:
|
|
|
|
logging.error("subprocess ended with return code %d", retcode)
|
2009-01-21 14:39:29 +01:00
|
|
|
db = anydbm.open(pkg_resources.resource_filename(__name__,
|
|
|
|
'keyringcache'), 'c')
|
|
|
|
for key in resultdict:
|
|
|
|
db[key] = ":".join(resultdict[key])
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2010-10-26 15:40:04 +02:00
|
|
|
logging.basicConfig(stream=sys.stderr, level=logging.WARNING)
|
2014-01-11 01:03:09 +01:00
|
|
|
CONFIG.readfp(pkg_resources.resource_stream(
|
|
|
|
__name__, 'ddportfolio.ini'))
|
|
|
|
gpghome = os.path.expanduser(CONFIG.get('DEFAULT', 'gnupghome'))
|
|
|
|
if not os.path.isdir(gpghome):
|
|
|
|
os.makedirs(gpghome, 0700)
|
2009-01-21 14:39:29 +01:00
|
|
|
process_keyrings()
|