-
Notifications
You must be signed in to change notification settings - Fork 13
/
usertool.py
executable file
·64 lines (48 loc) · 1.78 KB
/
usertool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
"""
A utility for dealing with BerkeleyDB user/pass files for Glider DAC.
See glider_dac.bdb.UserDB for more information.
Usage:
./usertool.py mydb.db init
./usertool.py mydb.db set testuser
Password:
./usertool.py mydb.db list
testuser
./usertool.py mydb.db check testuser
Password:
Success
"""
import os.path
import sys
import argparse
import getpass
from glider_util.bdb import UserDB
class UserAction(argparse.Action):
def __call__(self, parser, args, values, option = None):
if args.op in ['set', 'check'] and not values:
parser.error("You must specify a user for the '%s' operation" % args.op)
args.user=values
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('db_file', help='Path to the BDB user file')
parser.add_argument('op', choices=['set', 'list', 'check', 'init'], help='"set" a user\'s password, "check" a user\'s password, "list" all known users')
parser.add_argument('user', nargs='?', action=UserAction, help='Username')
args = parser.parse_args()
if not os.path.exists(args.db_file) and not args.op == "init":
raise Exception("File %s does not exist. Use %s %s 'init' if you want to create it" % (args.db_file, sys.argv[0], args.db_file))
u = UserDB(args.db_file)
if args.op == 'set':
pw = getpass.getpass()
u.set(args.user.encode(), pw.encode())
elif args.op == 'check':
pw = getpass.getpass()
cv = u.check(args.user.encode(), pw.encode())
if cv:
print("Success")
else:
print("Failure")
sys.exit(1)
elif args.op == 'list':
print("\n".join(u.list_users()))
elif args.op == 'init':
u.init_db(args.db_file)