示例#1
0
 def unzip_testfiles_configonly(self):
     """
     unzip_testfiles_configonly
     """
     ensure_directory(self.cboptions.dir)
     ensure_directory(get_data_dir(self.cboptions))
     os.system("cd testdata; cp testmap_config.zip test.zip")
     os.system("cd testdata; unzip -o test.zip > /dev/null")
     os.system("rm testdata/test.zip")
     self.cbmemory.load(get_data_dir(self.cboptions))
示例#2
0
 def reset_cb_dir(self):
     """
     complete_reset
     """
     os.system("rm -Rf testdata/test")
     ensure_directory(self.cboptions.dir)
     ensure_directory(get_data_dir(self.cboptions))
示例#3
0
def hide_config(options, salt, secret):
    """
    @type options: optparse.Values, instance
    @param salt:
    @type salt:
    @param secret:
    @type secret:
    """
    if salt and secret:
        datadir = get_data_dir(options)
        mempath = os.path.join(datadir, "memory.pickle")
        jsonpath = os.path.join(datadir, "memory.pickle.json")

        if os.path.exists(jsonpath):
            os.remove(jsonpath)

        if os.path.exists(mempath):
            read_and_encrypt_file(mempath, mempath + ".enc", secret)
            os.remove(mempath)
            hidden_name = "." + get_uuid(3)

            while hidden_name in os.listdir(options.dir):
                hidden_name = "." + get_uuid(3)

            encrypted_name = msgpack_encrypt_pyobject(secret, options.cryptobox)
            pickle_object(os.path.join(options.dir, hidden_name + ".cryptoboxfolder"), {"encrypted_name": encrypted_name, "salt": salt})
            os.rename(options.dir, os.path.join(os.path.dirname(options.dir), hidden_name))
示例#4
0
def hide_config(options, salt, secret):
    """
    @type options: optparse.Values, instance
    @param salt:
    @type salt:
    @param secret:
    @type secret:
    """
    if salt and secret:
        datadir = get_data_dir(options)
        mempath = os.path.join(datadir, "memory.pickle")
        jsonpath = os.path.join(datadir, "memory.pickle.json")

        if os.path.exists(jsonpath):
            os.remove(jsonpath)

        if os.path.exists(mempath):
            read_and_encrypt_file(mempath, mempath + ".enc", secret)
            os.remove(mempath)
            hidden_name = "." + get_uuid(3)

            while hidden_name in os.listdir(options.dir):
                hidden_name = "." + get_uuid(3)

            encrypted_name = msgpack_encrypt_pyobject(secret,
                                                      options.cryptobox)
            pickle_object(
                os.path.join(options.dir, hidden_name + ".cryptoboxfolder"), {
                    "encrypted_name": encrypted_name,
                    "salt": salt
                })
            os.rename(options.dir,
                      os.path.join(os.path.dirname(options.dir), hidden_name))
示例#5
0
 def unzip_testfiles_clean(self):
     """
     unzip_testfiles_clean
     """
     ensure_directory(self.cboptions.dir)
     ensure_directory(get_data_dir(self.cboptions))
     os.system("cd testdata; cp testmap_clean.zip test.zip")
     os.system("cd testdata; unzip -o test.zip > /dev/null")
     os.system("rm testdata/test.zip")
示例#6
0
 def test_index_clear(self):
     self.do_wait_for_tasks = False
     self.unzip_testfiles_clean()
     salt, secret, self.cbmemory, localindex = index_and_encrypt(self.cbmemory, self.cboptions)
     self.cboptions.clear = True
     self.cboptions.encrypt = False
     self.cboptions.clear = "1"
     reset_cryptobox_local(self.cboptions)
     dd = get_data_dir(self.cboptions)
     self.assertEquals(os.path.exists(dd), False)
示例#7
0
    def test_encrypt_hide_decrypt(self):
        """
        encrypt_hide_decrypt
        """
        self.do_wait_for_tasks = False
        encrypt = 1
        decrypt = 1
        self.reset_cb_dir()
        self.unzip_testfiles_synced()
        p = os.path.join(os.path.join(os.getcwd(), "testdata"), "test")
        org_files = get_files_dir(p, ignore_hidden=True)
        org_files = [x for x in org_files if "memory.pickle.json" not in x]

        #decrypt_and_build_filetree, hide_config
        if encrypt:
            self.delete_hidden_configs()
            self.do_wait_for_tasks = False
            self.cboptions.remove = True
            salt, secret, self.cbmemory, localindex = index_and_encrypt(self.cbmemory, self.cboptions)
            datadir = get_data_dir(self.cboptions)
            self.cbmemory.save(datadir)
            hide_config(self.cboptions, salt, secret)
            self.assertEqual(count_files_dir(get_blob_dir(self.cboptions)), 0)

        if decrypt:
            os.system("rm -Rf testdata/test")
            if not encrypt:
                os.system("cd testdata; unzip -o hidden_config.zip > /dev/null")

            options = self.cboptions
            options.encrypt = False
            options.decrypt = True
            options.remove = False
            secret = restore_hidden_config(options)
            datadir = get_data_dir(self.cboptions)
            memory = Memory()
            memory.load(datadir)
            decrypt_and_build_filetree(memory, options, secret)

        org_files2 = get_files_dir(p, ignore_hidden=True)
        self.assertEqual(set(org_files), set(org_files2))
