__init__.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. appname = "Pwman3"
  29. try:
  30. version = pkg_resources.get_distribution('pwman3').version
  31. except pkg_resources.DistributionNotFound: # pragma: no cover
  32. version = "0.5"
  33. website = "http://github.com/pwman3/pwman3"
  34. author = "Oz Nahum"
  35. authoremail = "nahumoz@gmail.com"
  36. description = "Pwman -a command line password management application."
  37. keywords = "password management sqlite crypto"
  38. _db_warn = ("pwman3 detected that you are using the old database format"
  39. " which is insecure."
  40. " pwman3 will try to automatically convert the database now."
  41. "\n"
  42. "If you choose not to convert the database, pwman3, will quit."
  43. "\nYou can check the help (pwman3 -h) or look at the manpage how to convert "
  44. " the database manually."
  45. )
  46. def which(cmd):
  47. _, cmdname = os.path.split(cmd)
  48. for path in os.environ["PATH"].split(os.pathsep):
  49. cmd = os.path.join(path, cmdname)
  50. if os.path.isfile(cmd) and os.access(cmd, os.X_OK): # pragma: no cover
  51. return cmd
  52. config_dir = os.path.expanduser("~/.pwman")
  53. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  54. 'cls_timeout': '5',
  55. 'save': 'True'
  56. },
  57. 'Database': {'type': 'SQLite',
  58. 'filename': os.path.join(config_dir,
  59. "pwman.db")},
  60. 'Encryption': {'algorithm': 'AES'},
  61. 'Readline': {'history': os.path.join(config_dir,
  62. "history")}
  63. }
  64. def parser_options():
  65. parser = argparse.ArgumentParser(description=('pwman3 - a command line '
  66. 'password manager.'))
  67. parser.add_argument('-c', '--config', dest='cfile',
  68. default=os.path.expanduser("~/.pwman/config"),
  69. help='cofiguration file to read')
  70. parser.add_argument('-d', '--database', dest='dbase')
  71. parser.add_argument('-e', '--encryption', dest="algo",
  72. help=("Possible options are: AES(default), ARC2, ARC4,"
  73. " Blowfish, CAST, DES, DES3, IDEA, RC5"))
  74. parser.add_argument('-k', '--convert', dest='dbconvert',
  75. action='store_true', default=False,
  76. # os.path.expanduser('~/.pwman/pwman.db'),
  77. help=("Convert old DB format to version >= 0.4."
  78. " The database that will be converted is the"
  79. " one found in the config file, or the one given"
  80. " as command line argument."))
  81. parser.add_argument('-O', '--output', dest='output',
  82. #default=os.path.expanduser('~/.pwman/pwman-newdb.db'),
  83. help=("The name of the newly created database after "
  84. "converting."))
  85. return parser
  86. def get_conf(args):
  87. config_dir = os.path.expanduser("~/.pwman")
  88. if not os.path.isdir(config_dir):
  89. os.mkdir(config_dir)
  90. if not os.path.exists(args.cfile):
  91. config.set_defaults(default_config)
  92. else:
  93. config.load(args.cfile)
  94. return config
  95. def set_xsel(config, OSX):
  96. if not OSX:
  97. xselpath = which("xsel")
  98. config.set_value("Global", "xsel", xselpath)
  99. elif OSX:
  100. pbcopypath = which("pbcopy")
  101. config.set_value("Global", "xsel", pbcopypath)
  102. def set_win_colors(config):
  103. if 'win' in sys.platform:
  104. try:
  105. import colorama
  106. colorama.init()
  107. except ImportError:
  108. config.set_value("Global", "colors", 'no')
  109. def set_umask(config):
  110. # set umask before creating/opening any files
  111. try:
  112. umask = config.get_value("Global", "umask")
  113. if re.search(r'^\d{4}$', umask):
  114. os.umask(int(umask))
  115. else:
  116. raise ValueError
  117. except ValueError:
  118. print("Could not determine umask from config!")
  119. sys.exit(2)
  120. def set_db(args):
  121. if args.dbase:
  122. config.set_value("Database", "filename", args.dbase)
  123. config.set_value("Global", "save", "False")
  124. def set_algorithm(args, config):
  125. if args.algo:
  126. config.set_value("Encryption", "algorithm", args.algo)
  127. config.set_value("Global", "save", "False")
  128. def get_conf_options(args, OSX):
  129. config = get_conf(args)
  130. xselpath = config.get_value("Global", "xsel")
  131. if not xselpath:
  132. set_xsel(config, OSX)
  133. set_win_colors(config)
  134. set_db(args)
  135. set_umask(config)
  136. set_algorithm(args, config)
  137. dbtype = config.get_value("Database", "type")
  138. if not dbtype:
  139. print("Could not read the Database type from the config!")
  140. sys.exit(1)
  141. return xselpath, dbtype
  142. def get_db_version(config, dbtype, args):
  143. if os.path.exists(config.get_value("Database", "filename")):
  144. dbver = data.factory.check_db_version(dbtype)
  145. if dbver < 0.4 and not args.dbconvert:
  146. print(_db_warn)
  147. else:
  148. dbver = 0.4
  149. return dbver