[2e46f35] | 1 | #!/usr/bin/env python |
---|
[457c4ac] | 2 | |
---|
| 3 | import os, sys |
---|
| 4 | import MySQLdb |
---|
[62f3dd9] | 5 | from federation.util import file_expanding_opts |
---|
[457c4ac] | 6 | |
---|
| 7 | |
---|
[62f3dd9] | 8 | class opt_parser(file_expanding_opts): |
---|
[457c4ac] | 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") |
---|
[62f3dd9] | 14 | self.add_option('-f', '--keyfile', dest='keyfile', |
---|
| 15 | action='callback', callback=self.expand_file, type='str', |
---|
[457c4ac] | 16 | default=None, help="file containing pubkey to confirm") |
---|
[62f3dd9] | 17 | self.add_option('-k', '--key', dest='key', default=None, |
---|
| 18 | action='callback', callback=self.expand_file, type='str', |
---|
[457c4ac] | 19 | help='Key on the command line') |
---|
| 20 | self.add_option('-q', '--quiet', dest='verbose', action='store_false', |
---|
| 21 | default=True, help="No output") |
---|
[ff2d928] | 22 | self.add_option('-C', dest='dummy', |
---|
[3584a9f] | 23 | help="Compatibility option, no function") |
---|
[457c4ac] | 24 | |
---|
| 25 | |
---|
| 26 | def read_keyfile(file): |
---|
| 27 | """ |
---|
| 28 | Return the contents of file as a single line |
---|
| 29 | """ |
---|
| 30 | |
---|
| 31 | f = open(file, "r") |
---|
| 32 | key = "" |
---|
| 33 | for line in f: |
---|
| 34 | line = line.strip() |
---|
| 35 | key += line |
---|
| 36 | f.close() |
---|
| 37 | return key |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | query_template = """ |
---|
| 41 | SELECT |
---|
| 42 | uid |
---|
| 43 | FROM user_pubkeys |
---|
| 44 | WHERE uid = '%s' AND pubkey='%s' |
---|
| 45 | """ |
---|
| 46 | parser = opt_parser() |
---|
| 47 | opts, args = parser.parse_args() |
---|
| 48 | if len(args): |
---|
| 49 | parser.error("Unlabeled option. Did you forget to quote a key?") |
---|
| 50 | |
---|
| 51 | if opts.user: user = opts.user |
---|
| 52 | else: parser.error("--user is required") |
---|
| 53 | |
---|
| 54 | if opts.keyfile: |
---|
| 55 | try: |
---|
| 56 | key = read_keyfile(opts.keyfile) |
---|
| 57 | except IOError, e: |
---|
| 58 | sys.exit('Cannot read keyfile %s: %s' % (opts.keyfile, e)) |
---|
| 59 | elif opts.key: key = opts.key.strip() |
---|
| 60 | else: parser.error("--keyfile or --key is required") |
---|
| 61 | |
---|
| 62 | query = query_template % (user, key) |
---|
| 63 | |
---|
[ff2d928] | 64 | try: |
---|
| 65 | db = MySQLdb.connect(db='tbdb') |
---|
| 66 | except Exception, e: |
---|
| 67 | if len(e.args) == 2: |
---|
| 68 | num, str = e.args |
---|
| 69 | else: |
---|
| 70 | str = unicode(e) |
---|
| 71 | num = -1 |
---|
| 72 | sys.exit("Cannot connect: %s (%d)" % (str, num)) |
---|
| 73 | |
---|
[457c4ac] | 74 | c = db.cursor() |
---|
| 75 | c.execute(query) |
---|
| 76 | |
---|
| 77 | if len(c.fetchall()) == 1: |
---|
| 78 | if opts.verbose: print "Confirmed" |
---|
| 79 | sys.exit(0) |
---|
| 80 | else: |
---|
| 81 | if opts.verbose: print "No such key for %s" % user |
---|
| 82 | sys.exit(1) |
---|