1 | import os |
---|
2 | import sys |
---|
3 | |
---|
4 | from trac.env import Environment |
---|
5 | from acct_mgr.api import AccountManager |
---|
6 | from acct_mgr.htfile import HtPasswdStore, HtDigestStore |
---|
7 | from acct_mgr.pwhash import HtPasswdHashMethod, HtDigestHashMethod |
---|
8 | |
---|
9 | env = Environment(sys.argv[1]) |
---|
10 | |
---|
11 | store = AccountManager(env).password_store |
---|
12 | if isinstance(store, HtPasswdStore): |
---|
13 | env.config.set('account-manager', 'hash_method', 'HtPasswdHashMethod') |
---|
14 | prefix = '' |
---|
15 | elif isinstance(store, HtDigestStore): |
---|
16 | env.config.set('account-manager', 'hash_method', 'HtDigestHashMethod') |
---|
17 | prefix = store.realm + ':' |
---|
18 | else: |
---|
19 | print >>sys.stderr, 'Unsupported password store:', store.__class__.__name__ |
---|
20 | sys.exit(1) |
---|
21 | |
---|
22 | password_file = os.path.join(env.path, env.config.get('account-manager', |
---|
23 | 'password_file')) |
---|
24 | hashes = [line.strip().split(':', 1) for line in open(password_file)] |
---|
25 | hashes = [(u,p) for u,p in hashes if p.startswith(prefix)] |
---|
26 | if hashes: |
---|
27 | db = env.get_db_cnx() |
---|
28 | cursor = db.cursor() |
---|
29 | cursor.executemany("INSERT INTO session_attribute " |
---|
30 | "(sid,authenticated,name,value) " |
---|
31 | "VALUES (%s,1,'password',%s)", |
---|
32 | hashes) |
---|
33 | db.commit() |
---|
34 | |
---|
35 | env.config.set('account-manager', 'password_store', 'SessionStore') |
---|
36 | env.config.save() |
---|