1 | #!/usr/local/bin/python |
---|
2 | |
---|
3 | # Encode the set of fixed keys as a colon-separated user/key pairs. This also |
---|
4 | # includes a command line utility to manipulate the DB. |
---|
5 | |
---|
6 | def read_db(file): |
---|
7 | """ |
---|
8 | Read the set of fixed keys fom the file |
---|
9 | """ |
---|
10 | keys = set() |
---|
11 | |
---|
12 | f = open(file, "r") |
---|
13 | for line in f: |
---|
14 | u, k = line.rstrip().split(':', 2) |
---|
15 | keys.add((u, k)) |
---|
16 | f.close() |
---|
17 | return keys |
---|
18 | |
---|
19 | def write_db(file, keys): |
---|
20 | """ |
---|
21 | Write the set of keys to the given file |
---|
22 | """ |
---|
23 | |
---|
24 | f = open(file, 'w') |
---|
25 | for t in keys: |
---|
26 | print >>f, "%s:%s" % t |
---|
27 | f.close() |
---|
28 | |
---|
29 | # This is the command line utility |
---|
30 | if __name__ =='__main__': |
---|
31 | import sys |
---|
32 | import MySQLdb |
---|
33 | from optparse import OptionParser |
---|
34 | |
---|
35 | actions = set(("init", "add", "delete")) |
---|
36 | |
---|
37 | def read_key_file(file): |
---|
38 | """ |
---|
39 | Read a single line from a keyfile and trim the whitespace |
---|
40 | """ |
---|
41 | f = open(file, 'r') |
---|
42 | key = f.readline().rstrip() |
---|
43 | f.close() |
---|
44 | return key |
---|
45 | |
---|
46 | class key_opts(OptionParser): |
---|
47 | def __init__(self): |
---|
48 | OptionParser.__init__(self, usage="%prog (init|add|delete) " + \ |
---|
49 | "[opts] (--help for details)", version="0.1") |
---|
50 | self.add_option('-A', '--all-users', dest='all', default=False, |
---|
51 | action='store_true', help='add all users to the database') |
---|
52 | self.add_option('-u','--user', dest='user', default=None, |
---|
53 | action='store', help='user to add/delete') |
---|
54 | self.add_option('-k','--key', dest='key', default=None, |
---|
55 | action='store', help='key to add/delete (string)') |
---|
56 | self.add_option('-K','--keyfile', dest='keyfile', default=None, |
---|
57 | action='store', help='key to add/delete (file)') |
---|
58 | self.add_option('-d', '--database', dest='file', default=None, |
---|
59 | action='store', help='database file') |
---|
60 | self.add_option('-f', '--file', dest='file', action='store', |
---|
61 | help='database file (synonym for --database)') |
---|
62 | |
---|
63 | |
---|
64 | parser = key_opts() |
---|
65 | action = sys.argv[1] |
---|
66 | keys = set() |
---|
67 | |
---|
68 | # Check the action |
---|
69 | if action in actions: del sys.argv[1] |
---|
70 | else: sys.exit("Bad action, must be one of %s" % ", ".join(actions)) |
---|
71 | |
---|
72 | # Parse the options |
---|
73 | (opts, args) = parser.parse_args() |
---|
74 | if not opts.file: |
---|
75 | sys.exit("Must specify DB file") |
---|
76 | |
---|
77 | # If we're initializing, clear the DB unless we're adding all from the |
---|
78 | # Emulab DB |
---|
79 | if action == 'init': |
---|
80 | if not opts.all: |
---|
81 | write_db(opts.file, keys) |
---|
82 | sys.exit(0) |
---|
83 | else: |
---|
84 | keys = read_db(opts.file) |
---|
85 | |
---|
86 | if opts.all: |
---|
87 | if action == 'delete': |
---|
88 | sys.exit("delete and --all don't mix") |
---|
89 | # Add all users from the Emulab DB |
---|
90 | try: |
---|
91 | db = MySQLdb.connect(db="tbdb") |
---|
92 | except: |
---|
93 | sys.exit("Cannot access the Emulab database") |
---|
94 | |
---|
95 | c = db.cursor() |
---|
96 | c.execute("SELECT uid, pubkey from user_pubkeys") |
---|
97 | for u, k in c.fetchall(): |
---|
98 | keys.add((u, k)) |
---|
99 | c.close() |
---|
100 | db.close() |
---|
101 | write_db(opts.file, keys) |
---|
102 | else: |
---|
103 | # Add/delete a single user |
---|
104 | if opts.user: user = opts.user |
---|
105 | else: sys.exit("Must provide user (--user)") |
---|
106 | |
---|
107 | if opts.key: |
---|
108 | key = opts.key |
---|
109 | elif opts.keyfile: |
---|
110 | try: |
---|
111 | key = read_key_file(opts.keyfile) |
---|
112 | except IOError, e: |
---|
113 | sys.exit("Error reading keyfile: %s" % e) |
---|
114 | else: |
---|
115 | sys.exit("Must provide key (--key or --keyfile)") |
---|
116 | |
---|
117 | if action == 'add': |
---|
118 | keys.add((user, key)) |
---|
119 | write_db(opts.file, keys) |
---|
120 | elif action == 'delete': |
---|
121 | if (user, key) in keys: |
---|
122 | keys.remove((user, key)) |
---|
123 | write_db(opts.file, keys) |
---|
124 | else: |
---|
125 | print >>sys.stderr, "Cannot delete (%s, %s) not in db" % \ |
---|
126 | (user, key) |
---|
127 | else: |
---|
128 | sys.exit("How did we get here??") |
---|
129 | sys.exit(0) |
---|