pwman3 6.2 KB

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