__init__.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. #============================================================================
  17. # Copyright (C) 2012 Oz Nahum <nahumoz@gmail.com>
  18. #============================================================================
  19. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  20. #============================================================================
  21. import os
  22. import pkg_resources
  23. import argparse
  24. from util import config
  25. import sys
  26. import re
  27. import data.factory
  28. from pwman.data.database import __DB_FORMAT__
  29. import colorama
  30. appname = "pwman3"
  31. try:
  32. version = pkg_resources.get_distribution('pwman3').version
  33. except pkg_resources.DistributionNotFound: # pragma: no cover
  34. version = "0.5"
  35. website = "http://pwman3.github.io/pwman3/"
  36. author = "Oz Nahum"
  37. authoremail = "nahumoz@gmail.com"
  38. description = "a command line password management application."
  39. keywords = "password management sqlite crypto"
  40. long_description = ("Pwman3 aims to provide a simple but powerful command "
  41. "line interface for password management.\nIt allows one "
  42. "to store your password in a SQLite database locked by "
  43. "a\nmaster password which can be encrypted with different "
  44. "algorithms (e.g AES, Blowfish, DES3, IDEA, etc.).")
  45. _db_warn = ("pwman3 detected that you are using the old database format"
  46. " which is insecure."
  47. " pwman3 will try to automatically convert the database now."
  48. "\n"
  49. "If you choose not to convert the database, pwman3, will quit."
  50. "\nYou can check the help (pwman3 -h) or look at the manpage how "
  51. "to convert the database manually."
  52. )
  53. def which(cmd):
  54. _, cmdname = os.path.split(cmd)
  55. for path in os.environ["PATH"].split(os.pathsep):
  56. cmd = os.path.join(path, cmdname)
  57. if os.path.isfile(cmd) and os.access(cmd, os.X_OK): # pragma: no cover
  58. return cmd
  59. config_dir = os.path.expanduser("~/.pwman")
  60. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  61. 'cls_timeout': '5',
  62. 'save': 'True'
  63. },
  64. 'Database': {'type': 'SQLite',
  65. 'filename': os.path.join(config_dir,
  66. "pwman.db")},
  67. 'Encryption': {'algorithm': 'AES'},
  68. 'Readline': {'history': os.path.join(config_dir,
  69. "history")}
  70. }
  71. def parser_options(formatter_class=argparse.HelpFormatter):
  72. parser = argparse.ArgumentParser(prog=appname,
  73. description=description,
  74. formatter_class=formatter_class)
  75. parser.add_argument('-c', '--config', dest='cfile',
  76. default=os.path.expanduser("~/.pwman/config"),
  77. help='cofiguration file to read')
  78. parser.add_argument('-d', '--database', dest='dbase')
  79. parser.add_argument('-e', '--encryption', dest="algo",
  80. help=("Possible options are: AES(default), ARC2, ARC4,"
  81. " Blowfish, CAST, DES, DES3, IDEA, RC5"))
  82. parser.add_argument('-k', '--convert', dest='dbconvert',
  83. action='store_true', default=False,
  84. # os.path.expanduser('~/.pwman/pwman.db'),
  85. help=("Convert old DB format to version >= 0.4."
  86. " The database that will be converted is the"
  87. " one found in the config file, or the one given"
  88. " as command line argument."))
  89. parser.add_argument('-O', '--output', dest='output',
  90. #default=os.path.expanduser('~/.pwman/pwman-newdb.db'),
  91. help=("The name of the newly created database after "
  92. "converting."))
  93. return parser
  94. def get_conf_file(args):
  95. config_dir = os.path.expanduser("~/.pwman")
  96. if not os.path.isdir(config_dir):
  97. os.mkdir(config_dir)
  98. if not os.path.exists(args.cfile):
  99. config.set_defaults(default_config)
  100. else:
  101. config.load(args.cfile)
  102. return config
  103. def set_xsel(config, OSX):
  104. if not OSX:
  105. xselpath = which("xsel")
  106. config.set_value("Global", "xsel", xselpath)
  107. elif OSX:
  108. pbcopypath = which("pbcopy")
  109. config.set_value("Global", "xsel", pbcopypath)
  110. def set_win_colors(config): # pragma: no cover
  111. if 'win' in sys.platform:
  112. colorama.init()
  113. def set_umask(config):
  114. # set umask before creating/opening any files
  115. try:
  116. umask = config.get_value("Global", "umask")
  117. if re.search(r'^\d{4}$', umask):
  118. os.umask(int(umask))
  119. else:
  120. raise ValueError
  121. except ValueError:
  122. print("Could not determine umask from config!")
  123. sys.exit(2)
  124. def set_db(args):
  125. if args.dbase:
  126. config.set_value("Database", "filename", args.dbase)
  127. config.set_value("Global", "save", "False")
  128. def set_algorithm(args, config):
  129. if args.algo:
  130. config.set_value("Encryption", "algorithm", args.algo)
  131. config.set_value("Global", "save", "False")
  132. def get_conf_options(args, OSX):
  133. config = get_conf_file(args)
  134. xselpath = config.get_value("Global", "xsel")
  135. if not xselpath:
  136. set_xsel(config, OSX)
  137. set_win_colors(config)
  138. set_db(args)
  139. set_umask(config)
  140. set_algorithm(args, config)
  141. dbtype = config.get_value("Database", "type")
  142. if not dbtype:
  143. raise Exception("Could not read the Database type from the config!")
  144. return xselpath, dbtype
  145. def get_db_version(config, dbtype, args):
  146. if os.path.exists(config.get_value("Database", "filename")):
  147. dbver = data.factory.check_db_version(dbtype)
  148. if dbver < 0.4 and not args.dbconvert:
  149. print(_db_warn)
  150. else:
  151. dbver = __DB_FORMAT__
  152. return dbver