diff options
| author | Guillaume Horel <guillaume.horel@gmail.com> | 2011-05-20 01:28:09 -0400 |
|---|---|---|
| committer | Guillaume Horel <guillaume.horel@gmail.com> | 2011-05-20 01:28:09 -0400 |
| commit | ff79cfce0fed38375b8ff459c66648ec014bf4c6 (patch) | |
| tree | 869d0ddc747986d0cb47ce8300ad59d379071b08 | |
| parent | 3565d0c362646d681cbac605924fcf7984b05a92 (diff) | |
| download | alias-ff79cfce0fed38375b8ff459c66648ec014bf4c6.tar.gz | |
Rewrote the objects storage engine
objects are stored under each user's directory to make zaran happy.
| -rw-r--r-- | crypto/test.js | 2 | ||||
| -rw-r--r-- | server/object.py | 61 | ||||
| -rw-r--r-- | server/user.py | 29 |
3 files changed, 48 insertions, 44 deletions
diff --git a/crypto/test.js b/crypto/test.js index 6924648..91c0dc4 100644 --- a/crypto/test.js +++ b/crypto/test.js @@ -17,5 +17,7 @@ key.readPrivateKeyFromPEMString(key_string) cypher = key.encrypt("Guillaume est plus fort que Thibaut") test = key.decrypt(cypher) +key2 = new RSAKey() +key2.generate(2048,"10001") diff --git a/server/object.py b/server/object.py index d3bdf87..4c1e0c1 100644 --- a/server/object.py +++ b/server/object.py @@ -15,16 +15,20 @@ class ObjectError(Exception): pass; class Object: - def __init__(self, hash): - self.hash = hash - self.object_path = os.path.join(config.root, hash[:2], hash[2:]) + def __init__(self, name, owner, split_name = True): + self.hash = name + self.owner = owner + if split_name: + self.object_path = os.path.join(config.root, owner, name[:2], name[2:]) + else: + self.object_path = os.path.join(config.root, owner, name) def exists(self): return os.path.exists(self.object_path) class ObjectReader(Object): - def __init__(self, hash): - Object.__init__(self, hash) + def __init__(self, hash, owner, split_name = True): + Object.__init__(self, hash, owner, split_name) if not self.exists(): logger.error("Object {} can't be found for user {}".format(self.hash, self.owner)) @@ -66,7 +70,7 @@ class ObjectReader(Object): for line in file: name = line.rstrip('\n') try: - child = ObjectReader(name) + child = ObjectReader(name, self.owner) except ObjectError: logger.error('Object {} doesn\'t exist'.format(name)) else: @@ -85,24 +89,28 @@ class ObjectReader(Object): .format(user, self.hash)) raise PermissionError with open(os.path.join(self.object_path, 'object'), 'r') as file: - content = {} - for line in file: - k, v = line.split() - content[k] = v + content = file.read() return content, key class ObjectWriter(ObjectReader): - def __init__(self, hash): - Object.__init__(self, hash) + def __init__(self, hash, owner, split_name = True, key = None): + Object.__init__(self, hash, owner, split_name) + self.files = ('permissions', 'children', 'object') + self.__create_skeleton(key) + + def __create_skeleton(self, key): #new object if not self.exists(): os.makedirs(self.object_path) - for filename in ['permissions', 'children', 'object']: - file=open(os.path.join(self.object_path, filename), "w") + for filename in self.files: + file = open(os.path.join(self.object_path, filename), "w") file.close() - + #give all the permissions to the owner + ALLPERM = READ + MODIFY + APPEND + LIST + self.add_user(self.owner, ALLPERM, key) + def write(self, user, content): perm = self.get_permission(user) if not perm & MODIFY: @@ -110,11 +118,10 @@ class ObjectWriter(ObjectReader): .format(user, self.hash)) raise PermissionError with open(os.path.join(self.object_path, 'object'), "w") as file: - for k, v in content: - file.write('{} {}'.format(k,v)) + file.write('{}'.format(content)) def append(self, user, content, parent): - parent_object = ObjectReader(parent) + parent_object = ObjectReader(parent, self.owner) perm = parent_object.get_permission(user) if not perm & APPEND: logger.error("User {} doesn't have the modify permission for object {}" @@ -125,22 +132,20 @@ class ObjectWriter(ObjectReader): file.write('{} {}\n'.format(k,v)) #add the child hash to the parent with open(os.path.join(parent_object.object_path, 'children'), "a") as file: - file.write('{}\n'.format(self.hash)) + file.write('{} {}\n'.format(self.hash)) - def create_root_object(self, user, content): - with open(os.path.join(self.object_path, 'object'), "w") as file: - for k, v in content.iteritems(): - file.write('{} {}\n'.format(k,v)) - #give all the permissions to the user - ALLPERM = READ + MODIFY + APPEND + LIST + def add_user(self, user, perm, key = None): with open(os.path.join(self.object_path, 'permissions'), "a") as file: - file.write('{} {} xxxxxx\n'.format(user,ALLPERM)) - + if key: + file.write('{} {} {}\n'.format(user, perm, key)) + else: + file.write('{} {} None\n'.format(user, perm, key)) + if __name__ == '__main__': jid = 'thrasibule@alias.im' hash = hashlib.sha1(jid).hexdigest() config.root = '/var/lib/alias' - print ObjectReader(hash).get_content(jid) + print ObjectReader(hash, jid).get_content(jid) diff --git a/server/user.py b/server/user.py index 1cb2d63..7f01149 100644 --- a/server/user.py +++ b/server/user.py @@ -11,22 +11,26 @@ class User: def __init__(self, jid): self.jid = jid - #self.hash = hashlib.sha1(jid).hexdigest() - self.hash = base64.b64encode(jid) + self.hash = hashlib.sha1(jid).hexdigest() def register(self, registration): - ObjectWriter(self.hash).create_root_object(self.jid, registration) - + ObjectWriter('pubkey', self.jid, split_name = False).write(self.jid, registration['pubkey']) + #everybody can read the pubkey + ObjectWriter('pubkey', self.jid, split_name = False).add_user('*', READ) + ObjectWriter('privkey', self.jid, split_name = False).write(self.jid, registration['privkey']) + ObjectWriter(self.hash, self.jid) + def get_registration(self): - registration, key = ObjectReader(self.hash).get_content(self.jid) + registration = {} + registration['pubkey'], ignore = ObjectReader('pubkey',self.jid, split_name = False).get_content(self.jid) + registration['privkey'], ignore = ObjectReader('privkey',self.jid, split_name = False).get_content(self.jid) return registration def is_registered(self): - return Object(self.hash).exists() + return Object(self.hash, self.jid).exists() def unregister(self, jid): - ObjectWriter(self.jid, self.hash).delete() - shutil.rmtree(self.root + '/' + jid) + pass class UserHandler: @@ -34,14 +38,7 @@ class UserHandler: self.root = root def get_user_list(self): - result = [] - for stem in os.listdir(self.root): - for leaf in os.listdir(os.path.join(self.root, stem)): - try: - result.append(base64.b64decode(stem + leaf)) - except TypeError: - logger.error("User tree corrupted") - return result + return os.listdir(self.root) if __name__ == '__main__': print UserHandler('/var/lib/alias').get_user_list()
\ No newline at end of file |
