]>
Commit | Line | Data |
---|---|---|
4cb3de1c MT |
1 | #!/usr/bin/python |
2 | ||
3 | import hashlib | |
bb929f43 | 4 | import ldap |
4cb3de1c MT |
5 | import sqlite3 |
6 | import os.path | |
7 | ||
8 | ||
9 | class HashDatabase(object): | |
10 | def __init__(self): | |
11 | self.conn = sqlite3.connect("/srv/www/ipfire.org/source/hashes.db") | |
8c70b84d MT |
12 | self.conn.isolation_level = None # autocommit mode |
13 | ||
14 | self.prepare() | |
4cb3de1c MT |
15 | |
16 | def __del__(self): | |
17 | self.conn.close() | |
18 | ||
19 | def prepare(self): | |
20 | c = self.conn.cursor() | |
21 | c.execute("CREATE TABLE IF NOT EXISTS hashes(file, sha1)") | |
22 | c.close() | |
23 | ||
24 | def _save_hash(self, path, hash): | |
25 | c = self.conn.cursor() | |
26 | c.execute("INSERT INTO hashes VALUES('%s', '%s')" % (os.path.basename(path), hash)) | |
27 | c.close() | |
28 | ||
29 | def get_hash(self, path): | |
30 | c = self.conn.cursor() | |
31 | c.execute("SELECT sha1 FROM hashes WHERE file = '%s'" % os.path.basename(path)) | |
32 | ||
33 | hash = c.fetchone() | |
34 | c.close() | |
35 | ||
36 | if not hash: | |
37 | hash = self._calc_hash(path) | |
38 | self._save_hash(path, hash) | |
39 | ||
40 | if hash: | |
41 | return "%s" % hash | |
42 | ||
43 | def _calc_hash(self, path): | |
44 | if not os.path.exists(path): | |
45 | return | |
46 | ||
47 | m = hashlib.sha1() | |
48 | f = open(path) | |
49 | m.update(f.read()) | |
50 | f.close() | |
51 | ||
52 | return m.hexdigest() | |
bb929f43 MT |
53 | |
54 | ||
55 | class UserDatabase(object): | |
56 | HOST = "ldap://ldap.ipfire.org" | |
57 | BASEDN = "ou=People,dc=mcfly,dc=local" | |
58 | ||
59 | def __init__(self): | |
60 | self.conn = ldap.initialize(self.HOST) | |
61 | self.conn.simple_bind() | |
62 | ||
63 | def __del__(self): | |
64 | self.conn.unbind() | |
65 | ||
66 | def _find_dn_by_name(self, name): | |
67 | results = self._search(filterstr="(uid=%s)" % name) | |
68 | assert len(results) == 1 | |
69 | return results[0][0] | |
70 | ||
71 | def _search(self, filterstr="(objectClass=*)", attrlist=None): | |
72 | return self.conn.search_st(self.BASEDN, ldap.SCOPE_SUBTREE, | |
73 | filterstr=filterstr, attrlist=attrlist) | |
74 | ||
75 | def check_password(self, name, password): | |
76 | dn = self._find_dn_by_name(name) | |
77 | conn = ldap.initialize(self.HOST) | |
78 | try: | |
79 | conn.simple_bind_s(dn, password) | |
80 | return True | |
81 | except ldap.INVALID_CREDENTIALS: | |
82 | return False | |
83 | finally: | |
84 | conn.unbind_s() | |
85 | ||
86 | def get_user_by_id(self, id): | |
87 | results = self._search(filterstr="(uidNumber=%s)" % id) | |
88 | assert len(results) == 1 | |
89 | return User(results[0][1]) | |
90 | ||
91 | def get_user_by_name(self, name): | |
92 | results = self._search(filterstr="(uid=%s)" % name) | |
93 | assert len(results) == 1 | |
94 | return User(results[0][1]) | |
95 | ||
96 | @property | |
97 | def users(self): | |
98 | ret = [] | |
99 | ||
100 | for dn, attr in self._search(): | |
101 | if dn == self.BASEDN or not attr: | |
102 | continue | |
103 | ret.append(User(attr)) | |
104 | ||
105 | return sorted(ret) | |
106 | ||
107 | ||
108 | class User(object): | |
109 | def __init__(self, obj): | |
110 | self.obj = obj | |
111 | ||
112 | def __cmp__(self, other): | |
113 | return cmp(self.realname, other.realname) | |
114 | ||
115 | def __repr__(self): | |
116 | return "<%s '%s'>" % (self.__class__.__name__, self.name) | |
117 | ||
118 | @property | |
119 | def name(self): | |
120 | return self.obj["uid"][0] | |
121 | ||
122 | @property | |
123 | def id(self): | |
124 | return int(self.obj["uidNumber"][0]) | |
125 | ||
126 | @property | |
127 | def mail(self): | |
128 | #return self.obj["mail"] | |
129 | return "%s@ipfire.org" % self.name | |
130 | ||
131 | @property | |
132 | def realname(self): | |
133 | return self.obj["cn"][0] |