cryptomator-utils/list_directory.py

69 lines
2.5 KiB
Python
Raw Normal View History

2024-05-26 00:30:15 +10:00
#!/usr/bin/env python3
# cryptomator-utils: Python utilities for inspecting Cryptomator drives
# Copyright (C) 2024 Lee Yingtong Li (RunasSudo)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2024-05-26 01:18:01 +10:00
from lib_cryptomator_utils.cryptomator import encrypt_filename, hash_directory_id, list_directory, load_vault_config
2024-05-26 00:30:15 +10:00
import os
import sys
def main():
if len(sys.argv) < 3:
print('Usage: {} /path/to/vault.cryptomator /plaintext/path/within/drive'.format(sys.argv[0]))
2024-05-26 00:30:15 +10:00
sys.exit(1)
2024-05-26 01:18:01 +10:00
# Parse CLI arguments
2024-05-26 00:30:15 +10:00
vault_config_path = sys.argv[1]
target_directory = sys.argv[2]
vault_path = os.path.split(vault_config_path)[0]
2024-05-26 01:18:01 +10:00
# Load vault config (asks for password)
2024-05-26 00:30:15 +10:00
primary_master_key, hmac_master_key = load_vault_config(vault_config_path)
target_directory_parts = target_directory.strip('/').split('/')
2024-05-26 01:18:01 +10:00
# Begin in root directory
# The root directory in Cryptomator has an empty directory ID
2024-05-26 00:30:15 +10:00
directory_id = ''
# Traverse path
for path_part in target_directory_parts:
if not path_part:
continue
2024-05-26 01:18:01 +10:00
# Hash the current directory ID
2024-05-26 00:30:15 +10:00
hashed_directory_id = hash_directory_id(primary_master_key, hmac_master_key, directory_id)
2024-05-26 01:18:01 +10:00
# Look up the encrypted path_part in the current directory
encrypted_filename = encrypt_filename(primary_master_key, hmac_master_key, directory_id, path_part)
# Get the directory ID of the part_path
subdirectory_dir_file = os.path.join(vault_path, 'd', hashed_directory_id[:2], hashed_directory_id[2:], encrypted_filename, 'dir.c9r')
2024-05-26 00:30:15 +10:00
with open(subdirectory_dir_file, 'r') as f:
new_directory_id = f.read()
2024-05-26 01:18:01 +10:00
# Traverse to the new directory ID
2024-05-26 00:30:15 +10:00
directory_id = new_directory_id
2024-05-26 01:18:01 +10:00
# Now we have reached the requested directory, so print directory listing
for filename in sorted(list_directory(vault_path, primary_master_key, hmac_master_key, directory_id)):
2024-05-26 00:30:15 +10:00
print(filename)
if __name__ == '__main__':
main()