示例#8
0
def decrypt_and_build_filetree(memory, options, secret):
    """
    decrypt_and_build_filetree
    @type memory: Memory
    @type options: optparse.Values, instance
    @type secret: str
    """
    if not secret:
        raise Exception("decrypt_and_build_filetree: no secret given")

    datadir = get_data_dir(options)

    if not os.path.exists(datadir):
        print "cba_index.py:365", "nothing to decrypt", datadir, "does not exists"
        return memory

    output_json({"msg": "preparing decrypt"})
    blobdir = os.path.join(datadir, "blobs")
    localindex = get_localindex(memory)
    hashes = set()
    restored_hashes = []

    if localindex:
        for dirhash in localindex["dirnames"]:
            if "dirname" in localindex["dirnames"][dirhash]:
                if not os.path.exists(
                        localindex["dirnames"][dirhash]["dirname"]):
                    ensure_directory(
                        localindex["dirnames"][dirhash]["dirname"])

            for cfile in localindex["dirnames"][dirhash]["filenames"]:
                fpath = os.path.join(
                    localindex["dirnames"][dirhash]["dirname"], cfile["name"])

                if not os.path.exists(fpath):
                    hashes.add((cfile["hash"], cfile["name"]))
                else:
                    restored_hashes.append(cfile["hash"])

    processed_files = 0
    numfiles = len(hashes)

    for cfile in hashes:
        processed_files += 1
        update_progress(processed_files, numfiles, cfile[1])

        #noinspection PyUnusedLocal
        paths = decrypt_blob_to_filepaths(blobdir, localindex, cfile[0],
                                          secret)

    memory = store_localindex(memory, localindex)
    cleanup_tempfiles()
    return memory
示例#9
0
 def test_super_large_file(self):
     """
     test_super_large_file
     """
     self.reset_cb_db_clean()
     self.unzip_testfiles_clean()
     self.make_testfile("3000MB.txt", 3000)
     os.system("rm testdata/test/all_types/*")
     os.system("rm -Rf testdata/test/smalltest")
     os.system("cp testdata/3000MB.txt testdata/test/all_types/")
     localindex, self.cbmemory = sync_server(self.cbmemory, self.cboptions)
     self.assertTrue(self.files_synced())
     datadir = get_data_dir(self.cboptions)
     self.cbmemory.save(datadir)
     p = os.path.join(os.path.join(os.getcwd(), "testdata"), "test")
     org_files = get_files_dir(p, ignore_hidden=True)
     org_files = [x for x in org_files if "memory.pickle.json" not in x]
     org_files1 = [make_sha1_hash_file(fpath=x) for x in org_files]
     self.delete_hidden_configs()
     self.do_wait_for_tasks = False
     self.cboptions.remove = True
     salt, secret, self.cbmemory, localindex = index_and_encrypt(self.cbmemory, self.cboptions)
     datadir = get_data_dir(self.cboptions)
     self.cbmemory.save(datadir)
     hide_config(self.cboptions, salt, secret)
     self.assertEqual(count_files_dir(get_blob_dir(self.cboptions)), 0)
     options = self.cboptions
     options.encrypt = False
     options.decrypt = True
     options.remove = False
     secret = restore_hidden_config(options)
     datadir = get_data_dir(self.cboptions)
     memory = Memory()
     memory.load(datadir)
     decrypt_and_build_filetree(memory, options, secret)
     org_files2 = get_files_dir(p, ignore_hidden=True)
     org_files3 = [make_sha1_hash_file(fpath=x) for x in org_files2]
     self.assertEqual(set(org_files1), set(org_files3))
     os.system("rm -Rf testdata/test")
     os.system("rm testdata/3000MB.txt")
示例#10
0
def decrypt_and_build_filetree(memory, options, secret):
    """
    decrypt_and_build_filetree
    @type memory: Memory
    @type options: optparse.Values, instance
    @type secret: str
    """
    if not secret:
        raise Exception("decrypt_and_build_filetree: no secret given")

    datadir = get_data_dir(options)

    if not os.path.exists(datadir):
        print "cba_index.py:365", "nothing to decrypt", datadir, "does not exists"
        return memory

    output_json({"msg": "preparing decrypt"})
    blobdir = os.path.join(datadir, "blobs")
    localindex = get_localindex(memory)
    hashes = set()
    restored_hashes = []

    if localindex:
        for dirhash in localindex["dirnames"]:
            if "dirname" in localindex["dirnames"][dirhash]:
                if not os.path.exists(localindex["dirnames"][dirhash]["dirname"]):
                    ensure_directory(localindex["dirnames"][dirhash]["dirname"])

            for cfile in localindex["dirnames"][dirhash]["filenames"]:
                fpath = os.path.join(localindex["dirnames"][dirhash]["dirname"], cfile["name"])

                if not os.path.exists(fpath):
                    hashes.add((cfile["hash"], cfile["name"]))
                else:
                    restored_hashes.append(cfile["hash"])

    processed_files = 0
    numfiles = len(hashes)

    for cfile in hashes:
        processed_files += 1
        update_progress(processed_files, numfiles, cfile[1])

        #noinspection PyUnusedLocal
        paths = decrypt_blob_to_filepaths(blobdir, localindex, cfile[0], secret)

    memory = store_localindex(memory, localindex)
    cleanup_tempfiles()
    return memory
