Command-line client for Nextcloud’s Passman.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

116 lines
3.6 KiB

# pebble - Passman client
# Copyright (C) 2018 Damien Goutte-Gattat
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
from ConfigParser import SafeConfigParser
from getopt import getopt, GetoptError
from getpass import getpass
from incenp.pebble.server import Server
from incenp.pebble.cache import Vault
def die(msg):
sys.stderr.write("pbl: {}\n".format(msg))
sys.exit(1)
fields = [
('description', 'Description', None),
('tags', 'Tags', lambda tags: ', '.join([tag[u'text'] for tag in tags])),
('url', 'URL', None),
('username', 'Username', None),
('email', 'Email', None),
('password', 'Password', None)
]
def print_entry(cred, out=sys.stdout):
out.write("+---- {} -----\n".format(cred['label']))
for field_name, field_label, field_format in fields:
if cred[field_name]:
if field_format:
formatted_field = field_format(cred[field_name])
else:
formatted_field = cred[field_name]
out.write("| {}: {}\n".format(field_label, formatted_field))
if cred['custom_fields']:
for custom in cred['custom_fields']:
out.write("| {}: {}\n".format(custom[u'label'], custom[u'value']))
out.write("+----\n")
cached_key = None
def get_key():
global cached_key
if not cached_key:
cached_key = getpass()
return cached_key
def main(argv=None):
if not argv:
argv = sys.argv[1:]
try:
opts, args = getopt(argv, 'hvc:a:',
['help', 'version', 'config=', 'vault='])
except GetoptError as e:
die(e)
config = SafeConfigParser()
home_dir = os.getenv('HOME', default='')
config_dir = os.getenv('XDG_CONFIG_HOME',
default='{}/.config'.format(home_dir))
config_file = '{}/pebble/config'.format(config_dir)
data_dir = '{}/pebble'.format(os.getenv('XDG_DATA_HOME'))
vault_name = 'default'
for o, a in opts:
if o in ('-h', '--help'):
usage()
elif o in ('-v', '--version'):
version()
elif o in ('-c', '--config'):
config_file = a
elif o in ('-a', '--vault'):
vault_name = a
config.read(config_file)
if not config.has_section(vault_name):
die("No vault '{}' configured".format(vault_name))
server_section_name = config.get(vault_name, 'server')
if not config.has_section(server_section_name):
die("No server '{}' configured".format(server_section_name))
server = Server(config.get(server_section_name, 'host'),
config.get(server_section_name, 'user'),
config.get(server_section_name, 'password'))
vault = Vault(server, config.get(vault_name, 'vault'), data_dir, get_key)
vault.load()
if len(args) == 1:
creds = vault.search(args[0], decrypt=True)
for cred in creds:
print_entry(cred)
else:
creds = vault.list()
for cred in creds:
sys.stdout.write("{}\n".format(cred))
if __name__ == '__main__':
main()