pwman3 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. import subprocess as sp
  25. _saveconfig = True
  26. import argparse
  27. parser = argparse.ArgumentParser(description='pwman3 - a command line password'\
  28. +'manager.')
  29. parser.add_argument('-c','--config', dest='cfile',
  30. default=os.path.expanduser("~/.pwman/config"),
  31. help='cofiguretion file to read')
  32. parser.add_argument('-d', '--database', dest='dbase',
  33. default=os.path.expanduser("~/.pwman/pwman.db"))
  34. parser.add_argument('-e', '--encryption', dest="algo", default="Blowfish",
  35. help="Possible options are: AES, ARC2, ARC4, "\
  36. +"Blowfish(default) CAST, DES, DES3, IDEA, RC5")
  37. parser.add_argument('-t','--test', help="Run pwman from current directory \
  38. without installation", action="store_true")
  39. args = parser.parse_args()
  40. if args.test:
  41. import sys
  42. sys.path.insert(0,os.getcwd())
  43. from pwman.util.crypto import CryptoEngine
  44. import getopt
  45. import sys
  46. if 'darwin' in sys.platform:
  47. from pwman.ui.cli import PwmanCliMac as PwmanCli
  48. OSX = True
  49. else:
  50. from pwman.ui.cli import PwmanCli
  51. OSX = False
  52. import pwman.util.config as config
  53. import pwman.data.factory
  54. config_file=args.cfile
  55. def which(cmd):
  56. _, cmdname = os.path.split(cmd)
  57. for path in os.environ["PATH"].split(os.pathsep):
  58. cmd = os.path.join(path, cmdname)
  59. if os.path.isfile(cmd) and os.access(cmd, os.X_OK):
  60. return cmd
  61. return None
  62. try:
  63. config_dir = os.path.expanduser("~/.pwman")
  64. if not os.path.isdir(config_dir):
  65. os.mkdir(config_dir)
  66. config_file = os.path.join(config_dir, "config")
  67. # set cls_timout to negative number (e.g. -1) to disable
  68. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  69. 'cls_timeout': '5'
  70. },
  71. 'Database': {'type': 'SQLite',
  72. 'filename': os.path.join(config_dir,
  73. "pwman.db")},
  74. 'Encryption': {'algorithm': 'Blowfish'},
  75. 'Readline': {'history': os.path.join(config_dir,
  76. "history")}
  77. }
  78. config.set_defaults(default_config)
  79. if os.path.exists(config_file):
  80. config.load(config_file)
  81. xselpath = config.get_value("Global", "xselpath")
  82. elif not OSX :
  83. xselpath = which("xsel")
  84. config.set_value("Global", "xsel", xselpath)
  85. elif OSX:
  86. pbcopypath = which("pbcopy")
  87. config.set_value("Global", "xsel", pbcopypath)
  88. if args.dbase != config.get_value('Database', "filename"):
  89. config.set_value("Database", "filename", args.dbase)
  90. _saveconfig = False
  91. if args.algo != "Blowfish":
  92. config.set_value("Encryption", "algorithm", args.algo)
  93. _saveconfig = False
  94. # set umask before creating/opening any files
  95. umask = int(config.get_value("Global", "umask"))
  96. os.umask(umask)
  97. enc = CryptoEngine.get()
  98. dbtype = config.get_value("Database", "type")
  99. db = pwman.data.factory.create(dbtype)
  100. cli = PwmanCli(db, xselpath)
  101. except SystemExit, e:
  102. sys.exit(e)
  103. except Exception, e:
  104. print "Error: %s" % (e)
  105. sys.exit(-1)
  106. try:
  107. try:
  108. cli.cmdloop()
  109. except KeyboardInterrupt, e:
  110. print e
  111. finally:
  112. try:
  113. if _saveconfig:
  114. config.save(config_file)
  115. except Exception, e:
  116. print "Error: %s" % (e)
  117. sys.exit(-1)