Example #1
0
def rn_handler(arg):
    #assume root exists
    with open("private/root_dir.cap", 'r') as f:
        line = f.read()
        if line and line != "\n":
            line = line[line.find("|")+1:].strip("\n")
            root_cap = line.split(":")
            root_cap[2] = root_cap[2]
            cipher = get_data(line)
            (data, root_private, root_public)= crypto.unpackage_data(root_cap, cipher) 
            data = json.loads(data) 
            print "DATA BEFORE: " + str(data)
            data[arg.newfilename] = data[arg.filename]
            del data[arg.filename]
            print "DATA AFTER: " + str(data)
            #print "APPENDED DATA: ", data
            # post new dir 1st 
            data = json.dumps(data)
            new_data = crypto.package_data(data, root_cap, root_private, root_public)
            print_capabilities(root_cap)
            post_data(new_data, capToString(root_cap))
            print "ROOT UPDATED"
            return
        else:
            print "rn failed"
            return
Example #2
0
def put_handler(arg):
    if arg.writecap:
        # putting with a cap requires
        # 1) Getting the encrypted private key
        # 2) Decrypting it, and using it to sign the updated data
        # 3) Signing the encryption of the updated data with the private key
        cap = arg.writecap.split(":")
        cipher = get_data(arg.writecap) 
        
        (data, private, public) = crypto.unpackage_data(cap, cipher)
    
    else:
        with open("private/root_dir.cap", 'r') as f:
            line = f.read()
            if line and line != "\n":
                line = line[line.find("|")+1:].strip("\n")
                root_cap = line.split(":")
                cipher = get_data(line)
                (data, root_private, root_public)= crypto.unpackage_data(root_cap, cipher) 
                data = json.loads(data)
 
        # here cap is (my_hash(private_key), my_hash(public_key))
        (private, public, cap) = crypto.generate_RSA()
        cap = [FILE_WRITE_CAP, cap[0], cap[1]]
        # put name in dir
        data[arg.name] = capToString(cap)         
        # update root dir data 
        data = json.dumps(data) 
        data = crypto.package_data(data, root_cap, root_private, root_public)
        print_capabilities(root_cap)
        post_data(data, capToString(root_cap))
        # save the cap in a private file 
        with open('private/files.txt', "a") as f:
            c = arg.name+ "|" + capToString(cap)+ "\n"
            f.write(c)
        # TODO get rid of key storage by making get to the server via URI after createion
        # save the private key in a file 
        with open('private/keys/'+arg.name+"public", "w") as f:
            f.write(public)
        with open('private/keys/'+arg.name+"private", "w") as f:
            f.write(private)
    data = arg.data
    data = crypto.package_data(data, cap, private, public)
    print_capabilities(cap)
    h = crypto.my_hash(cap[1])[:16]
    cap[1] = crypto.my_hash(h)
    post_data(data, capToString(cap))
Example #3
0
def ls_handler(arg):
    # ls path by getting the file content
    if arg.path:
        return      
    else:
        # ls root 
        with open("private/root_dir.cap", 'r') as f:
            line = f.read()
            if line and line != "\n":
                line = line[line.find("|")+1:].strip("\n")
                cap = line.split(":")
                cipher = get_data(line)
                (data, private, public)= crypto.unpackage_data(cap, cipher) 
            else:
                return
    # TODO pretty print data
    data = json.loads(data) 
    print data.keys()
Example #4
0
def mkdir_handler(arg):
    if arg.path:
        # TODO do recursive get on the path /a/b/c/
        # TODO assert somehow that the path exists on the server
        return
    else:
        if arg.root:
            # create root dir
            (root_private, root_public, cap) = crypto.generate_RSA()
            root_cap = [DIR_WRITE_CAP, cap[0], cap[1]]
            data = {".": ":".join(root_cap)}
            print "ROOT CAP: ", root_cap
            with open("private/root_dir.cap", 'w') as f:
                c = arg.name + "|" + capToString(root_cap) + "\n"
                f.write(c)
        else:
            # assume the root exists 
            with open("private/root_dir.cap", 'r') as f:
                line = f.read()
                if line and line != "\n":
                    line = line[line.find("|")+1:].strip("\n")
                    root_cap = line.split(":")
                    root_cap[2] = root_cap[2]
                    cipher = get_data(line)
                    (data, root_private, root_public)= crypto.unpackage_data(root_cap, cipher) 
                    data = json.loads(data) 
                    (private, public, cap) = crypto.generate_RSA()
                    cap = [DIR_WRITE_CAP, cap[0], cap[1]]
                    data[arg.name] =  ":".join(cap)
                    # post new dir 1st 
                    new_data = {".": ":".join(cap)}
                    new_data = json.dumps(new_data)
                    new_data = crypto.package_data(new_data, cap, private, public)
                    print_capabilities(cap)
                    post_data(new_data, capToString(cap))
                else:
                    return
    # post root dir data 
    data = json.dumps(data) 
    data = crypto.package_data(data, root_cap, root_private, root_public)
    print_capabilities(root_cap)
    post_data(data, capToString(root_cap))