source: fedd/confirm_sshkey.py @ b745876

compt_changes
Last change on this file since b745876 was 2e46f35, checked in by mikeryan <mikeryan@…>, 14 years ago

switch to /usr/bin/env python to run python

  • Property mode set to 100755
File size: 2.0 KB
Line 
1#!/usr/bin/env python
2
3import os, sys
4import MySQLdb
5from federation.util import file_expanding_opts
6
7
8class 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
26def 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
40query_template = """
41SELECT
42    uid
43FROM user_pubkeys
44WHERE uid = '%s' AND pubkey='%s'
45"""
46parser = opt_parser()
47opts, args = parser.parse_args()
48if len(args):
49    parser.error("Unlabeled option.  Did you forget to quote a key?")
50
51if opts.user: user = opts.user
52else: parser.error("--user is required")
53
54if 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))
59elif opts.key: key = opts.key.strip()
60else: parser.error("--keyfile or --key is required")
61
62query = query_template % (user, key)
63
64try:
65    db = MySQLdb.connect(db='tbdb')
66except 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
74c = db.cursor()
75c.execute(query)
76
77if len(c.fetchall()) == 1:
78    if opts.verbose: print "Confirmed"
79    sys.exit(0)
80else:
81    if opts.verbose: print "No such key for %s" % user
82    sys.exit(1)
Note: See TracBrowser for help on using the repository browser.