Implemented multi-client deletion

This commit is contained in:
2022-02-02 09:23:43 -05:00
parent 3d7f08e43e
commit 7f00570249
2 changed files with 56 additions and 18 deletions
+37 -14
View File
@@ -2,7 +2,7 @@
# external modules # external modules
from os import environ, remove, system, path from os import environ, listdir, path, remove, system
from shutil import copy, move, rmtree from shutil import copy, move, rmtree
from sys import argv, exit as s_exit from sys import argv, exit as s_exit
from pathlib import Path from pathlib import Path
@@ -49,7 +49,7 @@ def edit_note(_shm_folder, _shm_entry):
def tweak(): # runs configuration wizard def tweak(): # runs configuration wizard
# storage/config directory creation # storage/config directory creation
Path(path.expanduser('~/.password-pasture')).mkdir(0o700, parents=True, exist_ok=True) Path(path.expanduser('~/.password-pasture')).mkdir(0o700, parents=True, exist_ok=True)
Path(path.expanduser('~/.config/sshyp')).mkdir(0o700, parents=True, exist_ok=True) Path(path.expanduser('~/.config/sshyp/devices')).mkdir(0o700, parents=True, exist_ok=True)
if not Path(f"{path.expanduser('~/.config/sshyp/tmp')}").exists(): if not Path(f"{path.expanduser('~/.config/sshyp/tmp')}").exists():
if Path("/data/data/com.termux").exists(): if Path("/data/data/com.termux").exists():
system(f"ln -s '/data/data/com.termux/files/usr/tmp' {path.expanduser('~/.config/sshyp/tmp')}") system(f"ln -s '/data/data/com.termux/files/usr/tmp' {path.expanduser('~/.config/sshyp/tmp')}")
@@ -59,7 +59,9 @@ def tweak(): # runs configuration wizard
_device_type = input('\nIs this installation for a client or for a server? (C/s) ') _device_type = input('\nIs this installation for a client or for a server? (C/s) ')
if _device_type.lower() == 's': if _device_type.lower() == 's':
open(path.expanduser('~/.config/sshyp/sshyp-device'), 'w').write(_device_type) open(path.expanduser('~/.config/sshyp/sshyp-device'), 'w').write(_device_type)
Path(path.expanduser('~/.config/sshyp/deleted')).mkdir(0o700, parents=True, exist_ok=True)
copy('/usr/bin/sshync.py', path.expanduser('~/')) copy('/usr/bin/sshync.py', path.expanduser('~/'))
copy('/usr/bin/sshyp-shear.py', path.expanduser('~/shear.py'))
print('\nMake sure the ssh service is running and properly configured.\n\nConfiguration complete!\n') print('\nMake sure the ssh service is running and properly configured.\n\nConfiguration complete!\n')
s_exit() s_exit()
else: else:
@@ -103,6 +105,15 @@ def tweak(): # runs configuration wizard
sshync.make_profile(path.expanduser('~/.config/sshyp/sshyp.sshync'), sshync.make_profile(path.expanduser('~/.config/sshyp/sshyp.sshync'),
path.expanduser('~/.password-pasture/'), f"/home/{_username_ssh}/.password-pasture/", path.expanduser('~/.password-pasture/'), f"/home/{_username_ssh}/.password-pasture/",
path.expanduser('~/.ssh/sshyp'), _ip, _port, _username_ssh) path.expanduser('~/.ssh/sshyp'), _ip, _port, _username_ssh)
# device name configuration
print('\nIMPORTANT: This name MUST be unique amongst your client devices. '
'\nThis is used to keep track of which devices have up-to-date databases.\n')
_client_device_name = str(input('What would you like to name this device? '))
open(f"{path.expanduser('~/.config/sshyp/devices/')}{_client_device_name}", 'w')
system(f"ssh -i '{path.expanduser('~/.ssh/sshyp')}' -p {_port} {_username_ssh}@{_ip} "
f"\"touch '/home/{_username_ssh}/.config/sshyp/devices/{_client_device_name}'\"")
print('\nConfiguration complete!\n') print('\nConfiguration complete!\n')
@@ -147,8 +158,8 @@ def print_info(): # prints help text based on argument
print('/ /') print('/ /')
print('/ sshyp Copyright (C) 2021-2022 Randall Winkhart /') print('/ sshyp Copyright (C) 2021-2022 Randall Winkhart /')
print('/ /') print('/ /')
print('/ Version 2022.01.26.fr3.4 /') print('/ Version 2022.02.02.fr4 /')
print('/ The Boxing Update /') print('/ The High-Tech Shears Update /')
print('/ /') print('/ /')
print('////////////////////////////////////////////////////////\n') print('////////////////////////////////////////////////////////\n')
elif argument == 'license': elif argument == 'license':
@@ -198,6 +209,22 @@ def read_shortcut(): # shortcut to quickly read an entry
def sync(): # calls sshync to sync changes to the user's server def sync(): # calls sshync to sync changes to the user's server
print('\nSyncing entries with the server device...\n') print('\nSyncing entries with the server device...\n')
# check for deletions
system(f"ssh -i '{path.expanduser('~/.ssh/sshyp')}' -p {port} {username_ssh}@{ip} \"python -c 'import shear"
f"; shear.check(\"'\"{client_device_name}\"'\")'\"")
system(f"sftp -p -q -i '{path.expanduser('~/.ssh/sshyp')}' -P {port} {username_ssh}@{ip}:"
f"'/home/{username_ssh}/.config/sshyp/deletion_database' {path.expanduser('~/.config/sshyp/')}")
try:
_deletion_database = open(path.expanduser('~/.config/sshyp/deletion_database')).readlines()
except (FileNotFoundError, IndexError):
print('\nThe deletion database does not exist or is corrupted.\n')
_deletion_database = None
s_exit()
for _file in _deletion_database:
try:
remove(f"{path.expanduser('~/.password-pasture/')}{_file[:-1]}")
except FileNotFoundError:
print('\nFile does not exist locally.\n')
# set permissions before uploading # set permissions before uploading
system('find ' + directory + ' -type d -exec chmod -R 700 {} +') system('find ' + directory + ' -type d -exec chmod -R 700 {} +')
system('find ' + directory + ' -type f -exec chmod -R 600 {} +') system('find ' + directory + ' -type f -exec chmod -R 600 {} +')
@@ -369,7 +396,7 @@ def copy_data(): # copies a specified field of an entry to the clipboard
rmtree(f"{tmp_dir}{_shm_folder}") rmtree(f"{tmp_dir}{_shm_folder}")
def remove_data(): # deletes an entry or folder from both the client and the server def remove_data(): # deletes an entry from the server and flags it for local deletion on sync
print() print()
system(f"cd {directory}; ls -1 *") # TODO replace with new list system(f"cd {directory}; ls -1 *") # TODO replace with new list
_entry_name = str(input('\nWhat would you like to delete? ')) _entry_name = str(input('\nWhat would you like to delete? '))
@@ -382,13 +409,8 @@ def remove_data(): # deletes an entry or folder from both the client and the se
except FileNotFoundError: except FileNotFoundError:
print('\nAccess denied.\n') print('\nAccess denied.\n')
s_exit() s_exit()
if device_type == 'c': system(f"ssh -i '{path.expanduser('~/.ssh/sshyp')}' -p {port} {username_ssh}@{ip} \"python -c 'import shear"
system(f"ssh -p {port} {username_ssh}@{ip} \"rm -rf '{directory_ssh}{_entry_name}.gpg'\"") f"; shear.delete(\"'\"{_entry_name}\"'\")'\"")
try:
remove(f"{directory}{_entry_name}.gpg")
except FileNotFoundError:
print("\nThe file does not exist!\n")
s_exit()
# program start sequence # program start sequence
@@ -416,6 +438,7 @@ if argument != 'tweak':
port = ssh_info[2].replace('\n', '') port = ssh_info[2].replace('\n', '')
directory = str(ssh_info[3].replace('\n', '')) directory = str(ssh_info[3].replace('\n', ''))
directory_ssh = '/home/' + username_ssh + '/.password-pasture/' directory_ssh = '/home/' + username_ssh + '/.password-pasture/'
client_device_name = listdir(path.expanduser('~/.config/sshyp/devices'))[0]
except FileNotFoundError: except FileNotFoundError:
if device_type.lower() != 's': if device_type.lower() != 's':
print('\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') print('\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
@@ -498,7 +521,7 @@ else:
if argument != '' and argument != 'help' and argument != '--help' and argument != '-h' and argument != 'license' and \ if argument != '' and argument != 'help' and argument != '--help' and argument != '-h' and argument != 'license' and \
argument != 'add' and argument != 'sync' and argument != 'edit' and argument != 'copy username' and argument \ argument != 'add' and argument != 'sync' and argument != 'edit' and argument != 'copy username' and argument \
!= 'copy -u' and argument != 'copy password' and argument != 'copy -p' and argument != 'copy url' and argument \ != 'copy -u' and argument != 'copy password' and argument != 'copy -p' and argument != 'copy url' and argument \
!= 'copy -l' and argument != 'copy note' and argument != 'copy -n' and argument != 'copy' and argument != \ != 'copy -l' and argument != 'copy note' and argument != 'copy -n' and argument != 'copy' and argument \
'shear' and argument != '-rm' and argument != 'version' and argument != '-v' and error == 0 and \ != 'version' and argument != '-v' and error == 0 and \
argument.startswith('/') is False: argument.startswith('/') is False:
sync() sync()
+19 -4
View File
@@ -8,8 +8,23 @@ from os.path import expanduser
# utility functions # utility functions
def delete(_file_path, _client_device_name): def delete(_file_path):
remove(f"{expanduser('~/.password-pasture/')}{_file_path}") try:
remove(f"{expanduser('~/.password-pasture/')}{_file_path}.gpg")
except FileNotFoundError:
print('\nFile does not exist remotely.\n')
for _device_name in listdir(expanduser('~/.config/sshyp/devices')): for _device_name in listdir(expanduser('~/.config/sshyp/devices')):
open(f"{expanduser('~/.config/sshyp/deleted/')}{_file_path}.{_device_name}.del", 'w') open(f"{expanduser('~/.config/sshyp/deleted/')}{_file_path.replace('/', '@')}.{_device_name}.del", 'w')
remove(f"{expanduser('~/.config/sshyp/deleted/')}{_file_path}.{_client_device_name}.del")
def check(_client_device_name):
open(expanduser('~/.config/sshyp/deletion_database'), 'w').write('')
for _file in listdir(expanduser('~/.config/sshyp/deleted')):
_file_path = _file.replace('.gpg', '').replace('@', '/').split('.')[0]
_device = _file.replace('.gpg', '').split('.')[1]
if _device == _client_device_name:
try:
remove(f"{expanduser('~/.config/sshyp/deleted/')}{_file}")
except FileNotFoundError:
pass
open(expanduser('~/.config/sshyp/deletion_database'), 'a').write(_file_path + '.gpg\n')