pwman3_old 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/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 pwman.util.crypto import CryptoEngine
  23. import getopt
  24. import sys
  25. if 'darwin' in sys.platform:
  26. from pwman.ui.cli import PwmanCliMac
  27. else:
  28. from pwman.ui.cli import PwmanCli
  29. import pwman.util.config as config
  30. import pwman.data.factory
  31. import os
  32. import os.path
  33. import subprocess as sp
  34. _saveconfig = True
  35. def print_help():
  36. print """Syntax: %s [options]
  37. -c, --config FILE Read configuration from FILE
  38. -d, --database FILE Use FILE as database
  39. -e, --encryption ALGO Use ALGO to encrypt data
  40. Possible options are:
  41. AES, ARC2, ARC4, Blowfish(default),
  42. CAST, DES, DES3, IDEA, RC5
  43. -h, --help Display this help and exit
  44. Please report bugs at https://github.com/pwman3/pwman3
  45. """ % (sys.argv[0])
  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):
  51. return cmd
  52. return None
  53. try:
  54. config_dir = os.path.expanduser("~/.pwman")
  55. if not os.path.isdir(config_dir):
  56. os.mkdir(config_dir)
  57. config_file = os.path.join(config_dir, "config")
  58. default_config = {'Global': {'umask': '0100', 'colors': 'yes'},
  59. 'Database': {'type': 'SQLite',
  60. 'filename': os.path.join(config_dir,
  61. "pwman.db")},
  62. 'Encryption': {'algorithm': 'Blowfish'},
  63. 'Readline': {'history': os.path.join(config_dir,
  64. "history")}
  65. }
  66. config.set_defaults(default_config)
  67. opts, args = getopt.getopt(sys.argv[1:], 'c:d:e:h',
  68. ["config=", "database=",
  69. "encryption=", "help"])
  70. for o in opts:
  71. if o[0] == '-c' or o[0] == "--config":
  72. config_file = os.path.expanduser(o[1])
  73. if o[0] == '-h' or o[0] == "--help":
  74. print_help()
  75. sys.exit(0)
  76. # if no config exists yet, look for xsel
  77. if os.path.exists(config_file):
  78. config.load(config_file)
  79. xselpath = config.get_value("Global", "xselpath")
  80. else:
  81. xselpath = which("xsel")
  82. config.set_value("Global", "xsel", xselpath)
  83. for o in opts:
  84. if o[0] == '-d' or o[0] == "--database":
  85. config.set_value("Database", "filename",
  86. os.path.expanduser(o[1]))
  87. _saveconfig = False
  88. if o[0] == '-e' or o[0] == "--encryption":
  89. config.set_value("Encryption", "algorithm", o[1])
  90. _saveconfig = False
  91. # set umask before creating/opening any files
  92. umask = int(config.get_value("Global", "umask"))
  93. os.umask(umask)
  94. enc = CryptoEngine.get()
  95. dbtype = config.get_value("Database", "type")
  96. db = pwman.data.factory.create(dbtype)
  97. cli = PwmanCli(db, xselpath)
  98. except SystemExit, e:
  99. sys.exit(e)
  100. except Exception, e:
  101. print "Error: %s" % (e)
  102. sys.exit(-1)
  103. try:
  104. try:
  105. cli.cmdloop()
  106. except KeyboardInterrupt, e:
  107. print
  108. finally:
  109. try:
  110. if _saveconfig:
  111. config.save(config_file)
  112. except Exception, e:
  113. print "Error: %s" % (e)
  114. sys.exit(-1)