pwman3 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. from __future__ import print_function
  23. import os
  24. import argparse
  25. import sys
  26. import re
  27. _saveconfig = True
  28. _db_warn = ("\n*** WARNNING: You are using the old database format"
  29. " which is insecure."
  30. " Please upgrade to the new database "
  31. " format. Do note: support for this DB format will be dropped in"
  32. " v0.5. This database format is on hold. No bugs are fixead"
  33. " Check the help (pwman3 -h) or look at the manpage which"
  34. " explains how to proceed. ***")
  35. def parser_options():
  36. parser = argparse.ArgumentParser(description=('pwman3 - a command line '
  37. 'password manager.'))
  38. parser.add_argument('-c', '--config', dest='cfile',
  39. default=os.path.expanduser("~/.pwman/config"),
  40. help='cofiguration file to read')
  41. parser.add_argument('-d', '--database', dest='dbase')
  42. parser.add_argument('-e', '--encryption', dest="algo",
  43. help=("Possible options are: AES(default), ARC2, ARC4,"
  44. " Blowfish, CAST, DES, DES3, IDEA, RC5"))
  45. parser.add_argument('-k', '--convert', dest='dbconvert',
  46. action='store_true', default=False,
  47. # os.path.expanduser('~/.pwman/pwman.db'),
  48. help=("Convert old DB format to version >= 0.4."
  49. " The database that will be converted is the"
  50. " one found in the config file, or the one given"
  51. " as command line argument."))
  52. parser.add_argument('-O', '--output', dest='output',
  53. default=os.path.expanduser('~/.pwman/pwman-newdb.db'),
  54. help=("The name of the newly created database after "
  55. "converting."))
  56. parser.add_argument('-t', '--test', help=("Run pwman from current"
  57. " directory"
  58. " without installation"),
  59. action="store_true")
  60. return parser
  61. def get_conf():
  62. config_dir = os.path.expanduser("~/.pwman")
  63. if not os.path.isdir(config_dir):
  64. os.mkdir(config_dir)
  65. if not os.path.exists(args.cfile):
  66. config.set_defaults(default_config)
  67. else:
  68. config.load(args.cfile)
  69. return config
  70. def set_xsel(config, OSX):
  71. if not OSX:
  72. xselpath = which("xsel")
  73. config.set_value("Global", "xsel", xselpath)
  74. elif OSX:
  75. pbcopypath = which("pbcopy")
  76. config.set_value("Global", "xsel", pbcopypath)
  77. def set_win_colors(config):
  78. if 'win' in sys.platform:
  79. try:
  80. import colorama
  81. colorama.init()
  82. except ImportError:
  83. config.set_value("Global", "colors", 'no')
  84. def set_umask(config):
  85. # set umask before creating/opening any files
  86. try:
  87. umask = config.get_value("Global", "umask")
  88. if re.search(r'^\d{4}$', umask):
  89. os.umask(int(umask))
  90. else:
  91. raise ValueError
  92. except ValueError:
  93. print("Could not determine umask from config!")
  94. sys.exit(2)
  95. def set_db(args):
  96. global _saveconfig
  97. if args.dbase:
  98. config.set_value("Database", "filename", args.dbase)
  99. _saveconfig = False
  100. def set_algorithm(args):
  101. global _saveconfig
  102. if args.algo:
  103. config.set_value("Encryption", "algorithm", args.algo)
  104. _saveconfig = False
  105. def get_conf_options(args, OSX):
  106. config = get_conf()
  107. xselpath = config.get_value("Global", "xselpath")
  108. if not xselpath:
  109. set_xsel(config, OSX)
  110. set_win_colors(config)
  111. set_db(args)
  112. set_umask(config)
  113. dbtype = config.get_value("Database", "type")
  114. if not dbtype:
  115. print("Could not read the Database type from the config!")
  116. sys.exit(1)
  117. return xselpath, dbtype
  118. def get_db_version(config, dbtype):
  119. if os.path.exists(config.get_value("Database", "filename")):
  120. dbver = pwman.data.factory.check_db_version(dbtype)
  121. if dbver < 0.4:
  122. print(_db_warn)
  123. else:
  124. dbver = 0.4
  125. return dbver
  126. def main(args):
  127. PwmanCliNew, OSX = get_ui_platform(sys.platform)
  128. xselpath, dbtype = get_conf_options(args, OSX)
  129. enc = CryptoEngine.get()
  130. dbver = get_db_version(config, dbtype)
  131. if args.dbconvert:
  132. dbconvertor = PwmanConvertDB(args, config)
  133. dbconvertor.run()
  134. sys.exit(0)
  135. if dbver < 0.4:
  136. sys.exit(1)
  137. db = pwman.data.factory.create(dbtype, dbver)
  138. cli = PwmanCliNew(db, xselpath, CLICallback)
  139. try:
  140. cli.cmdloop()
  141. except KeyboardInterrupt, e:
  142. print(e)
  143. if _saveconfig:
  144. try:
  145. config.save(args.cfile)
  146. except Exception, e:
  147. print ("Error: %s" % e)
  148. sys.exit(-1)
  149. if __name__ == '__main__':
  150. args = parser_options().parse_args()
  151. if args.test:
  152. sys.path.insert(0, os.getcwd())
  153. from pwman import default_config, which
  154. from pwman.ui import get_ui_platform
  155. from pwman.ui.tools import CLICallback
  156. import pwman.util.config as config
  157. import pwman.data.factory
  158. from pwman.data.convertdb import PwmanConvertDB
  159. from pwman.util.crypto import CryptoEngine
  160. main(args)