1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
#
#-*- coding:utf-8 -*-
"""
Gentoo-keys - seedhandler.py
Seed handling interface module
@copyright: 2012 by Brian Dolbec <dol-sen@gentoo.org>
@license: GNU GPL2, see COPYING for details.
"""
import os
import re
from json import load
from gkeys.config import GKEY
from gkeys.seed import Seeds
from gkeys.fileops import ensure_dirs
class SeedHandler(object):
def __init__(self, logger, config):
self.config = config
self.logger = logger
self.fingerprint_re = re.compile('[0-9A-Fa-f]{40}')
self.finerprint_re2 = re.compile('[0-9A-Fa-f]{4}( [0-9A-Fa-f]{4}){9}')
def new(self, args, checkgkey=False):
newgkey = self.build_gkeydict(args)
if checkgkey:
newgkey, is_good = self.check_gkey(newgkey)
if is_good:
newgkey = GKEY(**newgkey)
self.logger.debug("SeedHandler: new; new gkey: %s" % str(newgkey))
else:
return None
else:
newgkey = GKEY(**newgkey)
self.logger.debug("SeedHandler: new; NON-checked new gkey: %s" % str(newgkey))
return newgkey
@staticmethod
def build_gkeydict(args):
keyinfo = {}
for attr in GKEY._fields + ('keyid',):
try:
value = getattr(args, attr)
if attr == 'name' and value:
value = " ".join(value)
if value:
keyinfo[attr] = value
except AttributeError:
pass
return keyinfo
def load_seeds(self, seedfile=None, filepath=None):
'''Load seed file
@param seeds: string of the short name seed file
@param seedfile: string filepath of the file to load
@return Seeds class instance of the file loaded
'''
if not seedfile and not filepath:
self.logger.error("SeedHandler: load_seeds; no filename to load: "
"setting = %s. Please use the -S or -F option to indicate: which seed "
"file to use." % seedfile)
return False
if seedfile:
filepath = self.config.get_key('seeds', seedfile)
elif not filepath:
self.logger.error("SeedHandler: load_seeds; No filepath to load")
self.logger.debug("SeedHandler: load_seeds; seeds filepath to load: "
"%s" % filepath)
seeds = Seeds(config=self.config)
seeds.load(filepath)
return seeds
def load_category(self, category, nicks=None):
'''Loads the designated key directories
@param category: string
@param nicks: list of string nick ids to load
@return Seeds class object
'''
seeds = Seeds(config=self.config)
if category:
catdir = self.config.get_key(category + "-category")
else:
self.logger.debug("SeedHandler: load_category; Error invalid category: %s." % (str(category)))
return seeds
self.logger.debug("SeedHandler: load_category; catdir = %s" % catdir)
try:
if not nicks:
nicks = os.listdir(catdir)
for nick in nicks:
seed_path = os.path.join(catdir, nick)
gkey_path = os.path.join(seed_path, 'gkey.seeds')
seed = None
try:
with open(gkey_path, 'r') as fileseed:
seed = load(fileseed)
except IOError as error:
self.logger.debug("SeedHandler: load_category; IOError loading seed file %s." % gkey_path)
self.logger.debug("Error was: %s" % str(error))
if seed:
for nick in sorted(seed):
key = seed[nick]
seeds.add(nick, GKEY(**key))
except OSError as error:
self.logger.debug("SeedHandler: load_category; OSError for %s" % catdir)
self.logger.debug("Error was: %s" % str(error))
return seeds
def fetch_seeds(self, seeds, args, verified_dl=None):
'''Fetch new seed files
@param seeds: list of seed nicks to download
@param verified_dl: Function pointer to the Actions.verify()
instance needed to do the download and verification
'''
http_check = re.compile(r'^(http|https)://')
urls = []
messages = []
try:
for seed in [seeds]:
seedurl = self.config.get_key('seedurls', seed)
seedpath = self.config.get_key('seeds', seed)
if http_check.match(seedurl):
urls.extend([(seedurl, seedpath)])
else:
self.logger.info("Wrong seed file URLs... Switching to default URLs.")
urls.extend([(self.config['seedurls'][seed], seedpath)])
except KeyError:
pass
succeeded = []
seedsdir = self.config.get_key('seedsdir')
mode = int(self.config.get_key('permissions', 'directories'),0)
ensure_dirs(seedsdir, mode=mode)
for (url, filepath) in urls:
args.category = 'rel'
args.filename = url
args.signature = None
args.timestamp = True
args.destination = filepath
verified, messages_ = verified_dl(args)
succeeded.append(verified)
messages.append(messages_)
return (succeeded, messages)
def check_gkey(self, args):
# assume it's good until an error is found
is_good = True
try:
args['keydir'] = args.get('keydir', args['nick'])
fprs = []
if args['fingerprint']:
for fpr in args['fingerprint']:
is_good, fingerprint = self._check_fingerprint_integrity(fpr)
if is_good:
fprs.append(fingerprint)
else:
self.logger.error('Bad fingerprint from command line args: %s' % fpr)
if is_good:
args['fingerprint'] = fprs
except KeyError:
self.logger.error('GPG fingerprint not found.')
is_good = False
if not is_good:
self.logger.error('A valid fingerprint '
'was not found for %s' % args['name'])
return args, is_good
def _check_fingerprint_integrity(self, fpr):
# assume it's good unti an error is found
is_good = True
fingerprint = fpr.replace(" ", "")
# check fingerprint integrity
if len(fingerprint) != 40:
self.logger.error(' GPGKey incorrect fingerprint ' +
'length (%s) for fingerprint: %s' % (len(fingerprint), fingerprint))
is_good = False
if not self.fingerprint_re.match(fingerprint):
self.logger.error(' GPGKey: Non hexadecimal digits in ' + 'fingerprint for fingerprint: ' + fingerprint)
is_good = False
return is_good, fingerprint
|