pwman3 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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) 2006 Ivan Kelly <ivan@ivankelly.net>
  19. #============================================================================
  20. from pwman.util.crypto import CryptoEngine
  21. from pwman.ui.cli import PwmanCli
  22. import pwman.util.config as config
  23. import pwman.data.factory
  24. import getopt,sys
  25. import os
  26. import os.path
  27. _saveconfig = True
  28. def print_help():
  29. print """Syntax: %s [options]
  30. -c, --config FILE Read configuration from FILE
  31. -d, --database FILE Use FILE as database
  32. -e, --encryption ALGO Use ALGO to encrypt data
  33. Possible options are:
  34. AES, ARC2, ARC4, Blowfish(default),
  35. CAST, DES, DES3, IDEA, RC5
  36. -h, --help Display this help and exit
  37. Please report bugs at http://pwman.bleurgh.com
  38. """ % (sys.argv[0])
  39. try:
  40. config_dir = os.path.expanduser("~/.pwman")
  41. if not os.path.isdir(config_dir):
  42. os.mkdir(config_dir)
  43. config_file = os.path.join(config_dir, "config")
  44. default_config = {'Global': {'umask': '0077', 'colors' : 'yes'},
  45. 'Database':{'type':'SQLite',
  46. 'filename':os.path.join(config_dir, "pwman.db")},
  47. 'Encryption':{'algorithm': 'Blowfish'},
  48. 'Readline': {'history':os.path.join(config_dir, "history")}
  49. }
  50. config.set_defaults(default_config)
  51. opts, args = getopt.getopt(sys.argv[1:], 'c:d:e:h',
  52. ["config=", "database=", "encryption=", "help"])
  53. for o in opts:
  54. if o[0] == '-c' or o[0] == "--config":
  55. config_file = os.path.expanduser(o[1])
  56. if o[0] == '-h' or o[0] == "--help":
  57. print_help()
  58. sys.exit(0)
  59. if os.path.exists(config_file):
  60. config.load(config_file)
  61. for o in opts:
  62. if o[0] == '-d' or o[0] == "--database":
  63. config.set_value("Database", "filename",
  64. os.path.expanduser(o[1]))
  65. _saveconfig=False
  66. if o[0] == '-e' or o[0] == "--encryption":
  67. config.set_value("Encryption", "algorithm", o[1])
  68. _saveconfig=False
  69. # set umask before creating/opening any files
  70. umask = int(config.get_value("Global", "umask"))
  71. os.umask(umask)
  72. enc = CryptoEngine.get()
  73. type = config.get_value("Database", "type")
  74. db = pwman.data.factory.create(type)
  75. cli = PwmanCli(db)
  76. except SystemExit, e:
  77. sys.exit(e)
  78. except Exception, e:
  79. print "Error: %s" % (e)
  80. sys.exit(-1)
  81. try:
  82. try:
  83. cli.cmdloop()
  84. except KeyboardInterrupt, e:
  85. print
  86. finally:
  87. try:
  88. if _saveconfig:
  89. config.save(config_file)
  90. except Exception, e:
  91. print "Error: %s" % (e)
  92. sys.exit(-1)