示例#11
0
def reset_cryptobox_local(options):
    """
    check_and_clean_dir
    @type options: optparse.Values, instance
    """
    if not hasattr(options, "clear") or not hasattr(options, "encrypt"):
        print "cba_index.py:340", "check_and_clean_dir needs clear and encrypt option"
        return

    if options.clear == "1":
        if options.encrypt:
            print "cba_index.py:345", "clear options cannot be used together with encrypt, possible data loss"
            return

        datadir = get_data_dir(options)
        shutil.rmtree(datadir, True)
示例#12
0
    def tearDown(self):
        """
        tearDown
        """
        if self.do_wait_for_tasks:
            wait_for_tasks(self.cbmemory, self.cboptions)
        self.cbmemory.save(get_data_dir(self.cboptions))
        if os.path.exists('stdout.txt'):
            os.remove('stdout.txt')

        if os.path.exists('stderr.txt'):
            os.remove('stderr.txt')
        delete_progress_file("progress")
        delete_progress_file("item_progress")
        if self.remove_temp_files:
            self.clear_tempfiles()
示例#13
0
def reset_cryptobox_local(options):
    """
    check_and_clean_dir
    @type options: optparse.Values, instance
    """
    if not hasattr(options, "clear") or not hasattr(options, "encrypt"):
        print "cba_index.py:340", "check_and_clean_dir needs clear and encrypt option"
        return

    if options.clear == "1":
        if options.encrypt:
            print "cba_index.py:345", "clear options cannot be used together with encrypt, possible data loss"
            return

        datadir = get_data_dir(options)
        shutil.rmtree(datadir, True)
示例#14
0
def restore_config(config_file_path, cryptoboxname, options, secret):
    """
    @type config_file_path: str, unicode
    @type cryptoboxname: str, unicode
    @type options: optparse.Values, instance
    @type secret: str, unicode
    """
    if os.path.exists(config_file_path):
        os.remove(config_file_path)

    p1 = os.path.dirname(config_file_path)
    p2 = os.path.join(options.basedir, cryptoboxname)
    os.rename(p1, p2)
    if secret:
        datadir = get_data_dir(options)
        mempath = os.path.join(datadir, "memory.pickle")

        if os.path.exists(mempath + ".enc"):
            decrypt_file_and_write(mempath + ".enc", mempath, secret=secret)
示例#15
0
def restore_config(config_file_path, cryptoboxname, options, secret):
    """
    @type config_file_path: str, unicode
    @type cryptoboxname: str, unicode
    @type options: optparse.Values, instance
    @type secret: str, unicode
    """
    if os.path.exists(config_file_path):
        os.remove(config_file_path)

    p1 = os.path.dirname(config_file_path)
    p2 = os.path.join(options.basedir, cryptoboxname)
    os.rename(p1, p2)
    if secret:
        datadir = get_data_dir(options)
        mempath = os.path.join(datadir, "memory.pickle")

        if os.path.exists(mempath + ".enc"):
            decrypt_file_and_write(mempath + ".enc", mempath, secret=secret)
示例#16
0
def make_local_index(options):
    """
    make_local_index
    @type options: optparse.Values, instance
    """
    datadir = get_data_dir(options)
    args = {"DIR": options.dir,
            "folders": {"dirnames": {},
            "filestats": {}},
            "messages": [],
            "numfiles": 0}
    os.path.walk(options.dir, index_files_visit, args)

    for dir_name in args["folders"]["dirnames"].copy():
        if datadir in args["folders"]["dirnames"][dir_name]["dirname"]:
            del args["folders"]["dirnames"][dir_name]

    localindex = args["folders"]
    localindex["messages"] = tuple(set(args["messages"]))
    return localindex
示例#17
0
def make_local_index(options):
    """
    make_local_index
    @type options: optparse.Values, instance
    """
    datadir = get_data_dir(options)
    args = {
        "DIR": options.dir,
        "folders": {
            "dirnames": {},
            "filestats": {}
        },
        "messages": [],
        "numfiles": 0
    }
    os.path.walk(options.dir, index_files_visit, args)

    for dir_name in args["folders"]["dirnames"].copy():
        if datadir in args["folders"]["dirnames"][dir_name]["dirname"]:
            del args["folders"]["dirnames"][dir_name]

    localindex = args["folders"]
    localindex["messages"] = tuple(set(args["messages"]))
    return localindex
