pwman3 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. _saveconfig = True
  25. import argparse
  26. parser = argparse.ArgumentParser(description=('pwman3 - a command line '
  27. 'password manager.'))
  28. parser.add_argument('-c', '--config', dest='cfile',
  29. default=os.path.expanduser("~/.pwman/config"),
  30. help='cofiguration file to read')
  31. parser.add_argument('-d', '--database', dest='dbase')
  32. parser.add_argument('-e', '--encryption', dest="algo",
  33. help=("Possible options are: AES(default), ARC2, ARC4, "
  34. "Blowfish, CAST, DES, DES3, IDEA, RC5"))
  35. parser.add_argument('-k', '--convert', dest='dbconvert',
  36. action='store_true', default=False,
  37. # os.path.expanduser('~/.pwman/pwman.db'),
  38. help=("Convert old DB format to version >= 0.4."
  39. " The database that will be converted is the"
  40. " one found in the config file, or the one given"
  41. " as command line argument.")
  42. )
  43. parser.add_argument('-t', '--test', help="Run pwman from current directory \
  44. without installation", action="store_true")
  45. args = parser.parse_args()
  46. import sys
  47. if args.test:
  48. sys.path.insert(0, os.getcwd())
  49. from pwman.util.crypto import CryptoEngine
  50. if 'darwin' in sys.platform:
  51. from pwman.ui.mac import PwmanCliMac as PwmanCli
  52. from pwman.ui.mac import PwmanCliMacNew as PwmanCliNew
  53. OSX = True
  54. elif 'win' in sys.platform:
  55. from pwman.ui.cli import PwmanCli
  56. from pwman.ui.win import PwmanCliWinNew as PwmanCliNew
  57. OSX = False
  58. else:
  59. from pwman.ui.cli import PwmanCli
  60. from pwman.ui.cli import PwmanCliNew
  61. OSX = False
  62. import pwman.util.config as config
  63. import pwman.data.factory
  64. from pwman.data.convertdb import PwmanConvertDB
  65. config_file = args.cfile
  66. def which(cmd):
  67. _, cmdname = os.path.split(cmd)
  68. for path in os.environ["PATH"].split(os.pathsep):
  69. cmd = os.path.join(path, cmdname)
  70. if os.path.isfile(cmd) and os.access(cmd, os.X_OK):
  71. return cmd
  72. return None
  73. try:
  74. config_dir = os.path.expanduser("~/.pwman")
  75. if not os.path.isdir(config_dir):
  76. os.mkdir(config_dir)
  77. config_file = os.path.join(config_dir, "config")
  78. # set cls_timout to negative number (e.g. -1) to disable
  79. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  80. 'cls_timeout': '5'
  81. },
  82. 'Database': {'type': 'SQLite',
  83. 'filename': os.path.join(config_dir,
  84. "pwman.db")},
  85. 'Encryption': {'algorithm': 'AES'},
  86. 'Readline': {'history': os.path.join(config_dir,
  87. "history")}
  88. }
  89. config.set_defaults(default_config)
  90. if 'win' in sys.platform:
  91. try:
  92. import colorama
  93. colorama.init()
  94. except ImportError:
  95. config.set_value("Global", "colors", 'no')
  96. if os.path.exists(config_file):
  97. config.load(config_file)
  98. xselpath = config.get_value("Global", "xselpath")
  99. elif not OSX:
  100. xselpath = which("xsel")
  101. config.set_value("Global", "xsel", xselpath)
  102. elif OSX:
  103. pbcopypath = which("pbcopy")
  104. config.set_value("Global", "xsel", pbcopypath)
  105. if args.dbase:
  106. config.set_value("Database", "filename", args.dbase)
  107. _saveconfig = False
  108. if args.algo:
  109. config.set_value("Encryption", "algorithm", args.algo)
  110. _saveconfig = False
  111. # set umask before creating/opening any files
  112. umask = int(config.get_value("Global", "umask"))
  113. os.umask(umask)
  114. enc = CryptoEngine.get()
  115. dbtype = config.get_value("Database", "type")
  116. # if it is done here, we could do the following:
  117. # if db.ver == 0.4 :
  118. # db = pwman.data.factory.create(dbtyp, new_version)
  119. # else:
  120. # we use the old code untouched ... insecure, but
  121. # keeps backwards compatibility ...
  122. # if the database file exists check it's version
  123. # else: force version 0.4
  124. if os.path.exists(config.get_value("Database", "filename")):
  125. dbver = pwman.data.factory.check_db_version(dbtype)
  126. dbver = float(dbver.strip("\'"))
  127. else:
  128. dbver = 0.4
  129. # the method create could create an old instance that
  130. # accepts cPickle object or new style instance that
  131. # accepts only strings.
  132. # The user should be STRONGLY Prompted to CONVERT the
  133. # database to the new format using a command line tool.
  134. # version 0.5 pwman will depreciate that old and insecure
  135. # code ...
  136. if args.dbconvert:
  137. dbconvertor = PwmanConvertDB(args, config)
  138. status = dbconvertor.run()
  139. sys.exit(status)
  140. db = pwman.data.factory.create(dbtype, dbver)
  141. if dbver >= 0.4:
  142. cli = PwmanCliNew(db, xselpath)
  143. elif dbver < 0.4:
  144. cli = PwmanCli(db, xselpath)
  145. except SystemExit, e:
  146. sys.exit(e)
  147. try:
  148. try:
  149. cli.cmdloop()
  150. except KeyboardInterrupt, e:
  151. print e
  152. finally:
  153. try:
  154. if _saveconfig:
  155. config.save(config_file)
  156. except Exception, e:
  157. print "Error: %s" % (e)
  158. sys.exit(-1)