source: fedd/confirm_sshkey.py @ b4e5366

axis_examplecompt_changesinfo-ops
Last change on this file since b4e5366 was 62f3dd9, checked in by Ted Faber <faber@…>, 14 years ago

allow command line progams to expand tildes. Added a class derived from OptionParser? to make that easily available.

  • Property mode set to 100755
File size: 2.0 KB
RevLine 
[457c4ac]1#!/usr/local/bin/python
2
3import os, sys
4import MySQLdb
[62f3dd9]5from federation.util import file_expanding_opts
[457c4ac]6
7
[62f3dd9]8class 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
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
[ff2d928]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
[457c4ac]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.