| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | import os, sys |
|---|
| 4 | import MySQLdb |
|---|
| 5 | from federation.util import file_expanding_opts |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | class opt_parser(file_expanding_opts): |
|---|
| 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', |
|---|
| 15 | action='callback', callback=self.expand_file, type='str', |
|---|
| 16 | default=None, help="file containing pubkey to confirm") |
|---|
| 17 | self.add_option('-k', '--key', dest='key', default=None, |
|---|
| 18 | action='callback', callback=self.expand_file, type='str', |
|---|
| 19 | help='Key on the command line') |
|---|
| 20 | self.add_option('-q', '--quiet', dest='verbose', action='store_false', |
|---|
| 21 | default=True, help="No output") |
|---|
| 22 | self.add_option('-C', dest='dummy', |
|---|
| 23 | help="Compatibility option, no function") |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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) |
|---|