pwman3 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 password'
  27. + ' 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('-t', '--test', help="Run pwman from current directory \
  36. without installation", action="store_true")
  37. args = parser.parse_args()
  38. import sys
  39. if args.test:
  40. sys.path.insert(0, os.getcwd())
  41. from pwman.util.crypto import CryptoEngine
  42. if 'darwin' in sys.platform:
  43. from pwman.ui.cli import PwmanCliMac as PwmanCli
  44. from pwman.ui.cli import PwmanCliMacNew as PwmanCliNew
  45. OSX = True
  46. else:
  47. from pwman.ui.cli import PwmanCli
  48. from pwman.ui.cli import PwmanCliNew
  49. OSX = False
  50. import pwman.util.config as config
  51. import pwman.data.factory
  52. config_file = args.cfile
  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):
  58. return cmd
  59. return None
  60. try:
  61. config_dir = os.path.expanduser("~/.pwman")
  62. if not os.path.isdir(config_dir):
  63. os.mkdir(config_dir)
  64. config_file = os.path.join(config_dir, "config")
  65. # set cls_timout to negative number (e.g. -1) to disable
  66. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  67. 'cls_timeout': '5'
  68. },
  69. 'Database': {'type': 'SQLite',
  70. 'filename': os.path.join(config_dir,
  71. "pwman.db")},
  72. 'Encryption': {'algorithm': 'AES'},
  73. 'Readline': {'history': os.path.join(config_dir,
  74. "history")}
  75. }
  76. config.set_defaults(default_config)
  77. if os.path.exists(config_file):
  78. config.load(config_file)
  79. xselpath = config.get_value("Global", "xselpath")
  80. elif not OSX:
  81. xselpath = which("xsel")
  82. config.set_value("Global", "xsel", xselpath)
  83. elif OSX:
  84. pbcopypath = which("pbcopy")
  85. config.set_value("Global", "xsel", pbcopypath)
  86. if args.dbase:
  87. config.set_value("Database", "filename", args.dbase)
  88. _saveconfig = False
  89. if args.algo:
  90. config.set_value("Encryption", "algorithm", args.algo)
  91. _saveconfig = False
  92. # set umask before creating/opening any files
  93. umask = int(config.get_value("Global", "umask"))
  94. os.umask(umask)
  95. enc = CryptoEngine.get()
  96. dbtype = config.get_value("Database", "type")
  97. # if it is done here, we could do the following:
  98. # if db.ver == 0.4 :
  99. # db = pwman.data.factory.create(dbtyp, new_version)
  100. # else:
  101. # we use the old code untouched ... insecure, but
  102. # keeps backwards compatibility ...
  103. # if the database file exists check it's version
  104. # else: force version 0.4
  105. if os.path.exists(config.get_value("Database", "filename")):
  106. dbver = pwman.data.factory.check_db_version(dbtype)
  107. dbver = float(dbver.strip("\'"))
  108. else:
  109. dbver = 0.4
  110. # the method create could create an old instance that
  111. # accepts cPickle object or new style instance that
  112. # accepts only strings.
  113. # The user should be STRONGLY Prompted to CONVERT the
  114. # database to the new format using a command line tool.
  115. # version 0.5 pwman will depreciate that old and insecure
  116. # code ...
  117. db = pwman.data.factory.create(dbtype, dbver)
  118. if dbver >= 0.4:
  119. cli = PwmanCliNew(db, xselpath)
  120. elif dbver < 0.4:
  121. cli = PwmanCli(db, xselpath)
  122. except SystemExit, e:
  123. sys.exit(e)
  124. try:
  125. try:
  126. cli.cmdloop()
  127. except KeyboardInterrupt, e:
  128. print e
  129. finally:
  130. try:
  131. if _saveconfig:
  132. config.save(config_file)
  133. except Exception, e:
  134. print "Error: %s" % (e)
  135. sys.exit(-1)