summaryrefslogtreecommitdiff
path: root/dbm.py
diff options
context:
space:
mode:
Diffstat (limited to 'dbm.py')
-rwxr-xr-xdbm.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/dbm.py b/dbm.py
new file mode 100755
index 0000000..36b6233
--- /dev/null
+++ b/dbm.py
@@ -0,0 +1,60 @@
1#!/usr/bin/env python
2"""
3Utilities to manage a hash db of users for apache
4"""
5import anydbm
6import htpasswd
7__all__ = ["add_user", "delete_user", "update_user",
8 "get_user", "list_users", "raw_add"]
9
10
11def get_user(user, db):
12 """ Get user info from the DB """
13 db_dict = anydbm.open(db, 'r')
14 ret_user = "%s:%s" %(user, db_dict[user])
15 db_dict.close()
16 return ret_user
17
18
19def list_users(db):
20 """ List all the users in the DB """
21 db_dict = anydbm.open(db, 'r')
22 ret_str = '\n'.join(["%s:%s" %(k, v) for k, v in db_dict.iteritems()])
23 db_dict.close()
24 return ret_str
25
26
27def add_user(user, passwd, db):
28 """ Add user to the DB, creating if need be """
29 db_dict = anydbm.open(db, 'c')
30 db_dict[user] = htpasswd.hash_password(passwd)
31 db_dict.close()
32 return True
33
34
35def raw_add(line, db):
36 """ Add raw line to the DB """
37 db_dict = anydbm.open(db, 'w')
38 user, passwd = line.split(':')
39 db_dict[user] = passwd
40 db_dict.close()
41 return True
42
43
44def delete_user(user, db):
45 """ Remove user from the DB """
46 db_dict = anydbm.open(db, 'w')
47 del db_dict[user]
48 db_dict.close()
49 return True
50
51
52def update_user(user, passwd, db):
53 """ Change the users pass in the DB """
54 db_dict = anydbm.open(db, 'w')
55 if user in db_dict:
56 db_dict[user] = htpasswd.hash_password(passwd)
57 db_dict.close()
58 return True
59 else:
60 return False