def do_command(params): if (params.command == 'q'): return False elif (params.command == 'l'): if (len(params.record_cache) == 0): print('No records') else: results = api.search_records(params, '') display.formatted_records(results) elif (params.command == 'lsf'): if (len(params.shared_folder_cache) == 0): print('No shared folders') else: results = api.search_shared_folders(params, '') display.formatted_shared_folders(results) elif (params.command[:2] == 'g '): if (api.is_shared_folder(params, params.command[2:])): sf = api.get_shared_folder(params, params.command[2:]) if sf: sf.display() else: r = api.get_record(params, params.command[2:]) if r: r.display() elif (params.command[:2] == 'r '): api.rotate_password(params, params.command[2:]) elif (params.command[:2] == 'd '): api.delete_record(params, params.command[2:]) elif (params.command == 'c'): print(chr(27) + "[2J") elif (params.command[:2] == 's '): results = api.search_records(params, params.command[2:]) display.formatted_records(results) elif (params.command[:2] == 'b '): results = api.search_records(params, params.command[2:]) for r in results: api.rotate_password(params, r.record_uid) elif (params.command[:3] == 'an '): api.append_notes(params, params.command[3:]) elif (params.command == 'd'): api.sync_down(params) elif (params.command == 'a'): api.add_record(params) elif (params.command == 'h'): display.formatted_history(stack) elif (params.command == 'debug'): if params.debug: params.debug = False print('Debug OFF') else: params.debug = True print('Debug ON') elif params.command == '': pass else: print('\n\nCommands:\n') print(' d ... download & decrypt data') print(' l ... list folders and titles') print(' lsf ... list shared folders') print(' s <regex> ... search with regular expression') print(' g <uid> ... get record or shared folder details for uid') print(' r <uid> ... rotate password for uid') print( ' b <regex> ... rotate password for matches of regular expression' ) print(' a ... add a new record interactively') print(' an <uid> ... append some notes to the specified record') print(' c ... clear the screen') print(' h ... show command history') print(' q ... quit') print('') if params.command: if params.command != 'h': stack.append(params.command) stack.reverse() return True
def export(params, file_format, filename, **export_args): api.sync_down(params) exporter = exporter_for_format(file_format)() # type: BaseExporter if export_args: if 'max_size' in export_args: exporter.max_size = int(export_args['max_size']) to_export = [] if exporter.has_shared_folders(): shfolders = [ api.get_shared_folder(params, sf_uid) for sf_uid in params.shared_folder_cache ] shfolders.sort(key=lambda x: x.name.lower(), reverse=False) for f in shfolders: fol = ImportSharedFolder() fol.uid = f.shared_folder_uid fol.path = get_folder_path(params, f.shared_folder_uid) fol.manage_users = f.default_manage_users fol.manage_records = f.default_manage_records fol.can_edit = f.default_can_edit fol.can_share = f.default_can_share fol.permissions = [] if f.teams: for team in f.teams: perm = ImportPermission() perm.uid = team['team_uid'] perm.name = team['name'] perm.manage_users = team['manage_users'] perm.manage_records = team['manage_records'] fol.permissions.append(perm) if f.users: for user in f.users: perm = ImportPermission() perm.name = user['username'] perm.manage_users = user['manage_users'] perm.manage_records = user['manage_records'] fol.permissions.append(perm) to_export.append(fol) sf_count = len(to_export) records = [ api.get_record(params, record_uid) for record_uid in params.record_cache ] records.sort(key=lambda x: x.title.lower(), reverse=False) for r in records: rec = ImportRecord() rec.uid = r.record_uid rec.title = r.title.strip('\x00') if r.title else '' rec.login = r.login.strip('\x00') if r.login else '' rec.password = r.password.strip('\x00') if r.password else '' rec.login_url = r.login_url.strip('\x00') if r.login_url else '' rec.notes = r.notes.strip('\x00') if r.notes else '' for cf in r.custom_fields: name = cf.get('name') value = cf.get('value') if name and value: rec.custom_fields[name] = value if r.totp: rec.custom_fields[TWO_FACTOR_CODE] = r.totp for folder_uid in find_folders(params, r.record_uid): if folder_uid in params.folder_cache: folder = get_import_folder(params, folder_uid, r.record_uid) if rec.folders is None: rec.folders = [] rec.folders.append(folder) if exporter.has_attachments() and r.attachments: rec.attachments = [] names = set() for a in r.attachments: orig_name = a.get('title') or a.get('name') or 'attachment' name = orig_name counter = 0 while name in names: counter += 1 name = "{0}-{1}".format(orig_name, counter) names.add(name) atta = KeeperAttachment(params, rec.uid) atta.file_id = a['id'] atta.name = name atta.size = a['size'] atta.key = base64.urlsafe_b64decode(a['key'] + '==') atta.mime = a.get('type') or '' rec.attachments.append(atta) to_export.append(rec) rec_count = len(to_export) - sf_count if len(to_export) > 0: exporter.execute(filename, to_export) params.queue_audit_event('exported_records', file_format=file_format) logging.info('%d records exported', rec_count)
def do_command(params): if (params.command == 'q'): return False elif (params.command == 'l'): if (len(params.record_cache) == 0): print('No records') else: results = api.search_records(params, '') display.formatted_records(results) elif (params.command == 'lsf'): if (len(params.shared_folder_cache) == 0): print('No shared folders') else: results = api.search_shared_folders(params, '') display.formatted_shared_folders(results) elif (params.command == 'lt'): if (len(params.team_cache) == 0): print('No teams') else: results = api.search_teams(params, '') display.formatted_teams(results) elif (params.command[:2] == 'g '): if (api.is_shared_folder(params, params.command[2:])): sf = api.get_shared_folder(params, params.command[2:]) if sf: sf.display() elif (api.is_team(params, params.command[2:])): team = api.get_team(params, params.command[2:]) if team: team.display() else: r = api.get_record(params, params.command[2:]) if r: r.display() elif (params.command[:2] == 'r '): api.rotate_password(params, params.command[2:]) elif (params.command[:2] == 'd '): api.delete_record(params, params.command[2:]) elif (params.command == 'c'): print(chr(27) + "[2J") elif (params.command[:2] == 's '): results = api.search_records(params, params.command[2:]) display.formatted_records(results) elif (params.command[:2] == 'b '): results = api.search_records(params, params.command[2:]) for r in results: api.rotate_password(params, r.record_uid) elif (params.command[:3] == 'an '): api.append_notes(params, params.command[3:]) elif (params.command == 'd'): api.sync_down(params) elif (params.command == 'a'): record = Record() while not record.title: record.title = input("... Title (req'd): ") record.folder = input("... Folder: ") record.login = input("... Login: "******"... Password: "******"... Login URL: ") record.notes = input("... Notes: ") while True: custom_dict = {} custom_dict['name'] = input("... Custom Field Name : ") if not custom_dict['name']: break custom_dict['value'] = input("... Custom Field Value : ") custom_dict['type'] = 'text' record.custom_fields.append(custom_dict) api.add_record(params, record) elif (params.command == 'h'): display.formatted_history(stack) elif (params.command == 'debug'): if params.debug: params.debug = False print('Debug OFF') else: params.debug = True print('Debug ON') elif params.command == '': pass else: print('\n\nShell Commands:\n') print(' d ... download & decrypt data') print(' l ... list folders and record titles') print(' lsf ... list shared folders') print(' lt ... list teams') print(' s <regex> ... search with regular expression') print(' g <uid> ... get record or shared folder details for uid') print(' r <uid> ... rotate password for uid') print( ' b <regex> ... rotate password for matches of regular expression' ) print(' a ... add a new record interactively') print(' an <uid> ... append some notes to the specified record') print(' c ... clear the screen') print(' h ... show command history') print(' q ... quit') print('') if params.command: if params.command != 'h': stack.append(params.command) stack.reverse() return True