123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- from __future__ import print_function
- import os
- import os.path
- import argparse
- import sys
- import re
- from pwman.ui.tools import CLICallback
- import pwman.util.config as config
- import pwman.data.factory
- from pwman.data.convertdb import PwmanConvertDB
- from pwman.util.crypto import CryptoEngine
- from pwman import default_config, which
- _saveconfig = True
- def get_ui_platform(platform):
- if 'darwin' in platform:
- from pwman.ui.mac import PwmanCliMacNew as PwmanCliNew
- OSX = True
- elif 'win' in platform:
- from pwman.ui.win import PwmanCliWinNew as PwmanCliNew
- OSX = False
- else:
- from pwman.ui.cli import PwmanCliNew
- OSX = False
- return PwmanCliNew, OSX
- def parser_options():
- parser = argparse.ArgumentParser(description=('pwman3 - a command line '
- 'password manager.'))
- parser.add_argument('-c', '--config', dest='cfile',
- default=os.path.expanduser("~/.pwman/config"),
- help='cofiguration file to read')
- parser.add_argument('-d', '--database', dest='dbase')
- parser.add_argument('-e', '--encryption', dest="algo",
- help=("Possible options are: AES(default), ARC2, ARC4, "
- "Blowfish, CAST, DES, DES3, IDEA, RC5"))
- parser.add_argument('-k', '--convert', dest='dbconvert',
- action='store_true', default=False,
-
- help=("Convert old DB format to version >= 0.4."
- " The database that will be converted is the"
- " one found in the config file, or the one given"
- " as command line argument."))
- parser.add_argument('-t', '--test', help=("Run pwman from current directory "
- "without installation"),
- action="store_true")
- return parser
- def get_conf():
- config_dir = os.path.expanduser("~/.pwman")
- if not os.path.isdir(config_dir):
- os.mkdir(config_dir)
- if not os.path.exists(args.cfile):
- config.set_defaults(default_config)
- else:
- config.load(args.cfile)
- return config
- def set_xsel(config):
- if not OSX:
- xselpath = which("xsel")
- config.set_value("Global", "xsel", xselpath)
- elif OSX:
- pbcopypath = which("pbcopy")
- config.set_value("Global", "xsel", pbcopypath)
- def set_win_colors(config):
- if 'win' in sys.platform:
- try:
- import colorama
- colorama.init()
- except ImportError:
- config.set_value("Global", "colors", 'no')
- def set_umask(config):
-
- try:
- umask = config.get_value("Global", "umask")
- if re.search(r'^\d{4}$', umask):
- os.umask(int(umask))
- else:
- raise ValueError
- except ValueError:
- print("Could not determine umask from config!")
- sys.exit(2)
- def set_db(args):
- global _saveconfig
- if args.dbase:
- config.set_value("Database", "filename", args.dbase)
- _saveconfig = False
- def set_algorithm(args):
- global _saveconfig
- if args.algo:
- config.set_value("Encryption", "algorithm", args.algo)
- _saveconfig = False
- def get_conf_options(args, OSX):
- config = get_conf()
- xselpath = config.get_value("Global", "xselpath")
- if not xselpath:
- set_xsel(config)
- set_win_colors(config)
- set_db(args)
- set_umask(config)
- dbtype = config.get_value("Database", "type")
- if not dbtype:
- print("Could not read the Database type from the config!")
- sys.exit(1)
- return xselpath, dbtype
- if __name__ == '__main__':
- args = parser_options().parse_args()
- if args.test:
- sys.path.insert(0, os.getcwd())
- PwmanCliNew, OSX = get_ui_platform(sys.platform)
- xselpath, dbtype = get_conf_options(args, OSX)
- enc = CryptoEngine.get()
- if os.path.exists(config.get_value("Database", "filename")):
- dbver = pwman.data.factory.check_db_version(dbtype)
- dbver = float(dbver.strip("\'"))
- else:
- dbver = 0.4
- if args.dbconvert:
- dbconvertor = PwmanConvertDB(args, config)
- status = dbconvertor.run()
- sys.exit(status)
- db = pwman.data.factory.create(dbtype, dbver)
- if dbver >= 0.4:
- cli = PwmanCliNew(db, xselpath, CLICallback)
- elif dbver < 0.4:
- print("\n*** WARNNING: You are using the old database format"
- " which is insecure."
- " Please upgrade to the new database "
- " format. Do note: support for this DB format will be dropped in"
- " v0.5. This database format is on hold. No bugs are fixead"
- " Check the help (pwman3 -h) or look at the manpage which"
- " explains how to proceed. ***")
- sys.exit(0)
- try:
- cli.cmdloop()
- except KeyboardInterrupt, e:
- print(e)
- if _saveconfig:
- try:
- config.save(args.cfile)
- except Exception, e:
- print ("Error: %s" % e)
- sys.exit(-1)
|