| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 | #!/usr/bin/python#============================================================================# This file is part of Pwman3.## Pwman3 is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License, version 2# as published by the Free Software Foundation;## Pwman3 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 Pwman3; if not, write to the Free Software# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA#============================================================================# Copyright (C) 2012 Oz Nahum <nahumoz@gmail.com>#============================================================================# Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>#============================================================================from pwman.util.crypto import CryptoEngineimport getoptimport sysif 'darwin' in sys.platform:    from pwman.ui.cli import PwmanCliMacelse:    from pwman.ui.cli import PwmanCliimport pwman.util.config as configimport pwman.data.factoryimport osimport os.pathimport subprocess as sp_saveconfig = Truedef print_help():    print """Syntax: %s [options] -c, --config FILE      Read configuration from FILE -d, --database FILE    Use FILE as database -e, --encryption ALGO  Use ALGO to encrypt data                        Possible options are:                         AES, ARC2, ARC4, Blowfish(default),                         CAST, DES, DES3, IDEA, RC5 -h, --help             Display this help and exitPlease report bugs at https://github.com/pwman3/pwman3""" % (sys.argv[0])def which(cmd):    _, cmdname = os.path.split(cmd)    for path in os.environ["PATH"].split(os.pathsep):        cmd = os.path.join(path, cmdname)        if os.path.isfile(cmd) and os.access(cmd, os.X_OK):            return cmd    return Nonetry:    config_dir = os.path.expanduser("~/.pwman")    if not os.path.isdir(config_dir):        os.mkdir(config_dir)    config_file = os.path.join(config_dir, "config")    default_config = {'Global': {'umask': '0100', 'colors': 'yes'},                      'Database': {'type': 'SQLite',                                   'filename': os.path.join(config_dir,                                                            "pwman.db")},                      'Encryption': {'algorithm': 'Blowfish'},                      'Readline': {'history': os.path.join(config_dir,                                                           "history")}                      }    config.set_defaults(default_config)    opts, args = getopt.getopt(sys.argv[1:], 'c:d:e:h',                               ["config=", "database=",                               "encryption=", "help"])    for o in opts:        if o[0] == '-c' or o[0] == "--config":            config_file = os.path.expanduser(o[1])        if o[0] == '-h' or o[0] == "--help":            print_help()            sys.exit(0)    # if no config exists yet, look for xsel    if os.path.exists(config_file):        config.load(config_file)        xselpath = config.get_value("Global", "xselpath")    else:        xselpath = which("xsel")        config.set_value("Global", "xsel", xselpath)    for o in opts:        if o[0] == '-d' or o[0] == "--database":            config.set_value("Database", "filename",                             os.path.expanduser(o[1]))            _saveconfig = False        if o[0] == '-e' or o[0] == "--encryption":            config.set_value("Encryption", "algorithm", o[1])            _saveconfig = False    # set umask before creating/opening any files    umask = int(config.get_value("Global", "umask"))    os.umask(umask)    enc = CryptoEngine.get()    dbtype = config.get_value("Database", "type")    db = pwman.data.factory.create(dbtype)    cli = PwmanCli(db, xselpath)except SystemExit, e:    sys.exit(e)except Exception, e:    print "Error: %s" % (e)    sys.exit(-1)try:    try:        cli.cmdloop()    except KeyboardInterrupt, e:        printfinally:    try:        if _saveconfig:            config.save(config_file)    except Exception, e:        print "Error: %s" % (e)        sys.exit(-1)
 |