示例#18
0
def index_and_encrypt(memory, options):
    """
    index_and_encrypt
    @type memory: Memory
    @type options: optparse.Values, instance
    @rtype salt, secret, memory, localindex: str, str, Memory, dict
    """
    localindex = make_local_index(options)
    datadir = get_data_dir(options)

    if quick_lock_check(options):
        output_json({"message": "cryptobox is locked, nothing can be added now first decrypt (-d)"})
        return None, None, memory, localindex

    salt = None

    if memory.has("salt_b64"):
        salt = base64.decodestring(memory.get("salt_b64"))

    if not salt:
        salt = Random.new().read(32)
        memory.set("salt_b64", base64.encodestring(salt))

    output_json({"msg": "preparing encrypt"})
    secret = password_derivation(options.password, salt)
    ensure_directory(datadir)
    new_blobs = {}
    file_cnt = 0
    new_objects = 0
    hash_set_on_disk = set()
    processed_files = 0
    numfiles = 0

    for dirhash in localindex["dirnames"]:
        numfiles += len(localindex["dirnames"][dirhash]["filenames"])

    for dirhash in localindex["dirnames"]:
        for fname in localindex["dirnames"][dirhash]["filenames"]:
            file_cnt += 1
            file_dir = localindex["dirnames"][dirhash]["dirname"]
            file_path = os.path.join(file_dir, fname["name"])

            if os.path.exists(file_path):
                update_progress(processed_files, numfiles, "indexing " + os.path.basename(file_path))
                filedata, localindex = make_cryptogit_hash(file_path, datadir, localindex)
                fname["hash"] = filedata["filehash"]
                hash_set_on_disk.add(filedata["filehash"])
                if not filedata["blob_exists"]:
                    new_blobs[filedata["filehash"]] = filedata
                    new_objects += 1

                if len(new_blobs) > 1500:
                    encrypt_new_blobs(secret, new_blobs)
                    new_blobs = {}

    if len(new_blobs) > 0:
        if len(new_blobs) > 0:
            encrypt_new_blobs(secret, new_blobs)
    cleanup_tempfiles()
    memory = store_localindex(memory, localindex)

    if options.remove:
        ld = os.listdir(options.dir)
        ld.remove(".cryptobox")
        processed_files = 0
        numfiles = len(ld)

        for fname in ld:
            fpath = os.path.join(options.dir, fname)
            processed_files += 1
            update_progress(processed_files, numfiles, "delete " + os.path.basename(fpath))
            if os.path.isdir(fpath):
                if os.path.exists(fpath):
                    shutil.rmtree(fpath, True)
            else:
                if os.path.exists(fpath):
                    os.remove(fpath)

    obsolute_blob_store_entries = set()
    blob_dirs = os.path.join(datadir, "blobs")
    ensure_directory(blob_dirs)

    for blob_dir in os.listdir(blob_dirs):
        blob_store = os.path.join(blob_dirs, blob_dir.lstrip(os.path.sep))

        if os.path.isdir(blob_store):
            for blob_file in os.listdir(blob_store):
                found = False

                for fhash in hash_set_on_disk:
                    if fhash in (blob_dir + blob_file):
                        found = True

                if not found:
                    obsolute_blob_store_entries.add(blob_dir + blob_file)

    for f_hash in obsolute_blob_store_entries:
        blob_dir = os.path.join(blob_dirs, f_hash[:2])
        blob_path = os.path.join(blob_dir, f_hash[2:])
        os.remove(blob_path)
        if os.path.isdir(blob_dir):
            blob_entries = [f for f in os.listdir(blob_dir) if not f.startswith('.')]

            if len(blob_entries) == 0:
                shutil.rmtree(blob_dir, True)

    cleanup_tempfiles()
    return salt, secret, memory, localindex
