pwman3 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/env python
  2. #============================================================================
  3. # This file is part of Pwman3.
  4. #
  5. # Pwman3 is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License, version 2
  7. # as published by the Free Software Foundation;
  8. #
  9. # Pwman3 is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Pwman3; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. #============================================================================
  18. # Copyright (C) 2012 Oz Nahum <nahumoz@gmail.com>
  19. #============================================================================
  20. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  21. #============================================================================
  22. import os
  23. import os.path
  24. import subprocess as sp
  25. _saveconfig = True
  26. import argparse
  27. parser = argparse.ArgumentParser(description='pwman3 - a command line password'\
  28. +'manager.')
  29. parser.add_argument('-c','--config', dest='cfile',
  30. default=os.path.expanduser("~/.pwman/config"),
  31. help='cofiguretion file to read')
  32. parser.add_argument('-d', '--database', dest='dbase',
  33. default=os.path.expanduser("~/.pwman/pwman.db"))
  34. parser.add_argument('-e', '--encryption', dest="algo", default="Blowfish",
  35. help="Possible options are: AES, ARC2, ARC4, "\
  36. +"Blowfish(default) CAST, DES, DES3, IDEA, RC5")
  37. parser.add_argument('-t','--test', help="Run pwman from current directory \
  38. without installation", action="store_true")
  39. args = parser.parse_args()
  40. if args.test:
  41. import sys
  42. sys.path.insert(0,os.getcwd())
  43. from pwman.util.crypto import CryptoEngine
  44. import getopt
  45. import sys
  46. if 'darwin' in sys.platform:
  47. from pwman.ui.cli import PwmanCliMac as PwmanCli
  48. OSX=True
  49. else:
  50. from pwman.ui.cli import PwmanCli
  51. import pwman.util.config as config
  52. import pwman.data.factory
  53. config_file=args.cfile
  54. def which(cmd):
  55. _, cmdname = os.path.split(cmd)
  56. for path in os.environ["PATH"].split(os.pathsep):
  57. cmd = os.path.join(path, cmdname)
  58. if os.path.isfile(cmd) and os.access(cmd, os.X_OK):
  59. return cmd
  60. return None
  61. try:
  62. config_dir = os.path.expanduser("~/.pwman")
  63. if not os.path.isdir(config_dir):
  64. os.mkdir(config_dir)
  65. config_file = os.path.join(config_dir, "config")
  66. default_config = {'Global': {'umask': '0100', 'colors': 'yes'},
  67. 'Database': {'type': 'SQLite',
  68. 'filename': os.path.join(config_dir,
  69. "pwman.db")},
  70. 'Encryption': {'algorithm': 'Blowfish'},
  71. 'Readline': {'history': os.path.join(config_dir,
  72. "history")}
  73. }
  74. config.set_defaults(default_config)
  75. if os.path.exists(config_file):
  76. config.load(config_file)
  77. xselpath = config.get_value("Global", "xselpath")
  78. elif not OSX :
  79. xselpath = which("xsel")
  80. config.set_value("Global", "xsel", xselpath)
  81. elif OSX:
  82. pbcopypath = which("pbcopy")
  83. config.set_value("Global", "xsel", pbcopypath)
  84. if args.dbase != config.get_value('Database', "filename"):
  85. config.set_value("Database", "filename", args.dbase)
  86. _saveconfig = False
  87. if args.algo != "Blowfish":
  88. config.set_value("Encryption", "algorithm", args.algo)
  89. _saveconfig = False
  90. # set umask before creating/opening any files
  91. umask = int(config.get_value("Global", "umask"))
  92. os.umask(umask)
  93. enc = CryptoEngine.get()
  94. dbtype = config.get_value("Database", "type")
  95. db = pwman.data.factory.create(dbtype)
  96. cli = PwmanCli(db, xselpath)
  97. except SystemExit, e:
  98. sys.exit(e)
  99. except Exception, e:
  100. print "Error: %s" % (e)
  101. sys.exit(-1)
  102. try:
  103. try:
  104. cli.cmdloop()
  105. except KeyboardInterrupt, e:
  106. print e
  107. finally:
  108. try:
  109. if _saveconfig:
  110. config.save(config_file)
  111. except Exception, e:
  112. print "Error: %s" % (e)
  113. sys.exit(-1)