Example #1
0
def open_file(data_dic, memory=False):
    # Get passphrase
    passphrase = LoginController().get_passphrase(data_dic["label"])

    if not passphrase:
        passphrase = gui_utils.get_user_secret_input(
            "YourLocalBox - Enter Passphrase",
            "Please provide the passphrase to unlock file.")

    if not passphrase:
        gui_utils.show_error_dialog(_('No passphrase provided. aborting'),
                                    'Error',
                                    standalone=True)
        return None

    # Stat local box instance
    localbox_client = LocalBox(data_dic["url"], data_dic["label"], "")

    # Attempt to decode the file

    # print data_dic, passphrase

    try:
        decoded_contents = localbox_client.decode_file(
            data_dic["localbox_filename"], data_dic["filename"], passphrase)

    # If there was a failure, answer wit ha 404 to state that the file doesn't exist
    except Exception as e:
        gui_utils.show_error_dialog(
            _('Failed to decode contents. aborting : {}').format(e),
            'Error',
            standalone=True)
        getLogger(__name__).info(
            'failed to decode contents. aborting : {}'.format(e))

        return None

    # If the file was decoded, write it to disk
    tmp_decoded_filename = \
        os_utils.remove_extension(data_dic["filename"],
                                  defaults.LOCALBOX_EXTENSION)

    getLogger(__name__).info('tmp_decoded_filename: %s' % tmp_decoded_filename)

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

    if not memory:
        memory = LocalBoxMemoryFS()

    if not memory:
        localfile = open(tmp_decoded_filename, 'wb')
        localfile.write(decoded_contents)
        localfile.close()
    else:
        tmp_decoded_filename = memory.createfile(
            tmp_decoded_filename.split('/')[-1], decoded_contents)

    # Keep file in list of opened files
    openfiles_ctrl.add(tmp_decoded_filename)

    return tmp_decoded_filename
Example #2
0
def run_file_decryption(filename):
    try:
        getLogger(__name__).info('Decrypting and opening file: %s', filename)

        # verify if the file belongs to any of the configured syncs
        sync_list = sync_ctrl.list

        localbox_client = None
        localbox_filename = None
        for sync_item in sync_list:
            getLogger(__name__).debug('sync path: %s' % sync_item.path)
            sync_path = sync_item.path if sync_item.path.endswith(
                '/') else sync_item.path + os.path.sep
            if filename.startswith(sync_path):
                localbox_filename = os_utils.remove_extension(
                    filename.replace(sync_item.path, ''),
                    defaults.LOCALBOX_EXTENSION)
                localbox_client = LocalBox(sync_item.url, sync_item.label)
                break

        if not localbox_client or not localbox_filename:
            gui_utils.show_error_dialog(
                _('%s does not belong to any configured localbox') % filename,
                'Error', True)
            getLogger(__name__).error(
                '%s does not belong to any configured localbox' % filename)
            exit(1)

        # get passphrase
        label = localbox_client.authenticator.label
        passphrase = LoginController().get_passphrase(label, remote=True)
        if not passphrase:
            gui_wx.ask_passphrase(localbox_client.username, label)
            passphrase = LoginController().get_passphrase(label, remote=False)
            if not passphrase:
                gui_utils.show_error_dialog(
                    _('Failed to get passphrase for label: %s.') % label,
                    'Error', True)
                getLogger(__name__).error(
                    'failed to get passphrase for label: %s. Exiting..' %
                    label)
                exit(1)

        # decode file
        try:
            decoded_contents = localbox_client.decode_file(
                localbox_filename, filename, passphrase)
        except URLError:
            gui_utils.show_error_dialog(_('Failed to decode contents'),
                                        'Error',
                                        standalone=True)
            getLogger(__name__).info('failed to decode contents. aborting')
            return 1

        # write file
        tmp_decoded_filename = os_utils.remove_extension(
            filename, defaults.LOCALBOX_EXTENSION)
        getLogger(__name__).info('tmp_decoded_filename: %s' %
                                 tmp_decoded_filename)

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

        localfile = open(tmp_decoded_filename, 'wb')
        localfile.write(decoded_contents)
        localfile.close()

        # open file
        open_file_ext(tmp_decoded_filename)

        openfiles_ctrl.add(tmp_decoded_filename)

        getLogger(__name__).info('Finished decrypting and opening file: %s',
                                 filename)

    except Exception as ex:
        getLogger(__name__).exception(ex)