示例#19
0
def cryptobox_command(options):
    """
    @param options: dictionary with options
    @type options: namedtuple, Values
    @return: succes indicator
    @rtype: bool
    """
    Events = Events()

    try:
        if options.acommand:
            if options.acommand == "open_folder":
                if options.dir:
                    p = os.path.join(options.dir, options.cryptobox)
                    open_folder(p)
                    output_json({"log": "open " + p})
                else:
                    message_json("no folder given(-f)")
            elif options.acommand == "hash":
                if not options.input:
                    message_json("need input (-i)")

                path = options.input
                output_json({"hash": make_hash_path(path)})
                return
            elif options.acommand == "check_new_release":
                if not options.server:
                    message_json("server mising")

                if not options.compiled:
                    message_json("compiled mising")

                log_json("options.compiled: " + options.compiled)
                current_hash = make_hash_path(options.compiled)
                hash_url = urlparse.urljoin(options.server, "/st/data/cba_main.hash.json")
                log_json("hash_url: " + hash_url)
                result = urllib2.urlopen(hash_url).read()
                result_json = json.loads(result)
                new_release = not (current_hash == result_json["hash"])
                output_json(
                    {"new_release": new_release, "current_hash": current_hash, "hash_server": result_json["hash"]}
                )
                return
            elif options.acommand == "download_new_release":
                if not options.server:
                    message_json("server mising")

                output_json({"msg": "Downloading Cryptobox.dmg"})
                download_url = urlparse.urljoin(options.server, "/st/data/Cryptobox.dmg")
                tempfile_cb = download_server(
                    None, options, download_url, output_name_item_percentage="global_progress"
                )
                output_json({"msg": ""})
                output_json({"item_progress": 0})
                output_json({"global_progress": 0})
                root = Tkinter.Tk()
                root.withdraw()
                file_path = tkFileDialog.asksaveasfilename(
                    parent=None, message="Cryptobox", initialfile="Cryptobox.dmg"
                )

                if file_path:
                    if os.path.exists(os.path.dirname(file_path)):
                        if os.path.exists(tempfile_cb):
                            os.rename(tempfile_cb, file_path)
                            cleanup_tempfiles()

                return
            elif options.acommand == "delete_blobs":
                if not options.dir:
                    message_json("dir mising")
                    return

                if not options.cryptobox:
                    message_json("cryptobox mising")
                    return

                blobpath = os.path.join(options.dir, options.cryptobox)
                blobpath = os.path.join(blobpath, ".cryptobox")
                blobpath = os.path.join(blobpath, "blobs")

                if os.path.exists(blobpath):
                    shutil.rmtree(blobpath, True)
                    message_json("encrypted cache emptied")

                return
            elif options.acommand == "open_website":
                if not options.username:
                    message_json("username mising")
                    return

                if not options.password:
                    message_json("password missing")
                    return

                if not options.cryptobox:
                    message_json("cryptobox missing")
                    return

                m = Memory()
                m = authorize_user(m, options, force=True)

                if not m.has_get("authorized"):
                    message_json("Username or password is not correct")
                else:
                    encoded_token = b64_encode_safe("session_token:" + m.get("session_token"))
                    private_key = b64_encode_safe(m.get("private_key"))
                    webbrowser.open_new_tab(
                        options.server
                        + options.cryptobox
                        + "/autologin/"
                        + options.username
                        + "/"
                        + encoded_token
                        + "/"
                        + private_key
                    )
            else:
                print "cba_main.py:267", "unknown command"
            return

        if options.motivation:
            qlist = msgpack.load(open("quotes.list"))
            q = qlist[random.randint(0, len(qlist)) - 1]
            output_json({"motivation": q[0] + "\n\n- " + q[1]})
            return

        # noinspection PyUnusedLocal
        single_instance = singleton.SingleInstance()

        if options.decrypt:
            if options.remove:
                print "cba_main.py:281", "option remove (-r) cannot be used together with decrypt (dataloss)"
                return False

            if options.sync:
                print "cba_main.py:285", "option sync (-s) cannot be used together with decrypt (hashmismatch)"
                return False

            if options.check:
                print "cba_main.py:289", "option check (-o) cannot be used together with decrypt (hashmismatch)"
                return False

        if not options.password:
            print "cba_main.py:293", "No password given (-p or --password)"
            return False

        if options.username or options.cryptobox:
            if not options.username:
                print "cba_main.py:298", "No username given (-u or --username)"
                return False

            if not options.cryptobox:
                print "cba_main.py:302", "No cryptobox given (-b or --cryptobox)"
                return False

        if isinstance(options, dict):
            options = Dict2Obj(options)

        if options.version:
            return "0.1"

        if not options.numdownloadthreads:
            options.numdownloadthreads = 2
        else:
            options.numdownloadthreads = int(options.numdownloadthreads)

        options.numdownloadthreads = 8

        if not options.dir:
            print "cba_main.py:319", "Need DIR -f or --dir to continue"
            return False

        if not options.cryptobox:
            print "cba_main.py:323", "No cryptobox given -b or --cryptobox"
            return False

        options.basedir = options.dir
        ensure_directory(options.basedir)
        options.dir = os.path.join(options.dir, options.cryptobox)

        if not options.decrypt:
            if quick_lock_check(options):
                output_json({"locked": True})
                return False

        if not options.encrypt:
            restore_hidden_config(options)
        ensure_directory(options.dir)
        datadir = get_data_dir(options)

        if options.clear == "1":
            if os.path.exists(datadir):
                shutil.rmtree(datadir)
                output_json({"info_message": "cryptobox cache removed: " + str(datadir)})
            else:
                output_json({"info_message": "cryptobox cache already removed: " + str(datadir)})

            return

        ensure_directory(datadir)
        if not datadir:
            print "cba_main.py:351", "datadir is None"

        memory = Memory()
        memory.load(datadir)
        memory.replace("cryptobox_folder", options.dir)
        if not os.path.exists(options.basedir):
            print "cba_main.py:357", "DIR [", options.dir, "] does not exist"
            return False

        if options.sync:
            if not options.username:
                print "cba_main.py:362", "No username given (-u or --username)"
                return False

            if not options.password:
                print "cba_main.py:366", "No password given (-p or --password)"
                return False

        if options.logout:
            result, memory = on_server(memory, options, "logoutserver", {}, memory.get("session"))
            return result[0]
        elif options.treeseq:
            memory = authorize_user(memory, options)
            tree_seq = get_tree_sequence(memory, options)

            if tree_seq:
                output_json({"tree_seq": tree_seq})

            return True
        elif options.password and options.username and options.cryptobox and (options.sync or options.check):
            memory = authorize_user(memory, options, force=True)

            if not memory.get("connection"):
                return

            if not memory.get("authorized"):
                message_json("Username or password is not correct")
                output_json({"instruction": "lock_buttons_password_wrong"})
                return

            if memory.get("authorized"):
                if options.check:
                    if quick_lock_check(options):
                        return False
                    ensure_directory(options.dir)
                    Events.event("check get_server_index")
                    serverindex, memory = get_server_index(memory, options)
                    Events.event("check make_local_index")
                    localindex = make_local_index(options)
                    Events.event("check get_sync_changes")
                    memory, options, file_del_server, file_downloads, file_uploads, dir_del_server, dir_make_local, dir_make_server, dir_del_local, file_del_local, server_path_nodes, unique_content, rename_server, rename_dirs, rename_local_dirs = get_sync_changes(
                        memory, options, localindex, serverindex
                    )
                    all_synced = all_item_zero_len(
                        [
                            file_del_server,
                            file_downloads,
                            file_uploads,
                            dir_del_server,
                            dir_make_local,
                            dir_make_server,
                            dir_del_local,
                            file_del_local,
                            rename_server,
                            rename_dirs,
                        ]
                    )
                    outputdict = {
                        "file_del_server": file_del_server,
                        "file_downloads": file_downloads,
                        "file_uploads": file_uploads,
                        "dir_del_server": dir_del_server,
                        "dir_make_local": dir_make_local,
                        "dir_make_server": dir_make_server,
                        "dir_del_local": dir_del_local,
                        "file_del_local": file_del_local,
                        "all_synced": all_synced,
                        "rename_file_server": rename_server,
                        "rename_folder_server": rename_dirs,
                        "rename_local_dirs": rename_local_dirs,
                    }

                    output_json(outputdict)
                elif options.sync:
                    if options.encrypt:
                        message_json("sync and encrypt called together")
                        return False

                    if quick_lock_check(options):
                        message_json("cryptobox is locked, nothing can be added now first decrypt (-d)")
                        return False
                    ensure_directory(options.dir)
                    Events.event("check sync_server")
                    localindex, memory = sync_server(memory, options)

                    # Events.report_measurements()

        salt = secret = None
        if options.encrypt:
            salt, secret, memory, localindex = index_and_encrypt(memory, options)
            output_json({"msg": ""})
            output_json({"item_progress": 0})
            output_json({"global_progress": 0})

        if options.decrypt:
            if not options.clear == "1":
                if not secret:
                    if memory.has("salt_b64"):
                        salt = base64.decodestring(memory.get("salt_b64"))

                    if not salt:
                        raise Exception("decrypt, no salt")

                    secret = password_derivation(options.password, salt)

                memory = decrypt_and_build_filetree(memory, options, secret)
                output_json({"msg": ""})
                output_json({"item_progress": 0})
                output_json({"global_progress": 0})
        reset_cryptobox_local(options)
        memory.save(datadir)
        if options.remove and salt and secret:
            hide_config(options, salt, secret)
            output_json({"msg": ""})
            output_json({"item_progress": 0})
            output_json({"global_progress": 0})
    finally:
        pass
    return True
