1 | #!/usr/local/bin/python |
---|
2 | |
---|
3 | import os, sys |
---|
4 | import MySQLdb |
---|
5 | from optparse import OptionParser |
---|
6 | |
---|
7 | |
---|
8 | class opt_parser(OptionParser): |
---|
9 | def __init__(self): |
---|
10 | OptionParser.__init__(self, usage="%prog [opts] (--help for details)", |
---|
11 | version="0.1") |
---|
12 | self.add_option('-u', '--user', dest='user', action='store', |
---|
13 | default=None, help="User to confirm key of") |
---|
14 | self.add_option('-f', '--keyfile', dest='keyfile', action='store', |
---|
15 | default=None, help="file containing pubkey to confirm") |
---|
16 | self.add_option('-k', '--key', dest='key', |
---|
17 | default=None, action='store', |
---|
18 | help='Key on the command line') |
---|
19 | self.add_option('-q', '--quiet', dest='verbose', action='store_false', |
---|
20 | default=True, help="No output") |
---|
21 | self.add_option('-C', dest=dummy, |
---|
22 | help="Compatibility option, no function") |
---|
23 | |
---|
24 | |
---|
25 | def read_keyfile(file): |
---|
26 | """ |
---|
27 | Return the contents of file as a single line |
---|
28 | """ |
---|
29 | |
---|
30 | f = open(file, "r") |
---|
31 | key = "" |
---|
32 | for line in f: |
---|
33 | line = line.strip() |
---|
34 | key += line |
---|
35 | f.close() |
---|
36 | return key |
---|
37 | |
---|
38 | |
---|
39 | query_template = """ |
---|
40 | SELECT |
---|
41 | uid |
---|
42 | FROM user_pubkeys |
---|
43 | WHERE uid = '%s' AND pubkey='%s' |
---|
44 | """ |
---|
45 | parser = opt_parser() |
---|
46 | opts, args = parser.parse_args() |
---|
47 | if len(args): |
---|
48 | parser.error("Unlabeled option. Did you forget to quote a key?") |
---|
49 | |
---|
50 | if opts.user: user = opts.user |
---|
51 | else: parser.error("--user is required") |
---|
52 | |
---|
53 | if opts.keyfile: |
---|
54 | try: |
---|
55 | key = read_keyfile(opts.keyfile) |
---|
56 | except IOError, e: |
---|
57 | sys.exit('Cannot read keyfile %s: %s' % (opts.keyfile, e)) |
---|
58 | elif opts.key: key = opts.key.strip() |
---|
59 | else: parser.error("--keyfile or --key is required") |
---|
60 | |
---|
61 | query = query_template % (user, key) |
---|
62 | |
---|
63 | db = MySQLdb.connect(db='tbdb') |
---|
64 | c = db.cursor() |
---|
65 | c.execute(query) |
---|
66 | |
---|
67 | if len(c.fetchall()) == 1: |
---|
68 | if opts.verbose: print "Confirmed" |
---|
69 | sys.exit(0) |
---|
70 | else: |
---|
71 | if opts.verbose: print "No such key for %s" % user |
---|
72 | sys.exit(1) |
---|