1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import os, sys |
---|
4 | import MySQLdb |
---|
5 | import tempfile |
---|
6 | import stat |
---|
7 | from optparse import OptionParser |
---|
8 | |
---|
9 | from deter import fedid |
---|
10 | |
---|
11 | class opt_parser(OptionParser): |
---|
12 | def __init__(self): |
---|
13 | OptionParser.__init__(self, usage="%prog [opts] (--help for details)", |
---|
14 | version="0.1") |
---|
15 | self.add_option('-u', '--user', dest='users', action='append', |
---|
16 | default=[], help="Users to extract from DB") |
---|
17 | self.add_option('-p', '--project', dest='projects', action='append', |
---|
18 | default=[], help="Projects to extract from DB") |
---|
19 | self.add_option('-d', '--cert-dir', dest='cert_dir', |
---|
20 | default=None, help='Directory to store copies of certificates') |
---|
21 | |
---|
22 | |
---|
23 | def cert_to_fid(cstr): |
---|
24 | fd, path = tempfile.mkstemp('.pem') |
---|
25 | try: |
---|
26 | try: |
---|
27 | f = os.fdopen(fd, "w") |
---|
28 | print >>f, cstr |
---|
29 | f.close() |
---|
30 | except IOError, e: |
---|
31 | print >>sys.stderr, "Error creating user %s" % u |
---|
32 | return fedid(file=path) |
---|
33 | finally: |
---|
34 | os.remove(path) |
---|
35 | |
---|
36 | |
---|
37 | def add_list(l, field): |
---|
38 | str = "" |
---|
39 | for x in l: |
---|
40 | if str: str += " OR " |
---|
41 | else: str = " AND (" |
---|
42 | |
---|
43 | str += "%s='%s'" % (field, x) |
---|
44 | if str: str += ")" |
---|
45 | return str |
---|
46 | |
---|
47 | |
---|
48 | fids = { } |
---|
49 | q_start = """ |
---|
50 | SELECT |
---|
51 | g.uid, |
---|
52 | CASE g.gid |
---|
53 | WHEN g.pid THEN g.pid |
---|
54 | ELSE CONCAT(g.pid, '/', g.gid) |
---|
55 | END, |
---|
56 | CONCAT('-----BEGIN RSA PRIVATE KEY-----\\n', |
---|
57 | s.privkey, |
---|
58 | '-----END RSA PRIVATE KEY-----\\n', |
---|
59 | '-----BEGIN CERTIFICATE-----\\n', |
---|
60 | s.cert, |
---|
61 | '-----END CERTIFICATE-----\\n'), |
---|
62 | encrypted |
---|
63 | FROM group_membership g INNER JOIN user_sslcerts s |
---|
64 | ON g.uid = s.uid |
---|
65 | WHERE revoked is NULL AND encrypted = 0 |
---|
66 | """ |
---|
67 | q_end =""" |
---|
68 | ORDER BY s.uid |
---|
69 | """ |
---|
70 | |
---|
71 | opts, args = opt_parser().parse_args() |
---|
72 | |
---|
73 | if opts.cert_dir: |
---|
74 | if not os.path.isdir(opts.cert_dir): |
---|
75 | sys.exit('Not a directory: %s' % opts.cert_dir) |
---|
76 | elif not os.access(opts.cert_dir, os.W_OK): |
---|
77 | sys.exit('Cannot write %s' % opts.cert_dir) |
---|
78 | else: |
---|
79 | os.chmod(opts.cert_dir, stat.S_IRWXU) |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | user_clause= add_list(opts.users, 'g.uid') |
---|
84 | project_clause= add_list(opts.projects, 'g.pid') |
---|
85 | |
---|
86 | query = q_start + user_clause + project_clause + q_end |
---|
87 | |
---|
88 | db = MySQLdb.connect(db='tbdb') |
---|
89 | c = db.cursor() |
---|
90 | c.execute(query) |
---|
91 | |
---|
92 | for u, p, c, e in c.fetchall(): |
---|
93 | fid = fids.get(c, None) |
---|
94 | |
---|
95 | if not fid: |
---|
96 | fid = cert_to_fid(c) |
---|
97 | fids[c] = fid |
---|
98 | if opts.cert_dir: |
---|
99 | cf =os.path.join(opts.cert_dir, '%s.pem' % u) |
---|
100 | if not os.access(cf, os.F_OK) or os.access(cf, os.W_OK): |
---|
101 | try: |
---|
102 | f = open(cf, 'w') |
---|
103 | f.write(c) |
---|
104 | f.close() |
---|
105 | os.chmod(cf, stat.S_IREAD) |
---|
106 | except EnvironmentError, e: |
---|
107 | print >>sys.stderr, 'Cannot write %s: %s' % \ |
---|
108 | (e.filename, e.strerror) |
---|