示例#20
0
def index_and_encrypt(memory, options):
    """
    index_and_encrypt
    @type memory: Memory
    @type options: optparse.Values, instance
    @rtype salt, secret, memory, localindex: str, str, Memory, dict
    """
    localindex = make_local_index(options)
    datadir = get_data_dir(options)

    if quick_lock_check(options):
        output_json({
            "message":
            "cryptobox is locked, nothing can be added now first decrypt (-d)"
        })
        return None, None, memory, localindex

    salt = None

    if memory.has("salt_b64"):
        salt = base64.decodestring(memory.get("salt_b64"))

    if not salt:
        salt = Random.new().read(32)
        memory.set("salt_b64", base64.encodestring(salt))

    output_json({"msg": "preparing encrypt"})
    secret = password_derivation(options.password, salt)
    ensure_directory(datadir)
    new_blobs = {}
    file_cnt = 0
    new_objects = 0
    hash_set_on_disk = set()
    processed_files = 0
    numfiles = 0

    for dirhash in localindex["dirnames"]:
        numfiles += len(localindex["dirnames"][dirhash]["filenames"])

    for dirhash in localindex["dirnames"]:
        for fname in localindex["dirnames"][dirhash]["filenames"]:
            file_cnt += 1
            file_dir = localindex["dirnames"][dirhash]["dirname"]
            file_path = os.path.join(file_dir, fname["name"])

            if os.path.exists(file_path):
                update_progress(processed_files, numfiles,
                                "indexing " + os.path.basename(file_path))
                filedata, localindex = make_cryptogit_hash(
                    file_path, datadir, localindex)
                fname["hash"] = filedata["filehash"]
                hash_set_on_disk.add(filedata["filehash"])
                if not filedata["blob_exists"]:
                    new_blobs[filedata["filehash"]] = filedata
                    new_objects += 1

                if len(new_blobs) > 1500:
                    encrypt_new_blobs(secret, new_blobs)
                    new_blobs = {}

    if len(new_blobs) > 0:
        if len(new_blobs) > 0:
            encrypt_new_blobs(secret, new_blobs)
    cleanup_tempfiles()
    memory = store_localindex(memory, localindex)

    if options.remove:
        ld = os.listdir(options.dir)
        ld.remove(".cryptobox")
        processed_files = 0
        numfiles = len(ld)

        for fname in ld:
            fpath = os.path.join(options.dir, fname)
            processed_files += 1
            update_progress(processed_files, numfiles,
                            "delete " + os.path.basename(fpath))
            if os.path.isdir(fpath):
                if os.path.exists(fpath):
                    shutil.rmtree(fpath, True)
            else:
                if os.path.exists(fpath):
                    os.remove(fpath)

    obsolute_blob_store_entries = set()
    blob_dirs = os.path.join(datadir, "blobs")
    ensure_directory(blob_dirs)

    for blob_dir in os.listdir(blob_dirs):
        blob_store = os.path.join(blob_dirs, blob_dir.lstrip(os.path.sep))

        if os.path.isdir(blob_store):
            for blob_file in os.listdir(blob_store):
                found = False

                for fhash in hash_set_on_disk:
                    if fhash in (blob_dir + blob_file):
                        found = True

                if not found:
                    obsolute_blob_store_entries.add(blob_dir + blob_file)

    for f_hash in obsolute_blob_store_entries:
        blob_dir = os.path.join(blob_dirs, f_hash[:2])
        blob_path = os.path.join(blob_dir, f_hash[2:])
        os.remove(blob_path)
        if os.path.isdir(blob_dir):
            blob_entries = [
                f for f in os.listdir(blob_dir) if not f.startswith('.')
            ]

            if len(blob_entries) == 0:
                shutil.rmtree(blob_dir, True)

    cleanup_tempfiles()
    return salt, secret, memory, localindex
