lib: Fixed mblock bug for epoll.
[ashd.git] / etc / extauth / mkhtpasswd
CommitLineData
ea4e6725
FT
1#!/usr/bin/python
2
3import sys, os, termios, hmac, hashlib, getopt, getpass
4
5def usage(out):
6 out.write("usage: mkhtpasswd [-h] FILE USERNAME\n")
7
8opts, args = getopt.getopt(sys.argv[1:], "h")
9for o, a in opts:
10 if o == "-h":
11 usage(sys.stdout)
12 sys.exit(0)
13if len(args) < 2:
14 usage(sys.stderr)
15 sys.exit(1)
16
17def hashpw(usr, pw):
18 dig = hmac.new(pw, digestmod=hashlib.sha1)
19 dig.update(usr)
20 return dig.hexdigest()
21
22if ':' in args[1]:
23 sys.stderr.write("mkhtpasswd: username cannot contain `:'\n")
24 sys.exit(1)
25
26passwds = {}
27if os.path.exists(args[0]):
28 with open(args[0]) as fp:
29 for line in fp:
30 usr, pw = line.strip().split(':')
31 passwds[usr] = pw
32
33passwds[args[1]] = hashpw(args[1], getpass.getpass())
34
35with open(args[0], "w") as fp:
36 for usr, pw in passwds.iteritems():
37 fp.write("%s:%s\n" % (usr, pw))