示例#21
0
def cryptobox_command(options):
    """
    @param options: dictionary with options
    @type options: namedtuple, Values
    @return: succes indicator
    @rtype: bool
    """
    Events = Events()

    try:
        if options.acommand:
            if options.acommand == "open_folder":
                if options.dir:
                    p = os.path.join(options.dir, options.cryptobox)
                    open_folder(p)
                    output_json({"log": "open " + p})
                else:
                    message_json("no folder given(-f)")
            elif options.acommand == "hash":
                if not options.input:
                    message_json("need input (-i)")

                path = options.input
                output_json({"hash": make_hash_path(path)})
                return
            elif options.acommand == "check_new_release":
                if not options.server:
                    message_json("server mising")

                if not options.compiled:
                    message_json("compiled mising")

                log_json("options.compiled: " + options.compiled)
                current_hash = make_hash_path(options.compiled)
                hash_url = urlparse.urljoin(options.server,
                                            "/st/data/cba_main.hash.json")
                log_json("hash_url: " + hash_url)
                result = urllib2.urlopen(hash_url).read()
                result_json = json.loads(result)
                new_release = not (current_hash == result_json["hash"])
                output_json({
                    "new_release": new_release,
                    "current_hash": current_hash,
                    "hash_server": result_json["hash"]
                })
                return
            elif options.acommand == "download_new_release":
                if not options.server:
                    message_json("server mising")

                output_json({"msg": "Downloading Cryptobox.dmg"})
                download_url = urlparse.urljoin(options.server,
                                                "/st/data/Cryptobox.dmg")
                tempfile_cb = download_server(
                    None,
                    options,
                    download_url,
                    output_name_item_percentage="global_progress")
                output_json({"msg": ""})
                output_json({"item_progress": 0})
                output_json({"global_progress": 0})
                root = Tkinter.Tk()
                root.withdraw()
                file_path = tkFileDialog.asksaveasfilename(
                    parent=None,
                    message="Cryptobox",
                    initialfile='Cryptobox.dmg')

                if file_path:
                    if os.path.exists(os.path.dirname(file_path)):
                        if os.path.exists(tempfile_cb):
                            os.rename(tempfile_cb, file_path)
                            cleanup_tempfiles()

                return
            elif options.acommand == "delete_blobs":
                if not options.dir:
                    message_json("dir mising")
                    return

                if not options.cryptobox:
                    message_json("cryptobox mising")
                    return

                blobpath = os.path.join(options.dir, options.cryptobox)
                blobpath = os.path.join(blobpath, ".cryptobox")
                blobpath = os.path.join(blobpath, "blobs")

                if os.path.exists(blobpath):
                    shutil.rmtree(blobpath, True)
                    message_json("encrypted cache emptied")

                return
            elif options.acommand == "open_website":
                if not options.username:
                    message_json("username mising")
                    return

                if not options.password:
                    message_json("password missing")
                    return

                if not options.cryptobox:
                    message_json("cryptobox missing")
                    return

                m = Memory()
                m = authorize_user(m, options, force=True)

                if not m.has_get("authorized"):
                    message_json("Username or password is not correct")
                else:
                    encoded_token = b64_encode_safe("session_token:" +
                                                    m.get("session_token"))
                    private_key = b64_encode_safe(m.get("private_key"))
                    webbrowser.open_new_tab(options.server +
                                            options.cryptobox + "/autologin/" +
                                            options.username + "/" +
                                            encoded_token + "/" + private_key)
            else:
                print "cba_main.py:267", "unknown command"
            return

        if options.motivation:
            qlist = msgpack.load(open("quotes.list"))
            q = qlist[random.randint(0, len(qlist)) - 1]
            output_json({"motivation": q[0] + "\n\n- " + q[1]})
            return

        #noinspection PyUnusedLocal
        single_instance = singleton.SingleInstance()

        if options.decrypt:
            if options.remove:
                print "cba_main.py:281", "option remove (-r) cannot be used together with decrypt (dataloss)"
                return False

            if options.sync:
                print "cba_main.py:285", "option sync (-s) cannot be used together with decrypt (hashmismatch)"
                return False

            if options.check:
                print "cba_main.py:289", "option check (-o) cannot be used together with decrypt (hashmismatch)"
                return False

        if not options.password:
            print "cba_main.py:293", "No password given (-p or --password)"
            return False

        if options.username or options.cryptobox:
            if not options.username:
                print "cba_main.py:298", "No username given (-u or --username)"
                return False

            if not options.cryptobox:
                print "cba_main.py:302", "No cryptobox given (-b or --cryptobox)"
                return False

        if isinstance(options, dict):
            options = Dict2Obj(options)

        if options.version:
            return "0.1"

        if not options.numdownloadthreads:
            options.numdownloadthreads = 2
        else:
            options.numdownloadthreads = int(options.numdownloadthreads)

        options.numdownloadthreads = 8

        if not options.dir:
            print "cba_main.py:319", "Need DIR -f or --dir to continue"
            return False

        if not options.cryptobox:
            print "cba_main.py:323", "No cryptobox given -b or --cryptobox"
            return False

        options.basedir = options.dir
        ensure_directory(options.basedir)
        options.dir = os.path.join(options.dir, options.cryptobox)

        if not options.decrypt:
            if quick_lock_check(options):
                output_json({"locked": True})
                return False

        if not options.encrypt:
            restore_hidden_config(options)
        ensure_directory(options.dir)
        datadir = get_data_dir(options)

        if options.clear == "1":
            if os.path.exists(datadir):
                shutil.rmtree(datadir)
                output_json({
                    "info_message":
                    "cryptobox cache removed: " + str(datadir)
                })
            else:
                output_json({
                    "info_message":
                    "cryptobox cache already removed: " + str(datadir)
                })

            return

        ensure_directory(datadir)
        if not datadir:
            print "cba_main.py:351", "datadir is None"

        memory = Memory()
        memory.load(datadir)
        memory.replace("cryptobox_folder", options.dir)
        if not os.path.exists(options.basedir):
            print "cba_main.py:357", "DIR [", options.dir, "] does not exist"
            return False

        if options.sync:
            if not options.username:
                print "cba_main.py:362", "No username given (-u or --username)"
                return False

            if not options.password:
                print "cba_main.py:366", "No password given (-p or --password)"
                return False

        if options.logout:
            result, memory = on_server(memory, options, "logoutserver", {},
                                       memory.get("session"))
            return result[0]
        elif options.treeseq:
            memory = authorize_user(memory, options)
            tree_seq = get_tree_sequence(memory, options)

            if tree_seq:
                output_json({"tree_seq": tree_seq})

            return True
        elif options.password and options.username and options.cryptobox and (
                options.sync or options.check):
            memory = authorize_user(memory, options, force=True)

            if not memory.get("connection"):
                return

            if not memory.get("authorized"):
                message_json("Username or password is not correct")
                output_json({"instruction": "lock_buttons_password_wrong"})
                return

            if memory.get("authorized"):
                if options.check:
                    if quick_lock_check(options):
                        return False
                    ensure_directory(options.dir)
                    Events.event("check get_server_index")
                    serverindex, memory = get_server_index(memory, options)
                    Events.event("check make_local_index")
                    localindex = make_local_index(options)
                    Events.event("check get_sync_changes")
                    memory, options, file_del_server, file_downloads, file_uploads, dir_del_server, dir_make_local, dir_make_server, dir_del_local, file_del_local, server_path_nodes, unique_content, rename_server, rename_dirs, rename_local_dirs = get_sync_changes(
                        memory, options, localindex, serverindex)
                    all_synced = all_item_zero_len([
                        file_del_server, file_downloads, file_uploads,
                        dir_del_server, dir_make_local, dir_make_server,
                        dir_del_local, file_del_local, rename_server,
                        rename_dirs
                    ])
                    outputdict = {
                        "file_del_server": file_del_server,
                        "file_downloads": file_downloads,
                        "file_uploads": file_uploads,
                        "dir_del_server": dir_del_server,
                        "dir_make_local": dir_make_local,
                        "dir_make_server": dir_make_server,
                        "dir_del_local": dir_del_local,
                        "file_del_local": file_del_local,
                        "all_synced": all_synced,
                        "rename_file_server": rename_server,
                        "rename_folder_server": rename_dirs,
                        "rename_local_dirs": rename_local_dirs
                    }

                    output_json(outputdict)
                elif options.sync:
                    if options.encrypt:
                        message_json("sync and encrypt called together")
                        return False

                    if quick_lock_check(options):
                        message_json(
                            "cryptobox is locked, nothing can be added now first decrypt (-d)"
                        )
                        return False
                    ensure_directory(options.dir)
                    Events.event("check sync_server")
                    localindex, memory = sync_server(memory, options)

                    #Events.report_measurements()

        salt = secret = None
        if options.encrypt:
            salt, secret, memory, localindex = index_and_encrypt(
                memory, options)
            output_json({"msg": ""})
            output_json({"item_progress": 0})
            output_json({"global_progress": 0})

        if options.decrypt:
            if not options.clear == "1":
                if not secret:
                    if memory.has("salt_b64"):
                        salt = base64.decodestring(memory.get("salt_b64"))

                    if not salt:
                        raise Exception("decrypt, no salt")

                    secret = password_derivation(options.password, salt)

                memory = decrypt_and_build_filetree(memory, options, secret)
                output_json({"msg": ""})
                output_json({"item_progress": 0})
                output_json({"global_progress": 0})
        reset_cryptobox_local(options)
        memory.save(datadir)
        if options.remove and salt and secret:
            hide_config(options, salt, secret)
            output_json({"msg": ""})
            output_json({"item_progress": 0})
            output_json({"global_progress": 0})
    finally:
        pass
    return True